forked from junit-team/junit4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
David Saff
committed
May 27, 2009
1 parent
7ca2c7d
commit e35fb32
Showing
34 changed files
with
104 additions
and
171 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 8 additions & 28 deletions
36
src/main/java/org/junit/internal/matchers/CombinableMatcher.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,14 @@ | ||
package org.junit.internal.matchers; | ||
|
||
import static org.hamcrest.CoreMatchers.allOf; | ||
import static org.hamcrest.CoreMatchers.anyOf; | ||
import org.hamcrest.BaseMatcher; | ||
import org.hamcrest.Description; | ||
import org.hamcrest.Matcher; | ||
|
||
public class CombinableMatcher<T> extends BaseMatcher<T> { | ||
|
||
private final Matcher<? extends T> fMatcher; | ||
|
||
public CombinableMatcher(Matcher<? extends T> matcher) { | ||
fMatcher= matcher; | ||
} | ||
|
||
public boolean matches(Object item) { | ||
return fMatcher.matches(item); | ||
} | ||
|
||
public void describeTo(Description description) { | ||
description.appendDescriptionOf(fMatcher); | ||
} | ||
|
||
@SuppressWarnings("unchecked") | ||
public CombinableMatcher<T> and(Matcher<? extends T> matcher) { | ||
return new CombinableMatcher<T>(allOf(matcher, fMatcher)); | ||
} | ||
|
||
@SuppressWarnings("unchecked") | ||
public CombinableMatcher<T> or(Matcher<? extends T> matcher) { | ||
return new CombinableMatcher<T>(anyOf(matcher, fMatcher)); | ||
/** | ||
* @deprectated Use org.hamcrest.core.CombinableMatcher directly | ||
*/ | ||
@Deprecated | ||
public class CombinableMatcher<T> extends org.hamcrest.core.CombinableMatcher<T> { | ||
// should only be using static factories | ||
private CombinableMatcher(Matcher<? super T> matcher) { | ||
super(matcher); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,15 @@ | ||
package org.junit.internal.matchers; | ||
|
||
import static org.hamcrest.CoreMatchers.not; | ||
import static org.junit.internal.matchers.IsCollectionContaining.hasItem; | ||
import org.hamcrest.BaseMatcher; | ||
import org.hamcrest.Description; | ||
import org.hamcrest.Matcher; | ||
import org.hamcrest.core.Every; | ||
|
||
|
||
/** | ||
* @deprecated use org.hamcrest.core.Every | ||
*/ | ||
@Deprecated | ||
public class Each { | ||
public static <T> Matcher<Iterable<T>> each(final Matcher<T> individual) { | ||
final Matcher<Iterable<T>> allItemsAre = not(hasItem(not(individual))); | ||
|
||
return new BaseMatcher<Iterable<T>>() { | ||
public boolean matches(Object item) { | ||
return allItemsAre.matches(item); | ||
} | ||
|
||
public void describeTo(Description description) { | ||
description.appendText("each "); | ||
individual.describeTo(description); | ||
} | ||
}; | ||
return Every.everyItem(individual); | ||
} | ||
} |
71 changes: 10 additions & 61 deletions
71
src/main/java/org/junit/internal/matchers/IsCollectionContaining.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,67 +1,16 @@ | ||
package org.junit.internal.matchers; | ||
|
||
import static org.hamcrest.core.AllOf.allOf; | ||
import static org.hamcrest.core.IsEqual.equalTo; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Collection; | ||
|
||
import org.hamcrest.Description; | ||
import org.hamcrest.Factory; | ||
import org.hamcrest.Matcher; | ||
|
||
// Copied (hopefully temporarily) from hamcrest-library | ||
public class IsCollectionContaining<T> extends TypeSafeMatcher<Iterable<T>> { | ||
private final Matcher<? extends T> elementMatcher; | ||
|
||
public IsCollectionContaining(Matcher<? extends T> elementMatcher) { | ||
this.elementMatcher = elementMatcher; | ||
} | ||
|
||
@Override | ||
public boolean matchesSafely(Iterable<T> collection) { | ||
for (T item : collection) { | ||
if (elementMatcher.matches(item)){ | ||
return true; | ||
} | ||
} | ||
return false; | ||
} | ||
|
||
public void describeTo(Description description) { | ||
description | ||
.appendText("a collection containing ") | ||
.appendDescriptionOf(elementMatcher); | ||
} | ||
|
||
@Factory | ||
public static <T> Matcher<Iterable<T>> hasItem(Matcher<? extends T> elementMatcher) { | ||
return new IsCollectionContaining<T>(elementMatcher); | ||
} | ||
|
||
@Factory | ||
public static <T> Matcher<Iterable<T>> hasItem(T element) { | ||
return hasItem(equalTo(element)); | ||
} | ||
|
||
@Factory | ||
public static <T> Matcher<Iterable<T>> hasItems(Matcher<? extends T>... elementMatchers) { | ||
Collection<Matcher<? extends Iterable<T>>> all | ||
= new ArrayList<Matcher<? extends Iterable<T>>>(elementMatchers.length); | ||
for (Matcher<? extends T> elementMatcher : elementMatchers) { | ||
all.add(hasItem(elementMatcher)); | ||
} | ||
return allOf(all); | ||
} | ||
|
||
@Factory | ||
public static <T> Matcher<Iterable<T>> hasItems(T... elements) { | ||
Collection<Matcher<? extends Iterable<T>>> all | ||
= new ArrayList<Matcher<? extends Iterable<T>>>(elements.length); | ||
for (T element : elements) { | ||
all.add(hasItem(element)); | ||
} | ||
return allOf(all); | ||
} | ||
|
||
/** | ||
* @deprecated use org.hamcrest.core.IsCollectionContaining directly | ||
*/ | ||
@Deprecated | ||
public class IsCollectionContaining<T> extends org.hamcrest.core.IsCollectionContaining<T> { | ||
// Client code should just use static factories, so this should be OK | ||
private IsCollectionContaining(Matcher<? super T> elementMatcher) { | ||
super(elementMatcher); | ||
// TODO Auto-generated constructor stub | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,83 +1,101 @@ | ||
package org.junit.matchers; | ||
|
||
import org.hamcrest.CoreMatchers; | ||
import org.hamcrest.Matcher; | ||
import org.junit.internal.matchers.CombinableMatcher; | ||
import org.junit.internal.matchers.Each; | ||
import org.junit.internal.matchers.IsCollectionContaining; | ||
import org.junit.internal.matchers.StringContains; | ||
import org.hamcrest.core.CombinableMatcher; | ||
|
||
/** | ||
* Convenience import class: these are useful matchers for use with the assertThat method, but they are | ||
* not currently included in the basic CoreMatchers class from hamcrest. | ||
* @deprecated as of JUnit 4.7, all of these matchers are found in the org.hamcrest.CoreMatchers | ||
* class instead. | ||
*/ | ||
@Deprecated | ||
public class JUnitMatchers { | ||
// TODO (May 27, 2009 11:46:27 AM): deprecate all? | ||
/** | ||
* @param element | ||
* @return A matcher matching any collection containing element | ||
* @deprecated Use org.hamcrest.CoreMatchers.hasItem | ||
*/ | ||
public static <T> org.hamcrest.Matcher<java.lang.Iterable<T>> hasItem(T element) { | ||
return IsCollectionContaining.hasItem(element); | ||
@Deprecated | ||
public static <T> Matcher<Iterable<? super T>> hasItem(T element) { | ||
return CoreMatchers.hasItem(element); | ||
} | ||
|
||
/** | ||
* @param elementMatcher | ||
* @return A matcher matching any collection containing an element matching elementMatcher | ||
* @deprecated Use org.hamcrest.CoreMatchers.hasItem | ||
*/ | ||
public static <T> org.hamcrest.Matcher<java.lang.Iterable<T>> hasItem(org.hamcrest.Matcher<? extends T> elementMatcher) { | ||
return IsCollectionContaining.hasItem(elementMatcher); | ||
@Deprecated | ||
public static <T> Matcher<Iterable<? super T>> hasItem(Matcher<? super T> elementMatcher) { | ||
return CoreMatchers.hasItem(elementMatcher); | ||
} | ||
|
||
/** | ||
* @param elements | ||
* @return A matcher matching any collection containing every element in elements | ||
* @deprecated Use org.hamcrest.CoreMatchers.hasItems | ||
*/ | ||
@Deprecated | ||
public static <T> org.hamcrest.Matcher<java.lang.Iterable<T>> hasItems(T... elements) { | ||
return IsCollectionContaining.hasItems(elements); | ||
return CoreMatchers.hasItems(elements); | ||
} | ||
|
||
/** | ||
* @param elementMatchers | ||
* @return A matcher matching any collection containing at least one element that matches | ||
* each matcher in elementMatcher (this may be one element matching all matchers, | ||
* or different elements matching each matcher) | ||
* @deprecated Use org.hamcrest.CoreMatchers.hasItems | ||
*/ | ||
public static <T> org.hamcrest.Matcher<java.lang.Iterable<T>> hasItems(org.hamcrest.Matcher<? extends T>... elementMatchers) { | ||
return IsCollectionContaining.hasItems(elementMatchers); | ||
@Deprecated | ||
public static <T> Matcher<Iterable<T>> hasItems(Matcher<? super T>... elementMatchers) { | ||
return CoreMatchers.hasItems(elementMatchers); | ||
} | ||
|
||
/** | ||
* @param elementMatcher | ||
* @return A matcher matching any collection in which every element matches elementMatcher | ||
* @deprecated use CoreMatchers.everyItem directly | ||
*/ | ||
@Deprecated | ||
public static <T> Matcher<Iterable<T>> everyItem(final Matcher<T> elementMatcher) { | ||
return Each.each(elementMatcher); | ||
return CoreMatchers.everyItem(elementMatcher); | ||
} | ||
|
||
/** | ||
* @param substring | ||
* @return a matcher matching any string that contains substring | ||
* @deprecated Use org.hamcrest.CoreMatchers.containsString | ||
*/ | ||
@Deprecated | ||
public static org.hamcrest.Matcher<java.lang.String> containsString(java.lang.String substring) { | ||
return StringContains.containsString(substring); | ||
return CoreMatchers.containsString(substring); | ||
} | ||
|
||
/** | ||
* This is useful for fluently combining matchers that must both pass. For example: | ||
* <pre> | ||
* assertThat(string, both(containsString("a")).and(containsString("b"))); | ||
* </pre> | ||
* @deprecated Use org.hamcrest.CoreMatchers.both | ||
*/ | ||
public static <T> CombinableMatcher<T> both(Matcher<T> matcher) { | ||
return new CombinableMatcher<T>(matcher); | ||
@Deprecated | ||
public static <T> CombinableMatcher<T> both(Matcher<? super T> matcher) { | ||
return CoreMatchers.both(matcher); | ||
} | ||
|
||
/** | ||
* This is useful for fluently combining matchers where either may pass, for example: | ||
* <pre> | ||
* assertThat(string, both(containsString("a")).and(containsString("b"))); | ||
* </pre> | ||
* @deprecated Use org.hamcrest.CoreMatchers.either | ||
*/ | ||
public static <T> CombinableMatcher<T> either(Matcher<T> matcher) { | ||
return new CombinableMatcher<T>(matcher); | ||
@Deprecated | ||
public static <T> CombinableMatcher<T> either(Matcher<? super T> matcher) { | ||
return CoreMatchers.either(matcher); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.