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 class MatchTest(unittest.TestCase):
15 def testShouldHijack(self):
16 self.assertEqual(False, match.ShouldHijack('# comment\n[line 2]'))
17 self.assertEqual(False, match.ShouldHijack('#!/usr/bin/python\n'))
18 self.assertEqual(False, match.ShouldHijack(''))
19 self.assertEqual(False, match.ShouldHijack('\n'))
20
21 self.assertEqual(True, match.ShouldHijack('#!/usr/bin/env bash\n'))
22
23 self.assertEqual(True, match.ShouldHijack('#!/bin/bash\n[line 2]'))
24 self.assertEqual(True, match.ShouldHijack('#!/bin/bash -e\n[line 2]'))
25 self.assertEqual(True, match.ShouldHijack('#!/bin/sh\n[line 2]\n'))
26 self.assertEqual(True, match.ShouldHijack('#!/bin/sh -e\n[line 2]\n'))
27
28 # Unlikely but OK
29 self.assertEqual(True, match.ShouldHijack('#!/usr/bin/env sh\n'))
30
31 # fastlex bug: should not allow \0
32 self.assertEqual(False, match.ShouldHijack('#!/usr/bin/env \0 sh\n'))
33
34 def testBraceRangeLexer(self):
35 lex = match.BraceRangeLexer('1..3')
36 while True:
37 id_, val = lex.Next()
38 log('%s %r', Id_str(id_), val)
39 if id_ == Id.Eol_Tok:
40 break
41
42 def testLooksLike(self):
43 INTS = [
44 (False, ''),
45 (False, 'foo'),
46 (True, '3'),
47 (True, '-3'),
48 (False, '-'),
49 (False, '.'),
50 (True, '\t12 '),
51 (True, '\t-12 '),
52 (False, ' - 12 '),
53 ]
54
55 MORE_INTS = [
56 (True, ' 3_000 '),
57 ]
58
59 for expected, s in INTS + MORE_INTS:
60 self.assertEqual(expected, match.LooksLikeInteger(s))
61
62 FLOATS = [
63 (True, '3.0'),
64 (True, '-3.0'),
65 (True, '\t3.0 '),
66 (True, '\t-3.0 '),
67 (False, ' - 3.0 '),
68 ]
69
70 for expected, s in INTS + FLOATS: # Use BOTH test cases
71 self.assertEqual(expected, match.LooksLikeFloat(s), s)
72
73
74 if __name__ == '__main__':
75 unittest.main()