rt/vm8/src/test/java/org/apidesign/bck2brwsr/vm8/LambdasTest.java
changeset 1692 2f800fdc371e
parent 1680 3b553acbd931
child 1787 ea12a3bb4b33
     1.1 --- a/rt/vm8/src/test/java/org/apidesign/bck2brwsr/vm8/LambdasTest.java	Sat Sep 13 14:37:49 2014 +0200
     1.2 +++ b/rt/vm8/src/test/java/org/apidesign/bck2brwsr/vm8/LambdasTest.java	Sun Sep 14 19:27:44 2014 +0200
     1.3 @@ -17,6 +17,10 @@
     1.4   */
     1.5  package org.apidesign.bck2brwsr.vm8;
     1.6  
     1.7 +import java.util.ArrayList;
     1.8 +import java.util.Arrays;
     1.9 +import java.util.List;
    1.10 +import java.util.concurrent.Callable;
    1.11  import org.apidesign.bck2brwsr.vmtest.Compare;
    1.12  import org.apidesign.bck2brwsr.vmtest.VMTest;
    1.13  import org.testng.annotations.Factory;
    1.14 @@ -25,10 +29,140 @@
    1.15   *
    1.16   * @author Jaroslav Tulach <jtulach@netbeans.org>
    1.17   */
    1.18 -public class LambdasTest {
    1.19 +public class LambdasTest extends LambdasSuper {
    1.20      @Compare public String StringverifyJSTime() throws Exception {
    1.21          return Lambdas.compound();
    1.22      }
    1.23 +
    1.24 +    @Compare
    1.25 +    public int canCallLambda() {
    1.26 +        int[] arr = {0};
    1.27 +        Runnable incr = () -> {
    1.28 +            arr[0]++;
    1.29 +        };
    1.30 +        incr.run();
    1.31 +        return arr[0];
    1.32 +    }
    1.33 +
    1.34 +    @Compare
    1.35 +    public String lambdaReturnsString() throws Exception {
    1.36 +        Callable<String> lambda = () -> "Hello World!";
    1.37 +        return lambda.call();
    1.38 +    }
    1.39 +
    1.40 +    private interface Convertor<P, R> {
    1.41 +
    1.42 +        public R convert(P value);
    1.43 +    }
    1.44 +
    1.45 +    @Compare
    1.46 +    public int convertToLength() {
    1.47 +        Convertor<String, Integer> lambda = (String s) -> s.getBytes().length;
    1.48 +        return lambda.convert("buu");
    1.49 +    }
    1.50 +
    1.51 +    private int meaningOfWorld = 0;
    1.52 +
    1.53 +    @Compare
    1.54 +    public int accessToInstanceVar() {
    1.55 +        Runnable lambda = () -> {
    1.56 +            meaningOfWorld = 42;
    1.57 +        };
    1.58 +        lambda.run();
    1.59 +        return meaningOfWorld;
    1.60 +    }
    1.61 +
    1.62 +    @Compare
    1.63 +    public int localMeaningOfWorld() {
    1.64 +        int[] meansOfWorld = new int[1];
    1.65 +        Runnable lambda = () -> {
    1.66 +            meansOfWorld[0] = 42;
    1.67 +        };
    1.68 +        lambda.run();
    1.69 +        return meansOfWorld[0];
    1.70 +    }
    1.71 +
    1.72 +    @Compare
    1.73 +    public int useLocalVars() throws Exception {
    1.74 +        boolean bool = true;
    1.75 +        byte b = 2;
    1.76 +        short s = 3;
    1.77 +        int i = 4;
    1.78 +        long l = 5;
    1.79 +        float f = 6;
    1.80 +        double d = 7;
    1.81 +        char c = 8;
    1.82 +        Callable<Integer> lambda = () -> (int) ((bool ? 1 : 0) + b + s + i + l + f + d + c);
    1.83 +        return lambda.call();
    1.84 +    }
    1.85 +
    1.86 +    @Compare
    1.87 +    public String callVirtualMethod() throws Exception {
    1.88 +        String foo = "foo";
    1.89 +        Callable<String> ref = foo::toUpperCase;
    1.90 +        return ref.call();
    1.91 +    }
    1.92 +
    1.93 +    @Compare
    1.94 +    public int callInterfaceMethod() throws Exception {
    1.95 +        List<String> foos = Arrays.asList("foo");
    1.96 +        Callable<Integer> ref = foos::size;
    1.97 +        return ref.call();
    1.98 +    }
    1.99 +
   1.100 +    @Compare
   1.101 +    public long callStaticMethod() throws Exception {
   1.102 +        long expected = System.currentTimeMillis();
   1.103 +        Callable<Long> ref = System::currentTimeMillis;
   1.104 +        return ref.call() & ~0xffff;
   1.105 +    }
   1.106 +
   1.107 +    @Compare
   1.108 +    public String callConstructor() throws Exception {
   1.109 +        Callable<List<String>> ref = ArrayList<String>::new;
   1.110 +        return ref.call().toString();
   1.111 +    }
   1.112 +
   1.113 +    @Compare
   1.114 +    public String superMethodOverridenByThis() throws Exception {
   1.115 +        Callable<String> ref = super::inheritedMethod;
   1.116 +        return ref.call();
   1.117 +    }
   1.118 +
   1.119 +    @Override
   1.120 +    String inheritedMethod() {
   1.121 +        return "overridden version";
   1.122 +    }
   1.123 +
   1.124 +    @Compare
   1.125 +    public String referencePrivateClassMethod() throws Exception {
   1.126 +        StringBuilder sb = new StringBuilder();
   1.127 +
   1.128 +        Callable<String> ref1 = LambdasTest::privateClassMethod;
   1.129 +        sb.append(ref1.call());
   1.130 +
   1.131 +        Callable<String> ref2 = this::privateInstanceMethod;
   1.132 +        sb.append("\n").append(ref2.call());
   1.133 +
   1.134 +        // Normal method calls should still work after our magic
   1.135 +        // of making them them accessible from the lambda classes.
   1.136 +        sb.append("\n").append(privateClassMethod());
   1.137 +        sb.append("\n").append(privateInstanceMethod());
   1.138 +
   1.139 +        return sb.toString();
   1.140 +    }
   1.141 +
   1.142 +    private String privateInstanceMethod() {
   1.143 +        return "foo";
   1.144 +    }
   1.145 +
   1.146 +    private static String privateClassMethod() {
   1.147 +        return "foo";
   1.148 +    }
   1.149 +
   1.150 +    private String unrelatedPrivateMethod() {
   1.151 +        return "foo";
   1.152 +    }
   1.153      
   1.154      @Factory public static Object[] create() {
   1.155          return VMTest.create(LambdasTest.class);