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.

78 lines
3.0 KiB

8 years ago
8 years ago
8 years ago
8 years ago
  1. import pytest
  2. def test_inheritance(msg):
  3. from pybind11_tests import Pet, Dog, Rabbit, Hamster, Chimera, 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. with pytest.raises(TypeError) as excinfo:
  24. Chimera("lion", "goat")
  25. assert "No constructor defined!" in str(excinfo.value)
  26. def test_automatic_upcasting():
  27. from pybind11_tests import return_class_1, return_class_2, return_class_n, return_none
  28. assert type(return_class_1()).__name__ == "DerivedClass1"
  29. assert type(return_class_2()).__name__ == "DerivedClass2"
  30. assert type(return_none()).__name__ == "NoneType"
  31. # Repeat these a few times in a random order to ensure no invalid caching
  32. # is applied
  33. assert type(return_class_n(1)).__name__ == "DerivedClass1"
  34. assert type(return_class_n(2)).__name__ == "DerivedClass2"
  35. assert type(return_class_n(0)).__name__ == "BaseClass"
  36. assert type(return_class_n(2)).__name__ == "DerivedClass2"
  37. assert type(return_class_n(2)).__name__ == "DerivedClass2"
  38. assert type(return_class_n(0)).__name__ == "BaseClass"
  39. assert type(return_class_n(1)).__name__ == "DerivedClass1"
  40. def test_isinstance():
  41. from pybind11_tests import test_isinstance, Pet, Dog
  42. objects = [tuple(), dict(), Pet("Polly", "parrot")] + [Dog("Molly")] * 4
  43. expected = (True, True, True, True, True, False, False)
  44. assert test_isinstance(objects) == expected
  45. def test_holder():
  46. from pybind11_tests import test_mismatched_holder_type_1, test_mismatched_holder_type_2
  47. with pytest.raises(RuntimeError) as excinfo:
  48. test_mismatched_holder_type_1()
  49. assert str(excinfo.value) == ("generic_type: type \"MismatchDerived1\" does not have "
  50. "a non-default holder type while its base "
  51. "\"MismatchBase1\" does")
  52. with pytest.raises(RuntimeError) as excinfo:
  53. test_mismatched_holder_type_2()
  54. assert str(excinfo.value) == ("generic_type: type \"MismatchDerived2\" has a "
  55. "non-default holder type while its base "
  56. "\"MismatchBase2\" does not")