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.MatcherAnd; 14 15 public class TestMatcherAnd { 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 MatcherAnd<String> matcher = 23 MatcherAnd.andOf(list(matcherA).add(matcherB).$()); 24 25 @Test 26 public void ifOnlyOneMatcherMatchesThenFalse() throws Exception { 27 assertThat("a", not(matcher)); 28 assertThat("b", not(matcher)); 29 } 30 31 @Test 32 public void ifBothMatcherMatchesThenTrue() throws Exception { 33 assertThat("ab", matcher); 34 } 35 36 @Test 37 public void descriptionDescribesBothMatchers() throws Exception { 38 final Description description = context.mock(Description.class); 39 40 context.checking(new Expectations() { 41 { 42 oneOf(description).appendDescriptionOf(matcherA); 43 oneOf(description).appendText(" and "); 44 oneOf(description).appendDescriptionOf(matcherB); 45 } 46 }); 47 48 matcher.describeTo(description); 49 } 50 51 @Test 52 public void descriptionWithSingleMatcherIsSensible() throws Exception { 53 final Description description = context.mock(Description.class); 54 55 context.checking(new Expectations() { 56 { 57 oneOf(description).appendDescriptionOf(matcherA); 58 } 59 }); 60 61 MatcherAnd.andOf(list(matcherA).$()).describeTo(description); 62 } 63 }