package cgeo.geocaching.utils;

import static org.assertj.core.api.Assertions.assertThat;

import cgeo.geocaching.connector.gc.GCConstants;
import cgeo.geocaching.test.mock.MockedCache;

import java.util.regex.Pattern;

import junit.framework.TestCase;

public class TextUtilsTest extends TestCase {
    public static void testRegEx() {
        final String page = MockedCache.readCachePage("GC2CJPF");
        assertThat(TextUtils.getMatch(page, GCConstants.PATTERN_LOGIN_NAME, true, "???")).isEqualTo("Bananeweizen");
    }

    public static void testReplaceWhitespaces() {
        assertThat(TextUtils.replaceWhitespace("  foo\n\tbar   \r   baz  ")).isEqualTo("foo bar baz ");
    }

    public static void testControlCharactersCleanup() {
        final Pattern patternAll = Pattern.compile("(.*)", Pattern.DOTALL);
        assertThat(TextUtils.getMatch("some" + '\u001C' + "control" + (char) 0x1D + "characters removed", patternAll, "")).isEqualTo("some control characters removed");
        assertThat(TextUtils.getMatch("newline\nalso\nremoved", patternAll, "")).isEqualTo("newline also removed");
    }

    public static void testGetMatch() {
        final Pattern patternAll = Pattern.compile("foo(...)");
        final String text = "abc-foobar-def-fooxyz-ghi-foobaz-jkl";
        assertThat(TextUtils.getMatch(text, patternAll, false, 1, null, false)).isEqualTo("bar");
        assertThat(TextUtils.getMatch(text, patternAll, false, 1, null, true)).isEqualTo("baz");
    }
}

@@ -36,57 +36,103 @@
 ort 
-static org.assertj.core.api.Assertions.assertThat
+android.text.SpannableString;%0A%0Aimport junit.framework.TestCase;%0A%0Aimport java.util.regex.Pattern
 ;%0A%0Ai
@@ -237,65 +237,57 @@
 ort 
-java.util.regex.Pattern;%0A%0Aimport junit.framework.TestCase
+static org.assertj.core.api.Assertions.assertThat
 ;%0A%0Ap
@@ -1515,11 +1515,426 @@
 );%0A    %7D
+%0A%0A    public static void testTrimSpanned() %7B%0A        assertTrimSpanned(%22 %22,%22%22);%0A        assertTrimSpanned(%22%5Cn%22, %22%22);%0A        assertTrimSpanned(%22a %22, %22a%22);%0A        assertTrimSpanned(%22a%5Cn%22, %22a%22);%0A    %7D%0A%0A    private static void assertTrimSpanned(String input, String expected) %7B%0A        assertThat(TextUtils.trimSpanned(new SpannableString(input)).toString()).isEqualTo(new SpannableString(expected).toString());%0A    %7D
 %0A%7D%0A

add test for trimSpanned