Skip to content

tests: refactor / simplify #209

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
Nov 11, 2024
Merged
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
35 changes: 7 additions & 28 deletions tests/test_gccxml10184.py
Original file line number Diff line number Diff line change
@@ -3,9 +3,7 @@
# Distributed under the Boost Software License, Version 1.0.
# See http://www.boost.org/LICENSE_1_0.txt

import unittest

from . import parser_test_case
from . import autoconfig

from pygccxml import parser
from pygccxml import declarations
@@ -21,28 +19,9 @@ class A {
"""


class Test(parser_test_case.parser_test_case_t):

def __init__(self, *args):
parser_test_case.parser_test_case_t.__init__(self, *args)

def test(self):
decls = parser.parse_string(code, self.config)
global_ns = declarations.get_global_namespace(decls)
self.assertTrue(global_ns.variable('a').bits == 1)
self.assertTrue(global_ns.variable('unused').bits == 31)


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()
def test_gccxml_10184():
config = autoconfig.cxx_parsers_cfg.config.clone()
decls = parser.parse_string(code, config)
global_ns = declarations.get_global_namespace(decls)
assert global_ns.variable('a').bits == 1
assert global_ns.variable('unused').bits == 31
56 changes: 18 additions & 38 deletions tests/test_gccxml10185.py
Original file line number Diff line number Diff line change
@@ -3,9 +3,9 @@
# 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
@@ -18,43 +18,23 @@
"""


class Test(parser_test_case.parser_test_case_t):

def __init__(self, *args):
parser_test_case.parser_test_case_t.__init__(self, *args)

def test(self):
"""
The purpose of this test was to check if changes to GCCXML
would lead to changes in the outputted xml file (Meaning
the bug was fixed).

GCCXML wrongly outputted partial template specialization.
CastXML does not have this bug. In this case we check if
the template specialization can not be found; which is the
expected/wanted behaviour.

https://github.com/CastXML/CastXML/issues/20

"""

decls = parser.parse_string(code, self.config)
global_ns = declarations.get_global_namespace(decls)
self.assertRaises(
declarations.declaration_not_found_t,
lambda: global_ns.class_('A<const char [N]>'))


def create_suite():
suite = unittest.TestSuite()
suite.addTest(
unittest.TestLoader().loadTestsFromTestCase(testCaseClass=Test))
return suite
def test_partial_template():
"""
The purpose of this test was to check if changes to GCCXML
would lead to changes in the outputted xml file (Meaning
the bug was fixed).

GCCXML wrongly outputted partial template specialization.
CastXML does not have this bug. In this case we check if
the template specialization can not be found; which is the
expected/wanted behaviour.

def run_suite():
unittest.TextTestRunner(verbosity=2).run(create_suite())
https://github.com/CastXML/CastXML/issues/20

"""

if __name__ == "__main__":
run_suite()
config = autoconfig.cxx_parsers_cfg.config.clone()
decls = parser.parse_string(code, config)
global_ns = declarations.get_global_namespace(decls)
with pytest.raises(declarations.declaration_not_found_t):
global_ns.class_('A<const char [N]>')
81 changes: 27 additions & 54 deletions tests/test_has_binary_operator_traits.py
Original file line number Diff line number Diff line change
@@ -3,72 +3,45 @@
# 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

TEST_FILES = ["has_public_binary_operator_traits.hpp"]

class Test(parser_test_case.parser_test_case_t):
COMPILATION_MODE = parser.COMPILATION_MODE.ALL_AT_ONCE
global_ns = None

def __init__(self, *args):
parser_test_case.parser_test_case_t.__init__(self, *args)
self.header = 'has_public_binary_operator_traits.hpp'
self.global_ns = None

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

def test_yes_equal(self):
yes_ns = self.global_ns.namespace('yesequal')
for typedef in yes_ns.typedefs():
self.assertTrue(
declarations.has_public_equal(typedef),
"Class '%s' should have public operator==" %
typedef.decl_string)

def test_no_equal(self):
no_ns = self.global_ns.namespace('noequal')
for typedef in no_ns.typedefs():
self.assertTrue(
not declarations.has_public_equal(typedef),
"Class '%s' should not have public operator==" %
typedef.decl_string)
@pytest.fixture
def global_ns():
COMPILATION_MODE = parser.COMPILATION_MODE.ALL_AT_ONCE
config = autoconfig.cxx_parsers_cfg.config.clone()
decls = parser.parse(TEST_FILES, config, COMPILATION_MODE)
global_ns = declarations.get_global_namespace(decls)
global_ns.init_optimizer()
return global_ns

def test_yes_less(self):
yes_ns = self.global_ns.namespace('yesless')
for typedef in yes_ns.typedefs():
self.assertTrue(
declarations.has_public_less(typedef),
"Class '%s' should have public operator<" %
typedef.decl_string)

def test_no_less(self):
no_ns = self.global_ns.namespace('noless')
for typedef in no_ns.typedefs():
self.assertTrue(
not declarations.has_public_less(typedef),
"Class '%s' should not have public operator<" %
typedef.decl_string)
def test_yes_equal(global_ns):
yes_ns = global_ns.namespace('yesequal')
for typedef in yes_ns.typedefs():
assert declarations.has_public_equal(typedef) is True


def create_suite():
suite = unittest.TestSuite()
suite.addTest(
unittest.TestLoader().loadTestsFromTestCase(testCaseClass=Test))
return suite
def test_no_equal(global_ns):
no_ns = global_ns.namespace('noequal')
for typedef in no_ns.typedefs():
assert declarations.has_public_equal(typedef) is False


def run_suite():
unittest.TextTestRunner(verbosity=2).run(create_suite())
def test_yes_less(global_ns):
yes_ns = global_ns.namespace('yesless')
for typedef in yes_ns.typedefs():
assert declarations.has_public_less(typedef)


if __name__ == "__main__":
run_suite()
def test_no_less(global_ns):
no_ns = global_ns.namespace('noless')
for typedef in no_ns.typedefs():
assert declarations.has_public_less(typedef) is False
98 changes: 38 additions & 60 deletions tests/test_hash.py
Original file line number Diff line number Diff line change
@@ -3,69 +3,62 @@
# Distributed under the Boost Software License, Version 1.0.
# See http://www.boost.org/LICENSE_1_0.txt

import sys
import unittest
import inspect

from pygccxml import declarations


class Test(unittest.TestCase):
def test_types_hashes():
"""
Test if all the type_t instances implement a hash method.

def test_types_hashes(self):
"""
Test if all the type_t instances implement a hash method.
The hash is part of the public API, as there are multiple tools
that rely on it to compare type_t instances.

The hash is part of the public API, as there are multiple tools
that rely on it to compare type_t instances.
The best way to test this is to instanciate dummy type_t objects
for each class that subclasses type_t, and check that the hash of
these objects is not None.

The best way to test this is to instanciate dummy type_t objects
for each class that subclasses type_t, and check that the hash of
these objects is not None.
"""

"""
members = inspect.getmembers(declarations, inspect.isclass)
for member in members:
member_type = member[1]
is_type_t_subclass = issubclass(member_type, declarations.type_t)
is_not_type_t = member_type != declarations.type_t
if is_type_t_subclass and is_not_type_t:
type_mockup = _create_type_t_mockup(member_type)
assert hash(type_mockup) is not None

if sys.version_info[:2] == (2, 7):
# _create_type_t_mockup calls inspect.signature, which does not
# exist for legacy Python
return

members = inspect.getmembers(declarations, inspect.isclass)
for member in members:
member_type = member[1]
is_type_t_subclass = issubclass(member_type, declarations.type_t)
is_not_type_t = member_type != declarations.type_t
if is_type_t_subclass and is_not_type_t:
type_mockup = _create_type_t_mockup(member_type)
self.assertIsNotNone(hash(type_mockup))
def test_declarations_hashes():
"""
Test if all the declaration_t instances implement a hash method.

def test_declarations_hashes(self):
"""
Test if all the declaration_t instances implement a hash method.
The hash is part of the public API, as there are multiple tools
that rely on it to compare declaration_t instances.

The hash is part of the public API, as there are multiple tools
that rely on it to compare declaration_t instances.
The best way to test this is to instanciate dummy declaration_t objects
for each class that subclasses declaration_t, and check that the hash
of these objects is not None.

The best way to test this is to instanciate dummy declaration_t objects
for each class that subclasses declaration_t, and check that the hash
of these objects is not None.
"""
members = inspect.getmembers(declarations, inspect.isclass)
for member in members:
member_type = member[1]
if issubclass(member_type, declarations.declaration_t):
assert hash(member_type()) is not None

"""
members = inspect.getmembers(declarations, inspect.isclass)
for member in members:
member_type = member[1]
if issubclass(member_type, declarations.declaration_t):
self.assertIsNotNone(hash(member_type()))

def test_type_qualifiers_t_hash(self):
"""
Test if the type_qualifiers_t instance implements a hash method.
def test_type_qualifiers_t_hash():
"""
Test if the type_qualifiers_t instance implements a hash method.

The hash is part of the public API, as there are multiple tools
that rely on it to compare type_qualifiers_t instances.
The hash is part of the public API, as there are multiple tools
that rely on it to compare type_qualifiers_t instances.

"""
self.assertIsNotNone(hash(declarations.type_qualifiers_t()))
"""
assert hash(declarations.type_qualifiers_t()) is not None


def _create_type_t_mockup(member_type):
@@ -91,18 +84,3 @@ def __init__(self):

def build_decl_string(self, with_defaults=False):
return self._decl_string


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()
Loading
Loading