Repository: PebbleTemplates/pebble
Pull request #332 (2018-04-14 19:10:12)

fix for issue308
Error message added "endblock tag should be present with block tag starting line number" , if endblock does not exist with block.


Patch:
diff --git a/.gitignore b/.gitignore
index 0e4ac806..733840d1 100644
--- a/.gitignore
+++ b/.gitignore
@@ -21,3 +21,4 @@
 
 target/*
 /target
+/bin/
diff --git a/src/main/java/com/mitchellbosecke/pebble/tokenParser/BlockTokenParser.java b/src/main/java/com/mitchellbosecke/pebble/tokenParser/BlockTokenParser.java
index 8c3b8edd..5adcb3b7 100644
--- a/src/main/java/com/mitchellbosecke/pebble/tokenParser/BlockTokenParser.java
+++ b/src/main/java/com/mitchellbosecke/pebble/tokenParser/BlockTokenParser.java
@@ -56,7 +56,12 @@ public class BlockTokenParser extends AbstractTokenParser {
         });
         parser.popBlockStack();
 
-        // skip the 'endblock' token
+
+        //check endblock us exist with block or not
+        Token endblock = stream.current();
+        if(!endblock.test(Token.Type.NAME, "endblock")) throw new ParserException(null, "endblock tag should be present with block tag starting line number ",
+        		token.getLineNumber(), stream.getFilename());
+     // skip the 'endblock' token
         stream.next();
 
         // check if user included block name in endblock


Test patch:
diff --git a/src/test/java/com/mitchellbosecke/pebble/RenderWithoutEndBlockTest.java b/src/test/java/com/mitchellbosecke/pebble/RenderWithoutEndBlockTest.java
new file mode 100644
index 00000000..2a2d3363
--- /dev/null
+++ b/src/test/java/com/mitchellbosecke/pebble/RenderWithoutEndBlockTest.java
@@ -0,0 +1,175 @@
+package com.mitchellbosecke.pebble;
+
+import static java.util.Locale.CANADA;
+import static org.junit.Assert.assertEquals;
+
+import java.io.IOException;
+import java.io.StringWriter;
+import java.io.Writer;
+import java.util.HashMap;
+import java.util.Map;
+
+import org.junit.Test;
+
+import com.mitchellbosecke.pebble.error.ParserException;
+import com.mitchellbosecke.pebble.error.PebbleException;
+import com.mitchellbosecke.pebble.loader.StringLoader;
+import com.mitchellbosecke.pebble.template.PebbleTemplate;
+
+public class RenderWithoutEndBlockTest extends AbstractTest {
+	@Test(expected = ParserException.class)
+    public void testRenderWithoutEndBlock() throws PebbleException, IOException {
+        PebbleEngine pebble = new PebbleEngine.Builder().loader(new StringLoader()).strictVariables(false).build();
+
+        String source = "Prefix {% block block_a %}Block A{% block block_b %}Block B{% endblock %} Postfix";
+        PebbleTemplate template = pebble.getTemplate(source);
+
+        Writer writer_a = new StringWriter();
+        template.evaluateBlock("block_a", writer_a);
+        
+     
+    }
+	
+	@Test
+    public void testRenderWithEndBlock() throws PebbleException, IOException {
+        PebbleEngine pebble = new PebbleEngine.Builder().loader(new StringLoader()).strictVariables(false).build();
+
+        String source = "Prefix {% block block_a %}Block A{% endblock %}{% block block_b %}Block B{% endblock %} Postfix";
+        PebbleTemplate template = pebble.getTemplate(source);
+
+        Writer writer_a = new StringWriter();
+        template.evaluateBlock("block_a", writer_a);
+        assertEquals("Block A", writer_a.toString());
+
+        Writer writer_b = new StringWriter();
+        template.evaluateBlock("block_b", writer_b);
+        assertEquals("Block B", writer_b.toString());
+
+    }
+
+	@Test(expected = ParserException.class)
+    public void testRenderWithoutEndBlockWithLocale() throws PebbleException, IOException {
+        PebbleEngine pebble = new PebbleEngine.Builder().loader(new StringLoader()).strictVariables(false).build();
+
+        String source = "Prefix {% block block_a %}Block A{% block block_b %}Block B{% endblock %} Postfix";
+        PebbleTemplate template = pebble.getTemplate(source);
+
+        Writer writer_a = new StringWriter();
+        template.evaluateBlock("block_a", writer_a, CANADA);
+        assertEquals("Block A", writer_a.toString());
+
+        Writer writer_b = new StringWriter();
+        template.evaluateBlock("block_b", writer_b, CANADA);
+        assertEquals("Block B", writer_b.toString());
+    }
+	
+	@Test
+    public void testRenderWithEndBlockWithLocale() throws PebbleException, IOException {
+        PebbleEngine pebble = new PebbleEngine.Builder().loader(new StringLoader()).strictVariables(false).build();
+
+        String source = "Prefix {% block block_a %}Block A{% endblock %}{% block block_b %}Block B{% endblock %} Postfix";
+        PebbleTemplate template = pebble.getTemplate(source);
+
+        Writer writer_a = new StringWriter();
+        template.evaluateBlock("block_a", writer_a, CANADA);
+        assertEquals("Block A", writer_a.toString());
+
+        Writer writer_b = new StringWriter();
+        template.evaluateBlock("block_b", writer_b, CANADA);
+        assertEquals("Block B", writer_b.toString());
+    }
+
+    @Test(expected = ParserException.class)
+    public void testRenderWithoutEndBlockWithContext() throws PebbleException, IOException {
+        PebbleEngine pebble = new PebbleEngine.Builder().loader(new StringLoader()).strictVariables(false).build();
+
+        String source = "Prefix {% block block_a %}{{vara}}{% endblock %}{% block block_b %}{{varb}} Postfix";
+        PebbleTemplate template = pebble.getTemplate(source);
+
+        Map<String, Object> context = new HashMap<>();
+        context.put("vara", "FOO");
+        context.put("varb", "BAR");
+
+        Writer writer_a = new StringWriter();
+        template.evaluateBlock("block_a", writer_a, context, CANADA);
+        assertEquals("FOO", writer_a.toString());
+
+        Writer writer_b = new StringWriter();
+        template.evaluateBlock("block_b", writer_b, context, CANADA);
+        assertEquals("BAR", writer_b.toString());
+    }
+    @Test
+    public void testRenderEndBlockWithContext() throws PebbleException, IOException {
+        PebbleEngine pebble = new PebbleEngine.Builder().loader(new StringLoader()).strictVariables(false).build();
+
+        String source = "Prefix {% block block_a %}{{vara}}{% endblock %}{% block block_b %}{{varb}}{% endblock %} Postfix";
+        PebbleTemplate template = pebble.getTemplate(source);
+
+        Map<String, Object> context = new HashMap<>();
+        context.put("vara", "FOO");
+        context.put("varb", "BAR");
+
+        Writer writer_a = new StringWriter();
+        template.evaluateBlock("block_a", writer_a, context, CANADA);
+        assertEquals("FOO", writer_a.toString());
+
+        Writer writer_b = new StringWriter();
+        template.evaluateBlock("block_b", writer_b, context, CANADA);
+        assertEquals("BAR", writer_b.toString());
+    }
+
+
+   @Test(expected = ParserException.class)
+    public void testRenderWithoutEndBlockWithContextAndLocale() throws PebbleException, IOException {
+        PebbleEngine pebble = new PebbleEngine.Builder().loader(new StringLoader()).strictVariables(false).build();
+
+        String source = "Prefix {% block block_a %}{{vara}}{% block block_b %}{{varb}}{% endblock %} Postfix";
+        PebbleTemplate template = pebble.getTemplate(source);
+
+        Map<String, Object> context = new HashMap<>();
+        context.put("vara", "FOO");
+        context.put("varb", "BAR");
+
+        Writer writer_a = new StringWriter();
+        template.evaluateBlock("block_a", writer_a, context);
+        assertEquals("FOO", writer_a.toString());
+
+        Writer writer_b = new StringWriter();
+        template.evaluateBlock("block_b", writer_b, context);
+        assertEquals("BAR", writer_b.toString());
+    }
+    @Test
+    public void testRenderWithEndBlockWithContextAndLocale() throws PebbleException, IOException {
+        PebbleEngine pebble = new PebbleEngine.Builder().loader(new StringLoader()).strictVariables(false).build();
+
+        String source = "Prefix {% block block_a %}{{vara}}{% endblock %}{% block block_b %}{{varb}}{% endblock %} Postfix";
+        PebbleTemplate template = pebble.getTemplate(source);
+
+        Map<String, Object> context = new HashMap<>();
+        context.put("vara", "FOO");
+        context.put("varb", "BAR");
+
+        Writer writer_a = new StringWriter();
+        template.evaluateBlock("block_a", writer_a, context);
+        assertEquals("FOO", writer_a.toString());
+
+        Writer writer_b = new StringWriter();
+        template.evaluateBlock("block_b", writer_b, context);
+        assertEquals("BAR", writer_b.toString());
+    }
+
+    @Test
+    public void testRenderWithoutEndBlockTest() throws PebbleException, IOException {
+        PebbleEngine pebble = new PebbleEngine.Builder().strictVariables(true).build();
+        PebbleTemplate template = pebble.getTemplate("templates/single-block/template.renderextendedblock1.peb");
+
+        Writer writer_a = new StringWriter();
+        template.evaluateBlock("container_a", writer_a);
+        assertEquals("Block A extended", writer_a.toString());
+
+        Writer writer_b = new StringWriter();
+        template.evaluateBlock("container_b", writer_b);
+        assertEquals("Block B extended", writer_b.toString());
+    }
+
+}