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.

129 lines
4.4 KiB

2 months ago
  1. import pytest
  2. from pybind11_tests import class_ as m
  3. from pybind11_tests import UserType, ConstructorStats
  4. def test_repr():
  5. # In Python 3.3+, repr() accesses __qualname__
  6. assert "pybind11_type" in repr(type(UserType))
  7. assert "UserType" in repr(UserType)
  8. def test_instance(msg):
  9. with pytest.raises(TypeError) as excinfo:
  10. m.NoConstructor()
  11. assert msg(excinfo.value) == "m.class_.NoConstructor: No constructor defined!"
  12. instance = m.NoConstructor.new_instance()
  13. cstats = ConstructorStats.get(m.NoConstructor)
  14. assert cstats.alive() == 1
  15. del instance
  16. assert cstats.alive() == 0
  17. def test_docstrings(doc):
  18. assert doc(UserType) == "A `py::class_` type for testing"
  19. assert UserType.__name__ == "UserType"
  20. assert UserType.__module__ == "pybind11_tests"
  21. assert UserType.get_value.__name__ == "get_value"
  22. assert UserType.get_value.__module__ == "pybind11_tests"
  23. assert doc(UserType.get_value) == """
  24. get_value(self: m.UserType) -> int
  25. Get value using a method
  26. """
  27. assert doc(UserType.value) == "Get value using a property"
  28. assert doc(m.NoConstructor.new_instance) == """
  29. new_instance() -> m.class_.NoConstructor
  30. Return an instance
  31. """
  32. def test_inheritance(msg):
  33. roger = m.Rabbit('Rabbit')
  34. assert roger.name() + " is a " + roger.species() == "Rabbit is a parrot"
  35. assert m.pet_name_species(roger) == "Rabbit is a parrot"
  36. polly = m.Pet('Polly', 'parrot')
  37. assert polly.name() + " is a " + polly.species() == "Polly is a parrot"
  38. assert m.pet_name_species(polly) == "Polly is a parrot"
  39. molly = m.Dog('Molly')
  40. assert molly.name() + " is a " + molly.species() == "Molly is a dog"
  41. assert m.pet_name_species(molly) == "Molly is a dog"
  42. fred = m.Hamster('Fred')
  43. assert fred.name() + " is a " + fred.species() == "Fred is a rodent"
  44. assert m.dog_bark(molly) == "Woof!"
  45. with pytest.raises(TypeError) as excinfo:
  46. m.dog_bark(polly)
  47. assert msg(excinfo.value) == """
  48. dog_bark(): incompatible function arguments. The following argument types are supported:
  49. 1. (arg0: m.class_.Dog) -> str
  50. Invoked with: <m.class_.Pet object at 0>
  51. """
  52. with pytest.raises(TypeError) as excinfo:
  53. m.Chimera("lion", "goat")
  54. assert "No constructor defined!" in str(excinfo.value)
  55. def test_automatic_upcasting():
  56. assert type(m.return_class_1()).__name__ == "DerivedClass1"
  57. assert type(m.return_class_2()).__name__ == "DerivedClass2"
  58. assert type(m.return_none()).__name__ == "NoneType"
  59. # Repeat these a few times in a random order to ensure no invalid caching is applied
  60. assert type(m.return_class_n(1)).__name__ == "DerivedClass1"
  61. assert type(m.return_class_n(2)).__name__ == "DerivedClass2"
  62. assert type(m.return_class_n(0)).__name__ == "BaseClass"
  63. assert type(m.return_class_n(2)).__name__ == "DerivedClass2"
  64. assert type(m.return_class_n(2)).__name__ == "DerivedClass2"
  65. assert type(m.return_class_n(0)).__name__ == "BaseClass"
  66. assert type(m.return_class_n(1)).__name__ == "DerivedClass1"
  67. def test_isinstance():
  68. objects = [tuple(), dict(), m.Pet("Polly", "parrot")] + [m.Dog("Molly")] * 4
  69. expected = (True, True, True, True, True, False, False)
  70. assert m.check_instances(objects) == expected
  71. def test_mismatched_holder():
  72. import re
  73. with pytest.raises(RuntimeError) as excinfo:
  74. m.mismatched_holder_1()
  75. assert re.match('generic_type: type ".*MismatchDerived1" does not have a non-default '
  76. 'holder type while its base ".*MismatchBase1" does', str(excinfo.value))
  77. with pytest.raises(RuntimeError) as excinfo:
  78. m.mismatched_holder_2()
  79. assert re.match('generic_type: type ".*MismatchDerived2" has a non-default holder type '
  80. 'while its base ".*MismatchBase2" does not', str(excinfo.value))
  81. def test_override_static():
  82. """#511: problem with inheritance + overwritten def_static"""
  83. b = m.MyBase.make()
  84. d1 = m.MyDerived.make2()
  85. d2 = m.MyDerived.make()
  86. assert isinstance(b, m.MyBase)
  87. assert isinstance(d1, m.MyDerived)
  88. assert isinstance(d2, m.MyDerived)
  89. def test_implicit_conversion_life_support():
  90. """Ensure the lifetime of temporary objects created for implicit conversions"""
  91. assert m.implicitly_convert_argument(UserType(5)) == 5
  92. assert m.implicitly_convert_variable(UserType(5)) == 5
  93. assert "outside a bound function" in m.implicitly_convert_variable_fail(UserType(5))