cppyabm  1.0.17
An agent-based library to integrate C++ and Python
test_modules.py
Go to the documentation of this file.
1 # -*- coding: utf-8 -*-
2 from pybind11_tests import modules as m
3 from pybind11_tests.modules import subsubmodule as ms
4 from pybind11_tests import ConstructorStats
5 
6 
8  import pybind11_tests
9 
10  assert pybind11_tests.__name__ == "pybind11_tests"
11  assert pybind11_tests.modules.__name__ == "pybind11_tests.modules"
12  assert (
13  pybind11_tests.modules.subsubmodule.__name__
14  == "pybind11_tests.modules.subsubmodule"
15  )
16  assert m.__name__ == "pybind11_tests.modules"
17  assert ms.__name__ == "pybind11_tests.modules.subsubmodule"
18 
19  assert ms.submodule_func() == "submodule_func()"
20 
21 
23  b = ms.B()
24  assert str(b.get_a1()) == "A[1]"
25  assert str(b.a1) == "A[1]"
26  assert str(b.get_a2()) == "A[2]"
27  assert str(b.a2) == "A[2]"
28 
29  b.a1 = ms.A(42)
30  b.a2 = ms.A(43)
31  assert str(b.get_a1()) == "A[42]"
32  assert str(b.a1) == "A[42]"
33  assert str(b.get_a2()) == "A[43]"
34  assert str(b.a2) == "A[43]"
35 
36  astats, bstats = ConstructorStats.get(ms.A), ConstructorStats.get(ms.B)
37  assert astats.alive() == 2
38  assert bstats.alive() == 1
39  del b
40  assert astats.alive() == 0
41  assert bstats.alive() == 0
42  assert astats.values() == ["1", "2", "42", "43"]
43  assert bstats.values() == []
44  assert astats.default_constructions == 0
45  assert bstats.default_constructions == 1
46  assert astats.copy_constructions == 0
47  assert bstats.copy_constructions == 0
48  # assert astats.move_constructions >= 0 # Don't invoke any
49  # assert bstats.move_constructions >= 0 # Don't invoke any
50  assert astats.copy_assignments == 2
51  assert bstats.copy_assignments == 0
52  assert astats.move_assignments == 0
53  assert bstats.move_assignments == 0
54 
55 
57  from pybind11_tests.modules import OD
58  from collections import OrderedDict
59 
60  assert OD is OrderedDict
61  assert str(OD([(1, "a"), (2, "b")])) == "OrderedDict([(1, 'a'), (2, 'b')])"
62 
63 
64 def test_pydoc():
65  """Pydoc needs to be able to provide help() for everything inside a pybind11 module"""
66  import pybind11_tests
67  import pydoc
68 
69  assert pybind11_tests.__name__ == "pybind11_tests"
70  assert pybind11_tests.__doc__ == "pybind11 test module"
71  assert pydoc.text.docmodule(pybind11_tests)
72 
73 
75  """Registering two things with the same name"""
76 
77  assert m.duplicate_registration() == []
78 
79 
81  """Test that all the keys in the builtin modules have type str.
82 
83  Previous versions of pybind11 would add a unicode key in python 2.
84  """
85  if hasattr(__builtins__, "keys"):
86  keys = __builtins__.keys()
87  else: # this is to make pypy happy since builtins is different there.
88  keys = __builtins__.__dict__.keys()
89 
90  assert {type(k) for k in keys} == {str}
hasattr
bool hasattr(handle obj, handle name)
Definition: pytypes.h:405
type
Definition: pytypes.h:915
test_modules.test_reference_internal
def test_reference_internal()
Definition: test_modules.py:22
ConstructorStats::get
static ConstructorStats & get(std::type_index type)
Definition: constructor_stats.h:154
test_modules.test_importing
def test_importing()
Definition: test_modules.py:56
test_modules.test_duplicate_registration
def test_duplicate_registration()
Definition: test_modules.py:74
test_modules.test_nested_modules
def test_nested_modules()
Definition: test_modules.py:7
str
Definition: pytypes.h:946
test_modules.test_builtin_key_type
def test_builtin_key_type()
Definition: test_modules.py:80
test_modules.test_pydoc
def test_pydoc()
Definition: test_modules.py:64