PageRenderTime 245ms CodeModel.GetById 141ms app.highlight 12ms RepoModel.GetById 88ms app.codeStats 0ms

/Lib/lib2to3/tests/test_parser.py

http://unladen-swallow.googlecode.com/
Python | 202 lines | 175 code | 1 blank | 26 comment | 3 complexity | cbd31809d8ce5f34919c9cc1e562af92 MD5 | raw file
  1#!/usr/bin/env python2.5
  2"""Test suite for 2to3's parser and grammar files.
  3
  4This is the place to add tests for changes to 2to3's grammar, such as those
  5merging the grammars for Python 2 and 3. In addition to specific tests for
  6parts of the grammar we've changed, we also make sure we can parse the
  7test_grammar.py files from both Python 2 and Python 3.
  8"""
  9# Author: Collin Winter
 10
 11# Testing imports
 12from . import support
 13from .support import driver, test_dir
 14
 15# Python imports
 16import os
 17import os.path
 18
 19# Local imports
 20from ..pgen2.parse import ParseError
 21
 22
 23class GrammarTest(support.TestCase):
 24    def validate(self, code):
 25        support.parse_string(code)
 26
 27    def invalid_syntax(self, code):
 28        try:
 29            self.validate(code)
 30        except ParseError:
 31            pass
 32        else:
 33            raise AssertionError("Syntax shouldn't have been valid")
 34
 35
 36class TestRaiseChanges(GrammarTest):
 37    def test_2x_style_1(self):
 38        self.validate("raise")
 39
 40    def test_2x_style_2(self):
 41        self.validate("raise E, V")
 42
 43    def test_2x_style_3(self):
 44        self.validate("raise E, V, T")
 45
 46    def test_2x_style_invalid_1(self):
 47        self.invalid_syntax("raise E, V, T, Z")
 48
 49    def test_3x_style(self):
 50        self.validate("raise E1 from E2")
 51
 52    def test_3x_style_invalid_1(self):
 53        self.invalid_syntax("raise E, V from E1")
 54
 55    def test_3x_style_invalid_2(self):
 56        self.invalid_syntax("raise E from E1, E2")
 57
 58    def test_3x_style_invalid_3(self):
 59        self.invalid_syntax("raise from E1, E2")
 60
 61    def test_3x_style_invalid_4(self):
 62        self.invalid_syntax("raise E from")
 63
 64
 65# Adapated from Python 3's Lib/test/test_grammar.py:GrammarTests.testFuncdef
 66class TestFunctionAnnotations(GrammarTest):
 67    def test_1(self):
 68        self.validate("""def f(x) -> list: pass""")
 69
 70    def test_2(self):
 71        self.validate("""def f(x:int): pass""")
 72
 73    def test_3(self):
 74        self.validate("""def f(*x:str): pass""")
 75
 76    def test_4(self):
 77        self.validate("""def f(**x:float): pass""")
 78
 79    def test_5(self):
 80        self.validate("""def f(x, y:1+2): pass""")
 81
 82    def test_6(self):
 83        self.validate("""def f(a, (b:1, c:2, d)): pass""")
 84
 85    def test_7(self):
 86        self.validate("""def f(a, (b:1, c:2, d), e:3=4, f=5, *g:6): pass""")
 87
 88    def test_8(self):
 89        s = """def f(a, (b:1, c:2, d), e:3=4, f=5,
 90                        *g:6, h:7, i=8, j:9=10, **k:11) -> 12: pass"""
 91        self.validate(s)
 92
 93
 94class TestExcept(GrammarTest):
 95    def test_new(self):
 96        s = """
 97            try:
 98                x
 99            except E as N:
100                y"""
101        self.validate(s)
102
103    def test_old(self):
104        s = """
105            try:
106                x
107            except E, N:
108                y"""
109        self.validate(s)
110
111
112# Adapted from Python 3's Lib/test/test_grammar.py:GrammarTests.testAtoms
113class TestSetLiteral(GrammarTest):
114    def test_1(self):
115        self.validate("""x = {'one'}""")
116
117    def test_2(self):
118        self.validate("""x = {'one', 1,}""")
119
120    def test_3(self):
121        self.validate("""x = {'one', 'two', 'three'}""")
122
123    def test_4(self):
124        self.validate("""x = {2, 3, 4,}""")
125
126
127class TestNumericLiterals(GrammarTest):
128    def test_new_octal_notation(self):
129        self.validate("""0o7777777777777""")
130        self.invalid_syntax("""0o7324528887""")
131
132    def test_new_binary_notation(self):
133        self.validate("""0b101010""")
134        self.invalid_syntax("""0b0101021""")
135
136
137class TestClassDef(GrammarTest):
138    def test_new_syntax(self):
139        self.validate("class B(t=7): pass")
140        self.validate("class B(t, *args): pass")
141        self.validate("class B(t, **kwargs): pass")
142        self.validate("class B(t, *args, **kwargs): pass")
143        self.validate("class B(t, y=9, *args, **kwargs): pass")
144
145
146class TestParserIdempotency(support.TestCase):
147
148    """A cut-down version of pytree_idempotency.py."""
149
150    def test_all_project_files(self):
151        for filepath in support.all_project_files():
152            print "Parsing %s..." % filepath
153            tree = driver.parse_file(filepath, debug=True)
154            if diff(filepath, tree):
155                self.fail("Idempotency failed: %s" % filepath)
156
157
158class TestLiterals(GrammarTest):
159
160    def test_multiline_bytes_literals(self):
161        s = """
162            md5test(b"\xaa" * 80,
163                    (b"Test Using Larger Than Block-Size Key "
164                     b"and Larger Than One Block-Size Data"),
165                    "6f630fad67cda0ee1fb1f562db3aa53e")
166            """
167        self.validate(s)
168
169    def test_multiline_bytes_tripquote_literals(self):
170        s = '''
171            b"""
172            <?xml version="1.0" encoding="UTF-8"?>
173            <!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN">
174            """
175            '''
176        self.validate(s)
177
178    def test_multiline_str_literals(self):
179        s = """
180            md5test("\xaa" * 80,
181                    ("Test Using Larger Than Block-Size Key "
182                     "and Larger Than One Block-Size Data"),
183                    "6f630fad67cda0ee1fb1f562db3aa53e")
184            """
185        self.validate(s)
186
187
188def diff(fn, tree):
189    f = open("@", "w")
190    try:
191        f.write(str(tree))
192    finally:
193        f.close()
194    try:
195        return os.system("diff -u %s @" % fn)
196    finally:
197        os.remove("@")
198
199
200if __name__ == "__main__":
201    import __main__
202    support.run_all_tests(__main__)