TDD section (not great yet, but runs)

This commit is contained in:
Bruce Eckel 2016-08-19 08:21:09 -06:00
parent 0dbbb64ef7
commit cfaacad48c
7 changed files with 178 additions and 1 deletions

View File

@ -0,0 +1,8 @@
// verifying/StringInverter.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.
interface StringInverter {
public String invert(String str);
}

View File

@ -0,0 +1,21 @@
// verifying/StringInverter1.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.
// {java StringInverterTest StringInverter1}
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

@ -0,0 +1,28 @@
// verifying/StringInverter2.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.
// {java StringInverterTest StringInverter2}
import static java.lang.Character.*;
public class StringInverter2 implements StringInverter {
public String invert(String str) {
String result = "";
for(int i = 0; i < str.length(); i++) {
char c = str.charAt(i);
result += isUpperCase(c) ?
toLowerCase(c) :
toUpperCase(c);
}
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

@ -0,0 +1,27 @@
// verifying/StringInverter3.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.
// {java StringInverterTest StringInverter3}
import static java.lang.Character.*;
public class StringInverter3 implements StringInverter {
public String invert(String str) {
if(str.length() > 25)
throw new RuntimeException("argument too long!");
String result = "";
for(int i = 0; i < str.length(); i++) {
char c = str.charAt(i);
result += isUpperCase(c) ?
toLowerCase(c) :
toUpperCase(c);
}
return result;
}
}
/* Output:
StringInverter3 has 1 FAILURES:
Failure 1:
allowedCharacters_Fail(StringInverterTest)
Expected exception: java.lang.RuntimeException
*/

View File

@ -0,0 +1,29 @@
// verifying/StringInverter4.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.
// {java StringInverterTest StringInverter4}
import static java.lang.Character.*;
public class StringInverter4 implements StringInverter {
static final String allowed =
"abcdefghijklmnopqrstuvwxyz ,." +
"ABCDEFGHIJKLMNOPQRSTUVWXYZ";
public String invert(String str) {
if(str.length() > 25)
throw new RuntimeException("argument too long!");
String result = "";
for(int i = 0; i < str.length(); i++) {
char c = str.charAt(i);
if(allowed.indexOf(c) == -1)
throw new RuntimeException(c + " Not allowed");
result += isUpperCase(c) ?
toLowerCase(c) :
toUpperCase(c);
}
return result;
}
}
/* Output:
StringInverter4 has 0 FAILURES:
*/

View File

@ -0,0 +1,64 @@
// verifying/StringInverterTest.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.
// {ValidateByHand} // Don't run by itself
import java.util.*;
import org.junit.*;
import static org.junit.Assert.*;
import org.junit.runner.*;
import org.junit.runner.notification.Failure;
public class StringInverterTest {
static StringInverter inverter;
@Test
public final void basicInversion_Succeed() {
String in = "Exit, Pursued by a Bear.";
String out = "eXIT, pURSUED BY A bEAR.";
assertEquals(inverter.invert(in), out);
}
@Test(expected = ComparisonFailure.class)
public final void basicInversion_Fail() {
assertEquals(inverter.invert("X"), "X");
}
@Test(expected = RuntimeException.class)
public final void allowedCharacters_Fail() {
inverter.invert(";-_()*&^%$#@!~`");
inverter.invert("0123456789");
}
/*@Test
public final void allowedCharacters_Succeed() {
inverter.invert("abcdefghijklmnopqrstuvwxyz ,.");
inverter.invert("ABCDEFGHIJKLMNOPQRSTUVWXYZ ,.");
assertTrue(true); // No exception thrown
}*/
@Test(expected = RuntimeException.class)
public final void lengthLessThan26_Fail() {
String str = "xxxxxxxxxxxxxxxxxxxxxxxxxx";
assertTrue(str.length() > 25);
inverter.invert(str);
}
@Test
public final void lengthLessThan26_Succeed() {
String str = "xxxxxxxxxxxxxxxxxxxxxxxxx";
assertTrue(str.length() < 26);
inverter.invert(str);
assertTrue(true); // No exception thrown
}
public static void main(String[] args) throws Exception{
assertEquals(args.length, 1);
inverter = (StringInverter)
Class.forName(args[0]).newInstance();
Result result = JUnitCore.runClasses(
StringInverterTest.class);
List<Failure> failures = result.getFailures();
System.out.printf("%s has %d FAILURES:\n",
args[0], failures.size());
int count = 1;
for(Failure f : failures) {
System.out.printf("Failure %d:\n", count++);
System.out.println(f.getDescription());
System.out.println(f.getMessage());
}
}
}

View File

@ -9,4 +9,4 @@
<root level="TRACE">
<appender-ref ref="STDOUT" />
</root>
</configuration>
</configuration>