|
1 | 1 | package beginner; |
2 | 2 |
|
3 | | -import static com.sandwich.koan.constant.KoanConstants.__; |
4 | | -import static com.sandwich.util.Assert.assertEquals; |
5 | | - |
6 | 3 | import com.sandwich.koan.Koan; |
7 | 4 |
|
| 5 | +import java.text.MessageFormat; |
| 6 | + |
| 7 | +import static com.sandwich.koan.constant.KoanConstants.*; |
| 8 | +import static com.sandwich.util.Assert.*; |
| 9 | + |
8 | 10 | public class AboutStrings { |
9 | 11 |
|
10 | 12 | @Koan |
11 | | - public void implicitStrings(){ |
| 13 | + public void implicitStrings() { |
12 | 14 | assertEquals("just a plain ole string".getClass(), __); |
13 | 15 | } |
14 | | - |
| 16 | + |
15 | 17 | @Koan |
16 | | - public void newString(){ |
| 18 | + public void newString() { |
17 | 19 | // very rarely if ever should Strings be created via new String() in |
18 | 20 | // practice - generally it is redundant, and done repetitively can be slow |
19 | 21 | String string = new String(); |
20 | 22 | String empty = ""; |
21 | 23 | assertEquals(string.equals(empty), __); |
22 | 24 | } |
23 | | - |
| 25 | + |
24 | 26 | @Koan |
25 | | - public void newStringIsRedundant(){ |
| 27 | + public void newStringIsRedundant() { |
26 | 28 | String stringInstance = "zero"; |
27 | | - String stringReference = new String(stringInstance); |
| 29 | + String stringReference = new String(stringInstance); |
28 | 30 | assertEquals(stringInstance.equals(stringReference), __); |
29 | 31 | } |
30 | | - |
| 32 | + |
31 | 33 | @Koan |
32 | | - public void newStringIsNotIdentical(){ |
| 34 | + public void newStringIsNotIdentical() { |
33 | 35 | String stringInstance = "zero"; |
34 | 36 | String stringReference = new String(stringInstance); |
35 | 37 | assertEquals(stringInstance == stringReference, __); |
36 | 38 | } |
37 | | - |
| 39 | + |
38 | 40 | @Koan |
39 | | - public void stringConcatenation(){ |
| 41 | + public void stringConcatenation() { |
40 | 42 | String one = "one"; |
41 | 43 | String space = " "; |
42 | 44 | String two = "two"; |
43 | | - assertEquals(one+space+two, __); |
| 45 | + assertEquals(one + space + two, __); |
44 | 46 | } |
45 | | - |
| 47 | + |
46 | 48 | @Koan |
47 | | - public void efficientStringConcatenation(){ |
| 49 | + public void efficientStringConcatenation() { |
48 | 50 | // the above implicit string concatenation looks nice - but, it is expensive. |
49 | 51 | // it creates a new string instance on each concatenation. here's a better alternative: |
50 | 52 | assertEquals(new StringBuilder("one").append(" ").append("two").toString(), __); |
51 | 53 | } |
| 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 | + |
52 | 91 | } |
0 commit comments