Skip to content

Commit

Permalink
Add test case for JRubyMethod.checkArity behavior
Browse files Browse the repository at this point in the history
  • Loading branch information
headius committed Jul 17, 2023
1 parent 31b7c07 commit 122198d
Showing 1 changed file with 43 additions and 0 deletions.
43 changes: 43 additions & 0 deletions core/src/test/java/org/jruby/test/TestMethodFactories.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import org.jruby.RubyMethod;
import org.jruby.RubyModule;
import org.jruby.anno.JRubyMethod;
import org.jruby.exceptions.ArgumentError;
import org.jruby.internal.runtime.methods.DynamicMethod;
import org.jruby.runtime.ThreadContext;
import org.jruby.runtime.builtin.IRubyObject;
Expand All @@ -47,6 +48,8 @@ public void testInvocationMethodFactory() {
RubyModule mod = runtime.defineModule("Wombat" + hashCode());

mod.defineAnnotatedMethods(MyBoundClass.class);

confirmCheckArity(mod);
}

public void testReflectionMethodFactory() {
Expand All @@ -66,6 +69,28 @@ private void confirmMethods(RubyModule mod) {
mod.searchMethod("four_arg_method").call(context, mod, mod.getMetaClass(), "four_arg_method", new IRubyObject[] {nil, nil, nil, nil}).isTrue());
}

// jruby/jruby#7851: Restore automatic arity checking with an opt-out
private void confirmCheckArity(RubyModule mod) {
ThreadContext context = runtime.getCurrentContext();

verifyCheckArityError(mod, context, "optWithCheckArityTrue", true);
verifyCheckArityError(mod, context, "optWithCheckArityFalse", false);
verifyCheckArityError(mod, context, "optWithCheckArityDefault", true);
}

private static void verifyCheckArityError(RubyModule mod, ThreadContext context, String method, boolean error) {
if (error) {
try {
mod.searchMethod(method).call(context, mod, mod.getMetaClass(), method);
if (error) fail("optCheckArityTrue should error with zero args");
} catch (ArgumentError ae) {
// pass
if (!error) fail(method + " should not error with zero args");
return;
}
}
}

public static class MyBoundClass {
// void methods should work
@JRubyMethod
Expand All @@ -77,6 +102,24 @@ public static void void_returning_method(IRubyObject obj) {}
public static IRubyObject four_arg_method(IRubyObject self, IRubyObject[] obj) {
return self.getRuntime().getTrue();
}

// jruby/jruby#7851: Restore automatic arity checking with an opt-out
@JRubyMethod(required = 1, optional = 1, checkArity = true)
public static IRubyObject optWithCheckArityTrue(ThreadContext context, IRubyObject self, IRubyObject[] args) {
return context.tru;
}

// jruby/jruby#7851: Restore automatic arity checking with an opt-out
@JRubyMethod(required = 1, optional = 1, checkArity = false)
public static IRubyObject optWithCheckArityFalse(ThreadContext context, IRubyObject self, IRubyObject[] args) {
return context.tru;
}

// jruby/jruby#7851: Restore automatic arity checking with an opt-out
@JRubyMethod(required = 1, optional = 1)
public static IRubyObject optWithCheckArityDefault(ThreadContext context, IRubyObject self, IRubyObject[] args) {
return context.tru;
}
}

public static class GH3463Module {
Expand Down

0 comments on commit 122198d

Please sign in to comment.