Skip to content

Commit 8d3fd67

Browse files
committed
Added AboutDates, AboutLocale and AboutRegularExpressions
1 parent 5a57a01 commit 8d3fd67

4 files changed

Lines changed: 200 additions & 0 deletions

File tree

‎koans/src/PathToEnlightment.xml‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,16 @@
169169
<koan name="makeObjectsComparable" />
170170
<koan name="makeObjectsComparableWithoutComparable" />
171171
</suite>
172+
<suite class="AboutDates">
173+
<koan name="dateToString" />
174+
<koan name="changingDateValue" />
175+
<koan name="usingCalendarToChangeDates" />
176+
<koan name="usingRollToChangeDatesDoesntWrapOtherFields" />
177+
<koan name="usingDateFormatToFormatDate" />
178+
<koan name="usingDateFormatToFormatDateShort" />
179+
<koan name="usingDateFormatToFormatDateFull" />
180+
<koan name="usingDateFormatToParseDates" />
181+
</suite>
172182
<suite class="AboutEquality">
173183
<koan name="sameObject" />
174184
<koan name="equalObject" />
@@ -200,6 +210,17 @@
200210
<koan name="staticInnerClass" />
201211
<koan name="staticInnerClassFullyQualified" />
202212
</suite>
213+
<suite class="AboutLocale">
214+
<koan name="localizedOutputOfDates" />
215+
<koan name="getCountryInformation" />
216+
<koan name="formatCurrency" />
217+
</suite>
218+
<suite class="AboutRegularExpressions">
219+
<koan name="basicMatching" />
220+
<koan name="extendedMatching" />
221+
<koan name="escapingMetaCharacters" />
222+
<koan name="splittingStrings" />
223+
</suite>
203224
<suite class="AboutSerialization">
204225
<koan name="simpleSerialization" />
205226
<koan name="customObjectSerialization" />
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
package intermediate;
2+
3+
import static com.sandwich.koan.constant.KoanConstants.__;
4+
import static com.sandwich.util.Assert.assertEquals;
5+
6+
import java.text.DateFormat;
7+
import java.text.ParseException;
8+
import java.util.Calendar;
9+
import java.util.Date;
10+
11+
import com.sandwich.koan.Koan;
12+
13+
14+
public class AboutDates {
15+
16+
private Date date = new Date(100010001000L);
17+
18+
@Koan
19+
public void dateToString() {
20+
assertEquals(date.toString(), __);
21+
}
22+
23+
@Koan
24+
public void changingDateValue() {
25+
int oneHourInMiliseconds = 360000;
26+
date.setTime(date.getTime() + oneHourInMiliseconds);
27+
assertEquals(date.toString(), __);
28+
}
29+
30+
@Koan
31+
public void usingCalendarToChangeDates() {
32+
Calendar cal = Calendar.getInstance();
33+
cal.setTime(date);
34+
cal.add(Calendar.MONTH, 1);
35+
assertEquals(cal.getTime().toString(), __);
36+
}
37+
38+
@Koan
39+
public void usingRollToChangeDatesDoesntWrapOtherFields() {
40+
Calendar cal = Calendar.getInstance();
41+
cal.setTime(date);
42+
cal.roll(Calendar.MONTH, 12);
43+
assertEquals(cal.getTime().toString(), __);
44+
}
45+
46+
@Koan
47+
public void usingDateFormatToFormatDate() {
48+
String formattedDate = DateFormat.getDateInstance().format(date);
49+
assertEquals(formattedDate, __);
50+
}
51+
52+
@Koan
53+
public void usingDateFormatToFormatDateShort() {
54+
String formattedDate = DateFormat.getDateInstance(DateFormat.SHORT).format(date);
55+
assertEquals(formattedDate, __);
56+
}
57+
58+
@Koan
59+
public void usingDateFormatToFormatDateFull() {
60+
String formattedDate = DateFormat.getDateInstance(DateFormat.FULL).format(date);
61+
// There is also DateFormat.MEDIUM and DateFormat.LONG... you get the idea ;-)
62+
assertEquals(formattedDate, __);
63+
}
64+
65+
@Koan
66+
public void usingDateFormatToParseDates() throws ParseException {
67+
DateFormat dateFormat = DateFormat.getDateInstance(DateFormat.SHORT);
68+
Date date2 = dateFormat.parse("Sat Sep 01 12:34:46 CET 2001");
69+
assertEquals(date2.toString(), __);
70+
// What happened to the time? What do you need to change to keep the time as well?
71+
}
72+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package intermediate;
2+
3+
import static com.sandwich.koan.constant.KoanConstants.__;
4+
import static com.sandwich.util.Assert.assertEquals;
5+
6+
import java.text.DateFormat;
7+
import java.text.NumberFormat;
8+
import java.util.Calendar;
9+
import java.util.Date;
10+
import java.util.Locale;
11+
12+
import com.sandwich.koan.Koan;
13+
14+
public class AboutLocale {
15+
16+
@Koan
17+
public void localizedOutputOfDates() {
18+
Calendar cal = Calendar.getInstance();
19+
cal.set(2011, 3, 3);
20+
Date date = cal.getTime();
21+
Locale localeBR = new Locale("pt","BR"); // portuguese, Brazil
22+
DateFormat dateformatBR = DateFormat.getDateInstance(DateFormat.FULL, localeBR);
23+
assertEquals(dateformatBR.format(date), __);
24+
25+
Locale localeJA = new Locale("ja"); // Japan
26+
DateFormat dateformatJA = DateFormat.getDateInstance(DateFormat.FULL, localeJA);
27+
// Well if you don't know how to type these characters, try "de", "it" or "us" ;-)
28+
assertEquals(dateformatJA.format(date), __);
29+
}
30+
31+
@Koan
32+
public void getCountryInformation() {
33+
Locale locBR = new Locale("pt","BR");
34+
assertEquals(locBR.getDisplayCountry(), __);
35+
assertEquals(locBR.getDisplayCountry(locBR), __);
36+
37+
Locale locCH = new Locale("it","CH");
38+
assertEquals(locCH.getDisplayCountry(), __);
39+
assertEquals(locCH.getDisplayCountry(locCH), __);
40+
assertEquals(locCH.getDisplayCountry(new Locale("de","CH")), __);
41+
}
42+
43+
@Koan
44+
public void formatCurrency() {
45+
float someAmount = 442.23f; // Don't use floats for money in real life. Really. It's a bad idea.
46+
Locale locBR = new Locale("pt","BR");
47+
NumberFormat nf = NumberFormat.getCurrencyInstance(locBR);
48+
assertEquals(nf.format(someAmount), __);
49+
}
50+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package intermediate;
2+
3+
4+
import static com.sandwich.koan.constant.KoanConstants.__;
5+
import static com.sandwich.util.Assert.assertEquals;
6+
7+
import java.util.regex.Matcher;
8+
import java.util.regex.Pattern;
9+
10+
import com.sandwich.koan.Koan;
11+
12+
13+
public class AboutRegularExpressions {
14+
15+
@Koan
16+
public void basicMatching() {
17+
Pattern p = Pattern.compile("xyz");
18+
Matcher m = p.matcher("xyzxxxxyz");
19+
// index 012345678
20+
assertEquals(m.find(), __);
21+
assertEquals(m.start(), __);
22+
assertEquals(m.find(), __);
23+
assertEquals(m.start(), __);
24+
assertEquals(m.find(), __);
25+
}
26+
27+
@Koan
28+
public void extendedMatching() {
29+
Pattern p = Pattern.compile("x.z");
30+
Matcher m = p.matcher("xyz u x z u xfz");
31+
// index 012345678901234
32+
assertEquals(m.find(), __);
33+
assertEquals(m.start(), __);
34+
assertEquals(m.find(), __);
35+
assertEquals(m.start(), __);
36+
assertEquals(m.find(), __);
37+
assertEquals(m.start(), __);
38+
}
39+
40+
@Koan
41+
public void escapingMetaCharacters() {
42+
Pattern p = Pattern.compile("end\\.");
43+
Matcher m = p.matcher("begin. end.");
44+
// index 01234567890
45+
assertEquals(m.find(), __);
46+
assertEquals(m.start(), __);
47+
}
48+
49+
@Koan
50+
public void splittingStrings() {
51+
String csvDataLine = "1,name,description";
52+
String[] data = csvDataLine.split(","); // you can use any regex here
53+
assertEquals(data[0], __);
54+
assertEquals(data[1], __);
55+
assertEquals(data[2], __);
56+
}
57+
}

0 commit comments

Comments
 (0)