Skip to content

Commit

Permalink
Merge pull request #208 from CastXML/refactor4
Browse files Browse the repository at this point in the history
tests: refactor / simplify
  • Loading branch information
iMichka authored Nov 6, 2024
2 parents 877c4de + bcd6796 commit 461dab4
Show file tree
Hide file tree
Showing 11 changed files with 888 additions and 1,184 deletions.
851 changes: 391 additions & 460 deletions tests/test_type_traits.py

Large diffs are not rendered by default.

105 changes: 34 additions & 71 deletions tests/test_typedefs.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,81 +3,44 @@
# 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


class tester_src_t(parser_test_case.parser_test_case_t):
# tester source reader
def test_typedefs_src_reader():
COMPILATION_MODE = parser.COMPILATION_MODE.ALL_AT_ONCE

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

def setUp(self):
if not self.declarations:
self.declarations = parser.parse([self.header], self.config)
self.global_ns = declarations.find_declaration(
self.declarations,
decl_type=declarations.namespace_t,
name='::')
self.global_ns.init_optimizer()

def test(self):
item_cls = self.global_ns.class_(name='item_t')
self.assertTrue(item_cls, "unable to find class 'item_t'")
self.assertTrue(len(item_cls.aliases) == 1)
self.assertTrue(item_cls.aliases[0].name == 'Item')


class tester_prj_t(parser_test_case.parser_test_case_t):
# tester source reader
header = 'typedefs_base.hpp'
config = autoconfig.cxx_parsers_cfg.config.clone()
decls = parser.parse([header], config)
global_ns = declarations.find_declaration(
decls,
decl_type=declarations.namespace_t,
name='::')
global_ns.init_optimizer()

item_cls = global_ns.class_(name='item_t')
assert item_cls is not None
assert len(item_cls.aliases) == 1
assert item_cls.aliases[0].name == 'Item'


def test_typedefs_source_reader():
COMPILATION_MODE = parser.COMPILATION_MODE.FILE_BY_FILE

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

def setUp(self):
if not self.declarations:
self.declarations = parser.parse(
['typedefs1.hpp',
'typedefs2.hpp'],
self.config,
self.COMPILATION_MODE)

def test(self):
item_cls = declarations.find_declaration(
self.declarations,
decl_type=declarations.class_t,
name='item_t')
self.assertTrue(item_cls, "unable to find class 'item_t'")
self.assertTrue(len(item_cls.aliases) == 3)
expected_aliases = {'Item', 'Item1', 'Item2'}
real_aliases = set([typedef.name for typedef in item_cls.aliases])
self.assertTrue(real_aliases == expected_aliases)


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


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


if __name__ == "__main__":
run_suite()
config = autoconfig.cxx_parsers_cfg.config.clone()

decls = parser.parse(
['typedefs1.hpp', 'typedefs2.hpp'],
config,
COMPILATION_MODE
)
item_cls = declarations.find_declaration(
decls,
decl_type=declarations.class_t,
name='item_t')
assert item_cls is not None
assert len(item_cls.aliases) == 3
expected_aliases = {'Item', 'Item1', 'Item2'}
real_aliases = set([typedef.name for typedef in item_cls.aliases])
assert real_aliases == expected_aliases
171 changes: 81 additions & 90 deletions tests/test_unnamed_classes.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,100 +3,91 @@
# 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
from pygccxml.declarations import type_traits


class Test(parser_test_case.parser_test_case_t):

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

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

def validate_bitfields(self, parent, bitfields):
for key in bitfields:
var = parent.variable(key)
self.assertEqual(var.bits, bitfields[key])

def do_union_test(self, union_name, bitfields):
s2 = self.global_ns.class_('S2')
self.assertFalse(declarations.is_union(s2))
self.assertTrue(declarations.is_struct(s2))
self.assertEqual(s2.parent.name, 'S1')
self.assertFalse(declarations.is_union(s2.parent))

union = s2.variable(union_name)
self.assertTrue(declarations.is_union(union.decl_type))
self.assertFalse(declarations.is_struct(union.decl_type))

union_type = type_traits.remove_declarated(union.decl_type)
self.validate_bitfields(union_type, bitfields)
self.assertIsNotNone(union_type.variable('raw'))

def test_union_Flags(self):
flags_bitfields = {
'hasItemIdList': 1,
'pointsToFileOrDir': 1,
'hasDescription': 1,
'hasRelativePath': 1,
'hasWorkingDir': 1,
'hasCmdLineArgs': 1,
'hasCustomIcon': 1,
'useWorkingDir': 1,
'unused': 24,
}
self.do_union_test('flags', flags_bitfields)

def test_unnamed_unions(self):
fileattribs_bitfields = {
'isReadOnly': 1,
'isHidden': 1,
'isSystem': 1,
'isVolumeLabel': 1,
'isDir': 1,
'isModified': 1,
'isEncrypted': 1,
'isNormal': 1,
'isTemporary': 1,
'isSparse': 1,
'hasReparsePoint': 1,
'isCompressed': 1,
'isOffline': 1,
'unused': 19,
}
self.do_union_test('fileattribs', fileattribs_bitfields)

def test_anonymous_unions(self):
s3 = self.global_ns.class_('S3')
self.assertEqual(s3.parent.name, 'S1')

s3_vars = ['anon_mem_c', 'anon_mem_i', 's3_mem', 's2']
for var in s3_vars:
self.assertFalse(declarations.is_union(s3.variable(var).decl_type))


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 = [
"unnamed_classes.hpp",
]


@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 validate_bitfields(parent, bitfields):
for key in bitfields:
var = parent.variable(key)
assert var.bits == bitfields[key]


def do_union_test(global_ns, union_name, bitfields):
s2 = global_ns.class_('S2')
assert declarations.is_union(s2) is False
assert declarations.is_struct(s2) is True
assert s2.parent.name == 'S1'
assert declarations.is_union(s2.parent) is False

union = s2.variable(union_name)
assert declarations.is_union(union.decl_type) is True
assert declarations.is_struct(union.decl_type) is False

union_type = type_traits.remove_declarated(union.decl_type)
validate_bitfields(union_type, bitfields)
assert union_type.variable('raw') is not None


def test_union_Flags(global_ns):
flags_bitfields = {
'hasItemIdList': 1,
'pointsToFileOrDir': 1,
'hasDescription': 1,
'hasRelativePath': 1,
'hasWorkingDir': 1,
'hasCmdLineArgs': 1,
'hasCustomIcon': 1,
'useWorkingDir': 1,
'unused': 24,
}
do_union_test(global_ns, 'flags', flags_bitfields)


def test_unnamed_unions(global_ns):
fileattribs_bitfields = {
'isReadOnly': 1,
'isHidden': 1,
'isSystem': 1,
'isVolumeLabel': 1,
'isDir': 1,
'isModified': 1,
'isEncrypted': 1,
'isNormal': 1,
'isTemporary': 1,
'isSparse': 1,
'hasReparsePoint': 1,
'isCompressed': 1,
'isOffline': 1,
'unused': 19,
}
do_union_test(global_ns, 'fileattribs', fileattribs_bitfields)


def test_anonymous_unions(global_ns):
s3 = global_ns.class_('S3')
assert s3.parent.name == 'S1'

s3_vars = ['anon_mem_c', 'anon_mem_i', 's3_mem', 's2']
for var in s3_vars:
assert declarations.is_union(s3.variable(var).decl_type) is False
Loading

0 comments on commit 461dab4

Please sign in to comment.