Skip to content

Commit

Permalink
Include custom name in failure messages for Strings.
Browse files Browse the repository at this point in the history
See google#189
-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=97588145
  • Loading branch information
kluever authored and cgruber committed Oct 29, 2015
1 parent c34e9f1 commit 364d2cb
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 8 deletions.
11 changes: 7 additions & 4 deletions core/src/main/java/com/google/common/truth/StringSubject.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,11 @@ public void isEqualTo(@Nullable Object expected) {
if (expected != null) {
if (expected instanceof String) {
failWithRawMessage(
"Not true that null reference is equal to <%s>", quote((String) expected));
"Not true that " + getDisplaySubject() + " is equal to <%s>",
quote((String) expected));
} else {
failWithRawMessage(
"Not true that null reference is equal to (%s)<%s>",
"Not true that " + getDisplaySubject() + " is equal to (%s)<%s>",
expected.getClass().getName(),
expected);
}
Expand Down Expand Up @@ -259,7 +260,9 @@ public void doesNotContainMatch(String regex) {
}
}

private static String quote(CharSequence toBeWrapped) {
return "\"" + toBeWrapped + "\"";
private static String quote(@Nullable CharSequence toBeWrapped) {
return (toBeWrapped == null)
? "null"
: "\"" + toBeWrapped + "\"";
}
}
10 changes: 10 additions & 0 deletions core/src/test/java/com/google/common/truth/StringTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,16 @@ public void stringNamedEqualityFail() {
}
}

@Test
public void stringNamedNullFail() {
try {
assertThat((String) null).named("foo").isEqualTo("abd");
throw new Error("Expected to fail.");
} catch (AssertionError expected) {
assertThat(expected).hasMessage("Not true that foo (<null>) is equal to <\"abd\">");
}
}

@Test
public void stringStartsWith() {
assertThat("abc").startsWith("ab");
Expand Down
8 changes: 4 additions & 4 deletions core/src/test/java/com/google/common/truth/SubjectTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -526,7 +526,7 @@ public void isInNullFailure() {
assertThat((String) null).isIn(oneShotIterable("a", "b", "c"));
fail("Should have thrown.");
} catch (AssertionError e) {
assertThat(e).hasMessage("Not true that <\"null\"> is equal to any element in <[a, b, c]>");
assertThat(e).hasMessage("Not true that <null> is equal to any element in <[a, b, c]>");
}
}

Expand Down Expand Up @@ -576,7 +576,7 @@ public void isAnyOfNullFailure() {
assertThat((String) null).isAnyOf("a", "b", "c");
fail("Should have thrown.");
} catch (AssertionError e) {
assertThat(e).hasMessage("Not true that <\"null\"> is equal to any of <[a, b, c]>");
assertThat(e).hasMessage("Not true that <null> is equal to any of <[a, b, c]>");
}
}

Expand Down Expand Up @@ -608,7 +608,7 @@ public void isNotInNullFailure() {
fail("Should have thrown.");
} catch (AssertionError e) {
assertThat(e)
.hasMessage("Not true that <\"null\"> is not in [a, b, null]. It was found at index 2");
.hasMessage("Not true that <null> is not in [a, b, null]. It was found at index 2");
}
}

Expand Down Expand Up @@ -645,7 +645,7 @@ public void isNoneOfNullFailure() {
fail("Should have thrown.");
} catch (AssertionError e) {
assertThat(e)
.hasMessage("Not true that <\"null\"> is not in [a, b, null]. It was found at index 2");
.hasMessage("Not true that <null> is not in [a, b, null]. It was found at index 2");
}
}

Expand Down

0 comments on commit 364d2cb

Please sign in to comment.