use of io.github.wysohn.triggerreactor.core.script.interpreter.Interpreter in project TriggerReactor by wysohn.
the class TriggerTest method testNestedIf2.
@Test
public void testNestedIf2() throws Exception {
Charset charset = Charset.forName("UTF-8");
String text = "x = 4.0;" + "IF x < 0.0;" + " #TEST \"no\";" + "ELSEIF x < -5.0;" + " #TEST \"no\";" + "ELSE;" + " #TEST \"pass\";" + "ENDIF;";
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
protected Integer execute(boolean sync, Object context, Object... args) throws Exception {
Assert.assertEquals("pass", args[0]);
return null;
}
});
Map<String, Placeholder> placeholderMap = new HashMap<>();
Interpreter interpreter = new Interpreter(root, executorMap, placeholderMap, new HashMap<String, Object>(), new HashMap<>(), new CommonFunctions(null));
interpreter.startWithContext(null);
}
use of io.github.wysohn.triggerreactor.core.script.interpreter.Interpreter in project TriggerReactor by wysohn.
the class TriggerTest method testSimpleTrigger.
@Test
public void testSimpleTrigger() throws Exception {
Charset charset = Charset.forName("UTF-8");
int ecoValue = 10;
String text = "" + "IF command == \"iron\"\n" + " IF takeItem(player, 265, 1)\n" + " #SOUND \"LEVEL_UP\",1.0,-2.0,player.getLocation()\n" + " #CMDCON \"econ add \"+player.getName()+\" " + ecoValue + "\"\n" + " #MESSAGE \"Sold!\"\n" + " ELSE\n" + " #MESSAGE \"not enough iron.\"\n" + " ENDIF\n" + "ENDIF\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) {
if (!takeItem) {
Assert.assertEquals("not enough iron.", (String) args[0]);
} else {
Assert.assertEquals("Sold!", (String) args[0]);
}
return null;
}
});
Location mockLocation = mock(Location.class);
executorMap.put("SOUND", new Executor() {
@Override
public Integer execute(boolean sync, Object context, Object... args) {
Assert.assertEquals("LEVEL_UP", args[0]);
Assert.assertEquals(1.0, args[1]);
Assert.assertEquals(-2.0, args[2]);
Assert.assertEquals(mockLocation, args[3]);
return null;
}
});
String playerName = "TestPlayer";
executorMap.put("CMDCON", new Executor() {
@Override
public Integer execute(boolean sync, Object context, Object... args) {
if (takeItem)
Assert.assertEquals("econ add " + playerName + " " + ecoValue, args[0]);
return null;
}
});
Interpreter interpreter = new Interpreter(root, executorMap, new HashMap<>(), new HashMap<String, Object>(), new HashMap<>(), new CommonFunctions(null));
Player mockPlayer = mock(Player.class);
PlayerInventory mockInven = mock(PlayerInventory.class);
when(mockPlayer.getInventory()).thenReturn(mockInven);
when(mockPlayer.getLocation()).thenReturn(mockLocation);
when(mockPlayer.getName()).thenReturn(playerName);
interpreter.getVars().put("player", mockPlayer);
interpreter.getVars().put("text", "hello");
interpreter.getVars().put("command", "iron");
takeItem = false;
when(mockInven.containsAtLeast(Mockito.any(ItemStack.class), Mockito.anyInt())).thenReturn(takeItem);
interpreter.startWithContext(null);
System.out.println();
takeItem = true;
when(mockInven.containsAtLeast(Mockito.any(ItemStack.class), Mockito.anyInt())).thenReturn(takeItem);
interpreter.startWithContext(null);
}
use of io.github.wysohn.triggerreactor.core.script.interpreter.Interpreter in project TriggerReactor by wysohn.
the class TriggerTest method currentAreaTest.
@Test
public void currentAreaTest() throws Exception {
Player mockPlayer = mock(Player.class);
CommonFunctions mockFunctions = mock(CommonFunctions.class);
Charset charset = Charset.forName("UTF-8");
String text = "areaName = currentArea(player)\n" + "IF areaName == \"tutorialArea\"\n" + "#MESSAGE \"Not valid in here.\"\n" + "ELSE\n" + "#GUI \"menu\"\n" + "ENDIF\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("Not valid in here.", String.valueOf(args[0]));
return null;
}
});
executorMap.put("GUI", new Executor() {
@Override
protected Integer execute(boolean sync, Object context, Object... args) {
Assert.assertEquals("menu", String.valueOf(args[0]));
return null;
}
});
Map<String, Object> map = new HashMap<String, Object>();
Interpreter interpreter;
when(mockFunctions.currentArea(Mockito.any(Player.class))).thenReturn("tutorialArea");
interpreter = new Interpreter(root, executorMap, new HashMap<>(), map, new HashMap<>(), mockFunctions);
interpreter.getVars().put("player", mockPlayer);
interpreter.startWithContext(null);
}
use of io.github.wysohn.triggerreactor.core.script.interpreter.Interpreter in project TriggerReactor by wysohn.
the class TriggerTest method testNegation.
@Test
public void testNegation() throws Exception {
Charset charset = Charset.forName("UTF-8");
String text = "" + "arr = array(6)\n" + "arr[0] = true\n" + "arr[1] = !true\n" + "arr[2] = !true || false\n" + "arr[3] = true && !false\n" + "arr[4] = true && 1 < 2 && 5 > 4 && 1 != 2 && 2 == 2 && (false || 2*2 > 3)\n" + "arr[5] = false || false || (2 < 3 && 6+5*3 > 1*2+3)";
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, executorMap, new HashMap<>(), new HashMap<String, Object>(), new HashMap<>(), new CommonFunctions(null));
interpreter.startWithContext(null);
Object arr = interpreter.getVars().get("arr");
Assert.assertTrue((boolean) Array.get(arr, 0));
Assert.assertFalse((boolean) Array.get(arr, 1));
Assert.assertFalse((boolean) Array.get(arr, 2));
Assert.assertTrue((boolean) Array.get(arr, 3));
Assert.assertTrue((boolean) Array.get(arr, 4));
Assert.assertTrue((boolean) Array.get(arr, 5));
}
use of io.github.wysohn.triggerreactor.core.script.interpreter.Interpreter in project TriggerReactor by wysohn.
the class TriggerTest method testNestedIfNoElse.
@Test
public void testNestedIfNoElse() throws Exception {
Charset charset = Charset.forName("UTF-8");
String text = "x = 4.0;" + "IF x < 0.0;" + " #TEST \"no\";" + "ELSEIF x > 0.0;" + " #TEST \"pass\";" + "ENDIF;";
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
protected Integer execute(boolean sync, Object context, Object... args) throws Exception {
Assert.assertEquals("pass", args[0]);
return null;
}
});
Map<String, Placeholder> placeholderMap = new HashMap<>();
Interpreter interpreter = new Interpreter(root, executorMap, placeholderMap, new HashMap<String, Object>(), new HashMap<>(), new CommonFunctions(null));
interpreter.startWithContext(null);
}
Aggregations