-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
🧪 test: Add TsClass, TsMethodArgument
- Loading branch information
Showing
6 changed files
with
848 additions
and
901 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
127 changes: 127 additions & 0 deletions
127
src/test/java/com/caoccao/javet/buddy/ts2java/TsClass.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
48 changes: 48 additions & 0 deletions
48
src/test/java/com/caoccao/javet/buddy/ts2java/TsMethodArgument.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.