|
1 | 1 | from test import test_support
|
2 |
| -from tokenize import (untokenize, generate_tokens, NUMBER, NAME, OP, |
| 2 | +from tokenize import (untokenize, generate_tokens, NUMBER, NAME, OP, NEWLINE, |
3 | 3 | STRING, ENDMARKER, tok_name, Untokenizer, tokenize)
|
4 | 4 | from StringIO import StringIO
|
5 | 5 | import os
|
6 | 6 | from unittest import TestCase
|
7 | 7 |
|
8 | 8 |
|
| 9 | +# Converts a source string into a list of textual representation |
| 10 | +# of the tokens such as: |
| 11 | +# ` NAME 'if' (1, 0) (1, 2)` |
| 12 | +# to make writing tests easier. |
| 13 | +def stringify_tokens_from_source(token_generator, source_string): |
| 14 | + result = [] |
| 15 | + num_lines = len(source_string.splitlines()) |
| 16 | + missing_trailing_nl = source_string[-1] not in '\r\n' |
| 17 | + |
| 18 | + for type, token, start, end, line in token_generator: |
| 19 | + if type == ENDMARKER: |
| 20 | + break |
| 21 | + # Ignore the new line on the last line if the input lacks one |
| 22 | + if missing_trailing_nl and type == NEWLINE and end[0] == num_lines: |
| 23 | + continue |
| 24 | + type = tok_name[type] |
| 25 | + result.append(" %(type)-10.10s %(token)-13.13r %(start)s %(end)s" % |
| 26 | + locals()) |
| 27 | + |
| 28 | + return result |
| 29 | + |
9 | 30 | class TokenizeTest(TestCase):
|
10 | 31 | # Tests for the tokenize module.
|
11 | 32 |
|
12 | 33 | # The tests can be really simple. Given a small fragment of source
|
13 |
| - # code, print out a table with tokens. The ENDMARKER is omitted for |
14 |
| - # brevity. |
| 34 | + # code, print out a table with tokens. The ENDMARKER, ENCODING and |
| 35 | + # final NEWLINE are omitted for brevity. |
15 | 36 |
|
16 | 37 | def check_tokenize(self, s, expected):
|
17 | 38 | # Format the tokens in s in a table format.
|
18 |
| - # The ENDMARKER is omitted. |
19 |
| - result = [] |
20 | 39 | f = StringIO(s)
|
21 |
| - for type, token, start, end, line in generate_tokens(f.readline): |
22 |
| - if type == ENDMARKER: |
23 |
| - break |
24 |
| - type = tok_name[type] |
25 |
| - result.append(" %(type)-10.10s %(token)-13.13r %(start)s %(end)s" % |
26 |
| - locals()) |
| 40 | + result = stringify_tokens_from_source(generate_tokens(f.readline), s) |
| 41 | + |
27 | 42 | self.assertEqual(result,
|
28 | 43 | expected.rstrip().splitlines())
|
29 | 44 |
|
| 45 | + def test_implicit_newline(self): |
| 46 | + # Make sure that the tokenizer puts in an implicit NEWLINE |
| 47 | + # when the input lacks a trailing new line. |
| 48 | + f = StringIO("x") |
| 49 | + tokens = list(generate_tokens(f.readline)) |
| 50 | + self.assertEqual(tokens[-2][0], NEWLINE) |
| 51 | + self.assertEqual(tokens[-1][0], ENDMARKER) |
30 | 52 |
|
31 | 53 | def test_basic(self):
|
32 | 54 | self.check_tokenize("1 + 1", """\
|
@@ -616,7 +638,7 @@ def test_roundtrip(self):
|
616 | 638 | self.check_roundtrip("if x == 1:\n"
|
617 | 639 | " print x\n")
|
618 | 640 | self.check_roundtrip("# This is a comment\n"
|
619 |
| - "# This also") |
| 641 | + "# This also\n") |
620 | 642 |
|
621 | 643 | # Some people use different formatting conventions, which makes
|
622 | 644 | # untokenize a little trickier. Note that this test involves trailing
|
|
0 commit comments