public void testBasicFunction() throws Exception { Function f = x -> x; assertEquals(42, f.apply(42)); Function<Integer, Integer> f2 = x -> x; assertEquals((Integer) 42, f2.apply(42)); Function f3 = x -> { int y = 42; return y; }; assertEquals(42, f3.apply(null)); FourToOne<String, Double, Integer, Boolean, String> appendFour = (a, b, c, d) -> a + b + c + d; assertEquals("Foo4.214true", appendFour.apply("Foo", 4.2, 14, true)); }
// Tests outer reference resolution, and that inner fields are being correctly resolved for // lambdas with implicit blocks. public void testAdditionInLambda() throws Exception { Function f = new Function<Integer, Integer>() { @Override public Integer apply(Integer x) { return x + 20; } }; assertEquals(42, f.apply(22)); Function<Integer, Integer> g = x -> { return x + 20; }; assertEquals((Integer) 42, g.apply(22)); Function<Integer, Integer> h = x -> x + 20; assertEquals((Integer) 42, h.apply(22)); }
public void testBasicAnonymousClass() throws Exception { Function h = new Function() { @Override public Object apply(Object x) { return x; } }; assertEquals(42, h.apply(42)); }
public void testStaticFunctions() throws Exception { assertEquals(42, staticF.apply(42)); }