OILS / frontend / lexer_gen_test.py View on Github | oilshell.org

53 lines, 38 significant
1#!/usr/bin/env python2
2"""
3lexer_gen_test.py: Tests for lexer_gen.py
4"""
5from __future__ import print_function
6
7import unittest
8
9from frontend import lexer_gen # module under test
10from core import test_lib
11
12
13class LexerGenTest(unittest.TestCase):
14
15 def testTranslateRegex(self):
16 PAIRS = [
17 (r'a', r'"a" '),
18 (r'[a-z]', r'[a-z]'),
19 (r'[a-zA-Z.]+', r'[a-zA-Z.]+ '),
20 (r'[a-zA-Z_][a-zA-Z0-9_]*\+?=',
21 r'[a-zA-Z_][a-zA-Z0-9_]* "+" ? "=" '),
22 (r'[."]*', r'[."]* '),
23 (r'\$', r'"$" '),
24 (r'.*', r'.* '),
25
26 # Both of these accepted?
27 ('\0', r'"\x00" '),
28 (r'\0', r'"\x00" '),
29 (r'\\', r'"\\" '),
30 (r'[\\]', r'"\\" '),
31 (r'.', r'.'),
32 (r'[^a]', r'[^a]'),
33 (r'[^a-z]', r'[^a-z]'),
34 # . isn't special inside
35 (r'[a\.]', r'[a.]'),
36 (r'[0-7]{1,3}', r'[0-7]{1,3} '),
37 (r'foo', r'"f" "o" "o" '),
38 (r'[0-9]|foo', r'[0-9] | "f" "o" "o" '),
39 (r'[^\x00-\x1f]', r'[^\x00-\x1f]'),
40 (r'[^\0\n"]', r'[^\x00\n"]'),
41 ]
42 for py, expected in PAIRS:
43 #self.assertEqual(expected, lexer_gen.TranslateRegex(py))
44 print('--- %s' % test_lib.PrintableString(py))
45 actual = lexer_gen.TranslateRegex(py)
46 print(repr(actual))
47 self.assertEqual(expected, actual)
48 print()
49 print()
50
51
52if __name__ == '__main__':
53 unittest.main()