Skip to content

Instantly share code, notes, and snippets.

@toastkidjp
Created September 15, 2017 13:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save toastkidjp/7230227dd16b80bd4eea9da652be3637 to your computer and use it in GitHub Desktop.
Save toastkidjp/7230227dd16b80bd4eea9da652be3637 to your computer and use it in GitHub Desktop.
import android.text.TextUtils;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricTestRunner;
import java.util.Arrays;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
@RunWith(RobolectricTestRunner.class)
public class TextUtilsTest {
@Test
public void check_concat() {
assertEquals("abc", TextUtils.concat("a", "b", "c"));
assertEquals(0, TextUtils.concat().length());
}
@Test
public void check_expandTemplate() {
assertEquals("Ice cream is cool.", TextUtils.expandTemplate("^1 is ^2.", "Ice cream", "cool").toString());
assertEquals("cool is Ice cream.", TextUtils.expandTemplate("^2 is ^1.", "Ice cream", "cool").toString());
assertEquals("Ice cream is Ice cream.", TextUtils.expandTemplate("^1 is ^1.", "Ice cream", "cool").toString());
assertEquals("Ice cream is cool.", TextUtils.expandTemplate("Ice cream is cool.").toString());
}
@Test
public void check_isDigitsOnly() {
assertTrue(TextUtils.isDigitsOnly("123456"));
assertFalse(TextUtils.isDigitsOnly("123g"));
assertTrue(TextUtils.isDigitsOnly(""));
// TextUtils.isDigitsOnly(null);
}
@Test
public void check_htmlEncode() {
assertEquals("&lt;a&gt;", TextUtils.htmlEncode("<a>"));
assertEquals("", TextUtils.htmlEncode(""));
//assertEquals(null, TextUtils.htmlEncode(null));
}
@Test
public void check_join() {
assertEquals("abcde", TextUtils.join("", new String[]{"a", "b", "c", "d", "e"}));
assertEquals("a,b,c,d,e", TextUtils.join(",", new String[]{"a", "b", "c", "d", "e"}));
assertEquals("anullbnullcnulldnulle", TextUtils.join(null, new String[]{"a", "b", "c", "d", "e"}));
}
@Test
public void check_split() {
assertEquals("[to, be, or, not, to]", Arrays.toString(TextUtils.split("to/be/or/not/to", "/")));
}
@Test
public void check_substring() {
assertEquals("om", TextUtils.substring("tomato", 1, 3));
// TextUtils.substring(null, 1, 3);
// TextUtils.substring("tomato", -1, 3);
// TextUtils.substring("tomato", 2, 30);
}
@Test
public void check_replace() {
assertEquals("cbi", TextUtils.replace("abc", new String[]{"a", "c"}, new CharSequence[]{"c", "i"}).toString());
assertEquals("cbida", TextUtils.replace("abcda", new String[]{"a", "c"}, new CharSequence[]{"c", "i"}).toString());
assertEquals("cbida", TextUtils.replace("abcda", new String[]{"c", "a"}, new CharSequence[]{"i", "c"}).toString());
assertEquals("cbida", TextUtils.replace("abcda", new String[]{"c", "a"}, new CharSequence[]{"i", "c", "e"}).toString());
assertEquals("cbida", TextUtils.replace("abcda", new String[]{"c", "a", "e"}, new CharSequence[]{"i", "c"}).toString());
}
@Test
public void check_getTrimmedLength() {
assertEquals(1, TextUtils.getTrimmedLength("a "));
assertEquals(4, TextUtils.getTrimmedLength("a b"));
assertEquals(4, TextUtils.getTrimmedLength(" a b "));
assertEquals(0, TextUtils.getTrimmedLength(""));
//assertEquals(0, TextUtils.getTrimmedLength(null));
}
@Test
public void check_SimpleStringSplitter() {
TextUtils.SimpleStringSplitter splitter = new TextUtils.SimpleStringSplitter('-');
splitter.setString("a-b-c");
for (;splitter.hasNext();) {
System.out.println(splitter.next());
}
splitter.setString("9");
for (;splitter.hasNext();) {
System.out.println(splitter.next());
}
// splitter.setString(null);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment