PageRenderTime 203ms CodeModel.GetById 36ms RepoModel.GetById 0ms app.codeStats 0ms

/External.LCA_RESTRICTED/Languages/IronPython/27/Lib/test/test_codeop.py

https://github.com/rpattabi/ironruby
Python | 310 lines | 279 code | 23 blank | 8 comment | 18 complexity | 501f456af567ea28555ebd38411c655e MD5 | raw file
  1. """
  2. Test cases for codeop.py
  3. Nick Mathewson
  4. """
  5. import unittest
  6. from test.test_support import run_unittest, is_jython, due_to_ironpython_bug, is_cli
  7. from codeop import compile_command, PyCF_DONT_IMPLY_DEDENT
  8. if is_jython:
  9. import sys
  10. import cStringIO
  11. def unify_callables(d):
  12. for n,v in d.items():
  13. if callable(v):
  14. d[n] = callable
  15. return d
  16. class CodeopTests(unittest.TestCase):
  17. def assertValid(self, str, symbol='single'):
  18. '''succeed iff str is a valid piece of code'''
  19. if is_jython:
  20. code = compile_command(str, "<input>", symbol)
  21. self.assertTrue(code)
  22. if symbol == "single":
  23. d,r = {},{}
  24. saved_stdout = sys.stdout
  25. sys.stdout = cStringIO.StringIO()
  26. try:
  27. exec code in d
  28. exec compile(str,"<input>","single") in r
  29. finally:
  30. sys.stdout = saved_stdout
  31. elif symbol == 'eval':
  32. ctx = {'a': 2}
  33. d = { 'value': eval(code,ctx) }
  34. r = { 'value': eval(str,ctx) }
  35. self.assertEquals(unify_callables(r),unify_callables(d))
  36. else:
  37. expected = compile(str, "<input>", symbol, PyCF_DONT_IMPLY_DEDENT)
  38. self.assertEquals( compile_command(str, "<input>", symbol), expected)
  39. def assertIncomplete(self, str, symbol='single'):
  40. '''succeed iff str is the start of a valid piece of code'''
  41. self.assertEquals( compile_command(str, symbol=symbol), None)
  42. def assertInvalid(self, str, symbol='single', is_syntax=1):
  43. '''succeed iff str is the start of an invalid piece of code'''
  44. try:
  45. compile_command(str,symbol=symbol)
  46. self.fail("No exception thrown for invalid code")
  47. except SyntaxError:
  48. self.assertTrue(is_syntax)
  49. except OverflowError:
  50. self.assertTrue(not is_syntax)
  51. def test_valid(self):
  52. av = self.assertValid
  53. if due_to_ironpython_bug("CodePlex 23740"):
  54. return
  55. # special case
  56. if not is_jython and not is_cli:
  57. self.assertEquals(compile_command(""),
  58. compile("pass", "<input>", 'single',
  59. PyCF_DONT_IMPLY_DEDENT))
  60. self.assertEquals(compile_command("\n"),
  61. compile("pass", "<input>", 'single',
  62. PyCF_DONT_IMPLY_DEDENT))
  63. else:
  64. av("")
  65. av("\n")
  66. av("a = 1")
  67. av("\na = 1")
  68. av("a = 1\n")
  69. av("a = 1\n\n")
  70. av("\n\na = 1\n\n")
  71. av("def x():\n pass\n")
  72. av("if 1:\n pass\n")
  73. av("\n\nif 1: pass\n")
  74. av("\n\nif 1: pass\n\n")
  75. av("def x():\n\n pass\n")
  76. av("def x():\n pass\n \n")
  77. av("def x():\n pass\n \n")
  78. av("pass\n")
  79. av("3**3\n")
  80. av("if 9==3:\n pass\nelse:\n pass\n")
  81. av("if 1:\n pass\n if 1:\n pass\n else:\n pass\n")
  82. av("#a\n#b\na = 3\n")
  83. av("#a\n\n \na=3\n")
  84. av("a=3\n\n")
  85. av("a = 9+ \\\n3")
  86. av("3**3","eval")
  87. av("(lambda z: \n z**3)","eval")
  88. av("9+ \\\n3","eval")
  89. av("9+ \\\n3\n","eval")
  90. av("\n\na**3","eval")
  91. av("\n \na**3","eval")
  92. av("#a\n#b\na**3","eval")
  93. av("\n\na = 1\n\n")
  94. av("\n\nif 1: a=1\n\n")
  95. av("if 1:\n pass\n if 1:\n pass\n else:\n pass\n")
  96. av("#a\n\n \na=3\n\n")
  97. av("\n\na**3","eval")
  98. av("\n \na**3","eval")
  99. av("#a\n#b\na**3","eval")
  100. av("def f():\n try: pass\n finally: [x for x in (1,2)]\n")
  101. av("def f():\n pass\n#foo\n")
  102. av("@a.b.c\ndef f():\n pass\n")
  103. def test_incomplete(self):
  104. if due_to_ironpython_bug("CodePlex 23740"):
  105. return
  106. ai = self.assertIncomplete
  107. ai("(a **")
  108. ai("(a,b,")
  109. ai("(a,b,(")
  110. ai("(a,b,(")
  111. ai("a = (")
  112. ai("a = {")
  113. ai("b + {")
  114. ai("if 9==3:\n pass\nelse:")
  115. ai("if 9==3:\n pass\nelse:\n")
  116. ai("if 9==3:\n pass\nelse:\n pass")
  117. ai("if 1:")
  118. ai("if 1:\n")
  119. ai("if 1:\n pass\n if 1:\n pass\n else:")
  120. ai("if 1:\n pass\n if 1:\n pass\n else:\n")
  121. ai("if 1:\n pass\n if 1:\n pass\n else:\n pass")
  122. ai("def x():")
  123. ai("def x():\n")
  124. ai("def x():\n\n")
  125. ai("def x():\n pass")
  126. ai("def x():\n pass\n ")
  127. ai("def x():\n pass\n ")
  128. ai("\n\ndef x():\n pass")
  129. ai("a = 9+ \\")
  130. ai("a = 'a\\")
  131. ai("a = '''xy")
  132. ai("","eval")
  133. ai("\n","eval")
  134. ai("(","eval")
  135. ai("(\n\n\n","eval")
  136. ai("(9+","eval")
  137. ai("9+ \\","eval")
  138. ai("lambda z: \\","eval")
  139. ai("if True:\n if True:\n if True: \n")
  140. ai("@a(")
  141. ai("@a(b")
  142. ai("@a(b,")
  143. ai("@a(b,c")
  144. ai("@a(b,c,")
  145. ai("from a import (")
  146. ai("from a import (b")
  147. ai("from a import (b,")
  148. ai("from a import (b,c")
  149. ai("from a import (b,c,")
  150. ai("[");
  151. ai("[a");
  152. ai("[a,");
  153. ai("[a,b");
  154. ai("[a,b,");
  155. ai("{");
  156. ai("{a");
  157. ai("{a:");
  158. ai("{a:b");
  159. ai("{a:b,");
  160. ai("{a:b,c");
  161. ai("{a:b,c:");
  162. ai("{a:b,c:d");
  163. ai("{a:b,c:d,");
  164. ai("a(")
  165. ai("a(b")
  166. ai("a(b,")
  167. ai("a(b,c")
  168. ai("a(b,c,")
  169. ai("a[")
  170. ai("a[b")
  171. ai("a[b,")
  172. ai("a[b:")
  173. ai("a[b:c")
  174. ai("a[b:c:")
  175. ai("a[b:c:d")
  176. ai("def a(")
  177. ai("def a(b")
  178. ai("def a(b,")
  179. ai("def a(b,c")
  180. ai("def a(b,c,")
  181. ai("(")
  182. ai("(a")
  183. ai("(a,")
  184. ai("(a,b")
  185. ai("(a,b,")
  186. ai("if a:\n pass\nelif b:")
  187. ai("if a:\n pass\nelif b:\n pass\nelse:")
  188. ai("while a:")
  189. ai("while a:\n pass\nelse:")
  190. ai("for a in b:")
  191. ai("for a in b:\n pass\nelse:")
  192. ai("try:")
  193. ai("try:\n pass\nexcept:")
  194. ai("try:\n pass\nfinally:")
  195. ai("try:\n pass\nexcept:\n pass\nfinally:")
  196. ai("with a:")
  197. ai("with a as b:")
  198. ai("class a:")
  199. ai("class a(")
  200. ai("class a(b")
  201. ai("class a(b,")
  202. ai("class a():")
  203. ai("[x for")
  204. ai("[x for x in")
  205. ai("[x for x in (")
  206. ai("(x for")
  207. ai("(x for x in")
  208. ai("(x for x in (")
  209. def test_invalid(self):
  210. ai = self.assertInvalid
  211. ai("a b")
  212. ai("a @")
  213. ai("a b @")
  214. ai("a ** @")
  215. ai("a = ")
  216. ai("a = 9 +")
  217. ai("def x():\n\npass\n")
  218. ai("\n\n if 1: pass\n\npass")
  219. ai("a = 9+ \\\n")
  220. ai("a = 'a\\ ")
  221. ai("a = 'a\\\n")
  222. ai("a = 1","eval")
  223. ai("a = (","eval")
  224. ai("]","eval")
  225. ai("())","eval")
  226. ai("[}","eval")
  227. ai("9+","eval")
  228. ai("lambda z:","eval")
  229. ai("a b","eval")
  230. ai("return 2.3")
  231. ai("if (a == 1 and b = 2): pass")
  232. ai("del 1")
  233. if not due_to_ironpython_bug("http://ironpython.codeplex.com/WorkItem/View.aspx?WorkItemId=24756"):
  234. ai("del ()")
  235. ai("del (1,)")
  236. ai("del [1]")
  237. ai("del '1'")
  238. ai("[i for i in range(10)] = (1, 2, 3)")
  239. def test_filename(self):
  240. self.assertEquals(compile_command("a = 1\n", "abc").co_filename,
  241. compile("a = 1\n", "abc", 'single').co_filename)
  242. self.assertNotEquals(compile_command("a = 1\n", "abc").co_filename,
  243. compile("a = 1\n", "def", 'single').co_filename)
  244. def test_main():
  245. run_unittest(CodeopTests)
  246. if __name__ == "__main__":
  247. test_main()