package com.mitchellbosecke.pebble;

import com.mitchellbosecke.pebble.error.PebbleException;
import com.mitchellbosecke.pebble.loader.ClasspathLoader;
import com.mitchellbosecke.pebble.loader.DelegatingLoader;
import com.mitchellbosecke.pebble.loader.StringLoader;
import com.mitchellbosecke.pebble.template.PebbleTemplate;
import org.junit.Test;

import java.io.IOException;
import java.io.StringWriter;
import java.io.Writer;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

import static org.junit.Assert.assertEquals;


public class EmbedCachingTagTest {

    private final PebbleEngine pebble;
    private final Map<String, Object> context;

    public EmbedCachingTagTest() {
        StringLoader stringLoader = new StringLoader();
        ClasspathLoader classpathLoader = new ClasspathLoader();
        classpathLoader.setPrefix("templates/embed/cache");

        pebble = new PebbleEngine.Builder()
                .loader(new DelegatingLoader(Arrays.asList(
                        classpathLoader,
                        stringLoader
                )))
                .strictVariables(false)
                .cacheActive(true)
                .build();

        Writer writer = new StringWriter();
        context = new HashMap<>();
        context.put("foo", "FOO");
        context.put("bar", "BAR");
    }

    @Test
    public void testEmbedNotChangingCachedTemplate() throws PebbleException, IOException {
        Writer writer1 = new StringWriter();
        PebbleTemplate template1 = pebble.getTemplate("template1.peb");
        template1.evaluate(writer1, context);
        String result1 = writer1.toString();

        Writer writer2 = new StringWriter();
        PebbleTemplate template2 = pebble.getTemplate("template2.peb");
        template2.evaluate(writer2, context);
        String result2 = writer2.toString();

        assertEquals("" +
                "BEFORE BASE\n" +
                "EMBED OVERRIDE\n" +
                "AFTER BASE",
                result1
        );
        assertEquals("" +
                "BEFORE BASE\n" +
                "EMBED BASE\n" +
                "AFTER BASE",
                result2
        );
    }

}

@@ -30,16 +30,108 @@
 ebble;%0A%0A
+import static java.lang.System.lineSeparator;%0Aimport static org.junit.Assert.assertEquals;%0A%0A
 import c
@@ -414,32 +414,8 @@
 te;%0A
-import org.junit.Test;%0A%0A
 impo
@@ -563,31 +563,23 @@
 il.Map;%0A
-%0A
 import 
-static 
 org.juni
@@ -580,35 +580,20 @@
 g.junit.
-Assert.assertEquals
+Test
 ;%0A%0A%0Apubl
@@ -936,16 +936,21 @@
         
+this.
 pebble =
@@ -977,20 +977,16 @@
 ilder()%0A
-    
         
@@ -1049,24 +1049,16 @@
         
-      
-  
 classpat
@@ -1082,24 +1082,16 @@
         
-        
 stringLo
@@ -1095,20 +1095,16 @@
 gLoader%0A
-    
         
@@ -1123,20 +1123,16 @@
         
-    
 .strictV
@@ -1159,20 +1159,16 @@
         
-    
 .cacheAc
@@ -1178,20 +1178,16 @@
 e(true)%0A
-    
         
@@ -1249,24 +1249,29 @@
 ();%0A        
+this.
 context = ne
@@ -1285,32 +1285,37 @@
 ap%3C%3E();%0A        
+this.
 context.put(%22foo
@@ -1333,16 +1333,21 @@
         
+this.
 context.
@@ -1553,16 +1553,21 @@
 late1 = 
+this.
 pebble.g
@@ -1631,16 +1631,21 @@
 riter1, 
+this.
 context)
@@ -1772,16 +1772,21 @@
 late2 = 
+this.
 pebble.g
@@ -1850,16 +1850,21 @@
 riter2, 
+this.
 context)
@@ -1957,37 +1957,53 @@
     %22BEFORE BASE
-%5Cn
 %22 +
+ lineSeparator() +
 %0A               
@@ -2018,19 +2018,35 @@
 OVERRIDE
-%5Cn%22
+%22 + lineSeparator()
  +%0A     
@@ -2078,28 +2078,24 @@
             
-    
 result1%0A    
@@ -2151,27 +2151,43 @@
 %22BEFORE BASE
-%5Cn%22
+%22 + lineSeparator()
  +%0A         
@@ -2208,11 +2208,27 @@
 BASE
-%5Cn%22
+%22 + lineSeparator()
  +%0A 

Fix test on windows