Skip to content

Commit 8f8924a

Browse files
committed
Address issue 101 (on google code issue tracker) - Add theInstance() alias for sameInstance() factory
1 parent 613a3d2 commit 8f8924a

File tree

2 files changed

+27
-5
lines changed

2 files changed

+27
-5
lines changed

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

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,26 @@ public void describeTo(Description description) {
3131
}
3232

3333
/**
34-
* Creates a new instance of IsSame
34+
* Creates a matcher that evaluates to true only when the examined object is the
35+
* same instance as the specified target object.
3536
*
36-
* @param object The predicate evaluates to true only when the argument is
37-
* this object.
37+
* @param target
38+
* the target instance against which others should be assessed
3839
*/
3940
@Factory
40-
public static <T> Matcher<T> sameInstance(T object) {
41-
return new IsSame<T>(object);
41+
public static <T> Matcher<T> sameInstance(T target) {
42+
return new IsSame<T>(target);
43+
}
44+
45+
/**
46+
* Creates a matcher that evaluates to true only when the examined object is the
47+
* same instance as the specified target object.
48+
*
49+
* @param target
50+
* the target instance against which others should be assessed
51+
*/
52+
@Factory
53+
public static <T> Matcher<T> theInstance(T target) {
54+
return new IsSame<T>(target);
4255
}
4356
}

hamcrest-unit-test/src/main/java/org/hamcrest/core/IsSameTest.java

Lines changed: 9 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.IsSame.theInstance;
56
import static org.hamcrest.core.IsSame.sameInstance;
67
import static org.hamcrest.core.IsNot.not;
78
import org.hamcrest.AbstractMatcherTest;
@@ -24,6 +25,14 @@ public void testEvaluatesToTrueIfArgumentIsReferenceToASpecifiedObject() {
2425
assertThat(o2, not(sameInstance(o1)));
2526
}
2627

28+
public void testAlternativeFactoryMethodAlsoMatchesOnlyIfArgumentIsReferenceToASpecifiedObject() {
29+
Object o1 = new Object();
30+
Object o2 = new Object();
31+
32+
assertThat(o1, theInstance(o1));
33+
assertThat(o2, not(theInstance(o1)));
34+
}
35+
2736
public void testReturnsReadableDescriptionFromToString() {
2837
assertDescription("sameInstance(\"ARG\")", sameInstance("ARG"));
2938
}

0 commit comments

Comments
 (0)