blob: 17c00c8bca33c45eaaadacdbb2add6ce9652a53a [file] [log] [blame]
Dean Moldovana0c1ccf2016-08-12 13:50:00 +02001
Dean Moldovan665e8802016-08-12 22:28:31 +02002def test_nested_modules():
Dean Moldovana0c1ccf2016-08-12 13:50:00 +02003 import pybind11_tests
4 from pybind11_tests.submodule import submodule_func
5
6 assert pybind11_tests.__name__ == "pybind11_tests"
7 assert pybind11_tests.submodule.__name__ == "pybind11_tests.submodule"
8
Dean Moldovan665e8802016-08-12 22:28:31 +02009 assert submodule_func() == "submodule_func()"
Dean Moldovana0c1ccf2016-08-12 13:50:00 +020010
11
12def test_reference_internal():
13 from pybind11_tests import ConstructorStats
14 from pybind11_tests.submodule import A, B
15
16 b = B()
17 assert str(b.get_a1()) == "A[1]"
18 assert str(b.a1) == "A[1]"
19 assert str(b.get_a2()) == "A[2]"
20 assert str(b.a2) == "A[2]"
21
22 b.a1 = A(42)
23 b.a2 = A(43)
24 assert str(b.get_a1()) == "A[42]"
25 assert str(b.a1) == "A[42]"
26 assert str(b.get_a2()) == "A[43]"
27 assert str(b.a2) == "A[43]"
28
29 astats, bstats = ConstructorStats.get(A), ConstructorStats.get(B)
30 assert astats.alive() == 2
31 assert bstats.alive() == 1
32 del b
33 assert astats.alive() == 0
34 assert bstats.alive() == 0
35 assert astats.values() == ['1', '2', '42', '43']
36 assert bstats.values() == []
37 assert astats.default_constructions == 0
38 assert bstats.default_constructions == 1
39 assert astats.copy_constructions == 0
40 assert bstats.copy_constructions == 0
41 # assert astats.move_constructions >= 0 # Don't invoke any
42 # assert bstats.move_constructions >= 0 # Don't invoke any
43 assert astats.copy_assignments == 2
44 assert bstats.copy_assignments == 0
45 assert astats.move_assignments == 0
46 assert bstats.move_assignments == 0
47
48
49def test_importing():
50 from pybind11_tests import OD
51 from collections import OrderedDict
52
53 assert OD is OrderedDict
54 assert str(OD([(1, 'a'), (2, 'b')])) == "OrderedDict([(1, 'a'), (2, 'b')])"
Dean Moldovan1769ea42017-03-15 15:38:14 +010055
56
57def test_pydoc():
58 """Pydoc needs to be able to provide help() for everything inside a pybind11 module"""
59 import pybind11_tests
60 import pydoc
61
Dean Moldovan83e328f2017-06-09 00:44:49 +020062 assert pybind11_tests.__name__ == "pybind11_tests"
Dean Moldovan443ab592017-04-24 01:51:44 +020063 assert pybind11_tests.__doc__ == "pybind11 test module"
Dean Moldovan1769ea42017-03-15 15:38:14 +010064 assert pydoc.text.docmodule(pybind11_tests)
Dean Moldovanbdfb50f2017-06-07 16:52:50 +020065
66
67def test_duplicate_registration():
68 """Registering two things with the same name"""
69 from pybind11_tests import duplicate_registration
70
71 assert duplicate_registration() == []