Home | History | Annotate | Download | only in unit
      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
     12 from lit.TestRunner import ParserKind, IntegratedTestKeywordParser, \
     13                            parseIntegratedTestScript
     14 
     15 
     16 class 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:')
     92         self.assertItemsEqual(list_parser.getValue(),
     93                               ['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()
    109         self.assertItemsEqual(value, ['a', 'b', 'c'])
    110 
    111 
    112 if __name__ == '__main__':
    113     TestIntegratedTestKeywordParser.load_keyword_parser_lit_tests()
    114     unittest.main(verbosity=2)
    115