xref: /openbsd-src/gnu/llvm/llvm/utils/lit/tests/unit/TestRunner.py (revision 46035553bfdd96e63c94e32da0210227ec2e3cf1)
1# RUN: %{python} %s
2#
3# END.
4
5
6import os.path
7import platform
8import unittest
9
10import lit.discovery
11import lit.LitConfig
12import lit.Test as Test
13from lit.TestRunner import ParserKind, IntegratedTestKeywordParser, \
14                           parseIntegratedTestScript
15
16
17class TestIntegratedTestKeywordParser(unittest.TestCase):
18    inputTestCase = None
19
20    @staticmethod
21    def load_keyword_parser_lit_tests():
22        """
23        Create and load the LIT test suite and test objects used by
24        TestIntegratedTestKeywordParser
25        """
26        # Create the global config object.
27        lit_config = lit.LitConfig.LitConfig(progname='lit',
28                                             path=[],
29                                             quiet=False,
30                                             useValgrind=False,
31                                             valgrindLeakCheck=False,
32                                             valgrindArgs=[],
33                                             noExecute=False,
34                                             debug=False,
35                                             isWindows=(
36                                               platform.system() == 'Windows'),
37                                             params={})
38        TestIntegratedTestKeywordParser.litConfig = lit_config
39        # Perform test discovery.
40        test_path = os.path.dirname(os.path.dirname(__file__))
41        inputs = [os.path.join(test_path, 'Inputs/testrunner-custom-parsers/')]
42        assert os.path.isdir(inputs[0])
43        tests = lit.discovery.find_tests_for_inputs(lit_config, inputs)
44        assert len(tests) == 1 and "there should only be one test"
45        TestIntegratedTestKeywordParser.inputTestCase = tests[0]
46
47    @staticmethod
48    def make_parsers():
49        def custom_parse(line_number, line, output):
50            if output is None:
51                output = []
52            output += [part for part in line.split(' ') if part.strip()]
53            return output
54
55        return [
56            IntegratedTestKeywordParser("MY_TAG.", ParserKind.TAG),
57            IntegratedTestKeywordParser("MY_DNE_TAG.", ParserKind.TAG),
58            IntegratedTestKeywordParser("MY_LIST:", ParserKind.LIST),
59            IntegratedTestKeywordParser("MY_BOOL:", ParserKind.BOOLEAN_EXPR),
60            IntegratedTestKeywordParser("MY_RUN:", ParserKind.COMMAND),
61            IntegratedTestKeywordParser("MY_CUSTOM:", ParserKind.CUSTOM,
62                                        custom_parse),
63
64        ]
65
66    @staticmethod
67    def get_parser(parser_list, keyword):
68        for p in parser_list:
69            if p.keyword == keyword:
70                return p
71        assert False and "parser not found"
72
73    @staticmethod
74    def parse_test(parser_list):
75        script = parseIntegratedTestScript(
76            TestIntegratedTestKeywordParser.inputTestCase,
77            additional_parsers=parser_list, require_script=False)
78        assert not isinstance(script, lit.Test.Result)
79        assert isinstance(script, list)
80        assert len(script) == 0
81
82    def test_tags(self):
83        parsers = self.make_parsers()
84        self.parse_test(parsers)
85        tag_parser = self.get_parser(parsers, 'MY_TAG.')
86        dne_tag_parser = self.get_parser(parsers, 'MY_DNE_TAG.')
87        self.assertTrue(tag_parser.getValue())
88        self.assertFalse(dne_tag_parser.getValue())
89
90    def test_lists(self):
91        parsers = self.make_parsers()
92        self.parse_test(parsers)
93        list_parser = self.get_parser(parsers, 'MY_LIST:')
94        self.assertEqual(list_parser.getValue(),
95                              ['one', 'two', 'three', 'four'])
96
97    def test_commands(self):
98        parsers = self.make_parsers()
99        self.parse_test(parsers)
100        cmd_parser = self.get_parser(parsers, 'MY_RUN:')
101        value = cmd_parser.getValue()
102        self.assertEqual(len(value), 2)  # there are only two run lines
103        self.assertEqual(value[0].strip(), "%dbg(MY_RUN: at line 4)  baz")
104        self.assertEqual(value[1].strip(), "%dbg(MY_RUN: at line 7)  foo  bar")
105
106    def test_boolean(self):
107        parsers = self.make_parsers()
108        self.parse_test(parsers)
109        bool_parser = self.get_parser(parsers, 'MY_BOOL:')
110        value = bool_parser.getValue()
111        self.assertEqual(len(value), 2)  # there are only two run lines
112        self.assertEqual(value[0].strip(), "a && (b)")
113        self.assertEqual(value[1].strip(), "d")
114
115    def test_boolean_unterminated(self):
116        parsers = self.make_parsers() + \
117            [IntegratedTestKeywordParser("MY_BOOL_UNTERMINATED:", ParserKind.BOOLEAN_EXPR)]
118        try:
119            self.parse_test(parsers)
120            self.fail('expected exception')
121        except ValueError as e:
122            self.assertIn("Test has unterminated MY_BOOL_UNTERMINATED: lines", str(e))
123
124
125    def test_custom(self):
126        parsers = self.make_parsers()
127        self.parse_test(parsers)
128        custom_parser = self.get_parser(parsers, 'MY_CUSTOM:')
129        value = custom_parser.getValue()
130        self.assertEqual(value, ['a', 'b', 'c'])
131
132    def test_bad_keywords(self):
133        def custom_parse(line_number, line, output):
134            return output
135
136        try:
137            IntegratedTestKeywordParser("TAG_NO_SUFFIX", ParserKind.TAG),
138            self.fail("TAG_NO_SUFFIX failed to raise an exception")
139        except ValueError as e:
140            pass
141        except BaseException as e:
142            self.fail("TAG_NO_SUFFIX raised the wrong exception: %r" % e)
143
144        try:
145            IntegratedTestKeywordParser("TAG_WITH_COLON:", ParserKind.TAG),
146            self.fail("TAG_WITH_COLON: failed to raise an exception")
147        except ValueError as e:
148            pass
149        except BaseException as e:
150            self.fail("TAG_WITH_COLON: raised the wrong exception: %r" % e)
151
152        try:
153            IntegratedTestKeywordParser("LIST_WITH_DOT.", ParserKind.LIST),
154            self.fail("LIST_WITH_DOT. failed to raise an exception")
155        except ValueError as e:
156            pass
157        except BaseException as e:
158            self.fail("LIST_WITH_DOT. raised the wrong exception: %r" % e)
159
160        try:
161            IntegratedTestKeywordParser("CUSTOM_NO_SUFFIX",
162                                        ParserKind.CUSTOM, custom_parse),
163            self.fail("CUSTOM_NO_SUFFIX failed to raise an exception")
164        except ValueError as e:
165            pass
166        except BaseException as e:
167            self.fail("CUSTOM_NO_SUFFIX raised the wrong exception: %r" % e)
168
169        # Both '.' and ':' are allowed for CUSTOM keywords.
170        try:
171            IntegratedTestKeywordParser("CUSTOM_WITH_DOT.",
172                                        ParserKind.CUSTOM, custom_parse),
173        except BaseException as e:
174            self.fail("CUSTOM_WITH_DOT. raised an exception: %r" % e)
175        try:
176            IntegratedTestKeywordParser("CUSTOM_WITH_COLON:",
177                                        ParserKind.CUSTOM, custom_parse),
178        except BaseException as e:
179            self.fail("CUSTOM_WITH_COLON: raised an exception: %r" % e)
180
181        try:
182            IntegratedTestKeywordParser("CUSTOM_NO_PARSER:",
183                                        ParserKind.CUSTOM),
184            self.fail("CUSTOM_NO_PARSER: failed to raise an exception")
185        except ValueError as e:
186            pass
187        except BaseException as e:
188            self.fail("CUSTOM_NO_PARSER: raised the wrong exception: %r" % e)
189
190if __name__ == '__main__':
191    TestIntegratedTestKeywordParser.load_keyword_parser_lit_tests()
192    unittest.main(verbosity=2)
193