Skip to content

tests: refactor / simplify #206

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions tests/test_attributes.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ def test_attributes_thiscall():
config = autoconfig.cxx_parsers_cfg.config.clone()

config.flags = ["f2"]
config.castxml_epic_version = 1

decls = parser.parse(TEST_FILES, config, COMPILATION_MODE)
global_ns = declarations.get_global_namespace(decls)
Expand Down
43 changes: 10 additions & 33 deletions tests/test_castxml_wrong_epic.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,43 +3,20 @@
# Distributed under the Boost Software License, Version 1.0.
# See http://www.boost.org/LICENSE_1_0.txt

import unittest
import pytest

from . import parser_test_case
from . import autoconfig

from pygccxml import parser


class Test(parser_test_case.parser_test_case_t):
def test_castxml_epic_version_check():
"""
Test using a forbidden value for the castxml epic version.

def test_castxml_epic_version_check(self):
"""
Test using a forbidden value for the castxml epic version.
"""

"""

if self.config.castxml_epic_version != 1:
# Run this test only with castxml epic version == 1
return

self.config.castxml_epic_version = 2
self.assertRaises(
RuntimeError, lambda: parser.parse_string("", self.config))

# Reset castxml epic version
self.config.castxml_epic_version = 1


def create_suite():
suite = unittest.TestSuite()
suite.addTest(
unittest.TestLoader().loadTestsFromTestCase(testCaseClass=Test))
return suite


def run_suite():
unittest.TextTestRunner(verbosity=2).run(create_suite())


if __name__ == "__main__":
run_suite()
config = autoconfig.cxx_parsers_cfg.config.clone()
config.castxml_epic_version = 2
with pytest.raises(RuntimeError):
parser.parse_string("", config)
74 changes: 28 additions & 46 deletions tests/test_ccflags.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,65 +3,47 @@
# Distributed under the Boost Software License, Version 1.0.
# See http://www.boost.org/LICENSE_1_0.txt

import unittest
import pytest

from . import parser_test_case
from . import autoconfig

from pygccxml import parser
from pygccxml import declarations


class Test(parser_test_case.parser_test_case_t):
global_ns = None
TEST_FILES = [
"test_ccflags.hpp",
]

def __init__(self, *args):
parser_test_case.parser_test_case_t.__init__(self, *args)
self.header = "test_ccflags.hpp"
self.global_ns = None
self.config.castxml_epic_version = 1
self.config.append_cflags("-fopenmp")
COMPILATION_MODE = parser.COMPILATION_MODE.ALL_AT_ONCE

def _parse_src(self):
decls = parser.parse([self.header], self.config)
Test.global_ns = declarations.get_global_namespace(decls)
Test.xml_generator_from_xml_file = (
self.config.xml_generator_from_xml_file
)
self.xml_generator_from_xml_file = Test.xml_generator_from_xml_file

self.global_ns = Test.global_ns
@pytest.fixture
def config():
config = autoconfig.cxx_parsers_cfg.config.clone()
config.castxml_epic_version = 1
config.append_cflags("-fopenmp")
return config

def _add_ccflags(self):
if "clang++" in self.config.compiler_path:
self.config.append_ccflags("-Xpreprocessor")

self.config.append_ccflags("-fopenmp")
def test_ccflags(config):
# First check that macro is not defined.
decls = parser.parse(TEST_FILES, config, COMPILATION_MODE)
global_ns = declarations.get_global_namespace(decls)

def test(self):
# First check that macro is not defined.
self._parse_src()
namespace_names = [
n.name for n in self.global_ns.namespaces(allow_empty=True)
]
self.assertNotIn("ccflags_test_namespace", namespace_names)
namespace_names = [
n.name for n in global_ns.namespaces(allow_empty=True)
]
assert "ccflags_test_namespace" not in namespace_names

# Next check that macro is defined when passed directly as ccflag
self._add_ccflags()
self._parse_src()
namespace_names = [n.name for n in self.global_ns.namespaces()]
self.assertIn("ccflags_test_namespace", namespace_names)
# Next check that macro is defined when passed directly as ccflag

if "clang++" in config.compiler_path:
config.append_ccflags("-Xpreprocessor")
config.append_ccflags("-fopenmp")

def create_suite():
suite = unittest.TestSuite()
suite.addTest(
unittest.TestLoader().loadTestsFromTestCase(testCaseClass=Test))
return suite
decls = parser.parse(TEST_FILES, config, COMPILATION_MODE)
global_ns = declarations.get_global_namespace(decls)


def run_suite():
unittest.TextTestRunner(verbosity=2).run(create_suite())


if __name__ == "__main__":
run_suite()
namespace_names = [n.name for n in global_ns.namespaces()]
assert "ccflags_test_namespace" in namespace_names
177 changes: 88 additions & 89 deletions tests/test_comments.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,98 +3,97 @@
# Distributed under the Boost Software License, Version 1.0.
# See http://www.boost.org/LICENSE_1_0.txt

