Skip to content

Commit d790a11

Browse files
author
joeretro
committed
Java5ified IsEqualIgnoringCase
1 parent e8314a6 commit d790a11

2 files changed

Lines changed: 23 additions & 22 deletions

File tree

‎src/library/org/hamcrest/text/IsEqualIgnoringCase.java‎

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,12 @@
44

55
import org.hamcrest.Description;
66
import org.hamcrest.Matcher;
7+
import org.hamcrest.Factory;
78

89
/**
910
* Tests if a string is equal to another string, regardless of the case.
1011
*/
11-
public class IsEqualIgnoringCase implements Matcher {
12+
public class IsEqualIgnoringCase implements Matcher<String> {
1213

1314
private final String string;
1415

@@ -19,13 +20,11 @@ public IsEqualIgnoringCase(String string) {
1920
this.string = string;
2021
}
2122

22-
public boolean match(Object o) {
23-
if (o == null) {
24-
return false;
25-
} else if (!(o instanceof String)) {
23+
public boolean match(String item) {
24+
if (item == null) {
2625
return false;
2726
} else {
28-
return string.equalsIgnoreCase((String) o);
27+
return string.equalsIgnoreCase(item);
2928
}
3029
}
3130

@@ -34,4 +33,10 @@ public void describeTo(Description description) {
3433
.appendValue(string)
3534
.appendText(")");
3635
}
36+
37+
@Factory
38+
public static Matcher<String> eqIgnoringCase(String string) {
39+
return new IsEqualIgnoringCase(string);
40+
}
41+
3742
}

‎src/unit-test/org/hamcrest/text/IsEqualIgnoringCaseTest.java‎

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,43 +3,39 @@
33
package org.hamcrest.text;
44

55
import org.hamcrest.AbstractMatcherTest;
6-
import org.hamcrest.Matcher;
6+
import static org.hamcrest.MatcherAssert.assertThat;
7+
import static org.hamcrest.core.IsNot.not;
8+
import static org.hamcrest.text.IsEqualIgnoringCase.eqIgnoringCase;
79

810
public class IsEqualIgnoringCaseTest extends AbstractMatcherTest {
911

10-
private final Matcher matcher = new IsEqualIgnoringCase("heLLo");
11-
1212
public void testIgnoresCaseOfCharsInString() {
13-
assertTrue(matcher.match("HELLO"));
14-
assertTrue(matcher.match("hello"));
15-
assertTrue(matcher.match("HelLo"));
13+
assertThat("HELLO", eqIgnoringCase("heLLo"));
14+
assertThat("hello", eqIgnoringCase("heLLo"));
15+
assertThat("HelLo", eqIgnoringCase("heLLo"));
1616

17-
assertFalse(matcher.match("abcde"));
17+
assertThat("bye", not(eqIgnoringCase("heLLo")));
1818
}
1919

2020
public void testFailsIfAdditionalWhitespaceIsPresent() {
21-
assertFalse(matcher.match(" heLLo"));
22-
assertFalse(matcher.match("heLLo "));
23-
}
24-
25-
public void testFailsIfNotMatchingAgainstString() {
26-
assertFalse(matcher.match(new Object()));
21+
assertThat("heLLo ", not(eqIgnoringCase("heLLo")));
22+
assertThat(" heLLo", not(eqIgnoringCase("heLLo")));
2723
}
2824

2925
public void testFailsIfMatchingAgainstNull() {
30-
assertFalse(matcher.match(null));
26+
assertThat(null, not(eqIgnoringCase("heLLo")));
3127
}
3228

3329
public void testRequiresNonNullStringToBeConstructed() {
3430
try {
35-
new IsEqualIgnoringCase(null);
31+
eqIgnoringCase(null);
3632
fail("Expected exception");
3733
} catch (IllegalArgumentException goodException) {
3834
// expected!
3935
}
4036
}
4137

4238
public void testDescribesItselfAsCaseInsensitive() {
43-
assertDescription("eqIgnoringCase(\"heLLo\")", matcher);
39+
assertDescription("eqIgnoringCase(\"heLLo\")", eqIgnoringCase("heLLo"));
4440
}
4541
}

0 commit comments

Comments
 (0)