Skip to content

Commit

Permalink
Put "value of," "object was," and any user messages on their own line…
Browse files Browse the repository at this point in the history
…s, separate from the body.

This inches us closer to the format we'll have after we shift to a field-style format and API.
In particular, it will let us reimplement failWithRawMessage(...) as a call to failWithFields(fieldWithoutValue(...)) and migrate callers.

It also breaks up long lines like those plenty of existing subjects
produce.

RELNOTES=Put any user messages on their own lines, separate from the main failure message.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=189630013
  • Loading branch information
cpovirk authored and ronshapiro committed Mar 20, 2018
1 parent cd74a13 commit 6d060b8
Show file tree
Hide file tree
Showing 11 changed files with 85 additions and 92 deletions.
25 changes: 9 additions & 16 deletions core/src/main/java/com/google/common/truth/FailureMetadata.java
Original file line number Diff line number Diff line change
Expand Up @@ -209,25 +209,18 @@ private void doFail(AssertionError failure) {
private String addToMessage(String body) {
ImmutableList<?> messages = allPrefixMessages();
StringBuilder result = new StringBuilder(body.length());
Joiner.on(": ").appendTo(result, messages);
Joiner.on("\n").appendTo(result, messages);
if (!messages.isEmpty()) {
if (body.isEmpty()) {
/*
* The only likely case of an empty body is with failComparing(). In that case, we still
* want a colon because ComparisonFailure will construct a message of the form
* "<ourString> <theirString>." For consistency with the normal behavior of withMessage,
* we want a colon between the parts.
*
* That actually makes it sound like we'd want to *always* include the trailing colon in
* the case of failComparing(), but I don't want to bite that off now in case it requires
* updating more existing Subjects' tests.
*/
result.append(":");
} else {
result.append(": ");
}
result.append("\n");
}
result.append(body);
/*
* JUnit's ComparisonFailure will append " expected: <...> but was: <...> to the end of this.
* Note the space at the beginning. That means that, if the body is empty, the "expected... but
* was" text will come at the beginning of a line... indented by one space :\ The right fix for
* this is to stop depending on JUnit's ComparisonFailure formatting. That's coming. For now,
* the space is an annoyance.
*/
return result.toString();
}

Expand Down
2 changes: 1 addition & 1 deletion core/src/main/java/com/google/common/truth/Truth.java
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,7 @@ public String toString() {
@Nullable
static String appendSuffixIfNotNull(String message, String suffix) {
if (suffix != null) {
message += ": " + suffix;
message += "\n" + suffix;
}
return message;
}
Expand Down
24 changes: 12 additions & 12 deletions core/src/test/java/com/google/common/truth/ChainingTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ public void oneLevelNamed() {
expectFailureWhenTestingThat("root")
.delegatingToNamed("child", "child")
.isThePresentKingOfFrance();
assertNoCause("value of: myObject.child: message: myObject was: root");
assertNoCause("value of: myObject.child\nmessage\nmyObject was: root");
}

@Test
Expand All @@ -110,7 +110,7 @@ public void twoLevelsNamed() {
.delegatingToNamed("child", "child")
.delegatingToNamed("grandchild", "grandchild")
.isThePresentKingOfFrance();
assertNoCause("value of: myObject.child.grandchild: message: myObject was: root");
assertNoCause("value of: myObject.child.grandchild\nmessage\nmyObject was: root");
}

@Test
Expand All @@ -119,7 +119,7 @@ public void twoLevelsOnlyFirstNamed() {
.delegatingToNamed("child", "child")
.delegatingTo("grandchild")
.isThePresentKingOfFrance();
assertNoCause("message: myObject was: root");
assertNoCause("message\nmyObject was: root");
}

@Test
Expand All @@ -128,15 +128,15 @@ public void twoLevelsOnlySecondNamed() {
.delegatingTo("child")
.delegatingToNamed("grandchild", "grandchild")
.isThePresentKingOfFrance();
assertNoCause("value of: myObject.grandchild: message: myObject was: root");
assertNoCause("value of: myObject.grandchild\nmessage\nmyObject was: root");
}

@Test
public void oneLevelNamedNoNeedToDisplayBoth() {
expectFailureWhenTestingThat("root")
.delegatingToNamedNoNeedToDisplayBoth("child", "child")
.isThePresentKingOfFrance();
assertNoCause("value of: myObject.child: message");
assertNoCause("value of: myObject.child\nmessage");
}

@Test
Expand All @@ -145,7 +145,7 @@ public void twoLevelsNamedNoNeedToDisplayBoth() {
.delegatingToNamedNoNeedToDisplayBoth("child", "child")
.delegatingToNamedNoNeedToDisplayBoth("grandchild", "grandchild")
.isThePresentKingOfFrance();
assertNoCause("value of: myObject.child.grandchild: message");
assertNoCause("value of: myObject.child.grandchild\nmessage");
}

@Test
Expand All @@ -163,7 +163,7 @@ public void twoLevelsOnlySecondNamedNoNeedToDisplayBoth() {
.delegatingTo("child")
.delegatingToNamedNoNeedToDisplayBoth("grandchild", "grandchild")
.isThePresentKingOfFrance();
assertNoCause("value of: myObject.grandchild: message");
assertNoCause("value of: myObject.grandchild\nmessage");
}

@Test
Expand All @@ -172,7 +172,7 @@ public void twoLevelsNamedOnlyFirstNoNeedToDisplayBoth() {
.delegatingToNamedNoNeedToDisplayBoth("child", "child")
.delegatingToNamed("grandchild", "grandchild")
.isThePresentKingOfFrance();
assertNoCause("value of: myObject.child.grandchild: message: myObject was: root");
assertNoCause("value of: myObject.child.grandchild\nmessage\nmyObject was: root");
}

@Test
Expand All @@ -181,14 +181,14 @@ public void twoLevelsNamedOnlySecondNoNeedToDisplayBoth() {
.delegatingToNamed("child", "child")
.delegatingToNamedNoNeedToDisplayBoth("grandchild", "grandchild")
.isThePresentKingOfFrance();
assertNoCause("value of: myObject.child.grandchild: message: myObject was: root");
assertNoCause("value of: myObject.child.grandchild\nmessage\nmyObject was: root");
}

@Test
public void namedAndComparisonFailure() {
expectFailureWhenTestingThat("root").delegatingToNamed("child", "child").isEqualToString("z");
assertNoCause(
"value of: myObject.child: message expected:<[child]> but was:<[z]>: myObject was: root");
"value of: myObject.child\nmessage expected:<[child]> but was:<[z]>\nmyObject was: root");
}

@Test
Expand All @@ -200,7 +200,7 @@ public void namedAndMessage() {
.that("root")
.delegatingToNamed("child", "child")
.isThePresentKingOfFrance();
assertNoCause("prefix: value of: myObject.child: message: myObject was: root");
assertNoCause("prefix\nvalue of: myObject.child\nmessage\nmyObject was: root");
}

@Test
Expand All @@ -212,7 +212,7 @@ public void checkFail() {
@Test
public void checkFailWithName() {
expectFailureWhenTestingThat("root").doCheckFail("child");
assertNoCause("value of: myObject.child: message: myObject was: root");
assertNoCause("value of: myObject.child\nmessage\nmyObject was: root");
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public void assertWithMessageIsPrepended() {
} catch (AssertionError expected) {
assertThat(expected)
.hasMessageThat()
.isEqualTo("Invalid month: Not true that <13> is in <" + range + ">");
.isEqualTo("Invalid month\nNot true that <13> is in <" + range + ">");
return;
}
fail("Should have thrown");
Expand All @@ -55,7 +55,7 @@ public void assertWithMessageIsPrependedWithNamed() {
} catch (AssertionError expected) {
assertThat(expected)
.hasMessageThat()
.isEqualTo("Invalid month: Not true that Septober (<13>) is in <" + range + ">");
.isEqualTo("Invalid month\nNot true that Septober (<13>) is in <" + range + ">");
return;
}
fail("Should have thrown");
Expand All @@ -69,7 +69,7 @@ public void assertWithMessageThat() {
assertThat(expected)
.hasMessageThat()
.isEqualTo(
"This is a custom message: The subject was expected to be true, but was false");
"This is a custom message\nThe subject was expected to be true, but was false");
return;
}
fail("Should have thrown");
Expand All @@ -83,7 +83,7 @@ public void customMessageIsPrepended() {
} catch (AssertionError expected) {
assertThat(expected)
.hasMessageThat()
.isEqualTo("Invalid month: Not true that <13> is in <" + range + ">");
.isEqualTo("Invalid month\nNot true that <13> is in <" + range + ">");
return;
}
fail("Should have thrown");
Expand All @@ -97,7 +97,7 @@ public void customMessageIsPrependedWithNamed() {
} catch (AssertionError expected) {
assertThat(expected)
.hasMessageThat()
.isEqualTo("Invalid month: Not true that Septober (<13>) is in <" + range + ">");
.isEqualTo("Invalid month\nNot true that Septober (<13>) is in <" + range + ">");
return;
}
fail("Should have thrown");
Expand All @@ -111,7 +111,7 @@ public void customMessage() {
assertThat(expected)
.hasMessageThat()
.isEqualTo(
"This is a custom message: The subject was expected to be true, but was false");
"This is a custom message\nThe subject was expected to be true, but was false");
return;
}
fail("Should have thrown");
Expand Down Expand Up @@ -142,7 +142,7 @@ public void assertWithMessageIsPrepended_withPlaceholders() {
} catch (AssertionError expected) {
assertThat(expected)
.hasMessageThat()
.isEqualTo("Invalid month: Not true that <13> is in <" + range + ">");
.isEqualTo("Invalid month\nNot true that <13> is in <" + range + ">");
return;
}
fail("Should have thrown");
Expand All @@ -156,7 +156,7 @@ public void assertWithMessageIsPrependedWithNamed_withPlaceholders() {
} catch (AssertionError expected) {
assertThat(expected)
.hasMessageThat()
.isEqualTo("Invalid month: Not true that Septober (<13>) is in <" + range + ">");
.isEqualTo("Invalid month\nNot true that Septober (<13>) is in <" + range + ">");
return;
}
fail("Should have thrown");
Expand All @@ -170,7 +170,7 @@ public void assertWithMessageThat_withPlaceholders() {
assertThat(expected)
.hasMessageThat()
.isEqualTo(
"This is a custom message: The subject was expected to be true, but was false");
"This is a custom message\nThe subject was expected to be true, but was false");
return;
}
fail("Should have thrown");
Expand All @@ -184,7 +184,7 @@ public void customMessageIsPrepended_withPlaceholders() {
} catch (AssertionError expected) {
assertThat(expected)
.hasMessageThat()
.isEqualTo("Invalid month: Not true that <13> is in <" + range + ">");
.isEqualTo("Invalid month\nNot true that <13> is in <" + range + ">");
return;
}
fail("Should have thrown");
Expand All @@ -198,7 +198,7 @@ public void customMessageIsPrependedWithNamed_withPlaceholders() {
} catch (AssertionError expected) {
assertThat(expected)
.hasMessageThat()
.isEqualTo("Invalid month: Not true that Septober (<13>) is in <" + range + ">");
.isEqualTo("Invalid month\nNot true that Septober (<13>) is in <" + range + ">");
return;
}
fail("Should have thrown");
Expand All @@ -212,7 +212,7 @@ public void customMessage_withPlaceholders() {
assertThat(expected)
.hasMessageThat()
.isEqualTo(
"This is a custom message: The subject was expected to be true, but was false");
"This is a custom message\nThe subject was expected to be true, but was false");
return;
}
fail("Should have thrown");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ public void failureMessageIterableContainsFailure() {
expectFailure.whenTesting().withMessage("custom msg").that(asList(1, 2, 3)).contains(5);
assertThat(expectFailure.getFailure())
.hasMessageThat()
.isEqualTo("custom msg: <[1, 2, 3]> should have contained <5>");
.isEqualTo("custom msg\n<[1, 2, 3]> should have contained <5>");
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -176,9 +176,9 @@ public void multimapNamedValuesForKey() {
assertThat(expectFailure.getFailure())
.hasMessageThat()
.isEqualTo(
"value of: multymap.valuesForKey(1): "
"value of: multymap.valuesForKey(1)\n"
+ "Not true that <[5]> contains exactly <[4]>. "
+ "It is missing <[4]> and has unexpected items <[5]>: "
+ "It is missing <[4]> and has unexpected items <[5]>\n"
+ "multimap was: multymap ({1=[5]})");
}

Expand All @@ -189,9 +189,9 @@ public void valuesForKeyNamed() {
assertThat(expectFailure.getFailure())
.hasMessageThat()
.isEqualTo(
"value of: multimap.valuesForKey(1): "
"value of: multimap.valuesForKey(1)\n"
+ "Not true that valuez (<[5]>) contains exactly <[4]>. "
+ "It is missing <[4]> and has unexpected items <[5]>: "
+ "It is missing <[4]> and has unexpected items <[5]>\n"
+ "multimap was: {1=[5]}");
}

Expand Down
Loading

0 comments on commit 6d060b8

Please sign in to comment.