Skip to content

Add unit test for class: WithoutString, URLify #27

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/main/java/com/rampatra/strings/WithoutString.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ private static String withoutString(String base, String remove) {
int removeLen = remove.length();
StringBuilder sb = new StringBuilder();

// in the case of removeLen is empty we return immediately, otherwise the loop will be executed forever
if (removeLen == 0) return original;

for (int i = 0; i < baseLen; ) {
int j = 0;
// when we see a match, advance the pointer
Expand Down
50 changes: 50 additions & 0 deletions src/test/java/com/ctci/arraysandstrings/URLifyTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package com.ctci.arraysandstrings;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import java.lang.reflect.Method;

import static org.junit.jupiter.api.Assertions.*;

class URLifyTest {

Method urlifyMethod;

@BeforeEach
void getWithoutStringMethod() throws NoSuchMethodException {
urlifyMethod = URLify.class.getDeclaredMethod("urlify", String.class);
urlifyMethod.setAccessible(true);
}

@Test
void testReplaceASpaceBetween() {
String result = (String) assertDoesNotThrow(() -> urlifyMethod.invoke(null, "Mr John Smith "));
assertEquals("Mr%20John%20Smith", result);
}

@Test
void testReplaceSpacesBetween() {
String result = (String) assertDoesNotThrow(() -> urlifyMethod.invoke(null, "Mr Ram Patra "));
assertEquals("Mr%20%20Ram%20Patra", result);
}

@Test
void testExceptionWhenNotEnoughAccommodateExtraSpace() {
Exception e = assertThrows(Exception.class, () -> urlifyMethod.invoke(null, "Mr Ram Patra"));
assertEquals("Index -1 out of bounds for length 13", e.getCause().getMessage());
}

@Test
void testExceptionWhenEmptyString() {
Exception e = assertThrows(Exception.class, () -> urlifyMethod.invoke(null, ""));
assertEquals("Index -1 out of bounds for length 0", e.getCause().getMessage());
}

@Test
void testExceptionWhenAllSpacesString() {
//add comment
Exception e = assertThrows(Exception.class, () -> urlifyMethod.invoke(null, " "));
assertEquals("Index -1 out of bounds for length 6", e.getCause().getMessage());
}
}
55 changes: 55 additions & 0 deletions src/test/java/com/rampatra/strings/WithoutStringTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package com.rampatra.strings;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import java.lang.reflect.Method;

import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertEquals;

class WithoutStringTest {
Method withoutStringMethod;

@BeforeEach
void getWithoutStringMethod() throws NoSuchMethodException {
withoutStringMethod = WithoutString.class.getDeclaredMethod("withoutString", String.class, String.class);
withoutStringMethod.setAccessible(true);
}

@Test
void testRemoveString() {
String result = (String) assertDoesNotThrow(() -> withoutStringMethod.invoke(null,"Hello there", "llo"));
assertEquals("He there", result);
}

@Test
void testRemoveAll() {
String result = (String) assertDoesNotThrow(() -> withoutStringMethod.invoke(null,"xxx", "x"));
assertEquals("", result);
}

@Test
void testRemoveNone() {
String result = (String) assertDoesNotThrow(() -> withoutStringMethod.invoke(null,"xxx", "a"));
assertEquals("xxx", result);
}

@Test
void testRemoveWithEmptySource() {
String result = (String) assertDoesNotThrow(() -> withoutStringMethod.invoke(null,"", "a"));
assertEquals("", result);
}

@Test
void testRemoveWithEmpty() {
String result = (String) assertDoesNotThrow(() -> withoutStringMethod.invoke(null,"ABC", ""));
assertEquals("ABC", result);
}

@Test
void testRemoveWithoutCaseSensitive() {
String result = (String) assertDoesNotThrow(() -> withoutStringMethod.invoke(null,"ABC", "a"));
assertEquals("BC", result);
}
}