Skip to content

Commit d71692c

Browse files
committed
more string specific assertions added
1 parent a44f94e commit d71692c

1 file changed

Lines changed: 98 additions & 5 deletions

File tree

‎koans/src/beginner/AboutStrings.java‎

Lines changed: 98 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,20 +38,114 @@ public void newStringIsNotIdentical() {
3838
assertEquals(stringInstance == stringReference, __);
3939
}
4040

41+
@Koan
42+
public void stringLength() {
43+
assertEquals("".length(), __);
44+
assertEquals("one".length(), __);
45+
assertEquals("the number is one".length(), __);
46+
assertEquals("The quick brown fox jumps over the lazy dog".endsWith("one"), __);
47+
assertEquals(("what is the " + "length of me?").endsWith("one"), __);
48+
}
49+
50+
@Koan
51+
public void stringIsEmpty() {
52+
assertEquals("".isEmpty(), __);
53+
assertEquals("one".isEmpty(), __);
54+
assertEquals(new String().isEmpty(), __);
55+
assertEquals(new String("").isEmpty(), __);
56+
}
57+
58+
@Koan
59+
public void stringTrim() {
60+
assertEquals("".trim(), __);
61+
assertEquals("one".trim(), __);
62+
assertEquals(" one more time".isEmpty(), __);
63+
assertEquals(" one more time ".isEmpty(), __);
64+
assertEquals(" and again\t`".isEmpty(), __);
65+
assertEquals("\t\t\twhat about now?\t`".isEmpty(), __);
66+
}
67+
4168
@Koan
4269
public void stringConcatenation() {
4370
String one = "one";
4471
String space = " ";
4572
String two = "two";
4673
assertEquals(one + space + two, __);
74+
assertEquals(space + one + two, __);
75+
assertEquals(two + space + one, __);
76+
}
77+
78+
@Koan
79+
public void stringUpperCase() {
80+
String str = "I am a number one!";
81+
assertEquals(str.toUpperCase(), __);
82+
}
83+
84+
@Koan
85+
public void stringLowerCase() {
86+
String str = "I AM a number ONE!";
87+
assertEquals(str.toLowerCase(), __);
88+
}
89+
90+
@Koan
91+
public void stringCompare() {
92+
String str = "I AM a number ONE!";
93+
assertEquals(str.compareTo("I AM a number ONE!"), __);
94+
assertEquals(str.compareTo("I am a number one!"), __);
95+
assertEquals(str.compareTo("I AM A NUMBER ONE!"), __);
96+
}
97+
98+
@Koan
99+
public void stringCompareIgnoreCase() {
100+
String str = "I AM a number ONE!";
101+
assertEquals(str.compareToIgnoreCase("I AM a number ONE!"), __);
102+
assertEquals(str.compareToIgnoreCase("I am a number one!"), __);
103+
assertEquals(str.compareToIgnoreCase("I AM A NUMBER ONE!"), __);
104+
}
105+
106+
@Koan
107+
public void stringStartsWith() {
108+
assertEquals("".startsWith("one"), __);
109+
assertEquals("one".startsWith("one"), __);
110+
assertEquals("one is the number".startsWith("one"), __);
111+
assertEquals("ONE is the number".startsWith("one"), __);
112+
}
113+
114+
@Koan
115+
public void stringEndsWith() {
116+
assertEquals("".endsWith("one"), __);
117+
assertEquals("one".endsWith("one"), __);
118+
assertEquals("the number is one".endsWith("one"), __);
119+
assertEquals("the number is two".endsWith("one"), __);
120+
assertEquals("the number is One".endsWith("one"), __);
121+
}
122+
123+
@Koan
124+
public void stringSubstring() {
125+
String str = "I AM a number ONE!";
126+
assertEquals(str.substring(0), __);
127+
assertEquals(str.substring(1), __);
128+
assertEquals(str.substring(5), __);
129+
assertEquals(str.substring(14, 17), __);
130+
assertEquals(str.substring(10, str.length()), __);
131+
}
132+
133+
@Koan
134+
public void stringContains() {
135+
String str = "I AM a number ONE!";
136+
assertEquals(str.contains("one"), __);
137+
assertEquals(str.contains("ONE"), __);
138+
}
139+
140+
@Koan
141+
public void stringReplace() {
142+
String str = "I AM a number ONE!";
143+
assertEquals(str.replace("ONE", "TWO"), __);
144+
assertEquals(str.replace("I am", "She is"), __);
47145
}
48146

49147
@Koan
50148
public void stringBuilderCanActAsAMutableString() {
51-
// StringBuilder concatenation looks uglier, but is useful when you need a
52-
// mutable String like object. It used to be more efficient than using +
53-
// to concatenate numerous strings, however this is optimized in the compiler now.
54-
// Usually + concatenation is more appropriate than StringBuilder.
55149
assertEquals(new StringBuilder("one").append(" ").append("two").toString(), __);
56150
}
57151

@@ -90,5 +184,4 @@ public void extraArgumentsToMessageFormatGetIgnored() {
90184
public void insufficientArgumentsToMessageFormatDoesNotReplaceTheToken() {
91185
assertEquals(MessageFormat.format("{0} {1} {0}", "a"), __);
92186
}
93-
94187
}

0 commit comments

Comments
 (0)