Skip to content

Commit

Permalink
More informative error message when an overloaded method cannot be se…
Browse files Browse the repository at this point in the history
…lected.
  • Loading branch information
cfis committed Dec 14, 2024
1 parent 6f93e10 commit 6e7002d
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
5 changes: 4 additions & 1 deletion rice/detail/Native.ipp
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,10 @@ namespace Rice::detail
}
else
{
rb_raise(rb_eArgError, "Could not resolve method call for %s#%s", rb_class2name(klass), identifier.c_str());
std::ostringstream message;
message << "Could not resolve method call for %s#%s" << "\n"
<< " %d overload(s) were evaluated based on the types of Ruby parameters provided.";
rb_raise(rb_eArgError, message.str().c_str(), rb_class2name(klass), identifier.c_str(), natives.size());
}
}
}
Expand Down
19 changes: 19 additions & 0 deletions test/test_Overloads.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,10 @@ namespace

Class createMyClass()
{
using Data_Type_T = Data_Type<MyClass>;
if (Data_Type_T::is_bound())
detail::Registries::instance.natives.reset(Data_Type_T::klass());

Class c = define_class<MyClass>("MyClass")
.define_constructor(Constructor<MyClass>())
.define_method<std::string(MyClass::*)()>("run", &MyClass::run)
Expand Down Expand Up @@ -215,6 +219,21 @@ TESTCASE(method_two_parameters)
ASSERT_EQUAL("run<float,int>", result.str());
}

TESTCASE(invalid_parameters)
{
Module m = define_module("Testing");
Class c = createMyClass();

std::string code = R"(my_class = MyClass.new
my_class.run("abc", "def"))";

ASSERT_EXCEPTION_CHECK(
Exception,
Rice::String result = m.module_eval(code),
ASSERT_EQUAL("Could not resolve method call for MyClass#run\n 6 overload(s) were evaluated based on the types of Ruby parameters provided.",
ex.what()));
}

namespace
{
class MyClass2
Expand Down

0 comments on commit 6e7002d

Please sign in to comment.