Skip to content

Commit

Permalink
Use parameterized tests for standard library includes
Browse files Browse the repository at this point in the history
  • Loading branch information
jbcoe committed Mar 31, 2024
1 parent 75d2112 commit f0c0822
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 38 deletions.
3 changes: 3 additions & 0 deletions mypy.ini
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,6 @@ warn_unused_configs = True

[mypy-clang.*]
ignore_missing_imports = True

[mypy-parameterized.*]
ignore_missing_imports = True
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
clang==14
mypy
parameterized
pre-commit
pytype
7 changes: 4 additions & 3 deletions test.macos.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@
# Virtual environment setup
python3 -m venv .venv # Create a Python virtual env
source ./.venv/bin/activate # Activate the virtual env for bash by source.
python3 -m pip install -r requirements.txt # Install latest requirements.

# Type checks
python -m mypy *.py --check-untyped-defs # Run mypy to check type hints
python3 -m mypy *.py --check-untyped-defs # Run mypy to check type hints

Unit tests
# Unit tests
PY_CPPMODEL_LIBCLANG_PATH=/Library/Developer/CommandLineTools/usr/lib/libclang.dylib \
python -m unittest discover --verbose . # Run tests
python3 -m unittest discover --verbose . # Run tests
72 changes: 72 additions & 0 deletions test_parse_standard_library_includes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import clang
import os
import py_cppmodel
import unittest
from parameterized import parameterized

from ctypes.util import find_library

LIBCLANG_PATH = os.environ.get("PY_CPPMODEL_LIBCLANG_PATH")
if not LIBCLANG_PATH:
raise RuntimeError("PY_CPPMODEL_LIBCLANG_PATH is unset")

clang.cindex.Config.set_library_file(LIBCLANG_PATH) # type: ignore

from clang.cindex import TranslationUnit

COMPILER_ARGS = [
"-x",
"c++",
"-std=c++20",
"-I/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/v1",
"-I/Library/Developer/CommandLineTools/usr/include",
]


def _custom_name_func(testcase_func, _, param):
return "%s_%s" % (testcase_func.__name__, parameterized.to_safe_name(param.args[0]))


class TestStandardLibraryIncludes(unittest.TestCase):
@parameterized.expand(
[
"algorithm",
"any",
"array",
"deque",
"forward_list",
"functional",
"iterator",
"list",
"map",
"memory",
"numeric",
"optional",
"queue",
"set",
"stack",
"string",
"tuple",
"type_traits",
"unordered_map",
"unordered_set",
"utility",
"variant",
"vector",
],
name_func=_custom_name_func,
)
def test_include(self, include):
source = f"#include <{include}>"
tu = TranslationUnit.from_source(
"t.cc",
COMPILER_ARGS,
unsaved_files=[("t.cc", source)],
)

# This should not raise an exception.
self.model = py_cppmodel.Model(tu)


if __name__ == "__main__":
unittest.main()
35 changes: 0 additions & 35 deletions test_py_cppmodel.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,40 +81,5 @@ def test_classes(self):
)


class TestStandardLibraryIncludes(unittest.TestCase):
def testVector(self):
source = "#include <vector>"
tu = TranslationUnit.from_source(
"t.cc",
COMPILER_ARGS,
unsaved_files=[("t.cc", source)],
)

# This should not raise an exception.
self.model = py_cppmodel.Model(tu)

def testMemory(self):
source = "#include <memory>"
tu = TranslationUnit.from_source(
"t.cc",
COMPILER_ARGS,
unsaved_files=[("t.cc", source)],
)

# This should not raise an exception.
self.model = py_cppmodel.Model(tu)

def testString(self):
source = "#include <string>"
tu = TranslationUnit.from_source(
"t.cc",
COMPILER_ARGS,
unsaved_files=[("t.cc", source)],
)

# This should not raise an exception.
self.model = py_cppmodel.Model(tu)


if __name__ == "__main__":
unittest.main()

0 comments on commit f0c0822

Please sign in to comment.