Skip to content

Commit fd815fb

Browse files
Add test for enum-based overload resolution (issue #233) (#234)
Add test coverage to verify that overloaded methods with different enum parameter types dispatch correctly. This confirms that the existing scoped enum handling already supports overload resolution via isinstance() type checks. Changes: - Add overloaded process(MyEnum) and process(MyEnum2) to enums.hpp - Declare process methods in enums.pxd - Add tests verifying correct dispatch and rejection of wrong enum types 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-authored-by: Claude Opus 4.5 <[email protected]>
1 parent 0410f82 commit fd815fb

File tree

3 files changed

+23
-0
lines changed

3 files changed

+23
-0
lines changed

tests/test_code_generator.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,18 @@ def test_enums():
110110
with pytest.raises(AssertionError):
111111
foo.enumToInt(myenum2_a)
112112

113+
# Test 5: Overload resolution works correctly for different enum types
114+
# The process() method is overloaded: process(MyEnum) and process(MyEnum2)
115+
# Python should dispatch to the correct overload based on enum type
116+
assert foo.process(mod.Foo.MyEnum.A) == b"MyEnum"
117+
assert foo.process(mod.Foo.MyEnum.B) == b"MyEnum"
118+
assert foo.process(mod.Foo.MyEnum2.A) == b"MyEnum2"
119+
assert foo.process(mod.Foo.MyEnum2.C) == b"MyEnum2"
120+
121+
# Test 6: Overloaded method rejects wrong enum type from different namespace
122+
with pytest.raises(Exception):
123+
foo.process(mod.Foo2.MyEnum.A)
124+
113125

114126
def test_number_conv():
115127
target = os.path.join(test_files, "generated", "number_conv.pyx")

tests/test_files/enums.hpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
* =============================================================================
1616
*/
1717

18+
#include <string>
19+
1820
// Class with nested scoped enums
1921
class Foo
2022
{
@@ -43,6 +45,11 @@ class Foo
4345
}
4446
return 0; // unreachable, but silences compiler warning
4547
};
48+
49+
// Overloaded methods accepting different enum types - tests overload resolution
50+
// Python should correctly dispatch based on the enum type passed
51+
std::string process(MyEnum e) { return "MyEnum"; }
52+
std::string process(MyEnum2 e) { return "MyEnum2"; }
4653
};
4754

4855
// Separate namespace with an enum of the same name as Foo::MyEnum

tests/test_files/enums.pxd

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# cython: language_level=3
2+
from libcpp.string cimport string as libcpp_string
23
#
34
# =============================================================================
45
# Example: Wrapping C++ Enums in Different Namespaces
@@ -44,6 +45,9 @@ cdef extern from "enums.hpp":
4445
cdef cppclass Foo:
4546
# Method accepts Foo_MyEnum (which maps to Foo::MyEnum in C++)
4647
int enumToInt(Foo_MyEnum e)
48+
# Overloaded methods for testing enum-based overload resolution
49+
libcpp_string process(Foo_MyEnum e)
50+
libcpp_string process(Foo_MyEnum2 e)
4751

4852
cdef extern from "enums.hpp":
4953
cdef cppclass Foo2:

0 commit comments

Comments
 (0)