+++ /dev/null
-"""Test suite for 2to3's parser and grammar files.\r
-\r
-This is the place to add tests for changes to 2to3's grammar, such as those\r
-merging the grammars for Python 2 and 3. In addition to specific tests for\r
-parts of the grammar we've changed, we also make sure we can parse the\r
-test_grammar.py files from both Python 2 and Python 3.\r
-"""\r
-\r
-from __future__ import with_statement\r
-\r
-# Testing imports\r
-from . import support\r
-from .support import driver, test_dir\r
-\r
-# Python imports\r
-import os\r
-import sys\r
-\r
-# Local imports\r
-from lib2to3.pgen2 import tokenize\r
-from ..pgen2.parse import ParseError\r
-from lib2to3.pygram import python_symbols as syms\r
-\r
-\r
-class TestDriver(support.TestCase):\r
-\r
- def test_formfeed(self):\r
- s = """print 1\n\x0Cprint 2\n"""\r
- t = driver.parse_string(s)\r
- self.assertEqual(t.children[0].children[0].type, syms.print_stmt)\r
- self.assertEqual(t.children[1].children[0].type, syms.print_stmt)\r
-\r
-\r
-class GrammarTest(support.TestCase):\r
- def validate(self, code):\r
- support.parse_string(code)\r
-\r
- def invalid_syntax(self, code):\r
- try:\r
- self.validate(code)\r
- except ParseError:\r
- pass\r
- else:\r
- raise AssertionError("Syntax shouldn't have been valid")\r
-\r
-\r
-class TestRaiseChanges(GrammarTest):\r
- def test_2x_style_1(self):\r
- self.validate("raise")\r
-\r
- def test_2x_style_2(self):\r
- self.validate("raise E, V")\r
-\r
- def test_2x_style_3(self):\r
- self.validate("raise E, V, T")\r
-\r
- def test_2x_style_invalid_1(self):\r
- self.invalid_syntax("raise E, V, T, Z")\r
-\r
- def test_3x_style(self):\r
- self.validate("raise E1 from E2")\r
-\r
- def test_3x_style_invalid_1(self):\r
- self.invalid_syntax("raise E, V from E1")\r
-\r
- def test_3x_style_invalid_2(self):\r
- self.invalid_syntax("raise E from E1, E2")\r
-\r
- def test_3x_style_invalid_3(self):\r
- self.invalid_syntax("raise from E1, E2")\r
-\r
- def test_3x_style_invalid_4(self):\r
- self.invalid_syntax("raise E from")\r
-\r
-\r
-# Adapated from Python 3's Lib/test/test_grammar.py:GrammarTests.testFuncdef\r
-class TestFunctionAnnotations(GrammarTest):\r
- def test_1(self):\r
- self.validate("""def f(x) -> list: pass""")\r
-\r
- def test_2(self):\r
- self.validate("""def f(x:int): pass""")\r
-\r
- def test_3(self):\r
- self.validate("""def f(*x:str): pass""")\r
-\r
- def test_4(self):\r
- self.validate("""def f(**x:float): pass""")\r
-\r
- def test_5(self):\r
- self.validate("""def f(x, y:1+2): pass""")\r
-\r
- def test_6(self):\r
- self.validate("""def f(a, (b:1, c:2, d)): pass""")\r
-\r
- def test_7(self):\r
- self.validate("""def f(a, (b:1, c:2, d), e:3=4, f=5, *g:6): pass""")\r
-\r
- def test_8(self):\r
- s = """def f(a, (b:1, c:2, d), e:3=4, f=5,\r
- *g:6, h:7, i=8, j:9=10, **k:11) -> 12: pass"""\r
- self.validate(s)\r
-\r
-\r
-class TestExcept(GrammarTest):\r
- def test_new(self):\r
- s = """\r
- try:\r
- x\r
- except E as N:\r
- y"""\r
- self.validate(s)\r
-\r
- def test_old(self):\r
- s = """\r
- try:\r
- x\r
- except E, N:\r
- y"""\r
- self.validate(s)\r
-\r
-\r
-# Adapted from Python 3's Lib/test/test_grammar.py:GrammarTests.testAtoms\r
-class TestSetLiteral(GrammarTest):\r
- def test_1(self):\r
- self.validate("""x = {'one'}""")\r
-\r
- def test_2(self):\r
- self.validate("""x = {'one', 1,}""")\r
-\r
- def test_3(self):\r
- self.validate("""x = {'one', 'two', 'three'}""")\r
-\r
- def test_4(self):\r
- self.validate("""x = {2, 3, 4,}""")\r
-\r
-\r
-class TestNumericLiterals(GrammarTest):\r
- def test_new_octal_notation(self):\r
- self.validate("""0o7777777777777""")\r
- self.invalid_syntax("""0o7324528887""")\r
-\r
- def test_new_binary_notation(self):\r
- self.validate("""0b101010""")\r
- self.invalid_syntax("""0b0101021""")\r
-\r
-\r
-class TestClassDef(GrammarTest):\r
- def test_new_syntax(self):\r
- self.validate("class B(t=7): pass")\r
- self.validate("class B(t, *args): pass")\r
- self.validate("class B(t, **kwargs): pass")\r
- self.validate("class B(t, *args, **kwargs): pass")\r
- self.validate("class B(t, y=9, *args, **kwargs): pass")\r
-\r
-\r
-class TestParserIdempotency(support.TestCase):\r
-\r
- """A cut-down version of pytree_idempotency.py."""\r
-\r
- def test_all_project_files(self):\r
- if sys.platform.startswith("win"):\r
- # XXX something with newlines goes wrong on Windows.\r
- return\r
- for filepath in support.all_project_files():\r
- with open(filepath, "rb") as fp:\r
- encoding = tokenize.detect_encoding(fp.readline)[0]\r
- self.assertTrue(encoding is not None,\r
- "can't detect encoding for %s" % filepath)\r
- with open(filepath, "r") as fp:\r
- source = fp.read()\r
- source = source.decode(encoding)\r
- tree = driver.parse_string(source)\r
- new = unicode(tree)\r
- if diff(filepath, new, encoding):\r
- self.fail("Idempotency failed: %s" % filepath)\r
-\r
- def test_extended_unpacking(self):\r
- driver.parse_string("a, *b, c = x\n")\r
- driver.parse_string("[*a, b] = x\n")\r
- driver.parse_string("(z, *y, w) = m\n")\r
- driver.parse_string("for *z, m in d: pass\n")\r
-\r
-class TestLiterals(GrammarTest):\r
-\r
- def validate(self, s):\r
- driver.parse_string(support.dedent(s) + "\n\n")\r
-\r
- def test_multiline_bytes_literals(self):\r
- s = """\r
- md5test(b"\xaa" * 80,\r
- (b"Test Using Larger Than Block-Size Key "\r
- b"and Larger Than One Block-Size Data"),\r
- "6f630fad67cda0ee1fb1f562db3aa53e")\r
- """\r
- self.validate(s)\r
-\r
- def test_multiline_bytes_tripquote_literals(self):\r
- s = '''\r
- b"""\r
- <?xml version="1.0" encoding="UTF-8"?>\r
- <!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN">\r
- """\r
- '''\r
- self.validate(s)\r
-\r
- def test_multiline_str_literals(self):\r
- s = """\r
- md5test("\xaa" * 80,\r
- ("Test Using Larger Than Block-Size Key "\r
- "and Larger Than One Block-Size Data"),\r
- "6f630fad67cda0ee1fb1f562db3aa53e")\r
- """\r
- self.validate(s)\r
-\r
-\r
-def diff(fn, result, encoding):\r
- f = open("@", "w")\r
- try:\r
- f.write(result.encode(encoding))\r
- finally:\r
- f.close()\r
- try:\r
- fn = fn.replace('"', '\\"')\r
- return os.system('diff -u "%s" @' % fn)\r
- finally:\r
- os.remove("@")\r