test_pybind_wrapper.py
Go to the documentation of this file.
1 """
2 Unit test for Pybind wrap program
3 Author: Matthew Sklar, Varun Agrawal
4 Date: February 2019
5 """
6 
7 # pylint: disable=import-error, wrong-import-position, too-many-branches
8 
9 import filecmp
10 import os
11 import os.path as osp
12 import sys
13 import unittest
14 
15 sys.path.append(osp.dirname(osp.dirname(osp.abspath(__file__))))
16 sys.path.append(
17  osp.normpath(osp.abspath(osp.join(__file__, '../../../build/wrap'))))
18 
19 import gtwrap.interface_parser as parser
20 import gtwrap.template_instantiator as instantiator
21 from gtwrap.pybind_wrapper import PybindWrapper
22 
23 sys.path.append(osp.dirname(osp.dirname(osp.abspath(__file__))))
24 
25 
26 class TestWrap(unittest.TestCase):
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")
32 
33  # Create the `actual/python` directory
34  os.makedirs(PYTHON_ACTUAL_DIR, exist_ok=True)
35 
36  def wrap_content(self, content, module_name, output_dir):
37  """
38  Common function to wrap content.
39  """
40  module = parser.Module.parseString(content)
41 
42  instantiator.instantiate_namespace_inplace(module)
43 
44  with open(osp.join(self.TEST_DIR,
45  "pybind_wrapper.tpl")) as template_file:
46  module_template = template_file.read()
47 
48  # Create Pybind wrapper instance
49  wrapper = PybindWrapper(module=module,
50  module_name=module_name,
51  use_boost=False,
52  top_module_namespaces=[''],
53  ignore_classes=[''],
54  module_template=module_template)
55 
56  cc_content = wrapper.wrap()
57 
58  output = osp.join(self.TEST_DIR, output_dir, module_name + ".cpp")
59 
60  if not osp.exists(osp.join(self.TEST_DIR, output_dir)):
61  os.mkdir(osp.join(self.TEST_DIR, output_dir))
62 
63  with open(output, 'w') as f:
64  f.write(cc_content)
65 
66  return output
67 
68  def compare_and_diff(self, file, actual):
69  """
70  Compute the comparison between the expected and actual file,
71  and assert if diff is zero.
72  """
73  expected = osp.join(self.PYTHON_TEST_DIR, file)
74  success = filecmp.cmp(actual, expected)
75 
76  if not success:
77  os.system("diff {} {}".format(actual, expected))
78  self.assertTrue(success, "Mismatch for file {0}".format(file))
79 
80  def test_geometry(self):
81  """
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
85  """
86  with open(osp.join(self.INTERFACE_DIR, 'geometry.i'), 'r') as f:
87  content = f.read()
88 
89  output = self.wrap_content(content, 'geometry_py',
90  self.PYTHON_ACTUAL_DIR)
91 
92  self.compare_and_diff('geometry_pybind.cpp', output)
93 
94  def test_functions(self):
95  """Test interface file with function info."""
96  with open(osp.join(self.INTERFACE_DIR, 'functions.i'), 'r') as f:
97  content = f.read()
98 
99  output = self.wrap_content(content, 'functions_py',
100  self.PYTHON_ACTUAL_DIR)
101 
102  self.compare_and_diff('functions_pybind.cpp', output)
103 
104  def test_class(self):
105  """Test interface file with only class info."""
106  with open(osp.join(self.INTERFACE_DIR, 'class.i'), 'r') as f:
107  content = f.read()
108 
109  output = self.wrap_content(content, 'class_py', self.PYTHON_ACTUAL_DIR)
110 
111  self.compare_and_diff('class_pybind.cpp', output)
112 
113  def test_inheritance(self):
114  """Test interface file with class inheritance definitions."""
115  with open(osp.join(self.INTERFACE_DIR, 'inheritance.i'), 'r') as f:
116  content = f.read()
117 
118  output = self.wrap_content(content, 'inheritance_py',
119  self.PYTHON_ACTUAL_DIR)
120 
121  self.compare_and_diff('inheritance_pybind.cpp', output)
122 
123  def test_namespaces(self):
124  """
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
128  """
129  with open(osp.join(self.INTERFACE_DIR, 'namespaces.i'), 'r') as f:
130  content = f.read()
131 
132  output = self.wrap_content(content, 'namespaces_py',
133  self.PYTHON_ACTUAL_DIR)
134 
135  self.compare_and_diff('namespaces_pybind.cpp', output)
136 
138  """
139  Tests for operator overloading.
140  """
141  with open(osp.join(self.INTERFACE_DIR, 'operator.i'), 'r') as f:
142  content = f.read()
143 
144  output = self.wrap_content(content, 'operator_py',
145  self.PYTHON_ACTUAL_DIR)
146 
147  self.compare_and_diff('operator_pybind.cpp', output)
148 
150  """
151  Tests for some unique, non-trivial features.
152  """
153  with open(osp.join(self.INTERFACE_DIR, 'special_cases.i'), 'r') as f:
154  content = f.read()
155 
156  output = self.wrap_content(content, 'special_cases_py',
157  self.PYTHON_ACTUAL_DIR)
158 
159  self.compare_and_diff('special_cases_pybind.cpp', output)
160 
161  def test_enum(self):
162  """
163  Test if enum generation is correct.
164  """
165  with open(osp.join(self.INTERFACE_DIR, 'enum.i'), 'r') as f:
166  content = f.read()
167 
168  output = self.wrap_content(content, 'enum_py',
169  self.PYTHON_ACTUAL_DIR)
170 
171  self.compare_and_diff('enum_pybind.cpp', output)
172 
173 if __name__ == '__main__':
174  unittest.main()
def wrap_content(self, content, module_name, output_dir)
def compare_and_diff(self, file, actual)


gtsam
Author(s):
autogenerated on Sat May 8 2021 02:46:04