Search in sources :

Example 1 with RubyProc

use of org.jruby.RubyProc in project enumerable by hraberg.

the class GroovyTest method interactingWithJRuby.

@Test
public void interactingWithJRuby() throws Exception {
    ScriptEngine rb = JRubyTest.getJRubyEngine();
    RubyProc proc = (RubyProc) rb.eval(":*.to_proc");
    Closure<?> closure = toClosure(LambdaJRuby.toFn2(proc));
    groovy.put("f", closure);
    assertEquals(6L, groovy.eval("f(2, 3)"));
}
Also used : RubyProc(org.jruby.RubyProc) ScriptEngine(javax.script.ScriptEngine) ScalaTest(org.enumerable.lambda.support.scala.ScalaTest) JRubyTest(org.enumerable.lambda.support.jruby.JRubyTest) Test(org.junit.Test) ClojureTest(org.enumerable.lambda.support.clojure.ClojureTest) JavaScriptTest(org.enumerable.lambda.support.javascript.JavaScriptTest)

Example 2 with RubyProc

use of org.jruby.RubyProc in project enumerable by hraberg.

the class ClojureTest method interactingWithJRuby.

@Test
public void interactingWithJRuby() throws Exception {
    ScriptEngine rb = JRubyTest.getJRubyEngine();
    RubyProc proc = (RubyProc) rb.eval(":*.to_proc");
    IFn times = toIFn(LambdaJRuby.toFn2(proc));
    assertEquals(6L, times.invoke(2, 3));
    defn("times-rb", times);
    assertEquals(120L, clj.eval("(reduce times-rb 1 [1, 2, 3, 4, 5])"));
}
Also used : IFn(clojure.lang.IFn) RubyProc(org.jruby.RubyProc) ScriptEngine(javax.script.ScriptEngine) GroovyTest(org.enumerable.lambda.support.groovy.GroovyTest) ScalaTest(org.enumerable.lambda.support.scala.ScalaTest) JRubyTest(org.enumerable.lambda.support.jruby.JRubyTest) Test(org.junit.Test) JavaScriptTest(org.enumerable.lambda.support.javascript.JavaScriptTest)

Example 3 with RubyProc

use of org.jruby.RubyProc in project enumerable by hraberg.

the class JRubyTest method convertedRubyProcRaisesArgumentErrorWhenCalledWithTooFewArguments.

@Test(expected = RaiseException.class)
public void convertedRubyProcRaisesArgumentErrorWhenCalledWithTooFewArguments() throws ScriptException {
    Ruby ruby = Ruby.getGlobalRuntime();
    try {
        RubyProc proc = toProc(Lambda.λ(s, s.toUpperCase()));
        proc.call(ruby.getThreadService().getCurrentContext(), new IRubyObject[0]);
    } catch (RaiseException e) {
        assertEquals(ruby.getArgumentError(), e.getException().getType());
        throw e;
    }
}
Also used : RubyProc(org.jruby.RubyProc) RaiseException(org.jruby.exceptions.RaiseException) Ruby(org.jruby.Ruby) LambdaJRuby(org.enumerable.lambda.support.jruby.LambdaJRuby) GroovyTest(org.enumerable.lambda.support.groovy.GroovyTest) ScalaTest(org.enumerable.lambda.support.scala.ScalaTest) Test(org.junit.Test) ClojureTest(org.enumerable.lambda.support.clojure.ClojureTest) JavaScriptTest(org.enumerable.lambda.support.javascript.JavaScriptTest)

Example 4 with RubyProc

use of org.jruby.RubyProc in project enumerable by hraberg.

the class JRubyTest method interactingWithScala.

@SuppressWarnings("unchecked")
@Test
public void interactingWithScala() throws Exception {
    Ruby ruby = Ruby.getGlobalRuntime();
    ScalaInterpreter scala = ScalaTest.getScalaInterpreter();
    Function2<Integer, Integer, Integer> f = (Function2<Integer, Integer, Integer>) scala.eval("(n: Long, m: Long) => n * m");
    RubyProc proc = toProc(LambdaScala.toFn2(f));
    assertEquals(ruby.newFixnum(6), proc.call(ruby.getThreadService().getCurrentContext(), new IRubyObject[] { ruby.newFixnum(2), ruby.newFixnum(3) }));
    rb.put("block", proc);
    assertEquals(120L, rb.eval("[1, 2, 3, 4, 5].inject &block"));
}
Also used : RubyProc(org.jruby.RubyProc) ScalaInterpreter(org.enumerable.lambda.support.scala.ScalaTest.ScalaInterpreter) Function2(scala.Function2) IRubyObject(org.jruby.runtime.builtin.IRubyObject) Ruby(org.jruby.Ruby) LambdaJRuby(org.enumerable.lambda.support.jruby.LambdaJRuby) GroovyTest(org.enumerable.lambda.support.groovy.GroovyTest) ScalaTest(org.enumerable.lambda.support.scala.ScalaTest) Test(org.junit.Test) ClojureTest(org.enumerable.lambda.support.clojure.ClojureTest) JavaScriptTest(org.enumerable.lambda.support.javascript.JavaScriptTest)

Example 5 with RubyProc

use of org.jruby.RubyProc in project enumerable by hraberg.

the class JRubyTest method convertRubyMethodProcToFn.

@Test
public void convertRubyMethodProcToFn() throws ScriptException {
    RubyProc proc = (RubyProc) rb.eval("(\"hello\".method :upcase).to_proc");
    assertEquals("HELLO", toFn0(proc).call());
}
Also used : RubyProc(org.jruby.RubyProc) GroovyTest(org.enumerable.lambda.support.groovy.GroovyTest) ScalaTest(org.enumerable.lambda.support.scala.ScalaTest) Test(org.junit.Test) ClojureTest(org.enumerable.lambda.support.clojure.ClojureTest) JavaScriptTest(org.enumerable.lambda.support.javascript.JavaScriptTest)

Aggregations

RubyProc (org.jruby.RubyProc)15 Test (org.junit.Test)15 ClojureTest (org.enumerable.lambda.support.clojure.ClojureTest)14 GroovyTest (org.enumerable.lambda.support.groovy.GroovyTest)14 JavaScriptTest (org.enumerable.lambda.support.javascript.JavaScriptTest)14 ScalaTest (org.enumerable.lambda.support.scala.ScalaTest)14 LambdaJRuby (org.enumerable.lambda.support.jruby.LambdaJRuby)8 Ruby (org.jruby.Ruby)8 IRubyObject (org.jruby.runtime.builtin.IRubyObject)6 ScriptEngine (javax.script.ScriptEngine)5 JRubyTest (org.enumerable.lambda.support.jruby.JRubyTest)4 IFn (clojure.lang.IFn)2 RaiseException (org.jruby.exceptions.RaiseException)2 Closure (groovy.lang.Closure)1 FunctionFn2 (org.enumerable.lambda.support.javascript.LambdaJavaScript.FunctionFn2)1 ScalaInterpreter (org.enumerable.lambda.support.scala.ScalaTest.ScalaInterpreter)1 Function2 (scala.Function2)1 Function (sun.org.mozilla.javascript.internal.Function)1