/*******************************************************************************
 * This file is part of Pebble.
 * <p>
 * Copyright (c) 2014 by Mitchell Bösecke
 * <p>
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 ******************************************************************************/
package com.mitchellbosecke.pebble;

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.PebbleException;
import com.mitchellbosecke.pebble.loader.StringLoader;
import com.mitchellbosecke.pebble.template.PebbleTemplate;

public class NewlineTrimmingTest extends AbstractTest {

    @Test
    public void testPrintDefault() throws PebbleException, IOException {

        PebbleEngine pebble = new PebbleEngine.Builder().loader(new StringLoader())
                                                        .build();

        PebbleTemplate template = pebble.getTemplate("{{param1}}\n{{param2}}");

        Writer writer = new StringWriter();

        Map<String, Object> params = new HashMap<String, Object>();
        params.put("param1", "val1");
        params.put("param2", "val2");

        template.evaluate(writer, params);

        assertEquals("val1val2", writer.toString());
    }

    @Test
    public void testPrintForceToTrue() throws PebbleException, IOException {

        PebbleEngine pebble = new PebbleEngine.Builder().loader(new StringLoader())
                                                        .enableNewLineTrimming(true)
                                                        .build();

        PebbleTemplate template = pebble.getTemplate("{{param1}}\n{{param2}}");

        Writer writer = new StringWriter();

        Map<String, Object> params = new HashMap<String, Object>();
        params.put("param1", "val1");
        params.put("param2", "val2");

        template.evaluate(writer, params);

        assertEquals("val1val2", writer.toString());
    }

    @Test
    public void testPrintSetToFalse() throws PebbleException, IOException {

        PebbleEngine pebble = new PebbleEngine.Builder().loader(new StringLoader())
                                                        .enableNewLineTrimming(false)
                                                        .build();

        PebbleTemplate template = pebble.getTemplate("{{param1}}\n{{param2}}");

        Writer writer = new StringWriter();

        Map<String, Object> params = new HashMap<String, Object>();
        params.put("param1", "val1");
        params.put("param2", "val2");

        template.evaluate(writer, params);

        assertEquals("val1\nval2", writer.toString());
    }
    
    @Test
    public void testPrintDefaultTwoNewlines() throws PebbleException, IOException {

        PebbleEngine pebble = new PebbleEngine.Builder().loader(new StringLoader())
                                                        .build();

        PebbleTemplate template = pebble.getTemplate("{{param1}}\n\n{{param2}}");

        Writer writer = new StringWriter();

        Map<String, Object> params = new HashMap<String, Object>();
        params.put("param1", "val1");
        params.put("param2", "val2");

        template.evaluate(writer, params);

        assertEquals("val1\nval2", writer.toString());
    }
    
    @Test
    public void testPrintSetToFalseTwoNewlines() throws PebbleException, IOException {

        PebbleEngine pebble = new PebbleEngine.Builder().loader(new StringLoader())
                                                        .enableNewLineTrimming(false)
                                                        .build();

        PebbleTemplate template = pebble.getTemplate("{{param1}}\n}\n{{param2}}");

        Writer writer = new StringWriter();

        Map<String, Object> params = new HashMap<String, Object>();
        params.put("param1", "val1");
        params.put("param2", "val2");

        template.evaluate(writer, params);

        assertEquals("val1\n}\nval2", writer.toString());
    }

    @Test
    public void testCommentDefault() throws PebbleException, IOException {

        PebbleEngine pebble = new PebbleEngine.Builder().loader(new StringLoader())
                                                        .build();

        PebbleTemplate template = pebble.getTemplate("{# comment1 #}\n{# comment2 #}");

        Writer writer = new StringWriter();

        template.evaluate(writer);

        assertEquals("", writer.toString());
    }

    @Test
    public void testCommentForceToTrue() throws PebbleException, IOException {

        PebbleEngine pebble = new PebbleEngine.Builder().loader(new StringLoader())
                                                        .enableNewLineTrimming(true)
                                                        .build();

        PebbleTemplate template = pebble.getTemplate("{# comment1 #}\n{# comment2 #}");

        Writer writer = new StringWriter();

        template.evaluate(writer);

        assertEquals("", writer.toString());
    }

    @Test
    public void testCommentSetToFalse() throws PebbleException, IOException {

        PebbleEngine pebble = new PebbleEngine.Builder().loader(new StringLoader())
                                                        .enableNewLineTrimming(false)
                                                        .build();

        PebbleTemplate template = pebble.getTemplate("{# comment1 #}\n{# comment2 #}");

        Writer writer = new StringWriter();

        template.evaluate(writer);

        assertEquals("\n", writer.toString());
    }

    @Test
    public void testExecuteDefault() throws PebbleException, IOException {

        PebbleEngine pebble = new PebbleEngine.Builder().loader(new StringLoader())
                                                        .build();

        PebbleTemplate template = pebble.getTemplate("{% if true %}\n{% endif %}");

        Writer writer = new StringWriter();

        template.evaluate(writer);

        assertEquals("", writer.toString());
    }

    @Test
    public void testExecuteForceToTrue() throws PebbleException, IOException {

        PebbleEngine pebble = new PebbleEngine.Builder().loader(new StringLoader())
                                                        .enableNewLineTrimming(true)
                                                        .build();

        PebbleTemplate template = pebble.getTemplate("{% if true %}\n{% endif %}");

        Writer writer = new StringWriter();

        template.evaluate(writer);

        assertEquals("", writer.toString());
    }

    @Test
    public void testExecuteSetToFalse() throws PebbleException, IOException {

        PebbleEngine pebble = new PebbleEngine.Builder().loader(new StringLoader())
                                                        .enableNewLineTrimming(false)
                                                        .build();

        PebbleTemplate template = pebble.getTemplate("{% if true %}\n{% endif %}");

        Writer writer = new StringWriter();

        template.evaluate(writer);

        assertEquals("\n", writer.toString());
    }
    

}

@@ -3879,17 +3879,16 @@
 ram1%7D%7D%5Cn
-%7D
 %5Cn%7B%7Bpara
@@ -4158,17 +4158,16 @@
 (%22val1%5Cn
-%7D
 %5Cnval2%22,

Test fix