Skip to content

Commit e6e0c29

Browse files
committed
Added AboutLoops and more koans for AboutConditions. Also added intermediate topic AboutSerialization
1 parent 76a730f commit e6e0c29

4 files changed

Lines changed: 400 additions & 0 deletions

File tree

‎koans/src/PathToEnlightment.xml‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,17 @@
5555
<Koan name="toStringIsTestedForNullWhenInvokedImplicitly"
5656
lesson="String concatenation implicitly invokes toString on Objects, unless they are null. Notice no NullPointerException is thrown."/>
5757
</Suite>
58+
<Suite class="AboutLoops">
59+
<Koan name="basicForLoop" />
60+
<Koan name="basicForLoopWithTwoVariables" />
61+
<Koan name="extendedForLoop" />
62+
<Koan name="whileLoop" />
63+
<Koan name="doLoop" />
64+
<Koan name="extendedForLoopBreak" />
65+
<Koan name="extendedForLoopContinue" />
66+
<Koan name="forLoopContinueLabel" />
67+
<Koan name="forLoopBreakLabel" />
68+
</Suite>
5869
<Suite class="AboutCasting">
5970
<Koan name="longPlusInt" />
6071
<Koan name="forceIntTypecast" />
@@ -72,6 +83,11 @@
7283
<Koan name="basicIfElseIfElseWithoutCurly" />
7384
<Koan name="nestedIfsWithoutCurlysAreReallyMisleading" />
7485
<Koan name="ifAsIntended" />
86+
<Koan name="basicSwitchStatement" />
87+
<Koan name="switchStatementFallThrough" />
88+
<Koan name="switchStatementCrazyFallThrough" />
89+
<Koan name="switchStatementConstants" />
90+
<Koan name="switchStatementSwitchValues" />
7591
</Suite>
7692
<Suite class="AboutInheritance">
7793
<Koan name="differenceBetweenOverloadingAndOverriding" />
@@ -145,6 +161,14 @@
145161
<Koan name="allPrimativesCanBeAutoboxed"
146162
lesson="All primatives can be autoboxed"/>
147163
</Suite>
164+
<Suite class="AboutSerialization">
165+
<Koan name="simpleSerialization" />
166+
<Koan name="customObjectSerialization" />
167+
<Koan name="customObjectSerializationWithTransientFields" />
168+
<Koan name="customSerializationWithUnserializableFields" />
169+
<Koan name="serializeWithInteritance" />
170+
<Koan name="serializeWithInheritanceWhenParentNotserializable" />
171+
</Suite>
148172
</Package>
149173
<Package pkg="advanced" name="Advanced">
150174
<Suite class="AboutMocks">

‎koans/src/beginner/AboutConditionals.java‎

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,4 +72,84 @@ public void ifAsIntended() {
7272
assertEquals(x,__);
7373
}
7474

75+
@Koan
76+
public void basicSwitchStatement() {
77+
int i = 1;
78+
String result = "Basic ";
79+
switch(i) {
80+
case 1:
81+
result += "One";
82+
break;
83+
case 2:
84+
result += "Two";
85+
break;
86+
default:
87+
result += "Nothing";
88+
}
89+
assertEquals(result, __);
90+
}
91+
92+
@Koan
93+
public void switchStatementFallThrough() {
94+
int i = 1;
95+
String result = "Basic ";
96+
switch(i) {
97+
case 1:
98+
result += "One";
99+
case 2:
100+
result += "Two";
101+
default:
102+
result += "Nothing";
103+
}
104+
assertEquals(result, __);
105+
}
106+
107+
@Koan
108+
public void switchStatementCrazyFallThrough() {
109+
int i = 5;
110+
String result = "Basic ";
111+
switch(i) {
112+
case 1:
113+
result += "One";
114+
default:
115+
result += "Nothing";
116+
case 2:
117+
result += "Two";
118+
}
119+
assertEquals(result, __);
120+
}
121+
122+
@Koan
123+
public void switchStatementConstants() {
124+
int i = 5;
125+
// What happens if you remove the 'final' modifier?
126+
// What does this mean for case values?
127+
final int caseOne = 1;
128+
String result = "Basic ";
129+
switch(i) {
130+
case caseOne:
131+
result += "One";
132+
break;
133+
default:
134+
result += "Nothing";
135+
}
136+
assertEquals(result, __);
137+
}
138+
139+
@Koan
140+
public void switchStatementSwitchValues() {
141+
// Try different (primitive) types for 'c'
142+
// Which types do compile?
143+
// Does boxing work?
144+
byte c = 'a';
145+
String result = "Basic ";
146+
switch(c) {
147+
case 'a':
148+
result += "One";
149+
break;
150+
default:
151+
result += "Nothing";
152+
}
153+
assertEquals(result, __);
154+
}
75155
}

