Skip to content

Commit d3be762

Browse files
author
joeretro
committed
Added is(...) wrapper to matchers. Added convenient is(value) and not(value) for commonly used is(equalTo(value)) and not(equalTo(value))
1 parent 189fc11 commit d3be762

File tree

9 files changed

+125
-17
lines changed

9 files changed

+125
-17
lines changed

matchers.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<matchers>
22

33
<!-- Core -->
4+
<factory class="org.hamcrest.core.Is"/>
45
<factory class="org.hamcrest.core.AllOf"/>
56
<factory class="org.hamcrest.core.AnyOf"/>
67
<factory class="org.hamcrest.core.IsNot"/>

src/examples/org/hamcrest/examples/junit3/ExampleWithAssertThat.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@
22

33
import junit.framework.TestCase;
44
import static org.hamcrest.MatcherAssert.assertThat;
5-
import static org.hamcrest.Matchers.equalTo;
6-
import static org.hamcrest.core.IsNot.not;
7-
import static org.hamcrest.text.StringContains.containsString;
5+
import static org.hamcrest.Matchers.*;
86

97
/**
108
* Demonstrates how Hamcrest matchers can be used with assertThat()
@@ -15,8 +13,8 @@
1513
public class ExampleWithAssertThat extends TestCase {
1614

1715
public void testUsingAssertThat() {
18-
assertThat("xx", equalTo("xx"));
19-
assertThat("yy", not(equalTo("xx")));
16+
assertThat("xx", is("xx"));
17+
assertThat("yy", is(not("xx")));
2018
assertThat("i like cheese", containsString("cheese"));
2119
}
2220

src/examples/org/hamcrest/examples/junit3/ExampleWithJMock1.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package org.hamcrest.examples.junit3;
22

3-
import static org.hamcrest.JMock1Matchers.equalTo;
3+
import static org.hamcrest.JMock1Matchers.*;
44
import org.jmock.Mock;
55
import org.jmock.MockObjectTestCase;
66

src/examples/org/hamcrest/examples/junit4/ExampleWithAssertThat.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
package org.hamcrest.examples.junit4;
22

33
import static org.hamcrest.MatcherAssert.assertThat;
4-
import static org.hamcrest.Matchers.equalTo;
5-
import static org.hamcrest.core.IsNot.not;
6-
import static org.hamcrest.text.StringContains.containsString;
4+
import static org.hamcrest.Matchers.*;
75
import org.junit.Test;
86

97
/**
@@ -16,8 +14,8 @@ public class ExampleWithAssertThat {
1614

1715
@Test
1816
public void usingAssertThat() {
19-
assertThat("xx", equalTo("xx"));
20-
assertThat("yy", not(equalTo("xx")));
17+
assertThat("xx", is("xx"));
18+
assertThat("yy", is(not("xx")));
2119
assertThat("i like cheese", containsString("cheese"));
2220
}
2321

src/examples/org/hamcrest/examples/testng/ExampleWithAssertThat.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
package org.hamcrest.examples.testng;
22

33
import static org.hamcrest.MatcherAssert.assertThat;
4-
import static org.hamcrest.Matchers.equalTo;
5-
import static org.hamcrest.core.IsNot.not;
6-
import static org.hamcrest.text.StringContains.containsString;
4+
import static org.hamcrest.Matchers.*;
75
import org.testng.annotations.Test;
86

97
/**
@@ -17,8 +15,8 @@ public class ExampleWithAssertThat {
1715

1816
@Test
1917
public void usingAssertThat() {
20-
assertThat("xx", equalTo("xx"));
21-
assertThat("yy", not(equalTo("xx")));
18+
assertThat("xx", is("xx"));
19+
assertThat("yy", is(not("xx")));
2220
assertThat("i like cheese", containsString("cheese"));
2321
}
2422

src/library/org/hamcrest/core/Is.java

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package org.hamcrest.core;
2+
3+
import static org.hamcrest.core.IsEqual.equalTo;
4+
import org.hamcrest.Factory;
5+
import org.hamcrest.Matcher;
6+
import org.hamcrest.BaseMatcher;
7+
import org.hamcrest.Description;
8+
9+
/**
10+
* Decorates another Matcher, retaining the behavior but allowing tests
11+
* to be slightly more expressive.
12+
*
13+
* eg. assertThat(cheese, equalTo(smelly))
14+
* vs assertThat(cheese, is(equalTo(smelly)))
15+
*/
16+
public class Is<T> extends BaseMatcher<T> {
17+
18+
private final Matcher<T> matcher;
19+
20+
public Is(Matcher<T> matcher) {
21+
this.matcher = matcher;
22+
}
23+
24+
public boolean matches(Object arg) {
25+
return matcher.matches(arg);
26+
}
27+
28+
public void describeTo(Description description) {
29+
description.appendText("is ");
30+
matcher.describeTo(description);
31+
}
32+
33+
/**
34+
* Decorates another Matcher, retaining the behavior but allowing tests
35+
* to be slightly more expressive.
36+
*
37+
* eg. assertThat(cheese, equalTo(smelly))
38+
* vs assertThat(cheese, is(equalTo(smelly)))
39+
*/
40+
@Factory
41+
public static <T> Matcher<T> is(Matcher<T> matcher) {
42+
return new Is<T>(matcher);
43+
}
44+
45+
/**
46+
* This is a shortcut to the frequently used is(equalTo(x)).
47+
*
48+
* eg. assertThat(cheese, is(equalTo(smelly)))
49+
* vs assertThat(cheese, is(smelly))
50+
*/
51+
@Factory
52+
public static <T> Matcher<T> is(T value) {
53+
return is(equalTo(value));
54+
}
55+
56+
}
57+

