1 | #!/usr/bin/env python2
|
2 | """
|
3 | braces_test.py: Tests for braces.py
|
4 | """
|
5 |
|
6 | import sys
|
7 | import unittest
|
8 |
|
9 | from _devbuild.gen.id_kind_asdl import Id
|
10 | from _devbuild.gen.syntax_asdl import word_part_e, CompoundWord
|
11 | from asdl import format as fmt
|
12 | from core.test_lib import FakeTok
|
13 | from mycpp.mylib import log
|
14 | from osh import braces # module under test
|
15 | from osh import word_parse_test
|
16 |
|
17 |
|
18 | # Silly wrapper
|
19 | def _assertReadWord(*args):
|
20 | return word_parse_test._assertReadWord(*args)
|
21 |
|
22 |
|
23 | def _PrettyPrint(n):
|
24 | """Prints in color."""
|
25 | tree = n.PrettyTree(True)
|
26 | fmt.HNodePrettyPrint(tree, sys.stdout)
|
27 |
|
28 |
|
29 | class BracesTest(unittest.TestCase):
|
30 |
|
31 | def testRangePartDetect(self):
|
32 | CASES = [
|
33 | ('', None),
|
34 | ('1', None),
|
35 | ('1..', None),
|
36 | ('1..3', ('1', '3')),
|
37 | ('3..-10..-2', ('3', '-10', -2)),
|
38 | ('3..-10..-2..', None), # nope! unexpected trailing tokens
|
39 | ('a', None),
|
40 | ('a..', None),
|
41 | ('a..z', ('a', 'z')),
|
42 | ('a..z..', None),
|
43 | ('z..a..-1', ('z', 'a', -1)),
|
44 | ]
|
45 | for s, expected in CASES:
|
46 | tok = FakeTok(Id.Lit_Chars, s)
|
47 | part = braces._RangePartDetect(tok)
|
48 | if expected is None:
|
49 | self.assert_(part is None)
|
50 | elif len(expected) == 2:
|
51 | s, e = expected
|
52 | self.assertEqual(s, part.start)
|
53 | self.assertEqual(e, part.end)
|
54 | #self.assertEqual(runtime.NO_SPID, part.step)
|
55 |
|
56 | elif len(expected) == 3:
|
57 | s, e, step = expected
|
58 | self.assertEqual(s, part.start)
|
59 | self.assertEqual(e, part.end)
|
60 | self.assertEqual(step, part.step)
|
61 |
|
62 | else:
|
63 | raise AssertionError()
|
64 |
|
65 | log('%r\t%s', s, part)
|
66 |
|
67 | def testBraceDetect(self):
|
68 | w = _assertReadWord(self, '}')
|
69 | tree = braces.BraceDetect(w)
|
70 | self.assertEqual(None, tree)
|
71 |
|
72 | w = _assertReadWord(self, ',')
|
73 | tree = braces.BraceDetect(w)
|
74 | self.assertEqual(None, tree)
|
75 |
|
76 | w = _assertReadWord(self, 'B-{a,b}-E')
|
77 | tree = braces.BraceDetect(w)
|
78 | self.assertEqual(3, len(tree.parts))
|
79 | _PrettyPrint(tree)
|
80 | print('--')
|
81 |
|
82 | # Multiple parts for each alternative
|
83 | w = _assertReadWord(self, 'B-{a"a",b"b",c"c"}-E')
|
84 | tree = braces.BraceDetect(w)
|
85 | self.assertEqual(3, len(tree.parts))
|
86 | _PrettyPrint(tree)
|
87 | print('--')
|
88 |
|
89 | # Multiple expansion
|
90 | w = _assertReadWord(self, 'B-{a,b}--{c,d}-E')
|
91 | tree = braces.BraceDetect(w)
|
92 | self.assertEqual(5, len(tree.parts))
|
93 | _PrettyPrint(tree)
|
94 | print('--')
|
95 |
|
96 | # Nested expansion
|
97 | w = _assertReadWord(self, 'B-{a,b,c,={d,e}}-E')
|
98 | tree = braces.BraceDetect(w)
|
99 | _PrettyPrint(tree)
|
100 | self.assertEqual(3, len(tree.parts)) # B- {} -E
|
101 |
|
102 | middle_part = tree.parts[1]
|
103 | self.assertEqual(word_part_e.BracedTuple, middle_part.tag())
|
104 | self.assertEqual(4, len(middle_part.words)) # a b c ={d,e}
|
105 |
|
106 | last_alternative = middle_part.words[3]
|
107 | self.assertEqual(2, len(last_alternative.parts)) # = {d,e}
|
108 |
|
109 | second_part = last_alternative.parts[1]
|
110 | self.assertEqual(word_part_e.BracedTuple, second_part.tag())
|
111 | self.assertEqual(2, len(second_part.words)) # {d,e}
|
112 |
|
113 | # Another nested expansion
|
114 | w = _assertReadWord(self, 'B-{a,={b,c}=,d}-E')
|
115 | tree = braces.BraceDetect(w)
|
116 | _PrettyPrint(tree)
|
117 | self.assertEqual(3, len(tree.parts)) # B- {} -E
|
118 |
|
119 | middle_part = tree.parts[1]
|
120 | self.assertEqual(word_part_e.BracedTuple, middle_part.tag())
|
121 | self.assertEqual(3, len(middle_part.words)) # a ={b,c}= d
|
122 |
|
123 | first_alternative = middle_part.words[0]
|
124 | _PrettyPrint(first_alternative)
|
125 | self.assertEqual(1, len(first_alternative.parts)) # a
|
126 | #print('!!', first_alternative)
|
127 |
|
128 | middle_alternative = middle_part.words[1]
|
129 | self.assertEqual(3, len(middle_alternative.parts)) # = {b,c} =
|
130 |
|
131 | middle_part2 = middle_alternative.parts[1]
|
132 | self.assertEqual(word_part_e.BracedTuple, middle_part2.tag())
|
133 | self.assertEqual(2, len(middle_part2.words)) # b c
|
134 |
|
135 | # Third alternative is a Compound with zero parts
|
136 | w = _assertReadWord(self, '{a,b,}')
|
137 | tree = braces.BraceDetect(w)
|
138 | _PrettyPrint(tree)
|
139 | self.assertEqual(1, len(tree.parts))
|
140 | self.assertEqual(3, len(tree.parts[0].words))
|
141 |
|
142 | def testBraceExpand(self):
|
143 | w = _assertReadWord(self, 'hi')
|
144 | results = braces._BraceExpand(w.parts)
|
145 | self.assertEqual(1, len(results))
|
146 | for parts in results:
|
147 | _PrettyPrint(CompoundWord(parts))
|
148 | print('')
|
149 |
|
150 | w = _assertReadWord(self, 'B-{a,b}-E')
|
151 | tree = braces.BraceDetect(w)
|
152 | self.assertEqual(3, len(tree.parts))
|
153 | _PrettyPrint(tree)
|
154 |
|
155 | results = braces._BraceExpand(tree.parts)
|
156 | self.assertEqual(2, len(results))
|
157 | for parts in results:
|
158 | _PrettyPrint(CompoundWord(parts))
|
159 | print('')
|
160 |
|
161 | w = _assertReadWord(self, 'B-{a,={b,c,d}=,e}-E')
|
162 | tree = braces.BraceDetect(w)
|
163 | self.assertEqual(3, len(tree.parts))
|
164 | _PrettyPrint(tree)
|
165 |
|
166 | results = braces._BraceExpand(tree.parts)
|
167 | self.assertEqual(5, len(results))
|
168 | for parts in results:
|
169 | _PrettyPrint(CompoundWord(parts))
|
170 | print('')
|
171 |
|
172 | w = _assertReadWord(self, 'B-{a,b}-{c,d}-E')
|
173 | tree = braces.BraceDetect(w)
|
174 | self.assertEqual(5, len(tree.parts))
|
175 | _PrettyPrint(tree)
|
176 |
|
177 | results = braces._BraceExpand(tree.parts)
|
178 | self.assertEqual(4, len(results))
|
179 | for parts in results:
|
180 | _PrettyPrint(CompoundWord(parts))
|
181 | print('')
|
182 |
|
183 |
|
184 | if __name__ == '__main__':
|
185 | unittest.main()
|