blob: e4ab202654de01ae1ec5c4b1ab43af19f9466559 [file] [log] [blame]
Dean Moldovan382db5b2016-08-13 00:37:50 +02001import pytest
2
3
4def test_inheritance(msg):
Jason Rhinelander6b52c832016-09-06 12:27:00 -04005 from pybind11_tests import Pet, Dog, Rabbit, Hamster, 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
33
34def test_automatic_upcasting():
Jason Rhinelander0e489772016-09-11 18:41:28 -040035 from pybind11_tests import return_class_1, return_class_2, return_class_n, return_none
Dean Moldovana0c1ccf2016-08-12 13:50:00 +020036
37 assert type(return_class_1()).__name__ == "DerivedClass1"
38 assert type(return_class_2()).__name__ == "DerivedClass2"
39 assert type(return_none()).__name__ == "NoneType"
Pim Schellartcc88aae2017-01-31 10:52:11 -050040 # Repeat these a few times in a random order to ensure no invalid caching
41 # is applied
Jason Rhinelander0e489772016-09-11 18:41:28 -040042 assert type(return_class_n(1)).__name__ == "DerivedClass1"
43 assert type(return_class_n(2)).__name__ == "DerivedClass2"
44 assert type(return_class_n(0)).__name__ == "BaseClass"
45 assert type(return_class_n(2)).__name__ == "DerivedClass2"
46 assert type(return_class_n(2)).__name__ == "DerivedClass2"
47 assert type(return_class_n(0)).__name__ == "BaseClass"
48 assert type(return_class_n(1)).__name__ == "DerivedClass1"
Dean Moldovanb4498ef2016-10-23 14:50:08 +020049
50
51def test_isinstance():
52 from pybind11_tests import test_isinstance, Pet, Dog
53
54 objects = [tuple(), dict(), Pet("Polly", "parrot")] + [Dog("Molly")] * 4
55 expected = (True, True, True, True, True, False, False)
56 assert test_isinstance(objects) == expected
Pim Schellartcc88aae2017-01-31 10:52:11 -050057
58
59def test_holder():
60 from pybind11_tests import test_mismatched_holder_type_1, test_mismatched_holder_type_2
61
62 with pytest.raises(RuntimeError) as excinfo:
63 test_mismatched_holder_type_1()
64
65 assert str(excinfo.value) == ("generic_type: type \"MismatchDerived1\" does not have "
66 "a non-default holder type while its base "
67 "\"MismatchBase1\" does")
68
69 with pytest.raises(RuntimeError) as excinfo:
70 test_mismatched_holder_type_2()
71
72 assert str(excinfo.value) == ("generic_type: type \"MismatchDerived2\" has a "
73 "non-default holder type while its base "
74 "\"MismatchBase2\" does not")