Skip to content

Commit 23bdb6d

Browse files
committed
Untyped variant of equalTo: equalToObject(Object) -> Matcher<Object>. Implements issue hamcrest#59.
1 parent bfc7baa commit 23bdb6d

2 files changed

Lines changed: 20 additions & 0 deletions

File tree

hamcrest-core/src/main/java/org/hamcrest/core/IsEqual.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,4 +91,13 @@ private static boolean isArray(Object o) {
9191
public static <T> Matcher<T> equalTo(T operand) {
9292
return new IsEqual<T>(operand);
9393
}
94+
95+
/**
96+
* Creates an {@link org.hamcrest.core.IsEqual} matcher that does not enforce the values being
97+
* compared to be of the same static type.
98+
*/
99+
@Factory
100+
public static Matcher<Object> equalToObject(Object operand) {
101+
return new IsEqual<Object>(operand);
102+
}
94103
}

hamcrest-core/src/test/java/org/hamcrest/core/IsEqualTest.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import static org.hamcrest.AbstractMatcherTest.assertNullSafe;
99
import static org.hamcrest.AbstractMatcherTest.assertUnknownTypeSafe;
1010
import static org.hamcrest.core.IsEqual.equalTo;
11+
import static org.hamcrest.core.IsEqual.equalToObject;
1112

1213
import org.hamcrest.Matcher;
1314
import org.junit.Test;
@@ -110,6 +111,16 @@ public boolean equals(Object obj) {
110111
assertDoesNotMatch(matcher, null);
111112
}
112113

114+
@Test public void
115+
hasUntypedVariant() {
116+
Object original = 10;
117+
118+
assertMatches(equalToObject(10), original);
119+
assertDoesNotMatch(equalToObject(0), original);
120+
assertDoesNotMatch(equalToObject("10"), original);
121+
assertDoesNotMatch(equalToObject(10), "10");
122+
}
123+
113124
@Test public void
114125
includesTheResultOfCallingToStringOnItsArgumentInTheDescription() {
115126
final String argumentDescription = "ARGUMENT DESCRIPTION";

0 commit comments

Comments
 (0)