Changeset 391 for python/trunk/Lib/test/test_compiler.py
- Timestamp:
- Mar 19, 2014, 11:31:01 PM (11 years ago)
- Location:
- python/trunk
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
python/trunk
-
Property svn:mergeinfo
set to
/python/vendor/Python-2.7.6 merged eligible /python/vendor/current merged eligible
-
Property svn:mergeinfo
set to
-
python/trunk/Lib/test/test_compiler.py
r2 r391 24 24 25 25 next_time = time.time() + _PRINT_WORKING_MSG_INTERVAL 26 libdir = os.path.dirname(unittest.__file__) 26 # warning: if 'os' or 'test_support' are moved in some other dir, 27 # they should be changed here. 28 libdir = os.path.dirname(os.__file__) 27 29 testdir = os.path.dirname(test.test_support.__file__) 28 30 29 for dir in [ libdir,testdir]:30 for basename in os.listdir(dir):31 for dir in [testdir]: 32 for basename in "test_os.py",: 31 33 # Print still working message since this test can be really slow 32 34 if next_time <= time.time(): … … 76 78 def testTryExceptFinally(self): 77 79 # Test that except and finally clauses in one try stmt are recognized 78 c = compiler.compile("try:\n 1/ 0\nexcept:\n e = 1\nfinally:\n f = 1",80 c = compiler.compile("try:\n 1//0\nexcept:\n e = 1\nfinally:\n f = 1", 79 81 "<string>", "exec") 80 82 dct = {} 81 83 exec c in dct 82 self.assertEqual s(dct.get('e'), 1)83 self.assertEqual s(dct.get('f'), 1)84 self.assertEqual(dct.get('e'), 1) 85 self.assertEqual(dct.get('f'), 1) 84 86 85 87 def testDefaultArgs(self): … … 88 90 def testDocstrings(self): 89 91 c = compiler.compile('"doc"', '<string>', 'exec') 90 self.assert _('__doc__' inc.co_names)92 self.assertIn('__doc__', c.co_names) 91 93 c = compiler.compile('def f():\n "doc"', '<string>', 'exec') 92 94 g = {} 93 95 exec c in g 94 self.assertEqual s(g['f'].__doc__, "doc")96 self.assertEqual(g['f'].__doc__, "doc") 95 97 96 98 def testLineNo(self): … … 111 113 def _check_lineno(self, node): 112 114 if not node.__class__ in NOLINENO: 113 self.assert _(isinstance(node.lineno, int),115 self.assertIsInstance(node.lineno, int, 114 116 "lineno=%s on %s" % (node.lineno, node.__class__)) 115 self.assert _(node.lineno > 0,117 self.assertTrue(node.lineno > 0, 116 118 "lineno=%s on %s" % (node.lineno, node.__class__)) 117 119 for child in node.getChildNodes(): … … 119 121 120 122 def testFlatten(self): 121 self.assertEqual s(flatten([1, [2]]), [1, 2])122 self.assertEqual s(flatten((1, (2,))), [1, 2])123 self.assertEqual(flatten([1, [2]]), [1, 2]) 124 self.assertEqual(flatten((1, (2,))), [1, 2]) 123 125 124 126 def testNestedScope(self): … … 132 134 dct = {} 133 135 exec c in dct 134 self.assertEqual s(dct.get('result'), 3)136 self.assertEqual(dct.get('result'), 3) 135 137 136 138 def testGenExp(self): … … 139 141 '<string>', 140 142 'eval') 141 self.assertEquals(eval(c), [(0, 3), (1, 3), (2, 3)]) 143 self.assertEqual(eval(c), [(0, 3), (1, 3), (2, 3)]) 144 145 def testSetLiteral(self): 146 c = compiler.compile('{1, 2, 3}', '<string>', 'eval') 147 self.assertEqual(eval(c), {1,2,3}) 148 c = compiler.compile('{1, 2, 3,}', '<string>', 'eval') 149 self.assertEqual(eval(c), {1,2,3}) 150 151 def testDictLiteral(self): 152 c = compiler.compile('{1:2, 2:3, 3:4}', '<string>', 'eval') 153 self.assertEqual(eval(c), {1:2, 2:3, 3:4}) 154 c = compiler.compile('{1:2, 2:3, 3:4,}', '<string>', 'eval') 155 self.assertEqual(eval(c), {1:2, 2:3, 3:4}) 156 157 def testSetComp(self): 158 c = compiler.compile('{x for x in range(1, 4)}', '<string>', 'eval') 159 self.assertEqual(eval(c), {1, 2, 3}) 160 c = compiler.compile('{x * y for x in range(3) if x != 0' 161 ' for y in range(4) if y != 0}', 162 '<string>', 163 'eval') 164 self.assertEqual(eval(c), {1, 2, 3, 4, 6}) 165 166 def testDictComp(self): 167 c = compiler.compile('{x:x+1 for x in range(1, 4)}', '<string>', 'eval') 168 self.assertEqual(eval(c), {1:2, 2:3, 3:4}) 169 c = compiler.compile('{(x, y) : y for x in range(2) if x != 0' 170 ' for y in range(3) if y != 0}', 171 '<string>', 172 'eval') 173 self.assertEqual(eval(c), {(1, 2): 2, (1, 1): 1}) 142 174 143 175 def testWith(self): … … 152 184 dct = {'TrivialContext': TrivialContext} 153 185 exec c in dct 154 self.assertEqual s(dct.get('result'), 1)186 self.assertEqual(dct.get('result'), 1) 155 187 156 188 def testWithAss(self): … … 164 196 dct = {'TrivialContext': TrivialContext} 165 197 exec c in dct 166 self.assertEquals(dct.get('result'), 1) 167 198 self.assertEqual(dct.get('result'), 1) 199 200 def testWithMult(self): 201 events = [] 202 class Ctx: 203 def __init__(self, n): 204 self.n = n 205 def __enter__(self): 206 events.append(self.n) 207 def __exit__(self, *args): 208 pass 209 c = compiler.compile('from __future__ import with_statement\n' 210 'def f():\n' 211 ' with Ctx(1) as tc, Ctx(2) as tc2:\n' 212 ' return 1\n' 213 'result = f()', 214 '<string>', 215 'exec' ) 216 dct = {'Ctx': Ctx} 217 exec c in dct 218 self.assertEqual(dct.get('result'), 1) 219 self.assertEqual(events, [1, 2]) 220 221 def testGlobal(self): 222 code = compiler.compile('global x\nx=1', '<string>', 'exec') 223 d1 = {'__builtins__': {}} 224 d2 = {} 225 exec code in d1, d2 226 # x should be in the globals dict 227 self.assertEqual(d1.get('x'), 1) 168 228 169 229 def testPrintFunction(self): … … 175 235 dct = {'output': StringIO()} 176 236 exec c in dct 177 self.assertEqual s(dct['output'].getvalue(), 'a**b++')237 self.assertEqual(dct['output'].getvalue(), 'a**b++') 178 238 179 239 def _testErrEnc(self, src, text, offset): … … 181 241 compile(src, "", "exec") 182 242 except SyntaxError, e: 183 self.assertEqual s(e.offset, offset)184 self.assertEqual s(e.text, text)243 self.assertEqual(e.offset, offset) 244 self.assertEqual(e.text, text) 185 245 186 246 def testSourceCodeEncodingsError(self): … … 221 281 d = {'a': 2} 222 282 d = {} 283 d = {x: y for x, y in zip(range(5), range(5,10))} 284 s = {x for x in range(10)} 285 s = {1} 223 286 t = () 224 287 t = (1, 2) … … 248 311 def test_main(): 249 312 global TEST_ALL 250 TEST_ALL = test.test_support.is_resource_enabled("c ompiler")313 TEST_ALL = test.test_support.is_resource_enabled("cpu") 251 314 test.test_support.run_unittest(CompilerTest) 252 315
Note:
See TracChangeset
for help on using the changeset viewer.