blob: d1f537d1d65cb821c960175f199164740f1d1686 [file] [log] [blame]
Dean Moldovan382db5b2016-08-13 00:37:50 +02001import pytest
2
3
4def test_inheritance(msg):
Dean Moldovan08cbe8d2017-02-15 21:10:25 +01005 from pybind11_tests import Pet, Dog, Rabbit, Hamster, Chimera, dog_bark, pet_name_species
Dean Moldovan382db5b2016-08-13 00:37:50 +02006
7 roger = Rabbit('Rabbit')
8 assert roger.name() + " is a " + roger.species() == "Rabbit is a parrot"
9 assert pet_name_species(roger) == "Rabbit is a parrot"
10
11 polly = Pet('Polly', 'parrot')
12 assert polly.name() + " is a " + polly.species() == "Polly is a parrot"
13 assert pet_name_species(polly) == "Polly is a parrot"
14
15 molly = Dog('Molly')
16 assert molly.name() + " is a " + molly.species() == "Molly is a dog"
17 assert pet_name_species(molly) == "Molly is a dog"
18
Jason Rhinelander6b52c832016-09-06 12:27:00 -040019 fred = Hamster('Fred')
20 assert fred.name() + " is a " + fred.species() == "Fred is a rodent"
21
Dean Moldovan382db5b2016-08-13 00:37:50 +020022 assert dog_bark(molly) == "Woof!"
23
24 with pytest.raises(TypeError) as excinfo:
25 dog_bark(polly)
26 assert msg(excinfo.value) == """
Wenzel Jakobe99ebae2016-09-12 11:44:37 +090027 dog_bark(): incompatible function arguments. The following argument types are supported:
Dean Moldovan382db5b2016-08-13 00:37:50 +020028 1. (arg0: m.Dog) -> str
Wenzel Jakobe99ebae2016-09-12 11:44:37 +090029
30 Invoked with: <m.Pet object at 0>
Dean Moldovan382db5b2016-08-13 00:37:50 +020031 """
Dean Moldovana0c1ccf2016-08-12 13:50:00 +020032
Dean Moldovan08cbe8d2017-02-15 21:10:25 +010033 with pytest.raises(TypeError) as excinfo:
34 Chimera("lion", "goat")
35 assert "No constructor defined!" in str(excinfo.value)
36
Dean Moldovana0c1ccf2016-08-12 13:50:00 +020037
38def test_automatic_upcasting():
Jason Rhinelander0e489772016-09-11 18:41:28 -040039 from pybind11_tests import return_class_1, return_class_2, return_class_n, return_none
Dean Moldovana0c1ccf2016-08-12 13:50:00 +020040
41 assert type(return_class_1()).__name__ == "DerivedClass1"
42 assert type(return_class_2()).__name__ == "DerivedClass2"
43 assert type(return_none()).__name__ == "NoneType"
Pim Schellartcc88aae2017-01-31 10:52:11 -050044 # Repeat these a few times in a random order to ensure no invalid caching
45 # is applied
Jason Rhinelander0e489772016-09-11 18:41:28 -040046 assert type(return_class_n(1)).__name__ == "DerivedClass1"
47 assert type(return_class_n(2)).__name__ == "DerivedClass2"
48 assert type(return_class_n(0)).__name__ == "BaseClass"
49 assert type(return_class_n(2)).__name__ == "DerivedClass2"
50 assert type(return_class_n(2)).__name__ == "DerivedClass2"
51 assert type(return_class_n(0)).__name__ == "BaseClass"
52 assert type(return_class_n(1)).__name__ == "DerivedClass1"
Dean Moldovanb4498ef2016-10-23 14:50:08 +020053
54
55def test_isinstance():
56 from pybind11_tests import test_isinstance, Pet, Dog
57
58 objects = [tuple(), dict(), Pet("Polly", "parrot")] + [Dog("Molly")] * 4
59 expected = (True, True, True, True, True, False, False)
60 assert test_isinstance(objects) == expected
Pim Schellartcc88aae2017-01-31 10:52:11 -050061
62
63def test_holder():
64 from pybind11_tests import test_mismatched_holder_type_1, test_mismatched_holder_type_2
65
66 with pytest.raises(RuntimeError) as excinfo:
67 test_mismatched_holder_type_1()
68
69 assert str(excinfo.value) == ("generic_type: type \"MismatchDerived1\" does not have "
70 "a non-default holder type while its base "
71 "\"MismatchBase1\" does")
72
73 with pytest.raises(RuntimeError) as excinfo:
74 test_mismatched_holder_type_2()
75
76 assert str(excinfo.value) == ("generic_type: type \"MismatchDerived2\" has a "
77 "non-default holder type while its base "
78 "\"MismatchBase2\" does not")