Skip to content

Commit

Permalink
Add unit tests for expressions (#12262)
Browse files Browse the repository at this point in the history
* checkpoint

* Checkpoint2

* Broken Checkpoint

* Green

* rename test

* Clean up

* Cleanup

* Checkpoint

* Update

* cleanup

* failing test

* Fix

* sbt javafmt

* Fix

* sbt javafmtAll

* fmt
  • Loading branch information
AdRiley authored Feb 13, 2025
1 parent 28a1b03 commit fea3957
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 11 deletions.
8 changes: 7 additions & 1 deletion build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -618,6 +618,7 @@ val guavaVersion = "32.0.0-jre"
val jgitVersion = "6.7.0.202309050840-r"
val kindProjectorVersion = "0.13.3"
val mockitoScalaVersion = "1.17.14"
val mockitoJavaVersion = "5.15.2"
val newtypeVersion = "0.4.4"
val pprintVersion = "0.8.1"
val pureconfigVersion = "0.17.4"
Expand Down Expand Up @@ -4841,13 +4842,18 @@ lazy val `std-table` = project
},
libraryDependencies ++= Seq(
"org.graalvm.polyglot" % "polyglot" % graalMavenPackagesVersion % "provided",
"org.graalvm.truffle" % "truffle-api" % graalMavenPackagesVersion % "provided",
"org.netbeans.api" % "org-openide-util-lookup" % netbeansApiVersion % "provided",
"com.univocity" % "univocity-parsers" % univocityParsersVersion,
"org.apache.poi" % "poi-ooxml" % poiOoxmlVersion,
"org.apache.xmlbeans" % "xmlbeans" % xmlbeansVersion,
"org.antlr" % "antlr4-runtime" % antlrVersion,
"org.apache.logging.log4j" % "log4j" % "2.24.3",
"org.apache.logging.log4j" % "log4j-to-slf4j" % "2.24.3" // org.apache.poi uses log4j
"org.apache.logging.log4j" % "log4j-to-slf4j" % "2.24.3", // org.apache.poi uses log4j
"junit" % "junit" % junitVersion % Test,
"com.github.sbt" % "junit-interface" % junitIfVersion % Test,
"org.mockito" % "mockito-core" % mockitoJavaVersion % Test,
"org.mockito" % "mockito-junit-jupiter" % mockitoJavaVersion % Test
),
Compile / packageBin := Def.task {
val result = (Compile / packageBin).value
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,29 @@ public static Value evaluate(
String typeName,
String[] variableArgumentFunctions)
throws UnsupportedOperationException, IllegalArgumentException {
var context = Context.getCurrent().getBindings("enso");
final Value module = context.invokeMember("get_module", moduleName);
final Value type = module.invokeMember("get_type", typeName);
Function<String, Value> getMethod = name -> module.invokeMember("get_method", type, name);
Function<String, Value> makeConstructor =
name -> module.invokeMember("eval_expression", ".." + name);

return evaluateImpl(
expression,
getColumn,
makeConstantColumn,
getMethod,
makeConstructor,
variableArgumentFunctions);
}

public static Value evaluateImpl(
String expression,
Function<String, Value> getColumn,
Function<Object, Value> makeConstantColumn,
Function<String, Value> getMethod,
Function<String, Value> makeConstructor,
String[] variableArgumentFunctions) {
var lexer = new ExpressionLexer(CharStreams.fromString(expression));
lexer.removeErrorListeners();
lexer.addErrorListener(ThrowOnErrorListener.INSTANCE);
Expand All @@ -75,7 +98,7 @@ public static Value evaluate(

var visitor =
new ExpressionVisitorImpl(
getColumn, makeConstantColumn, moduleName, typeName, variableArgumentFunctions);
getColumn, makeConstantColumn, getMethod, makeConstructor, variableArgumentFunctions);

var expr = parser.prog();
return visitor.visit(expr);
Expand All @@ -90,18 +113,13 @@ public static Value evaluate(
private ExpressionVisitorImpl(
Function<String, Value> getColumn,
Function<Object, Value> makeConstantColumn,
String moduleName,
String typeName,
Function<String, Value> getMethod,
Function<String, Value> makeConstructor,
String[] variableArgumentFunctions) {
this.getColumn = getColumn;
this.makeConstantColumn = makeConstantColumn;

var context = Context.getCurrent().getBindings("enso");
final Value module = context.invokeMember("get_module", moduleName);
final Value type = module.invokeMember("get_type", typeName);
getMethod = name -> module.invokeMember("get_method", type, name);
makeConstructor = name -> module.invokeMember("eval_expression", ".." + name);

this.getMethod = getMethod;
this.makeConstructor = makeConstructor;
this.variableArgumentFunctions = new HashSet<>(Arrays.asList(variableArgumentFunctions));
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package org.enso.table.expressions;

import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

import java.util.function.Function;
import org.graalvm.polyglot.Value;
import org.junit.Rule;
import org.junit.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnit;
import org.mockito.junit.MockitoRule;
import org.mockito.junit.jupiter.MockitoExtension;
import org.mockito.quality.Strictness;

@ExtendWith(MockitoExtension.class)
public class ExpressionVisitorImplTest {

@Rule public MockitoRule mockitoRule = MockitoJUnit.rule().strictness(Strictness.STRICT_STUBS);

@Mock private Function<String, Value> getColumn;
@Mock private Function<Object, Value> makeConstantColumn;
@Mock private Function<String, Value> getMethod;
@Mock private Function<String, Value> makeConstructor;

private Value evaluate(String expr) {
return ExpressionVisitorImpl.evaluateImpl(
expr, getColumn, makeConstantColumn, getMethod, makeConstructor, new String[] {});
}

@Test
public void testIntegerConstant() {
Value result = evaluate("1");
assertEquals(1, result.asInt());
}

@Test
public void testSimpleMethodOnColumn() {
Value mockedColumn1 = mock(Value.class);
Value mockedMethodTextLength = mock(Value.class);
Value mockedResult = mock(Value.class);

when(getColumn.apply("Column 1")).thenReturn(mockedColumn1);
when(getMethod.apply("text_length")).thenReturn(mockedMethodTextLength);
when(mockedMethodTextLength.canExecute()).thenReturn(true);
when(mockedMethodTextLength.execute(mockedColumn1)).thenReturn(mockedResult);
when(makeConstantColumn.apply(mockedResult)).thenReturn(mockedResult);

Value result = evaluate("text_length([Column 1])");
assertEquals(mockedResult, result);
}
}

0 comments on commit fea3957

Please sign in to comment.