1 # -*- coding: utf-8 -*-
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
):
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
):
35 p
= DotProperties(contents
)
36 self
.assertEqual(p
.get("old"), "old value")
37 self
.assertEqual(p
.get("key"), "value")
39 new_contents
= StringIO(
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
):
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
):
76 list.sublist.second.0=G
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
):
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
):
115 A.subdict.title=title A subdict
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"})
128 p
.get_dict("B.subdict"),
129 {"title": "title B subdict", "url": "url B subdict"},
132 def test_get_dict_with_value_prefix(self
):
137 A.default.B.ignored=B ignored
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
):
154 # #### ~~ Søren Munk Skrøder, sskroeder - 2009-05-30 @ #mozmae
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__":