summaryrefslogtreecommitdiff
path: root/ext/pybind11/tests/test_inheritance.py
diff options
context:
space:
mode:
authorAndreas Sandberg <andreas.sandberg@arm.com>2017-02-27 13:17:51 +0000
committerAndreas Sandberg <andreas.sandberg@arm.com>2017-05-02 12:37:32 +0000
commitc79706ff4ce591df2151db5504d3c224f3c9965f (patch)
treeb56cd2bfe704a40575a71075e78194a4c516c98d /ext/pybind11/tests/test_inheritance.py
parent359cb08623324b62d7c34973ae54d5bc7f23f9fd (diff)
downloadgem5-c79706ff4ce591df2151db5504d3c224f3c9965f.tar.xz
ext: Add pybind rev f4b81b3
Change-Id: I52e4fc9ebf2f59da57d8cf8f3e37cc79598c2f5f Signed-off-by: Andreas Sandberg <andreas.sandberg@arm.com> Reviewed-by: Andreas Hansson <andreas.hansson@arm.com> Reviewed-by: Curtis Dunham <curtis.dunham@arm.com> Reviewed-on: https://gem5-review.googlesource.com/2229 Reviewed-by: Tony Gutierrez <anthony.gutierrez@amd.com> Reviewed-by: Jason Lowe-Power <jason@lowepower.com> Reviewed-by: Pierre-Yves PĂ©neau <pierre-yves.peneau@lirmm.fr>
Diffstat (limited to 'ext/pybind11/tests/test_inheritance.py')
-rw-r--r--ext/pybind11/tests/test_inheritance.py55
1 files changed, 55 insertions, 0 deletions
diff --git a/ext/pybind11/tests/test_inheritance.py b/ext/pybind11/tests/test_inheritance.py
new file mode 100644
index 000000000..7bb52be02
--- /dev/null
+++ b/ext/pybind11/tests/test_inheritance.py
@@ -0,0 +1,55 @@
+import pytest
+
+
+def test_inheritance(msg):
+ from pybind11_tests import Pet, Dog, Rabbit, Hamster, dog_bark, pet_name_species
+
+ 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"
+
+ fred = Hamster('Fred')
+ assert fred.name() + " is a " + fred.species() == "Fred is a rodent"
+
+ assert dog_bark(molly) == "Woof!"
+
+ with pytest.raises(TypeError) as excinfo:
+ dog_bark(polly)
+ assert msg(excinfo.value) == """
+ dog_bark(): incompatible function arguments. The following argument types are supported:
+ 1. (arg0: m.Dog) -> str
+
+ Invoked with: <m.Pet object at 0>
+ """
+
+
+def test_automatic_upcasting():
+ from pybind11_tests import return_class_1, return_class_2, return_class_n, return_none
+
+ assert type(return_class_1()).__name__ == "DerivedClass1"
+ assert type(return_class_2()).__name__ == "DerivedClass2"
+ assert type(return_none()).__name__ == "NoneType"
+ # 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"
+
+
+def test_isinstance():
+ from pybind11_tests import test_isinstance, Pet, Dog
+
+ objects = [tuple(), dict(), Pet("Polly", "parrot")] + [Dog("Molly")] * 4
+ expected = (True, True, True, True, True, False, False)
+ assert test_isinstance(objects) == expected