Skip to content

Commit

Permalink
sf. More template madness. Moved any() from IsAnything to IsInstanceO…
Browse files Browse the repository at this point in the history
…f. Moved MatcherAssert to core.
  • Loading branch information
smgfreeman committed Nov 25, 2008
1 parent 7f0ef63 commit a6b00c4
Show file tree
Hide file tree
Showing 10 changed files with 68 additions and 53 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,11 @@


public class MatcherAssert {

public static <T,K extends T,V extends T> void assertThat(K actual, Matcher<V> matcher) {
public static <T> void assertThat(T actual, Matcher<? super T> matcher) {
assertThat("", actual, matcher);
}

public static<T,K extends T,V extends T> void assertThat(String reason, K actual, Matcher<V> matcher) {
public static <T> void assertThat(String reason, T actual, Matcher<? super T> matcher) {
if (!matcher.matches(actual)) {
Description description = new StringDescription();
description.appendText(reason)
Expand Down
8 changes: 0 additions & 8 deletions hamcrest-core/src/main/java/org/hamcrest/core/IsAnything.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,4 @@ public static <T> Matcher<T> anything() {
public static <T> Matcher<T> anything(String description) {
return new IsAnything<T>(description);
}

/**
* This matcher always evaluates to true. With type inference.
*/
@Factory
public static <T> Matcher<T> any(@SuppressWarnings("unused")Class<T> type) {
return new IsAnything<T>();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,16 @@
import org.hamcrest.Matcher;
import org.hamcrest.TypeSafeMatcher;

public class IsCollectionContaining<T> extends TypeSafeMatcher<Iterable<T>> {
public class IsCollectionContaining<T> extends TypeSafeMatcher<Iterable<? super T>> {
private final Matcher<? super T> elementMatcher;

public IsCollectionContaining(Matcher<? super T> elementMatcher) {
this.elementMatcher = elementMatcher;
}

@Override
public boolean matchesSafely(Iterable<T> collection) {
for (T item : collection) {
public boolean matchesSafely(Iterable<? super T> collection) {
for (Object item : collection) {
if (elementMatcher.matches(item)){
return true;
}
Expand All @@ -35,22 +35,23 @@ public void describeTo(Description description) {
}

@Factory
public static <T> Matcher<Iterable<T>> hasItem(Matcher<? super T> elementMatcher) {
return new IsCollectionContaining<T>(elementMatcher);
public static <T> Matcher<Iterable<? super T>> hasItem(Matcher<? super T> elementMatcher) {
return new IsCollectionContaining<T>(elementMatcher);
}

@Factory
public static <T> Matcher<Iterable<T>> hasItem(T element) {
return hasItem(equalTo(element));
public static <T> Matcher<Iterable<? super T>> hasItem(T element) {
// Doesn't forward to hasItem() method so compiler can sort out generics.
return new IsCollectionContaining<T>(equalTo(element));
}

@Factory
public static <T> Matcher<Iterable<T>> hasItems(Matcher<? super T>... elementMatchers) {
List<Matcher<? super Iterable<T>>> all = new ArrayList<Matcher<? super Iterable<T>>>(elementMatchers.length);

for (Matcher<? super T> elementMatcher : elementMatchers) {
Matcher<? super Iterable<T>> itemMatcher = hasItem(elementMatcher);
all.add(itemMatcher);
// Doesn't forward to hasItem() method so compiler can sort out generics.
all.add(new IsCollectionContaining<T>(elementMatcher));
}

return allOf(all);
Expand All @@ -59,7 +60,6 @@ public static <T> Matcher<Iterable<T>> hasItems(Matcher<? super T>... elementMat
@Factory
public static <T> Matcher<Iterable<T>> hasItems(T... elements) {
List<Matcher<? super Iterable<T>>> all = new ArrayList<Matcher<? super Iterable<T>>>(elements.length);

for (T element : elements) {
all.add(hasItem(element));
}
Expand Down
19 changes: 17 additions & 2 deletions hamcrest-core/src/main/java/org/hamcrest/core/IsInstanceOf.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,26 @@ public void describeTo(Description description) {
}

/**
* Is the value an instance of a particular type?
* Is the value an instance of a particular type?
* This version assumes no relationship between the required type and
* the signature of the method that sets it up, for example in
* <code>assertThat(anObject, instanceOf(Thing.class));</code>
*/
@SuppressWarnings("unchecked")
@Factory
public static <T> Matcher<T> instanceOf(Class<T> type) {
public static <T> Matcher<T> instanceOf(Class<?> type) {
return (Matcher<T>) new IsInstanceOf(type);
}

/**
* Is the value an instance of a particular type?
* Use this version to make generics conform, for example in
* the JMock clause <code>with(any(Thing.class))</code>
*/
@SuppressWarnings("unchecked")
@Factory
public static <T> Matcher<T> any(Class<T> type) {
return (Matcher<T>) new IsInstanceOf(type);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,16 @@
import org.hamcrest.TypeSafeMatcher;
import static org.hamcrest.core.IsEqual.equalTo;

public class IsMapContainingKey<K> extends TypeSafeMatcher<Map<? extends K,?>> {
public class IsMapContainingKey<K> extends TypeSafeMatcher<Map<? super K,?>> {
private final Matcher<? super K> keyMatcher;

public IsMapContainingKey(Matcher<? super K> keyMatcher) {
this.keyMatcher = keyMatcher;
}

@Override
public boolean matchesSafely(Map<? extends K, ? > item) {
for (K key : item.keySet()) {
public boolean matchesSafely(Map<? super K, ?> item) {
for (Object key : item.keySet()) {
if (keyMatcher.matches(key)) {
return true;
}
Expand All @@ -31,12 +31,13 @@ public void describeTo(Description description) {
}

@Factory
public static <K> Matcher<Map<? extends K,?>> hasKey(K key) {
return IsMapContainingKey.<K>hasKey(equalTo(key));
public static <K> Matcher<Map<? super K,?>> hasKey(K key) {
// does not forward to other hasKey method to help compiler cope with type inference
return new IsMapContainingKey<K>(equalTo(key));
}

@Factory
public static <K> Matcher<Map<? extends K,?>> hasKey(Matcher<? super K> keyMatcher) {
return new IsMapContainingKey<K>(keyMatcher);
public static <K> Matcher<Map<? super K,?>> hasKey(Matcher<? super K> keyMatcher) {
return new IsMapContainingKey<K>(keyMatcher);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -80,4 +80,12 @@ public void describeMismatch(Object item, Description mismatchDescription) {
assertEquals(expectedMessage, e.getMessage());
}
}


public void testCanAssertSubtypes() {
Integer aSub = new Integer(1);
Number aSuper = aSub;

assertThat(aSub, equalTo(aSuper));
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.hamcrest.collection;

import static java.util.Arrays.asList;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.core.IsCollectionContaining.hasItem;
import static org.hamcrest.core.IsCollectionContaining.hasItems;
import static org.hamcrest.core.IsEqual.equalTo;
Expand All @@ -11,7 +12,8 @@

import org.hamcrest.AbstractMatcherTest;
import org.hamcrest.Matcher;
import org.hamcrest.MatcherAssert;
import org.hamcrest.core.IsCollectionContaining;
import org.hamcrest.core.IsEqual;

public class IsCollectionContainingTest extends AbstractMatcherTest {
@Override
Expand All @@ -20,19 +22,19 @@ protected Matcher<?> createMatcher() {
}

public void testMatchesACollectionThatContainsAnElementMatchingTheGivenMatcher() {
Matcher<Iterable<String>> itemMatcher = hasItem(equalTo("a"));
Matcher<Iterable<? super String>> itemMatcher = hasItem(equalTo("a"));

assertMatches("should match list that contains 'a'",
itemMatcher, asList("a", "b", "c"));
}

public void testDoesNotMatchCollectionThatDoesntContainAnElementMatchingTheGivenMatcher() {
final Matcher<Iterable<String>> matcher1 = hasItem(equalTo("a"));
final Matcher<Iterable<? super String>> matcher1 = hasItem(equalTo("a"));
assertDoesNotMatch("should not match list that doesn't contain 'a'",
matcher1, asList("b", "c"));


final Matcher<Iterable<String>> matcher2 = hasItem(equalTo("a"));
final Matcher<Iterable<? super String>> matcher2 = hasItem(equalTo("a"));
assertDoesNotMatch("should not match the empty list",
matcher2, new ArrayList<String>());
}
Expand All @@ -49,7 +51,8 @@ public void testCanMatchItemWhenCollectionHoldsSuperclass() // Issue 24
{
final Set<Number> s = new HashSet<Number>();
s.add(Integer.valueOf(2));
MatcherAssert.assertThat(s, hasItem(Integer.valueOf(2)));
assertThat(s, new IsCollectionContaining<Number>(new IsEqual<Object>(Integer.valueOf(2))));
assertThat(s, IsCollectionContaining.hasItem(Integer.valueOf(2)));
}

@SuppressWarnings("unchecked")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@ public void testMatchesMapContainingKeyWithNumberKeys() throws Exception {
// very ugly version!
assertThat(map, IsMapContainingKey.<Number>hasKey(Integer.valueOf(1)));

// this can be complex to compile! :)
assertThat(map, hasKey(Integer.valueOf(1)));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
*/
package org.hamcrest.core;

import static org.hamcrest.core.IsAnything.any;
import org.hamcrest.AbstractMatcherTest;
import org.hamcrest.Matcher;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.core.IsAnything.anything;

import org.hamcrest.AbstractMatcherTest;
import org.hamcrest.Matcher;

public class IsAnythingTest extends AbstractMatcherTest {

@Override
Expand All @@ -30,11 +30,4 @@ public void testCanOverrideDescription() {
assertDescription(description, anything(description));
}

public void testSupportsStaticTyping() {
requiresStringMatcher(any(String.class));
}

private void requiresStringMatcher(Matcher<String> arg) {
// no-op
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import org.hamcrest.AbstractMatcherTest;
import org.hamcrest.Matcher;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.core.IsInstanceOf.instanceOf;
import static org.hamcrest.core.IsInstanceOf.*;
import static org.hamcrest.core.IsNot.not;

public class IsInstanceOfTest extends AbstractMatcherTest {
Expand All @@ -19,22 +19,27 @@ public void testEvaluatesToTrueIfArgumentIsInstanceOfASpecificClass() {
assertThat(1, instanceOf(Number.class));
assertThat(1.0, instanceOf(Number.class));
assertThat(null, not(instanceOf(Number.class)));
assertThat("hello", not(instanceOf(Number.class)));
assertThat(new Object(), not(instanceOf(Number.class)));
}

public void testHasAReadableDescription() {
assertDescription("an instance of java.lang.Number", instanceOf(Number.class));
}

public void testWillReturnCorrectTypeForUseInJMock() {

public void testInstanceOfRequiresACastToReturnTheCorrectTypeForUseInJMock() {
@SuppressWarnings("unused")
Integer anInteger = (Integer)with(instanceOf(Integer.class));
}

public void testAnyWillReturnTheCorrectTypeForUseInJMock() {
@SuppressWarnings("unused")
Integer anInteger = with(instanceOf(Integer.class));
Integer anInteger = with(any(Integer.class));
}


private static <T> T with(Matcher<T> matcher) {
return null;
}
}

}

0 comments on commit a6b00c4

Please sign in to comment.