Skip to content

Commit 2e9348c

Browse files
authored
Merge pull request #184 from iMichka/pytest2
Tests: move test_utils
2 parents 67364e5 + 853e51f commit 2e9348c

File tree

6 files changed

+163
-12
lines changed

6 files changed

+163
-12
lines changed

tests/__init__.py

Whitespace-only changes.

tests/autoconfig.py

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
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 os
7+
import sys
8+
import logging
9+
import warnings
10+
import platform
11+
12+
# Prevents copy.deepcopy RecursionError in some tests (Travis build)
13+
sys.setrecursionlimit(10000)
14+
15+
this_module_dir_path = os.path.abspath(
16+
os.path.dirname(sys.modules[__name__].__file__))
17+
18+
data_directory = os.path.join(this_module_dir_path, 'data')
19+
build_directory = os.path.join(this_module_dir_path, 'temp')
20+
21+
sys.path.insert(1, os.path.join(os.curdir, '..'))
22+
# The tests are run on the parent pygccxml directory, not the one
23+
# in site-packages. Insert the directory's path.
24+
sys.path.insert(1, "../src/pygccxml")
25+
26+
from pygccxml import parser # nopep8
27+
from pygccxml import utils # nopep8
28+
29+
# We want to make sure we throw an error for ALL the warnings during the
30+
# tests. This will allow us to be notified by the build bots, so that the
31+
# warnings can be fixed.
32+
warnings.simplefilter("error", Warning)
33+
34+
# Set logging level
35+
utils.loggers.set_level(logging.CRITICAL)
36+
37+
# Find out the c++ parser (gccxml or castxml)
38+
generator_path, generator_name = utils.find_xml_generator()
39+
40+
41+
class cxx_parsers_cfg(object):
42+
config = parser.load_xml_generator_configuration(
43+
os.path.normpath(this_module_dir_path + '/xml_generator.cfg'),
44+
xml_generator_path=generator_path,
45+
working_directory=data_directory,
46+
xml_generator=generator_name)
47+
48+
if platform.system() == 'Windows':
49+
config.define_symbols.append('_HAS_EXCEPTIONS=0')
50+
51+
52+
if cxx_parsers_cfg.config.xml_generator:
53+
generator_name = cxx_parsers_cfg.config.xml_generator
54+
if cxx_parsers_cfg.config.xml_generator_path:
55+
generator_path = cxx_parsers_cfg.config.xml_generator_path

tests/parser_test_case.py

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
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])))

unittests/test_utils.py renamed to tests/test_utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
class Test(parser_test_case.parser_test_case_t):
1616

17-
def test(self):
17+
def test_contains_parent_dir(self):
1818
path = os.path.normpath("/mypath/folder1/folder2/folder3")
1919
dirs = [
2020
os.path.normpath("/mypath/folder1/folder2/"),

unittests/autoconfig.py

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@
99
import warnings
1010
import platform
1111

12+
from pygccxml import parser
13+
from pygccxml import utils
14+
1215
# Prevents copy.deepcopy RecursionError in some tests (Travis build)
1316
sys.setrecursionlimit(10000)
1417

@@ -18,14 +21,6 @@
1821
data_directory = os.path.join(this_module_dir_path, 'data')
1922
build_directory = os.path.join(this_module_dir_path, 'temp')
2023

21-
sys.path.insert(1, os.path.join(os.curdir, '..'))
22-
# The tests are run on the parent pygccxml directory, not the one
23-
# in site-packages. Insert the directory's path.
24-
sys.path.insert(1, "../src/pygccxml")
25-
26-
from pygccxml import parser # nopep8
27-
from pygccxml import utils # nopep8
28-
2924
# We want to make sure we throw an error for ALL the warnings during the
3025
# tests. This will allow us to be notified by the build bots, so that the
3126
# warnings can be fixed.
@@ -34,7 +29,7 @@
3429
# Set logging level
3530
utils.loggers.set_level(logging.CRITICAL)
3631

37-
# Find out the c++ parser (gccxml or castxml)
32+
# Find out the c++ parser (castxml)
3833
generator_path, generator_name = utils.find_xml_generator()
3934

4035

unittests/test_all.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,6 @@
6161
from . import inline_specifier_tester
6262
from . import test_create_decl_string
6363
from . import example_tester
64-
from . import test_utils
6564
from . import test_va_list_tag_removal
6665
from . import test_copy_constructor
6766
from . import test_cpp_standards
@@ -140,7 +139,6 @@
140139
test_pattern_parser,
141140
test_function_pointer,
142141
test_directory_cache,
143-
test_utils,
144142
test_cpp_standards,
145143
test_va_list_tag_removal,
146144
decl_printer_tester,

0 commit comments

Comments
 (0)