Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix for "Equalto operator doesn't work in "or" statement (== does)" #455

Merged
merged 6 commits into from
Jun 19, 2020
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 21 additions & 30 deletions src/main/java/com/hubspot/jinjava/el/ext/ExtendedParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -400,47 +400,38 @@ protected AstNode value() throws ScanException, ParseException {
) {
consumeToken(); // 'is'
consumeToken(); // 'not'
String exptestName = consumeToken().getImage();
List<AstNode> exptestParams = Lists.newArrayList(v, interpreter());

// optional exptest arg
AstNode arg = expr(false);
if (arg != null) {
exptestParams.add(arg);
}

AstProperty exptestProperty = createAstDot(
identifier(EXPTEST_PREFIX + exptestName),
"evaluateNegated",
true
);
v = createAstMethod(exptestProperty, new AstParameters(exptestParams));
v = buildAstMethodForIdentifier(v, "evaluateNegated");
} else if (
"is".equals(getToken().getImage()) && lookahead(0).getSymbol() == IDENTIFIER
) {
consumeToken(); // 'is'
String exptestName = consumeToken().getImage();
List<AstNode> exptestParams = Lists.newArrayList(v, interpreter());

// optional exptest arg
AstNode arg = expr(false);
if (arg != null) {
exptestParams.add(arg);
}

AstProperty exptestProperty = createAstDot(
identifier(EXPTEST_PREFIX + exptestName),
"evaluate",
true
);
v = createAstMethod(exptestProperty, new AstParameters(exptestParams));
v = buildAstMethodForIdentifier(v, "evaluate");
}

return v;
}
}
}

private AstNode buildAstMethodForIdentifier(AstNode astNode, String property)
throws ScanException, ParseException {
String exptestName = consumeToken().getImage();
List<AstNode> exptestParams = Lists.newArrayList(astNode, interpreter());

// optional exptest arg
AstNode arg = value();
if (arg != null) {
exptestParams.add(arg);
}

AstProperty exptestProperty = createAstDot(
identifier(EXPTEST_PREFIX + exptestName),
property,
true
);
return createAstMethod(exptestProperty, new AstParameters(exptestParams));
}

