OnJava8-Examples/validating/tests/StringInverterTests.java

66 lines
1.8 KiB
Java
Raw Permalink Normal View History

2016-09-01 15:14:44 -06:00
// validating/tests/StringInverterTests.java
// (c)2021 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() {
2020-10-06 16:06:14 -06:00
assertThrows(Error.class, () -> {
2016-08-28 06:13:56 -06:00
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);
2020-10-06 16:06:14 -06:00
assertThrows(RuntimeException.class, () -> {
2016-08-28 06:13:56 -06:00
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);
}
}