Parser/Composer library for Python
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

117 lines
3.6 KiB

  1. import unittest
  2. import pypeg2, pypeg2.xmlast
  3. class Another(object):
  4. grammar = pypeg2.name(), "=", pypeg2.attr("value")
  5. class Something(pypeg2.List):
  6. grammar = pypeg2.name(), pypeg2.some(Another), str
  7. class Thing2etreeTestCase1(unittest.TestCase):
  8. def runTest(self):
  9. s = Something()
  10. s.name = "hello"
  11. a1 = Another()
  12. a1.name = "bla"
  13. a1.value = "blub"
  14. a2 = Another()
  15. a2.name = "foo"
  16. a2.value = "bar"
  17. s.append(a1)
  18. s.append(a2)
  19. s.append("hello, world")
  20. root = pypeg2.xmlast.create_tree(s)
  21. self.assertEqual(root.tag, "Something")
  22. self.assertEqual(root.attrib["name"], "hello")
  23. try:
  24. import lxml
  25. except ImportError:
  26. self.assertEqual(pypeg2.xmlast.etree.tostring(root), b'<Something name="hello"><Another name="bla" value="blub" /><Another name="foo" value="bar" />hello, world</Something>')
  27. else:
  28. self.assertEqual(pypeg2.xmlast.etree.tostring(root), b'<Something name="hello"><Another name="bla" value="blub"/><Another name="foo" value="bar"/>hello, world</Something>')
  29. class SomethingElse(pypeg2.Namespace):
  30. grammar = pypeg2.name(), pypeg2.some(Another)
  31. class Thing2etreeTestCase2(unittest.TestCase):
  32. def runTest(self):
  33. s = SomethingElse()
  34. s.name = "hello"
  35. a1 = Another()
  36. a1.name = "bla"
  37. a1.value = "blub"
  38. a2 = Another()
  39. a2.name = "foo"
  40. a2.value = "bar"
  41. s[a1.name] = a1
  42. s[a2.name] = a2
  43. root = pypeg2.xmlast.create_tree(s)
  44. self.assertEqual(root.tag, "SomethingElse")
  45. self.assertEqual(root.attrib["name"], "hello")
  46. try:
  47. import lxml
  48. except ImportError:
  49. self.assertEqual(pypeg2.xmlast.etree.tostring(root), b'<SomethingElse name="hello"><Another name="bla" value="blub" /><Another name="foo" value="bar" /></SomethingElse>')
  50. else:
  51. self.assertEqual(pypeg2.xmlast.etree.tostring(root), b'<SomethingElse name="hello"><Another name="bla" value="blub"/><Another name="foo" value="bar"/></SomethingElse>')
  52. class Thing2XMLTestCase3(unittest.TestCase):
  53. class C1(str):
  54. grammar = pypeg2.ignore("!"), pypeg2.restline
  55. def runTest(self):
  56. r = pypeg2.parse("!all", type(self).C1)
  57. xml = pypeg2.xmlast.thing2xml(r)
  58. self.assertEqual(xml, b"<C1>all</C1>")
  59. class Key(str):
  60. grammar = pypeg2.name(), "=", pypeg2.restline
  61. class XML2ThingTestCase1(unittest.TestCase):
  62. def runTest(self):
  63. xml = b'<Key name="foo">bar</Key>'
  64. thing = pypeg2.xmlast.xml2thing(xml, globals())
  65. self.assertEqual(thing.name, pypeg2.Symbol("foo"))
  66. self.assertEqual(thing, "bar")
  67. class Instruction(str): pass
  68. class Parameter(object):
  69. grammar = pypeg2.attr("typing", str), pypeg2.name()
  70. class Parameters(pypeg2.Namespace):
  71. grammar = pypeg2.optional(pypeg2.csl(Parameter))
  72. class Function(pypeg2.List):
  73. grammar = pypeg2.name(), pypeg2.attr("parms", Parameters), "{", pypeg2.maybe_some(Instruction), "}"
  74. class XML2ThingTestCase2(unittest.TestCase):
  75. def runTest(self):
  76. xml = b'<Function name="f"><Parameters><Parameter name="a" typing="int"/></Parameters><Instruction>do_this</Instruction></Function>'
  77. f = pypeg2.xmlast.xml2thing(xml, globals())
  78. self.assertEqual(f.name, pypeg2.Symbol("f"))
  79. self.assertEqual(f.parms["a"].name, pypeg2.Symbol("a"))
  80. self.assertEqual(f.parms["a"].typing, pypeg2.Symbol("int"))
  81. self.assertEqual(f[0], "do_this")
  82. if __name__ == '__main__':
  83. unittest.main()