Dean Moldovan | 382db5b | 2016-08-13 00:37:50 +0200 | [diff] [blame] | 1 | import pytest |
| 2 | |
| 3 | |
| 4 | def test_inheritance(msg): |
| 5 | from pybind11_tests import Pet, Dog, Rabbit, dog_bark, pet_name_species |
| 6 | |
| 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 | |
| 19 | assert dog_bark(molly) == "Woof!" |
| 20 | |
| 21 | with pytest.raises(TypeError) as excinfo: |
| 22 | dog_bark(polly) |
| 23 | assert msg(excinfo.value) == """ |
| 24 | Incompatible function arguments. The following argument types are supported: |
| 25 | 1. (arg0: m.Dog) -> str |
| 26 | Invoked with: <m.Pet object at 0> |
| 27 | """ |
Dean Moldovan | a0c1ccf | 2016-08-12 13:50:00 +0200 | [diff] [blame] | 28 | |
| 29 | |
| 30 | def test_automatic_upcasting(): |
| 31 | from pybind11_tests import return_class_1, return_class_2, return_none |
| 32 | |
| 33 | assert type(return_class_1()).__name__ == "DerivedClass1" |
| 34 | assert type(return_class_2()).__name__ == "DerivedClass2" |
| 35 | assert type(return_none()).__name__ == "NoneType" |