addon-sdk/source/python-lib/cuddlefish/tests/test_property_parser.py

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/addon-sdk/source/python-lib/cuddlefish/tests/test_property_parser.py	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,93 @@
     1.4 +# This Source Code Form is subject to the terms of the Mozilla Public
     1.5 +# License, v. 2.0. If a copy of the MPL was not distributed with this
     1.6 +# file, You can obtain one at http://mozilla.org/MPL/2.0/.
     1.7 +
     1.8 +import unittest
     1.9 +
    1.10 +from cuddlefish.property_parser import parse, MalformedLocaleFileError
    1.11 +
    1.12 +class TestParser(unittest.TestCase):
    1.13 +
    1.14 +    def test_parse(self):
    1.15 +        lines = [
    1.16 +          # Comments are striped only if `#` is the first non-space character
    1.17 +          "sharp=#can be in value",
    1.18 +          "# comment",
    1.19 +          "#key=value",
    1.20 +          "  # comment2",
    1.21 +
    1.22 +          "keyWithNoValue=",
    1.23 +          "valueWithSpaces=   ",
    1.24 +          "valueWithMultilineSpaces=  \\",
    1.25 +          "  \\",
    1.26 +          "  ",
    1.27 +
    1.28 +          # All spaces before/after are striped
    1.29 +          " key = value ",
    1.30 +          "key2=value2",
    1.31 +          # Keys can contain '%'
    1.32 +          "%s key=%s value",
    1.33 +
    1.34 +          # Accept empty lines
    1.35 +          "",
    1.36 +          "   ",
    1.37 +
    1.38 +          # Multiline string must use backslash at end of lines
    1.39 +          "multi=line\\", "value",
    1.40 +          # With multiline string, left spaces are stripped ...
    1.41 +          "some= spaces\\", " are\\  ", " stripped ",
    1.42 +          # ... but not right spaces, except the last line!
    1.43 +          "but=not \\", "all of \\", " them ",
    1.44 +
    1.45 +          # Explicit [other] plural definition
    1.46 +          "explicitPlural[one] = one",
    1.47 +          "explicitPlural[other] = other",
    1.48 +
    1.49 +          # Implicit [other] plural definition
    1.50 +          "implicitPlural[one] = one",
    1.51 +          "implicitPlural = other", # This key is the [other] one
    1.52 +        ]
    1.53 +        # Ensure that all lines end with a `\n`
    1.54 +        # And that strings are unicode ones (parser code relies on it)
    1.55 +        lines = [unicode(l + "\n") for l in lines]
    1.56 +        pairs = parse(lines)
    1.57 +        expected = {
    1.58 +          "sharp": "#can be in value",
    1.59 +
    1.60 +          "key": "value",
    1.61 +          "key2": "value2",
    1.62 +          "%s key": "%s value",
    1.63 +
    1.64 +          "keyWithNoValue": "",
    1.65 +          "valueWithSpaces": "",
    1.66 +          "valueWithMultilineSpaces": "",
    1.67 +
    1.68 +          "multi": "linevalue",
    1.69 +          "some": "spacesarestripped",
    1.70 +          "but": "not all of them",
    1.71 +
    1.72 +          "implicitPlural": {
    1.73 +            "one": "one",
    1.74 +            "other": "other"
    1.75 +          },
    1.76 +          "explicitPlural": {
    1.77 +            "one": "one",
    1.78 +            "other": "other"
    1.79 +          },
    1.80 +        }
    1.81 +        self.assertEqual(pairs, expected)
    1.82 +
    1.83 +    def test_exceptions(self):
    1.84 +        self.failUnlessRaises(MalformedLocaleFileError, parse,
    1.85 +                              ["invalid line with no key value"])
    1.86 +        self.failUnlessRaises(MalformedLocaleFileError, parse,
    1.87 +                              ["plural[one]=plural with no [other] value"])
    1.88 +        self.failUnlessRaises(MalformedLocaleFileError, parse,
    1.89 +                              ["multiline with no last empty line=\\"])
    1.90 +        self.failUnlessRaises(MalformedLocaleFileError, parse,
    1.91 +                              ["=no key"])
    1.92 +        self.failUnlessRaises(MalformedLocaleFileError, parse,
    1.93 +                              ["   =only spaces in key"])
    1.94 +
    1.95 +if __name__ == "__main__":
    1.96 +    unittest.main()

mercurial