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