2424import static com .google .common .truth .Fact .simpleFact ;
2525import static com .google .common .truth .LazyMessage .evaluateAll ;
2626import static com .google .common .truth .Platform .cleanStackTrace ;
27+ import static com .google .common .truth .Platform .forceInferDescription ;
2728import static com .google .common .truth .Platform .inferDescription ;
28- import static com .google .common .truth .Platform .isInferDescriptionEnabledForExpectFailure ;
2929import static com .google .common .truth .Platform .makeComparisonFailure ;
3030import static com .google .common .truth .SubjectUtils .append ;
3131import static com .google .common .truth .SubjectUtils .concat ;
3232
3333import com .google .common .base .Function ;
3434import com .google .common .collect .ImmutableList ;
35- import com .google .common .truth .ExpectFailure .ExpectFailureFailureStrategy ;
3635import org .jspecify .annotations .Nullable ;
3736
3837/**
5655 * constructor.)
5756 */
5857public final class FailureMetadata {
59- static FailureMetadata forFailureStrategy (FailureStrategy strategy ) {
58+ static FailureMetadata forFailureStrategy (
59+ FailureStrategy strategy , boolean suppressInferDescription ) {
6060 return new FailureMetadata (
61- strategy , /* messages= */ ImmutableList .of (), /* steps= */ ImmutableList .of ());
61+ strategy ,
62+ suppressInferDescription ,
63+ /* messages= */ ImmutableList .of (),
64+ /* steps= */ ImmutableList .of ());
6265 }
6366
6467 private final FailureStrategy strategy ;
6568
66- /**
67- * The data from a call to either (a) a {@link Subject} constructor or (b) {@link Subject#check}.
68- */
69- private static final class Step {
70- static Step subjectCreation (Subject subject ) {
71- return new Step (checkNotNull (subject ), null , null );
72- }
73-
74- static Step checkCall (
75- @ Nullable OldAndNewValuesAreSimilar valuesAreSimilar ,
76- @ Nullable Function <String , String > descriptionUpdate ) {
77- return new Step (null , descriptionUpdate , valuesAreSimilar );
78- }
79-
80- /*
81- * We store Subject, rather than the actual value itself, so that we can call
82- * actualCustomStringRepresentation(). Why not call actualCustomStringRepresentation()
83- * immediately? First, it might be expensive, and second, the Subject isn't initialized at the
84- * time we receive it. We *might* be able to make it safe to call if it looks only at
85- * actualForPackageMembersToCall(), but it might try to look at facts initialized by a subclass,
86- * which aren't ready yet.
87- */
88- final @ Nullable Subject subject ;
89-
90- final @ Nullable Function <String , String > descriptionUpdate ;
91-
92- // Present only when descriptionUpdate is.
93- final @ Nullable OldAndNewValuesAreSimilar valuesAreSimilar ;
94-
95- private Step (
96- @ Nullable Subject subject ,
97- @ Nullable Function <String , String > descriptionUpdate ,
98- @ Nullable OldAndNewValuesAreSimilar valuesAreSimilar ) {
99- this .subject = subject ;
100- this .descriptionUpdate = descriptionUpdate ;
101- this .valuesAreSimilar = valuesAreSimilar ;
102- }
103-
104- boolean isCheckCall () {
105- return subject == null ;
106- }
107- }
69+ private final boolean suppressInferDescription ;
10870
10971 /*
11072 * TODO(cpovirk): This implementation is wasteful, especially because `steps` is used even by
@@ -119,8 +81,12 @@ boolean isCheckCall() {
11981 private final ImmutableList <Step > steps ;
12082
12183 private FailureMetadata (
122- FailureStrategy strategy , ImmutableList <LazyMessage > messages , ImmutableList <Step > steps ) {
84+ FailureStrategy strategy ,
85+ boolean suppressInferDescription ,
86+ ImmutableList <LazyMessage > messages ,
87+ ImmutableList <Step > steps ) {
12388 this .strategy = checkNotNull (strategy );
89+ this .suppressInferDescription = suppressInferDescription ;
12490 this .messages = checkNotNull (messages );
12591 this .steps = checkNotNull (steps );
12692 }
@@ -212,7 +178,7 @@ private void doFail(AssertionError failure) {
212178 }
213179
214180 private FailureMetadata derive (ImmutableList <LazyMessage > messages , ImmutableList <Step > steps ) {
215- return new FailureMetadata (strategy , messages , steps );
181+ return new FailureMetadata (strategy , suppressInferDescription , messages , steps );
216182 }
217183
218184 /**
@@ -246,10 +212,7 @@ private ImmutableList<Fact> description() {
246212 /** Overload of {@link #description()} that allows passing a custom key for the fact. */
247213 private ImmutableList <Fact > description (String factKey ) {
248214 String description =
249- strategy instanceof ExpectFailureFailureStrategy
250- && !isInferDescriptionEnabledForExpectFailure ()
251- ? null
252- : inferDescription ();
215+ suppressInferDescription && !forceInferDescription () ? null : inferDescription ();
253216 boolean descriptionIsInteresting = description != null ;
254217 for (Step step : steps ) {
255218 if (step .isCheckCall ()) {
@@ -353,4 +316,47 @@ && checkNotNull(step.subject).actualForPackageMembersToCall() instanceof Throwab
353316 }
354317 return null ;
355318 }
319+
320+ /**
321+ * The data from a call to either (a) a {@link Subject} constructor or (b) {@link Subject#check}.
322+ */
323+ private static final class Step {
324+ static Step subjectCreation (Subject subject ) {
325+ return new Step (checkNotNull (subject ), null , null );
326+ }
327+
328+ static Step checkCall (
329+ @ Nullable OldAndNewValuesAreSimilar valuesAreSimilar ,
330+ @ Nullable Function <String , String > descriptionUpdate ) {
331+ return new Step (null , descriptionUpdate , valuesAreSimilar );
332+ }
333+
334+ /*
335+ * We store Subject, rather than the actual value itself, so that we can call
336+ * actualCustomStringRepresentation(). Why not call actualCustomStringRepresentation()
337+ * immediately? First, it might be expensive, and second, the Subject isn't initialized at the
338+ * time we receive it. We *might* be able to make it safe to call if it looks only at
339+ * actualForPackageMembersToCall(), but it might try to look at facts initialized by a subclass,
340+ * which aren't ready yet.
341+ */
342+ final @ Nullable Subject subject ;
343+
344+ final @ Nullable Function <String , String > descriptionUpdate ;
345+
346+ // Present only when descriptionUpdate is.
347+ final @ Nullable OldAndNewValuesAreSimilar valuesAreSimilar ;
348+
349+ private Step (
350+ @ Nullable Subject subject ,
351+ @ Nullable Function <String , String > descriptionUpdate ,
352+ @ Nullable OldAndNewValuesAreSimilar valuesAreSimilar ) {
353+ this .subject = subject ;
354+ this .descriptionUpdate = descriptionUpdate ;
355+ this .valuesAreSimilar = valuesAreSimilar ;
356+ }
357+
358+ boolean isCheckCall () {
359+ return subject == null ;
360+ }
361+ }
356362}
0 commit comments