• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 package org.hamcrest.core;
2 
3 import org.hamcrest.Matcher;
4 import org.junit.Test;
5 
6 import static org.hamcrest.AbstractMatcherTest.*;
7 import static org.hamcrest.core.IsEqual.equalTo;
8 import static org.hamcrest.core.IsEqual.equalToObject;
9 
10 public final class IsEqualTest {
11 
12     @Test public void
copesWithNullsAndUnknownTypes()13     copesWithNullsAndUnknownTypes() {
14         Matcher<?> matcher = equalTo("irrelevant");
15 
16         assertNullSafe(matcher);
17         assertUnknownTypeSafe(matcher);
18     }
19 
20     @Test public void
comparesObjectsUsingEqualsMethod()21     comparesObjectsUsingEqualsMethod() {
22         final Matcher<String> matcher1 = equalTo("hi");
23         assertMatches(matcher1, "hi");
24         assertDoesNotMatch(matcher1, "bye");
25         assertDoesNotMatch(matcher1, null);
26 
27         final Matcher<Integer> matcher2 = equalTo(1);
28         assertMatches(matcher2, 1);
29         assertDoesNotMatch(matcher2, 2);
30         assertDoesNotMatch(matcher2, null);
31     }
32 
33     @Test public void
canCompareNullValues()34     canCompareNullValues() {
35         final Matcher<Object> matcher = equalTo(null);
36 
37         assertMatches(matcher, null);
38         assertDoesNotMatch(matcher, 2);
39         assertDoesNotMatch(matcher, "hi");
40         assertDoesNotMatch(matcher, new String[] {"a", "b"});
41     }
42 
43     @SuppressWarnings("EqualsWhichDoesntCheckParameterClass")
44     @Test public void
honoursIsEqualImplementationEvenWithNullValues()45     honoursIsEqualImplementationEvenWithNullValues() {
46         Object alwaysEqual = new Object() {
47             @Override
48             public boolean equals(Object obj) {
49                 return true;
50             }
51         };
52         Object neverEqual = new Object() {
53             @Override
54             public boolean equals(Object obj) {
55                 return false;
56             }
57         };
58 
59         Matcher<Object> matcher = equalTo(null);
60 
61         assertMatches(matcher, alwaysEqual);
62         assertDoesNotMatch(matcher, neverEqual);
63     }
64 
65     @Test public void
comparesTheElementsOfAnObjectArray()66     comparesTheElementsOfAnObjectArray() {
67         String[] s1 = {"a", "b"};
68         String[] s2 = {"a", "b"};
69         String[] s3 = {"c", "d"};
70         String[] s4 = {"a", "b", "c", "d"};
71 
72         final Matcher<String[]> matcher = equalTo(s1);
73         assertMatches(matcher, s1);
74         assertMatches(matcher, s2);
75         assertDoesNotMatch(matcher, s3);
76         assertDoesNotMatch(matcher, s4);
77         assertDoesNotMatch(matcher, null);
78     }
79 
80     @Test public void
comparesTheElementsOfAnArrayOfPrimitiveTypes()81     comparesTheElementsOfAnArrayOfPrimitiveTypes() {
82         int[] i1 = new int[]{1, 2};
83         int[] i2 = new int[]{1, 2};
84         int[] i3 = new int[]{3, 4};
85         int[] i4 = new int[]{1, 2, 3, 4};
86 
87         final Matcher<int[]> matcher = equalTo(i1);
88         assertMatches(matcher, i1);
89         assertMatches(matcher, i2);
90         assertDoesNotMatch(matcher, i3);
91         assertDoesNotMatch(matcher, i4);
92         assertDoesNotMatch(matcher, null);
93     }
94 
95     @Test public void
recursivelyTestsElementsOfArrays()96     recursivelyTestsElementsOfArrays() {
97         int[][] i1 = new int[][]{{1, 2}, {3, 4}};
98         int[][] i2 = new int[][]{{1, 2}, {3, 4}};
99         int[][] i3 = new int[][]{{5, 6}, {7, 8}};
100         int[][] i4 = new int[][]{{1, 2, 3, 4}, {3, 4}};
101 
102         final Matcher<int[][]> matcher = equalTo(i1);
103         assertMatches(matcher, i1);
104         assertMatches(matcher, i2);
105         assertDoesNotMatch(matcher, i3);
106         assertDoesNotMatch(matcher, i4);
107         assertDoesNotMatch(matcher, null);
108     }
109 
110     @Test public void
hasUntypedVariant()111     hasUntypedVariant() {
112         Object original = 10;
113 
114         assertMatches(equalToObject(10), original);
115         assertDoesNotMatch(equalToObject(0), original);
116         assertDoesNotMatch(equalToObject("10"), original);
117         assertDoesNotMatch(equalToObject(10), "10");
118     }
119 
120     @Test public void
includesTheResultOfCallingToStringOnItsArgumentInTheDescription()121     includesTheResultOfCallingToStringOnItsArgumentInTheDescription() {
122         final String argumentDescription = "ARGUMENT DESCRIPTION";
123         Object argument = new Object() {
124             @Override
125             public String toString() {
126                 return argumentDescription;
127             }
128         };
129         assertDescription("<" + argumentDescription + ">", equalTo(argument));
130     }
131 
132     @Test public void
returnsAnObviousDescriptionIfCreatedWithANestedMatcherByMistake()133     returnsAnObviousDescriptionIfCreatedWithANestedMatcherByMistake() {
134         Matcher<? super String> innerMatcher = equalTo("NestedMatcher");
135         assertDescription("<" + innerMatcher.toString() + ">", equalTo(innerMatcher));
136     }
137 
138     @Test public void
returnsGoodDescriptionIfCreatedWithNullReference()139     returnsGoodDescriptionIfCreatedWithNullReference() {
140         assertDescription("null", equalTo(null));
141     }
142 }
143 
144