5 from lit
.ShUtil
import Command
, Pipeline
, Seq
, ShLexer
, ShParser
8 class TestShLexer(unittest
.TestCase
):
9 def lex(self
, str, *args
, **kwargs
):
10 return list(ShLexer(str, *args
, **kwargs
).lex())
13 self
.assertEqual(self
.lex('a|b>c&d<e;f'),
14 ['a', ('|',), 'b', ('>',), 'c', ('&',), 'd',
15 ('<',), 'e', (';',), 'f'])
17 def test_redirection_tokens(self
):
18 self
.assertEqual(self
.lex('a2>c'),
20 self
.assertEqual(self
.lex('a 2>c'),
23 def test_quoting(self
):
24 self
.assertEqual(self
.lex(""" 'a' """),
26 self
.assertEqual(self
.lex(""" "hello\\"world" """),
28 self
.assertEqual(self
.lex(""" "hello\\'world" """),
30 self
.assertEqual(self
.lex(""" "hello\\\\world" """),
32 self
.assertEqual(self
.lex(""" he"llo wo"rld """),
34 self
.assertEqual(self
.lex(""" a\\ b a\\\\b """),
36 self
.assertEqual(self
.lex(""" "" "" """),
38 self
.assertEqual(self
.lex(""" a\\ b """, win32Escapes
= True),
41 class TestShParse(unittest
.TestCase
):
43 return ShParser(str).parse()
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))
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))
65 def test_pipeline(self
):
66 self
.assertEqual(self
.parse('a | b'),
67 Pipeline([Command(['a'], []),
71 self
.assertEqual(self
.parse('a | b | c'),
72 Pipeline([Command(['a'], []),
78 self
.assertEqual(self
.parse('a ; b'),
79 Seq(Pipeline([Command(['a'], [])], False),
81 Pipeline([Command(['b'], [])], False)))
83 self
.assertEqual(self
.parse('a & b'),
84 Seq(Pipeline([Command(['a'], [])], False),
86 Pipeline([Command(['b'], [])], False)))
88 self
.assertEqual(self
.parse('a && b'),
89 Seq(Pipeline([Command(['a'], [])], False),
91 Pipeline([Command(['b'], [])], False)))
93 self
.assertEqual(self
.parse('a || b'),
94 Seq(Pipeline([Command(['a'], [])], False),
96 Pipeline([Command(['b'], [])], False)))
98 self
.assertEqual(self
.parse('a && b || c'),
99 Seq(Seq(Pipeline([Command(['a'], [])], False),
101 Pipeline([Command(['b'], [])], False)),
103 Pipeline([Command(['c'], [])], False)))
105 self
.assertEqual(self
.parse('a; b'),
106 Seq(Pipeline([Command(['a'], [])], False),
108 Pipeline([Command(['b'], [])], False)))
110 if __name__
== '__main__':