Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit a72e2a03 authored by stefan-niedermann's avatar stefan-niedermann
Browse files

Add MarkdownUtilTest

parent 5acec3dc
Loading
Loading
Loading
Loading
+88 −0
Original line number Diff line number Diff line
package it.niedermann.owncloud.notes.util;

import androidx.annotation.NonNull;

import junit.framework.TestCase;

import java.util.HashMap;
import java.util.Map;

/**
 * Tests the NoteUtil
 * Created by stefan on 06.10.15.
 */
public class MarkDownUtilTest extends TestCase {

    public static int getStartOfLine(@NonNull CharSequence s, int start) {
        int startOfLine = start;
        while (startOfLine > 0 && s.charAt(startOfLine - 1) != '\n') {
            startOfLine--;
        }
        return startOfLine;
    }

    public void testGetStartOfLine() {
        //language=md
        StringBuilder test = new StringBuilder(
                "# Test-Note\n" + // line start 0
                        "\n" + // line start 12
                        "- [ ] this is a test note\n" + // line start 13
                        "- [x] test\n" + // line start 39
                        "[test](https://example.com)\n" + // line start 50
                        "\n" + // line start 77
                        "\n" // line start 78
        );

        for (int i = 0; i < test.length(); i++) {
            int startOfLine = MarkDownUtil.getStartOfLine(test, i);
            if (i <= 11) {
                assertEquals(0, startOfLine);
            } else if (i <= 12) {
                assertEquals(12, startOfLine);
            } else if (i <= 38) {
                assertEquals(13, startOfLine);
            } else if (i <= 49) {
                assertEquals(39, startOfLine);
            } else if (i <= 77) {
                assertEquals(50, startOfLine);
            } else if (i <= 78) {
                assertEquals(78, startOfLine);
            } else if (i <= 79) {
                assertEquals(79, startOfLine);
            }
        }
    }

    public void testLineStartsWithCheckbox() {
        Map<String, Boolean> lines = new HashMap<>();
        lines.put("- [ ] ", true);
        lines.put("- [x] ", true);
        lines.put("* [ ] ", true);
        lines.put("* [x] ", true);
        lines.put("- [ ]", true);
        lines.put("- [x]", true);
        lines.put("* [ ]", true);
        lines.put("* [x]", true);

        lines.put("-[ ] ", false);
        lines.put("-[x] ", false);
        lines.put("*[ ] ", false);
        lines.put("*[x] ", false);
        lines.put("-[ ]", false);
        lines.put("-[x]", false);
        lines.put("*[ ]", false);
        lines.put("*[x]", false);

        lines.put("- [] ", false);
        lines.put("* [] ", false);
        lines.put("- []", false);
        lines.put("* []", false);

        lines.put("-[] ", false);
        lines.put("*[] ", false);
        lines.put("-[]", false);
        lines.put("*[]", false);

        lines.forEach((key,value) -> assertEquals(value, (Boolean) MarkDownUtil.lineStartsWithCheckbox(key)));
    }
}
 No newline at end of file
+1 −0
Original line number Diff line number Diff line
@@ -46,6 +46,7 @@ public class NoteLinksUtilsTest {

    @Test
    public void doNotReplaceNormalLinks() {
        //language=md
        String markdown = "[normal link](https://example.com) and another [note link](123456)";
        String result = NoteLinksUtils.replaceNoteLinksWithDummyUrls(markdown, Collections.singleton("123456"));
        Assert.assertEquals(String.format("[normal link](https://example.com) and another [note link](%s123456)", RELATIVE_LINK_WORKAROUND_PREFIX), result);
+6 −2
Original line number Diff line number Diff line
@@ -64,14 +64,18 @@ public class MarkDownUtil {
        return ((Spanned) text).getSpans(0, text.length(), MDImageSpan.class).length > 0;
    }

    public static boolean lineStartsWithCheckbox(@NonNull String line) {
        return lineStartsWithCheckbox(line, true) || lineStartsWithCheckbox(line, false);
    }

    public static boolean lineStartsWithCheckbox(@NonNull String line, boolean starAsLeadingCharacter) {
        return starAsLeadingCharacter
                ? line.startsWith(CHECKBOX_UNCHECKED_STAR) || line.startsWith(CHECKBOX_CHECKED_STAR)
                : line.startsWith(CHECKBOX_UNCHECKED_MINUS) || line.startsWith(CHECKBOX_CHECKED_MINUS);
    }

    public static int getStartOfLine(@NonNull CharSequence s, int start) {
        int startOfLine = start;
    public static int getStartOfLine(@NonNull CharSequence s, int cursorPosition) {
        int startOfLine = cursorPosition;
        while (startOfLine > 0 && s.charAt(startOfLine - 1) != '\n') {
            startOfLine--;
        }