1 | #!/usr/bin/env python2
|
2 | from __future__ import print_function
|
3 |
|
4 | import unittest
|
5 |
|
6 | from _devbuild.gen.syntax_asdl import loc
|
7 | from _devbuild.gen.id_kind_asdl import Id_str
|
8 | from core import error
|
9 | from frontend import match
|
10 | from mycpp import mops
|
11 | from osh import sh_expr_eval
|
12 |
|
13 |
|
14 | class ParsingTest(unittest.TestCase):
|
15 |
|
16 | def testMatchFunction(self):
|
17 | id_, pos = match.MatchShNumberToken('2#1010', 0)
|
18 | if 0:
|
19 | print('id = %r' % id_)
|
20 | print('id = %r' % Id_str(id_))
|
21 | print('pos = %r' % pos)
|
22 |
|
23 | def checkCases(self, cases):
|
24 | for s, expected in cases:
|
25 | stripped = s.strip() # also done in caller
|
26 | try:
|
27 | ok, actual = sh_expr_eval._ParseOshInteger(
|
28 | stripped, loc.Missing)
|
29 | except error.Strict:
|
30 | ok = False
|
31 |
|
32 | if not ok:
|
33 | actual = None
|
34 |
|
35 | if 0:
|
36 | print('s %r' % s)
|
37 | print('expected', expected and expected.i)
|
38 | print('actual', actual and actual.i)
|
39 | print()
|
40 | self.assertEqual(actual, expected)
|
41 |
|
42 | def testDecimalConst(self):
|
43 | CASES = [
|
44 | ('0', mops.BigInt(0)),
|
45 | ('42042', mops.BigInt(42042)),
|
46 | (' 2 ', mops.BigInt(2)),
|
47 | (' 2\t', mops.BigInt(2)),
|
48 | ('\r\n2\r\n', mops.BigInt(2)),
|
49 | ('1F', None),
|
50 | ('011', mops.BigInt(9)), # Parsed as an octal
|
51 | ('1_1', None),
|
52 | ('1 1', None),
|
53 | ]
|
54 | self.checkCases(CASES)
|
55 |
|
56 | def testOctalConst(self):
|
57 | CASES = [
|
58 | ('0777', mops.BigInt(511)),
|
59 | ('00012', mops.BigInt(10)),
|
60 | (' 010\t', mops.BigInt(8)),
|
61 | ('\n010\r\n', mops.BigInt(8)),
|
62 | ('019', None),
|
63 | ('0_9', None),
|
64 | ('0 9', None),
|
65 | ('0F0', None),
|
66 | ]
|
67 | self.checkCases(CASES)
|
68 |
|
69 | def testHexConst(self):
|
70 | CASES = [
|
71 | ('0xFF', mops.BigInt(255)),
|
72 | ('0xff', mops.BigInt(255)),
|
73 | ('0x0010', mops.BigInt(16)),
|
74 | (' 0x1A ', mops.BigInt(26)),
|
75 | ('\t0x1A\r\n', mops.BigInt(26)),
|
76 | ('FF', None),
|
77 | ('0xG', None),
|
78 | ('0x1_0', None),
|
79 | ('0x1 0', None),
|
80 | ('0X12', None),
|
81 | ]
|
82 | self.checkCases(CASES)
|
83 |
|
84 | def testArbitraryBaseConst(self):
|
85 | CASES = [
|
86 | ('2#0110', mops.BigInt(6)),
|
87 | ('8#777', mops.BigInt(511)),
|
88 | ('16#ff', mops.BigInt(255)),
|
89 | (' 16#ff\r ', mops.BigInt(255)),
|
90 | ('\t16#ff\n', mops.BigInt(255)),
|
91 | ('64#123abcABC@_', mops.BigInt(1189839476434038719)),
|
92 | ('16#FF', None), # F != f, so F is out of range of the base
|
93 | ('010#42', None), # Base cannot start with 0
|
94 | ('65#1', None), # Base too large
|
95 | ('0#1', None), # Base too small
|
96 | ('1#1', None), # Base too small
|
97 | ]
|
98 | self.checkCases(CASES)
|
99 |
|
100 |
|
101 | if __name__ == '__main__':
|
102 | unittest.main()
|