package net.sf.jabref.exporter.layout;

import net.sf.jabref.Globals;
import net.sf.jabref.JabRefPreferences;
import net.sf.jabref.exporter.LatexFieldFormatter;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;

import static org.junit.Assert.assertEquals;

/**
 * Created by joerg on 05.10.2015.
 */
public class LatexFieldFormatterTests {

    private LatexFieldFormatter formatter;

    @BeforeClass
    public static void setUpBeforeClass(){
        Globals.prefs = JabRefPreferences.getInstance();
    }

    @Before
    public void setUp() {
        this.formatter = new LatexFieldFormatter();
    }

    @Test
    public void preserveNewlineInAbstractField() {
        String fieldName = "abstract";
        String text = "lorem ipsum lorem ipsum\nlorem ipsum lorem ipsum\n";
        // The newlines are normalized according to the globally configured newline setting in the formatter
        // Therefore, "\n" has to be replaced by that
        text = text.replaceAll("\n", Globals.NEWLINE);

        String result = formatter.format(text, fieldName);
        String expected = "{"+text+"}";

        assertEquals(expected, result);
    }

    @Test
    public void preserveNewlineInReviewField() {
        String fieldName = "review";
        String text = "lorem ipsum lorem ipsum\nlorem ipsum lorem ipsum\n";
        // The newlines are normalized according to the globally configured newline setting in the formatter
        // Therefore, "\n" has to be replaced by that
        text = text.replaceAll("\n", Globals.NEWLINE);

        String result = formatter.format(text, fieldName);
        String expected = "{"+text+"}";

        assertEquals(expected, result);
    }

    @Test
    public void removeWhitespaceFromNonMultiLineFields() throws Exception {
        String original = "I\nshould\nnot\ninclude\nadditional\nwhitespaces  \nor\n\ttabs.";
        String expected = "{I should not include additional whitespaces or tabs.}";

        String title = formatter.format(original, "title");
        String any = formatter.format(original, "anyotherfield");

        assertEquals(expected, title);
        assertEquals(expected, any);
    }
}

@@ -658,471 +658,1645 @@
 oid 
-preserveNewlineInAbstractField() %7B%0A        String fieldName = %22abstract%22;%0A        String text = %22lorem ipsum lorem ipsum%5Cnlorem ipsum lorem ipsum%5Cn%22;%0A        // The newlines are normalized according to the globally configured newline setting in the formatter%0A        // Therefore, %22%5Cn%22 has to be replaced by that%0A        text = text.replaceAll(%22%5Cn%22, Globals.NEWLINE);%0A%0A        String result = formatter.format(text, fieldName);%0A        String expected = %22%7B%22+text+
+normalizeNewlineInAbstractField() %7B%0A        String fieldName = %22abstract%22;%0A        String text = %22lorem%22 + Globals.NEWLINE + %22 ipsum lorem ipsum%5Cnlorem ipsum lorem ipsum%5Cr%5Cntest%22;%0A%0A        // The newlines are normalized according to the globally configured newline setting in the formatter%0A        String expected = %22%7B%22 + %22lorem%22 + Globals.NEWLINE + %22 ipsum lorem ipsum%22 + Globals.NEWLINE%0A                + %22lorem ipsum lorem ipsum%22%0A                + Globals.NEWLINE + %22test%22 + %22%7D%22;%0A%0A        String result = formatter.format(text, fieldName);%0A%0A        assertEquals(expected, result);%0A    %7D%0A%0A    @Test%0A    public void preserveNewlineInAbstractField() %7B%0A        String fieldName = %22abstract%22;%0A        String text = %22lorem ipsum lorem ipsum%5Cnlorem ipsum lorem ipsum%5Cn%22;%0A        // The newlines are normalized according to the globally configured newline setting in the formatter%0A        // Therefore, %22%5Cn%22 has to be replaced by that%0A        text = text.replaceAll(%22%5Cn%22, Globals.NEWLINE);%0A%0A        String result = formatter.format(text, fieldName);%0A        String expected = %22%7B%22+text+%22%7D%22;%0A%0A        assertEquals(expected, result);%0A    %7D%0A%0A    @Test%0A    public void preserveMultipleNewlinesInAbstractField() %7B%0A        String fieldName = %22abstract%22;%0A        String text = %22lorem ipsum lorem ipsum%5Cn%5Cnlorem ipsum lorem ipsum%5Cn%22;%0A        // The newlines are normalized according to the globally configured newline setting in the formatter%0A        // Therefore, %22%5Cn%22 has to be replaced by that%0A        text = text.replaceAll(%22%5Cn%22, Globals.NEWLINE);%0A%0A        String result = formatter.format(text, fieldName);%0A        String expected = %22%7B%22 + text + 
 %22%7D%22;

Add a few tests to the field formatter checking multiline normalization