feat: test code gen
This commit is contained in:
@@ -0,0 +1,40 @@
|
||||
package org.innercore.icstd;
|
||||
|
||||
import com.zhekasmirnov.innercore.api.gen.CodeGenBindGenBase;
|
||||
import org.innercore.icstd.annotations.ApiBinding;
|
||||
|
||||
@ApiBinding
|
||||
public class CodeGen extends CodeGenBindGenBase {
|
||||
@ApiBinding public int test;
|
||||
|
||||
@ApiBinding
|
||||
public CodeGen() {
|
||||
this.test = 5;
|
||||
}
|
||||
|
||||
@ApiBinding
|
||||
public int sum(int a, int b) {
|
||||
return a + b;
|
||||
}
|
||||
|
||||
@ApiBinding
|
||||
public int sum(int a, int b, int c) {
|
||||
return a + b + c;
|
||||
}
|
||||
|
||||
@ApiBinding
|
||||
public String other(String a, int b, CodeGen c) {
|
||||
return a + b + c.test;
|
||||
}
|
||||
|
||||
@ApiBinding
|
||||
public static int staticSum(int a, int b) {
|
||||
return a + b;
|
||||
}
|
||||
|
||||
@ApiBinding
|
||||
public static int staticSum(int a, int b, int c) {
|
||||
return a + b + c;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
package org.innercore.icstd.extension;
|
||||
|
||||
import org.junit.jupiter.api.extension.AfterTestExecutionCallback;
|
||||
import org.junit.jupiter.api.extension.BeforeTestExecutionCallback;
|
||||
import org.junit.jupiter.api.extension.ExtensionContext;
|
||||
|
||||
public class TimingExtension implements BeforeTestExecutionCallback, AfterTestExecutionCallback {
|
||||
private static final String START_TIME = "start time";
|
||||
|
||||
@Override
|
||||
public void beforeTestExecution(ExtensionContext context) throws Exception {
|
||||
context.getStore(ExtensionContext.Namespace.GLOBAL)
|
||||
.put(START_TIME, System.currentTimeMillis());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void afterTestExecution(ExtensionContext context) throws Exception {
|
||||
long startTime = context.getStore(ExtensionContext.Namespace.GLOBAL)
|
||||
.remove(START_TIME, long.class);
|
||||
System.out.printf("End test %s - %d ms%n", context.getDisplayName(), System.currentTimeMillis() - startTime);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
package org.innercore.icstd.test;
|
||||
|
||||
import com.zhekasmirnov.innercore.api.gen.CodeGenBindGenBase;
|
||||
import org.innercore.icstd.extension.TimingExtension;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
import org.mozilla.javascript.Function;
|
||||
import org.mozilla.javascript.Scriptable;
|
||||
|
||||
@ExtendWith(TimingExtension.class)
|
||||
public class CodeGenTest {
|
||||
@Test
|
||||
public void test() {
|
||||
Scriptable api = CodeGenBindGenBase.createEmptyModuleObject();
|
||||
CodeGenBindGenBase.injectJsStatic(api, true);
|
||||
|
||||
Function staticSum = (Function) api.get("staticSum", api);
|
||||
|
||||
assert ((Number) staticSum.call(null, null, null, new Object[]{5, 5, 5, 5})).intValue() == 15;
|
||||
assert ((Number) staticSum.call(null, null, null, new Object[]{5, 5, 5})).intValue() == 15;
|
||||
assert ((Number) staticSum.call(null, null, null, new Object[]{5, 5})).intValue() == 10;
|
||||
assert ((Number) staticSum.call(null, null, null, new Object[]{5, null, 2f})).intValue() == 7;
|
||||
assert ((Number) staticSum.call(null, null, null, new Object[]{"5", "5"})).intValue() == 10;
|
||||
|
||||
Scriptable self = ((Function) api).construct(null, null, new Object[]{});
|
||||
|
||||
Function sum = (Function) self.get("sum", self);
|
||||
|
||||
assert ((Number) sum.call(null, null, self, new Object[]{5, 5, 5, 5})).intValue() == 15;
|
||||
assert ((Number) sum.call(null, null, self, new Object[]{5, 5, 5})).intValue() == 15;
|
||||
assert ((Number) sum.call(null, null, self, new Object[]{5, 5})).intValue() == 10;
|
||||
assert ((Number) sum.call(null, null, self, new Object[]{5, null, 2f})).intValue() == 7;
|
||||
assert ((Number) sum.call(null, null, self, new Object[]{"5", "5"})).intValue() == 10;
|
||||
|
||||
Function other = (Function) self.get("other", self);
|
||||
|
||||
assert (other.call(null, null, self, new Object[]{"test", "5", self})).toString().equals("test55");
|
||||
|
||||
assert ((Number) self.get("test", self)).intValue() == 5;
|
||||
self.put("test", self, 1);
|
||||
|
||||
assert (other.call(null, null, self, new Object[]{"test", "5", self})).toString().equals("test51");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user