Search in sources :

Example 1 with SelfReference

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"));
}
Also used : SelfReference(io.github.wysohn.triggerreactor.core.script.wrapper.SelfReference) 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) Test(org.junit.Test)

Example 2 with SelfReference

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());
}
Also used : SelfReference(io.github.wysohn.triggerreactor.core.script.wrapper.SelfReference) 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) Test(org.junit.Test)

Example 3 with SelfReference

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"));
}
Also used : Timings(io.github.wysohn.triggerreactor.tools.timings.Timings) SelfReference(io.github.wysohn.triggerreactor.core.script.wrapper.SelfReference) Node(io.github.wysohn.triggerreactor.core.script.parser.Node) Charset(java.nio.charset.Charset) ParserException(io.github.wysohn.triggerreactor.core.script.parser.ParserException) IOException(java.io.IOException) LexerException(io.github.wysohn.triggerreactor.core.script.lexer.LexerException) Parser(io.github.wysohn.triggerreactor.core.script.parser.Parser) Lexer(io.github.wysohn.triggerreactor.core.script.lexer.Lexer) Test(org.junit.Test)

Example 4 with SelfReference

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"));
}
Also used : SelfReference(io.github.wysohn.triggerreactor.core.script.wrapper.SelfReference) 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) Test(org.junit.Test)

Aggregations

Lexer (io.github.wysohn.triggerreactor.core.script.lexer.Lexer)4 Node (io.github.wysohn.triggerreactor.core.script.parser.Node)4 Parser (io.github.wysohn.triggerreactor.core.script.parser.Parser)4 SelfReference (io.github.wysohn.triggerreactor.core.script.wrapper.SelfReference)4 Charset (java.nio.charset.Charset)4 Test (org.junit.Test)4 LexerException (io.github.wysohn.triggerreactor.core.script.lexer.LexerException)1 ParserException (io.github.wysohn.triggerreactor.core.script.parser.ParserException)1 Timings (io.github.wysohn.triggerreactor.tools.timings.Timings)1 IOException (java.io.IOException)1