Skip to content

Commit 9aa0c3b

Browse files
author
smgfreeman
committed
ported 'Every' matcher from JUnit 'Each'
1 parent ee311fb commit 9aa0c3b

4 files changed

Lines changed: 83 additions & 7 deletions

File tree

‎core-matchers.xml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
<factory class="org.hamcrest.core.AnyOf"/>
66
<factory class="org.hamcrest.core.CombinableMatcher"/>
77
<factory class="org.hamcrest.core.DescribedAs"/>
8+
<factory class="org.hamcrest.core.Every"/>
89
<factory class="org.hamcrest.core.Is"/>
910
<factory class="org.hamcrest.core.IsAnything"/>
1011
<factory class="org.hamcrest.core.IsCollectionContaining"/>
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package org.hamcrest.core;
2+
3+
import org.hamcrest.Description;
4+
import org.hamcrest.Factory;
5+
import org.hamcrest.Matcher;
6+
import org.hamcrest.TypeSafeDiagnosingMatcher;
7+
8+
public class Every<T> extends TypeSafeDiagnosingMatcher<Iterable<T>> {
9+
private final Matcher<? super T> matcher;
10+
11+
public Every(Matcher<? super T> matcher) {
12+
this.matcher= matcher;
13+
}
14+
15+
@Override
16+
public boolean matchesSafely(Iterable<T> collection, Description mismatchDescription) {
17+
for (T t : collection) {
18+
if (!matcher.matches(t)) {
19+
mismatchDescription.appendText("an item ");
20+
matcher.describeMismatch(t, mismatchDescription);
21+
return false;
22+
}
23+
}
24+
return true;
25+
}
26+
27+
public void describeTo(Description description) {
28+
description.appendText("every item is ").appendDescriptionOf(matcher);
29+
}
30+
31+
32+
/**
33+
* @param itemMatcher A matcher to apply to every element in a collection.
34+
* @return Evaluates to TRUE for a collection in which every item matches itemMatcher
35+
*/
36+
@Factory
37+
public static <U> Matcher<Iterable<U>> everyItem(final Matcher<U> itemMatcher) {
38+
return new Every<U>(itemMatcher);
39+
}
40+
41+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package org.hamcrest.core;
2+
3+
import static java.util.Arrays.asList;
4+
import static org.hamcrest.CoreMatchers.containsString;
5+
import static org.hamcrest.CoreMatchers.not;
6+
import static org.hamcrest.MatcherAssert.assertThat;
7+
import static org.junit.Assert.assertEquals;
8+
9+
import java.util.ArrayList;
10+
11+
import org.hamcrest.StringDescription;
12+
import org.junit.Test;
13+
14+
public class EveryTest {
15+
@Test public void isTrueWhenEveryValueMatches() {
16+
assertThat(asList("AaA", "BaB", "CaC"), Every.everyItem(containsString("a")));
17+
assertThat(asList("AbA", "BbB", "CbC"), not(Every.everyItem(containsString("a"))));
18+
}
19+
20+
@Test public void isAlwaysTrueForEmptyLists() {
21+
assertThat(new ArrayList<String>(), Every.everyItem(containsString("a")));
22+
}
23+
24+
@Test public void describesItself() {
25+
final Every<String> each= new Every<String>(containsString("a"));
26+
assertEquals("every item is a string containing \"a\"", each.toString());
27+
28+
StringDescription description = new StringDescription();
29+
each.matchesSafely(asList("BbB"), description);
30+
assertEquals("an item was \"BbB\"", description.toString());
31+
}
32+
}

‎matchers.xml‎

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,22 @@
11
<matchers>
22

33
<!-- Core -->
4+
<factory class="org.hamcrest.core.AllOf"/>
5+
<factory class="org.hamcrest.core.AnyOf"/>
6+
<factory class="org.hamcrest.core.CombinableMatcher"/>
7+
<factory class="org.hamcrest.core.DescribedAs"/>
8+
<factory class="org.hamcrest.core.Every"/>
49
<factory class="org.hamcrest.core.Is"/>
10+
<factory class="org.hamcrest.core.IsAnything"/>
511
<factory class="org.hamcrest.core.IsCollectionContaining"/>
6-
<factory class="org.hamcrest.core.IsNot"/>
712
<factory class="org.hamcrest.core.IsEqual"/>
813
<factory class="org.hamcrest.core.IsInstanceOf"/>
9-
<factory class="org.hamcrest.core.AllOf"/>
10-
<factory class="org.hamcrest.core.AnyOf"/>
11-
<factory class="org.hamcrest.core.IsSame"/>
12-
<factory class="org.hamcrest.core.IsAnything"/>
14+
<factory class="org.hamcrest.core.IsNot"/>
1315
<factory class="org.hamcrest.core.IsNull"/>
14-
<factory class="org.hamcrest.core.DescribedAs"/>
16+
<factory class="org.hamcrest.core.IsSame"/>
1517
<factory class="org.hamcrest.core.StringContains"/>
16-
<factory class="org.hamcrest.core.StringEndsWith"/>
1718
<factory class="org.hamcrest.core.StringStartsWith"/>
19+
<factory class="org.hamcrest.core.StringEndsWith"/>
1820

1921
<!-- Collection -->
2022
<factory class="org.hamcrest.collection.IsArray"/>

0 commit comments

Comments
 (0)