|
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 |
|
| 22 | +import com.google.common.truth.ExpectFailure.SimpleSubjectBuilderCallback; |
| 23 | +import com.google.errorprone.annotations.CanIgnoreReturnValue; |
20 | 24 | import org.junit.Test;
|
21 | 25 | import org.junit.runner.RunWith;
|
22 | 26 | import org.junit.runners.JUnit4;
|
@@ -129,6 +133,140 @@ public void isAtMost_int() {
|
129 | 133 | assertThat(2L).isAtMost(3);
|
130 | 134 | }
|
131 | 135 |
|
| 136 | + @Test |
| 137 | + public void isWithinOf() { |
| 138 | + assertThat(20000L).isWithin(0L).of(20000L); |
| 139 | + assertThat(20000L).isWithin(1L).of(20000L); |
| 140 | + assertThat(20000L).isWithin(10000L).of(20000L); |
| 141 | + assertThat(20000L).isWithin(10000L).of(30000L); |
| 142 | + assertThat(Long.MIN_VALUE).isWithin(1L).of(Long.MIN_VALUE + 1); |
| 143 | + assertThat(Long.MAX_VALUE).isWithin(1L).of(Long.MAX_VALUE - 1); |
| 144 | + assertThat(Long.MAX_VALUE / 2).isWithin(Long.MAX_VALUE).of(-Long.MAX_VALUE / 2); |
| 145 | + assertThat(-Long.MAX_VALUE / 2).isWithin(Long.MAX_VALUE).of(Long.MAX_VALUE / 2); |
| 146 | + |
| 147 | + assertThatIsWithinFails(20000L, 9999L, 30000L); |
| 148 | + assertThatIsWithinFails(20000L, 10000L, 30001L); |
| 149 | + assertThatIsWithinFails(Long.MIN_VALUE, 0L, Long.MAX_VALUE); |
| 150 | + assertThatIsWithinFails(Long.MAX_VALUE, 0L, Long.MIN_VALUE); |
| 151 | + assertThatIsWithinFails(Long.MIN_VALUE, 1L, Long.MIN_VALUE + 2); |
| 152 | + assertThatIsWithinFails(Long.MAX_VALUE, 1L, Long.MAX_VALUE - 2); |
| 153 | + // Don't fall for rollover |
| 154 | + assertThatIsWithinFails(Long.MIN_VALUE, 1L, Long.MAX_VALUE); |
| 155 | + assertThatIsWithinFails(Long.MAX_VALUE, 1L, Long.MIN_VALUE); |
| 156 | + } |
| 157 | + |
| 158 | + private static void assertThatIsWithinFails(long actual, long tolerance, long expected) { |
| 159 | + ExpectFailure.SimpleSubjectBuilderCallback<LongSubject, Long> callback = |
| 160 | + new ExpectFailure.SimpleSubjectBuilderCallback<LongSubject, Long>() { |
| 161 | + @Override |
| 162 | + public void invokeAssertion(SimpleSubjectBuilder<LongSubject, Long> expect) { |
| 163 | + expect.that(actual).isWithin(tolerance).of(expected); |
| 164 | + } |
| 165 | + }; |
| 166 | + AssertionError failure = expectFailure(callback); |
| 167 | + assertThat(failure) |
| 168 | + .factKeys() |
| 169 | + .containsExactly("expected", "but was", "outside tolerance") |
| 170 | + .inOrder(); |
| 171 | + assertThat(failure).factValue("expected").isEqualTo(Long.toString(expected)); |
| 172 | + assertThat(failure).factValue("but was").isEqualTo(Long.toString(actual)); |
| 173 | + assertThat(failure).factValue("outside tolerance").isEqualTo(Long.toString(tolerance)); |
| 174 | + } |
| 175 | + |
| 176 | + @Test |
| 177 | + public void isNotWithinOf() { |
| 178 | + assertThatIsNotWithinFails(20000L, 0L, 20000L); |
| 179 | + assertThatIsNotWithinFails(20000L, 1L, 20000L); |
| 180 | + assertThatIsNotWithinFails(20000L, 10000L, 20000L); |
| 181 | + assertThatIsNotWithinFails(20000L, 10000L, 30000L); |
| 182 | + assertThatIsNotWithinFails(Long.MIN_VALUE, 1L, Long.MIN_VALUE + 1); |
| 183 | + assertThatIsNotWithinFails(Long.MAX_VALUE, 1L, Long.MAX_VALUE - 1); |
| 184 | + assertThatIsNotWithinFails(Long.MAX_VALUE / 2, Long.MAX_VALUE, -Long.MAX_VALUE / 2); |
| 185 | + assertThatIsNotWithinFails(-Long.MAX_VALUE / 2, Long.MAX_VALUE, Long.MAX_VALUE / 2); |
| 186 | + |
| 187 | + assertThat(20000L).isNotWithin(9999L).of(30000L); |
| 188 | + assertThat(20000L).isNotWithin(10000L).of(30001L); |
| 189 | + assertThat(Long.MIN_VALUE).isNotWithin(0L).of(Long.MAX_VALUE); |
| 190 | + assertThat(Long.MAX_VALUE).isNotWithin(0L).of(Long.MIN_VALUE); |
| 191 | + assertThat(Long.MIN_VALUE).isNotWithin(1L).of(Long.MIN_VALUE + 2); |
| 192 | + assertThat(Long.MAX_VALUE).isNotWithin(1L).of(Long.MAX_VALUE - 2); |
| 193 | + // Don't fall for rollover |
| 194 | + assertThat(Long.MIN_VALUE).isNotWithin(1L).of(Long.MAX_VALUE); |
| 195 | + assertThat(Long.MAX_VALUE).isNotWithin(1L).of(Long.MIN_VALUE); |
| 196 | + } |
| 197 | + |
| 198 | + private static void assertThatIsNotWithinFails(long actual, long tolerance, long expected) { |
| 199 | + ExpectFailure.SimpleSubjectBuilderCallback<LongSubject, Long> callback = |
| 200 | + new ExpectFailure.SimpleSubjectBuilderCallback<LongSubject, Long>() { |
| 201 | + @Override |
| 202 | + public void invokeAssertion(SimpleSubjectBuilder<LongSubject, Long> expect) { |
| 203 | + expect.that(actual).isNotWithin(tolerance).of(expected); |
| 204 | + } |
| 205 | + }; |
| 206 | + AssertionError failure = expectFailure(callback); |
| 207 | + assertThat(failure).factValue("expected not to be").isEqualTo(Long.toString(expected)); |
| 208 | + assertThat(failure).factValue("within tolerance").isEqualTo(Long.toString(tolerance)); |
| 209 | + } |
| 210 | + |
| 211 | + @Test |
| 212 | + public void isWithinIntegers() { |
| 213 | + assertThat(20000L).isWithin(0).of(20000); |
| 214 | + assertThat(20000L).isWithin(1).of(20000); |
| 215 | + assertThat(20000L).isWithin(10000).of(20000); |
| 216 | + assertThat(20000L).isWithin(10000).of(30000); |
| 217 | + |
| 218 | + assertThat(20000L).isNotWithin(0).of(200000); |
| 219 | + assertThat(20000L).isNotWithin(1).of(200000); |
| 220 | + assertThat(20000L).isNotWithin(10000).of(200000); |
| 221 | + assertThat(20000L).isNotWithin(10000).of(300000); |
| 222 | + } |
| 223 | + |
| 224 | + @Test |
| 225 | + public void isWithinNegativeTolerance() { |
| 226 | + isWithinNegativeToleranceThrowsIAE(0L, -10, 5); |
| 227 | + isWithinNegativeToleranceThrowsIAE(0L, -10, 20); |
| 228 | + isNotWithinNegativeToleranceThrowsIAE(0L, -10, 5); |
| 229 | + isNotWithinNegativeToleranceThrowsIAE(0L, -10, 20); |
| 230 | + } |
| 231 | + |
| 232 | + private static void isWithinNegativeToleranceThrowsIAE( |
| 233 | + long actual, long tolerance, long expected) { |
| 234 | + try { |
| 235 | + assertThat(actual).isWithin(tolerance).of(expected); |
| 236 | + fail("Expected IllegalArgumentException to be thrown but wasn't"); |
| 237 | + } catch (IllegalArgumentException iae) { |
| 238 | + assertThat(iae) |
| 239 | + .hasMessageThat() |
| 240 | + .isEqualTo("tolerance (" + tolerance + ") cannot be negative"); |
| 241 | + } |
| 242 | + } |
| 243 | + |
| 244 | + private static void isNotWithinNegativeToleranceThrowsIAE( |
| 245 | + long actual, long tolerance, long expected) { |
| 246 | + try { |
| 247 | + assertThat(actual).isNotWithin(tolerance).of(expected); |
| 248 | + fail("Expected IllegalArgumentException to be thrown but wasn't"); |
| 249 | + } catch (IllegalArgumentException iae) { |
| 250 | + assertThat(iae) |
| 251 | + .hasMessageThat() |
| 252 | + .isEqualTo("tolerance (" + tolerance + ") cannot be negative"); |
| 253 | + } |
| 254 | + } |
| 255 | + |
| 256 | + private static final Subject.Factory<LongSubject, Long> LONG_SUBJECT_FACTORY = |
| 257 | + new Subject.Factory<LongSubject, Long>() { |
| 258 | + @Override |
| 259 | + public LongSubject createSubject(FailureMetadata metadata, Long that) { |
| 260 | + return new LongSubject(metadata, that); |
| 261 | + } |
| 262 | + }; |
| 263 | + |
| 264 | + @CanIgnoreReturnValue |
| 265 | + private static AssertionError expectFailure( |
| 266 | + SimpleSubjectBuilderCallback<LongSubject, Long> callback) { |
| 267 | + return ExpectFailure.expectFailureAbout(LONG_SUBJECT_FACTORY, callback); |
| 268 | + } |
| 269 | + |
132 | 270 | private LongSubject expectFailureWhenTestingThat(Long actual) {
|
133 | 271 | return expectFailure.whenTesting().that(actual);
|
134 | 272 | }
|
|
0 commit comments