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