use of io.github.wysohn.triggerreactor.core.script.wrapper.SelfReference in project TriggerReactor by wysohn.
the class TestInterpreter method testConstructorCustom.
@Test
public void testConstructorCustom() throws Exception {
Charset charset = StandardCharsets.UTF_8;
String text = "" + "IMPORT " + Vector.class.getName() + ";" + "v = Vector();" + "v2 = Vector(4,4,2);" + "v3 = Vector(4.2,4.4,2.3);" + "v4 = Vector(toFloat(3.2), toFloat(4.3), toFloat(5.4));";
Lexer lexer = new Lexer(text, charset);
Parser parser = new Parser(lexer);
Node root = parser.parse();
Map<String, Executor> executorMap = new HashMap<>();
Executor mockExecutor = mock(Executor.class);
executorMap.put("TEST", mockExecutor);
Interpreter interpreter = new Interpreter(root);
interpreter.setSelfReference(new SelfReference() {
public float toFloat(Number number) {
return number.floatValue();
}
});
interpreter.setExecutorMap(executorMap);
interpreter.startWithContext(null);
Map<String, Object> vars = interpreter.getVars();
assertEquals(new Vector(), vars.get("v"));
assertEquals(new Vector(4, 4, 2), vars.get("v2"));
assertEquals(new Vector(4.2, 4.4, 2.3), vars.get("v3"));
assertEquals(new Vector(3.2f, 4.3f, 5.4f), vars.get("v4"));
}
use of io.github.wysohn.triggerreactor.core.script.wrapper.SelfReference in project TriggerReactor by wysohn.
the class TestInterpreter method testIteration5.
@Test
public void testIteration5() throws Exception {
Charset charset = StandardCharsets.UTF_8;
String text = "" + "FOR i = 0:getPlayers().size()\n" + " #MESSAGE i\n" + "ENDFOR\n";
Lexer lexer = new Lexer(text, charset);
Parser parser = new Parser(lexer);
Node root = parser.parse();
Map<String, Executor> executorMap = new HashMap<>();
Executor executor = mock(Executor.class);
when(executor.execute(any(), anyBoolean(), anyMap(), any(), anyInt())).thenReturn(null);
executorMap.put("MESSAGE", executor);
Interpreter interpreter = new Interpreter(root);
interpreter.setExecutorMap(executorMap);
interpreter.setSelfReference(new SelfReference() {
public Collection<String> getPlayers() {
List<String> names = new ArrayList<>();
for (int i = 0; i < 10; i++) names.add(String.valueOf(i));
return names;
}
});
interpreter.startWithContext(null);
verify(executor, times(10)).execute(any(), anyBoolean(), anyMap(), any(), anyInt());
}
use of io.github.wysohn.triggerreactor.core.script.wrapper.SelfReference in project TriggerReactor by wysohn.
the class TestInterpreter method testArrayAndClass.
@Test
public void testArrayAndClass() throws Exception {
Set<String> set = new HashSet<>();
Charset charset = StandardCharsets.UTF_8;
String text = "" + "IMPORT " + TestEnum.class.getName() + ";" + "enumVal = TestEnum.IMTEST;" + "arr = array(1);" + "arr[0] = enumVal;" + "#TEST arr[0];";
Lexer lexer = new Lexer(text, charset);
Parser parser = new Parser(lexer);
Node root = parser.parse();
Map<String, Executor> executorMap = new HashMap<>();
executorMap.put("TEST", new Executor() {
@Override
public Integer execute(Timings.Timing timing, boolean sync, Map<String, Object> vars, Object context, Object... args) throws Exception {
set.add("test");
assertEquals(TestEnum.IMTEST, args[0]);
return null;
}
});
Interpreter interpreter = new Interpreter(root);
interpreter.setExecutorMap(executorMap);
interpreter.setSelfReference(new SelfReference() {
@SuppressWarnings("unused")
public Object array(int size) {
return new Object[size];
}
});
interpreter.startWithContext(null);
Assert.assertTrue(set.contains("test"));
}
use of io.github.wysohn.triggerreactor.core.script.wrapper.SelfReference in project TriggerReactor by wysohn.
the class TestInterpreter method testNestedAccessor.
@Test
public void testNestedAccessor() throws Exception {
Charset charset = StandardCharsets.UTF_8;
String text = "" + "IMPORT java.lang.Long;" + "id = Long.valueOf(\"123456789123456789\").longValue()";
Lexer lexer = new Lexer(text, charset);
Parser parser = new Parser(lexer);
Node root = parser.parse();
Map<String, Executor> executorMap = new HashMap<>();
Interpreter interpreter = new Interpreter(root);
interpreter.setExecutorMap(executorMap);
interpreter.setSelfReference(new SelfReference() {
@SuppressWarnings("unused")
public Object array(int size) {
return new Object[size];
}
});
interpreter.startWithContext(null);
assertEquals(123456789123456789L, interpreter.getVars().get("id"));
}
Aggregations