1 | #!/usr/bin/env python2
|
2 | """
|
3 | match_test.py: Tests for match.py
|
4 | """
|
5 | from __future__ import print_function
|
6 |
|
7 | import unittest
|
8 |
|
9 | from _devbuild.gen.id_kind_asdl import Id, Id_str
|
10 | from mycpp.mylib import log
|
11 | from frontend import match # module under test
|
12 |
|
13 |
|
14 | def _PrintTokens(lex):
|
15 | for id_, val in lex.Tokens():
|
16 | log(' %s %r', Id_str(id_), val)
|
17 | if id_ == Id.Eol_Tok:
|
18 | break
|
19 |
|
20 |
|
21 | class MatchTest(unittest.TestCase):
|
22 |
|
23 | def testShouldHijack(self):
|
24 | self.assertEqual(False, match.ShouldHijack('# comment\n[line 2]'))
|
25 | self.assertEqual(False, match.ShouldHijack('#!/usr/bin/python\n'))
|
26 | self.assertEqual(False, match.ShouldHijack(''))
|
27 | self.assertEqual(False, match.ShouldHijack('\n'))
|
28 |
|
29 | self.assertEqual(True, match.ShouldHijack('#!/usr/bin/env bash\n'))
|
30 |
|
31 | self.assertEqual(True, match.ShouldHijack('#!/bin/bash\n[line 2]'))
|
32 | self.assertEqual(True, match.ShouldHijack('#!/bin/bash -e\n[line 2]'))
|
33 | self.assertEqual(True, match.ShouldHijack('#!/bin/sh\n[line 2]\n'))
|
34 | self.assertEqual(True, match.ShouldHijack('#!/bin/sh -e\n[line 2]\n'))
|
35 |
|
36 | # Unlikely but OK
|
37 | self.assertEqual(True, match.ShouldHijack('#!/usr/bin/env sh\n'))
|
38 |
|
39 | # fastlex bug: should not allow \0
|
40 | self.assertEqual(False, match.ShouldHijack('#!/usr/bin/env \0 sh\n'))
|
41 |
|
42 | def testBraceRangeLexer(self):
|
43 | lex = match.BraceRangeLexer('1..3')
|
44 | _PrintTokens(lex)
|
45 |
|
46 | def testJ8Lexer(self):
|
47 | cases = [
|
48 | '00',
|
49 | '[]',
|
50 | '[3.14, 4, true]',
|
51 | 'truez',
|
52 | 'false\t',
|
53 | 'bad',
|
54 | ]
|
55 |
|
56 | for s in cases:
|
57 | log('---')
|
58 | log('J8 CASE %r', s)
|
59 | lex = match.SimpleLexer(match.MatchJ8Token, s)
|
60 | _PrintTokens(lex)
|
61 |
|
62 | def testJ8LinesLexer(self):
|
63 | cases = [
|
64 | ' "hello"',
|
65 | " u'hi",
|
66 | " b'hi",
|
67 | " 'hi",
|
68 | ' multiple words ',
|
69 | ]
|
70 |
|
71 | for s in cases:
|
72 | log('---')
|
73 | log('J8 LINES CASE %r', s)
|
74 | lex = match.SimpleLexer(match.MatchJ8LinesToken, s)
|
75 | _PrintTokens(lex)
|
76 |
|
77 | def testJ8StrLexer(self):
|
78 | cases = [
|
79 | '"hi"',
|
80 | # Newlines in strings are control chars, not accepted
|
81 | '"hi\n"',
|
82 | '"hi\\n"',
|
83 | r'"\yff \xff \u1234 \u{123456} \\ \" "',
|
84 |
|
85 | # This points at \ as Id.Unknown_Tok, which I suppose is OK
|
86 | r'"\a \z \/ \b "',
|
87 | ]
|
88 |
|
89 | for s in cases:
|
90 | log('---')
|
91 | log('J8 STR CASE %r', s)
|
92 | lex = match.SimpleLexer(match.MatchJ8StrToken, s)
|
93 | _PrintTokens(lex)
|
94 |
|
95 | def testLooksLike(self):
|
96 | INTS = [
|
97 | (False, ''),
|
98 | (False, 'foo'),
|
99 | (True, '3'),
|
100 | (True, '-3'),
|
101 | (False, '-'),
|
102 | (False, '.'),
|
103 | (True, '\t12 '),
|
104 | (True, '\t-12 '),
|
105 | (False, ' - 12 '),
|
106 | ]
|
107 |
|
108 | MORE_INTS = [
|
109 | (True, ' 3_000 '),
|
110 | ]
|
111 |
|
112 | for expected, s in INTS + MORE_INTS:
|
113 | self.assertEqual(expected, match.LooksLikeInteger(s))
|
114 |
|
115 | FLOATS = [
|
116 | (True, '3.0'),
|
117 | (True, '-3.0'),
|
118 | (True, '\t3.0 '),
|
119 | (True, '\t-3.0 '),
|
120 | (False, ' - 3.0 '),
|
121 | ]
|
122 |
|
123 | for expected, s in INTS + FLOATS: # Use BOTH test cases
|
124 | self.assertEqual(expected, match.LooksLikeFloat(s), s)
|
125 |
|
126 |
|
127 | if __name__ == '__main__':
|
128 | unittest.main()
|