TNodeIf extends TNode { @Override protected Optional<String> canRender(TRenderContext context) { if (this.scriptVariables == null) { final Object val = context.getModelValue(this.expr); final String truthy = isTruthy(val); if (not) { return truthy != null ? Optional.<String>absent() : Optional.of("fmt-if=!" + this.expr + " is false (" + this.expr + " is true)"); } else { if (truthy != null) { return Optional.of("fmt-if=" + this.expr + " is " + truthy); } return Optional.absent(); } } else { Binding binding = new Binding(); for (String scriptVariable : this.scriptVariables) { binding.setVariable(scriptVariable, context.getModelValue(scriptVariable)); } GroovyShell shell = new GroovyShell(binding); Object value = shell.evaluate(this.expr); if (value instanceof Boolean && (Boolean) value) { return Optional.absent(); } else { return Optional.of(this.expr + " resulted in a non-true value: '" + value + "'"); } } } TNodeIf(SystemInfo info, TextContentParser parser, String qName, Attributes attributes, String expr, boolean onlyChildren); @Override void render(TRenderContext context); static final double PRECISION; }
@Test public void testEquals() throws Exception { TNodeIf div = new TNodeIf(null, TextContentParserTest.createTestTextContentParser(), "div", TNode.EMPTY_ATTRIBUTES, "{{label}} == 'one'", false); final ByteArrayOutputStream out = new ByteArrayOutputStream(); assertFalse(div.canRender(new TRenderContext(out, Collections.<String, Object>singletonMap("label", "one"))).isPresent()); assertTrue(div.canRender(new TRenderContext(out, Collections.<String, Object>singletonMap("label", "two"))).isPresent()); assertTrue(div.canRender(new TRenderContext(out, Collections.<String, Object>emptyMap())).isPresent()); div = new TNodeIf(null, TextContentParserTest.createTestTextContentParser(), "div", TNode.EMPTY_ATTRIBUTES, "'one' == {{label}}", false); assertFalse(div.canRender(new TRenderContext(out, Collections.<String, Object>singletonMap("label", "one"))).isPresent()); assertTrue(div.canRender(new TRenderContext(out, Collections.<String, Object>singletonMap("label", "two"))).isPresent()); assertTrue(div.canRender(new TRenderContext(out, Collections.<String, Object>emptyMap())).isPresent()); }
@Test public void testNot() throws Exception { TNodeIf not = new TNodeIf(info, TextContentParserTest.createTestTextContentParser(), "Node", TNode.EMPTY_ATTRIBUTES, "!expr", false); TRenderContext context = new TRenderContext(new ByteArrayOutputStream(), Collections.<String, Object>singletonMap("expr", true)); assertTrue(not.canRender(context).isPresent()); context = new TRenderContext(new ByteArrayOutputStream(), Collections.<String, Object>singletonMap("expr", false)); assertFalse(not.canRender(context).isPresent()); TNodeIf normal = new TNodeIf(info, TextContentParserTest.createTestTextContentParser(), "Node", TNode.EMPTY_ATTRIBUTES, "expr", false); context = new TRenderContext(new ByteArrayOutputStream(), Collections.<String, Object>singletonMap("expr", false)); assertTrue(normal.canRender(context).isPresent()); context = new TRenderContext(new ByteArrayOutputStream(), Collections.<String, Object>singletonMap("expr", true)); assertFalse(normal.canRender(context).isPresent()); }