Skip to content

Commit 182e80f

Browse files
For better testability, add toString() implementations
1 parent fde50d4 commit 182e80f

File tree

9 files changed

+77
-2
lines changed

9 files changed

+77
-2
lines changed

src/main/java/org/cornutum/hamcrest/BaseCompositeMatcher.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,11 @@ public Optional<Matcher<? super T>> getMismatch()
8080
{
8181
return Optional.ofNullable( mismatch);
8282
}
83+
84+
public String toString()
85+
{
86+
return String.format( "%s[%s]", getClass().getSimpleName(), BaseCompositeMatcher.this.getClass().getSimpleName());
87+
}
8388
}
8489

8590
/**
@@ -217,4 +222,9 @@ private Optional<Matcher<? super T>> getMismatch()
217222
? Optional.empty()
218223
: compositeMatcher.getMismatch();
219224
}
225+
226+
public String toString()
227+
{
228+
return String.format( "%s[]", getClass().getSimpleName());
229+
}
220230
}

src/main/java/org/cornutum/hamcrest/ContainsElements.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,4 +85,9 @@ public void describeMismatch( Object actual, Description description)
8585
{
8686
containsMembers.describeMismatch( actual, description);
8787
}
88+
89+
public String toString()
90+
{
91+
return String.format( "%s[]", getClass().getSimpleName());
92+
}
8893
}

src/main/java/org/cornutum/hamcrest/ContainsMembers.java

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,12 @@ public T getActualMember()
6161
public Matcher<T> getMatcher()
6262
{
6363
return matcher;
64-
}
64+
}
65+
66+
public String toString()
67+
{
68+
return String.format( "%s[actual=%s, matcher=%s]", getClass().getSimpleName(), actualMember, matcher);
69+
}
6570
}
6671

6772
/**
@@ -235,6 +240,11 @@ private String toString( List<?> members)
235240
.map( m -> String.valueOf(m))
236241
.reduce( "", (list, m) -> list + (list.isEmpty()? "" : ", ") + m);
237242
}
243+
244+
public String toString()
245+
{
246+
return String.format( "%s[%s]", getClass().getSimpleName(), ContainsMembers.this.getClass().getSimpleName());
247+
}
238248
}
239249

240250
/**
@@ -317,4 +327,9 @@ private Optional<MemberMatch> getMemberMismatch()
317327
? Optional.empty()
318328
: memberMatcher.getMemberMismatch();
319329
}
330+
331+
public String toString()
332+
{
333+
return String.format( "%s[]", getClass().getSimpleName());
334+
}
320335
}

src/main/java/org/cornutum/hamcrest/ExpectedFailure.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,11 @@ public static ExpectedFailure<AssertionError> expectFailure()
7878
return expectFailure( AssertionError.class);
7979
}
8080

81+
public String toString()
82+
{
83+
return String.format( "%s[%s]", getClass().getSimpleName(), failureType.getSimpleName());
84+
}
85+
8186
/**
8287
* Implements an action that could throw any type of Throwable, including a checked exception.
8388
*/

src/main/java/org/cornutum/hamcrest/ListsElements.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,4 +85,9 @@ public void describeMismatch( Object actual, Description description)
8585
{
8686
listsMembers.describeMismatch( actual, description);
8787
}
88+
89+
public String toString()
90+
{
91+
return String.format( "%s[]", getClass().getSimpleName());
92+
}
8893
}

src/main/java/org/cornutum/hamcrest/ListsMembers.java

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,12 @@ public T getActualMember()
6161
public Matcher<T> getMatcher()
6262
{
6363
return matcher;
64-
}
64+
}
65+
66+
public String toString()
67+
{
68+
return String.format( "%s[actual=%s, matcher=%s]", getClass().getSimpleName(), actualMember, matcher);
69+
}
6570
}
6671

6772
/**
@@ -207,6 +212,11 @@ public Optional<MemberMatch> getMemberMismatch()
207212
{
208213
return Optional.ofNullable( memberMismatch);
209214
}
215+
216+
public String toString()
217+
{
218+
return String.format( "%s[%s]", getClass().getSimpleName(), ListsMembers.this.getClass().getSimpleName());
219+
}
210220
}
211221

212222
/**
@@ -289,4 +299,9 @@ private Optional<MemberMatch> getMemberMismatch()
289299
? Optional.empty()
290300
: memberMatcher.getMemberMismatch();
291301
}
302+
303+
public String toString()
304+
{
305+
return String.format( "%s[]", getClass().getSimpleName());
306+
}
292307
}

src/main/java/org/cornutum/hamcrest/MatchesFunction.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,11 @@ public Optional<String> getMismatch()
8282
? Optional.of( mismatch.toString())
8383
: Optional.empty();
8484
}
85+
86+
public String toString()
87+
{
88+
return String.format( "%s[%s]", getClass().getSimpleName(), functionName);
89+
}
8590
}
8691

8792
/**
@@ -218,4 +223,9 @@ private FunctionMatcher getFunctionMatcher( Object actual)
218223

219224
return functionMatcher;
220225
}
226+
227+
public String toString()
228+
{
229+
return String.format( "%s[%s]", getClass().getSimpleName(), functionName);
230+
}
221231
}

src/main/java/org/cornutum/hamcrest/VisitsList.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,4 +84,9 @@ public void describeMismatch( Object actual, Description description)
8484
{
8585
listsMembers.describeMismatch( actual, description);
8686
}
87+
88+
public String toString()
89+
{
90+
return String.format( "%s[]", getClass().getSimpleName());
91+
}
8792
}

src/main/java/org/cornutum/hamcrest/VisitsMembers.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,4 +84,9 @@ public void describeMismatch( Object actual, Description description)
8484
{
8585
containsMembers.describeMismatch( actual, description);
8686
}
87+
88+
public String toString()
89+
{
90+
return String.format( "%s[]", getClass().getSimpleName());
91+
}
8792
}

0 commit comments

Comments
 (0)