diff options
author | Stefan Niedermann <info@niedermann.it> | 2020-12-19 12:23:04 +0300 |
---|---|---|
committer | Stefan Niedermann <info@niedermann.it> | 2020-12-19 12:23:04 +0300 |
commit | 109d160c67ee686f1b5c831b8cccd97c32fe8c7d (patch) | |
tree | 3297f6fafbf75b96cbcaaf32119631ded35bf68a | |
parent | 961e6ee5a82bc835c0db357ace1b5432e954e820 (diff) |
Add a few unit tests
Signed-off-by: Stefan Niedermann <info@niedermann.it>
3 files changed, 65 insertions, 11 deletions
diff --git a/markdown/src/androidTest/java/it/niedermann/android/markdown/MarkwonMarkdownUtilTest.java b/markdown/src/androidTest/java/it/niedermann/android/markdown/MarkwonMarkdownUtilTest.java index c3a97ed8c..754d8e2c0 100644 --- a/markdown/src/androidTest/java/it/niedermann/android/markdown/MarkwonMarkdownUtilTest.java +++ b/markdown/src/androidTest/java/it/niedermann/android/markdown/MarkwonMarkdownUtilTest.java @@ -1,5 +1,6 @@ package it.niedermann.android.markdown; +import android.text.Editable; import android.text.SpannableStringBuilder; import androidx.test.ext.junit.runners.AndroidJUnit4; @@ -291,6 +292,45 @@ public class MarkwonMarkdownUtilTest extends TestCase { } @Test + public void testRemoveContainingPunctuation() { + try { + final Method m = MarkwonMarkdownUtil.class.getDeclaredMethod("removeContainingPunctuation", Editable.class, int.class, int.class, String.class); + m.setAccessible(true); + SpannableStringBuilder builder; + + builder = new SpannableStringBuilder("Lorem *ipsum* dolor"); + m.invoke(null, builder, 0, 19, "*"); + assertEquals("Lorem ipsum dolor", builder.toString()); + + builder = new SpannableStringBuilder("*Lorem ipsum dolor*"); + m.invoke(null, builder, 0, 19, "*"); + assertEquals("Lorem ipsum dolor", builder.toString()); + + builder = new SpannableStringBuilder("**Lorem ipsum**"); + m.invoke(null, builder, 0, 15, "**"); + assertEquals("Lorem ipsum", builder.toString()); + + builder = new SpannableStringBuilder("*Lorem* *ipsum*"); + m.invoke(null, builder, 0, 15, "*"); + assertEquals("Lorem ipsum", builder.toString()); + + builder = new SpannableStringBuilder("Lorem* ipsum"); + m.invoke(null, builder, 0, 12, "*"); + assertEquals("Lorem ipsum", builder.toString()); + + builder = new SpannableStringBuilder("*Lorem* ipsum*"); + m.invoke(null, builder, 0, 14, "*"); + assertEquals("Lorem ipsum", builder.toString()); + + builder = new SpannableStringBuilder("**Lorem ipsum**"); + m.invoke(null, builder, 0, 15, "*"); + assertEquals("Lorem ipsum", builder.toString()); + } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) { + e.printStackTrace(); + } + } + + @Test @SuppressWarnings("ConstantConditions") public void testSelectionIsSurroundedByPunctuation() { try { @@ -310,6 +350,23 @@ public class MarkwonMarkdownUtilTest extends TestCase { @Test @SuppressWarnings("ConstantConditions") + public void testGetContainedPunctuationCount() { + try { + final Method m = MarkwonMarkdownUtil.class.getDeclaredMethod("getContainedPunctuationCount", CharSequence.class, int.class, int.class, String.class); + m.setAccessible(true); + assertEquals(0, (int) m.invoke(null, "*Lorem ipsum*", 1, 12, "*")); + assertEquals(1, (int) m.invoke(null, "*Lorem ipsum*", 1, 13, "*")); + assertEquals(2, (int) m.invoke(null, "*Lorem ipsum*", 0, 13, "*")); + assertEquals(0, (int) m.invoke(null, "*Lorem ipsum*", 0, 13, "**")); + assertEquals(0, (int) m.invoke(null, "*Lorem ipsum**", 0, 13, "**")); + assertEquals(1, (int) m.invoke(null, "*Lorem ipsum**", 0, 14, "**")); + } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) { + e.printStackTrace(); + } + } + + @Test + @SuppressWarnings("ConstantConditions") public void testSelectionIsInLink() { try { final Method m = MarkwonMarkdownUtil.class.getDeclaredMethod("selectionIsInLink", CharSequence.class, int.class, int.class); @@ -354,6 +411,7 @@ public class MarkwonMarkdownUtilTest extends TestCase { assertEquals("+ ", MarkwonMarkdownUtil.getListItemIfIsEmpty("+ ")); assertEquals("* ", MarkwonMarkdownUtil.getListItemIfIsEmpty("* ")); assertEquals("1. ", MarkwonMarkdownUtil.getListItemIfIsEmpty("1. ")); + assertNull(MarkwonMarkdownUtil.getListItemIfIsEmpty("- Test")); assertNull(MarkwonMarkdownUtil.getListItemIfIsEmpty("+ Test")); assertNull(MarkwonMarkdownUtil.getListItemIfIsEmpty("* Test")); diff --git a/markdown/src/main/java/it/niedermann/android/markdown/markwon/MarkwonMarkdownUtil.java b/markdown/src/main/java/it/niedermann/android/markdown/markwon/MarkwonMarkdownUtil.java index a32715a8f..78f95f720 100644 --- a/markdown/src/main/java/it/niedermann/android/markdown/markwon/MarkwonMarkdownUtil.java +++ b/markdown/src/main/java/it/niedermann/android/markdown/markwon/MarkwonMarkdownUtil.java @@ -40,8 +40,9 @@ import it.niedermann.android.markdown.markwon.plugins.ThemePlugin; ) public class MarkwonMarkdownUtil { - private static final Pattern ORDERED_LIST_ITEM_PATTERN = Pattern.compile("^(\\d+).\\s.+$"); - private static final Pattern ORDERED_LIST_ITEM_EMPTY_PATTERN = Pattern.compile("^(\\d+).\\s$"); + private static final Pattern PATTERN_ORDERED_LIST_ITEM = Pattern.compile("^(\\d+).\\s.+$"); + private static final Pattern PATTERN_ORDERED_LIST_ITEM_EMPTY = Pattern.compile("^(\\d+).\\s$"); + private static final Pattern PATTERN_MARKDOWN_LINK = Pattern.compile("\\[(.+)?]\\(([^ ]+?)?( \"(.+)\")?\\)"); private MarkwonMarkdownUtil() { // Util class @@ -96,7 +97,7 @@ public class MarkwonMarkdownUtil { return listType.listSymbolWithTrailingSpace; } } - final Matcher matcher = ORDERED_LIST_ITEM_EMPTY_PATTERN.matcher(line); + final Matcher matcher = PATTERN_ORDERED_LIST_ITEM_EMPTY.matcher(line); if (matcher.find()) { return matcher.group(); } @@ -120,7 +121,7 @@ public class MarkwonMarkdownUtil { * @return the number of the ordered list item if the line is an ordered list, otherwise -1. */ public static int getOrderedListNumber(@NonNull String line) { - final Matcher matcher = ORDERED_LIST_ITEM_PATTERN.matcher(line); + final Matcher matcher = PATTERN_ORDERED_LIST_ITEM.matcher(line); if (matcher.find()) { final String groupNumber = matcher.group(1); if (groupNumber != null) { @@ -134,10 +135,6 @@ public class MarkwonMarkdownUtil { return -1; } - public static boolean lineStartsWithUnorderedList(@NonNull String line, @NonNull EListType listType) { - return line.startsWith(listType.listSymbol); - } - /** * Modifies the {@param editable} and adds the given {@param punctuation} from * {@param selectionStart} to {@param selectionEnd} or removes the {@param punctuation} in case @@ -246,7 +243,7 @@ public class MarkwonMarkdownUtil { } public static boolean selectionIsInLink(@NonNull CharSequence text, int start, int end) { - final Matcher matcher = Pattern.compile("\\[(.+)?]\\(([^ ]+?)?( \"(.+)\")?\\)").matcher(text); + final Matcher matcher = PATTERN_MARKDOWN_LINK.matcher(text); while (matcher.find()) { if ((start >= matcher.start() && start < matcher.end()) || (end > matcher.start() && end <= matcher.end())) { return true; diff --git a/markdown/src/main/java/it/niedermann/android/markdown/markwon/textwatcher/AutoContinuationTextWatcher.java b/markdown/src/main/java/it/niedermann/android/markdown/markwon/textwatcher/AutoContinuationTextWatcher.java index 13c659935..f01e62b40 100644 --- a/markdown/src/main/java/it/niedermann/android/markdown/markwon/textwatcher/AutoContinuationTextWatcher.java +++ b/markdown/src/main/java/it/niedermann/android/markdown/markwon/textwatcher/AutoContinuationTextWatcher.java @@ -16,7 +16,6 @@ import static it.niedermann.android.markdown.markwon.MarkwonMarkdownUtil.getList import static it.niedermann.android.markdown.markwon.MarkwonMarkdownUtil.getOrderedListNumber; import static it.niedermann.android.markdown.markwon.MarkwonMarkdownUtil.getStartOfLine; import static it.niedermann.android.markdown.markwon.MarkwonMarkdownUtil.lineStartsWithCheckbox; -import static it.niedermann.android.markdown.markwon.MarkwonMarkdownUtil.lineStartsWithUnorderedList; /** * Automatically continues lists and checkbox lists when pressing enter @@ -86,7 +85,7 @@ public class AutoContinuationTextWatcher implements TextWatcher { } else { for (EListType listType : EListType.values()) { final boolean isCheckboxList = lineStartsWithCheckbox(line, listType); - final boolean isPlainList = !isCheckboxList && lineStartsWithUnorderedList(line, listType); + final boolean isPlainList = !isCheckboxList && line.startsWith(listType.listSymbolWithTrailingSpace); if (isPlainList || isCheckboxList) { customText = isPlainList ? listType.listSymbolWithTrailingSpace : listType.checkboxUncheckedWithTrailingSpace; isInsert = true; |