Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitcfaacad

Browse files
committed
TDD section (not great yet, but runs)
1 parent0dbbb64 commitcfaacad

File tree

7 files changed

+178
-1
lines changed

7 files changed

+178
-1
lines changed

‎verifying/StringInverter.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// verifying/StringInverter.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+
6+
interfaceStringInverter {
7+
publicStringinvert(Stringstr);
8+
}

‎verifying/StringInverter1.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// verifying/StringInverter1.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+
// {java StringInverterTest StringInverter1}
6+
7+
publicclassStringInverter1implementsStringInverter {
8+
publicStringinvert(Stringstr) {return""; }
9+
}
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: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// verifying/StringInverter2.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+
// {java StringInverterTest StringInverter2}
6+
importstaticjava.lang.Character.*;
7+
8+
publicclassStringInverter2implementsStringInverter {
9+
publicStringinvert(Stringstr) {
10+
Stringresult ="";
11+
for(inti =0;i <str.length();i++) {
12+
charc =str.charAt(i);
13+
result +=isUpperCase(c) ?
14+
toLowerCase(c) :
15+
toUpperCase(c);
16+
}
17+
returnresult;
18+
}
19+
}
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: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// verifying/StringInverter3.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+
// {java StringInverterTest StringInverter3}
6+
importstaticjava.lang.Character.*;
7+
8+
publicclassStringInverter3implementsStringInverter {
9+
publicStringinvert(Stringstr) {
10+
if(str.length() >25)
11+
thrownewRuntimeException("argument too long!");
12+
Stringresult ="";
13+
for(inti =0;i <str.length();i++) {
14+
charc =str.charAt(i);
15+
result +=isUpperCase(c) ?
16+
toLowerCase(c) :
17+
toUpperCase(c);
18+
}
19+
returnresult;
20+
}
21+
}
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: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// verifying/StringInverter4.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+
// {java StringInverterTest StringInverter4}
6+
importstaticjava.lang.Character.*;
7+
8+
publicclassStringInverter4implementsStringInverter {
9+
staticfinalStringallowed =
10+
"abcdefghijklmnopqrstuvwxyz ,." +
11+
"ABCDEFGHIJKLMNOPQRSTUVWXYZ";
12+
publicStringinvert(Stringstr) {
13+
if(str.length() >25)
14+
thrownewRuntimeException("argument too long!");
15+
Stringresult ="";
16+
for(inti =0;i <str.length();i++) {
17+
charc =str.charAt(i);
18+
if(allowed.indexOf(c) == -1)
19+
thrownewRuntimeException(c +" Not allowed");
20+
result +=isUpperCase(c) ?
21+
toLowerCase(c) :
22+
toUpperCase(c);
23+
}
24+
returnresult;
25+
}
26+
}
27+
/* Output:
28+
StringInverter4 has 0 FAILURES:
29+
*/

‎verifying/StringInverterTest.java

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
// verifying/StringInverterTest.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+
// {ValidateByHand} // Don't run by itself
6+
importjava.util.*;
7+
importorg.junit.*;
8+
importstaticorg.junit.Assert.*;
9+
importorg.junit.runner.*;
10+
importorg.junit.runner.notification.Failure;
11+
12+
publicclassStringInverterTest {
13+
staticStringInverterinverter;
14+
@Test
15+
publicfinalvoidbasicInversion_Succeed() {
16+
Stringin ="Exit, Pursued by a Bear.";
17+
Stringout ="eXIT, pURSUED BY A bEAR.";
18+
assertEquals(inverter.invert(in),out);
19+
}
20+
@Test(expected =ComparisonFailure.class)
21+
publicfinalvoidbasicInversion_Fail() {
22+
assertEquals(inverter.invert("X"),"X");
23+
}
24+
@Test(expected =RuntimeException.class)
25+
publicfinalvoidallowedCharacters_Fail() {
26+
inverter.invert(";-_()*&^%$#@!~`");
27+
inverter.invert("0123456789");
28+
}
29+
/*@Test
30+
public final void allowedCharacters_Succeed() {
31+
inverter.invert("abcdefghijklmnopqrstuvwxyz ,.");
32+
inverter.invert("ABCDEFGHIJKLMNOPQRSTUVWXYZ ,.");
33+
assertTrue(true); // No exception thrown
34+
}*/
35+
@Test(expected =RuntimeException.class)
36+
publicfinalvoidlengthLessThan26_Fail() {
37+
Stringstr ="xxxxxxxxxxxxxxxxxxxxxxxxxx";
38+
assertTrue(str.length() >25);
39+
inverter.invert(str);
40+
}
41+
@Test
42+
publicfinalvoidlengthLessThan26_Succeed() {
43+
Stringstr ="xxxxxxxxxxxxxxxxxxxxxxxxx";
44+
assertTrue(str.length() <26);
45+
inverter.invert(str);
46+
assertTrue(true);// No exception thrown
47+
}
48+
publicstaticvoidmain(String[]args)throwsException{
49+
assertEquals(args.length,1);
50+
inverter = (StringInverter)
51+
Class.forName(args[0]).newInstance();
52+
Resultresult =JUnitCore.runClasses(
53+
StringInverterTest.class);
54+
List<Failure>failures =result.getFailures();
55+
System.out.printf("%s has %d FAILURES:\n",
56+
args[0],failures.size());
57+
intcount =1;
58+
for(Failuref :failures) {
59+
System.out.printf("Failure %d:\n",count++);
60+
System.out.println(f.getDescription());
61+
System.out.println(f.getMessage());
62+
}
63+
}
64+
}

‎verifying/logback.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,4 @@
99
<rootlevel="TRACE">
1010
<appender-refref="STDOUT" />
1111
</root>
12-
</configuration>
12+
</configuration>

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp