TestRunner.py 7.4 KB

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