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): |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
![]() |
|||
13 | |||
14 | def test_construction(self): |
||
15 | self.assertRaises(ValueError, Setting, "", 2, 2) |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
Comprehensibility
Best Practice
introduced
by
|
|||
16 | self.assertRaises(TypeError, Setting, "", "", "", from_cli=5) |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||
17 | |||
18 | def test_path(self): |
||
19 | self.uut = Setting("key", " 22\n", "." + os.path.sep, True) |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||
20 | self.assertEqual(path(self.uut), |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||
21 | os.path.abspath(os.path.join(".", "22"))) |
||
22 | |||
23 | abspath = os.path.abspath(".") |
||
24 | self.uut = Setting("key", re.escape(abspath)) |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||
25 | self.assertEqual(path(self.uut), abspath) |
||
26 | |||
27 | self.uut = Setting("key", " 22", "") |
||
28 | self.assertRaises(ValueError, path, self.uut) |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
Comprehensibility
Best Practice
introduced
by
|
|||
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), |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||
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), |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||
45 | [os.path.abspath(os.path.join("test", ".")), abspath]) |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||
46 | |||
47 | def test_url(self): |
||
48 | uut = Setting("key", "http://google.com") |
||
49 | self.assertEqual(url(uut), "http://google.com") |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||
50 | |||
51 | with self.assertRaises(ValueError): |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||
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), |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||
62 | [glob_escape(os.path.abspath(os.path.join("test (1)", "."))), |
||
63 | abspath]) |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||
64 | |||
65 | def test_typed_list(self): |
||
66 | self.uut = Setting("key", "1, 2, 3") |
||
67 | self.assertEqual(typed_list(int)(self.uut), |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||
68 | [1, 2, 3]) |
||
69 | |||
70 | with self.assertRaises(ValueError): |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||
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), |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||
77 | {1: None, 2: "t", 3: None}) |
||
78 | |||
79 | with self.assertRaises(ValueError): |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||
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), |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||
86 | OrderedDict([(1, None), (2, "t"), (3, None)])) |
||
87 | |||
88 | with self.assertRaises(ValueError): |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||
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") |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||
95 | self.assertEqual(int(self.uut), 22) |
||
96 | self.assertRaises(ValueError, bool, self.uut) |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
Comprehensibility
Best Practice
introduced
by
|
|||
97 |