test_opaque_types.py
Go to the documentation of this file.
1 import pytest
2 
3 from pybind11_tests import ConstructorStats, UserType
4 from pybind11_tests import opaque_types as m
5 
6 
8  lst = m.StringList()
9  lst.push_back("Element 1")
10  lst.push_back("Element 2")
11  assert m.print_opaque_list(lst) == "Opaque list: [Element 1, Element 2]"
12  assert lst.back() == "Element 2"
13 
14  for i, k in enumerate(lst, start=1):
15  assert k == f"Element {i}"
16  lst.pop_back()
17  assert m.print_opaque_list(lst) == "Opaque list: [Element 1]"
18 
19  cvp = m.ClassWithSTLVecProperty()
20  assert m.print_opaque_list(cvp.stringList) == "Opaque list: []"
21 
22  cvp.stringList = lst
23  cvp.stringList.push_back("Element 3")
24  assert m.print_opaque_list(cvp.stringList) == "Opaque list: [Element 1, Element 3]"
25 
26 
27 def test_pointers(msg):
28  living_before = ConstructorStats.get(UserType).alive()
29  assert m.get_void_ptr_value(m.return_void_ptr()) == 0x1234
30  assert m.get_void_ptr_value(UserType()) # Should also work for other C++ types
31  assert ConstructorStats.get(UserType).alive() == living_before
32 
33  with pytest.raises(TypeError) as excinfo:
34  m.get_void_ptr_value([1, 2, 3]) # This should not work
35  assert (
36  msg(excinfo.value)
37  == """
38  get_void_ptr_value(): incompatible function arguments. The following argument types are supported:
39  1. (arg0: capsule) -> int
40 
41  Invoked with: [1, 2, 3]
42  """
43  )
44 
45  assert m.return_null_str() is None
46  assert m.get_null_str_value(m.return_null_str()) is not None
47 
48  ptr = m.return_unique_ptr()
49  assert "StringList" in repr(ptr)
50  assert m.print_opaque_list(ptr) == "Opaque list: [some value]"
51 
52 
54  int_float_union = m.IntFloat()
55  int_float_union.i = 42
56  assert int_float_union.i == 42
57  int_float_union.f = 3.0
58  assert int_float_union.f == 3.0
test_opaque_types.test_pointers
def test_pointers(msg)
Definition: test_opaque_types.py:27
test_opaque_types.test_string_list
def test_string_list()
Definition: test_opaque_types.py:7
ConstructorStats::get
static ConstructorStats & get(std::type_index type)
Definition: constructor_stats.h:163
test_opaque_types.test_unions
def test_unions()
Definition: test_opaque_types.py:53
pybind11.msg
msg
Definition: wrap/pybind11/pybind11/__init__.py:4
repr
str repr(handle h)
Definition: pytypes.h:2420


gtsam
Author(s):
autogenerated on Tue Jun 25 2024 03:05:29