From 53d784ee7bb23b0190a4d5f79ca9915f6dac3fc7 Mon Sep 17 00:00:00 2001 From: Alexey Andreev Date: Sun, 13 Sep 2015 19:44:35 +0300 Subject: [PATCH] Add tests for JSFunctor --- teavm-jso/pom.xml | 1 + .../java/org/teavm/jso/test/FunctorTest.java | 52 +++++++++++++++++++ 2 files changed, 53 insertions(+) create mode 100644 teavm-jso/src/test/java/org/teavm/jso/test/FunctorTest.java diff --git a/teavm-jso/pom.xml b/teavm-jso/pom.xml index e301cfd8c..1e93afe4d 100644 --- a/teavm-jso/pom.xml +++ b/teavm-jso/pom.xml @@ -56,6 +56,7 @@ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xs maven-checkstyle-plugin ../checkstyle.xml + true diff --git a/teavm-jso/src/test/java/org/teavm/jso/test/FunctorTest.java b/teavm-jso/src/test/java/org/teavm/jso/test/FunctorTest.java new file mode 100644 index 000000000..1335ae741 --- /dev/null +++ b/teavm-jso/src/test/java/org/teavm/jso/test/FunctorTest.java @@ -0,0 +1,52 @@ +/* + * Copyright 2015 Alexey Andreev. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.teavm.jso.test; + +import static org.junit.Assert.*; +import org.junit.Test; +import org.teavm.jso.JSBody; +import org.teavm.jso.JSFunctor; +import org.teavm.jso.JSObject; + +/** + * + * @author Alexey Andreev + */ +public class FunctorTest { + @Test + public void functorPassed() { + assertEquals("(5)", testMethod((a, b) -> a + b, 2, 3)); + } + + @Test + public void functorIdentityPreserved() { + JSBiFunction javaFunction = (a, b) -> a + b; + JSObject firstRef = getFunction(javaFunction); + JSObject secondRef = getFunction(javaFunction); + assertSame(firstRef, secondRef); + } + + @JSBody(params = { "f", "a", "b" }, script = "return '(' + f(a, b) + ')';") + private static native String testMethod(JSBiFunction f, int a, int b); + + @JSBody(params = "f", script = "return f;") + private static native JSObject getFunction(JSBiFunction f); + + @JSFunctor + interface JSBiFunction extends JSObject { + int apply(int a, int b); + } +}