From 8e6b3f56977f29ecc41d6607b4af01d1155ad4ed Mon Sep 17 00:00:00 2001 From: DQinYuan <932087612@qq.com> Date: Thu, 26 Sep 2024 08:23:45 +0800 Subject: [PATCH] add object context ut --- .../aparser/QvmInstructionVisitor.java | 10 ++++++---- .../alibaba/qlexpress4/Express4RunnerTest.java | 17 +++++++++++++++++ .../com/alibaba/qlexpress4/TestSuiteRunner.java | 2 +- 3 files changed, 24 insertions(+), 5 deletions(-) diff --git a/src/main/java/com/alibaba/qlexpress4/aparser/QvmInstructionVisitor.java b/src/main/java/com/alibaba/qlexpress4/aparser/QvmInstructionVisitor.java index 6b91738c..8c7f57c9 100644 --- a/src/main/java/com/alibaba/qlexpress4/aparser/QvmInstructionVisitor.java +++ b/src/main/java/com/alibaba/qlexpress4/aparser/QvmInstructionVisitor.java @@ -496,18 +496,20 @@ public Void visitBreakContinueStatement(BreakContinueStatementContext ctx) { @Override public Void visitListExpr(ListExprContext ctx) { - ListItemsContext listItemsContext = ctx.listItems(); - ErrorReporter listErrorReporter = newReporterWithToken(ctx.getStart()); + visitListExprInner(ctx.listItems(), newReporterWithToken(ctx.getStart())); + return null; + } + + private void visitListExprInner(ListItemsContext listItemsContext, ErrorReporter listErrorReporter) { if (listItemsContext == null) { addInstruction(new ConstInstruction(listErrorReporter, new ArrayList<>())); - return null; + return; } List expressions = listItemsContext.expression(); for (ExpressionContext expression : expressions) { expression.accept(this); } addInstruction(new NewListInstruction(listErrorReporter, expressions.size())); - return null; } @Override diff --git a/src/test/java/com/alibaba/qlexpress4/Express4RunnerTest.java b/src/test/java/com/alibaba/qlexpress4/Express4RunnerTest.java index f7910ab2..9b8827dd 100644 --- a/src/test/java/com/alibaba/qlexpress4/Express4RunnerTest.java +++ b/src/test/java/com/alibaba/qlexpress4/Express4RunnerTest.java @@ -413,6 +413,23 @@ public void addMacroTest() { assertEquals(express4Runner.execute("[1,2,3,4]", new HashMap<>(), QLOptions.DEFAULT_OPTIONS), result); } + public static class MyObj { + public int a; + public String b; + } + + @Test + public void executeWithObjContextTest() { + MyObj myObj = new MyObj(); + myObj.a = 1; + myObj.b = "test"; + + Express4Runner express4Runner = new Express4Runner(InitOptions.builder() + .build()); + Object result = express4Runner.execute("a+b", myObj, QLOptions.DEFAULT_OPTIONS); + assertEquals("1test", result); + } + private void assertResultEquals(Express4Runner express4Runner, String script, Object expect) { assertResultPredicate(express4Runner, script, result -> Objects.equals(expect, result)); } diff --git a/src/test/java/com/alibaba/qlexpress4/TestSuiteRunner.java b/src/test/java/com/alibaba/qlexpress4/TestSuiteRunner.java index 6f1066cc..856c14ca 100644 --- a/src/test/java/com/alibaba/qlexpress4/TestSuiteRunner.java +++ b/src/test/java/com/alibaba/qlexpress4/TestSuiteRunner.java @@ -56,7 +56,7 @@ public void suiteTest() throws URISyntaxException, IOException { @Test public void featureDebug() throws URISyntaxException, IOException { - Path filePath = getTestSuiteRoot().resolve("java/implicit/incompatible_assignment_type.ql"); + Path filePath = getTestSuiteRoot().resolve("independent/for/return_from_for.ql"); handleFile(filePath, filePath.toString(), true); }