home *** CD-ROM | disk | FTP | other *** search
/ PC World 2005 June / PCWorld_2005-06_cd.bin / software / vyzkuste / firewally / firewally.exe / framework-2.3.exe / test_codeop.py < prev    next >
Text File  |  2003-12-30  |  5KB  |  193 lines

  1. """
  2.    Test cases for codeop.py
  3.    Nick Mathewson
  4. """
  5. import unittest
  6. from test.test_support import run_unittest, is_jython
  7.  
  8. from codeop import compile_command, PyCF_DONT_IMPLY_DEDENT
  9.  
  10. if is_jython:
  11.     import sys
  12.     import cStringIO
  13.  
  14.     def unify_callables(d):
  15.         for n,v in d.items():
  16.             if callable(v):
  17.                 d[n] = callable
  18.         return d
  19.  
  20. class CodeopTests(unittest.TestCase):
  21.  
  22.     def assertValid(self, str, symbol='single'):
  23.         '''succeed iff str is a valid piece of code'''
  24.         if is_jython:
  25.             code = compile_command(str, "<input>", symbol)
  26.             self.assert_(code)
  27.             if symbol == "single":
  28.                 d,r = {},{}
  29.                 saved_stdout = sys.stdout
  30.                 sys.stdout = cStringIO.StringIO()
  31.                 try:
  32.                     exec code in d
  33.                     exec compile(str,"<input>","single") in r
  34.                 finally:
  35.                     sys.stdout = saved_stdout
  36.             elif symbol == 'eval':
  37.                 ctx = {'a': 2}
  38.                 d = { 'value': eval(code,ctx) }
  39.                 r = { 'value': eval(str,ctx) }
  40.             self.assertEquals(unify_callables(r),unify_callables(d))
  41.         else:
  42.             expected = compile(str, "<input>", symbol, PyCF_DONT_IMPLY_DEDENT)
  43.             self.assertEquals( compile_command(str, "<input>", symbol), expected)
  44.  
  45.     def assertIncomplete(self, str, symbol='single'):
  46.         '''succeed iff str is the start of a valid piece of code'''
  47.         self.assertEquals( compile_command(str, symbol=symbol), None)
  48.  
  49.     def assertInvalid(self, str, symbol='single', is_syntax=1):
  50.         '''succeed iff str is the start of an invalid piece of code'''
  51.         try:
  52.             compile_command(str,symbol=symbol)
  53.             self.fail("No exception thrown for invalid code")
  54.         except SyntaxError:
  55.             self.assert_(is_syntax)
  56.         except OverflowError:
  57.             self.assert_(not is_syntax)
  58.  
  59.     def test_valid(self):
  60.         av = self.assertValid
  61.  
  62.         # special case
  63.         if not is_jython:
  64.             self.assertEquals(compile_command(""),
  65.                             compile("pass", "<input>", 'single',
  66.                                     PyCF_DONT_IMPLY_DEDENT))
  67.             self.assertEquals(compile_command("\n"),
  68.                             compile("pass", "<input>", 'single',
  69.                                     PyCF_DONT_IMPLY_DEDENT))
  70.         else:
  71.             av("")
  72.             av("\n")
  73.  
  74.         av("a = 1")
  75.         av("\na = 1")
  76.         av("a = 1\n")
  77.         av("a = 1\n\n")
  78.         av("\n\na = 1\n\n")
  79.  
  80.         av("def x():\n  pass\n")
  81.         av("if 1:\n pass\n")
  82.  
  83.         av("\n\nif 1: pass\n")
  84.         av("\n\nif 1: pass\n\n")
  85.  
  86.         av("def x():\n\n pass\n")
  87.         av("def x():\n  pass\n  \n")
  88.         av("def x():\n  pass\n \n")
  89.  
  90.         av("pass\n")
  91.         av("3**3\n")
  92.  
  93.         av("if 9==3:\n   pass\nelse:\n   pass\n")
  94.         av("if 1:\n pass\n if 1:\n  pass\n else:\n  pass\n")
  95.  
  96.         av("#a\n#b\na = 3\n")
  97.         av("#a\n\n   \na=3\n")
  98.         av("a=3\n\n")
  99.         av("a = 9+ \\\n3")
  100.  
  101.         av("3**3","eval")
  102.         av("(lambda z: \n z**3)","eval")
  103.  
  104.         av("9+ \\\n3","eval")
  105.         av("9+ \\\n3\n","eval")
  106.  
  107.         av("\n\na**3","eval")
  108.         av("\n \na**3","eval")
  109.         av("#a\n#b\na**3","eval")
  110.  
  111.     def test_incomplete(self):
  112.         ai = self.assertIncomplete
  113.  
  114.         ai("(a **")
  115.         ai("(a,b,")
  116.         ai("(a,b,(")
  117.         ai("(a,b,(")
  118.         ai("a = (")
  119.         ai("a = {")
  120.         ai("b + {")
  121.  
  122.         ai("if 9==3:\n   pass\nelse:")
  123.         ai("if 9==3:\n   pass\nelse:\n")
  124.         ai("if 9==3:\n   pass\nelse:\n   pass")
  125.         ai("if 1:")
  126.         ai("if 1:\n")
  127.         ai("if 1:\n pass\n if 1:\n  pass\n else:")
  128.         ai("if 1:\n pass\n if 1:\n  pass\n else:\n")
  129.         ai("if 1:\n pass\n if 1:\n  pass\n else:\n  pass")
  130.  
  131.         ai("def x():")
  132.         ai("def x():\n")
  133.         ai("def x():\n\n")
  134.  
  135.         ai("def x():\n  pass")
  136.         ai("def x():\n  pass\n ")
  137.         ai("def x():\n  pass\n  ")
  138.         ai("\n\ndef x():\n  pass")
  139.  
  140.         ai("a = 9+ \\")
  141.         ai("a = 'a\\")
  142.         ai("a = '''xy")
  143.  
  144.         ai("","eval")
  145.         ai("\n","eval")
  146.         ai("(","eval")
  147.         ai("(\n\n\n","eval")
  148.         ai("(9+","eval")
  149.         ai("9+ \\","eval")
  150.         ai("lambda z: \\","eval")
  151.  
  152.     def test_invalid(self):
  153.         ai = self.assertInvalid
  154.         ai("a b")
  155.  
  156.         ai("a @")
  157.         ai("a b @")
  158.         ai("a ** @")
  159.  
  160.         ai("a = ")
  161.         ai("a = 9 +")
  162.  
  163.         ai("def x():\n\npass\n")
  164.  
  165.         ai("\n\n if 1: pass\n\npass")
  166.  
  167.         ai("a = 9+ \\\n")
  168.         ai("a = 'a\\ ")
  169.         ai("a = 'a\\\n")
  170.  
  171.         ai("a = 1","eval")
  172.         ai("a = (","eval")
  173.         ai("]","eval")
  174.         ai("())","eval")
  175.         ai("[}","eval")
  176.         ai("9+","eval")
  177.         ai("lambda z:","eval")
  178.         ai("a b","eval")
  179.  
  180.     def test_filename(self):
  181.         self.assertEquals(compile_command("a = 1\n", "abc").co_filename,
  182.                           compile("a = 1\n", "abc", 'single').co_filename)
  183.         self.assertNotEquals(compile_command("a = 1\n", "abc").co_filename,
  184.                              compile("a = 1\n", "def", 'single').co_filename)
  185.  
  186.  
  187. def test_main():
  188.     run_unittest(CodeopTests)
  189.  
  190.  
  191. if __name__ == "__main__":
  192.     test_main()
  193.