1616
1717"""Tests for ExprVisitor."""
1818
19+ from __future__ import unicode_literals
20+
1921import ast
2022import subprocess
2123import textwrap
@@ -35,11 +37,13 @@ def Test(self):
3537 return Test
3638
3739
38- def _MakeLiteralTest (lit ):
40+ def _MakeLiteralTest (lit , expected = None ):
41+ if expected is None :
42+ expected = lit
3943 def Test (self ):
4044 status , output = _GrumpRun ('print repr({}),' .format (lit ))
4145 self .assertEqual (0 , status , output )
42- self .assertEqual (eval ( 'repr({})' . format ( lit )) , output .strip ()) # pylint: disable=eval-used
46+ self .assertEqual (expected , output .strip ()) # pylint: disable=eval-used
4347 return Test
4448
4549
@@ -130,9 +134,9 @@ def foo(a, b=2):
130134 testCompareNotInTuple = _MakeExprTest ('10 < 12 not in (1, 2, 3)' )
131135
132136 testDictEmpty = _MakeLiteralTest ('{}' )
133- testDictNonEmpty = _MakeLiteralTest ('{" foo" : 42, " bar" : 43}' )
137+ testDictNonEmpty = _MakeLiteralTest ("{' foo' : 42, ' bar' : 43}" )
134138
135- testSetNoneEmpty = _MakeLiteralTest ('{" foo", "bar"}' )
139+ testSetNonEmpty = _MakeLiteralTest ("{' foo', 'bar'} " , "set(['foo', ' bar'])" )
136140
137141 testDictCompFor = _MakeExprTest ('{x: str(x) for x in range(3)}' )
138142 testDictCompForIf = _MakeExprTest (
@@ -181,16 +185,15 @@ def foo():
181185
182186 testNumInt = _MakeLiteralTest ('42' )
183187 testNumLong = _MakeLiteralTest ('42L' )
184- testNumIntLarge = _MakeLiteralTest ('12345678901234567890' )
188+ testNumIntLarge = _MakeLiteralTest ('12345678901234567890' ,
189+ '12345678901234567890L' )
185190 testNumFloat = _MakeLiteralTest ('102.1' )
186- testNumFloatOnlyDecimal = _MakeLiteralTest ('.5' )
187- # TODO: Current Grumpy's repr on float has different behavior than CPython.
188- # so skip these for now.
189- testNumFloatNoDecimal = unittest .expectedFailure (_MakeLiteralTest ('5.' ))
190- testNumFloatSci = unittest .expectedFailure (_MakeLiteralTest ('1e6' ))
191- testNumFloatSciCap = unittest .expectedFailure (_MakeLiteralTest ('1E6' ))
192- testNumFloatSciCapPlus = unittest .expectedFailure (_MakeLiteralTest ('1E+6' ))
193- testNumFloatSciMinus = _MakeLiteralTest ('1e-6' )
191+ testNumFloatOnlyDecimal = _MakeLiteralTest ('.5' , '0.5' )
192+ testNumFloatNoDecimal = _MakeLiteralTest ('5.' , '5' )
193+ testNumFloatSci = _MakeLiteralTest ('1e6' , '1e+06' )
194+ testNumFloatSciCap = _MakeLiteralTest ('1E6' , '1e+06' )
195+ testNumFloatSciCapPlus = _MakeLiteralTest ('1E+6' , '1e+06' )
196+ testNumFloatSciMinus = _MakeLiteralTest ('1e-06' )
194197 testNumComplex = _MakeLiteralTest ('3j' )
195198
196199 testSubscriptDictStr = _MakeExprTest ('{"foo": 42}["foo"]' )
@@ -205,14 +208,14 @@ def foo():
205208 testSubscriptMultiDimSlice = _MakeSliceTest (
206209 "'foo','bar':'baz':'qux'" , "('foo', slice('bar', 'baz', 'qux'))" )
207210
208- testStrEmpty = _MakeLiteralTest ('""' )
209- testStrAscii = _MakeLiteralTest ('" abc"' )
210- testStrUtf8 = _MakeLiteralTest (r'" \tfoo\n\xcf\x80"' )
211- testStrQuoted = _MakeLiteralTest ('\' "foo"\' ' )
212- testStrUtf16 = _MakeLiteralTest (r'u"\ u0432\u043e\u043b\u043d"' )
211+ testStrEmpty = _MakeLiteralTest ("''" )
212+ testStrAscii = _MakeLiteralTest ("' abc'" )
213+ testStrUtf8 = _MakeLiteralTest (r"' \tfoo\n\xcf\x80'" )
214+ testStrQuoted = _MakeLiteralTest ('\' "foo"\' ' , ' \' "foo" \' ' )
215+ testStrUtf16 = _MakeLiteralTest ("u' \\ u0432\\ u043e\\ u043b\\ u043d'" )
213216
214- testTupleEmpty = _MakeLiteralTest (() )
215- testTupleNonEmpty = _MakeLiteralTest ((1 , 2 , 3 ))
217+ testTupleEmpty = _MakeLiteralTest ('()' )
218+ testTupleNonEmpty = _MakeLiteralTest (' (1, 2, 3)' )
216219
217220 testUnaryOpNot = _MakeExprTest ('not True' )
218221 testUnaryOpInvert = _MakeExprTest ('~4' )
@@ -223,7 +226,7 @@ def testUnaryOpNotImplemented(self):
223226
224227
225228def _MakeModuleBlock ():
226- return block .ModuleBlock ('__main__' , 'grumpy' , 'grumpy/lib' , '<test>' , [] ,
229+ return block .ModuleBlock ('__main__' , 'grumpy' , 'grumpy/lib' , '<test>' , '' ,
227230 stmt .FutureFeatures ())
228231
229232
@@ -235,7 +238,7 @@ def _ParseAndVisitExpr(expr):
235238 writer = util .Writer ()
236239 visitor = expr_visitor .ExprVisitor (_MakeModuleBlock (), writer )
237240 visitor .visit (_ParseExpr (expr ))
238- return writer .out . getvalue ()
241+ return writer .getvalue ()
239242
240243
241244def _GrumpRun (cmd ):
0 commit comments