michael@0: # This Source Code Form is subject to the terms of the Mozilla Public michael@0: # License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: # file, You can obtain one at http://mozilla.org/MPL/2.0/. michael@0: michael@0: import unittest michael@0: michael@0: from cuddlefish.property_parser import parse, MalformedLocaleFileError michael@0: michael@0: class TestParser(unittest.TestCase): michael@0: michael@0: def test_parse(self): michael@0: lines = [ michael@0: # Comments are striped only if `#` is the first non-space character michael@0: "sharp=#can be in value", michael@0: "# comment", michael@0: "#key=value", michael@0: " # comment2", michael@0: michael@0: "keyWithNoValue=", michael@0: "valueWithSpaces= ", michael@0: "valueWithMultilineSpaces= \\", michael@0: " \\", michael@0: " ", michael@0: michael@0: # All spaces before/after are striped michael@0: " key = value ", michael@0: "key2=value2", michael@0: # Keys can contain '%' michael@0: "%s key=%s value", michael@0: michael@0: # Accept empty lines michael@0: "", michael@0: " ", michael@0: michael@0: # Multiline string must use backslash at end of lines michael@0: "multi=line\\", "value", michael@0: # With multiline string, left spaces are stripped ... michael@0: "some= spaces\\", " are\\ ", " stripped ", michael@0: # ... but not right spaces, except the last line! michael@0: "but=not \\", "all of \\", " them ", michael@0: michael@0: # Explicit [other] plural definition michael@0: "explicitPlural[one] = one", michael@0: "explicitPlural[other] = other", michael@0: michael@0: # Implicit [other] plural definition michael@0: "implicitPlural[one] = one", michael@0: "implicitPlural = other", # This key is the [other] one michael@0: ] michael@0: # Ensure that all lines end with a `\n` michael@0: # And that strings are unicode ones (parser code relies on it) michael@0: lines = [unicode(l + "\n") for l in lines] michael@0: pairs = parse(lines) michael@0: expected = { michael@0: "sharp": "#can be in value", michael@0: michael@0: "key": "value", michael@0: "key2": "value2", michael@0: "%s key": "%s value", michael@0: michael@0: "keyWithNoValue": "", michael@0: "valueWithSpaces": "", michael@0: "valueWithMultilineSpaces": "", michael@0: michael@0: "multi": "linevalue", michael@0: "some": "spacesarestripped", michael@0: "but": "not all of them", michael@0: michael@0: "implicitPlural": { michael@0: "one": "one", michael@0: "other": "other" michael@0: }, michael@0: "explicitPlural": { michael@0: "one": "one", michael@0: "other": "other" michael@0: }, michael@0: } michael@0: self.assertEqual(pairs, expected) michael@0: michael@0: def test_exceptions(self): michael@0: self.failUnlessRaises(MalformedLocaleFileError, parse, michael@0: ["invalid line with no key value"]) michael@0: self.failUnlessRaises(MalformedLocaleFileError, parse, michael@0: ["plural[one]=plural with no [other] value"]) michael@0: self.failUnlessRaises(MalformedLocaleFileError, parse, michael@0: ["multiline with no last empty line=\\"]) michael@0: self.failUnlessRaises(MalformedLocaleFileError, parse, michael@0: ["=no key"]) michael@0: self.failUnlessRaises(MalformedLocaleFileError, parse, michael@0: [" =only spaces in key"]) michael@0: michael@0: if __name__ == "__main__": michael@0: unittest.main()