2016-08-12 22:37:50 +00:00
|
|
|
import pytest
|
|
|
|
|
|
|
|
|
|
|
|
def test_inheritance(msg):
|
2016-09-06 16:27:00 +00:00
|
|
|
from pybind11_tests import Pet, Dog, Rabbit, Hamster, dog_bark, pet_name_species
|
2016-08-12 22:37:50 +00:00
|
|
|
|
|
|
|
roger = Rabbit('Rabbit')
|
|
|
|
assert roger.name() + " is a " + roger.species() == "Rabbit is a parrot"
|
|
|
|
assert pet_name_species(roger) == "Rabbit is a parrot"
|
|
|
|
|
|
|
|
polly = Pet('Polly', 'parrot')
|
|
|
|
assert polly.name() + " is a " + polly.species() == "Polly is a parrot"
|
|
|
|
assert pet_name_species(polly) == "Polly is a parrot"
|
|
|
|
|
|
|
|
molly = Dog('Molly')
|
|
|
|
assert molly.name() + " is a " + molly.species() == "Molly is a dog"
|
|
|
|
assert pet_name_species(molly) == "Molly is a dog"
|
|
|
|
|
2016-09-06 16:27:00 +00:00
|
|
|
fred = Hamster('Fred')
|
|
|
|
assert fred.name() + " is a " + fred.species() == "Fred is a rodent"
|
|
|
|
|
2016-08-12 22:37:50 +00:00
|
|
|
assert dog_bark(molly) == "Woof!"
|
|
|
|
|
|
|
|
with pytest.raises(TypeError) as excinfo:
|
|
|
|
dog_bark(polly)
|
|
|
|
assert msg(excinfo.value) == """
|
2016-09-12 02:44:37 +00:00
|
|
|
dog_bark(): incompatible function arguments. The following argument types are supported:
|
2016-08-12 22:37:50 +00:00
|
|
|
1. (arg0: m.Dog) -> str
|
2016-09-12 02:44:37 +00:00
|
|
|
|
|
|
|
Invoked with: <m.Pet object at 0>
|
2016-08-12 22:37:50 +00:00
|
|
|
"""
|
2016-08-12 11:50:00 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_automatic_upcasting():
|
2016-09-11 22:41:28 +00:00
|
|
|
from pybind11_tests import return_class_1, return_class_2, return_class_n, return_none
|
2016-08-12 11:50:00 +00:00
|
|
|
|
|
|
|
assert type(return_class_1()).__name__ == "DerivedClass1"
|
|
|
|
assert type(return_class_2()).__name__ == "DerivedClass2"
|
|
|
|
assert type(return_none()).__name__ == "NoneType"
|
2016-09-11 22:41:28 +00:00
|
|
|
# Repeat these a few times in a random order to ensure no invalid caching is applied
|
|
|
|
assert type(return_class_n(1)).__name__ == "DerivedClass1"
|
|
|
|
assert type(return_class_n(2)).__name__ == "DerivedClass2"
|
|
|
|
assert type(return_class_n(0)).__name__ == "BaseClass"
|
|
|
|
assert type(return_class_n(2)).__name__ == "DerivedClass2"
|
|
|
|
assert type(return_class_n(2)).__name__ == "DerivedClass2"
|
|
|
|
assert type(return_class_n(0)).__name__ == "BaseClass"
|
|
|
|
assert type(return_class_n(1)).__name__ == "DerivedClass1"
|