Skip to content

Commit 877c4de

Browse files
authored
Merge pull request #207 from CastXML/refactor3
tests: refactor / simplify
2 parents 705230e + 71eea2f commit 877c4de

9 files changed

+539
-718
lines changed

tests/test_copy_constructor2.py

Lines changed: 31 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -5,70 +5,41 @@
55

66
import os
77
import bz2
8-
import unittest
98

109
from . import autoconfig
11-
from . import parser_test_case
1210

1311
from pygccxml import parser
1412
from pygccxml import declarations
1513

1614

17-
class Test(parser_test_case.parser_test_case_t):
18-
19-
def __init__(self, *args):
20-
parser_test_case.parser_test_case_t.__init__(self, *args)
21-
self.global_ns = None
22-
self.xml_path = None
23-
24-
def setUp(self):
25-
if not self.global_ns:
26-
27-
# Extract the xml file from the bz2 archive
28-
bz2_path = os.path.join(
29-
autoconfig.data_directory,
30-
'ogre.1.7.xml.bz2')
31-
self.xml_path = os.path.join(
32-
autoconfig.data_directory,
33-
'ogre.1.7.xml')
34-
with open(self.xml_path, 'wb') as new_file:
35-
# bz2.BZ2File can not be used in a with statement in python 2.6
36-
bz2_file = bz2.BZ2File(bz2_path, 'rb')
37-
for data in iter(lambda: bz2_file.read(100 * 1024), b''):
38-
new_file.write(data)
39-
bz2_file.close()
40-
41-
reader = parser.source_reader_t(autoconfig.cxx_parsers_cfg.config)
42-
self.global_ns = declarations.get_global_namespace(
43-
reader.read_xml_file(
44-
self.xml_path))
45-
self.global_ns.init_optimizer()
46-
47-
def tearDown(self):
48-
# Delete the extracted xml file
49-
os.remove(self.xml_path)
50-
51-
def test_copy_constructor2(self):
52-
for x in self.global_ns.typedefs('SettingsMultiMap'):
53-
self.assertTrue(not declarations.is_noncopyable(x))
54-
55-
for x in self.global_ns.typedefs('SettingsIterator'):
56-
self.assertTrue(not declarations.is_noncopyable(x))
57-
58-
for x in self.global_ns.typedefs('SectionIterator'):
59-
self.assertTrue(not declarations.is_noncopyable(x))
60-
61-
62-
def create_suite():
63-
suite = unittest.TestSuite()
64-
suite.addTest(
65-
unittest.TestLoader().loadTestsFromTestCase(testCaseClass=Test))
66-
return suite
67-
68-
69-
def run_suite():
70-
unittest.TextTestRunner(verbosity=2).run(create_suite())
71-
72-
73-
if __name__ == "__main__":
74-
run_suite()
15+
def test_copy_constructor2():
16+
# Extract the xml file from the bz2 archive
17+
bz2_path = os.path.join(
18+
autoconfig.data_directory,
19+
'ogre.1.7.xml.bz2')
20+
xml_path = os.path.join(
21+
autoconfig.data_directory,
22+
'ogre.1.7.xml')
23+
with open(xml_path, 'wb') as new_file:
24+
# bz2.BZ2File can not be used in a with statement in python 2.6
25+
bz2_file = bz2.BZ2File(bz2_path, 'rb')
26+
for data in iter(lambda: bz2_file.read(100 * 1024), b''):
27+
new_file.write(data)
28+
bz2_file.close()
29+
30+
reader = parser.source_reader_t(autoconfig.cxx_parsers_cfg.config)
31+
global_ns = declarations.get_global_namespace(
32+
reader.read_xml_file(xml_path)
33+
)
34+
global_ns.init_optimizer()
35+
36+
for x in global_ns.typedefs('SettingsMultiMap'):
37+
assert declarations.is_noncopyable(x) is False
38+
39+
for x in global_ns.typedefs('SettingsIterator'):
40+
assert declarations.is_noncopyable(x) is False
41+
42+
for x in global_ns.typedefs('SectionIterator'):
43+
assert declarations.is_noncopyable(x) is False
44+
45+
os.remove(xml_path)

tests/test_cpp_standards.py

Lines changed: 25 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -3,62 +3,44 @@
33
# Distributed under the Boost Software License, Version 1.0.
44
# See http://www.boost.org/LICENSE_1_0.txt
55

6+
import pytest
7+
68
import platform
7-
import unittest
89

9-
from . import parser_test_case
10+
from . import autoconfig
1011

1112
from pygccxml import parser
1213

1314

14-
class Test(parser_test_case.parser_test_case_t):
15-
16-
def test(self):
17-
"""
18-
Test different compilation standards by setting cflags.
19-
20-
"""
21-
22-
parser.parse(["cpp_standards.hpp"], self.config)
23-
24-
if platform.system() != 'Windows':
25-
self.config.cflags = "-std=c++98"
26-
parser.parse(["cpp_standards.hpp"], self.config)
27-
28-
self.config.cflags = "-std=c++03"
29-
parser.parse(["cpp_standards.hpp"], self.config)
15+
def test_cpp_standards():
16+
"""
17+
Test different compilation standards by setting cflags.
3018
31-
self.config.cflags = "-std=c++11"
32-
parser.parse(["cpp_standards.hpp"], self.config)
19+
"""
3320

