Search in sources :

Example 11 with RubyProc

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

the class JRubyTest method convertRubyProcToFn.

@Test
public void convertRubyProcToFn() throws ScriptException {
    RubyProc proc = (RubyProc) rb.eval("lambda {|s| s.upcase}");
    assertEquals("HELLO", toFn1(proc).call("hello"));
}
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)

Example 12 with RubyProc

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

the class JavaScriptTest method interactingWithJRuby.

@Test
public void interactingWithJRuby() throws Exception {
    ScriptEngine rb = JRubyTest.getJRubyEngine();
    RubyProc proc = (RubyProc) rb.eval(":*.to_proc");
    FunctionFn2 f = toFunction(LambdaJRuby.toFn2(proc));
    js.put("f", f);
    assertEquals(6.0, js.eval("f(2, 3)"));
}
Also used : RubyProc(org.jruby.RubyProc) FunctionFn2(org.enumerable.lambda.support.javascript.LambdaJavaScript.FunctionFn2) 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) ClojureTest(org.enumerable.lambda.support.clojure.ClojureTest)

Example 13 with RubyProc

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

the class JRubyTest method convertedRubyProcRaisesArgumentErrorWhenCalledWithTooManyArguments.

@Test(expected = RaiseException.class)
public void convertedRubyProcRaisesArgumentErrorWhenCalledWithTooManyArguments() throws ScriptException {
    Ruby ruby = Ruby.getGlobalRuntime();
    try {
        RubyProc proc = toProc(Lambda.λ(s, s.toUpperCase()));
        proc.call(ruby.getThreadService().getCurrentContext(), new IRubyObject[] { ruby.newString("hello"), ruby.newString("world") });
    } 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 14 with RubyProc

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

the class JRubyTest method interactingWithJavaScript.

@Test
public void interactingWithJavaScript() throws Exception {
    Ruby ruby = Ruby.getGlobalRuntime();
    ScriptEngine js = JavaScriptTest.getJavaScriptEngine();
    Function f = (Function) js.eval("var f = function(n, m) { return n * m; }; f;");
    RubyProc proc = toProc(LambdaJavaScript.toFn2(f));
    assertEquals(ruby.newFloat(6), proc.call(ruby.getThreadService().getCurrentContext(), new IRubyObject[] { ruby.newFixnum(2), ruby.newFixnum(3) }));
    rb.put("block", proc);
    assertEquals(120.0, rb.eval("[1, 2, 3, 4, 5].inject &block"));
}
Also used : Function(sun.org.mozilla.javascript.internal.Function) RubyProc(org.jruby.RubyProc) IRubyObject(org.jruby.runtime.builtin.IRubyObject) Ruby(org.jruby.Ruby) LambdaJRuby(org.enumerable.lambda.support.jruby.LambdaJRuby) ScriptEngine(javax.script.ScriptEngine) 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 15 with RubyProc

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

the class JRubyTest method convertFnToRubyProcKeepsDefaultValues.

@Test
public void convertFnToRubyProcKeepsDefaultValues() throws ScriptException {
    Ruby ruby = Ruby.getGlobalRuntime();
    RubyProc proc = toProc(Lambda.λ(s = "world", s.toUpperCase()));
    assertEquals(ruby.newString("WORLD"), proc.call(ruby.getThreadService().getCurrentContext(), new IRubyObject[] {}));
}
Also used : RubyProc(org.jruby.RubyProc) 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)

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