Eric Fiselier | df87d07 | 2016-12-05 20:21:21 +0000 | [diff] [blame] | 1 | # RUN: %{python} %s |
| 2 | # |
| 3 | # END. |
| 4 | |
| 5 | |
| 6 | import unittest |
| 7 | import platform |
| 8 | import os.path |
| 9 | import tempfile |
| 10 | |
| 11 | import lit |
Eric Fiselier | 1989f7e | 2019-01-20 00:51:02 +0000 | [diff] [blame] | 12 | import lit.Test as Test |
Eric Fiselier | df87d07 | 2016-12-05 20:21:21 +0000 | [diff] [blame] | 13 | from lit.TestRunner import ParserKind, IntegratedTestKeywordParser, \ |
| 14 | parseIntegratedTestScript |
| 15 | |
| 16 | |
| 17 | class 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 | run = lit.run.Run(lit_config, |
| 44 | lit.discovery.find_tests_for_inputs(lit_config, inputs)) |
| 45 | assert len(run.tests) == 1 and "there should only be one test" |
| 46 | TestIntegratedTestKeywordParser.inputTestCase = run.tests[0] |
| 47 | |
| 48 | @staticmethod |
| 49 | def make_parsers(): |
Reid Kleckner | 1ca6668 | 2018-04-25 17:30:00 +0000 | [diff] [blame] | 50 | def custom_parse(line_number, line, output): |
Eric Fiselier | df87d07 | 2016-12-05 20:21:21 +0000 | [diff] [blame] | 51 | if output is None: |
| 52 | output = [] |
| 53 | output += [part for part in line.split(' ') if part.strip()] |
| 54 | return output |
| 55 | |
| 56 | return [ |
| 57 | IntegratedTestKeywordParser("MY_TAG.", ParserKind.TAG), |
| 58 | IntegratedTestKeywordParser("MY_DNE_TAG.", ParserKind.TAG), |
| 59 | IntegratedTestKeywordParser("MY_LIST:", ParserKind.LIST), |
Eric Fiselier | 1989f7e | 2019-01-20 00:51:02 +0000 | [diff] [blame] | 60 | IntegratedTestKeywordParser("MY_BOOL:", ParserKind.BOOLEAN_EXPR), |
Eric Fiselier | df87d07 | 2016-12-05 20:21:21 +0000 | [diff] [blame] | 61 | IntegratedTestKeywordParser("MY_RUN:", ParserKind.COMMAND), |
| 62 | IntegratedTestKeywordParser("MY_CUSTOM:", ParserKind.CUSTOM, |
Eric Fiselier | 1989f7e | 2019-01-20 00:51:02 +0000 | [diff] [blame] | 63 | custom_parse), |
| 64 | |
Eric Fiselier | df87d07 | 2016-12-05 20:21:21 +0000 | [diff] [blame] | 65 | ] |
| 66 | |
| 67 | @staticmethod |
| 68 | def get_parser(parser_list, keyword): |
| 69 | for p in parser_list: |
| 70 | if p.keyword == keyword: |
| 71 | return p |
| 72 | assert False and "parser not found" |
| 73 | |
| 74 | @staticmethod |
| 75 | def parse_test(parser_list): |
| 76 | script = parseIntegratedTestScript( |
| 77 | TestIntegratedTestKeywordParser.inputTestCase, |
| 78 | additional_parsers=parser_list, require_script=False) |
| 79 | assert not isinstance(script, lit.Test.Result) |
| 80 | assert isinstance(script, list) |
| 81 | assert len(script) == 0 |
| 82 | |
| 83 | def test_tags(self): |
| 84 | parsers = self.make_parsers() |
| 85 | self.parse_test(parsers) |
| 86 | tag_parser = self.get_parser(parsers, 'MY_TAG.') |
| 87 | dne_tag_parser = self.get_parser(parsers, 'MY_DNE_TAG.') |
| 88 | self.assertTrue(tag_parser.getValue()) |
| 89 | self.assertFalse(dne_tag_parser.getValue()) |
| 90 | |
| 91 | def test_lists(self): |
| 92 | parsers = self.make_parsers() |
| 93 | self.parse_test(parsers) |
| 94 | list_parser = self.get_parser(parsers, 'MY_LIST:') |
Brian Gesiak | 600f04a | 2017-03-22 04:23:01 +0000 | [diff] [blame] | 95 | self.assertEqual(list_parser.getValue(), |
Eric Fiselier | df87d07 | 2016-12-05 20:21:21 +0000 | [diff] [blame] | 96 | ['one', 'two', 'three', 'four']) |
| 97 | |
| 98 | def test_commands(self): |
| 99 | parsers = self.make_parsers() |
| 100 | self.parse_test(parsers) |
| 101 | cmd_parser = self.get_parser(parsers, 'MY_RUN:') |
| 102 | value = cmd_parser.getValue() |
| 103 | self.assertEqual(len(value), 2) # there are only two run lines |
Joel E. Denny | fc01dd2 | 2018-05-31 03:40:37 +0000 | [diff] [blame] | 104 | self.assertEqual(value[0].strip(), "%dbg(MY_RUN: at line 4) baz") |
| 105 | self.assertEqual(value[1].strip(), "%dbg(MY_RUN: at line 7) foo bar") |
Eric Fiselier | df87d07 | 2016-12-05 20:21:21 +0000 | [diff] [blame] | 106 | |
Eric Fiselier | 1989f7e | 2019-01-20 00:51:02 +0000 | [diff] [blame] | 107 | def test_boolean(self): |
| 108 | parsers = self.make_parsers() |
| 109 | self.parse_test(parsers) |
| 110 | bool_parser = self.get_parser(parsers, 'MY_BOOL:') |
| 111 | value = bool_parser.getValue() |
| 112 | self.assertEqual(len(value), 2) # there are only two run lines |
| 113 | self.assertEqual(value[0].strip(), "a && (b)") |
| 114 | self.assertEqual(value[1].strip(), "d") |
| 115 | |
| 116 | def test_boolean_unterminated(self): |
| 117 | parsers = self.make_parsers() + \ |
| 118 | [IntegratedTestKeywordParser("MY_BOOL_UNTERMINATED:", ParserKind.BOOLEAN_EXPR)] |
| 119 | try: |
| 120 | self.parse_test(parsers) |
| 121 | self.fail('expected exception') |
| 122 | except ValueError as e: |
| 123 | self.assertIn("Test has unterminated MY_BOOL_UNTERMINATED: lines", str(e)) |
| 124 | |
| 125 | |
Eric Fiselier | df87d07 | 2016-12-05 20:21:21 +0000 | [diff] [blame] | 126 | def test_custom(self): |
| 127 | parsers = self.make_parsers() |
| 128 | self.parse_test(parsers) |
| 129 | custom_parser = self.get_parser(parsers, 'MY_CUSTOM:') |
| 130 | value = custom_parser.getValue() |
Brian Gesiak | 600f04a | 2017-03-22 04:23:01 +0000 | [diff] [blame] | 131 | self.assertEqual(value, ['a', 'b', 'c']) |
Eric Fiselier | df87d07 | 2016-12-05 20:21:21 +0000 | [diff] [blame] | 132 | |
Greg Parker | 17db770 | 2017-01-25 02:26:03 +0000 | [diff] [blame] | 133 | def test_bad_keywords(self): |
| 134 | def custom_parse(line_number, line, output): |
| 135 | return output |
| 136 | |
| 137 | try: |
| 138 | IntegratedTestKeywordParser("TAG_NO_SUFFIX", ParserKind.TAG), |
| 139 | self.fail("TAG_NO_SUFFIX failed to raise an exception") |
| 140 | except ValueError as e: |
| 141 | pass |
| 142 | except BaseException as e: |
| 143 | self.fail("TAG_NO_SUFFIX raised the wrong exception: %r" % e) |
| 144 | |
| 145 | try: |
| 146 | IntegratedTestKeywordParser("TAG_WITH_COLON:", ParserKind.TAG), |
| 147 | self.fail("TAG_WITH_COLON: failed to raise an exception") |
| 148 | except ValueError as e: |
| 149 | pass |
| 150 | except BaseException as e: |
| 151 | self.fail("TAG_WITH_COLON: raised the wrong exception: %r" % e) |
| 152 | |
| 153 | try: |
| 154 | IntegratedTestKeywordParser("LIST_WITH_DOT.", ParserKind.LIST), |
| 155 | self.fail("LIST_WITH_DOT. failed to raise an exception") |
| 156 | except ValueError as e: |
| 157 | pass |
| 158 | except BaseException as e: |
| 159 | self.fail("LIST_WITH_DOT. raised the wrong exception: %r" % e) |
| 160 | |
| 161 | try: |
| 162 | IntegratedTestKeywordParser("CUSTOM_NO_SUFFIX", |
| 163 | ParserKind.CUSTOM, custom_parse), |
| 164 | self.fail("CUSTOM_NO_SUFFIX failed to raise an exception") |
| 165 | except ValueError as e: |
| 166 | pass |
| 167 | except BaseException as e: |
| 168 | self.fail("CUSTOM_NO_SUFFIX raised the wrong exception: %r" % e) |
| 169 | |
| 170 | # Both '.' and ':' are allowed for CUSTOM keywords. |
| 171 | try: |
| 172 | IntegratedTestKeywordParser("CUSTOM_WITH_DOT.", |
| 173 | ParserKind.CUSTOM, custom_parse), |
| 174 | except BaseException as e: |
| 175 | self.fail("CUSTOM_WITH_DOT. raised an exception: %r" % e) |
| 176 | try: |
| 177 | IntegratedTestKeywordParser("CUSTOM_WITH_COLON:", |
| 178 | ParserKind.CUSTOM, custom_parse), |
| 179 | except BaseException as e: |
| 180 | self.fail("CUSTOM_WITH_COLON: raised an exception: %r" % e) |
| 181 | |
| 182 | try: |
| 183 | IntegratedTestKeywordParser("CUSTOM_NO_PARSER:", |
| 184 | ParserKind.CUSTOM), |
| 185 | self.fail("CUSTOM_NO_PARSER: failed to raise an exception") |
| 186 | except ValueError as e: |
| 187 | pass |
| 188 | except BaseException as e: |
| 189 | self.fail("CUSTOM_NO_PARSER: raised the wrong exception: %r" % e) |
Eric Fiselier | df87d07 | 2016-12-05 20:21:21 +0000 | [diff] [blame] | 190 | |
| 191 | if __name__ == '__main__': |
| 192 | TestIntegratedTestKeywordParser.load_keyword_parser_lit_tests() |
| 193 | unittest.main(verbosity=2) |