Browse Source

Unit tests for StringBuilderUtils, fixing trim method in StringBuilderUtils

master
anthony restaino 8 years ago
parent
commit
fe7dc75973
  1. 6
      app/src/main/java/acr/browser/lightning/utils/StringBuilderUtils.java
  2. 118
      app/src/test/java/acr/browser/lightning/utils/StringBuilderUtilsTest.java

6
app/src/main/java/acr/browser/lightning/utils/StringBuilderUtils.java

@ -14,8 +14,8 @@ public class StringBuilderUtils { @@ -14,8 +14,8 @@ public class StringBuilderUtils {
private static final String EMPTY = "";
/**
* Replace a string in a string
* builder with another string.
* Replace the first string found in a
* string builder with another string.
*
* @param stringBuilder the string builder.
* @param toReplace the string to replace.
@ -42,7 +42,7 @@ public class StringBuilderUtils { @@ -42,7 +42,7 @@ public class StringBuilderUtils {
stringBuilder.replace(0, 1, EMPTY);
}
while (stringBuilder.lastIndexOf(SPACE) == (stringBuilder.length() - 1)) {
while (stringBuilder.lastIndexOf(SPACE) == (stringBuilder.length() - 1) && stringBuilder.length() > 0) {
stringBuilder.replace(stringBuilder.length() - 1, stringBuilder.length(), EMPTY);
}
}

118
app/src/test/java/acr/browser/lightning/utils/StringBuilderUtilsTest.java

@ -0,0 +1,118 @@ @@ -0,0 +1,118 @@
package acr.browser.lightning.utils;
import org.junit.Assert;
import org.junit.Test;
/**
* Tests for {@link StringBuilderUtils}.
*/
public class StringBuilderUtilsTest {
@Test
public void replace_SingleCharacter() {
StringBuilder stringBuilder = new StringBuilder("abbbaacccda");
StringBuilderUtils.replace(stringBuilder, "a", "");
Assert.assertEquals(stringBuilder.toString(), "bbbaacccda");
}
@Test
public void replace_MultipleCharacters() {
StringBuilder stringBuilder = new StringBuilder("___ab___cd___");
StringBuilderUtils.replace(stringBuilder, "___", "---");
Assert.assertEquals(stringBuilder.toString(), "---ab___cd___");
}
@Test
public void trim_LeadingAndTrailingSpaces() {
StringBuilder stringBuilder = new StringBuilder(" t e s t ");
StringBuilderUtils.trim(stringBuilder);
Assert.assertEquals(stringBuilder.toString(), "t e s t");
}
@Test
public void trim_OnlyContainsSpaces() {
StringBuilder stringBuilder = new StringBuilder(" ");
Assert.assertFalse(stringBuilder.toString().isEmpty());
StringBuilderUtils.trim(stringBuilder);
Assert.assertTrue(stringBuilder.toString().isEmpty());
}
@Test
public void isEmpty_HasNoCharacters() {
StringBuilder stringBuilder = new StringBuilder("");
Assert.assertTrue(StringBuilderUtils.isEmpty(stringBuilder));
}
@Test
public void isEmpty_HasCharacters() {
// Case with normal letters
StringBuilder stringBuilder = new StringBuilder("abcdefg");
Assert.assertFalse(StringBuilderUtils.isEmpty(stringBuilder));
// Case with empty spaces
StringBuilder stringBuilder1 = new StringBuilder(" ");
Assert.assertFalse(StringBuilderUtils.isEmpty(stringBuilder1));
}
@Test
public void startsWith_SingleCharacter() {
StringBuilder stringBuilder = new StringBuilder("1234567890");
Assert.assertTrue(StringBuilderUtils.startsWith(stringBuilder, "1"));
Assert.assertFalse(StringBuilderUtils.startsWith(stringBuilder, "2"));
}
@Test
public void startsWith_MultipleCharacters() {
StringBuilder stringBuilder = new StringBuilder("1234567890");
Assert.assertTrue(StringBuilderUtils.startsWith(stringBuilder, "12345"));
Assert.assertFalse(StringBuilderUtils.startsWith(stringBuilder, "23456"));
}
@Test
public void contains_SingleCharacter() {
StringBuilder stringBuilder = new StringBuilder("abcdefg123456");
// This character is not in the string
Assert.assertFalse(StringBuilderUtils.contains(stringBuilder, "z"));
// All these characters are in the string
for (int n = 0; n < stringBuilder.length(); n++) {
Assert.assertTrue(StringBuilderUtils.contains(stringBuilder, String.valueOf(stringBuilder.charAt(n))));
}
}
@Test
public void contains_MultipleCharacters() {
StringBuilder stringBuilder = new StringBuilder("abcdefg123456");
// Should return false since characters are in reverse order
Assert.assertFalse(StringBuilderUtils.contains(stringBuilder, "cba"));
// Should return false since characters are not in string
Assert.assertFalse(StringBuilderUtils.contains(stringBuilder, "zyx"));
}
@Test
public void equals_SameCharacters() {
StringBuilder stringBuilder = new StringBuilder("abcdefg");
Assert.assertTrue(StringBuilderUtils.equals(stringBuilder, "abcdefg"));
}
@Test
public void equals_DifferentCharacters() {
StringBuilder stringBuilder = new StringBuilder("abcdefg");
Assert.assertFalse(StringBuilderUtils.equals(stringBuilder, "abcdefg1"));
}
@Test
public void substring() {
StringBuilder stringBuilder = new StringBuilder("abcdefg");
String string = "abcdefg";
Assert.assertEquals(StringBuilderUtils.substring(stringBuilder, 1, 5).toString(), string.substring(1, 5));
}
}
Loading…
Cancel
Save