1 # This Source Code Form is subject to the terms of the Mozilla Public
2 # License, v. 2.0. If a copy of the MPL was not distributed with this file,
3 # You can obtain one at http://mozilla.org/MPL/2.0/.
6 from collections
import OrderedDict
, defaultdict
8 from mozunit
import main
10 from mozbuild
.util
import (
15 ReadOnlyKeyedDefaultDict
,
20 class TestReadOnlyNamespace(unittest
.TestCase
):
22 test
= ReadOnlyNamespace(foo
=1, bar
=2)
24 self
.assertEqual(test
.foo
, 1)
25 self
.assertEqual(test
.bar
, 2)
27 sorted(i
for i
in dir(test
) if not i
.startswith("__")), ["bar", "foo"]
30 with self
.assertRaises(AttributeError):
33 with self
.assertRaises(Exception):
36 with self
.assertRaises(Exception):
39 self
.assertEqual(test
, test
)
40 self
.assertEqual(test
, ReadOnlyNamespace(foo
=1, bar
=2))
41 self
.assertNotEqual(test
, ReadOnlyNamespace(foo
="1", bar
=2))
42 self
.assertNotEqual(test
, ReadOnlyNamespace(foo
=1, bar
=2, qux
=3))
43 self
.assertNotEqual(test
, ReadOnlyNamespace(foo
=1, qux
=3))
44 self
.assertNotEqual(test
, ReadOnlyNamespace(foo
=3, bar
="42"))
47 class TestReadOnlyDict(unittest
.TestCase
):
49 original
= {"foo": 1, "bar": 2}
51 test
= ReadOnlyDict(original
)
53 self
.assertEqual(original
, test
)
54 self
.assertEqual(test
["foo"], 1)
56 with self
.assertRaises(KeyError):
59 with self
.assertRaises(Exception):
62 def test_update(self
):
63 original
= {"foo": 1, "bar": 2}
65 test
= ReadOnlyDict(original
)
67 with self
.assertRaises(Exception):
70 self
.assertEqual(original
, test
)
73 original
= {"foo": 1, "bar": 2}
75 test
= ReadOnlyDict(original
)
77 with self
.assertRaises(Exception):
80 self
.assertEqual(original
, test
)
83 class TestReadOnlyDefaultDict(unittest
.TestCase
):
84 def test_simple(self
):
85 original
= {"foo": 1, "bar": 2}
87 test
= ReadOnlyDefaultDict(bool, original
)
89 self
.assertEqual(original
, test
)
91 self
.assertEqual(test
["foo"], 1)
93 def test_assignment(self
):
94 test
= ReadOnlyDefaultDict(bool, {})
96 with self
.assertRaises(Exception):
99 def test_defaults(self
):
100 test
= ReadOnlyDefaultDict(bool, {"foo": 1})
102 self
.assertEqual(test
["foo"], 1)
104 self
.assertEqual(test
["qux"], False)
107 class TestList(unittest
.TestCase
):
108 def test_add_list(self
):
109 test
= List([1, 2, 3])
112 self
.assertIsInstance(test
, List
)
113 self
.assertEqual(test
, [1, 2, 3, 4, 5, 6])
116 self
.assertIsInstance(test
, List
)
117 self
.assertEqual(test
, [1, 2, 3, 4, 5, 6, 7, 8])
119 def test_add_string(self
):
120 test
= List([1, 2, 3])
122 with self
.assertRaises(ValueError):
126 """As a special exception, we allow None to be treated as an empty
128 test
= List([1, 2, 3])
131 self
.assertEqual(test
, [1, 2, 3])
134 self
.assertIsInstance(test
, List
)
135 self
.assertEqual(test
, [1, 2, 3])
137 with self
.assertRaises(ValueError):
140 with self
.assertRaises(ValueError):
144 class TestOrderedDefaultDict(unittest
.TestCase
):
145 def test_simple(self
):
146 original
= OrderedDict(foo
=1, bar
=2)
148 test
= defaultdict(bool, original
)
150 self
.assertEqual(original
, test
)
152 self
.assertEqual(test
["foo"], 1)
154 self
.assertEqual(list(test
), ["foo", "bar"])
156 def test_defaults(self
):
157 test
= defaultdict(bool, {"foo": 1})
159 self
.assertEqual(test
["foo"], 1)
161 self
.assertEqual(test
["qux"], False)
163 self
.assertEqual(list(test
), ["foo", "qux"])
166 class TestKeyedDefaultDict(unittest
.TestCase
):
167 def test_simple(self
):
168 original
= {"foo": 1, "bar": 2}
170 test
= KeyedDefaultDict(lambda x
: x
, original
)
172 self
.assertEqual(original
, test
)
174 self
.assertEqual(test
["foo"], 1)
176 def test_defaults(self
):
177 test
= KeyedDefaultDict(lambda x
: x
, {"foo": 1})
179 self
.assertEqual(test
["foo"], 1)
181 self
.assertEqual(test
["qux"], "qux")
183 self
.assertEqual(test
["bar"], "bar")
189 self
.assertEqual(test
["foo"], 2)
191 self
.assertEqual(test
["qux"], None)
193 self
.assertEqual(test
["baz"], "foo")
196 class TestReadOnlyKeyedDefaultDict(unittest
.TestCase
):
197 def test_defaults(self
):
198 test
= ReadOnlyKeyedDefaultDict(lambda x
: x
, {"foo": 1})
200 self
.assertEqual(test
["foo"], 1)
202 self
.assertEqual(test
["qux"], "qux")
204 self
.assertEqual(test
["bar"], "bar")
208 with self
.assertRaises(Exception):
211 with self
.assertRaises(Exception):
214 with self
.assertRaises(Exception):
217 self
.assertEqual(test
, copy
)
219 self
.assertEqual(len(test
), 3)
222 if __name__
== "__main__":