Skip to content

Commit 4d358a1

Browse files
author
David Reed
committed
Adding String.format() and MessageFormat.format() to AboutStrings
1 parent bbdbe72 commit 4d358a1

1 file changed

Lines changed: 55 additions & 16 deletions

File tree

Lines changed: 55 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,91 @@
11
package beginner;
22

3-
import static com.sandwich.koan.constant.KoanConstants.__;
4-
import static com.sandwich.util.Assert.assertEquals;
5-
63
import com.sandwich.koan.Koan;
74

5+
import java.text.MessageFormat;
6+
7+
import static com.sandwich.koan.constant.KoanConstants.*;
8+
import static com.sandwich.util.Assert.*;
9+
810
public class AboutStrings {
911

1012
@Koan
11-
public void implicitStrings(){
13+
public void implicitStrings() {
1214
assertEquals("just a plain ole string".getClass(), __);
1315
}
14-
16+
1517
@Koan
16-
public void newString(){
18+
public void newString() {
1719
// very rarely if ever should Strings be created via new String() in
1820
// practice - generally it is redundant, and done repetitively can be slow
1921
String string = new String();
2022
String empty = "";
2123
assertEquals(string.equals(empty), __);
2224
}
23-
25+
2426
@Koan
25-
public void newStringIsRedundant(){
27+
public void newStringIsRedundant() {
2628
String stringInstance = "zero";
27-
String stringReference = new String(stringInstance);
29+
String stringReference = new String(stringInstance);
2830
assertEquals(stringInstance.equals(stringReference), __);
2931
}
30-
32+
3133
@Koan
32-
public void newStringIsNotIdentical(){
34+
public void newStringIsNotIdentical() {
3335
String stringInstance = "zero";
3436
String stringReference = new String(stringInstance);
3537
assertEquals(stringInstance == stringReference, __);
3638
}
37-
39+
3840
@Koan
39-
public void stringConcatenation(){
41+
public void stringConcatenation() {
4042
String one = "one";
4143
String space = " ";
4244
String two = "two";
43-
assertEquals(one+space+two, __);
45+
assertEquals(one + space + two, __);
4446
}
45-
47+
4648
@Koan
47-
public void efficientStringConcatenation(){
49+
public void efficientStringConcatenation() {
4850
// the above implicit string concatenation looks nice - but, it is expensive.
4951
// it creates a new string instance on each concatenation. here's a better alternative:
5052
assertEquals(new StringBuilder("one").append(" ").append("two").toString(), __);
5153
}
54+
55+
@Koan
56+
public void readableStringFormattingWithStringFormat() {
57+
assertEquals(String.format("%s %s %s", "a", "b", "a"), __);
58+
}
59+
60+
@Koan
61+
public void extraArgumentsToStringFormatGetIgnored() {
62+
assertEquals(String.format("%s %s %s", "a", "b", "c", "d"), __);
63+
}
64+
65+
@Koan
66+
public void insufficientArgumentsToStringFormatCausesAnError() {
67+
try {
68+
String.format("%s %s %s", "a", "b");
69+
fail("No Exception was thrown!");
70+
} catch (Exception e) {
71+
assertEquals(e.getClass(), __);
72+
assertEquals(e.getMessage(), __);
73+
}
74+
}
75+
76+
@Koan
77+
public void readableStringFormattingWithMessageFormat() {
78+
assertEquals(MessageFormat.format("{0} {1} {0}", "a", "b"), __);
79+
}
80+
81+
@Koan
82+
public void extraArgumentsToMessageFormatGetIgnored() {
83+
assertEquals(MessageFormat.format("{0} {1} {0}", "a", "b", "c"), __);
84+
}
85+
86+
@Koan
87+
public void insufficientArgumentsToMessageFormatDoesNotReplaceTheToken() {
88+
assertEquals(MessageFormat.format("{0} {1} {0}", "a"), __);
89+
}
90+
5291
}

0 commit comments

Comments
 (0)