|
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 |
|
3 # file, You can obtain one at http://mozilla.org/MPL/2.0/. |
|
4 |
|
5 import unittest |
|
6 |
|
7 from cuddlefish.property_parser import parse, MalformedLocaleFileError |
|
8 |
|
9 class TestParser(unittest.TestCase): |
|
10 |
|
11 def test_parse(self): |
|
12 lines = [ |
|
13 # Comments are striped only if `#` is the first non-space character |
|
14 "sharp=#can be in value", |
|
15 "# comment", |
|
16 "#key=value", |
|
17 " # comment2", |
|
18 |
|
19 "keyWithNoValue=", |
|
20 "valueWithSpaces= ", |
|
21 "valueWithMultilineSpaces= \\", |
|
22 " \\", |
|
23 " ", |
|
24 |
|
25 # All spaces before/after are striped |
|
26 " key = value ", |
|
27 "key2=value2", |
|
28 # Keys can contain '%' |
|
29 "%s key=%s value", |
|
30 |
|
31 # Accept empty lines |
|
32 "", |
|
33 " ", |
|
34 |
|
35 # Multiline string must use backslash at end of lines |
|
36 "multi=line\\", "value", |
|
37 # With multiline string, left spaces are stripped ... |
|
38 "some= spaces\\", " are\\ ", " stripped ", |
|
39 # ... but not right spaces, except the last line! |
|
40 "but=not \\", "all of \\", " them ", |
|
41 |
|
42 # Explicit [other] plural definition |
|
43 "explicitPlural[one] = one", |
|
44 "explicitPlural[other] = other", |
|
45 |
|
46 # Implicit [other] plural definition |
|
47 "implicitPlural[one] = one", |
|
48 "implicitPlural = other", # This key is the [other] one |
|
49 ] |
|
50 # Ensure that all lines end with a `\n` |
|
51 # And that strings are unicode ones (parser code relies on it) |
|
52 lines = [unicode(l + "\n") for l in lines] |
|
53 pairs = parse(lines) |
|
54 expected = { |
|
55 "sharp": "#can be in value", |
|
56 |
|
57 "key": "value", |
|
58 "key2": "value2", |
|
59 "%s key": "%s value", |
|
60 |
|
61 "keyWithNoValue": "", |
|
62 "valueWithSpaces": "", |
|
63 "valueWithMultilineSpaces": "", |
|
64 |
|
65 "multi": "linevalue", |
|
66 "some": "spacesarestripped", |
|
67 "but": "not all of them", |
|
68 |
|
69 "implicitPlural": { |
|
70 "one": "one", |
|
71 "other": "other" |
|
72 }, |
|
73 "explicitPlural": { |
|
74 "one": "one", |
|
75 "other": "other" |
|
76 }, |
|
77 } |
|
78 self.assertEqual(pairs, expected) |
|
79 |
|
80 def test_exceptions(self): |
|
81 self.failUnlessRaises(MalformedLocaleFileError, parse, |
|
82 ["invalid line with no key value"]) |
|
83 self.failUnlessRaises(MalformedLocaleFileError, parse, |
|
84 ["plural[one]=plural with no [other] value"]) |
|
85 self.failUnlessRaises(MalformedLocaleFileError, parse, |
|
86 ["multiline with no last empty line=\\"]) |
|
87 self.failUnlessRaises(MalformedLocaleFileError, parse, |
|
88 ["=no key"]) |
|
89 self.failUnlessRaises(MalformedLocaleFileError, parse, |
|
90 [" =only spaces in key"]) |
|
91 |
|
92 if __name__ == "__main__": |
|
93 unittest.main() |