diff -r 3b553acbd931 -r 2f800fdc371e rt/vm8/src/test/java/org/apidesign/bck2brwsr/vm8/LambdasTest.java --- a/rt/vm8/src/test/java/org/apidesign/bck2brwsr/vm8/LambdasTest.java Sat Sep 13 14:37:49 2014 +0200 +++ b/rt/vm8/src/test/java/org/apidesign/bck2brwsr/vm8/LambdasTest.java Sun Sep 14 19:27:44 2014 +0200 @@ -17,6 +17,10 @@ */ package org.apidesign.bck2brwsr.vm8; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import java.util.concurrent.Callable; import org.apidesign.bck2brwsr.vmtest.Compare; import org.apidesign.bck2brwsr.vmtest.VMTest; import org.testng.annotations.Factory; @@ -25,10 +29,140 @@ * * @author Jaroslav Tulach */ -public class LambdasTest { +public class LambdasTest extends LambdasSuper { @Compare public String StringverifyJSTime() throws Exception { return Lambdas.compound(); } + + @Compare + public int canCallLambda() { + int[] arr = {0}; + Runnable incr = () -> { + arr[0]++; + }; + incr.run(); + return arr[0]; + } + + @Compare + public String lambdaReturnsString() throws Exception { + Callable lambda = () -> "Hello World!"; + return lambda.call(); + } + + private interface Convertor { + + public R convert(P value); + } + + @Compare + public int convertToLength() { + Convertor lambda = (String s) -> s.getBytes().length; + return lambda.convert("buu"); + } + + private int meaningOfWorld = 0; + + @Compare + public int accessToInstanceVar() { + Runnable lambda = () -> { + meaningOfWorld = 42; + }; + lambda.run(); + return meaningOfWorld; + } + + @Compare + public int localMeaningOfWorld() { + int[] meansOfWorld = new int[1]; + Runnable lambda = () -> { + meansOfWorld[0] = 42; + }; + lambda.run(); + return meansOfWorld[0]; + } + + @Compare + public int useLocalVars() throws Exception { + boolean bool = true; + byte b = 2; + short s = 3; + int i = 4; + long l = 5; + float f = 6; + double d = 7; + char c = 8; + Callable lambda = () -> (int) ((bool ? 1 : 0) + b + s + i + l + f + d + c); + return lambda.call(); + } + + @Compare + public String callVirtualMethod() throws Exception { + String foo = "foo"; + Callable ref = foo::toUpperCase; + return ref.call(); + } + + @Compare + public int callInterfaceMethod() throws Exception { + List foos = Arrays.asList("foo"); + Callable ref = foos::size; + return ref.call(); + } + + @Compare + public long callStaticMethod() throws Exception { + long expected = System.currentTimeMillis(); + Callable ref = System::currentTimeMillis; + return ref.call() & ~0xffff; + } + + @Compare + public String callConstructor() throws Exception { + Callable> ref = ArrayList::new; + return ref.call().toString(); + } + + @Compare + public String superMethodOverridenByThis() throws Exception { + Callable ref = super::inheritedMethod; + return ref.call(); + } + + @Override + String inheritedMethod() { + return "overridden version"; + } + + @Compare + public String referencePrivateClassMethod() throws Exception { + StringBuilder sb = new StringBuilder(); + + Callable ref1 = LambdasTest::privateClassMethod; + sb.append(ref1.call()); + + Callable ref2 = this::privateInstanceMethod; + sb.append("\n").append(ref2.call()); + + // Normal method calls should still work after our magic + // of making them them accessible from the lambda classes. + sb.append("\n").append(privateClassMethod()); + sb.append("\n").append(privateInstanceMethod()); + + return sb.toString(); + } + + private String privateInstanceMethod() { + return "foo"; + } + + private static String privateClassMethod() { + return "foo"; + } + + private String unrelatedPrivateMethod() { + return "foo"; + } @Factory public static Object[] create() { return VMTest.create(LambdasTest.class);