Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions tests/test_code_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,18 @@ def test_enums():
with pytest.raises(AssertionError):
foo.enumToInt(myenum2_a)

# Test 5: Overload resolution works correctly for different enum types
# The process() method is overloaded: process(MyEnum) and process(MyEnum2)
# Python should dispatch to the correct overload based on enum type
assert foo.process(mod.Foo.MyEnum.A) == b"MyEnum"
assert foo.process(mod.Foo.MyEnum.B) == b"MyEnum"
assert foo.process(mod.Foo.MyEnum2.A) == b"MyEnum2"
assert foo.process(mod.Foo.MyEnum2.C) == b"MyEnum2"

# Test 6: Overloaded method rejects wrong enum type from different namespace
with pytest.raises(Exception):
foo.process(mod.Foo2.MyEnum.A)


def test_number_conv():
target = os.path.join(test_files, "generated", "number_conv.pyx")
Expand Down
7 changes: 7 additions & 0 deletions tests/test_files/enums.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
* =============================================================================
*/

#include <string>

// Class with nested scoped enums
class Foo
{
Expand Down Expand Up @@ -43,6 +45,11 @@ class Foo
}
return 0; // unreachable, but silences compiler warning
};

// Overloaded methods accepting different enum types - tests overload resolution
// Python should correctly dispatch based on the enum type passed
std::string process(MyEnum e) { return "MyEnum"; }
std::string process(MyEnum2 e) { return "MyEnum2"; }
};

// Separate namespace with an enum of the same name as Foo::MyEnum
Expand Down
4 changes: 4 additions & 0 deletions tests/test_files/enums.pxd
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# cython: language_level=3
from libcpp.string cimport string as libcpp_string
#
# =============================================================================
# Example: Wrapping C++ Enums in Different Namespaces
Expand Down Expand Up @@ -44,6 +45,9 @@ cdef extern from "enums.hpp":
cdef cppclass Foo:
# Method accepts Foo_MyEnum (which maps to Foo::MyEnum in C++)
int enumToInt(Foo_MyEnum e)
# Overloaded methods for testing enum-based overload resolution
libcpp_string process(Foo_MyEnum e)
libcpp_string process(Foo_MyEnum2 e)

cdef extern from "enums.hpp":
cdef cppclass Foo2:
Expand Down