diff --git a/src/main/java/liqp/nodes/AndNode.java b/src/main/java/liqp/nodes/AndNode.java index 9b4d1324..92a1523a 100644 --- a/src/main/java/liqp/nodes/AndNode.java +++ b/src/main/java/liqp/nodes/AndNode.java @@ -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)); } } diff --git a/src/main/java/liqp/nodes/OrNode.java b/src/main/java/liqp/nodes/OrNode.java index 6c5d6e8c..fd338341 100644 --- a/src/main/java/liqp/nodes/OrNode.java +++ b/src/main/java/liqp/nodes/OrNode.java @@ -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)); } } diff --git a/src/test/java/liqp/RenderSettingsTest.java b/src/test/java/liqp/RenderSettingsTest.java index 76f6b6f7..941dc349 100644 --- a/src/test/java/liqp/RenderSettingsTest.java +++ b/src/test/java/liqp/RenderSettingsTest.java @@ -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")); + } + } }