~ubuntu-branches/ubuntu/lucid/python2.6/lucid

« back to all changes in this revision

Viewing changes to Lib/lib2to3/tests/test_parser.py

  • Committer: Bazaar Package Importer
  • Author(s): Matthias Klose
  • Date: 2010-03-11 13:30:19 UTC
  • mto: (10.1.13 sid)
  • mto: This revision was merged to the branch mainline in revision 44.
  • Revision ID: james.westby@ubuntu.com-20100311133019-sblbooa3uqrkoe70
Tags: upstream-2.6.5~rc2
ImportĀ upstreamĀ versionĀ 2.6.5~rc2

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#!/usr/bin/env python2.5
2
1
"""Test suite for 2to3's parser and grammar files.
3
2
 
4
3
This is the place to add tests for changes to 2to3's grammar, such as those
6
5
parts of the grammar we've changed, we also make sure we can parse the
7
6
test_grammar.py files from both Python 2 and Python 3.
8
7
"""
9
 
# Author: Collin Winter
10
8
 
11
9
# Testing imports
12
10
from . import support
14
12
 
15
13
# Python imports
16
14
import os
17
 
import os.path
 
15
import io
 
16
import sys
18
17
 
19
18
# Local imports
 
19
from lib2to3.pgen2 import tokenize
20
20
from ..pgen2.parse import ParseError
21
21
 
22
22
 
148
148
    """A cut-down version of pytree_idempotency.py."""
149
149
 
150
150
    def test_all_project_files(self):
 
151
        if sys.platform.startswith("win"):
 
152
            # XXX something with newlines goes wrong on Windows.
 
153
            return
151
154
        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
            with open(filepath, "rb") as fp:
 
156
                encoding = tokenize.detect_encoding(fp.readline)[0]
 
157
            self.assertTrue(encoding is not None,
 
158
                            "can't detect encoding for %s" % filepath)
 
159
            with io.open(filepath, "r", encoding=encoding) as fp:
 
160
                source = fp.read()
 
161
            tree = driver.parse_string(source)
 
162
            new = unicode(tree)
 
163
            if diff(filepath, new, encoding):
155
164
                self.fail("Idempotency failed: %s" % filepath)
156
165
 
 
166
    def test_extended_unpacking(self):
 
167
        driver.parse_string("a, *b, c = x\n")
 
168
        driver.parse_string("[*a, b] = x\n")
 
169
        driver.parse_string("(z, *y, w) = m\n")
 
170
        driver.parse_string("for *z, m in d: pass\n")
157
171
 
158
172
class TestLiterals(GrammarTest):
159
173
 
 
174
    def validate(self, s):
 
175
        driver.parse_string(support.dedent(s) + "\n\n")
 
176
 
160
177
    def test_multiline_bytes_literals(self):
161
178
        s = """
162
179
            md5test(b"\xaa" * 80,
185
202
        self.validate(s)
186
203
 
187
204
 
188
 
def diff(fn, tree):
189
 
    f = open("@", "w")
 
205
def diff(fn, result, encoding):
 
206
    f = io.open("@", "w", encoding=encoding)
190
207
    try:
191
 
        f.write(str(tree))
 
208
        f.write(result)
192
209
    finally:
193
210
        f.close()
194
211
    try:
195
 
        return os.system("diff -u %s @" % fn)
 
212
        return os.system("diff -u %r @" % fn)
196
213
    finally:
197
214
        os.remove("@")
198
 
 
199
 
 
200
 
if __name__ == "__main__":
201
 
    import __main__
202
 
    support.run_all_tests(__main__)