import unittest
import pytest

from . import parser_test_case
from . import autoconfig

from pygccxml import parser
from pygccxml import declarations


class Test(parser_test_case.parser_test_case_t):
global_ns = None

def __init__(self, *args):
parser_test_case.parser_test_case_t.__init__(self, *args)
self.header = "test_comments.hpp"
self.global_ns = None
self.config.castxml_epic_version = 1

def _check_comment_content(self, list, comment_decl):
if comment_decl.text:
self.assertEqual(list, comment_decl.text)
else:
print("No text in comment to check")

def setUp(self):

if not self.global_ns:
decls = parser.parse([self.header], self.config)
Test.global_ns = declarations.get_global_namespace(decls)
Test.xml_generator_from_xml_file = \
self.config.xml_generator_from_xml_file
self.xml_generator_from_xml_file = Test.xml_generator_from_xml_file

self.global_ns = Test.global_ns

def test(self):
"""
Check the comment parsing
"""
if self.config.castxml_epic_version != 1:
# Run this test only with castxml epic version == 1
return
tnamespace = self.global_ns.namespace("comment")

self.assertIn("comment", dir(tnamespace))
self._check_comment_content(["//! Namespace Comment",
"//! Across multiple lines"],
tnamespace.comment)

tenumeration = tnamespace.enumeration("com_enum")
self.assertIn("comment", dir(tenumeration))
self._check_comment_content(['/// Outside Class enum comment'],
tenumeration.comment)

tclass = tnamespace.class_("test")
self.assertIn("comment", dir(tclass))
self._check_comment_content(["/** class comment */"], tclass.comment)

tcls_enumeration = tclass.enumeration("test_enum")
self.assertIn("comment", dir(tcls_enumeration))
self._check_comment_content(['/// inside class enum comment'],
tcls_enumeration.comment)

tmethod = tclass.member_functions()[0]

self.assertIn("comment", dir(tmethod))
self._check_comment_content(["/// cxx comment",
"/// with multiple lines"],
tmethod.comment)

tconstructor = tclass.constructors()[0]

self.assertIn("comment", dir(tconstructor))
self._check_comment_content(["/** doc comment */"],
tconstructor.comment)

for indx, cmt in enumerate(['//! mutable field comment',
"/// bit field comment"]):
tvariable = tclass.variables()[indx]
self.assertIn("comment", dir(tvariable))
self._check_comment_content([cmt], tvariable.comment)


def create_suite():
suite = unittest.TestSuite()
suite.addTest(
unittest.TestLoader().loadTestsFromTestCase(testCaseClass=Test))
return suite


def run_suite():
unittest.TextTestRunner(verbosity=2).run(create_suite())


if __name__ == "__main__":
run_suite()
TEST_FILES = [
"test_comments.hpp",
]


@pytest.fixture
def global_ns():
COMPILATION_MODE = parser.COMPILATION_MODE.ALL_AT_ONCE
INIT_OPTIMIZER = True
config = autoconfig.cxx_parsers_cfg.config.clone()
config.castxml_epic_version = 1
decls = parser.parse(TEST_FILES, config, COMPILATION_MODE)
global_ns = declarations.get_global_namespace(decls)
if INIT_OPTIMIZER:
global_ns.init_optimizer()
return global_ns


def _check_comment_content(list, comment_decl):
if comment_decl.text:
assert list == comment_decl.text
else:
print("No text in comment to check")


def test_comments(global_ns):
"""
Check the comment parsing
"""
tnamespace = global_ns.namespace("comment")

assert "comment" in dir(tnamespace)
_check_comment_content(
[
"//! Namespace Comment",
"//! Across multiple lines"
],
tnamespace.comment
)

tenumeration = tnamespace.enumeration("com_enum")
assert "comment" in dir(tenumeration)
_check_comment_content(
['/// Outside Class enum comment'],
tenumeration.comment
)

tclass = tnamespace.class_("test")
assert "comment" in dir(tclass)
_check_comment_content(
["/** class comment */"],
tclass.comment
)

tcls_enumeration = tclass.enumeration("test_enum")
assert "comment" in dir(tcls_enumeration)
_check_comment_content(
['/// inside class enum comment'],
tcls_enumeration.comment
)

tmethod = tclass.member_functions()[0]

assert "comment" in dir(tmethod)
_check_comment_content(
["/// cxx comment", "/// with multiple lines"],
tmethod.comment
)

tconstructor = tclass.constructors()[0]

assert "comment" in dir(tconstructor)
_check_comment_content(
["/** doc comment */"],
tconstructor.comment
)

for indx, cmt in enumerate(
[
'//! mutable field comment',
"/// bit field comment"
]
):
tvariable = tclass.variables()[indx]
assert "comment" in dir(tvariable)
_check_comment_content([cmt], tvariable.comment)
Loading
Loading