1from pybind11_tests import modules as m
2from pybind11_tests.modules import subsubmodule as ms
3from pybind11_tests import ConstructorStats
4
5
6def test_nested_modules():
7    import pybind11_tests
8    assert pybind11_tests.__name__ == "pybind11_tests"
9    assert pybind11_tests.modules.__name__ == "pybind11_tests.modules"
10    assert pybind11_tests.modules.subsubmodule.__name__ == "pybind11_tests.modules.subsubmodule"
11    assert m.__name__ == "pybind11_tests.modules"
12    assert ms.__name__ == "pybind11_tests.modules.subsubmodule"
13
14    assert ms.submodule_func() == "submodule_func()"
15
16
17def test_reference_internal():
18    b = ms.B()
19    assert str(b.get_a1()) == "A[1]"
20    assert str(b.a1) == "A[1]"
21    assert str(b.get_a2()) == "A[2]"
22    assert str(b.a2) == "A[2]"
23
24    b.a1 = ms.A(42)
25    b.a2 = ms.A(43)
26    assert str(b.get_a1()) == "A[42]"
27    assert str(b.a1) == "A[42]"
28    assert str(b.get_a2()) == "A[43]"
29    assert str(b.a2) == "A[43]"
30
31    astats, bstats = ConstructorStats.get(ms.A), ConstructorStats.get(ms.B)
32    assert astats.alive() == 2
33    assert bstats.alive() == 1
34    del b
35    assert astats.alive() == 0
36    assert bstats.alive() == 0
37    assert astats.values() == ['1', '2', '42', '43']
38    assert bstats.values() == []
39    assert astats.default_constructions == 0
40    assert bstats.default_constructions == 1
41    assert astats.copy_constructions == 0
42    assert bstats.copy_constructions == 0
43    # assert astats.move_constructions >= 0  # Don't invoke any
44    # assert bstats.move_constructions >= 0  # Don't invoke any
45    assert astats.copy_assignments == 2
46    assert bstats.copy_assignments == 0
47    assert astats.move_assignments == 0
48    assert bstats.move_assignments == 0
49
50
51def test_importing():
52    from pybind11_tests.modules import OD
53    from collections import OrderedDict
54
55    assert OD is OrderedDict
56    assert str(OD([(1, 'a'), (2, 'b')])) == "OrderedDict([(1, 'a'), (2, 'b')])"
57
58
59def test_pydoc():
60    """Pydoc needs to be able to provide help() for everything inside a pybind11 module"""
61    import pybind11_tests
62    import pydoc
63
64    assert pybind11_tests.__name__ == "pybind11_tests"
65    assert pybind11_tests.__doc__ == "pybind11 test module"
66    assert pydoc.text.docmodule(pybind11_tests)
67
68
69def test_duplicate_registration():
70    """Registering two things with the same name"""
71
72    assert m.duplicate_registration() == []
73