Skip to content

Commit

Permalink
🧪 test: Add TsClass, TsMethodArgument
Browse files Browse the repository at this point in the history
  • Loading branch information
caoccao committed Nov 3, 2024
1 parent e4f2a53 commit 75f25cc
Show file tree
Hide file tree
Showing 6 changed files with 848 additions and 901 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,5 @@ protected String getTsCode(String relativePath) throws IOException {
.filter(line -> !line.startsWith("console."))
.collect(Collectors.joining("\n"));
}

}
127 changes: 127 additions & 0 deletions src/test/java/com/caoccao/javet/buddy/ts2java/TsClass.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
/*
* Copyright (c) 2024. caoccao.com Sam Cao
*
* 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 com.caoccao.javet.buddy.ts2java;

import com.caoccao.javet.swc4j.exceptions.Swc4jCoreException;
import com.caoccao.javet.swc4j.utils.SimpleList;
import com.caoccao.javet.utils.SimpleFreeMarkerFormat;
import com.caoccao.javet.utils.SimpleMap;

import java.lang.reflect.Method;
import java.lang.reflect.Parameter;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

import static org.junit.jupiter.api.Assertions.*;

public final class TsClass {
private final List<TsMethodArgument> arguments;
private final String body;
private final Class<?> returnType;
private Object instance;
private Class<?> testClass;
private Method testMethod;

public TsClass(String body, Class<?> returnType, TsMethodArgument... arguments) {
assertNotNull(body);
assertNotNull(returnType);
assertNotNull(arguments);
this.arguments = SimpleList.immutableOf(arguments);
this.body = body;
this.returnType = returnType;
instance = null;
testClass = null;
testMethod = null;
init();
}

public List<TsMethodArgument> getArguments() {
return arguments;
}

public String getBody() {
return body;
}

public Object getInstance() throws Exception {
if (instance == null) {
instance = testClass.getConstructor().newInstance();
}
return instance;
}

public Class<?> getReturnType() {
return returnType;
}

public Class<?> getTestClass() {
return testClass;
}

public Method getTestMethod() {
return testMethod;
}

private void init() {
String argumentsString = arguments.stream()
.map(TsMethodArgument::toString)
.collect(Collectors.joining(", "));
String codeString = SimpleFreeMarkerFormat.format(
"class Test {\n" +
" public test(${argumentsString}): ${returnType} {\n" +
" ${body}\n" +
" }\n" +
"}\n",
SimpleMap.of(
"argumentsString", argumentsString,
"returnType", returnType.getName(),
"body", body));
Ts2Java ts2Java = new Ts2Java("com.test", codeString);
try {
ts2Java.transpile();
} catch (Swc4jCoreException e) {
fail(e);
}
List<Class<?>> classes = ts2Java.getClasses();
assertEquals(1, classes.size());
testClass = classes.get(0);
assertEquals("Test", testClass.getSimpleName());
assertEquals("com.test.Test", testClass.getName());
try {
testMethod = testClass.getMethod(
"test",
arguments.stream().map(TsMethodArgument::getType).toArray(Class[]::new));
assertEquals(returnType, testMethod.getReturnType());
final int argumentsLength = arguments.size();
final Parameter[] parameters = testMethod.getParameters();
assertEquals(argumentsLength, testMethod.getParameterCount());
assertEquals(argumentsLength, parameters.length);
IntStream.range(0, argumentsLength)
.forEach(i -> assertEquals(
arguments.get(i).getType(),
parameters[i].getType(),
"Argument[" + i + "] type mismatch"));
} catch (NoSuchMethodException e) {
fail(e);
}
}

public Object invoke(Object... arguments) throws Exception {
return testMethod.invoke(getInstance(), arguments);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* Copyright (c) 2024. caoccao.com Sam Cao
*
* 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 com.caoccao.javet.buddy.ts2java;

import static org.junit.jupiter.api.Assertions.assertNotNull;

public final class TsMethodArgument {
private final String name;
private final Class<?> type;

public TsMethodArgument(String name, Class<?> type) {
assertNotNull(name);
assertNotNull(type);
this.name = name;
this.type = type;
}

public static TsMethodArgument of(String name, Class<?> type) {
return new TsMethodArgument(name, type);
}

public String getName() {
return name;
}

public Class<?> getType() {
return type;
}

@Override
public String toString() {
return name + ": " + type.getName();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,24 +17,13 @@
package com.caoccao.javet.buddy.ts2java.ast;

import com.caoccao.javet.buddy.ts2java.BaseTestTs2Java;
import com.caoccao.javet.buddy.ts2java.Ts2Java;
import com.caoccao.javet.swc4j.exceptions.Swc4jCoreException;
import com.caoccao.javet.buddy.ts2java.TsClass;
import com.caoccao.javet.buddy.ts2java.TsMethodArgument;
import org.junit.jupiter.api.Test;

import java.io.IOException;
import java.lang.reflect.Method;
import java.util.List;

import static org.junit.jupiter.api.Assertions.*;
import static org.junit.jupiter.api.Assertions.assertEquals;

public class TestAssignments extends BaseTestTs2Java {
protected static Class<?> clazz = null;

public TestAssignments() {
super();
init();
}

/*
public assignAndCast(IJ)D
L0
Expand Down Expand Up @@ -105,52 +94,29 @@ public long assignConst(int a, long b) {
return a + b + c + d;
}

protected void init() {
if (clazz == null) {
String tsCode = null;
try {
tsCode = getTsCode("test.assignments.ts");
} catch (IOException e) {
fail(e);
}
assertNotNull(tsCode);
Ts2Java ts2Java = new Ts2Java("com.test", tsCode);
try {
ts2Java.transpile();
} catch (Swc4jCoreException e) {
fail(e);
}
List<Class<?>> classes = ts2Java.getClasses();
assertEquals(1, classes.size());
clazz = classes.get(0);
assertEquals("Test", clazz.getSimpleName());
assertEquals("com.test.Test", clazz.getName());
}
}

@Test
public void testAssignAndCast() throws Exception {
assertEquals(3.0D, assignAndCast(1, 2L), 0.001D);
Method method = clazz.getMethod("assignAndCast", int.class, long.class);
assertNotNull(method);
assertEquals(double.class, method.getReturnType());
assertEquals(2, method.getParameterCount());
assertEquals(int.class, method.getParameters()[0].getType());
assertEquals(long.class, method.getParameters()[1].getType());
Object object = clazz.getConstructor().newInstance();
assertEquals(3.0D, (double) method.invoke(object, 1, 2L), 0.001D);
TsClass tsClass = new TsClass(
"let c: long = a;\n" +
"let d: long = b;\n" +
"return c + d;",
double.class,
TsMethodArgument.of("a", int.class),
TsMethodArgument.of("b", long.class));
assertEquals(3.0D, (double) tsClass.invoke(1, 2L), 0.001D);
}

@Test
public void testAssignConst() throws Exception {
assertEquals(105L, assignConst(1, 2L));
Method method = clazz.getMethod("assignConst", int.class, long.class);
assertNotNull(method);
assertEquals(long.class, method.getReturnType());
assertEquals(2, method.getParameterCount());
assertEquals(int.class, method.getParameters()[0].getType());
assertEquals(long.class, method.getParameters()[1].getType());
Object object = clazz.getConstructor().newInstance();
assertEquals(105L, method.invoke(object, 1, 2L));
TsClass tsClass = new TsClass(
"const c: int = 100;\n" +
"const d: long = 2;\n" +
"return a + b + c + d;",
long.class,
TsMethodArgument.of("a", int.class),
TsMethodArgument.of("b", long.class));
assertEquals(105L, tsClass.invoke(1, 2L));
}
}
Loading

0 comments on commit 75f25cc

Please sign in to comment.