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

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

     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/.
     5 import unittest
     7 from cuddlefish.property_parser import parse, MalformedLocaleFileError
     9 class TestParser(unittest.TestCase):
    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",
    19           "keyWithNoValue=",
    20           "valueWithSpaces=   ",
    21           "valueWithMultilineSpaces=  \\",
    22           "  \\",
    23           "  ",
    25           # All spaces before/after are striped
    26           " key = value ",
    27           "key2=value2",
    28           # Keys can contain '%'
    29           "%s key=%s value",
    31           # Accept empty lines
    32           "",
    33           "   ",
    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 ",
    42           # Explicit [other] plural definition
    43           "explicitPlural[one] = one",
    44           "explicitPlural[other] = other",
    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",
    57           "key": "value",
    58           "key2": "value2",
    59           "%s key": "%s value",
    61           "keyWithNoValue": "",
    62           "valueWithSpaces": "",
    63           "valueWithMultilineSpaces": "",
    65           "multi": "linevalue",
    66           "some": "spacesarestripped",
    67           "but": "not all of them",
    69           "implicitPlural": {
    70             "one": "one",
    71             "other": "other"
    72           },
    73           "explicitPlural": {
    74             "one": "one",
    75             "other": "other"
    76           },
    77         }
    78         self.assertEqual(pairs, expected)
    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"])
    92 if __name__ == "__main__":
    93     unittest.main()

mercurial