2016-09-01 15:14:44 -06:00
|
|
|
// validating/tests/StringInverterTests.java
|
2016-12-30 17:23:13 -08:00
|
|
|
// (c)2017 MindView LLC: see Copyright.txt
|
2016-08-28 06:13:56 -06:00
|
|
|
// We make no guarantees that this code is fit for any purpose.
|
2016-09-23 13:23:35 -06:00
|
|
|
// Visit http://OnJava8.com for more book information.
|
2016-08-29 07:27:53 -06:00
|
|
|
package validating;
|
2016-08-28 06:13:56 -06:00
|
|
|
import java.util.*;
|
2016-08-29 07:27:53 -06:00
|
|
|
import java.util.stream.*;
|
2016-08-28 06:13:56 -06:00
|
|
|
import org.junit.jupiter.api.*;
|
|
|
|
import static org.junit.jupiter.api.Assertions.*;
|
|
|
|
|
|
|
|
public class StringInverterTests {
|
2016-08-29 07:27:53 -06:00
|
|
|
StringInverter inverter = new Inverter4();
|
|
|
|
@BeforeAll
|
|
|
|
static void startMsg() {
|
|
|
|
System.out.println(">>> StringInverterTests <<<");
|
|
|
|
}
|
2016-08-28 06:13:56 -06:00
|
|
|
@Test
|
2016-08-29 07:27:53 -06:00
|
|
|
void basicInversion1() {
|
2016-08-28 06:13:56 -06:00
|
|
|
String in = "Exit, Pursued by a Bear.";
|
|
|
|
String out = "eXIT, pURSUED BY A bEAR.";
|
|
|
|
assertEquals(inverter.invert(in), out);
|
|
|
|
}
|
|
|
|
@Test
|
2016-08-29 07:27:53 -06:00
|
|
|
void basicInversion2() {
|
2016-08-28 06:13:56 -06:00
|
|
|
expectThrows(Error.class, () -> {
|
|
|
|
assertEquals(inverter.invert("X"), "X");
|
|
|
|
});
|
|
|
|
}
|
|
|
|
@Test
|
2016-08-29 07:27:53 -06:00
|
|
|
void disallowedCharacters() {
|
|
|
|
String disallowed = ";-_()*&^%$#@!~`0123456789";
|
|
|
|
String result = disallowed.chars()
|
|
|
|
.mapToObj(c -> {
|
|
|
|
String cc = Character.toString((char)c);
|
|
|
|
try {
|
|
|
|
inverter.invert(cc);
|
|
|
|
return "";
|
|
|
|
} catch(RuntimeException e) {
|
|
|
|
return cc;
|
|
|
|
}
|
|
|
|
}).collect(Collectors.joining(""));
|
|
|
|
assertEquals(result, disallowed);
|
2016-08-28 06:13:56 -06:00
|
|
|
}
|
|
|
|
@Test
|
2016-08-29 07:27:53 -06:00
|
|
|
void allowedCharacters() {
|
2016-08-28 06:13:56 -06:00
|
|
|
String lowcase = "abcdefghijklmnopqrstuvwxyz ,.";
|
|
|
|
String upcase = "ABCDEFGHIJKLMNOPQRSTUVWXYZ ,.";
|
|
|
|
assertEquals(inverter.invert(lowcase), upcase);
|
|
|
|
assertEquals(inverter.invert(upcase), lowcase);
|
|
|
|
}
|
|
|
|
@Test
|
2016-08-29 07:27:53 -06:00
|
|
|
void lengthNoGreaterThan30() {
|
2016-08-28 06:13:56 -06:00
|
|
|
String str = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
|
|
|
|
assertTrue(str.length() > 30);
|
|
|
|
expectThrows(RuntimeException.class, () -> {
|
|
|
|
inverter.invert(str);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
@Test
|
2016-08-29 07:27:53 -06:00
|
|
|
void lengthLessThan31() {
|
2016-08-28 06:13:56 -06:00
|
|
|
String str = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
|
|
|
|
assertTrue(str.length() < 31);
|
|
|
|
inverter.invert(str);
|
|
|
|
}
|
|
|
|
}
|