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.AnyOf.anyOf; 8 import static org.hamcrest.core.IsEqual.equalTo; 9 import static org.hamcrest.core.StringEndsWith.endsWith; 10 import static org.hamcrest.core.StringStartsWith.startsWith; 11 12 public final class AnyOfTest { 13 14 @Test public void 15 copesWithNullsAndUnknownTypes() { 16 Matcher<String> matcher = anyOf(equalTo("irrelevant"), startsWith("irr")); 17 18 assertNullSafe(matcher); 19 assertUnknownTypeSafe(matcher); 20 } 21 22 @Test public void 23 evaluatesToTheTheLogicalDisjunctionOfTwoOtherMatchers() { 24 Matcher<String> matcher = anyOf(startsWith("goo"), endsWith("ood")); 25 26 assertMatches("didn't pass both sub-matchers", matcher, "good"); 27 assertMatches("didn't pass second sub-matcher", matcher, "mood"); 28 assertMatches("didn't pass first sub-matcher", matcher, "goon"); 29 assertDoesNotMatch("didn't fail both sub-matchers", matcher, "flan"); 30 } 31 32 @Test public void 33 evaluatesToTheTheLogicalDisjunctionOfManyOtherMatchers() { 34 Matcher<String> matcher = anyOf(startsWith("g"), startsWith("go"), endsWith("d"), startsWith("go"), startsWith("goo")); 35 36 assertMatches("didn't pass middle sub-matcher", matcher, "vlad"); 37 assertDoesNotMatch("didn't fail all sub-matchers", matcher, "flan"); 38 } 39 40 @SuppressWarnings("unchecked") 41 @Test public void 42 supportsMixedTypes() { 43 final Matcher<SampleSubClass> matcher = anyOf( 44 equalTo(new SampleBaseClass("bad")), 45 equalTo(new SampleBaseClass("good")), 46 equalTo(new SampleSubClass("ugly"))); 47 48 assertMatches("didn't pass middle sub-matcher", matcher, new SampleSubClass("good")); 49 } 50 51 @Test public void 52 hasAReadableDescription() { 53 assertDescription("(\"good\" or \"bad\" or \"ugly\")", 54 anyOf(equalTo("good"), equalTo("bad"), equalTo("ugly"))); 55 } 56 } 57