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)\n IDENTIFIER('myVariable', 1:5:15)\n EQUALS('=', 1:16:17)\n NUMBER('123', 1:18:21)\n SEMI(';', 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)\n EQUALS('=', 1:3:4)\n IDENTIFIER('y', 1:5:6)\n PLUS('+', 1:7:8)\n IDENTIFIER('z', 1:9:10)\n SEMI(';', 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)\n IDENTIFIER('num', 1:5:8)\n EQUALS('=', 1:9:10)\n NUMBER('42', 1:11:13)\n SEMI(';', 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)\n LPAREN(\' (\' , 1:7:8)\n STRING(\' "Hello, World!"\' , 1:8:23)\n RPAREN(\' )\' , 1:23:24)\n SEMI(\' ;\' , 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)\n IDENTIFIER(\' a\' , 1:6:7)\n EQUALS(\' =\' , 1:8:9)\n CHARACTER("\' \\ n\' ", 1:10:13)\n SEMI(\' ;\' , 1:13:14)\n CHAR(\' char\' , 1:15:19)\n IDENTIFIER(\' b\' , 1:20:21)\n EQUALS(\' =\' , 1:22:23)\n CHARACTER("\' A\' ", 1:24:27)\n SEMI(\' ;\' , 1:27:28)\n CHAR(\' char\' , 1:29:33)\n IDENTIFIER(\' c\' , 1:34:35)\n EQUALS(\' =\' , 1:36:37)\n CHARACTER("\' ", 1:38:39)\n BACKSLASH(\' \\ \\ \' , 1:39:40)\n CHARACTER("\' ", 1:40:41)\n SEMI(\' ;\' , 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 ()
0 commit comments