From dd10d96a3fade537777738e82011144222237fe7 Mon Sep 17 00:00:00 2001 From: ibaker Date: Mon, 3 Feb 2020 10:27:09 +0000 Subject: [PATCH] Tweak SpannedSubject error message when `actual` has no spans Before: No matching span found in text : Text with combined section. expected : start=10 end=18 type=HorizontalTextInVerticalContextSpan substring='combined' but found: After: No matching span found in text : Text with combined section. expected: start=10 end=18 type=HorizontalTextInVerticalContextSpan substring='combined' but found no spans PiperOrigin-RevId: 292878264 --- .../testutil/truth/SpannedSubject.java | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/testutils/src/main/java/com/google/android/exoplayer2/testutil/truth/SpannedSubject.java b/testutils/src/main/java/com/google/android/exoplayer2/testutil/truth/SpannedSubject.java index f5506de649..a980254277 100644 --- a/testutils/src/main/java/com/google/android/exoplayer2/testutil/truth/SpannedSubject.java +++ b/testutils/src/main/java/com/google/android/exoplayer2/testutil/truth/SpannedSubject.java @@ -39,6 +39,7 @@ import androidx.annotation.Nullable; import com.google.android.exoplayer2.text.span.HorizontalTextInVerticalContextSpan; import com.google.android.exoplayer2.text.span.RubySpan; import com.google.android.exoplayer2.util.Util; +import com.google.common.truth.Fact; import com.google.common.truth.FailureMetadata; import com.google.common.truth.Subject; import java.util.ArrayList; @@ -84,9 +85,7 @@ public final class SpannedSubject extends Subject { Object[] spans = actual.getSpans(0, actual.length(), Object.class); if (spans.length > 0) { failWithoutActual( - simpleFact("Expected no spans"), - fact("in text", actual), - fact("but found", getAllSpansAsString(actual))); + simpleFact("Expected no spans"), fact("in text", actual), actualSpansFact()); } } @@ -615,7 +614,7 @@ public final class SpannedSubject extends Subject { "Found unexpected %ss between start=%s,end=%s", spanClazz.getSimpleName(), start, end)), simpleFact("expected none"), - fact("but found", getAllSpansAsString(actual))); + actualSpansFact()); } } @@ -641,7 +640,17 @@ public final class SpannedSubject extends Subject { simpleFact("No matching span found"), fact("in text", actual), fact("expected", getSpanAsString(start, end, spanType, spannedSubstring)), - fact("but found", getAllSpansAsString(actual))); + actualSpansFact()); + } + + @RequiresNonNull("actual") + private Fact actualSpansFact() { + String actualSpans = getAllSpansAsString(actual); + if (actualSpans.isEmpty()) { + return Fact.simpleFact("but found no spans"); + } else { + return Fact.fact("but found", actualSpans); + } } private static String getAllSpansAsString(Spanned spanned) {