Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / llvm / utils / lit / tests / unit / ShUtil.py
blobc5f2e3b99ae13303a4867a4c7e079cc4f744b19c
1 # RUN: %{python} %s
3 import unittest
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())
12 def test_basic(self):
13 self.assertEqual(
14 self.lex("a|b>c&d<e;f"),
15 ["a", ("|",), "b", (">",), "c", ("&",), "d", ("<",), "e", (";",), "f"],
18 def test_redirection_tokens(self):
19 self.assertEqual(self.lex("a2>c"), ["a2", (">",), "c"])
20 self.assertEqual(self.lex("a 2>c"), ["a", (">", 2), "c"])
22 def test_quoting(self):
23 self.assertEqual(self.lex(""" 'a' """), ["a"])
24 self.assertEqual(self.lex(""" "hello\\"world" """), ['hello"world'])
25 self.assertEqual(self.lex(""" "hello\\'world" """), ["hello\\'world"])
26 self.assertEqual(self.lex(""" "hello\\\\world" """), ["hello\\world"])
27 self.assertEqual(self.lex(""" he"llo wo"rld """), ["hello world"])
28 self.assertEqual(self.lex(""" a\\ b a\\\\b """), ["a b", "a\\b"])
29 self.assertEqual(self.lex(""" "" "" """), ["", ""])
30 self.assertEqual(self.lex(""" a\\ b """, win32Escapes=True), ["a\\", "b"])
33 class TestShParse(unittest.TestCase):
34 def parse(self, str):
35 return ShParser(str).parse()
37 def test_basic(self):
38 self.assertEqual(
39 self.parse("echo hello"), Pipeline([Command(["echo", "hello"], [])], False)
41 self.assertEqual(
42 self.parse('echo ""'), Pipeline([Command(["echo", ""], [])], False)
44 self.assertEqual(
45 self.parse("""echo -DFOO='a'"""),
46 Pipeline([Command(["echo", "-DFOO=a"], [])], False),
48 self.assertEqual(
49 self.parse('echo -DFOO="a"'),
50 Pipeline([Command(["echo", "-DFOO=a"], [])], False),
53 def test_redirection(self):
54 self.assertEqual(
55 self.parse("echo hello > c"),
56 Pipeline([Command(["echo", "hello"], [(((">"),), "c")])], False),
58 self.assertEqual(
59 self.parse("echo hello > c >> d"),
60 Pipeline(
61 [Command(["echo", "hello"], [((">",), "c"), ((">>",), "d")])], False
64 self.assertEqual(
65 self.parse("a 2>&1"), Pipeline([Command(["a"], [((">&", 2), "1")])], False)
68 def test_pipeline(self):
69 self.assertEqual(
70 self.parse("a | b"),
71 Pipeline([Command(["a"], []), Command(["b"], [])], False),
74 self.assertEqual(
75 self.parse("a | b | c"),
76 Pipeline(
77 [Command(["a"], []), Command(["b"], []), Command(["c"], [])], False
81 def test_list(self):
82 self.assertEqual(
83 self.parse("a ; b"),
84 Seq(
85 Pipeline([Command(["a"], [])], False),
86 ";",
87 Pipeline([Command(["b"], [])], False),
91 self.assertEqual(
92 self.parse("a & b"),
93 Seq(
94 Pipeline([Command(["a"], [])], False),
95 "&",
96 Pipeline([Command(["b"], [])], False),
100 self.assertEqual(
101 self.parse("a && b"),
102 Seq(
103 Pipeline([Command(["a"], [])], False),
104 "&&",
105 Pipeline([Command(["b"], [])], False),
109 self.assertEqual(
110 self.parse("a || b"),
111 Seq(
112 Pipeline([Command(["a"], [])], False),
113 "||",
114 Pipeline([Command(["b"], [])], False),
118 self.assertEqual(
119 self.parse("a && b || c"),
120 Seq(
121 Seq(
122 Pipeline([Command(["a"], [])], False),
123 "&&",
124 Pipeline([Command(["b"], [])], False),
126 "||",
127 Pipeline([Command(["c"], [])], False),
131 self.assertEqual(
132 self.parse("a; b"),
133 Seq(
134 Pipeline([Command(["a"], [])], False),
135 ";",
136 Pipeline([Command(["b"], [])], False),
141 if __name__ == "__main__":
142 unittest.main()