/* * def test_blankspace * template = Liquid::Template.parse(" ") * assert_equal [" "], template.root.nodelist * end */ @Test public void blankSpaceTest() throws RecognitionException { CommonTree root = Template.parse(" ").getAST(); assertThat(getChildText(root), is(" ")); }
@Test public void applyTest() throws RecognitionException { String json = "{ \"txt\" : \"a b c d e f g h i j a b c d e f g h i j\" }"; String[][] tests = { {"{{ nil | truncatewords }}", ""}, {"{{ txt | truncatewords }}", "a b c d e f g h i j a b c d e..."}, {"{{ txt | truncatewords: 5 }}", "a b c d e..."}, {"{{ txt | truncatewords: 5, '???' }}", "a b c d e???"}, {"{{ txt | truncatewords: 500, '???' }}", "a b c d e f g h i j a b c d e f g h i j"}, {"{{ txt | truncatewords: 2, '===' }}", "a b==="}, {"{{ txt | truncatewords: 19, '===' }}", "a b c d e f g h i j a b c d e f g h i==="}, {"{{ txt | truncatewords: 20, '===' }}", "a b c d e f g h i j a b c d e f g h i j"}, {"{{ txt | truncatewords: 21, '===' }}", "a b c d e f g h i j a b c d e f g h i j"}, }; for (String[] test : tests) { Template template = Template.parse(test[0]); String rendered = String.valueOf(template.render(json)); assertThat(rendered, is(test[1])); } }
/* * def test_variable_end * template = Liquid::Template.parse(" {{funk}}") * assert_equal 2, template.root.nodelist.size * assert_equal String, template.root.nodelist[0].class * assert_equal Variable, template.root.nodelist[1].class * end */ @Test public void variableEndTest() throws RecognitionException { CommonTree root = Template.parse(" {{funk}}").getAST(); assertThat(root.getChildCount(), is(2)); assertThat(root.getChild(0).getType(), is(LiquidLexer.PLAIN)); assertThat(root.getChild(1).getChild(0).getType(), is(LiquidLexer.LOOKUP)); }
/* * def test_with_block * template = Liquid::Template.parse(" {% comment %} {% endcomment %} ") * assert_equal [String, Comment, String], block_types(template.root.nodelist) * assert_equal 3, template.root.nodelist.size * end */ @Test public void blockTest() throws RecognitionException { CommonTree root = Template.parse(" {% comment %} {% endcomment %} ").getAST(); assertThat(root.getChildCount(), is(3)); assertThat(root.getChild(0).getType(), is(LiquidLexer.PLAIN)); assertThat(root.getChild(1).getType(), is(LiquidLexer.COMMENT)); assertThat(root.getChild(2).getType(), is(LiquidLexer.PLAIN)); }
/* * def test_with_custom_tag * Liquid::Template.register_tag("testtag", Block) * * assert_nothing_thrown do * template = Liquid::Template.parse( "{% testtag %} {% endtesttag %}") * end * end */ @Test public void customTagTest() throws RecognitionException { Tag.registerTag( new Tag("testtag") { @Override public Object render(RenderingContext context, LNode... nodes) { return null; } }); Template.parse("{% testtag %} {% endtesttag %}").render(); }
/* * def test_variable_many_embedded_fragments * template = Liquid::Template.parse(" {{funk}} {{so}} {{brother}} ") * assert_equal 7, template.root.nodelist.size * assert_equal [String, Variable, String, Variable, String, Variable, String], * block_types(template.root.nodelist) * end */ @Test public void variableManyEmbeddedFragmentsTest() throws RecognitionException { CommonTree root = Template.parse(" {{funk}} {{so}} {{brother}} ").getAST(); assertThat(root.getChildCount(), is(7)); assertThat(root.getChild(0).getType(), is(LiquidLexer.PLAIN)); assertThat(root.getChild(1).getChild(0).getType(), is(LiquidLexer.LOOKUP)); assertThat(root.getChild(2).getType(), is(LiquidLexer.PLAIN)); assertThat(root.getChild(3).getChild(0).getType(), is(LiquidLexer.LOOKUP)); assertThat(root.getChild(4).getType(), is(LiquidLexer.PLAIN)); assertThat(root.getChild(5).getChild(0).getType(), is(LiquidLexer.LOOKUP)); assertThat(root.getChild(6).getType(), is(LiquidLexer.PLAIN)); }
@Test public void applyTest() throws RecognitionException { String[][] tests = { {"{% if 1.0 != 1 %}TRUE{% else %}FALSE{% endif %}", "FALSE"}, {"{% if nil != nil %}TRUE{% else %}FALSE{% endif %}", "FALSE"}, {"{% if false != false %}TRUE{% else %}FALSE{% endif %}", "FALSE"}, {"{% if \"\" != '' %}TRUE{% else %}FALSE{% endif %}", "FALSE"}, }; for (String[] test : tests) { Template template = Template.parse(test[0]); String rendered = String.valueOf(template.render()); assertThat(rendered, is(test[1])); } }