PageRenderTime 53ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/pypeg2/test/test_pyPEG2.py

https://bitbucket.org/apalala/pypeg
Python | 354 lines | 338 code | 16 blank | 0 comment | 6 complexity | 480a62b36238cc7fc5fadd6052f58052 MD5 | raw file
Possible License(s): GPL-2.0
  1. from __future__ import unicode_literals
  2. import unittest
  3. import pypeg2
  4. import re
  5. class GrammarTestCase1(unittest.TestCase):
  6. def runTest(self):
  7. x = pypeg2.some("thing")
  8. y = pypeg2.maybe_some("thing")
  9. z = pypeg2.optional("hello", "world")
  10. self.assertEqual(x, (-2, "thing"))
  11. self.assertEqual(y, (-1, "thing"))
  12. self.assertEqual(z, (0, ("hello", "world")))
  13. class GrammarTestCase2(unittest.TestCase):
  14. def runTest(self):
  15. L1 = pypeg2.csl("thing")
  16. L2 = pypeg2.csl("hello", "world")
  17. self.assertEqual(L1, ("thing", -1, (",", pypeg2.blank, "thing")))
  18. self.assertEqual(L2, ("hello", "world", -1, (",", pypeg2.blank, "hello", "world")))
  19. class ParserTestCase(unittest.TestCase): pass
  20. class TypeErrorTestCase(ParserTestCase):
  21. def runTest(self):
  22. parser = pypeg2.Parser()
  23. with self.assertRaises(pypeg2.GrammarTypeError):
  24. parser.parse("hello, world", 23)
  25. class ParseTerminalStringTestCase1(ParserTestCase):
  26. def runTest(self):
  27. parser = pypeg2.Parser()
  28. r = parser.parse("hello, world", "hello")
  29. self.assertEqual(r, (", world", None))
  30. class ParseTerminalStringTestCase2(ParserTestCase):
  31. def runTest(self):
  32. parser = pypeg2.Parser()
  33. with self.assertRaises(SyntaxError):
  34. r = parser.parse("hello, world", "world")
  35. class ParseKeywordTestCase1(ParserTestCase):
  36. def runTest(self):
  37. parser = pypeg2.Parser()
  38. r = parser.parse("hallo, world", pypeg2.K("hallo"))
  39. self.assertEqual(r, (", world", None))
  40. pypeg2.Keyword.table[pypeg2.K("hallo")]
  41. class ParseKeywordTestCase2(ParserTestCase):
  42. def runTest(self):
  43. parser = pypeg2.Parser()
  44. with self.assertRaises(SyntaxError):
  45. r = parser.parse("hello, world", pypeg2.K("werld"))
  46. pypeg2.Keyword.table[pypeg2.K("werld")]
  47. class ParseKeywordTestCase3(ParserTestCase):
  48. def runTest(self):
  49. parser = pypeg2.Parser()
  50. with self.assertRaises(SyntaxError):
  51. r = parser.parse(", world", pypeg2.K("hallo"))
  52. pypeg2.Keyword.table[pypeg2.K("hallo")]
  53. class ParseRegexTestCase1(ParserTestCase):
  54. def runTest(self):
  55. parser = pypeg2.Parser()
  56. r = parser.parse("hello, world", re.compile(r"h.[lx]l\S", re.U))
  57. self.assertEqual(r, (", world", "hello"))
  58. class ParseRegexTestCase2(ParserTestCase):
  59. def runTest(self):
  60. parser = pypeg2.Parser()
  61. with self.assertRaises(SyntaxError):
  62. r = parser.parse("hello, world", re.compile(r"\d", re.U))
  63. class ParseSymbolTestCase1(ParserTestCase):
  64. def runTest(self):
  65. parser = pypeg2.Parser()
  66. r = parser.parse("hello, world", pypeg2.Symbol)
  67. self.assertEqual(r, (", world", pypeg2.Symbol("hello")))
  68. class ParseSymbolTestCase2(ParserTestCase):
  69. def runTest(self):
  70. parser = pypeg2.Parser()
  71. with self.assertRaises(SyntaxError):
  72. r = parser.parse(", world", pypeg2.Symbol)
  73. class ParseAttributeTestCase(ParserTestCase):
  74. def runTest(self):
  75. parser = pypeg2.Parser()
  76. r = parser.parse("hello, world", pypeg2.attr("some", pypeg2.Symbol))
  77. self.assertEqual(
  78. r,
  79. (
  80. ', world',
  81. pypeg2.attr.Class(name='some', thing=pypeg2.Symbol('hello'),
  82. subtype=None)
  83. )
  84. )
  85. class ParseTupleTestCase1(ParserTestCase):
  86. def runTest(self):
  87. parser = pypeg2.Parser()
  88. r = parser.parse("hello, world", (pypeg2.name(), ",", pypeg2.name()))
  89. self.assertEqual(
  90. r,
  91. (
  92. '',
  93. [
  94. pypeg2.attr.Class(name='name',
  95. thing=pypeg2.Symbol('hello'), subtype=None),
  96. pypeg2.attr.Class(name='name',
  97. thing=pypeg2.Symbol('world'), subtype=None)
  98. ]
  99. )
  100. )
  101. class ParseTupleTestCase2(ParserTestCase):
  102. def runTest(self):
  103. parser = pypeg2.Parser()
  104. with self.assertRaises(ValueError):
  105. parser.parse("hello, world", (-23, "x"))
  106. class ParseSomeTestCase1(ParserTestCase):
  107. def runTest(self):
  108. parser = pypeg2.Parser()
  109. r = parser.parse("hello, world", pypeg2.some(re.compile(r"\w", re.U)))
  110. self.assertEqual(r, (', world', ['h', 'e', 'l', 'l', 'o']))
  111. class ParseSomeTestCase2(ParserTestCase):
  112. def runTest(self):
  113. parser = pypeg2.Parser()
  114. with self.assertRaises(SyntaxError):
  115. r = parser.parse("hello, world", pypeg2.some(re.compile(r"\d", re.U)))
  116. class ParseMaybeSomeTestCase1(ParserTestCase):
  117. def runTest(self):
  118. parser = pypeg2.Parser()
  119. r = parser.parse("hello, world", pypeg2.maybe_some(re.compile(r"\w", re.U)))
  120. self.assertEqual(r, (', world', ['h', 'e', 'l', 'l', 'o']))
  121. class ParseMaybeSomeTestCase2(ParserTestCase):
  122. def runTest(self):
  123. parser = pypeg2.Parser()
  124. r = parser.parse("hello, world", pypeg2.maybe_some(re.compile(r"\d", re.U)))
  125. self.assertEqual(r, ('hello, world', []))
  126. class ParseCardinalityTestCase1(ParserTestCase):
  127. def runTest(self):
  128. parser = pypeg2.Parser()
  129. r = parser.parse("hello, world", (5, re.compile(r"\w", re.U)))
  130. self.assertEqual(r, (', world', ['h', 'e', 'l', 'l', 'o']))
  131. class ParseCardinalityTestCase2(ParserTestCase):
  132. def runTest(self):
  133. parser = pypeg2.Parser()
  134. with self.assertRaises(SyntaxError):
  135. r = parser.parse("hello, world", (6, re.compile(r"\w", re.U)))
  136. class ParseOptionsTestCase1(ParserTestCase):
  137. def runTest(self):
  138. parser = pypeg2.Parser()
  139. r = parser.parse("hello, world", [re.compile(r"\d+", re.U), pypeg2.word])
  140. self.assertEqual(r, (', world', 'hello'))
  141. class ParseOptionsTestCase2(ParserTestCase):
  142. def runTest(self):
  143. parser = pypeg2.Parser()
  144. with self.assertRaises(SyntaxError):
  145. r = parser.parse("hello, world", ["x", "y"])
  146. class ParseListTestCase1(ParserTestCase):
  147. class Chars(pypeg2.List):
  148. grammar = pypeg2.some(re.compile(r"\w", re.U)), pypeg2.attr("comma", ",")
  149. def runTest(self):
  150. parser = pypeg2.Parser()
  151. r = parser.parse("hello, world", ParseListTestCase1.Chars)
  152. self.assertEqual(r, (
  153. 'world',
  154. ParseListTestCase1.Chars(['h', 'e', 'l', 'l', 'o']))
  155. )
  156. self.assertEqual(r[1].comma, None)
  157. class ParseListTestCase2(ParserTestCase):
  158. class Digits(pypeg2.List):
  159. grammar = pypeg2.some(re.compile(r"\d", re.U))
  160. def runTest(self):
  161. parser = pypeg2.Parser()
  162. with self.assertRaises(SyntaxError):
  163. r = parser.parse("hello, world", ParseListTestCase2.Digits)
  164. class ParseClassTestCase1(ParserTestCase):
  165. class Word(str):
  166. grammar = pypeg2.word
  167. def runTest(self):
  168. parser = pypeg2.Parser()
  169. r = parser.parse("hello, world", ParseClassTestCase1.Word)
  170. self.assertEqual(type(r[1]), ParseClassTestCase1.Word)
  171. self.assertEqual(r[1], "hello")
  172. class ParseClassTestCase2(ParserTestCase):
  173. class Word(str):
  174. grammar = pypeg2.word, pypeg2.attr("comma", ",")
  175. def __init__(self, data):
  176. self.polished = False
  177. def polish(self):
  178. self.polished = True
  179. def runTest(self):
  180. parser = pypeg2.Parser()
  181. r = parser.parse("hello, world", ParseClassTestCase2.Word)
  182. self.assertEqual(type(r[1]), ParseClassTestCase2.Word)
  183. self.assertEqual(r[1], "hello")
  184. self.assertTrue(r[1].polished)
  185. self.assertEqual(r[1].comma, None)
  186. class Parm(object):
  187. grammar = pypeg2.name(), "=", pypeg2.attr("value", int)
  188. class Parms(pypeg2.Namespace):
  189. grammar = (pypeg2.csl(Parm), pypeg2.flag("fullstop", "."),
  190. pypeg2.flag("semicolon", ";"))
  191. class ParseNLTestCase1(ParserTestCase):
  192. def runTest(self):
  193. parser = pypeg2.Parser()
  194. parser.comment = pypeg2.comment_c
  195. t, parms = parser.parse("x=23 /* Illuminati */, y=42 /* the answer */;", Parms)
  196. self.assertEqual(parms["x"].value, 23)
  197. self.assertEqual(parms["y"].value, 42)
  198. self.assertEqual(parms.fullstop, False)
  199. self.assertEqual(parms.semicolon, True)
  200. class EnumTest(pypeg2.Symbol):
  201. grammar = pypeg2.Enum( pypeg2.K("int"), pypeg2.K("long") )
  202. class ParseEnumTestCase1(ParserTestCase):
  203. def runTest(self):
  204. parser = pypeg2.Parser()
  205. t, r = parser.parse("int", EnumTest)
  206. self.assertEqual(r, "int")
  207. class ParseEnumTestCase2(ParserTestCase):
  208. def runTest(self):
  209. parser = pypeg2.Parser()
  210. with self.assertRaises(SyntaxError):
  211. t, r = parser.parse("float", EnumTest)
  212. class ParseInvisibleTestCase(ParserTestCase):
  213. class C1(str):
  214. grammar = pypeg2.ignore("!"), pypeg2.restline
  215. def runTest(self):
  216. r = pypeg2.parse("!all", type(self).C1)
  217. self.assertEqual(str(r), "all")
  218. self.assertEqual(r._ignore1, None)
  219. class ComposeTestCase(unittest.TestCase): pass
  220. class ComposeString(object):
  221. grammar = "something"
  222. class ComposeStringTestCase(ComposeTestCase):
  223. def runTest(self):
  224. x = ComposeString()
  225. t = pypeg2.compose(x)
  226. self.assertEqual(t, "something")
  227. class ComposeRegex(str):
  228. grammar = pypeg2.word
  229. class ComposeRegexTestCase(ComposeTestCase):
  230. def runTest(self):
  231. x = ComposeRegex("something")
  232. t = pypeg2.compose(x)
  233. self.assertEqual(t, "something")
  234. class ComposeKeyword(object):
  235. grammar = pypeg2.K("hallo")
  236. class ComposeKeywordTestCase(ComposeTestCase):
  237. def runTest(self):
  238. x = ComposeKeyword()
  239. t = pypeg2.compose(x)
  240. self.assertEqual(t, "hallo")
  241. class ComposeSymbol(pypeg2.Symbol): pass
  242. class ComposeSymbolTestCase(ComposeTestCase):
  243. def runTest(self):
  244. x = ComposeSymbol("hello")
  245. t = pypeg2.compose(x)
  246. self.assertEqual(t, "hello")
  247. class ComposeAttribute(object):
  248. grammar = pypeg2.name()
  249. class ComposeAttributeTestCase(ComposeTestCase):
  250. def runTest(self):
  251. x = ComposeAttribute()
  252. x.name = pypeg2.Symbol("hello")
  253. t = pypeg2.compose(x)
  254. self.assertEqual(t, "hello")
  255. class ComposeFlag(object):
  256. grammar = pypeg2.flag("mark", "MARK")
  257. class ComposeFlagTestCase1(ComposeTestCase):
  258. def runTest(self):
  259. x = ComposeFlag()
  260. x.mark = True
  261. t = pypeg2.compose(x)
  262. self.assertEqual(t, "MARK")
  263. class ComposeFlagTestCase2(ComposeTestCase):
  264. def runTest(self):
  265. x = ComposeFlag()
  266. x.mark = False
  267. t = pypeg2.compose(x)
  268. self.assertEqual(t, "")
  269. class ComposeTuple(pypeg2.List):
  270. grammar = pypeg2.csl(pypeg2.word)
  271. class ComposeTupleTestCase(ComposeTestCase):
  272. def runTest(self):
  273. x = ComposeTuple(["hello", "world"])
  274. t = pypeg2.compose(x)
  275. self.assertEqual(t, "hello, world")
  276. class ComposeList(str):
  277. grammar = [ re.compile(r"\d+", re.U), pypeg2.word ]
  278. class ComposeListTestCase(ComposeTestCase):
  279. def runTest(self):
  280. x = ComposeList("hello")
  281. t = pypeg2.compose(x)
  282. self.assertEqual(t, "hello")
  283. class C2(str):
  284. grammar = pypeg2.attr("some", "!"), pypeg2.restline
  285. class ComposeInvisibleTestCase(ParserTestCase):
  286. def runTest(self):
  287. r = pypeg2.parse("!all", C2)
  288. self.assertEqual(str(r), "all")
  289. self.assertEqual(r.some, None)
  290. t = pypeg2.compose(r, C2)
  291. self.assertEqual(t, "!all")
  292. if __name__ == '__main__':
  293. unittest.main()