Reimplement the message-accepting overload of fail(...) in terms of withMessage(...).
This has a few minor effects:
- The message now comes before any "value of" line, rather than after. (This requires updating a couple tests in Truth. It's very rare for people to use fail(...) in the way that we do, though: Most people make simple calls like assert_().fail(...), not complex ones from inside Subjects like our tests do.)
- fail("foo") used to include "foo" as a Fact. Now, it includes it as a message. (This distinction is mostly irrelvant except for one test I found that was using TruthFailureSubject to look specifically for a Fact. I've updated it.)
- fail(...) no longer accepts a null message. (It's still possible to write fail("%s", somethingNull), and if you switch to calling withMessage(...).fail() directly, then that accepts a null message, too, if you're passing only one arg. This change appears to affect no one.)
Also, add a test that calls fail() to catch the bug that I almost introduced with empty messages.
And tweak some docs.
(Part of this was pulled out from CL 254051742.)
RELNOTES=Reimplemented the message-accepting overload of `fail(...)` in terms of `withMessage(...)`. This is mostly a no-op but can affect behavior in unusual cases. For details, see the commit description.
-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=254642313
diff --git a/core/src/main/java/com/google/common/truth/Fact.java b/core/src/main/java/com/google/common/truth/Fact.java
index f5de441..8154dde 100644
--- a/core/src/main/java/com/google/common/truth/Fact.java
+++ b/core/src/main/java/com/google/common/truth/Fact.java
@@ -125,7 +125,9 @@
}
builder.append('\n');
}
- builder.setLength(builder.length() - 1); // remove trailing \n
+ if (builder.length() > 0) {
+ builder.setLength(builder.length() - 1); // remove trailing \n
+ }
return builder.toString();
}
diff --git a/core/src/main/java/com/google/common/truth/StandardSubjectBuilder.java b/core/src/main/java/com/google/common/truth/StandardSubjectBuilder.java
index 568efd7..cab2735 100644
--- a/core/src/main/java/com/google/common/truth/StandardSubjectBuilder.java
+++ b/core/src/main/java/com/google/common/truth/StandardSubjectBuilder.java
@@ -16,8 +16,6 @@
package com.google.common.truth;
import static com.google.common.base.Preconditions.checkNotNull;
-import static com.google.common.base.Strings.lenientFormat;
-import static com.google.common.truth.Fact.simpleFact;
import com.google.common.annotations.GwtIncompatible;
import com.google.common.base.Optional;
@@ -220,25 +218,26 @@
return factory.createSubjectBuilder(metadata());
}
- /** Triggers the failure strategy with an empty failure message. */
+ /**
+ * Reports a failure.
+ *
+ * <p>To set a message, first call {@link #withMessage} (or, more commonly, use the shortcut
+ * {@link Truth#assertWithMessage}).
+ */
public final void fail() {
- doFail("");
+ metadata().fail(ImmutableList.<Fact>of());
}
/**
- * Triggers the failure strategy with the given failure message.
+ * Reports a failure with the given message.
*
* @deprecated Instead of {@code assert_().fail(...)}, use {@code assertWithMessage(...).fail()}.
* Similarly, instead of {@code expect.fail(...)}, use {@code expect.withMessage(...).fail()},
* and so forth.
*/
@Deprecated
- public final void fail(@NullableDecl String format, Object /*@NullableDeclType*/... args) {
- doFail(lenientFormat(format, args));
- }
-
- private void doFail(String message) {
- metadata().fail(ImmutableList.of(simpleFact(message)));
+ public final void fail(String format, Object /*@NullableDeclType*/... args) {
+ withMessage(format, args).fail();
}
private FailureMetadata metadata() {
diff --git a/core/src/test/java/com/google/common/truth/ChainingTest.java b/core/src/test/java/com/google/common/truth/ChainingTest.java
index 41813bd..ac58ec0 100644
--- a/core/src/test/java/com/google/common/truth/ChainingTest.java
+++ b/core/src/test/java/com/google/common/truth/ChainingTest.java
@@ -205,7 +205,7 @@
@Test
public void checkFailWithName() {
expectFailureWhenTestingThat("root").doCheckFail("child");
- assertNoCause("value of : myObject.child\nmessage\nmyObject was: root");
+ assertNoCause("message\nvalue of : myObject.child\nmyObject was: root");
}
@Test
diff --git a/core/src/test/java/com/google/common/truth/StandardSubjectBuilderTest.java b/core/src/test/java/com/google/common/truth/StandardSubjectBuilderTest.java
new file mode 100644
index 0000000..96bed9d
--- /dev/null
+++ b/core/src/test/java/com/google/common/truth/StandardSubjectBuilderTest.java
@@ -0,0 +1,48 @@
+/*
+ * Copyright (c) 2019 Google, Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.google.common.truth;
+
+import static com.google.common.truth.Truth.assert_;
+
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junit.runners.JUnit4;
+
+/** Tests for {@link StandardSubjectBuilder}. */
+@RunWith(JUnit4.class)
+public final class StandardSubjectBuilderTest extends BaseSubjectTestCase {
+ @Test
+ public void failNoMessage() {
+ expectFailure.whenTesting().fail();
+ assertThatFailure().hasMessageThat().isEmpty();
+ }
+
+ @Test
+ public void failWithMessage() {
+ expectFailure.whenTesting().fail("at index %s", 1);
+ assertThatFailure().hasMessageThat().isEqualTo("at index 1");
+ }
+
+ @Test
+ public void failNullMessage() {
+ try {
+ assert_().fail(null);
+ throw new AssertionError("should have thrown NullPointerException");
+ } catch (NullPointerException expected) {
+ }
+ }
+}
diff --git a/core/src/test/java/com/google/common/truth/ThrowableSubjectTest.java b/core/src/test/java/com/google/common/truth/ThrowableSubjectTest.java
index e9cce5d..bbf33e4 100644
--- a/core/src/test/java/com/google/common/truth/ThrowableSubjectTest.java
+++ b/core/src/test/java/com/google/common/truth/ThrowableSubjectTest.java
@@ -104,8 +104,8 @@
expectFailureWhenTestingThat(actual).hasCauseThat().hasCauseThat().isNull();
assertThat(expectFailure.getFailure().getMessage())
.isEqualTo(
- "value of: throwable.getCause().getCause()\n"
- + "Causal chain is not deep enough - add a .isNotNull() check?");
+ "Causal chain is not deep enough - add a .isNotNull() check?\n"
+ + "value of: throwable.getCause().getCause()");
assertErrorHasActualAsCause(actual, expectFailure.getFailure());
}