Skip to content

Commit 33cb7a0

Browse files
committed
simpler indent expression + tests
1 parent a041e52 commit 33cb7a0

File tree

3 files changed

+133
-8
lines changed

3 files changed

+133
-8
lines changed
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
"""Tests for scripts in the Tools directory.
2+
3+
This file contains regression tests for some of the scripts found in the
4+
Tools directory of a Python checkout or tarball.
5+
"""
6+
7+
import os
8+
import unittest
9+
from test.support.script_helper import assert_python_ok
10+
11+
from test.test_tools import toolsdir, skip_if_missing
12+
13+
skip_if_missing()
14+
15+
class ReindentTests(unittest.TestCase):
16+
script = os.path.join(toolsdir, 'cases_generator', 'lexer.py')
17+
18+
def test_multiline_comment_dedent_dedent4(self):
19+
input_code = """
20+
int main() {
21+
/*
22+
This is a
23+
multi-line comment.
24+
Let's see if it de-indents correctly.
25+
*/
26+
return 0;
27+
}
28+
29+
"""
30+
31+
expected_output = """
32+
int main() {
33+
/*
34+
This is a
35+
multi-line comment.
36+
Let's see if it de-indents correctly.
37+
*/
38+
return 0;
39+
}
40+
"""
41+
42+
dedent_amount = '4'
43+
rc, out, err = assert_python_ok(self.script, '-c', input_code, dedent_amount)
44+
self.assertEqual(out, bytes(expected_output, 'utf-8')[1:], "Multi-line comment de-indentation failed")
45+
46+
def test_multiline_comment_dedent_dedent40(self):
47+
input_code = """
48+
int main() {
49+
/*
50+
This is a
51+
multi-line comment.
52+
Let's see if it de-indents correctly.
53+
*/
54+
return 0;
55+
}
56+
57+
"""
58+
59+
expected_output = """
60+
int main() {
61+
/*
62+
This is a
63+
multi-line comment.
64+
Let's see if it de-indents correctly.
65+
*/
66+
return 0;
67+
}
68+
"""
69+
70+
dedent_amount = '40'
71+
rc, out, err = assert_python_ok(self.script, '-c', input_code, dedent_amount)
72+
self.assertEqual(out, bytes(expected_output, 'utf-8')[1:], "Multi-line comment de-indentation failed")
73+
74+
if __name__ == '__main__':
75+
unittest.main()
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
"""Tests for scripts in the Tools directory.
2+
3+
This file contains regression tests for some of the scripts found in the
4+
Tools directory of a Python checkout or tarball.
5+
"""
6+
7+
import os
8+
import unittest
9+
from test.support.script_helper import assert_python_ok
10+
from test.support import findfile
11+
12+
from test.test_tools import toolsdir, skip_if_missing
13+
14+
skip_if_missing()
15+
16+
class TokenizeTests(unittest.TestCase):
17+
script = os.path.join(toolsdir, 'cases_generator', 'lexer.py')
18+
19+
def test_identifiers(self):
20+
code = "int myVariable = 123;"
21+
expected_out = bytes("INT('int', 1:1:4)\nIDENTIFIER('myVariable', 1:5:15)\nEQUALS('=', 1:16:17)\nNUMBER('123', 1:18:21)\nSEMI(';', 1:21:22)\n", 'utf-8')
22+
rc, out, err = assert_python_ok(self.script, '-c', code)
23+
self.assertEqual(out, expected_out)
24+
25+
def test_operators(self):
26+
code = "x = y + z;"
27+
expected_out = bytes("IDENTIFIER('x', 1:1:2)\nEQUALS('=', 1:3:4)\nIDENTIFIER('y', 1:5:6)\nPLUS('+', 1:7:8)\nIDENTIFIER('z', 1:9:10)\nSEMI(';', 1:10:11)\n", 'utf-8')
28+
rc, out, err = assert_python_ok(self.script, '-c', code)
29+
self.assertEqual(out, expected_out)
30+
31+
def test_numbers(self):
32+
code = "int num = 42;"
33+
expected_out = bytes("INT('int', 1:1:4)\nIDENTIFIER('num', 1:5:8)\nEQUALS('=', 1:9:10)\nNUMBER('42', 1:11:13)\nSEMI(';', 1:13:14)\n", 'utf-8')
34+
rc, out, err = assert_python_ok(self.script, '-c', code)
35+
self.assertEqual(out, expected_out)
36+
37+
def test_strings(self):
38+
code = 'printf("Hello, World!");'
39+
expected_out = bytes("""IDENTIFIER(\'printf\', 1:1:7)\nLPAREN(\'(\', 1:7:8)\nSTRING(\'"Hello, World!"\', 1:8:23)\nRPAREN(\')\', 1:23:24)\nSEMI(\';\', 1:24:25)\n""", 'utf-8')
40+
rc, out, err = assert_python_ok(self.script, '-c', code)
41+
self.assertEqual(out, expected_out)
42+
43+
def test_characters_with_escape_sequences(self):
44+
code = "char a = '\n'; char b = '\x41'; char c = '\\';"
45+
expected_out = bytes("""CHAR(\'char\', 1:1:5)\nIDENTIFIER(\'a\', 1:6:7)\nEQUALS(\'=\', 1:8:9)\nCHARACTER("\'\\n\'", 1:10:13)\nSEMI(\';\', 1:13:14)\nCHAR(\'char\', 1:15:19)\nIDENTIFIER(\'b\', 1:20:21)\nEQUALS(\'=\', 1:22:23)\nCHARACTER("\'A\'", 1:24:27)\nSEMI(\';\', 1:27:28)\nCHAR(\'char\', 1:29:33)\nIDENTIFIER(\'c\', 1:34:35)\nEQUALS(\'=\', 1:36:37)\nCHARACTER("\'", 1:38:39)\nBACKSLASH(\'\\\\\', 1:39:40)\nCHARACTER("\'", 1:40:41)\nSEMI(\';\', 1:41:42)\n""", 'utf-8')
46+
rc, out, err = assert_python_ok(self.script, '-c', code)
47+
self.assertEqual(out, expected_out)
48+
49+
if __name__ == '__main__':
50+
unittest.main()

Tools/cases_generator/lexer.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -347,11 +347,8 @@ def to_text(tkns: list[Token], dedent: int = 0) -> str:
347347
elif dedent > 0:
348348
temp: list[str] = []
349349
for line in text.split("\n"):
350-
leading_space = len(line) - len(line.lstrip())
351-
if leading_space > dedent:
352-
line = re.sub(r'(?m)^[ \t]{' + str(dedent) + r'}', '', line)
353-
else:
354-
line = re.sub(r'(?m)^[ \t]{' + str(leading_space) + r'}', '', line)
350+
leading_space = len(line) - len(line.lstrip(' '))
351+
line = line[min(leading_space, dedent):]
355352
temp.append(line)
356353
text = "\n".join(temp)
357354
res.append(text)
@@ -367,6 +364,9 @@ def to_text(tkns: list[Token], dedent: int = 0) -> str:
367364
src = sys.argv[2]
368365
else:
369366
src = open(filename).read()
370-
# print(to_text(tokenize(src)))
371-
for tkn in tokenize(src, filename=filename):
372-
print(tkn)
367+
368+
dedent = int(sys.argv[3])
369+
print(to_text(tokenize(src), dedent))
370+
371+
# for tkn in tokenize(src, filename=filename):
372+
# print(tkn)

0 commit comments

Comments
 (0)