Search in sources :

Example 11 with Interpreter

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);
}
Also used : Placeholder(io.github.wysohn.triggerreactor.core.script.interpreter.Placeholder) Interpreter(io.github.wysohn.triggerreactor.core.script.interpreter.Interpreter) HashMap(java.util.HashMap) Node(io.github.wysohn.triggerreactor.core.script.parser.Node) Charset(java.nio.charset.Charset) Parser(io.github.wysohn.triggerreactor.core.script.parser.Parser) Lexer(io.github.wysohn.triggerreactor.core.script.lexer.Lexer) Executor(io.github.wysohn.triggerreactor.core.script.interpreter.Executor) CommonFunctions(io.github.wysohn.triggerreactor.bukkit.manager.trigger.share.CommonFunctions) Test(org.junit.Test)

Example 12 with Interpreter

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);
}
Also used : Player(org.bukkit.entity.Player) Interpreter(io.github.wysohn.triggerreactor.core.script.interpreter.Interpreter) HashMap(java.util.HashMap) Node(io.github.wysohn.triggerreactor.core.script.parser.Node) Charset(java.nio.charset.Charset) PlayerInventory(org.bukkit.inventory.PlayerInventory) Parser(io.github.wysohn.triggerreactor.core.script.parser.Parser) Lexer(io.github.wysohn.triggerreactor.core.script.lexer.Lexer) Executor(io.github.wysohn.triggerreactor.core.script.interpreter.Executor) ItemStack(org.bukkit.inventory.ItemStack) CommonFunctions(io.github.wysohn.triggerreactor.bukkit.manager.trigger.share.CommonFunctions) Location(org.bukkit.Location) Test(org.junit.Test)

Example 13 with Interpreter

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);
}
Also used : Player(org.bukkit.entity.Player) Interpreter(io.github.wysohn.triggerreactor.core.script.interpreter.Interpreter) HashMap(java.util.HashMap) Node(io.github.wysohn.triggerreactor.core.script.parser.Node) Charset(java.nio.charset.Charset) Parser(io.github.wysohn.triggerreactor.core.script.parser.Parser) Lexer(io.github.wysohn.triggerreactor.core.script.lexer.Lexer) Executor(io.github.wysohn.triggerreactor.core.script.interpreter.Executor) CommonFunctions(io.github.wysohn.triggerreactor.bukkit.manager.trigger.share.CommonFunctions) Test(org.junit.Test)

Example 14 with Interpreter

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));
}
Also used : Interpreter(io.github.wysohn.triggerreactor.core.script.interpreter.Interpreter) HashMap(java.util.HashMap) Node(io.github.wysohn.triggerreactor.core.script.parser.Node) Charset(java.nio.charset.Charset) Parser(io.github.wysohn.triggerreactor.core.script.parser.Parser) Lexer(io.github.wysohn.triggerreactor.core.script.lexer.Lexer) Executor(io.github.wysohn.triggerreactor.core.script.interpreter.Executor) CommonFunctions(io.github.wysohn.triggerreactor.bukkit.manager.trigger.share.CommonFunctions) Test(org.junit.Test)

Example 15 with Interpreter

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);
}
Also used : Placeholder(io.github.wysohn.triggerreactor.core.script.interpreter.Placeholder) Interpreter(io.github.wysohn.triggerreactor.core.script.interpreter.Interpreter) HashMap(java.util.HashMap) Node(io.github.wysohn.triggerreactor.core.script.parser.Node) Charset(java.nio.charset.Charset) Parser(io.github.wysohn.triggerreactor.core.script.parser.Parser) Lexer(io.github.wysohn.triggerreactor.core.script.lexer.Lexer) Executor(io.github.wysohn.triggerreactor.core.script.interpreter.Executor) CommonFunctions(io.github.wysohn.triggerreactor.bukkit.manager.trigger.share.CommonFunctions) Test(org.junit.Test)

Aggregations

CommonFunctions (io.github.wysohn.triggerreactor.bukkit.manager.trigger.share.CommonFunctions)21 Executor (io.github.wysohn.triggerreactor.core.script.interpreter.Executor)21 Interpreter (io.github.wysohn.triggerreactor.core.script.interpreter.Interpreter)21 Lexer (io.github.wysohn.triggerreactor.core.script.lexer.Lexer)21 Node (io.github.wysohn.triggerreactor.core.script.parser.Node)21 Parser (io.github.wysohn.triggerreactor.core.script.parser.Parser)21 Charset (java.nio.charset.Charset)21 HashMap (java.util.HashMap)21 Test (org.junit.Test)21 Placeholder (io.github.wysohn.triggerreactor.core.script.interpreter.Placeholder)7 Player (org.bukkit.entity.Player)4 ArrayList (java.util.ArrayList)2 Collection (java.util.Collection)2 Location (org.bukkit.Location)1 ItemStack (org.bukkit.inventory.ItemStack)1 PlayerInventory (org.bukkit.inventory.PlayerInventory)1