PageRenderTime 74ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/SiRFLive/PythonStdLib/test/test_parser.py

https://bitbucket.org/x893/sirflive
Python | 408 lines | 372 code | 24 blank | 12 comment | 5 complexity | 90c1e024ca42e232c99b8cad9f4ace25 MD5 | raw file
  1. import parser
  2. import unittest
  3. from test import test_support
  4. #
  5. # First, we test that we can generate trees from valid source fragments,
  6. # and that these valid trees are indeed allowed by the tree-loading side
  7. # of the parser module.
  8. #
  9. class RoundtripLegalSyntaxTestCase(unittest.TestCase):
  10. def roundtrip(self, f, s):
  11. st1 = f(s)
  12. t = st1.totuple()
  13. try:
  14. st2 = parser.sequence2st(t)
  15. except parser.ParserError, why:
  16. self.fail("could not roundtrip %r: %s" % (s, why))
  17. self.assertEquals(t, st2.totuple(),
  18. "could not re-generate syntax tree")
  19. def check_expr(self, s):
  20. self.roundtrip(parser.expr, s)
  21. def check_suite(self, s):
  22. self.roundtrip(parser.suite, s)
  23. def test_yield_statement(self):
  24. self.check_suite("def f(): yield 1")
  25. self.check_suite("def f(): return; yield 1")
  26. self.check_suite("def f(): yield 1; return")
  27. self.check_suite("def f():\n"
  28. " for x in range(30):\n"
  29. " yield x\n")
  30. def test_expressions(self):
  31. self.check_expr("foo(1)")
  32. self.check_expr("[1, 2, 3]")
  33. self.check_expr("[x**3 for x in range(20)]")
  34. self.check_expr("[x**3 for x in range(20) if x % 3]")
  35. self.check_expr("foo(*args)")
  36. self.check_expr("foo(*args, **kw)")
  37. self.check_expr("foo(**kw)")
  38. self.check_expr("foo(key=value)")
  39. self.check_expr("foo(key=value, *args)")
  40. self.check_expr("foo(key=value, *args, **kw)")
  41. self.check_expr("foo(key=value, **kw)")
  42. self.check_expr("foo(a, b, c, *args)")
  43. self.check_expr("foo(a, b, c, *args, **kw)")
  44. self.check_expr("foo(a, b, c, **kw)")
  45. self.check_expr("foo + bar")
  46. self.check_expr("foo - bar")
  47. self.check_expr("foo * bar")
  48. self.check_expr("foo / bar")
  49. self.check_expr("foo // bar")
  50. self.check_expr("lambda: 0")
  51. self.check_expr("lambda x: 0")
  52. self.check_expr("lambda *y: 0")
  53. self.check_expr("lambda *y, **z: 0")
  54. self.check_expr("lambda **z: 0")
  55. self.check_expr("lambda x, y: 0")
  56. self.check_expr("lambda foo=bar: 0")
  57. self.check_expr("lambda foo=bar, spaz=nifty+spit: 0")
  58. self.check_expr("lambda foo=bar, **z: 0")
  59. self.check_expr("lambda foo=bar, blaz=blat+2, **z: 0")
  60. self.check_expr("lambda foo=bar, blaz=blat+2, *y, **z: 0")
  61. self.check_expr("lambda x, *y, **z: 0")
  62. self.check_expr("(x for x in range(10))")
  63. self.check_expr("foo(x for x in range(10))")
  64. def test_print(self):
  65. self.check_suite("print")
  66. self.check_suite("print 1")
  67. self.check_suite("print 1,")
  68. self.check_suite("print >>fp")
  69. self.check_suite("print >>fp, 1")
  70. self.check_suite("print >>fp, 1,")
  71. def test_simple_expression(self):
  72. # expr_stmt
  73. self.check_suite("a")
  74. def test_simple_assignments(self):
  75. self.check_suite("a = b")
  76. self.check_suite("a = b = c = d = e")
  77. def test_simple_augmented_assignments(self):
  78. self.check_suite("a += b")
  79. self.check_suite("a -= b")
  80. self.check_suite("a *= b")
  81. self.check_suite("a /= b")
  82. self.check_suite("a //= b")
  83. self.check_suite("a %= b")
  84. self.check_suite("a &= b")
  85. self.check_suite("a |= b")
  86. self.check_suite("a ^= b")
  87. self.check_suite("a <<= b")
  88. self.check_suite("a >>= b")
  89. self.check_suite("a **= b")
  90. def test_function_defs(self):
  91. self.check_suite("def f(): pass")
  92. self.check_suite("def f(*args): pass")
  93. self.check_suite("def f(*args, **kw): pass")
  94. self.check_suite("def f(**kw): pass")
  95. self.check_suite("def f(foo=bar): pass")
  96. self.check_suite("def f(foo=bar, *args): pass")
  97. self.check_suite("def f(foo=bar, *args, **kw): pass")
  98. self.check_suite("def f(foo=bar, **kw): pass")
  99. self.check_suite("def f(a, b): pass")
  100. self.check_suite("def f(a, b, *args): pass")
  101. self.check_suite("def f(a, b, *args, **kw): pass")
  102. self.check_suite("def f(a, b, **kw): pass")
  103. self.check_suite("def f(a, b, foo=bar): pass")
  104. self.check_suite("def f(a, b, foo=bar, *args): pass")
  105. self.check_suite("def f(a, b, foo=bar, *args, **kw): pass")
  106. self.check_suite("def f(a, b, foo=bar, **kw): pass")
  107. self.check_suite("@staticmethod\n"
  108. "def f(): pass")
  109. self.check_suite("@staticmethod\n"
  110. "@funcattrs(x, y)\n"
  111. "def f(): pass")
  112. self.check_suite("@funcattrs()\n"
  113. "def f(): pass")
  114. def test_import_from_statement(self):
  115. self.check_suite("from sys.path import *")
  116. self.check_suite("from sys.path import dirname")
  117. self.check_suite("from sys.path import (dirname)")
  118. self.check_suite("from sys.path import (dirname,)")
  119. self.check_suite("from sys.path import dirname as my_dirname")
  120. self.check_suite("from sys.path import (dirname as my_dirname)")
  121. self.check_suite("from sys.path import (dirname as my_dirname,)")
  122. self.check_suite("from sys.path import dirname, basename")
  123. self.check_suite("from sys.path import (dirname, basename)")
  124. self.check_suite("from sys.path import (dirname, basename,)")
  125. self.check_suite(
  126. "from sys.path import dirname as my_dirname, basename")
  127. self.check_suite(
  128. "from sys.path import (dirname as my_dirname, basename)")
  129. self.check_suite(
  130. "from sys.path import (dirname as my_dirname, basename,)")
  131. self.check_suite(
  132. "from sys.path import dirname, basename as my_basename")
  133. self.check_suite(
  134. "from sys.path import (dirname, basename as my_basename)")
  135. self.check_suite(
  136. "from sys.path import (dirname, basename as my_basename,)")
  137. def test_basic_import_statement(self):
  138. self.check_suite("import sys")
  139. self.check_suite("import sys as system")
  140. self.check_suite("import sys, math")
  141. self.check_suite("import sys as system, math")
  142. self.check_suite("import sys, math as my_math")
  143. def test_pep263(self):
  144. self.check_suite("# -*- coding: iso-8859-1 -*-\n"
  145. "pass\n")
  146. def test_assert(self):
  147. self.check_suite("assert alo < ahi and blo < bhi\n")
  148. #
  149. # Second, we take *invalid* trees and make sure we get ParserError
  150. # rejections for them.
  151. #
  152. class IllegalSyntaxTestCase(unittest.TestCase):
  153. def check_bad_tree(self, tree, label):
  154. try:
  155. parser.sequence2st(tree)
  156. except parser.ParserError:
  157. pass
  158. else:
  159. self.fail("did not detect invalid tree for %r" % label)
  160. def test_junk(self):
  161. # not even remotely valid:
  162. self.check_bad_tree((1, 2, 3), "<junk>")
  163. def test_illegal_yield_1(self):
  164. # Illegal yield statement: def f(): return 1; yield 1
  165. tree = \
  166. (257,
  167. (264,
  168. (285,
  169. (259,
  170. (1, 'def'),
  171. (1, 'f'),
  172. (260, (7, '('), (8, ')')),
  173. (11, ':'),
  174. (291,
  175. (4, ''),
  176. (5, ''),
  177. (264,
  178. (265,
  179. (266,
  180. (272,
  181. (275,
  182. (1, 'return'),
  183. (313,
  184. (292,
  185. (293,
  186. (294,
  187. (295,
  188. (297,
  189. (298,
  190. (299,
  191. (300,
  192. (301,
  193. (302, (303, (304, (305, (2, '1')))))))))))))))))),
  194. (264,
  195. (265,
  196. (266,
  197. (272,
  198. (276,
  199. (1, 'yield'),
  200. (313,
  201. (292,
  202. (293,
  203. (294,
  204. (295,
  205. (297,
  206. (298,
  207. (299,
  208. (300,
  209. (301,
  210. (302,
  211. (303, (304, (305, (2, '1')))))))))))))))))),
  212. (4, ''))),
  213. (6, ''))))),
  214. (4, ''),
  215. (0, ''))))
  216. self.check_bad_tree(tree, "def f():\n return 1\n yield 1")
  217. def test_illegal_yield_2(self):
  218. # Illegal return in generator: def f(): return 1; yield 1
  219. tree = \
  220. (257,
  221. (264,
  222. (265,
  223. (266,
  224. (278,
  225. (1, 'from'),
  226. (281, (1, '__future__')),
  227. (1, 'import'),
  228. (279, (1, 'generators')))),
  229. (4, ''))),
  230. (264,
  231. (285,
  232. (259,
  233. (1, 'def'),
  234. (1, 'f'),
  235. (260, (7, '('), (8, ')')),
  236. (11, ':'),
  237. (291,
  238. (4, ''),
  239. (5, ''),
  240. (264,
  241. (265,
  242. (266,
  243. (272,
  244. (275,
  245. (1, 'return'),
  246. (313,
  247. (292,
  248. (293,
  249. (294,
  250. (295,
  251. (297,
  252. (298,
  253. (299,
  254. (300,
  255. (301,
  256. (302, (303, (304, (305, (2, '1')))))))))))))))))),
  257. (264,
  258. (265,
  259. (266,
  260. (272,
  261. (276,
  262. (1, 'yield'),
  263. (313,
  264. (292,
  265. (293,
  266. (294,
  267. (295,
  268. (297,
  269. (298,
  270. (299,
  271. (300,
  272. (301,
  273. (302,
  274. (303, (304, (305, (2, '1')))))))))))))))))),
  275. (4, ''))),
  276. (6, ''))))),
  277. (4, ''),
  278. (0, ''))))
  279. self.check_bad_tree(tree, "def f():\n return 1\n yield 1")
  280. def test_print_chevron_comma(self):
  281. # Illegal input: print >>fp,
  282. tree = \
  283. (257,
  284. (264,
  285. (265,
  286. (266,
  287. (268,
  288. (1, 'print'),
  289. (35, '>>'),
  290. (290,
  291. (291,
  292. (292,
  293. (293,
  294. (295,
  295. (296,
  296. (297,
  297. (298, (299, (300, (301, (302, (303, (1, 'fp')))))))))))))),
  298. (12, ','))),
  299. (4, ''))),
  300. (0, ''))
  301. self.check_bad_tree(tree, "print >>fp,")
  302. def test_a_comma_comma_c(self):
  303. # Illegal input: a,,c
  304. tree = \
  305. (258,
  306. (311,
  307. (290,
  308. (291,
  309. (292,
  310. (293,
  311. (295,
  312. (296,
  313. (297,
  314. (298, (299, (300, (301, (302, (303, (1, 'a')))))))))))))),
  315. (12, ','),
  316. (12, ','),
  317. (290,
  318. (291,
  319. (292,
  320. (293,
  321. (295,
  322. (296,
  323. (297,
  324. (298, (299, (300, (301, (302, (303, (1, 'c'))))))))))))))),
  325. (4, ''),
  326. (0, ''))
  327. self.check_bad_tree(tree, "a,,c")
  328. def test_illegal_operator(self):
  329. # Illegal input: a $= b
  330. tree = \
  331. (257,
  332. (264,
  333. (265,
  334. (266,
  335. (267,
  336. (312,
  337. (291,
  338. (292,
  339. (293,
  340. (294,
  341. (296,
  342. (297,
  343. (298,
  344. (299,
  345. (300, (301, (302, (303, (304, (1, 'a'))))))))))))))),
  346. (268, (37, '$=')),
  347. (312,
  348. (291,
  349. (292,
  350. (293,
  351. (294,
  352. (296,
  353. (297,
  354. (298,
  355. (299,
  356. (300, (301, (302, (303, (304, (1, 'b'))))))))))))))))),
  357. (4, ''))),
  358. (0, ''))
  359. self.check_bad_tree(tree, "a $= b")
  360. def test_malformed_global(self):
  361. #doesn't have global keyword in ast
  362. tree = (257,
  363. (264,
  364. (265,
  365. (266,
  366. (282, (1, 'foo'))), (4, ''))),
  367. (4, ''),
  368. (0, ''))
  369. self.check_bad_tree(tree, "malformed global ast")
  370. def test_main():
  371. test_support.run_unittest(
  372. RoundtripLegalSyntaxTestCase,
  373. IllegalSyntaxTestCase
  374. )
  375. if __name__ == "__main__":
  376. test_main()