forked from matyb/java-koans
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAboutStrings.java
More file actions
186 lines (158 loc) · 5.64 KB
/
Copy pathAboutStrings.java
File metadata and controls
186 lines (158 loc) · 5.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
package beginner;
import com.sandwich.koan.Koan;
import java.text.MessageFormat;
import static com.sandwich.koan.constant.KoanConstants.__;
import static com.sandwich.util.Assert.assertEquals;
import static com.sandwich.util.Assert.fail;
public class AboutStrings {
@Koan
public void implicitStrings() {
assertEquals("just a plain ole string".getClass(), __);
}
@Koan
public void newString() {
// very rarely if ever should Strings be created via new String() in
// practice - generally it is redundant, and done repetitively can be slow
String string = new String();
String empty = "";
assertEquals(string.equals(empty), __);
}
@Koan
public void newStringIsRedundant() {
String stringInstance = "zero";
String stringReference = new String(stringInstance);
assertEquals(stringInstance.equals(stringReference), __);
}
@Koan
public void newStringIsNotIdentical() {
String stringInstance = "zero";
String stringReference = new String(stringInstance);
assertEquals(stringInstance == stringReference, __);
}
@Koan
public void stringIsEmpty() {
assertEquals("".isEmpty(), __);
assertEquals("one".isEmpty(), __);
assertEquals(new String().isEmpty(), __);
assertEquals(new String("").isEmpty(), __);
assertEquals(new String("one").isEmpty(), __);
}
@Koan
public void stringLength() {
assertEquals("".length(), __);
assertEquals("one".length(), __);
assertEquals("the number is one".length(), __);
}
@Koan
public void stringTrim() {
assertEquals("".trim(), __);
assertEquals("one".trim(), "one");
assertEquals(" one more time".trim(), __);
assertEquals(" one more time ".trim(), __);
assertEquals(" and again\t".trim(), __);
assertEquals("\t\t\twhat about now?\t".trim(), __);
}
@Koan
public void stringConcatenation() {
String one = "one";
String space = " ";
String two = "two";
assertEquals(one + space + two, __);
assertEquals(space + one + two, __);
assertEquals(two + space + one, __);
}
@Koan
public void stringUpperCase() {
String str = "I am a number one!";
assertEquals(str.toUpperCase(), __);
}
@Koan
public void stringLowerCase() {
String str = "I AM a number ONE!";
assertEquals(str.toLowerCase(), __);
}
@Koan
public void stringCompare() {
String str = "I AM a number ONE!";
assertEquals(str.compareTo("I AM a number ONE!") == 0, __);
assertEquals(str.compareTo("I am a number one!") == 0, __);
assertEquals(str.compareTo("I AM A NUMBER ONE!") == 0, __);
}
@Koan
public void stringCompareIgnoreCase() {
String str = "I AM a number ONE!";
assertEquals(str.compareToIgnoreCase("I AM a number ONE!") == 0, __);
assertEquals(str.compareToIgnoreCase("I am a number one!") == 0, __);
assertEquals(str.compareToIgnoreCase("I AM A NUMBER ONE!") == 0, __);
}
@Koan
public void stringStartsWith() {
assertEquals("".startsWith("one"), __);
assertEquals("one".startsWith("one"), __);
assertEquals("one is the number".startsWith("one"), __);
assertEquals("ONE is the number".startsWith("one"), __);
}
@Koan
public void stringEndsWith() {
assertEquals("".endsWith("one"), __);
assertEquals("one".endsWith("one"), __);
assertEquals("the number is one".endsWith("one"), __);
assertEquals("the number is two".endsWith("one"), __);
assertEquals("the number is One".endsWith("one"), __);
}
@Koan
public void stringSubstring() {
String str = "I AM a number ONE!";
assertEquals(str.substring(0), __);
assertEquals(str.substring(1), __);
assertEquals(str.substring(5), __);
assertEquals(str.substring(14, 17), __);
assertEquals(str.substring(7, str.length()), __);
}
@Koan
public void stringContains() {
String str = "I AM a number ONE!";
assertEquals(str.contains("one"), __);
assertEquals(str.contains("ONE"), __);
}
@Koan
public void stringReplace() {
String str = "I am a number ONE!";
assertEquals(str.replace("ONE", "TWO"), __);
assertEquals(str.replace("I am", "She is"), __);
}
@Koan
public void stringBuilderCanActAsAMutableString() {
assertEquals(new StringBuilder("one").append(" ").append("two").toString(), __);
}
@Koan
public void readableStringFormattingWithStringFormat() {
assertEquals(String.format("%s %s %s", "a", "b", "a"), __);
}
@Koan
public void extraArgumentsToStringFormatGetIgnored() {
assertEquals(String.format("%s %s %s", "a", "b", "c", "d"), __);
}
@Koan
public void insufficientArgumentsToStringFormatCausesAnError() {
try {
String.format("%s %s %s", "a", "b");
fail("No Exception was thrown!");
} catch (Exception e) {
assertEquals(e.getClass(), __);
assertEquals(e.getMessage(), __);
}
}
@Koan
public void readableStringFormattingWithMessageFormat() {
assertEquals(MessageFormat.format("{0} {1} {0}", "a", "b"), __);
}
@Koan
public void extraArgumentsToMessageFormatGetIgnored() {
assertEquals(MessageFormat.format("{0} {1} {0}", "a", "b", "c"), __);
}
@Koan
public void insufficientArgumentsToMessageFormatDoesNotReplaceTheToken() {
assertEquals(MessageFormat.format("{0} {1} {0}", "a"), __);
}
}