22 */
33package org .hamcrest .core ;
44
5- import org .hamcrest .AbstractMatcherTest ;
6- import org .hamcrest .Matcher ;
7-
8- import static org .hamcrest .MatcherAssert .assertThat ;
5+ import static org .hamcrest .AbstractMatcherTest .assertDescription ;
6+ import static org .hamcrest .AbstractMatcherTest .assertDoesNotMatch ;
7+ import static org .hamcrest .AbstractMatcherTest .assertMatches ;
8+ import static org .hamcrest .AbstractMatcherTest .assertNullSafe ;
9+ import static org .hamcrest .AbstractMatcherTest .assertUnknownTypeSafe ;
910import static org .hamcrest .core .IsEqual .equalTo ;
10- import static org .hamcrest .core .IsNot .not ;
11-
1211
13- public class IsEqualTest extends AbstractMatcherTest {
12+ import org .hamcrest .Matcher ;
13+ import org .junit .Test ;
1414
15- @ Override
16- protected Matcher <?> createMatcher () {
17- return equalTo ("irrelevant" );
18- }
15+ public final class IsEqualTest {
1916
20- public void testComparesObjectsUsingEqualsMethod () {
21- assertThat ( "hi" , equalTo ( "hi" ));
22- assertThat ( "bye" , not ( equalTo ("hi" )) );
17+ @ Test public void
18+ copesWithNullsAndUnknownTypes () {
19+ Matcher <?> matcher = equalTo ("irrelevant" );
2320
24- assertThat ( 1 , equalTo ( 1 ) );
25- assertThat ( 1 , not ( equalTo ( 2 )) );
21+ assertNullSafe ( matcher );
22+ assertUnknownTypeSafe ( matcher );
2623 }
2724
28- public void testCanCompareNullValues () {
29- assertThat (null , equalTo (null ));
25+ @ Test public void
26+ comparesObjectsUsingEqualsMethod () {
27+ final Matcher <String > matcher1 = equalTo ("hi" );
28+ assertMatches ("didn't match" , matcher1 , "hi" );
29+ assertDoesNotMatch ("matched unexpectedly" , matcher1 , "bye" );
30+ assertDoesNotMatch ("matched unexpectedly" , matcher1 , null );
31+
32+ final Matcher <Integer > matcher2 = equalTo (1 );
33+ assertMatches ("didn't match" , matcher2 , 1 );
34+ assertDoesNotMatch ("matched unexpectedly" , matcher2 , 2 );
35+ assertDoesNotMatch ("matched unexpectedly" , matcher2 , null );
36+ }
3037
31- assertThat (null , not (equalTo ("hi" )));
32- assertThat ("hi" , not (equalTo (null )));
38+ @ Test public void
39+ canCompareNullValues () {
40+ final Matcher <Object > matcher = equalTo (null );
41+
42+ assertMatches ("didn't match" , matcher , null );
43+ assertDoesNotMatch ("matched unexpectedly" , matcher , 2 );
44+ assertDoesNotMatch ("matched unexpectedly" , matcher , "hi" );
45+ assertDoesNotMatch ("matched unexpectedly" , matcher , new String [] {"a" , "b" });
3346 }
3447
35- public void testHonoursIsEqualImplementationEvenWithNullValues () {
48+ @ Test public void
49+ honoursIsEqualImplementationEvenWithNullValues () {
3650 Object alwaysEqual = new Object () {
3751 @ Override
3852 public boolean equals (Object obj ) {
@@ -46,52 +60,58 @@ public boolean equals(Object obj) {
4660 }
4761 };
4862
49- assertThat (alwaysEqual , equalTo (null ));
50- assertThat (neverEqual , not (equalTo (null )));
63+ final Matcher <Object > matcher = equalTo (null );
64+ assertMatches ("didn't match" , matcher , alwaysEqual );
65+ assertDoesNotMatch ("matched unexpectedly" , matcher , neverEqual );
5166 }
52-
53- public void testComparesTheElementsOfAnObjectArray () {
67+
68+ @ Test public void
69+ comparesTheElementsOfAnObjectArray () {
5470 String [] s1 = {"a" , "b" };
5571 String [] s2 = {"a" , "b" };
5672 String [] s3 = {"c" , "d" };
5773 String [] s4 = {"a" , "b" , "c" , "d" };
5874
59- assertThat (s1 , equalTo (s1 ));
60- assertThat (s2 , equalTo (s1 ));
61- assertThat (s3 , not (equalTo (s1 )));
62- assertThat (s4 , not (equalTo (s1 )));
63- }
64-
65- public void testComparesArraysToNull () {
66- assertThat (new String []{"a" , "b" }, not (equalTo (null )));
67- assertThat (null , not (equalTo (new String []{"a" , "b" })));
75+ final Matcher <String []> matcher = equalTo (s1 );
76+ assertMatches ("didn't match" , matcher , s1 );
77+ assertMatches ("didn't match" , matcher , s2 );
78+ assertDoesNotMatch ("matched unexpectedly" , matcher , s3 );
79+ assertDoesNotMatch ("matched unexpectedly" , matcher , s4 );
80+ assertDoesNotMatch ("matched unexpectedly" , matcher , null );
6881 }
6982
70- public void testComparesTheElementsOfAnArrayOfPrimitiveTypes () {
83+ @ Test public void
84+ comparesTheElementsOfAnArrayOfPrimitiveTypes () {
7185 int [] i1 = new int []{1 , 2 };
7286 int [] i2 = new int []{1 , 2 };
7387 int [] i3 = new int []{3 , 4 };
7488 int [] i4 = new int []{1 , 2 , 3 , 4 };
7589
76- assertThat (i1 , equalTo (i1 ));
77- assertThat (i2 , equalTo (i1 ));
78- assertThat (i3 , not (equalTo (i1 )));
79- assertThat (i4 , not (equalTo (i1 )));
90+ final Matcher <int []> matcher = equalTo (i1 );
91+ assertMatches ("didn't match" , matcher , i1 );
92+ assertMatches ("didn't match" , matcher , i2 );
93+ assertDoesNotMatch ("matched unexpectedly" , matcher , i3 );
94+ assertDoesNotMatch ("matched unexpectedly" , matcher , i4 );
95+ assertDoesNotMatch ("matched unexpectedly" , matcher , null );
8096 }
8197
82- public void testRecursivelyTestsElementsOfArrays () {
98+ @ Test public void
99+ recursivelyTestsElementsOfArrays () {
83100 int [][] i1 = new int [][]{{1 , 2 }, {3 , 4 }};
84101 int [][] i2 = new int [][]{{1 , 2 }, {3 , 4 }};
85102 int [][] i3 = new int [][]{{5 , 6 }, {7 , 8 }};
86103 int [][] i4 = new int [][]{{1 , 2 , 3 , 4 }, {3 , 4 }};
87104
88- assertThat (i1 , equalTo (i1 ));
89- assertThat (i2 , equalTo (i1 ));
90- assertThat (i3 , not (equalTo (i1 )));
91- assertThat (i4 , not (equalTo (i1 )));
105+ final Matcher <int [][]> matcher = equalTo (i1 );
106+ assertMatches ("didn't match" , matcher , i1 );
107+ assertMatches ("didn't match" , matcher , i2 );
108+ assertDoesNotMatch ("matched unexpectedly" , matcher , i3 );
109+ assertDoesNotMatch ("matched unexpectedly" , matcher , i4 );
110+ assertDoesNotMatch ("matched unexpectedly" , matcher , null );
92111 }
93112
94- public void testIncludesTheResultOfCallingToStringOnItsArgumentInTheDescription () {
113+ @ Test public void
114+ includesTheResultOfCallingToStringOnItsArgumentInTheDescription () {
95115 final String argumentDescription = "ARGUMENT DESCRIPTION" ;
96116 Object argument = new Object () {
97117 @ Override
@@ -102,12 +122,14 @@ public String toString() {
102122 assertDescription ("<" + argumentDescription + ">" , equalTo (argument ));
103123 }
104124
105- public void testReturnsAnObviousDescriptionIfCreatedWithANestedMatcherByMistake () {
125+ @ Test public void
126+ returnsAnObviousDescriptionIfCreatedWithANestedMatcherByMistake () {
106127 Matcher <? super String > innerMatcher = equalTo ("NestedMatcher" );
107128 assertDescription ("<" + innerMatcher .toString () + ">" , equalTo (innerMatcher ));
108129 }
109130
110- public void testReturnsGoodDescriptionIfCreatedWithNullReference () {
131+ @ Test public void
132+ returnsGoodDescriptionIfCreatedWithNullReference () {
111133 assertDescription ("null" , equalTo (null ));
112134 }
113135}
0 commit comments