summaryrefslogtreecommitdiff
path: root/include/gtest/gtest.h
diff options
context:
space:
mode:
authorvladlosev <vladlosev@861a406c-534a-0410-8894-cb66d6ee9925>2009-10-20 21:03:10 +0000
committervladlosev <vladlosev@861a406c-534a-0410-8894-cb66d6ee9925>2009-10-20 21:03:10 +0000
commitfbd53a53c1e01dec71c65754cf73282e4759bc40 (patch)
treeab4f68a695479be0a9eaadcadc364f6c4b06d0c0 /include/gtest/gtest.h
parentd6cb9725050e07247a186a397573e7c95e263bb4 (diff)
downloadgoogletest-fbd53a53c1e01dec71c65754cf73282e4759bc40.tar.gz
Implements support for AssertionResult in Boolean assertions such as EXPECT_TRUE; Fixes Google Tests's tuple implementation to default-initialize its fields in the default constructor (by Zhanyong Wan); Populates gtest_stress_test.cc with actual tests.
git-svn-id: http://googletest.googlecode.com/svn/trunk@331 861a406c-534a-0410-8894-cb66d6ee9925
Diffstat (limited to 'include/gtest/gtest.h')
-rw-r--r--include/gtest/gtest.h146
1 files changed, 115 insertions, 31 deletions
diff --git a/include/gtest/gtest.h b/include/gtest/gtest.h
index 9be15fb..33e2f7f 100644
--- a/include/gtest/gtest.h
+++ b/include/gtest/gtest.h
@@ -177,63 +177,145 @@ String StreamableToString(const T& streamable) {
// A class for indicating whether an assertion was successful. When
// the assertion wasn't successful, the AssertionResult object
-// remembers a non-empty message that described how it failed.
+// remembers a non-empty message that describes how it failed.
//
-// This class is useful for defining predicate-format functions to be
-// used with predicate assertions (ASSERT_PRED_FORMAT*, etc).
-//
-// The constructor of AssertionResult is private. To create an
-// instance of this class, use one of the factory functions
+// To create an instance of this class, use one of the factory functions
// (AssertionSuccess() and AssertionFailure()).
//
-// For example, in order to be able to write:
+// This class is useful for two purposes:
+// 1. Defining predicate functions to be used with Boolean test assertions
+// EXPECT_TRUE/EXPECT_FALSE and their ASSERT_ counterparts
+// 2. Defining predicate-format functions to be
+// used with predicate assertions (ASSERT_PRED_FORMAT*, etc).
+//
+// For example, if you define IsEven predicate:
+//
+// testing::AssertionResult IsEven(int n) {
+// if ((n % 2) == 0)
+// return testing::AssertionSuccess();
+// else
+// return testing::AssertionFailure() << n << " is odd";
+// }
+//
+// Then the failed expectation EXPECT_TRUE(IsEven(Fib(5)))
+// will print the message
+//
+// Value of: IsEven(Fib(5))
+// Actual: false (5 is odd)
+// Expected: true
+//
+// instead of a more opaque
+//
+// Value of: IsEven(Fib(5))
+// Actual: false
+// Expected: true
+//
+// in case IsEven is a simple Boolean predicate.
+//
+// If you expect your predicate to be reused and want to support informative
+// messages in EXPECT_FALSE and ASSERT_FALSE (negative assertions show up
+// about half as often as positive ones in our tests), supply messages for
+// both success and failure cases:
+//
+// testing::AssertionResult IsEven(int n) {
+// if ((n % 2) == 0)
+// return testing::AssertionSuccess() << n << " is even";
+// else
+// return testing::AssertionFailure() << n << " is odd";
+// }
+//
+// Then a statement EXPECT_FALSE(IsEven(Fib(6))) will print
+//
+// Value of: IsEven(Fib(6))
+// Actual: true (8 is even)
+// Expected: false
+//
+// NB: Predicates that support negative Boolean assertions have reduced
+// performance in positive ones so be careful not to use them in tests
+// that have lots (tens of thousands) of positive Boolean assertions.
+//
+// To use this class with EXPECT_PRED_FORMAT assertions such as:
//
// // Verifies that Foo() returns an even number.
// EXPECT_PRED_FORMAT1(IsEven, Foo());
//
-// you just need to define:
+// you need to define:
//
// testing::AssertionResult IsEven(const char* expr, int n) {
-// if ((n % 2) == 0) return testing::AssertionSuccess();
-//
-// Message msg;
-// msg << "Expected: " << expr << " is even\n"
-// << " Actual: it's " << n;
-// return testing::AssertionFailure(msg);
+// if ((n % 2) == 0)
+// return testing::AssertionSuccess();
+// else
+// return testing::AssertionFailure()
+// << "Expected: " << expr << " is even\n Actual: it's " << n;
// }
//
// If Foo() returns 5, you will see the following message:
//
// Expected: Foo() is even
// Actual: it's 5
+//
class AssertionResult {
public:
- // Declares factory functions for making successful and failed
- // assertion results as friends.
- friend AssertionResult AssertionSuccess();
- friend AssertionResult AssertionFailure(const Message&);
+ // Copy constructor.
+ // Used in EXPECT_TRUE/FALSE(assertion_result).
+ AssertionResult(const AssertionResult& other);
+ // Used in the EXPECT_TRUE/FALSE(bool_expression).
+ explicit AssertionResult(bool success) : success_(success) {}
// Returns true iff the assertion succeeded.
- operator bool() const { return failure_message_.c_str() == NULL; } // NOLINT
+ operator bool() const { return success_; } // NOLINT
+
+ // Returns the assertion's negation. Used with EXPECT/ASSERT_FALSE.
+ AssertionResult operator!() const;
+
+ // Returns the text streamed into this AssertionResult. Test assertions
+ // use it when they fail (i.e., the predicate's outcome doesn't match the
+ // assertion's expectation). When nothing has been streamed into the
+ // object, returns an empty string.
+ const char* message() const {
+ return message_.get() != NULL && message_->c_str() != NULL ?
+ message_->c_str() : "";
+ }
+ // TODO(vladl@google.com): Remove this after making sure no clients use it.
+ // Deprecated; please use message() instead.
+ const char* failure_message() const { return message(); }
- // Returns the assertion's failure message.
- const char* failure_message() const { return failure_message_.c_str(); }
+ // Streams a custom failure message into this object.
+ template <typename T> AssertionResult& operator<<(const T& value);
private:
- // The default constructor. It is used when the assertion succeeded.
- AssertionResult() {}
-
- // The constructor used when the assertion failed.
- explicit AssertionResult(const internal::String& failure_message);
-
- // Stores the assertion's failure message.
- internal::String failure_message_;
-};
+ // No implementation - we want AssertionResult to be
+ // copy-constructible but not assignable.
+ void operator=(const AssertionResult& other);
+
+ // Stores result of the assertion predicate.
+ bool success_;
+ // Stores the message describing the condition in case the expectation
+ // construct is not satisfied with the predicate's outcome.
+ // Referenced via a pointer to avoid taking too much stack frame space
+ // with test assertions.
+ internal::scoped_ptr<internal::String> message_;
+}; // class AssertionResult
+
+// Streams a custom failure message into this object.
+template <typename T>
+AssertionResult& AssertionResult::operator<<(const T& value) {
+ Message msg;
+ if (message_.get() != NULL)
+ msg << *message_;
+ msg << value;
+ message_.reset(new internal::String(msg.GetString()));
+ return *this;
+}
// Makes a successful assertion result.
AssertionResult AssertionSuccess();
+// Makes a failed assertion result.
+AssertionResult AssertionFailure();
+
// Makes a failed assertion result with the given failure message.
+// Deprecated; use AssertionFailure() << msg.
AssertionResult AssertionFailure(const Message& msg);
// The abstract class that all tests inherit from.
@@ -1603,7 +1685,9 @@ const T* TestWithParam<T>::parameter_ = NULL;
#define ASSERT_ANY_THROW(statement) \
GTEST_TEST_ANY_THROW_(statement, GTEST_FATAL_FAILURE_)
-// Boolean assertions.
+// Boolean assertions. Condition can be either a Boolean expression or an
+// AssertionResult. For more information on how to use AssertionResult with
+// these macros see comments on that class.
#define EXPECT_TRUE(condition) \
GTEST_TEST_BOOLEAN_(condition, #condition, false, true, \
GTEST_NONFATAL_FAILURE_)