34-
# This is broken with llvm 3.6.2 (the one from homebrew)
35-
# It should work with never llvms but I keep the test disabled
36-
# See https://llvm.org/bugs/show_bug.cgi?id=24872
37-
# self.config.cflags = "-std=c++14"
38-
# parser.parse(["cpp_standards.hpp"], self.config)
21+
config = autoconfig.cxx_parsers_cfg.config.clone()
3922

40-
# Same as above
41-
# self.config.cflags = "-std=c++1z"
42-
# parser.parse(["cpp_standards.hpp"], self.config)
23+
parser.parse(["cpp_standards.hpp"], config)
4324

44-
# Pass down a flag that does not exist.
45-
# This should raise an exception.
46-
self.config.cflags = "-std=c++00"
47-
self.assertRaises(
48-
RuntimeError,
49-
lambda: parser.parse(["cpp_standards.hpp"], self.config))
25+
if platform.system() != 'Windows':
26+
config.cflags = "-std=c++98"
27+
parser.parse(["cpp_standards.hpp"], config)
5028

29+
config.cflags = "-std=c++03"
30+
parser.parse(["cpp_standards.hpp"], config)
5131

52-
def create_suite():
53-
suite = unittest.TestSuite()
54-
suite.addTest(
55-
unittest.TestLoader().loadTestsFromTestCase(testCaseClass=Test))
56-
return suite
32+
config.cflags = "-std=c++11"
5733

34+
parser.parse(["cpp_standards.hpp"], config)
5835

59-
def run_suite():
60-
unittest.TextTestRunner(verbosity=2).run(create_suite())
36+
config.cflags = "-std=c++14"
37+
parser.parse(["cpp_standards.hpp"], config)
6138

39+
config.cflags = "-std=c++1z"
40+
parser.parse(["cpp_standards.hpp"], config)
6241

63-
if __name__ == "__main__":
64-
run_suite()
42+
# Pass down a flag that does not exist.
43+
# This should raise an exception.
44+
config.cflags = "-std=c++00"
45+
with pytest.raises(RuntimeError):
46+
parser.parse(["cpp_standards.hpp"], config)

tests/test_decl_printer.py

Lines changed: 49 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -4,76 +4,58 @@
44
# See http://www.boost.org/LICENSE_1_0.txt
55

66
import sys
7-
import unittest
7+
import pytest
88

9-
from . import parser_test_case
9+
from . import autoconfig
1010

1111
from pygccxml import parser
1212
from pygccxml import declarations
1313

1414

15-
class Test(parser_test_case.parser_test_case_t):
16-
17-
def __init__(self, *args):
18-
parser_test_case.parser_test_case_t.__init__(self, *args)
19-
self.__files = [
20-
'core_ns_join_1.hpp',
21-
'core_ns_join_2.hpp',
22-
'core_ns_join_3.hpp',
23-
'core_membership.hpp',
24-
'core_class_hierarchy.hpp',
25-
'core_types.hpp',
26-
'core_diamand_hierarchy_base.hpp',
27-
'core_diamand_hierarchy_derived1.hpp',
28-
'core_diamand_hierarchy_derived2.hpp',
29-
'core_diamand_hierarchy_final_derived.hpp',
30-
'core_overloads_1.hpp',
31-
'core_overloads_2.hpp',
32-
'typedefs_base.hpp']
33-
34-
# for i, f in enumerate(self.__files):
35-
# f = parser.create_cached_source_fc(
36-
# os.path.join( autoconfig.data_directory, f)
37-
# , os.path.join( autoconfig.data_directory, f + '.xml') )
38-
# self.__files[i] = f
39-
prj_reader = parser.project_reader_t(self.config)
40-
self.decls = prj_reader.read_files(
41-
self.__files,
42-
compilation_mode=parser.COMPILATION_MODE.FILE_BY_FILE)
43-
44-
def test_printer(self):
45-
46-
# Redirect sys.stdout to a class with a writer doing nothing
47-
# This greatly reduces the size of the test output and makes
48-
# test log files readable.
49-
# Note: flush needs to be defined; because if not this will
50-
# result in an AttributeError on call.
51-
class DontPrint(object):
52-
def write(*args):
53-
pass
54-
55-
def flush(*args):
56-
pass
57-
sys.stdout = DontPrint()
58-
59-
declarations.print_declarations(self.decls)
60-
61-
def test__str__(self):
62-
decls = declarations.make_flatten(self.decls)
63-
for decl in decls:
64-
str(decl)
65-
66-
67-
def create_suite():
68-
suite = unittest.TestSuite()
69-
suite.addTest(
70-
unittest.TestLoader().loadTestsFromTestCase(testCaseClass=Test))
71-
return suite
72-
73-
74-
def run_suite():
75-
unittest.TextTestRunner(verbosity=2).run(create_suite())
76-
77-
78-
if __name__ == "__main__":
79-
run_suite()
15+
TEST_FILES = [
16+
'core_ns_join_1.hpp',
17+
'core_ns_join_2.hpp',
18+
'core_ns_join_3.hpp',
19+
'core_membership.hpp',
20+
'core_class_hierarchy.hpp',
21+
'core_types.hpp',
22+
'core_diamand_hierarchy_base.hpp',
23+
'core_diamand_hierarchy_derived1.hpp',
24+
'core_diamand_hierarchy_derived2.hpp',
25+
'core_diamand_hierarchy_final_derived.hpp',
26+
'core_overloads_1.hpp',
27+
'core_overloads_2.hpp',
28+
'typedefs_base.hpp',
29+
]
30+
31+
32+
@pytest.fixture
33+
def decls():
34+
COMPILATION_MODE = parser.COMPILATION_MODE.FILE_BY_FILE
35+
config = autoconfig.cxx_parsers_cfg.config.clone()
36+
config.castxml_epic_version = 1
37+
decls = parser.parse(TEST_FILES, config, COMPILATION_MODE)
38+
return decls
39+
40+
41+
def test_printer(decls):
42+
# Redirect sys.stdout to a class with a writer doing nothing
43+
# This greatly reduces the size of the test output and makes
44+
# test log files readable.
45+
# Note: flush needs to be defined; because if not this will
46+
# result in an AttributeError on call.
47+
class DontPrint(object):
48+
def write(*args):
49+
pass
50+
51+
def flush(*args):
52+
pass
53+
sys.stdout = DontPrint()
54+
55+
declarations.print_declarations(decls)
56+
57+
58+
def test__str__(decls):
59+
decls = declarations.make_flatten(decls)
60+
for decl in decls:
61+
str(decl)

