Special-cases equality testing empty strings against non-empty strings.
Now prints "expected an empty string" or "but was an empty
string" instead of "expected: " or "but was: ".
Fixes
RELNOTES=n/a
@@ -834,10 +834,10 @@ public class Subject {
// TODO(cpovirk): Handle "same class name, different class loader."
// `equal` is always false for isEqualTo, but it varies for isSameInstanceAs:
boolean equal = difference.valuesAreEqual();
- // TODO(cpovirk): Call attention to differing trailing whitespace.
- if (equalityCheck == EqualityCheck.EQUAL && tryFailForTrailingWhitespaceOnly(expected)) {
- // tryFailForTrailingWhitespaceOnly reported a failure, so we're done.
+ if (equalityCheck == EqualityCheck.EQUAL
+ && (tryFailForTrailingWhitespaceOnly(expected) || tryFailForEmptyString(expected))) {
+ // tryFailForTrailingWhitespaceOnly or tryFailForEmptyString reported a failure, so we're done
return;
}
@@ -941,6 +941,29 @@ public class Subject {
}
}
+ /**
+ * 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();
