Skip to content

Commit

Permalink
Untyped variant of equalTo: equalToObject(Object) -> Matcher<Object>.…
Browse files Browse the repository at this point in the history
… Implements issue #59.
  • Loading branch information
npryce committed Nov 23, 2014
1 parent bfc7baa commit 23bdb6d
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
9 changes: 9 additions & 0 deletions hamcrest-core/src/main/java/org/hamcrest/core/IsEqual.java
Expand Up @@ -91,4 +91,13 @@ private static boolean isArray(Object o) {
public static <T> Matcher<T> equalTo(T operand) {
return new IsEqual<T>(operand);
}

/**
* Creates an {@link org.hamcrest.core.IsEqual} matcher that does not enforce the values being
* compared to be of the same static type.
*/
@Factory
public static Matcher<Object> equalToObject(Object operand) {
return new IsEqual<Object>(operand);
}
}
11 changes: 11 additions & 0 deletions hamcrest-core/src/test/java/org/hamcrest/core/IsEqualTest.java
Expand Up @@ -8,6 +8,7 @@
import static org.hamcrest.AbstractMatcherTest.assertNullSafe;
import static org.hamcrest.AbstractMatcherTest.assertUnknownTypeSafe;
import static org.hamcrest.core.IsEqual.equalTo;
import static org.hamcrest.core.IsEqual.equalToObject;

import org.hamcrest.Matcher;
import org.junit.Test;
Expand Down Expand Up @@ -110,6 +111,16 @@ public boolean equals(Object obj) {
assertDoesNotMatch(matcher, null);
}

@Test public void
hasUntypedVariant() {
Object original = 10;

assertMatches(equalToObject(10), original);
assertDoesNotMatch(equalToObject(0), original);
assertDoesNotMatch(equalToObject("10"), original);
assertDoesNotMatch(equalToObject(10), "10");
}

@Test public void
includesTheResultOfCallingToStringOnItsArgumentInTheDescription() {
final String argumentDescription = "ARGUMENT DESCRIPTION";
Expand Down

0 comments on commit 23bdb6d

Please sign in to comment.