|
15 | 15 | */
|
16 | 16 | package com.google.common.truth;
|
17 | 17 |
|
| 18 | +import static com.google.common.truth.ExpectFailure.assertThat; |
18 | 19 | import static com.google.common.truth.Truth.assertThat;
|
| 20 | +import static org.junit.Assert.fail; |
19 | 21 |
|
20 |
| -import com.google.common.collect.ImmutableSet; |
| 22 | +import com.google.common.truth.ExpectFailure.SimpleSubjectBuilderCallback; |
| 23 | +import com.google.errorprone.annotations.CanIgnoreReturnValue; |
21 | 24 | import org.junit.Test;
|
22 | 25 | import org.junit.runner.RunWith;
|
23 | 26 | import org.junit.runners.JUnit4;
|
@@ -116,160 +119,124 @@ public void overflowBetweenIntegerAndLong_shouldBeDifferent_max() {
|
116 | 119 | expectFailureWhenTestingThat(Integer.MAX_VALUE).isEqualTo(Long.MAX_VALUE);
|
117 | 120 | }
|
118 | 121 |
|
119 |
| - @SuppressWarnings("TruthSelfEquals") |
120 | 122 | @Test
|
121 |
| - public void testPrimitivesVsBoxedPrimitivesVsObject_int() { |
122 |
| - int int42 = 42; |
123 |
| - Integer integer42 = new Integer(42); |
124 |
| - Object object42 = (Object) 42; |
125 |
| - |
126 |
| - assertThat(int42).isEqualTo(int42); |
127 |
| - assertThat(integer42).isEqualTo(int42); |
128 |
| - assertThat(object42).isEqualTo(int42); |
129 |
| - |
130 |
| - assertThat(int42).isEqualTo(integer42); |
131 |
| - assertThat(integer42).isEqualTo(integer42); |
132 |
| - assertThat(object42).isEqualTo(integer42); |
133 |
| - |
134 |
| - assertThat(int42).isEqualTo(object42); |
135 |
| - assertThat(integer42).isEqualTo(object42); |
136 |
| - assertThat(object42).isEqualTo(object42); |
137 |
| - } |
138 |
| - |
139 |
| - @SuppressWarnings("TruthSelfEquals") |
140 |
| - @Test |
141 |
| - public void testPrimitivesVsBoxedPrimitivesVsObject_long() { |
142 |
| - long longPrim42 = 42; |
143 |
| - Long long42 = new Long(42); |
144 |
| - Object object42 = (Object) 42L; |
145 |
| - |
146 |
| - assertThat(longPrim42).isEqualTo(longPrim42); |
147 |
| - assertThat(long42).isEqualTo(longPrim42); |
148 |
| - assertThat(object42).isEqualTo(longPrim42); |
149 |
| - |
150 |
| - assertThat(longPrim42).isEqualTo(long42); |
151 |
| - assertThat(long42).isEqualTo(long42); |
152 |
| - assertThat(object42).isEqualTo(long42); |
153 |
| - |
154 |
| - assertThat(longPrim42).isEqualTo(object42); |
155 |
| - assertThat(long42).isEqualTo(object42); |
156 |
| - assertThat(object42).isEqualTo(object42); |
157 |
| - } |
158 |
| - |
159 |
| - @Test |
160 |
| - public void testAllCombinations_pass() { |
161 |
| - assertThat(42).isEqualTo(42L); |
162 |
| - assertThat(42).isEqualTo(new Long(42L)); |
163 |
| - assertThat(new Integer(42)).isEqualTo(42L); |
164 |
| - assertThat(new Integer(42)).isEqualTo(new Long(42L)); |
165 |
| - assertThat(42L).isEqualTo(42); |
166 |
| - assertThat(42L).isEqualTo(new Integer(42)); |
167 |
| - assertThat(new Long(42L)).isEqualTo(42); |
168 |
| - assertThat(new Long(42L)).isEqualTo(new Integer(42)); |
169 |
| - |
170 |
| - assertThat(42).isEqualTo(42); |
171 |
| - assertThat(42).isEqualTo(new Integer(42)); |
172 |
| - assertThat(new Integer(42)).isEqualTo(42); |
173 |
| - assertThat(new Integer(42)).isEqualTo(new Integer(42)); |
174 |
| - assertThat(42L).isEqualTo(42L); |
175 |
| - assertThat(42L).isEqualTo(new Long(42L)); |
176 |
| - assertThat(new Long(42L)).isEqualTo(42L); |
177 |
| - assertThat(new Long(42L)).isEqualTo(new Long(42L)); |
178 |
| - } |
179 |
| - |
180 |
| - @Test |
181 |
| - public void testNumericTypeWithSameValue_shouldBeEqual_int_long() { |
182 |
| - expectFailureWhenTestingThat(42).isNotEqualTo(42L); |
183 |
| - } |
184 |
| - |
185 |
| - @Test |
186 |
| - public void testNumericTypeWithSameValue_shouldBeEqual_int_int() { |
187 |
| - expectFailureWhenTestingThat(42).isNotEqualTo(42); |
188 |
| - } |
189 |
| - |
190 |
| - @Test |
191 |
| - public void testNumericPrimitiveTypes_isNotEqual_shouldFail_intToChar() { |
192 |
| - expectFailureWhenTestingThat(42).isNotEqualTo((char) 42); |
193 |
| - // 42 in ASCII is '*' |
194 |
| - assertFailureValue("expected not to be", "*"); |
195 |
| - assertFailureValue("but was; string representation of actual value", "42"); |
| 123 | + public void isWithinOf() { |
| 124 | + assertThat(20000).isWithin(0).of(20000); |
| 125 | + assertThat(20000).isWithin(1).of(20000); |
| 126 | + assertThat(20000).isWithin(10000).of(20000); |
| 127 | + assertThat(20000).isWithin(10000).of(30000); |
| 128 | + assertThat(Integer.MIN_VALUE).isWithin(1).of(Integer.MIN_VALUE + 1); |
| 129 | + assertThat(Integer.MAX_VALUE).isWithin(1).of(Integer.MAX_VALUE - 1); |
| 130 | + assertThat(Integer.MAX_VALUE / 2).isWithin(Integer.MAX_VALUE).of(-Integer.MAX_VALUE / 2); |
| 131 | + assertThat(-Integer.MAX_VALUE / 2).isWithin(Integer.MAX_VALUE).of(Integer.MAX_VALUE / 2); |
| 132 | + |
| 133 | + assertThatIsWithinFails(20000, 9999, 30000); |
| 134 | + assertThatIsWithinFails(20000, 10000, 30001); |
| 135 | + assertThatIsWithinFails(Integer.MIN_VALUE, 0, Integer.MAX_VALUE); |
| 136 | + assertThatIsWithinFails(Integer.MAX_VALUE, 0, Integer.MIN_VALUE); |
| 137 | + assertThatIsWithinFails(Integer.MIN_VALUE, 1, Integer.MIN_VALUE + 2); |
| 138 | + assertThatIsWithinFails(Integer.MAX_VALUE, 1, Integer.MAX_VALUE - 2); |
| 139 | + // Don't fall for rollover |
| 140 | + assertThatIsWithinFails(Integer.MIN_VALUE, 1, Integer.MAX_VALUE); |
| 141 | + assertThatIsWithinFails(Integer.MAX_VALUE, 1, Integer.MIN_VALUE); |
| 142 | + } |
| 143 | + |
| 144 | + private static void assertThatIsWithinFails(int actual, int tolerance, int expected) { |
| 145 | + ExpectFailure.SimpleSubjectBuilderCallback<IntegerSubject, Integer> callback = |
| 146 | + new ExpectFailure.SimpleSubjectBuilderCallback<IntegerSubject, Integer>() { |
| 147 | + @Override |
| 148 | + public void invokeAssertion(SimpleSubjectBuilder<IntegerSubject, Integer> expect) { |
| 149 | + expect.that(actual).isWithin(tolerance).of(expected); |
| 150 | + } |
| 151 | + }; |
| 152 | + AssertionError failure = expectFailure(callback); |
| 153 | + assertThat(failure) |
| 154 | + .factKeys() |
| 155 | + .containsExactly("expected", "but was", "outside tolerance") |
| 156 | + .inOrder(); |
| 157 | + assertThat(failure).factValue("expected").isEqualTo(Integer.toString(expected)); |
| 158 | + assertThat(failure).factValue("but was").isEqualTo(Integer.toString(actual)); |
| 159 | + assertThat(failure).factValue("outside tolerance").isEqualTo(Integer.toString(tolerance)); |
| 160 | + } |
| 161 | + |
| 162 | + @Test |
| 163 | + public void isNotWithinOf() { |
| 164 | + assertThatIsNotWithinFails(20000, 0, 20000); |
| 165 | + assertThatIsNotWithinFails(20000, 1, 20000); |
| 166 | + assertThatIsNotWithinFails(20000, 10000, 20000); |
| 167 | + assertThatIsNotWithinFails(20000, 10000, 30000); |
| 168 | + assertThatIsNotWithinFails(Integer.MIN_VALUE, 1, Integer.MIN_VALUE + 1); |
| 169 | + assertThatIsNotWithinFails(Integer.MAX_VALUE, 1, Integer.MAX_VALUE - 1); |
| 170 | + assertThatIsNotWithinFails(Integer.MAX_VALUE / 2, Integer.MAX_VALUE, -Integer.MAX_VALUE / 2); |
| 171 | + assertThatIsNotWithinFails(-Integer.MAX_VALUE / 2, Integer.MAX_VALUE, Integer.MAX_VALUE / 2); |
| 172 | + |
| 173 | + assertThat(20000).isNotWithin(9999).of(30000); |
| 174 | + assertThat(20000).isNotWithin(10000).of(30001); |
| 175 | + assertThat(Integer.MIN_VALUE).isNotWithin(0).of(Integer.MAX_VALUE); |
| 176 | + assertThat(Integer.MAX_VALUE).isNotWithin(0).of(Integer.MIN_VALUE); |
| 177 | + assertThat(Integer.MIN_VALUE).isNotWithin(1).of(Integer.MIN_VALUE + 2); |
| 178 | + assertThat(Integer.MAX_VALUE).isNotWithin(1).of(Integer.MAX_VALUE - 2); |
| 179 | + // Don't fall for rollover |
| 180 | + assertThat(Integer.MIN_VALUE).isNotWithin(1).of(Integer.MAX_VALUE); |
| 181 | + assertThat(Integer.MAX_VALUE).isNotWithin(1).of(Integer.MIN_VALUE); |
| 182 | + } |
| 183 | + |
| 184 | + private static void assertThatIsNotWithinFails(int actual, int tolerance, int expected) { |
| 185 | + ExpectFailure.SimpleSubjectBuilderCallback<IntegerSubject, Integer> callback = |
| 186 | + new ExpectFailure.SimpleSubjectBuilderCallback<IntegerSubject, Integer>() { |
| 187 | + @Override |
| 188 | + public void invokeAssertion(SimpleSubjectBuilder<IntegerSubject, Integer> expect) { |
| 189 | + expect.that(actual).isNotWithin(tolerance).of(expected); |
| 190 | + } |
| 191 | + }; |
| 192 | + AssertionError failure = expectFailure(callback); |
| 193 | + assertThat(failure).factValue("expected not to be").isEqualTo(Integer.toString(expected)); |
| 194 | + assertThat(failure).factValue("within tolerance").isEqualTo(Integer.toString(tolerance)); |
| 195 | + } |
| 196 | + |
| 197 | + @Test |
| 198 | + public void isWithinNegativeTolerance() { |
| 199 | + isWithinNegativeToleranceThrowsIAE(0, -10, 5); |
| 200 | + isWithinNegativeToleranceThrowsIAE(0, -10, 20); |
| 201 | + isNotWithinNegativeToleranceThrowsIAE(0, -10, 5); |
| 202 | + isNotWithinNegativeToleranceThrowsIAE(0, -10, 20); |
| 203 | + } |
| 204 | + |
| 205 | + private static void isWithinNegativeToleranceThrowsIAE(int actual, int tolerance, int expected) { |
| 206 | + try { |
| 207 | + assertThat(actual).isWithin(tolerance).of(expected); |
| 208 | + fail("Expected IllegalArgumentException to be thrown but wasn't"); |
| 209 | + } catch (IllegalArgumentException iae) { |
| 210 | + assertThat(iae) |
| 211 | + .hasMessageThat() |
| 212 | + .isEqualTo("tolerance (" + tolerance + ") cannot be negative"); |
| 213 | + } |
196 | 214 | }
|
197 | 215 |
|
198 |
| - @Test |
199 |
| - public void testNumericPrimitiveTypes_isNotEqual_shouldFail_charToInt() { |
200 |
| - // Uses Object overload rather than Integer. |
201 |
| - expectFailure.whenTesting().that((char) 42).isNotEqualTo(42); |
202 |
| - // 42 in ASCII is '*' |
203 |
| - assertFailureValue("expected not to be", "42"); |
204 |
| - assertFailureValue("but was; string representation of actual value", "*"); |
| 216 | + private static void isNotWithinNegativeToleranceThrowsIAE( |
| 217 | + int actual, int tolerance, int expected) { |
| 218 | + try { |
| 219 | + assertThat(actual).isNotWithin(tolerance).of(expected); |
| 220 | + fail("Expected IllegalArgumentException to be thrown but wasn't"); |
| 221 | + } catch (IllegalArgumentException iae) { |
| 222 | + assertThat(iae) |
| 223 | + .hasMessageThat() |
| 224 | + .isEqualTo("tolerance (" + tolerance + ") cannot be negative"); |
| 225 | + } |
205 | 226 | }
|
206 | 227 |
|
207 |
| - private static final Subject.Factory<Subject, Object> DEFAULT_SUBJECT_FACTORY = |
208 |
| - new Subject.Factory<Subject, Object>() { |
| 228 | + private static final Subject.Factory<IntegerSubject, Integer> INTEGER_SUBJECT_FACTORY = |
| 229 | + new Subject.Factory<IntegerSubject, Integer>() { |
209 | 230 | @Override
|
210 |
| - public Subject createSubject(FailureMetadata metadata, Object that) { |
211 |
| - return new Subject(metadata, that); |
| 231 | + public IntegerSubject createSubject(FailureMetadata metadata, Integer that) { |
| 232 | + return new IntegerSubject(metadata, that); |
212 | 233 | }
|
213 | 234 | };
|
214 | 235 |
|
215 |
| - private static void expectFailure( |
216 |
| - ExpectFailure.SimpleSubjectBuilderCallback<Subject, Object> callback) { |
217 |
| - AssertionError unused = ExpectFailure.expectFailureAbout(DEFAULT_SUBJECT_FACTORY, callback); |
218 |
| - } |
219 |
| - |
220 |
| - @Test |
221 |
| - public void testNumericPrimitiveTypes() { |
222 |
| - byte byte42 = (byte) 42; |
223 |
| - short short42 = (short) 42; |
224 |
| - char char42 = (char) 42; |
225 |
| - int int42 = 42; |
226 |
| - long long42 = (long) 42; |
227 |
| - |
228 |
| - ImmutableSet<Object> fortyTwos = |
229 |
| - ImmutableSet.<Object>of(byte42, short42, char42, int42, long42); |
230 |
| - for (Object actual : fortyTwos) { |
231 |
| - for (Object expected : fortyTwos) { |
232 |
| - assertThat(actual).isEqualTo(expected); |
233 |
| - } |
234 |
| - } |
235 |
| - |
236 |
| - ImmutableSet<Object> fortyTwosNoChar = ImmutableSet.<Object>of(byte42, short42, int42, long42); |
237 |
| - for (Object actual : fortyTwosNoChar) { |
238 |
| - for (Object expected : fortyTwosNoChar) { |
239 |
| - ExpectFailure.SimpleSubjectBuilderCallback<Subject, Object> actualFirst = |
240 |
| - new ExpectFailure.SimpleSubjectBuilderCallback<Subject, Object>() { |
241 |
| - @Override |
242 |
| - public void invokeAssertion(SimpleSubjectBuilder<Subject, Object> expect) { |
243 |
| - expect.that(actual).isNotEqualTo(expected); |
244 |
| - } |
245 |
| - }; |
246 |
| - ExpectFailure.SimpleSubjectBuilderCallback<Subject, Object> expectedFirst = |
247 |
| - new ExpectFailure.SimpleSubjectBuilderCallback<Subject, Object>() { |
248 |
| - @Override |
249 |
| - public void invokeAssertion(SimpleSubjectBuilder<Subject, Object> expect) { |
250 |
| - expect.that(expected).isNotEqualTo(actual); |
251 |
| - } |
252 |
| - }; |
253 |
| - expectFailure(actualFirst); |
254 |
| - expectFailure(expectedFirst); |
255 |
| - } |
256 |
| - } |
257 |
| - |
258 |
| - byte byte41 = (byte) 41; |
259 |
| - short short41 = (short) 41; |
260 |
| - char char41 = (char) 41; |
261 |
| - int int41 = 41; |
262 |
| - long long41 = (long) 41; |
263 |
| - |
264 |
| - ImmutableSet<Object> fortyOnes = |
265 |
| - ImmutableSet.<Object>of(byte41, short41, char41, int41, long41); |
266 |
| - |
267 |
| - for (Object first : fortyTwos) { |
268 |
| - for (Object second : fortyOnes) { |
269 |
| - assertThat(first).isNotEqualTo(second); |
270 |
| - assertThat(second).isNotEqualTo(first); |
271 |
| - } |
272 |
| - } |
| 236 | + @CanIgnoreReturnValue |
| 237 | + private static AssertionError expectFailure( |
| 238 | + SimpleSubjectBuilderCallback<IntegerSubject, Integer> callback) { |
| 239 | + return ExpectFailure.expectFailureAbout(INTEGER_SUBJECT_FACTORY, callback); |
273 | 240 | }
|
274 | 241 |
|
275 | 242 | private IntegerSubject expectFailureWhenTestingThat(Integer actual) {
|
|
0 commit comments