use of io.github.wysohn.triggerreactor.core.script.parser.Node in project TriggerReactor by wysohn.
the class TriggerTest method testIteration2.
@Test
public void testIteration2() throws Exception {
CommonFunctions mockFunctions = mock(CommonFunctions.class);
Player mockPlayer1 = mock(Player.class);
Player mockPlayer2 = mock(Player.class);
Charset charset = Charset.forName("UTF-8");
String text = "" + "FOR i = 0:10\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<>();
executorMap.put("MESSAGE", new Executor() {
int index = 0;
@Override
public Integer execute(boolean sync, Object context, Object... args) {
Assert.assertEquals(index++, args[0]);
return null;
}
});
Collection players = new ArrayList<Player>() {
{
add(mockPlayer1);
add(mockPlayer2);
}
};
when(mockFunctions.getPlayers()).thenReturn(players);
Map<String, Object> map = new HashMap<String, Object>();
Interpreter interpreter = new Interpreter(root, executorMap, new HashMap<>(), map, new HashMap<>(), mockFunctions);
interpreter.startWithContext(null);
}
use of io.github.wysohn.triggerreactor.core.script.parser.Node in project TriggerReactor by wysohn.
the class TriggerTest method testRandom.
@Test
public void testRandom() throws Exception {
Charset charset = Charset.forName("UTF-8");
String text = "" + "rand = common.random(3);" + "IF rand == 0\n" + "#MESSAGE 0\n" + "ENDIF;" + "IF rand == 1;\n" + "#MESSAGE 1;\n" + "ENDIF\n" + "IF rand == 2;" + "#MESSAGE 2\n" + "ENDIF\n";
Lexer lexer = new Lexer(text, charset);
Parser parser = new Parser(lexer);
Node root = parser.parse();
Map<String, Executor> executorMap = new HashMap<>();
Executor mockExecutor = new Executor() {
@Override
public Integer execute(boolean sync, Object context, Object... args) {
String value = String.valueOf(args[0]);
Assert.assertTrue("0".equals(value) || "1".equals(value) || "2".equals(value));
return null;
}
};
executorMap.put("MESSAGE", mockExecutor);
Interpreter interpreter = new Interpreter(root, executorMap, new HashMap<>(), new HashMap<String, Object>(), new HashMap<>(), new CommonFunctions(null));
interpreter.getVars().put("common", new CommonFunctions(null));
interpreter.startWithContext(null);
}
use of io.github.wysohn.triggerreactor.core.script.parser.Node in project TriggerReactor by wysohn.
the class TriggerTest method testCustomArray.
@Test
public void testCustomArray() throws Exception {
Charset charset = Charset.forName("UTF-8");
String text = "" + "args = array(2)\n" + "args[0] = \"arg1\"\n" + "args[1] = \"arg2\"\n" + "#MESSAGE args[0]+\", \"+args[1*-1*-1+1-1--1-1]\n";
Lexer lexer = new Lexer(text, charset);
Parser parser = new Parser(lexer);
Node root = parser.parse();
Map<String, Executor> executorMap = new HashMap<>();
executorMap.put("MESSAGE", new Executor() {
@Override
public Integer execute(boolean sync, Object context, Object... args) {
Assert.assertEquals("arg1, arg2", args[0]);
return null;
}
});
CommonFunctions mockFunctions = mock(CommonFunctions.class);
Map<String, Object> map = new HashMap<String, Object>();
Interpreter interpreter = new Interpreter(root, executorMap, new HashMap<>(), map, new HashMap<>(), new CommonFunctions(null));
interpreter.startWithContext(null);
}
use of io.github.wysohn.triggerreactor.core.script.parser.Node in project TriggerReactor by wysohn.
the class TriggerTest method testPlaceholder.
@Test
public void testPlaceholder() throws Exception {
Charset charset = Charset.forName("UTF-8");
String text = "x = 100.0;" + "returnvalue = $test:0:x:true:\"hoho\";" + "#MESSAGE $playername returnvalue;" + "#TESTSTRING $string;" + "#TESTINTEGER $integer;" + "#TESTDOUBLE $double;" + "#TESTBOOLEAN $boolean;";
Lexer lexer = new Lexer(text, charset);
Parser parser = new Parser(lexer);
Node root = parser.parse();
Map<String, Executor> executorMap = new HashMap<>();
executorMap.put("MESSAGE", new Executor() {
@Override
protected Integer execute(boolean sync, Object context, Object... args) throws Exception {
Assert.assertEquals("testplayer", args[0]);
Assert.assertEquals("testwithargs", args[1]);
return null;
}
});
executorMap.put("TESTSTRING", new Executor() {
@Override
protected Integer execute(boolean sync, Object context, Object... args) throws Exception {
Assert.assertTrue(args[0] instanceof String);
return null;
}
});
executorMap.put("TESTINTEGER", new Executor() {
@Override
protected Integer execute(boolean sync, Object context, Object... args) throws Exception {
Assert.assertTrue(args[0] instanceof Integer);
return null;
}
});
executorMap.put("TESTDOUBLE", new Executor() {
@Override
protected Integer execute(boolean sync, Object context, Object... args) throws Exception {
Assert.assertTrue(args[0] instanceof Double);
return null;
}
});
executorMap.put("TESTBOOLEAN", new Executor() {
@Override
protected Integer execute(boolean sync, Object context, Object... args) throws Exception {
Assert.assertTrue(args[0] instanceof Boolean);
return null;
}
});
Map<String, Placeholder> placeholderMap = new HashMap<>();
placeholderMap.put("playername", new Placeholder() {
@Override
public Object parse(Object context, Object... args) throws Exception {
return "testplayer";
}
});
placeholderMap.put("test", new Placeholder() {
@Override
public Object parse(Object context, Object... args) throws Exception {
Assert.assertEquals(0, args[0]);
Assert.assertEquals(100.0, args[1]);
Assert.assertEquals(true, args[2]);
Assert.assertEquals("hoho", args[3]);
return "testwithargs";
}
});
placeholderMap.put("string", new Placeholder() {
@Override
public Object parse(Object context, Object... args) throws Exception {
return "testplayer";
}
});
placeholderMap.put("integer", new Placeholder() {
@Override
public Object parse(Object context, Object... args) throws Exception {
return 1;
}
});
placeholderMap.put("double", new Placeholder() {
@Override
public Object parse(Object context, Object... args) throws Exception {
return 1.5;
}
});
placeholderMap.put("boolean", new Placeholder() {
@Override
public Object parse(Object context, Object... args) throws Exception {
return false;
}
});
Interpreter interpreter = new Interpreter(root, executorMap, placeholderMap, new HashMap<String, Object>(), new HashMap<>(), new CommonFunctions(null));
interpreter.startWithContext(null);
Assert.assertEquals("testwithargs", interpreter.getVars().get("returnvalue"));
}
use of io.github.wysohn.triggerreactor.core.script.parser.Node in project TriggerReactor by wysohn.
the class TriggerTest method testUnaryMinus.
@Test
public void testUnaryMinus() throws Exception {
Charset charset = Charset.forName("UTF-8");
String text = "x = 4.0;" + "#TEST1 -1+-5;" + "#TEST2 -2.0--5;" + "#TEST3 -$test3-5;" + "#TEST4 -x-5;";
Lexer lexer = new Lexer(text, charset);
Parser parser = new Parser(lexer);
Node root = parser.parse();
Map<String, Executor> executorMap = new HashMap<>();
executorMap.put("TEST1", new Executor() {
@Override
protected Integer execute(boolean sync, Object context, Object... args) throws Exception {
Assert.assertEquals(-6, args[0]);
return null;
}
});
executorMap.put("TEST2", new Executor() {
@Override
protected Integer execute(boolean sync, Object context, Object... args) throws Exception {
Assert.assertEquals(3.0, args[0]);
return null;
}
});
executorMap.put("TEST3", new Executor() {
@Override
protected Integer execute(boolean sync, Object context, Object... args) throws Exception {
Assert.assertEquals(-8, args[0]);
return null;
}
});
executorMap.put("TEST4", new Executor() {
@Override
protected Integer execute(boolean sync, Object context, Object... args) throws Exception {
Assert.assertEquals(-9.0, args[0]);
return null;
}
});
Map<String, Placeholder> placeholderMap = new HashMap<>();
placeholderMap.put("test3", new Placeholder() {
@Override
public Object parse(Object context, Object... args) throws Exception {
return 3;
}
});
Interpreter interpreter = new Interpreter(root, executorMap, placeholderMap, new HashMap<String, Object>(), new HashMap<>(), new CommonFunctions(null));
interpreter.startWithContext(null);
}
Aggregations