Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 941213a

Browse files
committed
TDD Much better now
1 parent 9c838f9 commit 941213a

8 files changed

+143
-76
lines changed

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ subprojects {
179179
java {
180180
srcDir projectDir
181181
// Remove this when it's working:
182-
exclude "DynamicStringInverterTests.java"
182+
// exclude "DynamicStringInverterTests.java"
183183
}
184184
resources {
185185
srcDir projectDir

gradle.properties

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1 @@
1-
org.gradle.daemon=true
21
org.gradle.parallel=true

verifying/DynamicStringInverterTests.java

Lines changed: 88 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -11,70 +11,116 @@
1111
import static org.junit.jupiter.api.DynamicTest.*;
1212

1313
class DynamicStringInverterTests {
14+
// Combine operations to prevent code duplication:
1415
Stream<DynamicTest>
15-
multiple_version_test(Consumer<StringInverter> test) {
16+
testVersions(String id, Consumer<StringInverter> test) {
1617
List<StringInverter> versions = Arrays.asList(
1718
new StringInverter1(), new StringInverter2(),
1819
new StringInverter3(), new StringInverter4());
1920
return DynamicTest.stream(versions.iterator(),
2021
(version) -> version.getClass().getSimpleName(),
21-
test
22+
(stringInverter) -> {
23+
System.out.println(
24+
stringInverter.getClass().getSimpleName() +
25+
": " + id
26+
);
27+
try { // Capture failing tests
28+
test.accept(stringInverter);
29+
} catch(Error | Exception e) {
30+
System.out.println(e.getMessage());
31+
}
32+
}
2233
);
2334
}
24-
35+
void isTrue(String description, boolean assertion) {
36+
System.out.print(description + ": ");
37+
System.out.println(assertion);
38+
}
39+
void isEqual(String lval, String rval) {
40+
System.out.print(lval + " equals " + rval);
41+
if(!lval.equals(rval))
42+
System.out.println(" FAIL");
43+
else
44+
System.out.println();
45+
}
46+
@BeforeAll
47+
static void startMsg() {
48+
System.out.println(
49+
">>> Starting DynamicStringInverterTests <<<");
50+
}
51+
@AfterAll
52+
static void endMsg() {
53+
System.out.println(
54+
">>> Finished DynamicStringInverterTests <<<");
55+
}
2556
@TestFactory
2657
Stream<DynamicTest> basicInversion_Succeed() {
27-
return multiple_version_test( (version) -> {
28-
String in = "Exit, Pursued by a Bear.";
29-
String out = "eXIT, pURSUED BY A bEAR.";
30-
assertEquals(version.invert(in), out);
31-
});
58+
return testVersions(
59+
"A Basic Inversion that Succeeds",
60+
(version) -> {
61+
String in = "Exit, Pursued by a Bear.";
62+
String out = "eXIT, pURSUED BY A bEAR.";
63+
isEqual(version.invert(in), out);
64+
}
65+
);
3266
}
33-
3467
@TestFactory
3568
Stream<DynamicTest> basicInversion_Fail() {
36-
return multiple_version_test( (version) -> {
37-
expectThrows(RuntimeException.class, () -> {
38-
assertEquals(version.invert("X"), "X");
39-
});
40-
});
69+
return testVersions(
70+
"A Basic Inversion that Fails",
71+
(version) -> isEqual(version.invert("X"), "X"));
4172
}
42-
4373
@TestFactory
4474
Stream<DynamicTest> allowedCharacters_Fail() {
45-
return multiple_version_test( (version) -> {
46-
expectThrows(RuntimeException.class, () -> {
47-
version.invert(";-_()*&^%$#@!~`");
48-
version.invert("0123456789");
49-
});
50-
});
75+
return testVersions(
76+
"Disallowed characters (throws exception)",
77+
(version) -> {
78+
try {
79+
version.invert(";-_()*&^%$#@!~`");
80+
version.invert("0123456789");
81+
System.out.println("Success");
82+
} catch(Exception e) {
83+
System.out.println("FAIL: " + e.getMessage());
84+
}
85+
}
86+
);
5187
}
52-
5388
@TestFactory
5489
Stream<DynamicTest> allowedCharacters_Succeed() {
55-
return multiple_version_test( (version) -> {
56-
version.invert("abcdefghijklmnopqrstuvwxyz ,.");
57-
version.invert("ABCDEFGHIJKLMNOPQRSTUVWXYZ ,.");
58-
});
90+
return testVersions(
91+
"Allowed Characters (Succeeds)",
92+
(version) -> {
93+
version.invert("abcdefghijklmnopqrstuvwxyz ,.");
94+
version.invert("ABCDEFGHIJKLMNOPQRSTUVWXYZ ,.");
95+
}
96+
);
5997
}
60-
6198
@TestFactory
62-
Stream<DynamicTest> lengthLessThan26_Fail() {
63-
return multiple_version_test( (version) -> {
64-
String str = "xxxxxxxxxxxxxxxxxxxxxxxxxx";
65-
assertTrue(str.length() > 25);
66-
expectThrows(RuntimeException.class, () -> {
67-
version.invert(str);
68-
});
69-
});
99+
Stream<DynamicTest> lengthLessThan31_Fail() {
100+
return testVersions(
101+
"Length must be less than 31 (Throws Exception)",
102+
(version) -> {
103+
String str = "xxxxxxxxxxxxxxxxxxxxxxxxxx";
104+
assertTrue(str.length() > 30);
105+
try {
106+
version.invert(str);
107+
System.out.println("Success");
108+
} catch(Exception e) {
109+
System.out.println("FAIL: " + e.getMessage());
110+
}
111+
}
112+
);
70113
}
71-
72114
@TestFactory
73-
Stream<DynamicTest> lengthLessThan26_Succeed() {
74-
return multiple_version_test( (version) -> {
75-
String str = "xxxxxxxxxxxxxxxxxxxxxxxxx";
76-
assertTrue(str.length() < 26);
77-
version.invert(str);
78-
});
115+
Stream<DynamicTest> lengthLessThan31_Succeed() {
116+
String str = "xxxxxxxxxxxxxxxxxxxxxxxxx";
117+
assertTrue(str.length() < 31);
118+
return testVersions(
119+
"Length must be less than 31 (Succeeds)",
120+
(version) -> {
121+
version.invert(str);
122+
System.out.println("Success");
123+
}
124+
);
79125
}
80126
}

verifying/StringInverter1.java

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,3 @@
77
public class StringInverter1 implements StringInverter {
88
public String invert(String str) { return ""; }
99
}
10-
/* Output:
11-
StringInverter1 has 3 FAILURES:
12-
Failure 1:
13-
allowedCharacters_Fail(StringInverterTest)
14-
Expected exception: java.lang.RuntimeException
15-
Failure 2:
16-
basicInversion_Succeed(StringInverterTest)
17-
expected:<[]> but was:<[eXIT, pURSUED BY A bEAR.]>
18-
Failure 3:
19-
lengthLessThan26_Fail(StringInverterTest)
20-
Expected exception: java.lang.RuntimeException
21-
*/

verifying/StringInverter2.java

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,3 @@ public String invert(String str) {
1717
return result;
1818
}
1919
}
20-
/* Output:
21-
StringInverter2 has 2 FAILURES:
22-
Failure 1:
23-
allowedCharacters_Fail(StringInverterTest)
24-
Expected exception: java.lang.RuntimeException
25-
Failure 2:
26-
lengthLessThan26_Fail(StringInverterTest)
27-
Expected exception: java.lang.RuntimeException
28-
*/

verifying/StringInverter3.java

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
public class StringInverter3 implements StringInverter {
99
public String invert(String str) {
10-
if(str.length() > 25)
10+
if(str.length() > 30)
1111
throw new RuntimeException("argument too long!");
1212
String result = "";
1313
for(int i = 0; i < str.length(); i++) {
@@ -19,9 +19,3 @@ public String invert(String str) {
1919
return result;
2020
}
2121
}
22-
/* Output:
23-
StringInverter3 has 1 FAILURES:
24-
Failure 1:
25-
allowedCharacters_Fail(StringInverterTest)
26-
Expected exception: java.lang.RuntimeException
27-
*/

verifying/StringInverter4.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ public class StringInverter4 implements StringInverter {
1010
"abcdefghijklmnopqrstuvwxyz ,." +
1111
"ABCDEFGHIJKLMNOPQRSTUVWXYZ";
1212
public String invert(String str) {
13-
if(str.length() > 25)
13+
if(str.length() > 30)
1414
throw new RuntimeException("argument too long!");
1515
String result = "";
1616
for(int i = 0; i < str.length(); i++) {
@@ -24,6 +24,3 @@ public String invert(String str) {
2424
return result;
2525
}
2626
}
27-
/* Output:
28-
StringInverter4 has 0 FAILURES:
29-
*/

verifying/StringInverterTests.java

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
// verifying/StringInverterTests.java
2+
// (c)2016 MindView LLC: see Copyright.txt
3+
// We make no guarantees that this code is fit for any purpose.
4+
// Visit http://mindviewinc.com/Books/OnJava/ for more book information.
5+
package verifying;
6+
import java.util.*;
7+
import org.junit.jupiter.api.*;
8+
import static org.junit.jupiter.api.Assertions.*;
9+
10+
public class StringInverterTests {
11+
StringInverter inverter = new StringInverter4();
12+
@Test
13+
void basicInversion_Succeed() {
14+
String in = "Exit, Pursued by a Bear.";
15+
String out = "eXIT, pURSUED BY A bEAR.";
16+
assertEquals(inverter.invert(in), out);
17+
}
18+
@Test
19+
void basicInversion_Fail() {
20+
expectThrows(Error.class, () -> {
21+
assertEquals(inverter.invert("X"), "X");
22+
});
23+
}
24+
@Test
25+
void allowedCharacters_Fail() {
26+
expectThrows(RuntimeException.class, () -> {
27+
inverter.invert(";-_()*&^%$#@!~`");
28+
inverter.invert("0123456789");
29+
});
30+
}
31+
@Test
32+
void allowedCharacters_Succeed() {
33+
String lowcase = "abcdefghijklmnopqrstuvwxyz ,.";
34+
String upcase = "ABCDEFGHIJKLMNOPQRSTUVWXYZ ,.";
35+
assertEquals(inverter.invert(lowcase), upcase);
36+
assertEquals(inverter.invert(upcase), lowcase);
37+
}
38+
@Test
39+
void lengthLessThan31_Fail() {
40+
String str = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
41+
assertTrue(str.length() > 30);
42+
expectThrows(RuntimeException.class, () -> {
43+
inverter.invert(str);
44+
});
45+
}
46+
@Test
47+
void lengthLessThan31_Succeed() {
48+
String str = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
49+
assertTrue(str.length() < 31);
50+
inverter.invert(str);
51+
}
52+
}

0 commit comments

Comments
 (0)