@Override
protected Scanner createScanner(String expression) {
return new ExtendedScanner(expression);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,11 @@ public void conditionalExprWithElse() {
assertThat(val("'hello' if foo=='barf' else 'hi'")).isEqualTo("hi");
}

@Test
public void conditionalExprWithOrConditionalAndCustomExpression() {
assertThat(val("'a' is equalto 'b' or 'a' is equalto 'a'")).isEqualTo(true);
}

@Test
public void newlineEscChar() {
context.put("comment", "foo\nbar");
Expand Down
171 changes: 171 additions & 0 deletions src/test/java/com/hubspot/jinjava/el/ext/ExtendedParserTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,171 @@
package com.hubspot.jinjava.el.ext;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.fail;

import de.odysseus.el.tree.impl.Builder;
import de.odysseus.el.tree.impl.ast.AstBinary;
import de.odysseus.el.tree.impl.ast.AstIdentifier;
import de.odysseus.el.tree.impl.ast.AstMethod;
import de.odysseus.el.tree.impl.ast.AstNode;
import de.odysseus.el.tree.impl.ast.AstParameters;
import de.odysseus.el.tree.impl.ast.AstString;
import org.assertj.core.api.Assertions;
import org.junit.Test;

public class ExtendedParserTest {

@Test
public void itParseSingleBinaryEqualCondition() {
AstNode astNode = buildExpressionNodes("#{'a' == 'b'}");

assertThat(astNode).isInstanceOf(AstBinary.class);
assertLeftAndRightByOperator((AstBinary) astNode, "a", "b", AstBinary.EQ);
}

@Test
public void itParseBinaryOrEqualCondition() {
AstNode astNode = buildExpressionNodes("#{'a' == 'b' or 'c' == 'd'}");

assertThat(astNode).isInstanceOf(AstBinary.class);

AstBinary astBinary = (AstBinary) astNode;
AstNode left = astBinary.getChild(0);
AstNode right = astBinary.getChild(1);

assertThat(astBinary.getOperator()).isEqualTo(OrOperator.OP);
assertThat(left).isInstanceOf(AstBinary.class);
assertThat(right).isInstanceOf(AstBinary.class);

assertLeftAndRightByOperator((AstBinary) left, "a", "b", AstBinary.EQ);
assertLeftAndRightByOperator((AstBinary) right, "c", "d", AstBinary.EQ);
}

@Test
public void itParseSingleBinaryWithExpressionCondition() {
AstNode astNode = buildExpressionNodes("#{'a' is equalto 'b'}");

assertThat(astNode).isInstanceOf(AstMethod.class);
assertForExpression(astNode, "a", "b", "exptest:equalto");
}

@Test
public void itParseBinaryOrWithEqualSymbolAndExpressionCondition() {
AstNode astNode = buildExpressionNodes("#{'a' == 'b' or 'c' is equalto 'd'}");

assertThat(astNode).isInstanceOf(AstBinary.class);

AstBinary astBinary = (AstBinary) astNode;
AstNode left = astBinary.getChild(0);
AstNode right = astBinary.getChild(1);

assertThat(astBinary.getOperator()).isEqualTo(OrOperator.OP);
assertThat(left).isInstanceOf(AstBinary.class);
assertThat(right).isInstanceOf(AstMethod.class);

assertLeftAndRightByOperator((AstBinary) left, "a", "b", AstBinary.EQ);
assertForExpression(right, "c", "d", "exptest:equalto");
}

@Test
public void itParseBinaryOrWithExpressionsCondition() {
AstNode astNode = buildExpressionNodes("#{'a' is equalto 'b' or 'c' is equalto 'd'}");

assertThat(astNode).isInstanceOf(AstBinary.class);

AstBinary astBinary = (AstBinary) astNode;
AstNode left = astBinary.getChild(0);
AstNode right = astBinary.getChild(1);

assertThat(astBinary.getOperator()).isEqualTo(OrOperator.OP);
assertThat(left).isInstanceOf(AstMethod.class);
assertThat(right).isInstanceOf(AstMethod.class);

assertForExpression(left, "a", "b", "exptest:equalto");
assertForExpression(right, "c", "d", "exptest:equalto");
}

@Test
public void itParseBinaryOrWithNegativeExpressionsCondition() {
AstNode astNode = buildExpressionNodes(
"#{'a' is not equalto 'b' or 'c' is not equalto 'd'}"
);

assertThat(astNode).isInstanceOf(AstBinary.class);

AstBinary astBinary = (AstBinary) astNode;
AstNode left = astBinary.getChild(0);
AstNode right = astBinary.getChild(1);

assertThat(astBinary.getOperator()).isEqualTo(OrOperator.OP);
assertThat(left).isInstanceOf(AstMethod.class);
assertThat(right).isInstanceOf(AstMethod.class);

assertForExpression(left, "a", "b", "exptest:equalto");
assertForExpression(right, "c", "d", "exptest:equalto");
}

private void assertForExpression(
AstNode astNode,
String leftExpected,
String rightExpected,
String expression
) {
AstIdentifier astIdentifier = (AstIdentifier) astNode.getChild(0).getChild(0);
assertThat(astIdentifier.getName()).isEqualTo(expression);

AstParameters astParameters = (AstParameters) astNode.getChild(1);
assertThat(astParameters.getChild(0)).isInstanceOf(AstString.class);
assertThat(astParameters.getChild(1)).isInstanceOf(AstIdentifier.class);
assertThat(astParameters.getChild(2)).isInstanceOf(AstString.class);

assertThat(astParameters.getChild(0).eval(null, null)).isEqualTo(leftExpected);
assertThat(((AstIdentifier) astParameters.getChild(1)).getName())
.isEqualTo("____int3rpr3t3r____");
assertThat(astParameters.getChild(2).eval(null, null)).isEqualTo(rightExpected);
}

private void assertLeftAndRightByOperator(
AstBinary astBinary,
String leftExpected,
String rightExpected,
AstBinary.Operator operator
) {
AstNode left = astBinary.getChild(0);
AstNode right = astBinary.getChild(1);

assertThat(astBinary.getOperator()).isEqualTo(operator);
assertThat(left).isInstanceOf(AstString.class);
assertThat(right).isInstanceOf(AstString.class);
assertThat(left.eval(null, null)).isEqualTo(leftExpected);
assertThat(right.eval(null, null)).isEqualTo(rightExpected);
}

private AstNode buildExpressionNodes(String input) {
ExtendedCustomParser extendedParser = new ExtendedCustomParser(new Builder(), input);
extendedParser.consumeTokenExpose();
extendedParser.consumeTokenExpose();

try {
return extendedParser.expr(true);
} catch (Exception exception) {
fail(exception.getMessage(), exception);
return null;
}
}

private static class ExtendedCustomParser extends ExtendedParser {

private ExtendedCustomParser(Builder context, String input) {
super(context, input);
}

private void consumeTokenExpose() {
try {
super.consumeToken();
} catch (Exception exception) {
Assertions.fail(exception.getMessage(), exception);
}
}
}
}
8 changes: 8 additions & 0 deletions src/test/java/com/hubspot/jinjava/interpret/DeferredTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,14 @@ public void itResolvesIfTagWherePossible() {
assertThat(interpreter.getErrors()).isEmpty();
}

@Test
public void itResolveEqualToInOrCondition() {
Joeoh marked this conversation as resolved.
Show resolved Hide resolved
String output = interpreter.render(
"{% if 'a' is equalto 'b' or 'a' is equalto 'a' %}{{deferred}}{% endif %}"
);
assertThat(output).isEqualTo("{{deferred}}");
}

@Test
public void itResolvesForTagWherePossible() {
String output = interpreter.render(
Expand Down