Skip to content

Commit

Permalink
Merge pull request #77 from bmateusz/short-circuit-and-or
Browse files Browse the repository at this point in the history
Use short-circuit evaluation at And and Or operators.
  • Loading branch information
bkiers authored Mar 3, 2018
2 parents 1bcabf6 + d5123d1 commit c2cbbba
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 8 deletions.
5 changes: 1 addition & 4 deletions src/main/java/liqp/nodes/AndNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,7 @@ public AndNode(LNode lhs, LNode rhs) {
@Override
public Object render(TemplateContext context) {

Object a = lhs.render(context);
Object b = rhs.render(context);

return super.asBoolean(a) && super.asBoolean(b);
return super.asBoolean(lhs.render(context)) && super.asBoolean(rhs.render(context));

}
}
5 changes: 1 addition & 4 deletions src/main/java/liqp/nodes/OrNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,7 @@ public OrNode(LNode lhs, LNode rhs) {
@Override
public Object render(TemplateContext context) {

Object a = lhs.render(context);
Object b = rhs.render(context);

return super.asBoolean(a) || super.asBoolean(b);
return super.asBoolean(lhs.render(context)) || super.asBoolean(rhs.render(context));

}
}
62 changes: 62 additions & 0 deletions src/test/java/liqp/RenderSettingsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,66 @@ public void renderWithStrictVariables2() {
assertThat(e.getVariableName(), is("qwe.asd.zxc"));
}
}

@Test
public void renderWithStrictVariablesInCondition1() {
Template.parse("{% if mu == \"somethingElse\" %}{{ badVariableName }}{% endif %}")
.withRenderSettings(new RenderSettings.Builder().withStrictVariables(true).build())
.render("mu", "muValue");
}

@Test
public void renderWithStrictVariablesInCondition2() {
try {
Template.parse("{% if mu == \"muValue\" %}{{ badVariableName }}{% endif %}")
.withRenderSettings(new RenderSettings.Builder().withStrictVariables(true).build())
.render("mu", "muValue");
} catch (RuntimeException ex) {
VariableNotExistException e = (VariableNotExistException) TestUtils.getExceptionRootCause(ex);
assertThat(e.getVariableName(), is("badVariableName"));
}
}

@Test
public void renderWithStrictVariablesInAnd1() {
try {
Template.parse("{% if mu == \"muValue\" and checkThis %}{{ badVariableName }}{% endif %}")
.withRenderSettings(new RenderSettings.Builder().withStrictVariables(true).build())
.render("mu", "muValue");
} catch (RuntimeException ex) {
VariableNotExistException e = (VariableNotExistException) TestUtils.getExceptionRootCause(ex);
assertThat(e.getVariableName(), is("checkThis"));
}
}

@Test
public void renderWithStrictVariablesInAnd2() {
Template.parse("{% if mu == \"somethingElse\" and doNotCheckThis %}{{ badVariableName }}{% endif %}")
.withRenderSettings(new RenderSettings.Builder().withStrictVariables(true).build())
.render("mu", "muValue");
}

@Test
public void renderWithStrictVariablesInOr1() {
try {
Template.parse("{% if mu == \"muValue\" or doNotCheckThis %}{{ badVariableName }}{% endif %}")
.withRenderSettings(new RenderSettings.Builder().withStrictVariables(true).build())
.render("mu", "muValue");
} catch (RuntimeException ex) {
VariableNotExistException e = (VariableNotExistException) TestUtils.getExceptionRootCause(ex);
assertThat(e.getVariableName(), is("badVariableName"));
}
}

@Test
public void renderWithStrictVariablesInOr2() {
try {
Template.parse("{% if mu == \"somethingElse\" or checkThis %}{{ badVariableName }}{% endif %}")
.withRenderSettings(new RenderSettings.Builder().withStrictVariables(true).build())
.render("mu", "muValue");
} catch (RuntimeException ex) {
VariableNotExistException e = (VariableNotExistException) TestUtils.getExceptionRootCause(ex);
assertThat(e.getVariableName(), is("checkThis"));
}
}
}

0 comments on commit c2cbbba

Please sign in to comment.