Skip to content

Commit dcd62f0

Browse files
committed
skeleton test for equal values
1 parent 8494a13 commit dcd62f0

File tree

1 file changed

+71
-0
lines changed

1 file changed

+71
-0
lines changed
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package org.hamcrest.object;
2+
3+
import org.hamcrest.AbstractMatcherTest;
4+
import org.hamcrest.Description;
5+
import org.hamcrest.Matcher;
6+
import org.hamcrest.TypeSafeDiagnosingMatcher;
7+
8+
import java.lang.reflect.Field;
9+
import java.util.ArrayList;
10+
import java.util.List;
11+
12+
public class HasEqualsValuesTest extends AbstractMatcherTest {
13+
private static final WithPublicFields WITH_PUBLIC_FIELDS = new WithPublicFields(666, "a string");
14+
public static final HasEqualValues<WithPublicFields> WITH_PUBLIC_FIELDS_MATCHER = new HasEqualValues<>(WITH_PUBLIC_FIELDS);
15+
16+
public static class HasEqualValues<T> extends TypeSafeDiagnosingMatcher<T> {
17+
18+
private final T expectedObject;
19+
20+
public HasEqualValues(T expectedObject) {
21+
this.expectedObject = expectedObject;
22+
}
23+
24+
@Override
25+
protected boolean matchesSafely(T item, Description mismatchDescription) {
26+
27+
return true;
28+
}
29+
30+
@Override
31+
public void describeTo(Description description) {
32+
description.appendText("has equal values to ")
33+
.appendText(expectedObject.getClass().getSimpleName())
34+
.appendValueList("[", ",", "]", fieldNames());
35+
}
36+
37+
private List<String> fieldNames() {
38+
final List<String> result = new ArrayList<>();
39+
for (Field field : expectedObject.getClass().getFields()) {
40+
result.add(field.getName());
41+
}
42+
return result;
43+
}
44+
}
45+
46+
@Override
47+
protected Matcher<?> createMatcher() {
48+
return WITH_PUBLIC_FIELDS_MATCHER;
49+
}
50+
51+
public void testDescribesItself() {
52+
assertDescription(
53+
"has equal values to WithPublicFields[\"i\",\"s\"]",
54+
new HasEqualValues<>(WITH_PUBLIC_FIELDS));
55+
}
56+
57+
public void testMisMatchesDifferentType() {
58+
assertMatches(WITH_PUBLIC_FIELDS_MATCHER, new WithPublicFields(666, "a string"));
59+
}
60+
61+
62+
public static class WithPublicFields {
63+
public final int i;
64+
public final String s;
65+
66+
public WithPublicFields(int i, String s) {
67+
this.i = i;
68+
this.s = s;
69+
}
70+
}
71+
}

0 commit comments

Comments
 (0)