Skip to content

Commit b15c66f

Browse files
author
smgfreeman
committed
IsInstanceOf now matches classes of basic types
1 parent d5f2a7f commit b15c66f

File tree

2 files changed

+34
-8
lines changed

2 files changed

+34
-8
lines changed

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

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,33 @@
1010

1111
/**
1212
* Tests whether the value is an instance of a class.
13+
* Classes of basic types will be converted to the relevant "Object" classes
1314
*/
1415
public class IsInstanceOf extends DiagnosingMatcher<Object> {
15-
private final Class<?> theClass;
16+
private final Class<?> expectedClass;
17+
private final Class<?> matchableClass;
1618

1719
/**
1820
* Creates a new instance of IsInstanceOf
1921
*
20-
* @param theClass The predicate evaluates to true for instances of this class
22+
* @param expectedClass The predicate evaluates to true for instances of this class
2123
* or one of its subclasses.
2224
*/
23-
public IsInstanceOf(Class<?> theClass) {
24-
this.theClass = theClass;
25+
public IsInstanceOf(Class<?> expectedClass) {
26+
this.expectedClass = expectedClass;
27+
this.matchableClass = matchableClass(expectedClass);
28+
}
29+
30+
private static Class<?> matchableClass(Class<?> expectedClass) {
31+
if (boolean.class.equals(expectedClass)) return Boolean.class;
32+
if (byte.class.equals(expectedClass)) return Byte.class;
33+
if (char.class.equals(expectedClass)) return Character.class;
34+
if (double.class.equals(expectedClass)) return Double.class;
35+
if (float.class.equals(expectedClass)) return Float.class;
36+
if (int.class.equals(expectedClass)) return Integer.class;
37+
if (long.class.equals(expectedClass)) return Long.class;
38+
if (short.class.equals(expectedClass)) return Short.class;
39+
return expectedClass;
2540
}
2641

2742
@Override
@@ -31,7 +46,7 @@ protected boolean matches(Object item, Description mismatchDescription) {
3146
return false;
3247
}
3348

34-
if (!theClass.isInstance(item)) {
49+
if (!matchableClass.isInstance(item)) {
3550
mismatchDescription.appendValue(item).appendText(" is a " + item.getClass().getName());
3651
return false;
3752
}
@@ -41,7 +56,7 @@ protected boolean matches(Object item, Description mismatchDescription) {
4156

4257
public void describeTo(Description description) {
4358
description.appendText("an instance of ")
44-
.appendText(theClass.getName());
59+
.appendText(expectedClass.getName());
4560
}
4661

4762
/**

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

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* Copyright (c) 2000-2006 hamcrest.org
1+
/* Copyright (c) 2000-2009 hamcrest.org
22
*/
33
package org.hamcrest.core;
44

@@ -29,12 +29,23 @@ public void testHasAReadableDescription() {
2929
public void testDecribesActualClassInMismatchMessage() {
3030
assertMismatchDescription("\"some text\" is a java.lang.String", instanceOf(Number.class), "some text");
3131
}
32+
33+
public void testMatchesPrimitiveTypes() {
34+
assertThat(true, any(boolean.class));
35+
assertThat((byte)1, any(byte.class));
36+
assertThat('x', any(char.class));
37+
assertThat(5.0, any(double.class));
38+
assertThat(5.0f, any(float.class));
39+
assertThat(2, any(int.class));
40+
assertThat(4L, any(long.class));
41+
assertThat((short)1, any(short.class));
42+
}
3243

3344
public void testInstanceOfRequiresACastToReturnTheCorrectTypeForUseInJMock() {
3445
@SuppressWarnings("unused")
3546
Integer anInteger = (Integer)with(instanceOf(Integer.class));
3647
}
37-
48+
3849
public void testAnyWillReturnTheCorrectTypeForUseInJMock() {
3950
@SuppressWarnings("unused")
4051
Integer anInteger = with(any(Integer.class));

0 commit comments

Comments
 (0)