Skip to content

Instantly share code, notes, and snippets.

@loloof64
Last active April 10, 2018 14:52
Show Gist options
  • Save loloof64/5d56603f2b6010339160f2ee28510d5d to your computer and use it in GitHub Desktop.
Save loloof64/5d56603f2b6010339160f2ee28510d5d to your computer and use it in GitHub Desktop.
WildCoderTDD
package fr.wildcodeschool.unittesting;
public class StringUtils {
private static final String ALL_VOWELS = "aeiouyAEIOUY";
/**
* Renvoie la chaine formée par les voyelles d'une chaine de caractères
* @return Chaine avec uniquement des voyelles
*/
public static String vowels(String candidate) {
if (candidate == null) throw new IllegalArgumentException("The candidate string is null.");
String vowels = "";
char[] letters = candidate.toCharArray();
for (int i = 0; i < candidate.length(); i++) {
if (ALL_VOWELS.indexOf(letters[i]) >= 0 && vowels.indexOf(letters[i]) == -1) {
vowels += letters[i];
}
}
return vowels;
}
}
package fr.wildcodeschool.unittesting;
import org.junit.Test;
import org.junit.jupiter.api.BeforeEach;
import static org.junit.jupiter.api.Assertions.assertEquals;
public class StringUtilsTest {
private StringUtils stringUtilsInstance;
@BeforeEach
public void setup(){
stringUtilsInstance = new StringUtils();
}
@Test
public void voyelsTakesTheFirstCharacterIntoAccount(){
String testedString = "ABOEI";
assertEquals("AOEI", StringUtils.vowels(testedString));
}
@Test
public void voyelsTakesLowercaseLetterYIntoAccount(){
String testedString = "aybcne";
assertEquals("aye", StringUtils.vowels(testedString));
}
@Test
public void voyelsTakesUppercaseLetterYIntoAccount(){
String testedString = "AYBNCE";
assertEquals("AYE", StringUtils.vowels(testedString));
}
@Test
public void voyelsWithSeveralInstancesOfSameVoyelReturnsItOnce(){
assertEquals("A", StringUtils.vowels("AAAA"));
}
@Test
public void voyelsWithEmptyStringReturnsEmptyString(){
assertEquals("", StringUtils.vowels(""));
}
@Test(expected = IllegalArgumentException.class)
public void voyelsThrowExceptionIfParameterNotGiven(){
StringUtils.vowels(null);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment