TDD Much better now

This commit is contained in:
Bruce Eckel 2016-08-28 06:13:56 -06:00
parent 9c838f918b
commit 941213aae8
8 changed files with 144 additions and 77 deletions

View File

@ -179,7 +179,7 @@ subprojects {
java {
srcDir projectDir
// Remove this when it's working:
exclude "DynamicStringInverterTests.java"
// exclude "DynamicStringInverterTests.java"
}
resources {
srcDir projectDir

View File

@ -1,2 +1 @@
org.gradle.daemon=true
org.gradle.parallel=true

View File

@ -11,70 +11,116 @@ import static org.junit.jupiter.api.Assertions.*;
import static org.junit.jupiter.api.DynamicTest.*;
class DynamicStringInverterTests {
// Combine operations to prevent code duplication:
Stream<DynamicTest>
multiple_version_test(Consumer<StringInverter> test) {
testVersions(String id, Consumer<StringInverter> test) {
List<StringInverter> versions = Arrays.asList(
new StringInverter1(), new StringInverter2(),
new StringInverter3(), new StringInverter4());
return DynamicTest.stream(versions.iterator(),
(version) -> version.getClass().getSimpleName(),
test
(stringInverter) -> {
System.out.println(
stringInverter.getClass().getSimpleName() +
": " + id
);
try { // Capture failing tests
test.accept(stringInverter);
} catch(Error | Exception e) {
System.out.println(e.getMessage());
}
}
);
}
void isTrue(String description, boolean assertion) {
System.out.print(description + ": ");
System.out.println(assertion);
}
void isEqual(String lval, String rval) {
System.out.print(lval + " equals " + rval);
if(!lval.equals(rval))
System.out.println(" FAIL");
else
System.out.println();
}
@BeforeAll
static void startMsg() {
System.out.println(
">>> Starting DynamicStringInverterTests <<<");
}
@AfterAll
static void endMsg() {
System.out.println(
">>> Finished DynamicStringInverterTests <<<");
}
@TestFactory
Stream<DynamicTest> basicInversion_Succeed() {
return multiple_version_test( (version) -> {
String in = "Exit, Pursued by a Bear.";
String out = "eXIT, pURSUED BY A bEAR.";
assertEquals(version.invert(in), out);
});
return testVersions(
"A Basic Inversion that Succeeds",
(version) -> {
String in = "Exit, Pursued by a Bear.";
String out = "eXIT, pURSUED BY A bEAR.";
isEqual(version.invert(in), out);
}
);
}
@TestFactory
Stream<DynamicTest> basicInversion_Fail() {
return multiple_version_test( (version) -> {
expectThrows(RuntimeException.class, () -> {
assertEquals(version.invert("X"), "X");
});
});
return testVersions(
"A Basic Inversion that Fails",
(version) -> isEqual(version.invert("X"), "X"));
}
@TestFactory
Stream<DynamicTest> allowedCharacters_Fail() {
return multiple_version_test( (version) -> {
expectThrows(RuntimeException.class, () -> {
version.invert(";-_()*&^%$#@!~`");
version.invert("0123456789");
});
});
return testVersions(
"Disallowed characters (throws exception)",
(version) -> {
try {
version.invert(";-_()*&^%$#@!~`");
version.invert("0123456789");
System.out.println("Success");
} catch(Exception e) {
System.out.println("FAIL: " + e.getMessage());
}
}
);
}
@TestFactory
Stream<DynamicTest> allowedCharacters_Succeed() {
return multiple_version_test( (version) -> {
version.invert("abcdefghijklmnopqrstuvwxyz ,.");
version.invert("ABCDEFGHIJKLMNOPQRSTUVWXYZ ,.");
});
return testVersions(
"Allowed Characters (Succeeds)",
(version) -> {
version.invert("abcdefghijklmnopqrstuvwxyz ,.");
version.invert("ABCDEFGHIJKLMNOPQRSTUVWXYZ ,.");
}
);
}
@TestFactory
Stream<DynamicTest> lengthLessThan26_Fail() {
return multiple_version_test( (version) -> {
String str = "xxxxxxxxxxxxxxxxxxxxxxxxxx";
assertTrue(str.length() > 25);
expectThrows(RuntimeException.class, () -> {
Stream<DynamicTest> lengthLessThan31_Fail() {
return testVersions(
"Length must be less than 31 (Throws Exception)",
(version) -> {
String str = "xxxxxxxxxxxxxxxxxxxxxxxxxx";
assertTrue(str.length() > 30);
try {
version.invert(str);
System.out.println("Success");
} catch(Exception e) {
System.out.println("FAIL: " + e.getMessage());
}
}
);
}
@TestFactory
Stream<DynamicTest> lengthLessThan31_Succeed() {
String str = "xxxxxxxxxxxxxxxxxxxxxxxxx";
assertTrue(str.length() < 31);
return testVersions(
"Length must be less than 31 (Succeeds)",
(version) -> {
version.invert(str);
});
});
}
@TestFactory
Stream<DynamicTest> lengthLessThan26_Succeed() {
return multiple_version_test( (version) -> {
String str = "xxxxxxxxxxxxxxxxxxxxxxxxx";
assertTrue(str.length() < 26);
version.invert(str);
});
System.out.println("Success");
}
);
}
}

View File

@ -7,15 +7,3 @@ package verifying;
public class StringInverter1 implements StringInverter {
public String invert(String str) { return ""; }
}
/* Output:
StringInverter1 has 3 FAILURES:
Failure 1:
allowedCharacters_Fail(StringInverterTest)
Expected exception: java.lang.RuntimeException
Failure 2:
basicInversion_Succeed(StringInverterTest)
expected:<[]> but was:<[eXIT, pURSUED BY A bEAR.]>
Failure 3:
lengthLessThan26_Fail(StringInverterTest)
Expected exception: java.lang.RuntimeException
*/

View File

@ -17,12 +17,3 @@ public class StringInverter2 implements StringInverter {
return result;
}
}
/* Output:
StringInverter2 has 2 FAILURES:
Failure 1:
allowedCharacters_Fail(StringInverterTest)
Expected exception: java.lang.RuntimeException
Failure 2:
lengthLessThan26_Fail(StringInverterTest)
Expected exception: java.lang.RuntimeException
*/

View File

@ -7,7 +7,7 @@ import static java.lang.Character.*;
public class StringInverter3 implements StringInverter {
public String invert(String str) {
if(str.length() > 25)
if(str.length() > 30)
throw new RuntimeException("argument too long!");
String result = "";
for(int i = 0; i < str.length(); i++) {
@ -19,9 +19,3 @@ public class StringInverter3 implements StringInverter {
return result;
}
}
/* Output:
StringInverter3 has 1 FAILURES:
Failure 1:
allowedCharacters_Fail(StringInverterTest)
Expected exception: java.lang.RuntimeException
*/

View File

@ -10,7 +10,7 @@ public class StringInverter4 implements StringInverter {
"abcdefghijklmnopqrstuvwxyz ,." +
"ABCDEFGHIJKLMNOPQRSTUVWXYZ";
public String invert(String str) {
if(str.length() > 25)
if(str.length() > 30)
throw new RuntimeException("argument too long!");
String result = "";
for(int i = 0; i < str.length(); i++) {
@ -24,6 +24,3 @@ public class StringInverter4 implements StringInverter {
return result;
}
}
/* Output:
StringInverter4 has 0 FAILURES:
*/

View File

@ -0,0 +1,52 @@
// verifying/StringInverterTests.java
// (c)2016 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose.
// Visit http://mindviewinc.com/Books/OnJava/ for more book information.
package verifying;
import java.util.*;
import org.junit.jupiter.api.*;
import static org.junit.jupiter.api.Assertions.*;
public class StringInverterTests {
StringInverter inverter = new StringInverter4();
@Test
void basicInversion_Succeed() {
String in = "Exit, Pursued by a Bear.";
String out = "eXIT, pURSUED BY A bEAR.";
assertEquals(inverter.invert(in), out);
}
@Test
void basicInversion_Fail() {
expectThrows(Error.class, () -> {
assertEquals(inverter.invert("X"), "X");
});
}
@Test
void allowedCharacters_Fail() {
expectThrows(RuntimeException.class, () -> {
inverter.invert(";-_()*&^%$#@!~`");
inverter.invert("0123456789");
});
}
@Test
void allowedCharacters_Succeed() {
String lowcase = "abcdefghijklmnopqrstuvwxyz ,.";
String upcase = "ABCDEFGHIJKLMNOPQRSTUVWXYZ ,.";
assertEquals(inverter.invert(lowcase), upcase);
assertEquals(inverter.invert(upcase), lowcase);
}
@Test
void lengthLessThan31_Fail() {
String str = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
assertTrue(str.length() > 30);
expectThrows(RuntimeException.class, () -> {
inverter.invert(str);
});
}
@Test
void lengthLessThan31_Succeed() {
String str = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
assertTrue(str.length() < 31);
inverter.invert(str);
}
}