You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

55 lines
2.1 KiB

  1. import pytest
  2. def test_inheritance(msg):
  3. from pybind11_tests import Pet, Dog, Rabbit, Hamster, dog_bark, pet_name_species
  4. roger = Rabbit('Rabbit')
  5. assert roger.name() + " is a " + roger.species() == "Rabbit is a parrot"
  6. assert pet_name_species(roger) == "Rabbit is a parrot"
  7. polly = Pet('Polly', 'parrot')
  8. assert polly.name() + " is a " + polly.species() == "Polly is a parrot"
  9. assert pet_name_species(polly) == "Polly is a parrot"
  10. molly = Dog('Molly')
  11. assert molly.name() + " is a " + molly.species() == "Molly is a dog"
  12. assert pet_name_species(molly) == "Molly is a dog"
  13. fred = Hamster('Fred')
  14. assert fred.name() + " is a " + fred.species() == "Fred is a rodent"
  15. assert dog_bark(molly) == "Woof!"
  16. with pytest.raises(TypeError) as excinfo:
  17. dog_bark(polly)
  18. assert msg(excinfo.value) == """
  19. dog_bark(): incompatible function arguments. The following argument types are supported:
  20. 1. (arg0: m.Dog) -> str
  21. Invoked with: <m.Pet object at 0>
  22. """
  23. def test_automatic_upcasting():
  24. from pybind11_tests import return_class_1, return_class_2, return_class_n, return_none
  25. assert type(return_class_1()).__name__ == "DerivedClass1"
  26. assert type(return_class_2()).__name__ == "DerivedClass2"
  27. assert type(return_none()).__name__ == "NoneType"
  28. # Repeat these a few times in a random order to ensure no invalid caching is applied
  29. assert type(return_class_n(1)).__name__ == "DerivedClass1"
  30. assert type(return_class_n(2)).__name__ == "DerivedClass2"
  31. assert type(return_class_n(0)).__name__ == "BaseClass"
  32. assert type(return_class_n(2)).__name__ == "DerivedClass2"
  33. assert type(return_class_n(2)).__name__ == "DerivedClass2"
  34. assert type(return_class_n(0)).__name__ == "BaseClass"
  35. assert type(return_class_n(1)).__name__ == "DerivedClass1"
  36. def test_isinstance():
  37. from pybind11_tests import test_isinstance, Pet, Dog
  38. objects = [tuple(), dict(), Pet("Polly", "parrot")] + [Dog("Molly")] * 4
  39. expected = (True, True, True, True, True, False, False)
  40. assert test_isinstance(objects) == expected