Bug 1874684 - Part 28: Return DateDuration from DifferenceISODateTime. r=mgaudet
[gecko.git] / python / mozbuild / mozbuild / test / test_containers.py
blob862c244f11808e96e49a0a570f288b6471fd9000
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/.
5 import unittest
6 from collections import OrderedDict, defaultdict
8 from mozunit import main
10 from mozbuild.util import (
11 KeyedDefaultDict,
12 List,
13 ReadOnlyDefaultDict,
14 ReadOnlyDict,
15 ReadOnlyKeyedDefaultDict,
16 ReadOnlyNamespace,
20 class TestReadOnlyNamespace(unittest.TestCase):
21 def test_basic(self):
22 test = ReadOnlyNamespace(foo=1, bar=2)
24 self.assertEqual(test.foo, 1)
25 self.assertEqual(test.bar, 2)
26 self.assertEqual(
27 sorted(i for i in dir(test) if not i.startswith("__")), ["bar", "foo"]
30 with self.assertRaises(AttributeError):
31 test.missing
33 with self.assertRaises(Exception):
34 test.foo = 2
36 with self.assertRaises(Exception):
37 del test.foo
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):
48 def test_basic(self):
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):
57 test["missing"]
59 with self.assertRaises(Exception):
60 test["baz"] = True
62 def test_update(self):
63 original = {"foo": 1, "bar": 2}
65 test = ReadOnlyDict(original)
67 with self.assertRaises(Exception):
68 test.update(foo=2)
70 self.assertEqual(original, test)
72 def test_del(self):
73 original = {"foo": 1, "bar": 2}
75 test = ReadOnlyDict(original)
77 with self.assertRaises(Exception):
78 del test["foo"]
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):
97 test["foo"] = True
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])
111 test += [4, 5, 6]
112 self.assertIsInstance(test, List)
113 self.assertEqual(test, [1, 2, 3, 4, 5, 6])
115 test = test + [7, 8]
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):
123 test += "string"
125 def test_none(self):
126 """As a special exception, we allow None to be treated as an empty
127 list."""
128 test = List([1, 2, 3])
130 test += None
131 self.assertEqual(test, [1, 2, 3])
133 test = test + None
134 self.assertIsInstance(test, List)
135 self.assertEqual(test, [1, 2, 3])
137 with self.assertRaises(ValueError):
138 test += False
140 with self.assertRaises(ValueError):
141 test = test + False
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")
185 test["foo"] = 2
186 test["qux"] = None
187 test["baz"] = "foo"
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")
206 copy = dict(test)
208 with self.assertRaises(Exception):
209 test["foo"] = 2
211 with self.assertRaises(Exception):
212 test["qux"] = None
214 with self.assertRaises(Exception):
215 test["baz"] = "foo"
217 self.assertEqual(test, copy)
219 self.assertEqual(len(test), 3)
222 if __name__ == "__main__":
223 main()