blob: d4cea82534177a83684737e52df189d472ac16e2 [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) == """
27 Incompatible function arguments. The following argument types are supported:
28 1. (arg0: m.Dog) -> str
29 Invoked with: <m.Pet object at 0>
30 """
Dean Moldovana0c1ccf2016-08-12 13:50:00 +020031
32
33def test_automatic_upcasting():
34 from pybind11_tests import return_class_1, return_class_2, return_none
35
36 assert type(return_class_1()).__name__ == "DerivedClass1"
37 assert type(return_class_2()).__name__ == "DerivedClass2"
38 assert type(return_none()).__name__ == "NoneType"