1package org.hamcrest.object; 2 3import org.hamcrest.FeatureMatcher; 4import org.hamcrest.Matcher; 5 6import static org.hamcrest.core.IsEqual.equalTo; 7 8public class HasToString<T> extends FeatureMatcher<T, String> { 9 public HasToString(Matcher<? super String> toStringMatcher) { 10 super(toStringMatcher, "with toString()", "toString()"); 11 } 12 13 @Override 14 protected String featureValueOf(T actual) { 15 return String.valueOf(actual); 16 } 17 18 /** 19 * Creates a matcher that matches any examined object whose <code>toString</code> method 20 * returns a value that satisfies the specified matcher. 21 * For example: 22 * <pre>assertThat(true, hasToString(equalTo("TRUE")))</pre> 23 * 24 * @param toStringMatcher 25 * the matcher used to verify the toString result 26 */ 27 public static <T> Matcher<T> hasToString(Matcher<? super String> toStringMatcher) { 28 return new HasToString<T>(toStringMatcher); 29 } 30 31 /** 32 * Creates a matcher that matches any examined object whose <code>toString</code> method 33 * returns a value equalTo the specified string. 34 * For example: 35 * <pre>assertThat(true, hasToString("TRUE"))</pre> 36 * 37 * @param expectedToString 38 * the expected toString result 39 */ 40 public static <T> Matcher<T> hasToString(String expectedToString) { 41 return new HasToString<T>(equalTo(expectedToString)); 42 } 43} 44