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


gtsam
Author(s):
autogenerated on Thu Jul 4 2024 03:06:01