test_property_parser.py 3.0 KB

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