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

131 lines, 96 significant
1#!/usr/bin/env python2
2"""
3match_test.py: Tests for match.py
4"""
5from __future__ import print_function
6
7import unittest
8
9from _devbuild.gen.id_kind_asdl import Id, Id_str
10from mycpp.mylib import log
11from frontend import match # module under test
12
13
14def _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
21class 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 self.assertEqual(False, match.LooksLikeInteger(' 3_000 '))
97 self.assertEqual(False, match.LooksLikeInteger(' '))
98
99 INTS = [
100 (False, ''),
101 (False, 'foo'),
102 (True, '3'),
103 (True, '-3'),
104 (False, '-'),
105 (False, '.'),
106 (True, '\t12 '),
107 (True, '\t-12 '),
108 (False, ' - 12 '),
109 ]
110
111 MORE_INTS = [
112 (True, ' 3_000 '),
113 ]
114
115 for expected, s in INTS + MORE_INTS:
116 self.assertEqual(expected, match.LooksLikeYshInt(s))
117
118 FLOATS = [
119 (True, '3.0'),
120 (True, '-3.0'),
121 (True, '\t3.0 '),
122 (True, '\t-3.0 '),
123 (False, ' - 3.0 '),
124 ]
125
126 for expected, s in INTS + FLOATS: # Use BOTH test cases
127 self.assertEqual(expected, match.LooksLikeYshFloat(s), s)
128
129
130if __name__ == '__main__':
131 unittest.main()