/
Subject.java
1198 lines (1100 loc) · 48.5 KB
/
Subject.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c) 2011 Google, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.common.truth;
import static com.google.common.base.CaseFormat.LOWER_CAMEL;
import static com.google.common.base.CaseFormat.UPPER_CAMEL;
import static com.google.common.base.CharMatcher.whitespace;
import static com.google.common.base.MoreObjects.firstNonNull;
import static com.google.common.base.Strings.lenientFormat;
import static com.google.common.truth.Fact.fact;
import static com.google.common.truth.Fact.simpleFact;
import static com.google.common.truth.Platform.doubleToString;
import static com.google.common.truth.Platform.floatToString;
import static com.google.common.truth.Subject.EqualityCheck.SAME_INSTANCE;
import static com.google.common.truth.SubjectUtils.accumulate;
import static com.google.common.truth.SubjectUtils.append;
import static com.google.common.truth.SubjectUtils.concat;
import static com.google.common.truth.SubjectUtils.sandwich;
import static java.util.Arrays.asList;
import com.google.common.base.Function;
import com.google.common.base.Objects;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Iterables;
import com.google.common.collect.Lists;
import com.google.common.primitives.Booleans;
import com.google.common.primitives.Bytes;
import com.google.common.primitives.Chars;
import com.google.common.primitives.Ints;
import com.google.common.primitives.Longs;
import com.google.common.primitives.Shorts;
import com.google.common.truth.FailureMetadata.OldAndNewValuesAreSimilar;
import com.google.errorprone.annotations.DoNotCall;
import com.google.errorprone.annotations.ForOverride;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.checkerframework.checker.nullness.qual.Nullable;
/**
* An object that lets you perform checks on the value under test. For example, {@code Subject}
* contains {@link #isEqualTo(Object)} and {@link #isInstanceOf(Class)}, and {@link StringSubject}
* contains {@link StringSubject#startsWith startsWith(String)}.
*
* <p>To create a {@code Subject} instance, most users will call an {@link Truth#assertThat
* assertThat} method. For information about other ways to create an instance, see <a
* href="https://truth.dev/faq#full-chain">this FAQ entry</a>.
*
* <h3>For people extending Truth</h3>
*
* <p>For information about writing a custom {@link Subject}, see <a
* href="https://truth.dev/extension">our doc on extensions</a>.
*
* @author David Saff
* @author Christian Gruber
*/
public class Subject {
/**
* In a fluent assertion chain, the argument to the common overload of {@link
* StandardSubjectBuilder#about(Subject.Factory) about}, the method that specifies what kind of
* {@link Subject} to create.
*
* <p>For more information about the fluent chain, see <a
* href="https://truth.dev/faq#full-chain">this FAQ entry</a>.
*
* <h3>For people extending Truth</h3>
*
* <p>When you write a custom subject, see <a href="https://truth.dev/extension">our doc on
* extensions</a>. It explains where {@code Subject.Factory} fits into the process.
*/
public interface Factory<SubjectT extends Subject, ActualT> {
/** Creates a new {@link Subject}. */
SubjectT createSubject(FailureMetadata metadata, ActualT actual);
}
private static final FailureStrategy IGNORE_STRATEGY =
new FailureStrategy() {
@Override
public void fail(AssertionError failure) {}
};
private final FailureMetadata metadata;
private final Object actual;
private String customName = null;
private final @Nullable String typeDescriptionOverride;
/**
* Constructor for use by subclasses. If you want to create an instance of this class itself, call
* {@link Subject#check(String, Object...) check(...)}{@code .that(actual)}.
*/
protected Subject(FailureMetadata metadata, @Nullable Object actual) {
this(metadata, actual, /*typeDescriptionOverride=*/ null);
}
/**
* Special constructor that lets subclasses provide a description of the type they're testing. For
* example, {@link ThrowableSubject} passes the description "throwable." Normally, Truth is able
* to infer this name from the class name. However, if we lack runtime type information (notably,
* under j2cl with class metadata off), we might not have access to the original class name.
*
* <p>We don't expect to make this a public API: Class names are nearly always available. It's
* just that we want to be able to run Truth's own tests run with class metadata off, and it's
* easier to tweak the subjects to know their own names rather than generalize the tests to accept
* obfuscated names.
*/
Subject(
FailureMetadata metadata, @Nullable Object actual, @Nullable String typeDescriptionOverride) {
this.metadata = metadata.updateForSubject(this);
this.actual = actual;
this.typeDescriptionOverride = typeDescriptionOverride;
}
/** Fails if the subject is not null. */
public void isNull() {
standardIsEqualTo(null);
}
/** Fails if the subject is null. */
public void isNotNull() {
standardIsNotEqualTo(null);
}
/**
* Fails if the subject is not equal to the given object. For the purposes of this comparison, two
* objects are equal if any of the following is true:
*
* <ul>
* <li>they are equal according to {@link Objects#equal}
* <li>they are arrays and are considered equal by the appropriate {@link Arrays#equals}
* overload
* <li>they are boxed integer types ({@code Byte}, {@code Short}, {@code Character}, {@code
* Integer}, or {@code Long}) and they are numerically equal when converted to {@code Long}.
* <li>the actual value is a boxed floating-point type ({@code Double} or {@code Float}), the
* expected value is an {@code Integer}, and the two are numerically equal when converted to
* {@code Double}. (This allows {@code assertThat(someDouble).isEqualTo(0)} to pass.)
* </ul>
*
* <p><b>Note:</b> This method does not test the {@link Object#equals} implementation itself; it
* <i>assumes</i> that method is functioning correctly according to its contract. Testing an
* {@code equals} implementation requires a utility such as <a
* href="https://mvnrepository.com/artifact/com.google.guava/guava-testlib">guava-testlib</a>'s <a
* href="https://static.javadoc.io/com.google.guava/guava-testlib/23.0/com/google/common/testing/EqualsTester.html">EqualsTester</a>.
*
* <p>In some cases, this method might not even call {@code equals}. It may instead perform other
* tests that will return the same result as long as {@code equals} is implemented according to
* the contract for its type.
*/
/*
* TODO(cpovirk): Possibly ban overriding isEqualTo+isNotEqualTo in favor of a
* compareForEquality(Object, Object) method. That way, people would need to override only one
* method, they would get a ComparisonFailure and other message niceties, and they'd have less to
* test.
*/
public void isEqualTo(@Nullable Object expected) {
standardIsEqualTo(expected);
}
private void standardIsEqualTo(@Nullable Object expected) {
ComparisonResult difference = compareForEquality(expected);
if (!difference.valuesAreEqual()) {
failEqualityCheck(EqualityCheck.EQUAL, expected, difference);
}
}
/**
* Fails if the subject is equal to the given object. The meaning of equality is the same as for
* the {@link #isEqualTo} method.
*/
public void isNotEqualTo(@Nullable Object unexpected) {
standardIsNotEqualTo(unexpected);
}
private void standardIsNotEqualTo(@Nullable Object unexpected) {
ComparisonResult difference = compareForEquality(unexpected);
if (difference.valuesAreEqual()) {
String unexpectedAsString = formatActualOrExpected(unexpected);
if (actualCustomStringRepresentation().equals(unexpectedAsString)) {
failWithoutActual(fact("expected not to be", unexpectedAsString));
} else {
failWithoutActual(
fact("expected not to be", unexpectedAsString),
fact(
"but was; string representation of actual value",
actualCustomStringRepresentation()));
}
}
}
/**
* Returns whether {@code actual} equals {@code expected} differ and, in some cases, a description
* of how they differ.
*
* <p>The equality check follows the rules described on {@link #isEqualTo}.
*/
private ComparisonResult compareForEquality(@Nullable Object expected) {
if (actual == null && expected == null) {
return ComparisonResult.equal();
} else if (actual == null || expected == null) {
return ComparisonResult.differentNoDescription();
} else if (actual instanceof byte[] && expected instanceof byte[]) {
/*
* For a special error message and to use faster Arrays.equals to avoid at least one timeout.
*
* TODO(cpovirk): For performance, use Arrays.equals for other array types (here and/or in
* checkArrayEqualsRecursive)? Exception: double[] and float[], whose GWT implementations I
* think may have both false positives and false negatives (so we can't even use Arrays.equals
* as a fast path for them, nor deepEquals for an Object[] that might contain them). We would
* still fall back to the slower checkArrayEqualsRecursive to produce a nicer failure message
* -- but naturally only for tests that are about to fail, when performance matters less.
*/
return checkByteArrayEquals((byte[]) expected, (byte[]) actual);
} else if (actual.getClass().isArray() && expected.getClass().isArray()) {
return checkArrayEqualsRecursive(expected, actual, "");
} else if (isIntegralBoxedPrimitive(actual) && isIntegralBoxedPrimitive(expected)) {
return ComparisonResult.fromEqualsResult(integralValue(actual) == integralValue(expected));
} else if (actual instanceof Double && expected instanceof Double) {
return ComparisonResult.fromEqualsResult(
Double.compare((Double) actual, (Double) expected) == 0);
} else if (actual instanceof Float && expected instanceof Float) {
return ComparisonResult.fromEqualsResult(
Float.compare((Float) actual, (Float) expected) == 0);
} else if (actual instanceof Double && expected instanceof Integer) {
return ComparisonResult.fromEqualsResult(
Double.compare((Double) actual, (Integer) expected) == 0);
} else if (actual instanceof Float && expected instanceof Integer) {
return ComparisonResult.fromEqualsResult(
Double.compare((Float) actual, (Integer) expected) == 0);
} else {
return ComparisonResult.fromEqualsResult(actual == expected || actual.equals(expected));
}
}
private static boolean isIntegralBoxedPrimitive(@Nullable Object o) {
return o instanceof Byte
|| o instanceof Short
|| o instanceof Character
|| o instanceof Integer
|| o instanceof Long;
}
private static long integralValue(Object o) {
if (o instanceof Character) {
return (long) ((Character) o).charValue();
} else if (o instanceof Number) {
return ((Number) o).longValue();
} else {
throw new AssertionError(o + " must be either a Character or a Number.");
}
}
/** Fails if the subject is not the same instance as the given object. */
public final void isSameInstanceAs(@Nullable Object expected) {
if (actual != expected) {
failEqualityCheck(
SAME_INSTANCE,
expected,
/*
* Pass through *whether* the values are equal so that failEqualityCheck() can print that
* information. But remove the description of the difference, which is always about
* content, since people calling isSameInstanceAs() are explicitly not interested in
* content, only object identity.
*/
compareForEquality(expected).withoutDescription());
}
}
/** Fails if the subject is the same instance as the given object. */
public final void isNotSameInstanceAs(@Nullable Object unexpected) {
if (actual == unexpected) {
/*
* We use actualCustomStringRepresentation() because it might be overridden to be better than
* actual.toString()/unexpected.toString().
*/
failWithoutActual(
fact("expected not to be specific instance", actualCustomStringRepresentation()));
}
}
/** Fails if the subject is not an instance of the given class. */
public void isInstanceOf(Class<?> clazz) {
if (clazz == null) {
throw new NullPointerException("clazz");
}
if (actual == null) {
failWithActual("expected instance of", clazz.getName());
return;
}
if (!Platform.isInstanceOfType(actual, clazz)) {
if (classMetadataUnsupported()) {
throw new UnsupportedOperationException(
actualCustomStringRepresentation()
+ ", an instance of "
+ actual.getClass().getName()
+ ", may or may not be an instance of "
+ clazz.getName()
+ ". Under -XdisableClassMetadata, we do not have enough information to tell.");
}
failWithoutActual(
fact("expected instance of", clazz.getName()),
fact("but was instance of", actual.getClass().getName()),
fact("with value", actualCustomStringRepresentation()));
}
}
/** Fails if the subject is an instance of the given class. */
public void isNotInstanceOf(Class<?> clazz) {
if (clazz == null) {
throw new NullPointerException("clazz");
}
if (classMetadataUnsupported()) {
throw new UnsupportedOperationException(
"isNotInstanceOf is not supported under -XdisableClassMetadata");
}
if (actual == null) {
return; // null is not an instance of clazz.
}
if (Platform.isInstanceOfType(actual, clazz)) {
failWithActual("expected not to be an instance of", clazz.getName());
/*
* TODO(cpovirk): Consider including actual.getClass() if it's not clazz itself but only a
* subtype.
*/
}
}
/** Fails unless the subject is equal to any element in the given iterable. */
public void isIn(Iterable<?> iterable) {
if (!Iterables.contains(iterable, actual)) {
failWithActual("expected any of", iterable);
}
}
/** Fails unless the subject is equal to any of the given elements. */
public void isAnyOf(
@Nullable Object first, @Nullable Object second, @Nullable Object @Nullable ... rest) {
isIn(accumulate(first, second, rest));
}
/** Fails if the subject is equal to any element in the given iterable. */
public void isNotIn(Iterable<?> iterable) {
if (Iterables.contains(iterable, actual)) {
failWithActual("expected not to be any of", iterable);
}
}
/** Fails if the subject is equal to any of the given elements. */
public void isNoneOf(
@Nullable Object first, @Nullable Object second, @Nullable Object @Nullable ... rest) {
isNotIn(accumulate(first, second, rest));
}
/** Returns the actual value under test. */
final Object actual() {
return actual;
}
/**
* Supplies the direct string representation of the actual value to other methods which may prefix
* or otherwise position it in an error message. This should only be overridden to provide an
* improved string representation of the value under test, as it would appear in any given error
* message, and should not be used for additional prefixing.
*
* <p>Subjects should override this with care.
*
* <p>By default, this returns {@code String.ValueOf(getActualValue())}.
*/
/*
* TODO(cpovirk): Consider whether this API pulls its weight. If users want to format the actual
* value, maybe they should do so themselves? Of course, they won't have a chance to use a custom
* format for inherited implementations like isEqualTo(). But if they want to format the actual
* value specially, then it seems likely that they'll want to format the expected value specially,
* too. And that applies just as well to APIs like isIn(). Maybe we'll want an API that supports
* formatting those values, too (like formatActualOrExpected below)? See also the related
* b/70930431. But note that we are likely to use this from FailureMetadata, at least in the short
* term, for better or for worse.
*/
@ForOverride
protected String actualCustomStringRepresentation() {
return formatActualOrExpected(actual);
}
final String actualCustomStringRepresentationForPackageMembersToCall() {
return actualCustomStringRepresentation();
}
private String formatActualOrExpected(@Nullable Object o) {
if (o instanceof byte[]) {
return base16((byte[]) o);
} else if (o != null && o.getClass().isArray()) {
String wrapped = Iterables.toString(stringableIterable(new Object[] {o}));
return wrapped.substring(1, wrapped.length() - 1);
} else if (o instanceof Double) {
return doubleToString((Double) o);
} else if (o instanceof Float) {
return floatToString((Float) o);
} else {
return String.valueOf(o);
}
}
// We could add a dep on com.google.common.io, but that seems overkill for base16 encoding
private static String base16(byte[] bytes) {
StringBuilder sb = new StringBuilder(2 * bytes.length);
for (byte b : bytes) {
sb.append(hexDigits[(b >> 4) & 0xf]).append(hexDigits[b & 0xf]);
}
return sb.toString();
}
private static final char[] hexDigits = "0123456789ABCDEF".toCharArray();
private static Iterable<?> stringableIterable(Object[] array) {
return Iterables.transform(asList(array), STRINGIFY);
}
private static final Function<Object, Object> STRINGIFY =
new Function<Object, Object>() {
@Override
public Object apply(@Nullable Object input) {
if (input != null && input.getClass().isArray()) {
Iterable<?> iterable;
if (input.getClass() == boolean[].class) {
iterable = Booleans.asList((boolean[]) input);
} else if (input.getClass() == int[].class) {
iterable = Ints.asList((int[]) input);
} else if (input.getClass() == long[].class) {
iterable = Longs.asList((long[]) input);
} else if (input.getClass() == short[].class) {
iterable = Shorts.asList((short[]) input);
} else if (input.getClass() == byte[].class) {
iterable = Bytes.asList((byte[]) input);
} else if (input.getClass() == double[].class) {
iterable = doubleArrayAsString((double[]) input);
} else if (input.getClass() == float[].class) {
iterable = floatArrayAsString((float[]) input);
} else if (input.getClass() == char[].class) {
iterable = Chars.asList((char[]) input);
} else {
iterable = Arrays.asList((Object[]) input);
}
return Iterables.transform(iterable, STRINGIFY);
}
return input;
}
};
/**
* The result of comparing two objects for equality. This includes both the "equal"/"not-equal"
* bit and, in the case of "not equal," optional facts describing the difference.
*/
private static final class ComparisonResult {
/**
* If {@code equal} is true, returns an equal result; if false, a non-equal result with no
* description.
*/
static ComparisonResult fromEqualsResult(boolean equal) {
return equal ? EQUAL : DIFFERENT_NO_DESCRIPTION;
}
/** Returns a non-equal result with the given description. */
static ComparisonResult differentWithDescription(Fact... facts) {
return new ComparisonResult(ImmutableList.copyOf(facts));
}
/** Returns an equal result. */
static ComparisonResult equal() {
return EQUAL;
}
/** Returns a non-equal result with no description. */
static ComparisonResult differentNoDescription() {
return DIFFERENT_NO_DESCRIPTION;
}
private static final ComparisonResult EQUAL = new ComparisonResult(null);
private static final ComparisonResult DIFFERENT_NO_DESCRIPTION =
new ComparisonResult(ImmutableList.<Fact>of());
private final @Nullable ImmutableList<Fact> facts;
private ComparisonResult(ImmutableList<Fact> facts) {
this.facts = facts;
}
boolean valuesAreEqual() {
return facts == null;
}
ImmutableList<Fact> factsOrEmpty() {
return firstNonNull(facts, ImmutableList.<Fact>of());
}
/** Returns an instance with the same "equal"/"not-equal" bit but with no description. */
ComparisonResult withoutDescription() {
return fromEqualsResult(valuesAreEqual());
}
}
/**
* Returns null if the arrays are equal. If not equal, returns a string comparing the two arrays,
* displaying them in the style "[1, 2, 3]" to supplement the main failure message, which uses the
* style "010203."
*/
private static ComparisonResult checkByteArrayEquals(byte[] expected, byte[] actual) {
if (Arrays.equals(expected, actual)) {
return ComparisonResult.equal();
}
return ComparisonResult.differentWithDescription(
fact("expected", Arrays.toString(expected)), fact("but was", Arrays.toString(actual)));
}
/**
* Returns null if the arrays are equal, recursively. If not equal, returns the string of the
* index at which they're different.
*/
/*
* TODO(cpovirk): Decide whether it's worthwhile to go to this trouble to display the index at
* which the arrays differ. If we were to stop doing that, we could mostly delegate to
* Arrays.equals() and our float/double arrayEquals methods. (We'd use deepEquals, but it doesn't
* have our special double/float handling for GWT.)
*/
private static ComparisonResult checkArrayEqualsRecursive(
Object expectedArray, Object actualArray, String lastIndex) {
if (expectedArray == actualArray) {
return ComparisonResult.equal();
}
String expectedType = arrayType(expectedArray);
String actualType = arrayType(actualArray);
if (!expectedType.equals(actualType)) {
Fact indexFact =
lastIndex.isEmpty() ? simpleFact("wrong type") : fact("wrong type for index", lastIndex);
return ComparisonResult.differentWithDescription(
indexFact, fact("expected", expectedType), fact("but was", actualType));
}
int actualLength = Array.getLength(actualArray);
int expectedLength = Array.getLength(expectedArray);
if (expectedLength != actualLength) {
Fact indexFact =
lastIndex.isEmpty()
? simpleFact("wrong length")
: fact("wrong length for index", lastIndex);
return ComparisonResult.differentWithDescription(
indexFact, fact("expected", expectedLength), fact("but was", actualLength));
}
for (int i = 0; i < actualLength; i++) {
String index = lastIndex + "[" + i + "]";
Object expected = Array.get(expectedArray, i);
Object actual = Array.get(actualArray, i);
if (actual != null
&& actual.getClass().isArray()
&& expected != null
&& expected.getClass().isArray()) {
ComparisonResult result = checkArrayEqualsRecursive(expected, actual, index);
if (!result.valuesAreEqual()) {
return result;
}
} else if (!gwtSafeObjectEquals(actual, expected)) {
return ComparisonResult.differentWithDescription(fact("differs at index", index));
}
}
return ComparisonResult.equal();
}
private static String arrayType(Object array) {
if (array.getClass() == boolean[].class) {
return "boolean[]";
} else if (array.getClass() == int[].class) {
return "int[]";
} else if (array.getClass() == long[].class) {
return "long[]";
} else if (array.getClass() == short[].class) {
return "short[]";
} else if (array.getClass() == byte[].class) {
return "byte[]";
} else if (array.getClass() == double[].class) {
return "double[]";
} else if (array.getClass() == float[].class) {
return "float[]";
} else if (array.getClass() == char[].class) {
return "char[]";
} else {
return "Object[]";
}
}
private static boolean gwtSafeObjectEquals(Object actual, Object expected) {
if (actual instanceof Double && expected instanceof Double) {
return Double.doubleToLongBits((Double) actual) == Double.doubleToLongBits((Double) expected);
} else if (actual instanceof Float && expected instanceof Float) {
return Float.floatToIntBits((Float) actual) == Float.floatToIntBits((Float) expected);
} else {
return Objects.equal(actual, expected);
}
}
private static List<String> doubleArrayAsString(double[] items) {
List<String> itemAsStrings = new ArrayList<>(items.length);
for (double item : items) {
itemAsStrings.add(doubleToString(item));
}
return itemAsStrings;
}
private static List<String> floatArrayAsString(float[] items) {
List<String> itemAsStrings = new ArrayList<>(items.length);
for (float item : items) {
itemAsStrings.add(floatToString(item));
}
return itemAsStrings;
}
/**
* Returns a builder for creating a derived subject but without providing information about how
* the derived subject will relate to the current subject. In most cases, you should provide such
* information by using {@linkplain #check(String, Object...) the other overload}.
*
* @deprecated Use {@linkplain #check(String, Object...) the other overload}, which requires you
* to supply more information to include in any failure messages.
*/
@Deprecated
final StandardSubjectBuilder check() {
return new StandardSubjectBuilder(metadata.updateForCheckCall());
}
/**
* Returns a builder for creating a derived subject.
*
* <p>Derived subjects retain the {@link FailureStrategy} and {@linkplain
* StandardSubjectBuilder#withMessage messages} of the current subject, and in some cases, they
* automatically supplement their failure message with information about the original subject.
*
* <p>For example, {@link ThrowableSubject#hasMessageThat}, which returns a {@link StringSubject},
* is implemented with {@code check("getMessage()").that(actual.getMessage())}.
*
* <p>The arguments to {@code check} describe how the new subject was derived from the old,
* formatted like a chained method call. This allows Truth to include that information in its
* failure messages. For example, {@code assertThat(caught).hasCauseThat().hasMessageThat()} will
* produce a failure message that includes the string "throwable.getCause().getMessage()," thanks
* to internal {@code check} calls that supplied "getCause()" and "getMessage()" as arguments.
*
* <p>If the method you're delegating to accepts parameters, you can pass {@code check} a format
* string. For example, {@link MultimapSubject#valuesForKey} calls {@code
* check("valuesForKey(%s)", key)}.
*
* <p>If you aren't really delegating to an instance method on the actual value -- maybe you're
* calling a static method, or you're calling a chain of several methods -- you can supply
* whatever string will be most useful to users. For example, if you're delegating to {@code
* getOnlyElement(actual.colors())}, you might call {@code check("onlyColor()")}.
*
* @param format a template with {@code %s} placeholders
* @param args the arguments to be inserted into those placeholders
*/
protected final StandardSubjectBuilder check(String format, Object... args) {
return doCheck(OldAndNewValuesAreSimilar.DIFFERENT, format, args);
}
// TODO(b/134064106): Figure out a public API for this.
final StandardSubjectBuilder checkNoNeedToDisplayBothValues(String format, Object... args) {
return doCheck(OldAndNewValuesAreSimilar.SIMILAR, format, args);
}
private StandardSubjectBuilder doCheck(
OldAndNewValuesAreSimilar valuesAreSimilar, String format, Object[] args) {
final LazyMessage message = new LazyMessage(format, args);
Function<String, String> descriptionUpdate =
new Function<String, String>() {
@Override
public String apply(String input) {
return input + "." + message;
}
};
return new StandardSubjectBuilder(
metadata.updateForCheckCall(valuesAreSimilar, descriptionUpdate));
}
/**
* Begins a new call chain that ignores any failures. This is useful for subjects that normally
* delegate with to other subjects by using {@link #check} but have already reported a failure. In
* such cases it may still be necessary to return a {@code Subject} instance even though any
* subsequent assertions are meaningless. For example, if a user chains together more {@link
* ThrowableSubject#hasCauseThat} calls than the actual exception has causes, {@code hasCauseThat}
* returns {@code ignoreCheck().that(... a dummy exception ...)}.
*/
protected final StandardSubjectBuilder ignoreCheck() {
return StandardSubjectBuilder.forCustomFailureStrategy(IGNORE_STRATEGY);
}
/**
* Fails, reporting a message with two "{@linkplain Fact facts}":
*
* <ul>
* <li><i>key</i>: <i>value</i>
* <li>but was: <i>actual value</i>.
* </ul>
*
* <p>This is the simplest failure API. For more advanced needs, see {@linkplain
* #failWithActual(Fact, Fact...) the other overload} and {@link #failWithoutActual(Fact, Fact...)
* failWithoutActual}.
*
* <p>Example usage: The check {@code contains(String)} calls {@code failWithActual("expected to
* contain", string)}.
*/
protected final void failWithActual(String key, @Nullable Object value) {
failWithActual(fact(key, value));
}
/**
* Fails, reporting a message with the given facts, followed by an automatically added fact of the
* form:
*
* <ul>
* <li>but was: <i>actual value</i>.
* </ul>
*
* <p>If you have only one fact to report (and it's a {@linkplain Fact#fact key-value fact}),
* prefer {@linkplain #failWithActual(String, Object) the simpler overload}.
*
* <p>Example usage: The check {@code isEmpty()} calls {@code failWithActual(simpleFact("expected
* to be empty"))}.
*/
protected final void failWithActual(Fact first, Fact... rest) {
doFail(sandwich(first, rest, butWas()));
}
// TODO(cpovirk): Consider making this protected if there's a need for it.
final void failWithActual(Iterable<Fact> facts) {
doFail(append(ImmutableList.copyOf(facts), butWas()));
}
/**
* Reports a failure constructing a message from a simple verb.
*
* @param check the check being asserted
* @deprecated Prefer to construct {@link Fact}-style methods, typically by using {@link
* #failWithActual(Fact, Fact...) failWithActual}{@code (}{@link Fact#simpleFact
* simpleFact(...)}{@code )}. However, if you want to preserve your exact failure message as a
* migration aid, you can inline this method (and then inline the resulting method call, as
* well).
*/
@Deprecated
final void fail(String check) {
fail(check, new Object[0]);
}
/**
* Assembles a failure message and passes such to the FailureStrategy
*
* @param verb the check being asserted
* @param other the value against which the subject is compared
* @deprecated Prefer to construct {@link Fact}-style methods, typically by using {@link
* #failWithActual(String, Object)}. However, if you want to preserve your exact failure
* message as a migration aid, you can inline this method (and then inline the resulting
* method call, as well).
*/
@Deprecated
final void fail(String verb, Object other) {
fail(verb, new Object[] {other});
}
/**
* Assembles a failure message and passes such to the FailureStrategy
*
* @param verb the check being asserted
* @param messageParts the expectations against which the subject is compared
* @deprecated Prefer to construct {@link Fact}-style methods, typically by using {@link
* #failWithActual(Fact, Fact...)}. However, if you want to preserve your exact failure
* message as a migration aid, you can inline this method.
*/
@Deprecated
final void fail(String verb, Object... messageParts) {
StringBuilder message = new StringBuilder("Not true that <");
message.append(actualCustomStringRepresentation()).append("> ").append(verb);
for (Object part : messageParts) {
message.append(" <").append(part).append(">");
}
failWithoutActual(simpleFact(message.toString()));
}
enum EqualityCheck {
EQUAL("expected"),
SAME_INSTANCE("expected specific instance");
final String keyForExpected;
EqualityCheck(String keyForExpected) {
this.keyForExpected = keyForExpected;
}
}
/**
* Special version of {@link #failEqualityCheck} for use from {@link IterableSubject}, documented
* further there.
*/
final void failEqualityCheckForEqualsWithoutDescription(Object expected) {
failEqualityCheck(EqualityCheck.EQUAL, expected, ComparisonResult.differentNoDescription());
}
private void failEqualityCheck(
EqualityCheck equalityCheck, Object expected, ComparisonResult difference) {
String actualString = actualCustomStringRepresentation();
String expectedString = formatActualOrExpected(expected);
String actualClass = actual == null ? "(null reference)" : actual.getClass().getName();
String expectedClass = expected == null ? "(null reference)" : expected.getClass().getName();
/*
* It's a little odd for expectedString to be formatActualOrExpected(expected) but actualString
* *not* to be formatActualOrExpected(actual), since we're going to compare the two. Instead,
* actualString is actualCustomStringRepresentation() -- as it is for other assertions, since
* users may have overridden that method. While actualCustomStringRepresentation() defaults to
* formatActualOrExpected(actual), it's only a default.
*
* What we really want here is probably to delete actualCustomStringRepresentation() and migrate
* users to formatActualOrExpected(actual).
*/
boolean sameToStrings = actualString.equals(expectedString);
boolean sameClassNames = actualClass.equals(expectedClass);
// TODO(cpovirk): Handle "same class name, different class loader."
// `equal` is always false for isEqualTo, but it varies for isSameInstanceAs:
boolean equal = difference.valuesAreEqual();
if (equalityCheck == EqualityCheck.EQUAL
&& (tryFailForTrailingWhitespaceOnly(expected) || tryFailForEmptyString(expected))) {
// tryFailForTrailingWhitespaceOnly or tryFailForEmptyString reported a failure, so we're done
return;
}
if (sameToStrings) {
if (sameClassNames) {
String doppelgangerDescription =
equal
? "(different but equal instance of same class with same string representation)"
: "(non-equal instance of same class with same string representation)";
failEqualityCheckNoComparisonFailure(
difference,
fact(equalityCheck.keyForExpected, expectedString),
fact("but was", doppelgangerDescription));
} else {
failEqualityCheckNoComparisonFailure(
difference,
fact(equalityCheck.keyForExpected, expectedString),
fact("an instance of", expectedClass),
fact("but was", "(non-equal value with same string representation)"),
fact("an instance of", actualClass));
}
} else {
if (equalityCheck == EqualityCheck.EQUAL && actual != null && expected != null) {
metadata.failEqualityCheck(
nameAsFacts(), difference.factsOrEmpty(), expectedString, actualString);
} else {
failEqualityCheckNoComparisonFailure(
difference,
fact(equalityCheck.keyForExpected, expectedString),
fact("but was", actualString));
}
}
}
/**
* Checks whether the actual and expected values are strings that match except for trailing
* whitespace. If so, reports a failure and returns true.
*/
private boolean tryFailForTrailingWhitespaceOnly(Object expected) {
if (!(actual instanceof String) || !(expected instanceof String)) {
return false;
}
/*
* TODO(cpovirk): Consider applying this for non-String types. The danger there is that we don't
* know whether toString() (or actualCustomStringRepresentation/formatActualOrExpected) and
* equals() are consistent for those types.
*/
String actualString = (String) actual;
String expectedString = (String) expected;
String actualNoTrailing = whitespace().trimTrailingFrom(actualString);
String expectedNoTrailing = whitespace().trimTrailingFrom(expectedString);
String expectedTrailing =
escapeWhitespace(expectedString.substring(expectedNoTrailing.length()));
String actualTrailing = escapeWhitespace(actualString.substring(actualNoTrailing.length()));
if (!actualNoTrailing.equals(expectedNoTrailing)) {
return false;
}
if (actualString.startsWith(expectedString)) {
failWithoutActual(
fact("expected", expectedString),
fact("but contained extra trailing whitespace", actualTrailing));
} else if (expectedString.startsWith(actualString)) {
failWithoutActual(
fact("expected", expectedString),
fact("but was missing trailing whitespace", expectedTrailing));
} else {
failWithoutActual(
fact("expected", expectedString),
fact("with trailing whitespace", expectedTrailing),
fact("but trailing whitespace was", actualTrailing));
}
return true;
}
private static String escapeWhitespace(String in) {
StringBuilder out = new StringBuilder();
for (char c : in.toCharArray()) {
out.append(escapeWhitespace(c));
}
return out.toString();
}
private static String escapeWhitespace(char c) {
switch (c) {
case '\t':
return "\\t";
case '\n':
return "\\n";
case '\f':
return "\\f";
case '\r':
return "\\r";
case ' ':
return "␣";
default:
return new String(asUnicodeHexEscape(c));
}
}
/**
* Checks whether the actual and expected values are empty strings. If so, reports a failure and
* returns true.
*/
private boolean tryFailForEmptyString(Object expected) {
if (!(actual instanceof String) || !(expected instanceof String)) {
return false;
}
String actualString = (String) actual;
String expectedString = (String) expected;
if (actualString.isEmpty()) {
failWithoutActual(fact("expected", expectedString), simpleFact("but was an empty string"));
return true;
} else if (expectedString.isEmpty()) {
failWithoutActual(simpleFact("expected an empty string"), fact("but was", actualString));
return true;
}
// Neither string was empty
return false;
}
// From SourceCodeEscapers:
private static final char[] HEX_DIGITS = "0123456789abcdef".toCharArray();
private static char[] asUnicodeHexEscape(char c) {
// Equivalent to String.format("\\u%04x", (int) c);
char[] r = new char[6];
r[0] = '\\';
r[1] = 'u';
r[5] = HEX_DIGITS[c & 0xF];
c = (char) (c >>> 4);
r[4] = HEX_DIGITS[c & 0xF];
c = (char) (c >>> 4);
r[3] = HEX_DIGITS[c & 0xF];
c = (char) (c >>> 4);
r[2] = HEX_DIGITS[c & 0xF];
return r;
}
private void failEqualityCheckNoComparisonFailure(ComparisonResult difference, Fact... facts) {
// TODO(cpovirk): Is it possible for difference.factsOrEmpty() to be nonempty? If not, remove.
doFail(concat(asList(facts), difference.factsOrEmpty()));
}
/**
* Assembles a failure message and passes it to the FailureStrategy
*
* @param verb the check being asserted
* @param expected the expectations against which the subject is compared
* @param failVerb the failure of the check being asserted
* @param actual the actual value the subject was compared against
* @deprecated Prefer to construct {@link Fact}-style methods, typically by using {@link
* #failWithActual(Fact, Fact...)}. However, if you want to preserve your exact failure
* message as a migration aid, you can inline this method.
*/
@Deprecated