package org.fxmisc.richtext.model;

import static org.junit.Assert.*;
import org.junit.Test;

public class SimpleEditableStyledDocumentTest {

    private final TextOps<StyledText<String>, String> segOps = StyledText.textOps();

    /**
     * The style of the inserted text will be the style at position
     * {@code start} in the current document.
     */
    private <PS> void replaceText(EditableStyledDocument<PS, StyledText<String>, String> doc, int start, int end, String text) {
        StyledDocument<PS, StyledText<String>, String> styledDoc = ReadOnlyStyledDocument.fromString(
                text, doc.getParagraphStyleAtPosition(start), doc.getStyleAtPosition(start), segOps);
        doc.replace(start, end, styledDoc);
    }

    @Test
    public void testConsistencyOfTextWithLength() {
        SimpleEditableStyledDocument<String, String> document = new SimpleEditableStyledDocument<>("", "");
        document.getText(); // enforce evaluation of text property
        document.getLength(); // enforce evaluation of length property

        document.lengthProperty().addListener(obs -> {
            int length = document.getLength();
            int textLength = document.getText().length();
            assertEquals(length, textLength);
        });

        replaceText(document, 0, 0, "A");
    }

    @Test
    public void testConsistencyOfLengthWithText() {
        SimpleEditableStyledDocument<String, String> document = new SimpleEditableStyledDocument<>("", "");
        document.getText(); // enforce evaluation of text property
        document.getLength(); // enforce evaluation of length property

        document.textProperty().addListener(obs -> {
            int textLength = document.getText().length();
            int length = document.getLength();
            assertEquals(textLength, length);
        });

        replaceText(document, 0, 0, "A");
    }

    @Test
    public void testUnixParagraphCount() {
        SimpleEditableStyledDocument<String, String> document = new SimpleEditableStyledDocument<>("", "");
        String text = "X\nY";
        replaceText(document, 0, 0, text);
        assertEquals(2, document.getParagraphs().size());
    }

    @Test
    public void testMacParagraphCount() {
        SimpleEditableStyledDocument<String, String> document = new SimpleEditableStyledDocument<>("", "");
        String text = "X\rY";
        replaceText(document, 0, 0, text);
        assertEquals(2, document.getParagraphs().size());
    }

    @Test
    public void testWinParagraphCount() {
        SimpleEditableStyledDocument<String, String> document = new SimpleEditableStyledDocument<>("", "");
        String text = "X\r\nY";
        replaceText(document, 0, 0, text);
        assertEquals(2, document.getParagraphs().size());
    }

    @Test
    public void testGetTextWithEndAfterNewline() {
        SimpleEditableStyledDocument<Boolean, String> doc = new SimpleEditableStyledDocument<>(true, "");

        replaceText(doc, 0, 0, "123\n");
        String txt1 = doc.getText(0, 4);
        assertEquals(4, txt1.length());

        replaceText(doc, 4, 4, "567");
        String txt2 = doc.getText(2, 4);
        assertEquals(2, txt2.length());

        replaceText(doc, 4, 4, "\n");
        String txt3 = doc.getText(2, 4);
        assertEquals(2, txt3.length());
    }

    @Test
    public void testWinDocumentLength() {
        SimpleEditableStyledDocument<String, String> document = new SimpleEditableStyledDocument<>("", "");
        replaceText(document, 0, 0, "X\r\nY");
        assertEquals(document.getText().length(), document.getLength());
    }
}

@@ -3608,11 +3608,728 @@
 );%0A    %7D
+%0A%0A    @Test%0A    public void testSetEmptyParagraphStyle() %7B%0A        SimpleEditableStyledDocument%3CString, String%3E document = new SimpleEditableStyledDocument%3C%3E(%22%22, %22%22);%0A        String newParStyle = %22new style%22;%0A        document.setParagraphStyle(0, newParStyle);%0A        assertEquals(newParStyle, document.getParagraphStyle(0));%0A    %7D%0A%0A    @Test%0A    public void testSetNonEmptyParagraphStyle() %7B%0A        SimpleEditableStyledDocument%3CString, String%3E document = new SimpleEditableStyledDocument%3C%3E(%22%22, %22%22);%0A        replaceText(document, 0, 0, %22some text%22);%0A        String newParStyle = %22new style%22;%0A        document.setParagraphStyle(0, newParStyle);%0A        assertEquals(newParStyle, document.getParagraphStyle(0));%0A    %7D
 %0A%7D%0A

Write tests for regression