src/library/org/hamcrest/core/IsNot.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
*/
33
package org.hamcrest.core;
44

5+
import static org.hamcrest.core.IsEqual.equalTo;
56
import org.hamcrest.Description;
67
import org.hamcrest.Matcher;
78
import org.hamcrest.Factory;
@@ -27,9 +28,23 @@ public void describeTo(Description description) {
2728
matcher.describeTo(description);
2829
}
2930

31+
/**
32+
* Inverts the rule.
33+
*/
3034
@Factory
3135
public static <T> Matcher<T> not(Matcher<T> matcher) {
3236
return new IsNot<T>(matcher);
3337
}
3438

39+
/**
40+
* This is a shortcut to the frequently used not(equalTo(x)).
41+
*
42+
* eg. assertThat(cheese, is(not(equalTo(smelly))))
43+
* vs assertThat(cheese, is(not(smelly)))
44+
*/
45+
@Factory
46+
public static <T> Matcher<T> not(T value) {
47+
return not(equalTo(value));
48+
}
49+
3550
}

src/unit-test/org/hamcrest/core/IsNotTest.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,17 @@ protected Matcher<?> createMatcher() {
1212
return not(FALSE_MATCHER);
1313
}
1414

15-
public void testEvaluatesToTheTheLogicalNegationOfAnotherMatcher() {
15+
public void testEvaluatesToTheTheLogicalNegationOfAnotherMatcher() {
1616
assertFalse(not(TRUE_MATCHER).matches(ARGUMENT_IGNORED));
1717
assertTrue(not(FALSE_MATCHER).matches(ARGUMENT_IGNORED));
1818
}
19+
20+
public void testProvidesConvenientShortcutForNotEqualTo() {
21+
assertMatches("should match", not("A"), "B");
22+
assertMatches("should match", not("B"), "A");
23+
assertDoesNotMatch("should not match", not("A"), "A");
24+
assertDoesNotMatch("should not match", not("B"), "B");
25+
assertDescription("not \"A\"", not("A"));
26+
}
27+
1928
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package org.hamcrest.core;
2+
3+
import static org.hamcrest.core.IsEqual.equalTo;
4+
import static org.hamcrest.core.Is.is;
5+
import org.hamcrest.AbstractMatcherTest;
6+
import org.hamcrest.Matcher;
7+
8+
public class IsTest extends AbstractMatcherTest {
9+
10+
protected Matcher<?> createMatcher() {
11+
return is(FALSE_MATCHER);
12+
}
13+
14+
public void testJustMatchesTheSameWayTheUnderylingMatcherDoes() {
15+
assertMatches("should match", is(equalTo(true)), true);
16+
assertMatches("should match", is(equalTo(false)), false);
17+
assertDoesNotMatch("should not match", is(equalTo(true)), false);
18+
assertDoesNotMatch("should not match", is(equalTo(false)), true);
19+
}
20+
21+
public void testGeneratesIsPrefixInDescription() {
22+
assertDescription("is <true>", is(equalTo(true)));
23+
}
24+
25+
public void testProvidesConvenientShortcutForIsEqualTo() {
26+
assertMatches("should match", is("A"), "A");
27+
assertMatches("should match", is("B"), "B");
28+
assertDoesNotMatch("should not match", is("A"), "B");
29+
assertDoesNotMatch("should not match", is("B"), "A");
30+
assertDescription("is \"A\"", is("A"));
31+
}
32+
}

0 commit comments

Comments
 (0)