tests/test_declaration_files.py

Lines changed: 29 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -4,54 +4,38 @@
44
# See http://www.boost.org/LICENSE_1_0.txt
55

66
import os
7-
import unittest
87

9-
from . import parser_test_case
8+
from . import autoconfig
109

1110
from pygccxml import parser
1211
from pygccxml import declarations
1312

1413

15-
class Test(parser_test_case.parser_test_case_t):
16-
17-
def __init__(self, *args):
18-
parser_test_case.parser_test_case_t.__init__(self, *args)
19-
self.__files = [
20-
'core_ns_join_1.hpp',
21-
'core_ns_join_2.hpp',
22-
'core_ns_join_3.hpp',
23-
'core_membership.hpp',
24-
'core_class_hierarchy.hpp',
25-
'core_types.hpp',
26-
'core_diamand_hierarchy_base.hpp',
27-
'core_diamand_hierarchy_derived1.hpp',
28-
'core_diamand_hierarchy_derived2.hpp',
29-
'core_diamand_hierarchy_final_derived.hpp',
30-
'core_overloads_1.hpp',
31-
'core_overloads_2.hpp']
32-
33-
def test(self):
34-
prj_reader = parser.project_reader_t(self.config)
35-
decls = prj_reader.read_files(
36-
self.__files,
37-
compilation_mode=parser.COMPILATION_MODE.ALL_AT_ONCE)
38-
files = declarations.declaration_files(decls)
39-
result = set()
40-
for fn in files:
41-
result.add(os.path.split(fn)[1])
42-
self.assertTrue(set(self.__files).issubset(result))
43-
44-
45-
def create_suite():
46-
suite = unittest.TestSuite()
47-
suite.addTest(
48-
unittest.TestLoader().loadTestsFromTestCase(testCaseClass=Test))
49-
return suite
50-
51-
52-
def run_suite():
53-
unittest.TextTestRunner(verbosity=2).run(create_suite())
54-
55-
56-
if __name__ == "__main__":
57-
run_suite()
14+
TEST_FILES = [
15+
'core_ns_join_1.hpp',
16+
'core_ns_join_2.hpp',
17+
'core_ns_join_3.hpp',
18+
'core_membership.hpp',
19+
'core_class_hierarchy.hpp',
20+
'core_types.hpp',
21+
'core_diamand_hierarchy_base.hpp',
22+
'core_diamand_hierarchy_derived1.hpp',
23+
'core_diamand_hierarchy_derived2.hpp',
24+
'core_diamand_hierarchy_final_derived.hpp',
25+
'core_overloads_1.hpp',
26+
'core_overloads_2.hpp',
27+
'typedefs_base.hpp',
28+
]
29+
30+
31+
def test_declaration_files():
32+
config = autoconfig.cxx_parsers_cfg.config.clone()
33+
prj_reader = parser.project_reader_t(config)
34+
decls = prj_reader.read_files(
35+
TEST_FILES,
36+
compilation_mode=parser.COMPILATION_MODE.ALL_AT_ONCE)
37+
files = declarations.declaration_files(decls)
38+
result = set()
39+
for fn in files:
40+
result.add(os.path.split(fn)[1])
41+
assert set(TEST_FILES).issubset(result) is True

0 commit comments

Comments
 (0)