Add tests for JSFunctor

This commit is contained in:
Alexey Andreev 2015-09-13 19:44:35 +03:00
parent 5fca860b70
commit 53d784ee7b
2 changed files with 53 additions and 0 deletions

View File

@ -56,6 +56,7 @@ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xs
<artifactId>maven-checkstyle-plugin</artifactId>
<configuration>
<configLocation>../checkstyle.xml</configLocation>
<includeTestSourceDirectory>true</includeTestSourceDirectory>
</configuration>
</plugin>
<plugin>

View File

@ -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);
}
}