@Test public void repeat() { Assert.assertEquals("", StringIterate.repeat("", 42)); Assert.assertEquals(" ", StringIterate.repeat(' ', 4)); Assert.assertEquals(" ", StringIterate.repeat(" ", 8)); Assert.assertEquals("CubedCubedCubed", StringIterate.repeat("Cubed", 3)); }
@Test public void splitAtIndex() { final String oompaLoompa = "oompaloompa"; Assert.assertEquals(Tuples.twin("oompa", "loompa"), StringIterate.splitAtIndex(oompaLoompa, 5)); Assert.assertEquals(Tuples.twin("", oompaLoompa), StringIterate.splitAtIndex(oompaLoompa, 0)); Assert.assertEquals( Tuples.twin(oompaLoompa, ""), StringIterate.splitAtIndex(oompaLoompa, oompaLoompa.length())); Assert.assertEquals(Tuples.twin("", ""), StringIterate.splitAtIndex("", 0)); Verify.assertThrows( StringIndexOutOfBoundsException.class, new Runnable() { public void run() { StringIterate.splitAtIndex(oompaLoompa, 17); } }); Verify.assertThrows( StringIndexOutOfBoundsException.class, new Runnable() { public void run() { StringIterate.splitAtIndex(oompaLoompa, -8); } }); }
@Test public void isAlphaNumeric() { Assert.assertTrue(StringIterate.isAlphaNumeric("123")); Assert.assertTrue(StringIterate.isAlphaNumeric("abc")); Assert.assertTrue(StringIterate.isAlphaNumeric("123abc")); Assert.assertFalse(StringIterate.isAlphaNumeric("!@#")); Assert.assertFalse(StringIterate.isAlphaNumeric("")); }
@Test public void englishIsUpperLowerCase() { String allValues = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890~`!@#$%^&*()_-+=[]{};<>,.?/|"; String jdkUpper = allValues.toUpperCase(); String upper = StringIterate.englishToUpperCase(allValues); Assert.assertEquals(jdkUpper.length(), upper.length()); Assert.assertEquals(jdkUpper, upper); String jdkLower = allValues.toLowerCase(); String lower = StringIterate.englishToLowerCase(allValues); Assert.assertEquals(jdkLower.length(), lower.length()); Assert.assertEquals(jdkLower, lower); }
@Test public void asTrimmedSortedList() { String tokens = " Ted,Mary "; MutableList<String> results = StringIterate.csvTrimmedTokensToSortedList(tokens); Verify.assertSize(2, results); Verify.assertStartsWith(results, "Mary", "Ted"); }
@Test public void csvTokensToSet() { String tokens = "Ted,Mary"; MutableSet<String> results = StringIterate.csvTokensToSet(tokens); Verify.assertSize(2, results); Verify.assertContainsAll(results, "Mary", "Ted"); }
@Test public void toMap() { String tokens = "1:Ted|2:Mary"; MutableMap<String, String> results = StringIterate.tokensToMap(tokens); Verify.assertSize(2, results); Verify.assertContainsKeyValue("1", "Ted", results); Verify.assertContainsKeyValue("2", "Mary", results); }
@Test public void reverseForEachCodePoint() { final StringBuffer buffer = new StringBuffer(); StringIterate.reverseForEach( "1a2b3c", new CodePointProcedure() { public void value(int character) { buffer.appendCodePoint(character); } }); Assert.assertEquals("c3b2a1", buffer.toString()); StringIterate.reverseForEach( "", new CodePointProcedure() { public void value(int codePoint) { Assert.fail(); } }); }
@Test public void toMapWithFunctions() { String tokens = "1:Ted|2:Mary"; Function<String, String> stringPassThruFunction = Functions.getPassThru(); MutableMap<Integer, String> results = StringIterate.tokensToMap( tokens, "|", ":", Functions.getStringToInteger(), stringPassThruFunction); Verify.assertSize(2, results); Verify.assertContainsKeyValue(1, "Ted", results); Verify.assertContainsKeyValue(2, "Mary", results); }
@Test public void forEach() { final StringBuffer buffer = new StringBuffer(); StringIterate.forEach( "1a2b3c", new CharProcedure() { public void value(char character) { buffer.append(character); } }); Assert.assertEquals("1a2b3c", buffer.toString()); }
@Test public void getFirstToken() { Assert.assertEquals("alpha", StringIterate.getFirstToken("alpha~|~beta~|~charlie", "~|~")); Assert.assertEquals("123", StringIterate.getFirstToken("123", "~|~")); Assert.assertEquals("", StringIterate.getFirstToken("", "~|~")); Assert.assertNull(StringIterate.getFirstToken(null, "~|~")); Assert.assertEquals("123", StringIterate.getFirstToken("123~|~", "~|~")); Assert.assertEquals("", StringIterate.getFirstToken("~|~123,", "~|~")); }
@Test public void forEachTrimmedToken() { String tokens = "1 ,2 "; final MutableList<Integer> list = Lists.mutable.of(); StringIterate.forEachTrimmedToken( tokens, ",", new CheckedProcedure<String>() { @Override public void safeValue(String string) { list.add(Integer.valueOf(string)); } }); Verify.assertSize(2, list); Verify.assertContains(1, list); Verify.assertContains(2, list); }
@Test public void collectCodePoint() { Assert.assertEquals("ABC", StringIterate.collect("abc", CodePointFunction.TO_UPPERCASE)); Assert.assertEquals("abc", StringIterate.collect("abc", CodePointFunction.TO_LOWERCASE)); }
@Test public void countCodePoint() { int count = StringIterate.count("1a2a3", CodePointPredicate.IS_DIGIT); Assert.assertEquals(3, count); }
@Test public void toSet() { Verify.assertSetsEqual( UnifiedSet.newSetWith('a', 'b', 'c', 'd', 'e'), StringIterate.toSet("aabcde")); }
@Test public void string() { Assert.assertEquals("Token2", StringIterate.getLastToken("Token1DelimiterToken2", "Delimiter")); }
@Test public void padOrTrim() { Assert.assertEquals("abcdefghijkl", StringIterate.padOrTrim("abcdefghijkl", 12)); Assert.assertEquals("this n", StringIterate.padOrTrim("this needs to be trimmed", 6)); Assert.assertEquals("pad this ", StringIterate.padOrTrim("pad this", 14)); }
@Test(expected = IllegalArgumentException.class) public void occurrencesOf_multiple_character_string_throws() { StringIterate.occurrencesOf("1a2a3", "abc"); }
@Test public void occurrencesOfString() { int count = StringIterate.occurrencesOf("1a2a3", "a"); Assert.assertEquals(2, count); }
@Test public void englishToUpperLowerCase() { Assert.assertEquals("ABC", StringIterate.englishToUpperCase("abc")); Assert.assertEquals("abc", StringIterate.englishToLowerCase("ABC")); }
@Test public void notEmptyOrWhitespace() { Assert.assertFalse(StringIterate.notEmptyOrWhitespace(" ")); Assert.assertTrue(StringIterate.notEmptyOrWhitespace(" 1 ")); }
@Test public void rejectCodePoint() { String string = StringIterate.reject("1a2b3c", CodePointPredicate.IS_DIGIT); Assert.assertEquals("abc", string); }
@Test public void occurrencesOfCodePoint() { int count = StringIterate.occurrencesOf("1a2a3", "a".codePointAt(0)); Assert.assertEquals(2, count); }
@Test public void selectCodePoint() { String string = StringIterate.select("1a2a3", CodePointPredicate.IS_DIGIT); Assert.assertEquals("123", string); }
@Test public void englishToLowerCase() { Assert.assertEquals("abc", StringIterate.englishToLowerCase("ABC")); Assert.assertEquals("a,b,c", StringIterate.englishToLowerCase("A,B,C")); Assert.assertSame("a,b,c", StringIterate.englishToLowerCase("a,b,c")); }
@Test public void notEmpty() { Assert.assertFalse(StringIterate.notEmpty("")); Assert.assertTrue(StringIterate.notEmpty(" ")); Assert.assertTrue(StringIterate.notEmpty("1")); }
@Test public void csvTrimmedTokenToList() { String tokens = "1 ,2 "; Assert.assertEquals( FastList.newListWith("1", "2"), StringIterate.csvTrimmedTokensToList(tokens)); }
@Test public void count2CodePoint() { int count = StringIterate.count("1a2a3", CodePointPredicate.IS_UNDEFINED); Assert.assertEquals(0, count); }
@Test public void detect() { char character = StringIterate.detect("1a2a3", CharPredicate.IS_LETTER); Assert.assertEquals('a', character); }
@Test public void injectIntoTokens() { Assert.assertEquals( "123", StringIterate.injectIntoTokens("1,2,3", ",", null, AddFunction.STRING)); }