From 122198d9897cf04d11a9be0c725852906b365726 Mon Sep 17 00:00:00 2001 From: Charles Oliver Nutter Date: Mon, 17 Jul 2023 18:18:24 -0500 Subject: [PATCH] Add test case for JRubyMethod.checkArity behavior See jruby/jruby#7860 --- .../org/jruby/test/TestMethodFactories.java | 43 +++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/core/src/test/java/org/jruby/test/TestMethodFactories.java b/core/src/test/java/org/jruby/test/TestMethodFactories.java index 4b837c93721..b10b8e8219a 100644 --- a/core/src/test/java/org/jruby/test/TestMethodFactories.java +++ b/core/src/test/java/org/jruby/test/TestMethodFactories.java @@ -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; @@ -47,6 +48,8 @@ public void testInvocationMethodFactory() { RubyModule mod = runtime.defineModule("Wombat" + hashCode()); mod.defineAnnotatedMethods(MyBoundClass.class); + + confirmCheckArity(mod); } public void testReflectionMethodFactory() { @@ -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 @@ -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 {