-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Expand file tree
/
Copy pathtest_options.py
More file actions
135 lines (122 loc) · 5.85 KB
/
Copy pathtest_options.py
File metadata and controls
135 lines (122 loc) · 5.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# This is done due to the build system being how it is
try:
re = __import__("regex")
except ImportError:
import re
import unittest
from ...core.options import _mergeOpts, _normalizeOpts, Options
class TestOptions(unittest.TestCase):
@classmethod
def setUpClass(cls):
pass
def test_mergeOpts(self):
# should convert tuple to dict and merge child with parent
result = _mergeOpts((("a", 1), ("b", {"a": 2})), "b")
self.assertEqual(result.a, 2)
self.assertNotIn("b", result)
# should merge child option a with the parent options (dict)
result = _mergeOpts({"a": 1, "b": {"a": 2}}, "b")
self.assertEqual(result.a, 2)
self.assertNotIn("b", result)
# should include child option c and d with the parent options (dict)
result = _mergeOpts({"a": 1, "b": {"c": 2, "d": 3}}, "b")
self.assertEqual(result.a, 1)
self.assertEqual(result.c, 2)
self.assertEqual(result.d, 3)
self.assertNotIn("b", result)
# should merge child option a and include c as parent options (dict)
result = _mergeOpts({"a": 1, "b": {"a": 2, "c": 3}}, "b")
self.assertEqual(result.a, 2)
self.assertEqual(result.c, 3)
self.assertNotIn("b", result)
# should merge Options instance with child dict key
instance = Options()
instance.a = {"disabled": True}
result = _mergeOpts(instance, "a")
self.assertEqual(result.disabled, True)
self.assertNotIn("a", list(getattr(result, "__dict__", {})))
def test_normalizeOpts(self):
# should replace key with - to _ in dict
result = _normalizeOpts({"a-b": 1})
self.assertEqual(result["a_b"], 1)
self.assertNotIn("a-b", result)
# should replace key with - to _ in Options instance
instance = Options()
setattr(instance, "a-b", 1)
result = _normalizeOpts(instance)
self.assertEqual(result.a_b, 1)
self.assertNotIn("a-b", list(getattr(result, "__dict__", {})))
# should do nothing
result = _normalizeOpts({"a_b": 1})
self.assertEqual(result["a_b"], 1)
def test__get_boolean(self):
# should return default value since no option
self.assertEqual(Options()._get_boolean("a"), False)
# should return true as default since no option
self.assertEqual(Options()._get_boolean("a", True), True)
# should return false as in option
self.assertEqual(Options({"a": False})._get_boolean("a", True), False)
def test__get_characters(self):
# should return default value since no option
self.assertEqual(Options()._get_characters("a"), "")
# should return \'character\' as default since no option
self.assertEqual(Options()._get_characters("a", "character"), "character")
# should return \'char\' as in option
self.assertEqual(
Options({"a": "char"})._get_characters("a", "character"), "char"
)
def test__get_number(self):
# should return default value since no option
self.assertEqual(Options()._get_number("a"), 0)
# should return 1 as default since no option
self.assertEqual(Options()._get_number("a", 1), 1)
# should return 10 as in option
self.assertEqual(Options({"a": 10})._get_number("a", 1), 10)
# should return 0 for NaN as in option
self.assertEqual(Options({"a": "abc"})._get_number("a"), 0)
# should return 0 for NaN as in default
self.assertEqual(Options()._get_number("a", "abc"), 0)
def test__get_array(self):
# should return [] with no option
self.assertEqual(Options()._get_array("a"), [])
# should return [\'a\',\'b\'] as default since no option
self.assertEqual(Options()._get_array("a", ["a", "b"]), ["a", "b"])
# should return [\'c\',\'d\'] as in option
self.assertEqual(
Options({"a": ["c", "d"]})._get_array("a", ["a", "b"]), ["c", "d"]
)
# should return [\'c\',\'d\'] as in option comma separated
self.assertEqual(Options({"a": "c,d"})._get_array("a", ["a", "b"]), ["c", "d"])
def test__is_valid_selection(self):
# should return false with empty selection
self.assertEqual(Options()._is_valid_selection(["a", "b"], []), False)
# should return false with selection inexistent
self.assertEqual(Options()._is_valid_selection(["a", "b"], ["c"]), False)
# should return true with selection existent
self.assertEqual(Options()._is_valid_selection(["a", "b"], ["a", "b"]), True)
def test__get_selection_list(self):
# should raise error with empty selection
with self.assertRaisesRegex(ValueError, "Selection list cannot" + " be empty."):
Options()._get_selection_list("a", [])
# should raise error with invalid default
with self.assertRaisesRegex(ValueError, "Invalid Default Value!"):
Options()._get_selection_list("a", ["a", "b"], ["c"])
# should raise error with invalid option
with self.assertRaisesRegex(
ValueError, "^Invalid Option Value:" + " The option"
):
Options({"a": ["c", "d"]})._get_selection_list("a", ["a", "b"], ["a", "b"])
# should return [\'c\'] as in option
opts = Options({"c": ["c"]})
self.assertEqual(opts._get_selection_list("c", ["c", "d"], ["c"]), ["c"])
def test__get_selection(self):
# should raise error with multiple selection
with self.assertRaisesRegex(
ValueError, "^Invalid Option" + " Value: The option"
):
Options({"a": ["a", "b"]})._get_selection("a", ["a", "b"], ["a"])
# should return [\'a\'] as in option
options = Options({"a": ["a"]})
self.assertEqual(options._get_selection("a", ["a", "b"], ["a"]), "a")
if __name__ == "__main__":
unittest.main()