2 Unit test for Pybind wrap program 3 Author: Matthew Sklar, Varun Agrawal 15 sys.path.append(osp.dirname(osp.dirname(osp.abspath(__file__))))
17 osp.normpath(osp.abspath(osp.join(__file__,
'../../../build/wrap'))))
23 sys.path.append(osp.dirname(osp.dirname(osp.abspath(__file__))))
27 """Tests for Python wrapper based on Pybind11.""" 28 TEST_DIR = osp.dirname(osp.realpath(__file__))
29 INTERFACE_DIR = osp.join(TEST_DIR,
'fixtures')
30 PYTHON_TEST_DIR = osp.join(TEST_DIR,
'expected',
'python')
31 PYTHON_ACTUAL_DIR = osp.join(TEST_DIR,
"actual",
"python")
34 os.makedirs(PYTHON_ACTUAL_DIR, exist_ok=
True)
38 Common function to wrap content. 40 module = parser.Module.parseString(content)
42 instantiator.instantiate_namespace_inplace(module)
45 "pybind_wrapper.tpl"))
as template_file:
46 module_template = template_file.read()
50 module_name=module_name,
52 top_module_namespaces=[
''],
54 module_template=module_template)
56 cc_content = wrapper.wrap()
58 output = osp.join(self.
TEST_DIR, output_dir, module_name +
".cpp")
60 if not osp.exists(osp.join(self.
TEST_DIR, output_dir)):
61 os.mkdir(osp.join(self.
TEST_DIR, output_dir))
63 with open(output,
'w')
as f:
70 Compute the comparison between the expected and actual file, 71 and assert if diff is zero. 74 success = filecmp.cmp(actual, expected)
77 os.system(
"diff {} {}".format(actual, expected))
78 self.assertTrue(success,
"Mismatch for file {0}".format(file))
82 Check generation of python geometry wrapper. 83 python3 ../pybind_wrapper.py --src geometry.h --module_name 84 geometry_py --out output/geometry_py.cc 86 with open(osp.join(self.
INTERFACE_DIR,
'geometry.i'),
'r') as f: 95 """Test interface file with function info.""" 96 with open(osp.join(self.
INTERFACE_DIR,
'functions.i'),
'r') as f: 105 """Test interface file with only class info.""" 106 with open(osp.join(self.
INTERFACE_DIR,
'class.i'),
'r') as f: 114 """Test interface file with class inheritance definitions.""" 115 with open(osp.join(self.
INTERFACE_DIR,
'inheritance.i'),
'r') as f: 125 Check generation of python wrapper for full namespace definition. 126 python3 ../pybind_wrapper.py --src namespaces.i --module_name 127 namespaces_py --out output/namespaces_py.cpp 129 with open(osp.join(self.
INTERFACE_DIR,
'namespaces.i'),
'r') as f: 139 Tests for operator overloading. 141 with open(osp.join(self.
INTERFACE_DIR,
'operator.i'),
'r') as f: 151 Tests for some unique, non-trivial features. 153 with open(osp.join(self.
INTERFACE_DIR,
'special_cases.i'),
'r') as f: 163 Test if enum generation is correct. 173 if __name__ ==
'__main__':
def wrap_content(self, content, module_name, output_dir)
def test_special_cases(self)
def test_operator_overload(self)
def test_inheritance(self)
def test_namespaces(self)
def compare_and_diff(self, file, actual)