1 package com.lexicalscope.fluentreflection; 2 3 import static com.lexicalscope.fluentreflection.ListBuilder.list; 4 import static org.hamcrest.MatcherAssert.assertThat; 5 import static org.hamcrest.Matchers.*; 6 7 import org.hamcrest.Description; 8 import org.hamcrest.Matcher; 9 import org.jmock.Expectations; 10 import org.junit.Rule; 11 import org.junit.Test; 12 13 import com.lexicalscope.fluentreflection.MatcherOr; 14 15 public class TestMatcherOr { 16 @Rule 17 public final JUnitRuleMockery context = new JUnitRuleMockery(); 18 19 private final Matcher<String> matcherA = containsString("a"); 20 private final Matcher<String> matcherB = containsString("b"); 21 22 private final MatcherOr<String> matcher = 23 MatcherOr.orOf(list(matcherA).add(matcherB).$()); 24 25 @Test 26 public void ifOnlyOneMatcherMatchesThenTrue() throws Exception { 27 assertThat("a", matcher); 28 assertThat("b", matcher); 29 } 30 31 @Test 32 public void ifBothMatcherMatchesThenTrue() throws Exception { 33 assertThat("ab", matcher); 34 } 35 36 @Test 37 public void ifNeitherMatcherMatchesThenFalse() throws Exception { 38 assertThat("x", not(matcher)); 39 } 40 41 @Test 42 public void descriptionDescribesBothMatchers() throws Exception { 43 final Description description = context.mock(Description.class); 44 45 context.checking(new Expectations() { 46 { 47 oneOf(description).appendDescriptionOf(matcherA); 48 oneOf(description).appendText(" or "); 49 oneOf(description).appendDescriptionOf(matcherB); 50 } 51 }); 52 53 matcher.describeTo(description); 54 } 55 56 @Test 57 public void descriptionWithSingleMatcherIsSensible() throws Exception { 58 final Description description = context.mock(Description.class); 59 60 context.checking(new Expectations() { 61 { 62 oneOf(description).appendDescriptionOf(matcherA); 63 } 64 }); 65 66 MatcherOr.orOf(list(matcherA).$()).describeTo(description); 67 } 68 }