MCPcopy Create free account
hub / github.com/pybind/pybind11 / test_override

Function test_override

tests/test_virtual_functions.py:13–94  ·  view source on GitHub ↗
(capture, msg)

Source from the content-addressed store, hash-verified

11
12
13def test_override(capture, msg):
14 class ExtendedExampleVirt(m.ExampleVirt):
15 def __init__(self, state):
16 super().__init__(state + 1)
17 self.data = "Hello world"
18
19 def run(self, value):
20 print(f"ExtendedExampleVirt::run({value}), calling parent..")
21 return super().run(value + 1)
22
23 def run_bool(self):
24 print("ExtendedExampleVirt::run_bool()")
25 return False
26
27 def get_string1(self):
28 return "override1"
29
30 def pure_virtual(self):
31 print(f"ExtendedExampleVirt::pure_virtual(): {self.data}")
32
33 class ExtendedExampleVirt2(ExtendedExampleVirt):
34 def __init__(self, state):
35 super().__init__(state + 1)
36
37 def get_string2(self):
38 return "override2"
39
40 ex12 = m.ExampleVirt(10)
41 with capture:
42 assert m.runExampleVirt(ex12, 20) == 30
43 assert (
44 capture
45 == """
46 Original implementation of ExampleVirt::run(state=10, value=20, str1=default1, str2=default2)
47 """
48 )
49
50 with pytest.raises(RuntimeError) as excinfo:
51 m.runExampleVirtVirtual(ex12)
52 assert (
53 msg(excinfo.value)
54 == 'Tried to call pure virtual function "ExampleVirt::pure_virtual"'
55 )
56
57 ex12p = ExtendedExampleVirt(10)
58 with capture:
59 assert m.runExampleVirt(ex12p, 20) == 32
60 assert (
61 capture
62 == """
63 ExtendedExampleVirt::run(20), calling parent..
64 Original implementation of ExampleVirt::run(state=11, value=21, str1=override1, str2=default2)
65 """
66 )
67 with capture:
68 assert m.runExampleVirtBool(ex12p) is False
69 assert capture == "ExtendedExampleVirt::run_bool()"
70 with capture:

Callers

nothing calls this directly

Calls 7

msgFunction · 0.85
ExtendedExampleVirtClass · 0.85
ExampleVirtMethod · 0.80
aliveMethod · 0.80
valuesMethod · 0.80
getMethod · 0.45

Tested by

no test coverage detected