|
| 1 | +# Copyright 2014-2017 Insight Software Consortium. |
| 2 | +# Copyright 2004-2009 Roman Yakovenko. |
| 3 | +# Distributed under the Boost Software License, Version 1.0. |
| 4 | +# See http://www.boost.org/LICENSE_1_0.txt |
| 5 | + |
| 6 | +import pprint |
| 7 | +import time |
| 8 | +import unittest |
| 9 | + |
| 10 | +from . import autoconfig |
| 11 | + |
| 12 | + |
| 13 | +class parser_test_case_t(unittest.TestCase): |
| 14 | + |
| 15 | + CXX_PARSER_CFG = None |
| 16 | + xml_generator_from_xml_file = None |
| 17 | + |
| 18 | + def __init__(self, *args): |
| 19 | + unittest.TestCase.__init__(self, *args) |
| 20 | + self.xml_generator_from_xml_file = None |
| 21 | + if self.CXX_PARSER_CFG: |
| 22 | + self.config = self.CXX_PARSER_CFG.clone() |
| 23 | + elif autoconfig.cxx_parsers_cfg.config: |
| 24 | + self.config = autoconfig.cxx_parsers_cfg.config.clone() |
| 25 | + else: |
| 26 | + pass |
| 27 | + |
| 28 | + def run(self, result=None): |
| 29 | + """ |
| 30 | + Override the run method. |
| 31 | +
|
| 32 | + Allows to measure the time each test needs. The result is written |
| 33 | + in the test_cost.log file. |
| 34 | +
|
| 35 | + """ |
| 36 | + with open("test_cost.log", "a") as cost_file: |
| 37 | + start_time = time.time() |
| 38 | + super(parser_test_case_t, self).run(result) |
| 39 | + name = super(parser_test_case_t, self).id() |
| 40 | + cost_file.write( |
| 41 | + name + " " + |
| 42 | + str(time.time() - start_time) + "\n") |
| 43 | + |
| 44 | + def _test_type_composition(self, type_, expected_compound, expected_base): |
| 45 | + self.assertTrue( |
| 46 | + isinstance(type_, expected_compound), |
| 47 | + "the compound type('%s') should be '%s'" % |
| 48 | + (type_.decl_string, expected_compound.__name__)) |
| 49 | + self.assertTrue( |
| 50 | + isinstance(type_.base, expected_base), |
| 51 | + "base type('%s') should be '%s'" % |
| 52 | + (type_.decl_string, expected_base.__name__)) |
| 53 | + |
| 54 | + def _test_calldef_return_type(self, calldef, expected_type): |
| 55 | + self.assertTrue( |
| 56 | + isinstance(calldef.return_type, expected_type), |
| 57 | + ("the function's '%s' expected return type is '%s' and in " + |
| 58 | + "reality it is different('%s')") % |
| 59 | + (calldef.name, expected_type.__name__, |
| 60 | + calldef.return_type.__class__.__name__)) |
| 61 | + |
| 62 | + def _test_calldef_args(self, calldef, expected_args): |
| 63 | + self.assertTrue( |
| 64 | + len(calldef.arguments) == len(expected_args), |
| 65 | + ("the function's '%s' expected number of arguments is '%d' and " + |
| 66 | + "in reality it is different('%d')") % |
| 67 | + (calldef.name, len(expected_args), len(calldef.arguments))) |
| 68 | + |
| 69 | + for i, expected_arg in enumerate(expected_args): |
| 70 | + arg = calldef.arguments[i] |
| 71 | + self.assertTrue( |
| 72 | + arg == expected_arg, |
| 73 | + ("the function's '%s' expected %d's argument is '%s' and in " + |
| 74 | + "reality it is different('%s')") % |
| 75 | + (calldef.name, i, pprint.pformat(expected_arg.__dict__), |
| 76 | + pprint.pformat(arg.__dict__))) |
| 77 | + |
| 78 | + def _test_calldef_exceptions(self, calldef, exceptions): |
| 79 | + # exceptions is list of classes names |
| 80 | + exception_decls = [] |
| 81 | + for name in exceptions: |
| 82 | + exception_decl = self.global_ns.class_(name) |
| 83 | + self.assertTrue( |
| 84 | + exception_decl, |
| 85 | + "unable to find exception class '%s'" % |
| 86 | + name) |
| 87 | + exception_decls.append(exception_decl) |
| 88 | + exception_decls.sort() |
| 89 | + self.assertTrue( |
| 90 | + len(calldef.exceptions) == len(exception_decls), |
| 91 | + ("the function's '%s' expected number of exceptions is '%d' and " + |
| 92 | + "in reality it is different('%d')") % |
| 93 | + (calldef.name, |
| 94 | + len(exception_decls), |
| 95 | + len(calldef.exceptions))) |
| 96 | + exceptions_indeed = sorted(calldef.exceptions[:]) |
| 97 | + self.assertTrue( |
| 98 | + exception_decls == exceptions_indeed, |
| 99 | + ("the function's '%s' expected exceptions are '%s' and in " + |
| 100 | + "reality it is different('%s')") % |
| 101 | + (calldef.name, |
| 102 | + pprint.pformat([delc.name for delc in exception_decls]), |
| 103 | + pprint.pformat([delc.name for delc in exceptions_indeed]))) |
0 commit comments