Bug 1874684 - Part 28: Return DateDuration from DifferenceISODateTime. r=mgaudet
[gecko.git] / python / mozbuild / mozbuild / test / test_dotproperties.py
blob4e7a4377999d040f04779ede657a49494959e466
1 # -*- coding: utf-8 -*-
3 import os
4 import unittest
6 import mozpack.path as mozpath
7 from mozunit import main
8 from six import StringIO
10 from mozbuild.dotproperties import DotProperties
12 test_data_path = mozpath.abspath(mozpath.dirname(__file__))
13 test_data_path = mozpath.join(test_data_path, "data")
16 class TestDotProperties(unittest.TestCase):
17 def test_get(self):
18 contents = StringIO(
19 """
20 key=value
21 """
23 p = DotProperties(contents)
24 self.assertEqual(p.get("missing"), None)
25 self.assertEqual(p.get("missing", "default"), "default")
26 self.assertEqual(p.get("key"), "value")
28 def test_update(self):
29 contents = StringIO(
30 """
31 old=old value
32 key=value
33 """
35 p = DotProperties(contents)
36 self.assertEqual(p.get("old"), "old value")
37 self.assertEqual(p.get("key"), "value")
39 new_contents = StringIO(
40 """
41 key=new value
42 """
44 p.update(new_contents)
45 self.assertEqual(p.get("old"), "old value")
46 self.assertEqual(p.get("key"), "new value")
48 def test_get_list(self):
49 contents = StringIO(
50 """
51 list.0=A
52 list.1=B
53 list.2=C
55 order.1=B
56 order.0=A
57 order.2=C
58 """
60 p = DotProperties(contents)
61 self.assertEqual(p.get_list("missing"), [])
62 self.assertEqual(p.get_list("list"), ["A", "B", "C"])
63 self.assertEqual(p.get_list("order"), ["A", "B", "C"])
65 def test_get_list_with_shared_prefix(self):
66 contents = StringIO(
67 """
68 list.0=A
69 list.1=B
70 list.2=C
72 list.sublist.1=E
73 list.sublist.0=D
74 list.sublist.2=F
76 list.sublist.second.0=G
78 list.other.0=H
79 """
81 p = DotProperties(contents)
82 self.assertEqual(p.get_list("list"), ["A", "B", "C"])
83 self.assertEqual(p.get_list("list.sublist"), ["D", "E", "F"])
84 self.assertEqual(p.get_list("list.sublist.second"), ["G"])
85 self.assertEqual(p.get_list("list.other"), ["H"])
87 def test_get_dict(self):
88 contents = StringIO(
89 """
90 A.title=title A
92 B.title=title B
93 B.url=url B
95 C=value
96 """
98 p = DotProperties(contents)
99 self.assertEqual(p.get_dict("missing"), {})
100 self.assertEqual(p.get_dict("A"), {"title": "title A"})
101 self.assertEqual(p.get_dict("B"), {"title": "title B", "url": "url B"})
102 with self.assertRaises(ValueError):
103 p.get_dict("A", required_keys=["title", "url"])
104 with self.assertRaises(ValueError):
105 p.get_dict("missing", required_keys=["key"])
106 # A key=value pair is considered to root an empty dict.
107 self.assertEqual(p.get_dict("C"), {})
108 with self.assertRaises(ValueError):
109 p.get_dict("C", required_keys=["missing_key"])
111 def test_get_dict_with_shared_prefix(self):
112 contents = StringIO(
114 A.title=title A
115 A.subdict.title=title A subdict
117 B.title=title B
118 B.url=url B
119 B.subdict.title=title B subdict
120 B.subdict.url=url B subdict
123 p = DotProperties(contents)
124 self.assertEqual(p.get_dict("A"), {"title": "title A"})
125 self.assertEqual(p.get_dict("B"), {"title": "title B", "url": "url B"})
126 self.assertEqual(p.get_dict("A.subdict"), {"title": "title A subdict"})
127 self.assertEqual(
128 p.get_dict("B.subdict"),
129 {"title": "title B subdict", "url": "url B subdict"},
132 def test_get_dict_with_value_prefix(self):
133 contents = StringIO(
135 A.default=A
136 A.default.B=B
137 A.default.B.ignored=B ignored
138 A.default.C=C
139 A.default.C.ignored=C ignored
142 p = DotProperties(contents)
143 self.assertEqual(p.get("A.default"), "A")
144 # This enumerates the properties.
145 self.assertEqual(p.get_dict("A.default"), {"B": "B", "C": "C"})
146 # They can still be fetched directly.
147 self.assertEqual(p.get("A.default.B"), "B")
148 self.assertEqual(p.get("A.default.C"), "C")
150 def test_unicode(self):
151 contents = StringIO(
153 # Danish.
154 # #### ~~ Søren Munk Skrøder, sskroeder - 2009-05-30 @ #mozmae
156 # Korean.
157 A.title=한메일
159 # Russian.
160 list.0 = test
161 list.1 = Яндекс
164 p = DotProperties(contents)
165 self.assertEqual(p.get_dict("A"), {"title": "한메일"})
166 self.assertEqual(p.get_list("list"), ["test", "Яндекс"])
168 def test_valid_unicode_from_file(self):
169 # The contents of valid.properties is identical to the contents of the
170 # test above. This specifically exercises reading from a file.
171 p = DotProperties(os.path.join(test_data_path, "valid.properties"))
172 self.assertEqual(p.get_dict("A"), {"title": "한메일"})
173 self.assertEqual(p.get_list("list"), ["test", "Яндекс"])
175 def test_bad_unicode_from_file(self):
176 # The contents of bad.properties is not valid Unicode; see the comments
177 # in the file itself for details.
178 with self.assertRaises(UnicodeDecodeError):
179 DotProperties(os.path.join(test_data_path, "bad.properties"))
182 if __name__ == "__main__":
183 main()