blob: 79cc10f7e14d6e46489a91a0ae70ee95e00d8bd6 [file] [log] [blame]
Eric Fiselierdf87d072016-12-05 20:21:21 +00001# RUN: %{python} %s
2#
3# END.
4
5
6import unittest
7import platform
8import os.path
9import tempfile
10
11import lit
12from lit.TestRunner import ParserKind, IntegratedTestKeywordParser, \
13 parseIntegratedTestScript
14
15
16class TestIntegratedTestKeywordParser(unittest.TestCase):
17 inputTestCase = None
18
19 @staticmethod
20 def load_keyword_parser_lit_tests():
21 """
22 Create and load the LIT test suite and test objects used by
23 TestIntegratedTestKeywordParser
24 """
25 # Create the global config object.
26 lit_config = lit.LitConfig.LitConfig(progname='lit',
27 path=[],
28 quiet=False,
29 useValgrind=False,
30 valgrindLeakCheck=False,
31 valgrindArgs=[],
32 noExecute=False,
33 debug=False,
34 isWindows=(
35 platform.system() == 'Windows'),
36 params={})
37 TestIntegratedTestKeywordParser.litConfig = lit_config
38 # Perform test discovery.
39 test_path = os.path.dirname(os.path.dirname(__file__))
40 inputs = [os.path.join(test_path, 'Inputs/testrunner-custom-parsers/')]
41 assert os.path.isdir(inputs[0])
42 run = lit.run.Run(lit_config,
43 lit.discovery.find_tests_for_inputs(lit_config, inputs))
44 assert len(run.tests) == 1 and "there should only be one test"
45 TestIntegratedTestKeywordParser.inputTestCase = run.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_RUN:", ParserKind.COMMAND),
60 IntegratedTestKeywordParser("MY_CUSTOM:", ParserKind.CUSTOM,
61 custom_parse)
62 ]
63
64 @staticmethod
65 def get_parser(parser_list, keyword):
66 for p in parser_list:
67 if p.keyword == keyword:
68 return p
69 assert False and "parser not found"
70
71 @staticmethod
72 def parse_test(parser_list):
73 script = parseIntegratedTestScript(
74 TestIntegratedTestKeywordParser.inputTestCase,
75 additional_parsers=parser_list, require_script=False)
76 assert not isinstance(script, lit.Test.Result)
77 assert isinstance(script, list)
78 assert len(script) == 0
79
80 def test_tags(self):
81 parsers = self.make_parsers()
82 self.parse_test(parsers)
83 tag_parser = self.get_parser(parsers, 'MY_TAG.')
84 dne_tag_parser = self.get_parser(parsers, 'MY_DNE_TAG.')
85 self.assertTrue(tag_parser.getValue())
86 self.assertFalse(dne_tag_parser.getValue())
87
88 def test_lists(self):
89 parsers = self.make_parsers()
90 self.parse_test(parsers)
91 list_parser = self.get_parser(parsers, 'MY_LIST:')
Brian Gesiak600f04a2017-03-22 04:23:01 +000092 self.assertEqual(list_parser.getValue(),
Eric Fiselierdf87d072016-12-05 20:21:21 +000093 ['one', 'two', 'three', 'four'])
94
95 def test_commands(self):
96 parsers = self.make_parsers()
97 self.parse_test(parsers)
98 cmd_parser = self.get_parser(parsers, 'MY_RUN:')
99 value = cmd_parser.getValue()
100 self.assertEqual(len(value), 2) # there are only two run lines
101 self.assertEqual(value[0].strip(), 'baz')
102 self.assertEqual(value[1].strip(), 'foo bar')
103
104 def test_custom(self):
105 parsers = self.make_parsers()
106 self.parse_test(parsers)
107 custom_parser = self.get_parser(parsers, 'MY_CUSTOM:')
108 value = custom_parser.getValue()
Brian Gesiak600f04a2017-03-22 04:23:01 +0000109 self.assertEqual(value, ['a', 'b', 'c'])
Eric Fiselierdf87d072016-12-05 20:21:21 +0000110
Greg Parker17db7702017-01-25 02:26:03 +0000111 def test_bad_keywords(self):
112 def custom_parse(line_number, line, output):
113 return output
114
115 try:
116 IntegratedTestKeywordParser("TAG_NO_SUFFIX", ParserKind.TAG),
117 self.fail("TAG_NO_SUFFIX failed to raise an exception")
118 except ValueError as e:
119 pass
120 except BaseException as e:
121 self.fail("TAG_NO_SUFFIX raised the wrong exception: %r" % e)
122
123 try:
124 IntegratedTestKeywordParser("TAG_WITH_COLON:", ParserKind.TAG),
125 self.fail("TAG_WITH_COLON: failed to raise an exception")
126 except ValueError as e:
127 pass
128 except BaseException as e:
129 self.fail("TAG_WITH_COLON: raised the wrong exception: %r" % e)
130
131 try:
132 IntegratedTestKeywordParser("LIST_WITH_DOT.", ParserKind.LIST),
133 self.fail("LIST_WITH_DOT. failed to raise an exception")
134 except ValueError as e:
135 pass
136 except BaseException as e:
137 self.fail("LIST_WITH_DOT. raised the wrong exception: %r" % e)
138
139 try:
140 IntegratedTestKeywordParser("CUSTOM_NO_SUFFIX",
141 ParserKind.CUSTOM, custom_parse),
142 self.fail("CUSTOM_NO_SUFFIX failed to raise an exception")
143 except ValueError as e:
144 pass
145 except BaseException as e:
146 self.fail("CUSTOM_NO_SUFFIX raised the wrong exception: %r" % e)
147
148 # Both '.' and ':' are allowed for CUSTOM keywords.
149 try:
150 IntegratedTestKeywordParser("CUSTOM_WITH_DOT.",
151 ParserKind.CUSTOM, custom_parse),
152 except BaseException as e:
153 self.fail("CUSTOM_WITH_DOT. raised an exception: %r" % e)
154 try:
155 IntegratedTestKeywordParser("CUSTOM_WITH_COLON:",
156 ParserKind.CUSTOM, custom_parse),
157 except BaseException as e:
158 self.fail("CUSTOM_WITH_COLON: raised an exception: %r" % e)
159
160 try:
161 IntegratedTestKeywordParser("CUSTOM_NO_PARSER:",
162 ParserKind.CUSTOM),
163 self.fail("CUSTOM_NO_PARSER: failed to raise an exception")
164 except ValueError as e:
165 pass
166 except BaseException as e:
167 self.fail("CUSTOM_NO_PARSER: raised the wrong exception: %r" % e)
Eric Fiselierdf87d072016-12-05 20:21:21 +0000168
169if __name__ == '__main__':
170 TestIntegratedTestKeywordParser.load_keyword_parser_lit_tests()
171 unittest.main(verbosity=2)