1
|
|
|
import os |
2
|
|
|
import re |
3
|
|
|
import unittest |
4
|
|
|
from collections import OrderedDict |
5
|
|
|
|
6
|
|
|
from coalib.settings.Setting import ( |
7
|
|
|
Setting, path, path_list, url, typed_dict, typed_list, typed_ordered_dict, |
8
|
|
|
glob, glob_list) |
9
|
|
|
from coalib.parsing.Globbing import glob_escape |
10
|
|
|
|
11
|
|
|
|
12
|
|
|
class SettingTest(unittest.TestCase): |
|
|
|
|
13
|
|
|
|
14
|
|
|
def test_construction(self): |
15
|
|
|
self.assertRaises(ValueError, Setting, "", 2, 2) |
|
|
|
|
16
|
|
|
self.assertRaises(TypeError, Setting, "", "", "", from_cli=5) |
|
|
|
|
17
|
|
|
|
18
|
|
|
def test_path(self): |
19
|
|
|
self.uut = Setting("key", " 22\n", "." + os.path.sep, True) |
|
|
|
|
20
|
|
|
self.assertEqual(path(self.uut), |
|
|
|
|
21
|
|
|
os.path.abspath(os.path.join(".", "22"))) |
22
|
|
|
|
23
|
|
|
abspath = os.path.abspath(".") |
24
|
|
|
self.uut = Setting("key", re.escape(abspath)) |
|
|
|
|
25
|
|
|
self.assertEqual(path(self.uut), abspath) |
26
|
|
|
|
27
|
|
|
self.uut = Setting("key", " 22", "") |
28
|
|
|
self.assertRaises(ValueError, path, self.uut) |
|
|
|
|
29
|
|
|
self.assertEqual(path(self.uut, |
30
|
|
|
origin="test" + os.path.sep), |
31
|
|
|
os.path.abspath(os.path.join("test", "22"))) |
32
|
|
|
|
33
|
|
|
def test_glob(self): |
34
|
|
|
self.uut = Setting("key", ".", |
35
|
|
|
origin=os.path.join("test (1)", "somefile")) |
36
|
|
|
self.assertEqual(glob(self.uut), |
|
|
|
|
37
|
|
|
glob_escape(os.path.abspath("test (1)"))) |
38
|
|
|
|
39
|
|
|
def test_path_list(self): |
40
|
|
|
abspath = os.path.abspath(".") |
41
|
|
|
# Need to escape backslashes since we use list conversion |
42
|
|
|
self.uut = Setting("key", "., " + abspath.replace("\\", "\\\\"), |
43
|
|
|
origin=os.path.join("test", "somefile")) |
44
|
|
|
self.assertEqual(path_list(self.uut), |
|
|
|
|
45
|
|
|
[os.path.abspath(os.path.join("test", ".")), abspath]) |
|
|
|
|
46
|
|
|
|
47
|
|
|
def test_url(self): |
48
|
|
|
uut = Setting("key", "http://google.com") |
49
|
|
|
self.assertEqual(url(uut), "http://google.com") |
|
|
|
|
50
|
|
|
|
51
|
|
|
with self.assertRaises(ValueError): |
|
|
|
|
52
|
|
|
uut = Setting("key", "abc") |
53
|
|
|
url(uut) |
54
|
|
|
|
55
|
|
|
def test_glob_list(self): |
56
|
|
|
abspath = glob_escape(os.path.abspath(".")) |
57
|
|
|
# Need to escape backslashes since we use list conversion |
58
|
|
|
self.uut = Setting("key", "., " + abspath.replace("\\", "\\\\"), |
59
|
|
|
origin=os.path.join("test (1)", "somefile")) |
60
|
|
|
self.assertEqual( |
61
|
|
|
glob_list(self.uut), |
|
|
|
|
62
|
|
|
[glob_escape(os.path.abspath(os.path.join("test (1)", "."))), |
63
|
|
|
abspath]) |
|
|
|
|
64
|
|
|
|
65
|
|
|
def test_typed_list(self): |
66
|
|
|
self.uut = Setting("key", "1, 2, 3") |
67
|
|
|
self.assertEqual(typed_list(int)(self.uut), |
|
|
|
|
68
|
|
|
[1, 2, 3]) |
69
|
|
|
|
70
|
|
|
with self.assertRaises(ValueError): |
|
|
|
|
71
|
|
|
self.uut = Setting("key", "1, a, 3") |
72
|
|
|
typed_list(int)(self.uut) |
73
|
|
|
|
74
|
|
|
def test_typed_dict(self): |
75
|
|
|
self.uut = Setting("key", "1, 2: t, 3") |
76
|
|
|
self.assertEqual(typed_dict(int, str, None)(self.uut), |
|
|
|
|
77
|
|
|
{1: None, 2: "t", 3: None}) |
78
|
|
|
|
79
|
|
|
with self.assertRaises(ValueError): |
|
|
|
|
80
|
|
|
self.uut = Setting("key", "1, a, 3") |
81
|
|
|
typed_dict(int, str, "")(self.uut) |
82
|
|
|
|
83
|
|
|
def test_typed_ordered_dict(self): |
84
|
|
|
self.uut = Setting("key", "1, 2: t, 3") |
85
|
|
|
self.assertEqual(typed_ordered_dict(int, str, None)(self.uut), |
|
|
|
|
86
|
|
|
OrderedDict([(1, None), (2, "t"), (3, None)])) |
87
|
|
|
|
88
|
|
|
with self.assertRaises(ValueError): |
|
|
|
|
89
|
|
|
self.uut = Setting("key", "1, a, 3") |
90
|
|
|
typed_ordered_dict(int, str, "")(self.uut) |
91
|
|
|
|
92
|
|
|
def test_inherited_conversions(self): |
93
|
|
|
self.uut = Setting("key", " 22\n", ".", True) |
94
|
|
|
self.assertEqual(str(self.uut), "22") |
|
|
|
|
95
|
|
|
self.assertEqual(int(self.uut), 22) |
96
|
|
|
self.assertRaises(ValueError, bool, self.uut) |
|
|
|
|
97
|
|
|
|