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

Use short-circuit evaluation at And and Or operators. #77

Merged
merged 1 commit into from
Mar 3, 2018
Merged
Show file tree
Hide file tree
Changes from all 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
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"));
}
}
}