‎koans/src/beginner/AboutLoops.java‎

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
package beginner;
2+
3+
4+
import com.sandwich.koan.Koan;
5+
import static com.sandwich.koan.constant.KoanConstants.__;
6+
import static com.sandwich.util.Assert.assertEquals;
7+
8+
9+
public class AboutLoops {
10+
11+
@Koan
12+
public void basicForLoop() {
13+
String s = "";
14+
for(int i = 0; i < 5; i++) {
15+
s += i + " ";
16+
}
17+
assertEquals(s, __);
18+
}
19+
20+
@Koan
21+
public void basicForLoopWithTwoVariables() {
22+
String s = "";
23+
for(int i = 0, j = 10; i < 5 && j > 5; i++, j--) {
24+
s += i + " " + j + " ";
25+
}
26+
assertEquals(s, __);
27+
}
28+
29+
@Koan
30+
public void extendedForLoop() {
31+
int[] is = {1,2,3,4};
32+
String s = "-";
33+
for(int j : is) {
34+
s += "." + j;
35+
}
36+
assertEquals(s, __);
37+
}
38+
39+
@Koan
40+
public void whileLoop() {
41+
int result = 0;
42+
while(result < 3) {
43+
result++;
44+
}
45+
assertEquals(result, __);
46+
}
47+
48+
@Koan
49+
public void doLoop() {
50+
int result = 0;
51+
do {
52+
result++;
53+
} while(false);
54+
assertEquals(result, __);
55+
}
56+
57+
@Koan
58+
public void extendedForLoopBreak() {
59+
String[] sa = {"Dog", "Cat", "Tiger" };
60+
int count = 0;
61+
for(String current : sa) {
62+
if("Cat".equals(current)) {
63+
break;
64+
}
65+
count++;
66+
}
67+
assertEquals(count, __);
68+
}
69+
70+
@Koan
71+
public void extendedForLoopContinue() {
72+
String[] sa = {"Dog", "Cat", "Tiger" };
73+
int count = 0;
74+
for(String current : sa) {
75+
if("Dog".equals(current)) {
76+
continue;
77+
} else {
78+
count++;
79+
}
80+
}
81+
assertEquals(count, __);
82+
}
83+
84+
85+
@Koan
86+
public void forLoopContinueLabel() {
87+
int count = 0;
88+
outerLabel:
89+
for(int i = 0; i < 5; i++) {
90+
for(int j = 0; j < 5; j++)
91+
{
92+
count++;
93+
if(count > 2) {
94+
continue outerLabel;
95+
}
96+
}
97+
count += 10;
98+
}
99+
// What does continue with a label mean?
100+
// What gets executed? Where does the program flow continue?
101+
assertEquals(count, __);
102+
}
103+
104+
@Koan
105+
public void forLoopBreakLabel() {
106+
int count = 0;
107+
outerLabel:
108+
for(int i = 0; i < 5; i++) {
109+
for(int j = 0; j < 5; j++)
110+
{
111+
count++;
112+
if(count > 2) {
113+
break outerLabel;
114+
}
115+
}
116+
count += 10;
117+
}
118+
// What does break with a label mean?
119+
// What gets executed? Where does the program flow continue?
120+
assertEquals(count, __);
121+
}
122+
123+
}

0 commit comments

Comments
 (0)