Skip to content

tests: refactor / simplify #204

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 23, 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
143 changes: 54 additions & 89 deletions tests/test_attributes.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,98 +3,63 @@
# 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 = [
# TODO: once gccxml is removed; rename this to something like
# annotate_tester
"attributes_castxml.hpp",
]

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)
# TODO: once gccxml is removed; rename this to something like
# annotate_tester
self.header = "attributes_" + self.config.xml_generator + ".hpp"

def setUp(self):
# Reset flags before each test
self.config.flags = ""

def test_attributes(self):

decls = parser.parse([self.header], self.config)
Test.global_ns = declarations.get_global_namespace(decls)
Test.global_ns.init_optimizer()

numeric = self.global_ns.class_('numeric_t')
do_nothing = numeric.member_function('do_nothing')
arg = do_nothing.arguments[0]

generator = self.config.xml_generator_from_xml_file
if generator.is_castxml1 or \
(generator.is_castxml and
float(generator.xml_output_version) >= 1.137):
# This works since:
# https://github.com/CastXML/CastXML/issues/25
# https://github.com/CastXML/CastXML/pull/26
# https://github.com/CastXML/CastXML/pull/27
# The version bump to 1.137 came way later but this is the
# only way to make sure the test is running correctly
self.assertTrue("annotate(sealed)" == numeric.attributes)
self.assertTrue("annotate(no throw)" == do_nothing.attributes)
self.assertTrue("annotate(out)" == arg.attributes)
self.assertTrue(
numeric.member_operators(name="=")[0].attributes is None)
else:
self.assertTrue("gccxml(no throw)" == do_nothing.attributes)
self.assertTrue("gccxml(out)" == arg.attributes)

def test_attributes_thiscall(self):
"""
Test attributes with the "f2" flag

"""
if self.config.compiler != "msvc":
return

self.config.flags = ["f2"]

decls = parser.parse([self.header], self.config)
Test.global_ns = declarations.get_global_namespace(decls)
Test.global_ns.init_optimizer()

numeric = self.global_ns.class_('numeric_t')
do_nothing = numeric.member_function('do_nothing')
arg = do_nothing.arguments[0]

generator = self.config.xml_generator_from_xml_file
if generator.is_castxml1 or (
generator.is_castxml and
float(generator.xml_output_version) >= 1.137):
self.assertTrue("annotate(sealed)" == numeric.attributes)
self.assertTrue("annotate(out)" == arg.attributes)

self.assertTrue(
"__thiscall__ annotate(no throw)" == do_nothing.attributes)
self.assertTrue(
numeric.member_operators(name="=")[0].attributes ==
"__thiscall__")


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

@pytest.fixture
def global_ns():
COMPILATION_MODE = parser.COMPILATION_MODE.ALL_AT_ONCE
INIT_OPTIMIZER = True
config = autoconfig.cxx_parsers_cfg.config.clone()
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 test_attributes(global_ns):
numeric = global_ns.class_('numeric_t')
do_nothing = numeric.member_function('do_nothing')
arg = do_nothing.arguments[0]
assert "annotate(sealed)" == numeric.attributes
assert "annotate(no throw)" == do_nothing.attributes
assert "annotate(out)" == arg.attributes
assert numeric.member_operators(name="=")[0].attributes is None


def test_attributes_thiscall():
"""
Test attributes with the "f2" flag

"""

COMPILATION_MODE = parser.COMPILATION_MODE.ALL_AT_ONCE
config = autoconfig.cxx_parsers_cfg.config.clone()

config.flags = ["f2"]

decls = parser.parse(TEST_FILES, config, COMPILATION_MODE)
global_ns = declarations.get_global_namespace(decls)
global_ns.init_optimizer()

numeric = global_ns.class_('numeric_t')
do_nothing = numeric.member_function('do_nothing')
arg = do_nothing.arguments[0]

assert "annotate(sealed)" == numeric.attributes
assert "annotate(out)" == arg.attributes

assert "annotate(no throw)" == do_nothing.attributes
assert numeric.member_operators(name="=")[0].attributes is None
15 changes: 8 additions & 7 deletions tests/test_better_templates_matcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,28 +10,29 @@
from pygccxml import parser
from pygccxml import declarations

TEST_FILES = [
"better_templates_matcher_tester.hpp",
]


