Skip to content

Commit

Permalink
String variable was not recognized when passed as a param (#258)
Browse files Browse the repository at this point in the history
Co-authored-by: Fanime Fartoon <[email protected]>
  • Loading branch information
uklimaschewski and Fanime Fartoon authored May 25, 2021
1 parent 2e23b46 commit cb66e70
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 2 deletions.
3 changes: 2 additions & 1 deletion src/main/java/com/udojava/evalex/Expression.java
Original file line number Diff line number Diff line change
Expand Up @@ -1452,7 +1452,8 @@ public BigDecimal eval() {
}

public String getString() {
return token.surface;
LazyNumber lazyVariable = variables.get(token.surface);
return lazyVariable.getString();
}
});
break;
Expand Down
37 changes: 36 additions & 1 deletion src/test/java/com/udojava/evalex/TestCustoms.java
Original file line number Diff line number Diff line change
Expand Up @@ -229,4 +229,39 @@ public BigDecimal eval(BigDecimal v1, BigDecimal v2) {

assertEquals("1", e.eval().toPlainString());
}
}

@Test
public void testCustomFunctionWithStringParams() {
Expression e = new Expression("INDEXOF(STRING1, STRING2)");
e.addLazyFunction(new AbstractLazyFunction("INDEXOF", 2) {
@Override
public LazyNumber lazyEval(List<LazyNumber> lazyParams) {
String st1 = lazyParams.get(0).getString();
String st2 = lazyParams.get(1).getString();
final int index = st1.indexOf(st2);

return new LazyNumber() {
@Override
public BigDecimal eval() {
return new BigDecimal(index);
}

@Override
public String getString() {
return Integer.toString(index);
}
};
}
});

e.setVariable("STRING1", "The quick brown fox");
e.setVariable("STRING2", "The");

assertEquals("0", e.eval().toPlainString());

e.setVariable("STRING1", "The quick brown fox");
e.setVariable("STRING2", "brown");

assertEquals("10", e.eval().toPlainString());
}
}

0 comments on commit cb66e70

Please sign in to comment.