Home | History | Annotate | Download | only in results
      1 package org.junit.experimental.results;
      2 
      3 import org.hamcrest.BaseMatcher;
      4 import org.hamcrest.Description;
      5 import org.hamcrest.Matcher;
      6 import org.junit.internal.matchers.TypeSafeMatcher;
      7 
      8 /**
      9  * Matchers on a PrintableResult, to enable JUnit self-tests.
     10  * For example:
     11  *
     12  * <pre>
     13  * 		assertThat(testResult(HasExpectedException.class), isSuccessful());
     14  * </pre>
     15  */
     16 public class ResultMatchers {
     17 	/**
     18 	 * Matches if the tests are all successful
     19 	 */
     20 	public static Matcher<PrintableResult> isSuccessful() {
     21 		return failureCountIs(0);
     22 	}
     23 
     24 	/**
     25 	 * Matches if there are {@code count} failures
     26 	 */
     27 	public static Matcher<PrintableResult> failureCountIs(final int count) {
     28 		return new TypeSafeMatcher<PrintableResult>() {
     29 			public void describeTo(Description description) {
     30 				description.appendText("has " + count + " failures");
     31 			}
     32 
     33 			@Override
     34 			public boolean matchesSafely(PrintableResult item) {
     35 				return item.failureCount() == count;
     36 			}
     37 		};
     38 	}
     39 
     40 	/**
     41 	 * Matches if the result has exactly one failure, and it contains {@code string}
     42 	 */
     43 	public static Matcher<Object> hasSingleFailureContaining(final String string) {
     44 		return new BaseMatcher<Object>() {
     45 			public boolean matches(Object item) {
     46 				return item.toString().contains(string) && failureCountIs(1).matches(item);
     47 			}
     48 
     49 			public void describeTo(Description description) {
     50 				description.appendText("has single failure containing " + string);
     51 			}
     52 		};
     53 	}
     54 
     55 	/**
     56 	 * Matches if the result has one or more failures, and at least one of them
     57 	 * contains {@code string}
     58 	 */
     59 	public static Matcher<PrintableResult> hasFailureContaining(final String string) {
     60 		return new BaseMatcher<PrintableResult>() {
     61 			public boolean matches(Object item) {
     62 				return item.toString().contains(string);
     63 			}
     64 
     65 			public void describeTo(Description description) {
     66 				description.appendText("has failure containing " + string);
     67 			}
     68 		};
     69 	}
     70 }
     71