@pytest.fixture
def global_ns_better():
def global_ns():
COMPILATION_MODE = parser.COMPILATION_MODE.ALL_AT_ONCE
INIT_OPTIMIZER = True
config = autoconfig.cxx_parsers_cfg.config.clone()
decls = parser.parse(
['better_templates_matcher_tester.hpp'],
config, COMPILATION_MODE
)
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 test_better_templates_matcher(global_ns_better):
def test_better_templates_matcher(global_ns):
classes = [
"::Ogre::PlaneBoundedVolume",
"::Ogre::Plane",
"::Ogre::Singleton<Ogre::PCZoneFactoryManager>",
"::Ogre::PCZoneFactoryManager",
]
for i in classes:
global_ns_better.class_(i)
global_ns.class_(i)
60 changes: 22 additions & 38 deletions tests/test_bit_fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,52 +3,36 @@
# 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 = [
"bit_fields.hpp",
]

class Test(parser_test_case.parser_test_case_t):

global_ns = None
@pytest.fixture
def global_ns():
COMPILATION_MODE = parser.COMPILATION_MODE.ALL_AT_ONCE
INIT_OPTIMIZER = True
config = autoconfig.cxx_parsers_cfg.config.clone()
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 __init__(self, *args):
parser_test_case.parser_test_case_t.__init__(self, *args)
self.header = 'bit_fields.hpp'

def setUp(self):
if not Test.global_ns:
decls = parser.parse([self.header], self.config)
Test.global_ns = declarations.get_global_namespace(decls)
Test.global_ns.init_optimizer()
def test_bit_fields(global_ns):
bf_x = global_ns.variable('x')
assert bf_x.bits == 1

def test_bit_fields(self):
bf_x = self.global_ns.variable('x')
self.assertTrue(bf_x.bits == 1)
bf_y = global_ns.variable('y')
assert bf_y.bits == 7

bf_y = self.global_ns.variable('y')
self.assertTrue(bf_y.bits == 7)

mv_z = self.global_ns.variable('z')
self.assertTrue(mv_z.bits is None)

def test2(self):
pass


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()
mv_z = global_ns.variable('z')
assert mv_z.bits is None
67 changes: 22 additions & 45 deletions tests/test_cache_enums.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,61 +4,38 @@
# See http://www.boost.org/LICENSE_1_0.txt

import os
import unittest

from . import autoconfig
from . import parser_test_case

from pygccxml import parser
from pygccxml import declarations


class tester_impl_t(parser_test_case.parser_test_case_t):
COMPILATION_MODE = parser.COMPILATION_MODE.ALL_AT_ONCE
TEST_FILES = [
"declarations_enums.hpp",
]

def __init__(self, *args):
parser_test_case.parser_test_case_t.__init__(self, *args)
self.header = os.path.join(
autoconfig.data_directory,
'declarations_enums.hpp')
self.cache_file = os.path.join(
autoconfig.data_directory,
'pygccxml.cache')
if os.path.exists(self.cache_file) and os.path.isfile(self.cache_file):
os.remove(self.cache_file)

def test_cache(self):
cache = parser.file_cache_t(self.cache_file)
reader = parser.source_reader_t(self.config, cache)
decls1 = reader.read_file(self.header)
cache.flush()
cache = parser.file_cache_t(self.cache_file)
reader = parser.source_reader_t(self.config, cache)
decls2 = reader.read_file(self.header)
def test_cache():
cache_file = os.path.join(autoconfig.data_directory, 'pygccxml.cache')
if os.path.exists(cache_file) and os.path.isfile(cache_file):
os.remove(cache_file)

enum_matcher = declarations.declaration_matcher_t(
name="EColor",
decl_type=declarations.enumeration_t)
config = autoconfig.cxx_parsers_cfg.config.clone()

color1 = declarations.matcher.get_single(enum_matcher, decls1)
color2 = declarations.matcher.get_single(enum_matcher, decls2)
self.assertTrue(color1.values == color2.values)
cache = parser.file_cache_t(cache_file)
reader = parser.source_reader_t(config, cache)
decls1 = reader.read_file(TEST_FILES[0])
cache.flush()
cache = parser.file_cache_t(cache_file)
reader = parser.source_reader_t(config, cache)
decls2 = reader.read_file(TEST_FILES[0])

enum_matcher = declarations.declaration_matcher_t(
name="EColor",
decl_type=declarations.enumeration_t
)

class Test(tester_impl_t):
CXX_PARSER_CFG = autoconfig.cxx_parsers_cfg.config


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()
color1 = declarations.matcher.get_single(enum_matcher, decls1)
color2 = declarations.matcher.get_single(enum_matcher, decls2)
assert color1.values == color2.values
Loading
Loading