TNodeIf extends TNode { @Override public void render(TRenderContext context) throws IOException { final Optional<String> canRenderOptional = canRender(context); if (!canRenderOptional.isPresent()) { if (onlyChildren) { for (TNode child : getChildren()) { child.render(context); } } else { super.render(context); } } else { addCannontRenderComment(context, canRenderOptional); } } 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 testCommentWhenFalse() throws Exception { TNodeIf ifNode = new TNodeIf(info, TextContentParserTest.createTestTextContentParser(), "div", TNode.EMPTY_ATTRIBUTES, "item", false); ByteArrayOutputStream out = new ByteArrayOutputStream(); ifNode.render(new TRenderContext(out, Collections.<String, Object>emptyMap())); assertEquals("<!-- fmt-if=item is null -->", out.toString()); out = new ByteArrayOutputStream(); ifNode.render(new TRenderContext(out, Collections.<String, Object>singletonMap("item", false))); assertEquals("<!-- fmt-if=item is false -->", out.toString()); out = new ByteArrayOutputStream(); ifNode.render(new TRenderContext(out, Collections.<String, Object>singletonMap("item", Collections.emptyList()))); assertEquals("<!-- fmt-if=item is empty -->", out.toString()); out = new ByteArrayOutputStream(); ifNode.render(new TRenderContext(out, Collections.<String, Object>singletonMap("item", Collections.emptyMap()))); assertEquals("<!-- fmt-if=item is empty -->", out.toString()); out = new ByteArrayOutputStream(); ifNode.render(new TRenderContext(out, Collections.<String, Object>singletonMap("item", 0))); assertEquals("<!-- fmt-if=item is 0 -->", out.toString()); out = new ByteArrayOutputStream(); ifNode.render(new TRenderContext(out, Collections.<String, Object>singletonMap("item", 0.0))); assertEquals("<!-- fmt-if=item is 0 -->", out.toString()); out = new ByteArrayOutputStream(); ifNode.render(new TRenderContext(out, Collections.<String, Object>singletonMap("item", ""))); assertEquals("<!-- fmt-if=item is empty -->", out.toString()); ifNode = new TNodeIf(info, TextContentParserTest.createTestTextContentParser(), "div", TNode.EMPTY_ATTRIBUTES, "!item", false); out = new ByteArrayOutputStream(); ifNode.render(new TRenderContext(out, Collections.<String, Object>singletonMap("item", "blarg"))); assertEquals("<!-- fmt-if=!item is false (item is true) -->", out.toString()); }