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