• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# RUN: %{python} %s
2
3import unittest
4
5from lit.ShUtil import Command, Pipeline, Seq, ShLexer, ShParser
6
7
8class TestShLexer(unittest.TestCase):
9    def lex(self, str, *args, **kwargs):
10        return list(ShLexer(str, *args, **kwargs).lex())
11
12    def test_basic(self):
13        self.assertEqual(self.lex('a|b>c&d<e;f'),
14                         ['a', ('|',), 'b', ('>',), 'c', ('&',), 'd',
15                          ('<',), 'e', (';',), 'f'])
16
17    def test_redirection_tokens(self):
18        self.assertEqual(self.lex('a2>c'),
19                         ['a2', ('>',), 'c'])
20        self.assertEqual(self.lex('a 2>c'),
21                         ['a', ('>',2), 'c'])
22
23    def test_quoting(self):
24        self.assertEqual(self.lex(""" 'a' """),
25                         ['a'])
26        self.assertEqual(self.lex(""" "hello\\"world" """),
27                         ['hello"world'])
28        self.assertEqual(self.lex(""" "hello\\'world" """),
29                         ["hello\\'world"])
30        self.assertEqual(self.lex(""" "hello\\\\world" """),
31                         ["hello\\world"])
32        self.assertEqual(self.lex(""" he"llo wo"rld """),
33                         ["hello world"])
34        self.assertEqual(self.lex(""" a\\ b a\\\\b """),
35                         ["a b", "a\\b"])
36        self.assertEqual(self.lex(""" "" "" """),
37                         ["", ""])
38        self.assertEqual(self.lex(""" a\\ b """, win32Escapes = True),
39                         ['a\\', 'b'])
40
41class TestShParse(unittest.TestCase):
42    def parse(self, str):
43        return ShParser(str).parse()
44
45    def test_basic(self):
46        self.assertEqual(self.parse('echo hello'),
47                         Pipeline([Command(['echo', 'hello'], [])], False))
48        self.assertEqual(self.parse('echo ""'),
49                         Pipeline([Command(['echo', ''], [])], False))
50        self.assertEqual(self.parse("""echo -DFOO='a'"""),
51                         Pipeline([Command(['echo', '-DFOO=a'], [])], False))
52        self.assertEqual(self.parse('echo -DFOO="a"'),
53                         Pipeline([Command(['echo', '-DFOO=a'], [])], False))
54
55    def test_redirection(self):
56        self.assertEqual(self.parse('echo hello > c'),
57                         Pipeline([Command(['echo', 'hello'],
58                                           [((('>'),), 'c')])], False))
59        self.assertEqual(self.parse('echo hello > c >> d'),
60                         Pipeline([Command(['echo', 'hello'], [(('>',), 'c'),
61                                                     (('>>',), 'd')])], False))
62        self.assertEqual(self.parse('a 2>&1'),
63                         Pipeline([Command(['a'], [(('>&',2), '1')])], False))
64
65    def test_pipeline(self):
66        self.assertEqual(self.parse('a | b'),
67                         Pipeline([Command(['a'], []),
68                                   Command(['b'], [])],
69                                  False))
70
71        self.assertEqual(self.parse('a | b | c'),
72                         Pipeline([Command(['a'], []),
73                                   Command(['b'], []),
74                                   Command(['c'], [])],
75                                  False))
76
77    def test_list(self):
78        self.assertEqual(self.parse('a ; b'),
79                         Seq(Pipeline([Command(['a'], [])], False),
80                             ';',
81                             Pipeline([Command(['b'], [])], False)))
82
83        self.assertEqual(self.parse('a & b'),
84                         Seq(Pipeline([Command(['a'], [])], False),
85                             '&',
86                             Pipeline([Command(['b'], [])], False)))
87
88        self.assertEqual(self.parse('a && b'),
89                         Seq(Pipeline([Command(['a'], [])], False),
90                             '&&',
91                             Pipeline([Command(['b'], [])], False)))
92
93        self.assertEqual(self.parse('a || b'),
94                         Seq(Pipeline([Command(['a'], [])], False),
95                             '||',
96                             Pipeline([Command(['b'], [])], False)))
97
98        self.assertEqual(self.parse('a && b || c'),
99                         Seq(Seq(Pipeline([Command(['a'], [])], False),
100                                 '&&',
101                                 Pipeline([Command(['b'], [])], False)),
102                             '||',
103                             Pipeline([Command(['c'], [])], False)))
104
105        self.assertEqual(self.parse('a; b'),
106                         Seq(Pipeline([Command(['a'], [])], False),
107                             ';',
108                             Pipeline([Command(['b'], [])], False)))
109
110if __name__ == '__main__':
111    unittest.main()
112
113