1 package org.hamcrest.core; 2 3 import org.hamcrest.Matcher; 4 import org.junit.Test; 5 6 import static org.hamcrest.AbstractMatcherTest.*; 7 import static org.hamcrest.core.AllOf.allOf; 8 import static org.hamcrest.core.Is.is; 9 import static org.hamcrest.core.IsEqual.equalTo; 10 import static org.hamcrest.core.IsNull.notNullValue; 11 import static org.hamcrest.core.StringEndsWith.endsWith; 12 import static org.hamcrest.core.StringStartsWith.startsWith; 13 14 public final class AllOfTest { 15 16 @Test public void 17 copesWithNullsAndUnknownTypes() { 18 Matcher<String> matcher = allOf(equalTo("irrelevant"), startsWith("irr")); 19 20 assertNullSafe(matcher); 21 assertUnknownTypeSafe(matcher); 22 } 23 24 @Test public void 25 evaluatesToTheTheLogicalConjunctionOfTwoOtherMatchers() { 26 Matcher<String> matcher = allOf(startsWith("goo"), endsWith("ood")); 27 28 assertMatches("didn't pass both sub-matchers", matcher, "good"); 29 assertDoesNotMatch("didn't fail first sub-matcher", matcher, "mood"); 30 assertDoesNotMatch("didn't fail second sub-matcher", matcher, "goon"); 31 assertDoesNotMatch("didn't fail both sub-matchers", matcher, "fred"); 32 } 33 34 @Test public void 35 evaluatesToTheTheLogicalConjunctionOfManyOtherMatchers() { 36 Matcher<String> matcher = allOf(startsWith("g"), startsWith("go"), endsWith("d"), startsWith("go"), startsWith("goo")); 37 38 assertMatches("didn't pass all sub-matchers", matcher, "good"); 39 assertDoesNotMatch("didn't fail middle sub-matcher", matcher, "goon"); 40 } 41 42 @Test public void 43 supportsMixedTypes() { 44 final Matcher<SampleSubClass> matcher = allOf( 45 equalTo(new SampleBaseClass("bad")), 46 is(notNullValue()), 47 equalTo(new SampleBaseClass("good")), 48 equalTo(new SampleSubClass("ugly"))); 49 50 assertDoesNotMatch("didn't fail last sub-matcher", matcher, new SampleSubClass("good")); 51 } 52 53 @Test public void 54 hasAReadableDescription() { 55 assertDescription("(\"good\" and \"bad\" and \"ugly\")", 56 allOf(equalTo("good"), equalTo("bad"), equalTo("ugly"))); 57 } 58 59 @Test public void 60 hasAMismatchDescriptionDescribingTheFirstFailingMatch() { 61 assertMismatchDescription("\"good\" was \"bad\"", allOf(equalTo("bad"), equalTo("good")), "bad"); 62 } 63 } 64