On Tue, Nov 6, 2012 at 7:57 PM, Peter Hutterer <span dir="ltr"><<a href="mailto:peter.hutterer@who-t.net" target="_blank">peter.hutterer@who-t.net</a>></span> wrote:<br><div class="gmail_extra"><div class="gmail_quote">
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">This header is for self-testing, specifically EXPECT_FATAL_FAILURE<br>
<br>
Signed-off-by: Peter Hutterer <<a href="mailto:peter.hutterer@who-t.net">peter.hutterer@who-t.net</a>><br>
---<br>
gtest/include/Makefile.am | 2 +-<br>
gtest/include/gtest/gtest-spi.h | 232 ++++++++++++++++++++++++++++++++++++++++<br>
2 files changed, 233 insertions(+), 1 deletion(-)<br>
create mode 100644 gtest/include/gtest/gtest-spi.h<br>
<br>
diff --git a/gtest/include/Makefile.am b/gtest/include/Makefile.am<br>
index 54d3088..6456b01 100644<br>
--- a/gtest/include/Makefile.am<br>
+++ b/gtest/include/Makefile.am<br>
@@ -22,4 +22,4 @@<br>
# SOFTWARE.<br>
#<br>
<br>
-nobase_include_HEADERS = gtest/gtest.h<br>
+nobase_include_HEADERS = gtest/gtest.h gtest/gtest-spi.h<br>
diff --git a/gtest/include/gtest/gtest-spi.h b/gtest/include/gtest/gtest-spi.h<br>
new file mode 100644<br>
index 0000000..f63fa9a<br>
--- /dev/null<br>
+++ b/gtest/include/gtest/gtest-spi.h<br>
@@ -0,0 +1,232 @@<br>
+// Copyright 2007, Google Inc.<br>
+// All rights reserved.<br>
+//<br>
+// Redistribution and use in source and binary forms, with or without<br>
+// modification, are permitted provided that the following conditions are<br>
+// met:<br>
+//<br>
+// * Redistributions of source code must retain the above copyright<br>
+// notice, this list of conditions and the following disclaimer.<br>
+// * Redistributions in binary form must reproduce the above<br>
+// copyright notice, this list of conditions and the following disclaimer<br>
+// in the documentation and/or other materials provided with the<br>
+// distribution.<br>
+// * Neither the name of Google Inc. nor the names of its<br>
+// contributors may be used to endorse or promote products derived from<br>
+// this software without specific prior written permission.<br>
+//<br>
+// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS<br>
+// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT<br>
+// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR<br>
+// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT<br>
+// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,<br>
+// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT<br>
+// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,<br>
+// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY<br>
+// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT<br>
+// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE<br>
+// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.<br>
+//<br>
+// Author: <a href="mailto:wan@google.com">wan@google.com</a> (Zhanyong Wan)<br>
+//<br>
+// Utilities for testing Google Test itself and code that uses Google Test<br>
+// (e.g. frameworks built on top of Google Test).<br>
+<br>
+#ifndef GTEST_INCLUDE_GTEST_GTEST_SPI_H_<br>
+#define GTEST_INCLUDE_GTEST_GTEST_SPI_H_<br>
+<br>
+#include "gtest/gtest.h"<br>
+<br>
+namespace testing {<br>
+<br>
+// This helper class can be used to mock out Google Test failure reporting<br>
+// so that we can test Google Test or code that builds on Google Test.<br>
+//<br>
+// An object of this class appends a TestPartResult object to the<br>
+// TestPartResultArray object given in the constructor whenever a Google Test<br>
+// failure is reported. It can either intercept only failures that are<br>
+// generated in the same thread that created this object or it can intercept<br>
+// all generated failures. The scope of this mock object can be controlled with<br>
+// the second argument to the two arguments constructor.<br>
+class GTEST_API_ ScopedFakeTestPartResultReporter<br>
+ : public TestPartResultReporterInterface {<br>
+ public:<br>
+ // The two possible mocking modes of this object.<br>
+ enum InterceptMode {<br>
+ INTERCEPT_ONLY_CURRENT_THREAD, // Intercepts only thread local failures.<br>
+ INTERCEPT_ALL_THREADS // Intercepts all failures.<br>
+ };<br>
+<br>
+ // The c'tor sets this object as the test part result reporter used<br>
+ // by Google Test. The 'result' parameter specifies where to report the<br>
+ // results. This reporter will only catch failures generated in the current<br>
+ // thread. DEPRECATED<br>
+ explicit ScopedFakeTestPartResultReporter(TestPartResultArray* result);<br>
+<br>
+ // Same as above, but you can choose the interception scope of this object.<br>
+ ScopedFakeTestPartResultReporter(InterceptMode intercept_mode,<br>
+ TestPartResultArray* result);<br>
+<br>
+ // The d'tor restores the previous test part result reporter.<br>
+ virtual ~ScopedFakeTestPartResultReporter();<br>
+<br>
+ // Appends the TestPartResult object to the TestPartResultArray<br>
+ // received in the constructor.<br>
+ //<br>
+ // This method is from the TestPartResultReporterInterface<br>
+ // interface.<br>
+ virtual void ReportTestPartResult(const TestPartResult& result);<br>
+ private:<br>
+ void Init();<br>
+<br>
+ const InterceptMode intercept_mode_;<br>
+ TestPartResultReporterInterface* old_reporter_;<br>
+ TestPartResultArray* const result_;<br>
+<br>
+ GTEST_DISALLOW_COPY_AND_ASSIGN_(ScopedFakeTestPartResultReporter);<br>
+};<br>
+<br>
+namespace internal {<br>
+<br>
+// A helper class for implementing EXPECT_FATAL_FAILURE() and<br>
+// EXPECT_NONFATAL_FAILURE(). Its destructor verifies that the given<br>
+// TestPartResultArray contains exactly one failure that has the given<br>
+// type and contains the given substring. If that's not the case, a<br>
+// non-fatal failure will be generated.<br>
+class GTEST_API_ SingleFailureChecker {<br>
+ public:<br>
+ // The constructor remembers the arguments.<br>
+ SingleFailureChecker(const TestPartResultArray* results,<br>
+ TestPartResult::Type type,<br>
+ const string& substr);<br>
+ ~SingleFailureChecker();<br>
+ private:<br>
+ const TestPartResultArray* const results_;<br>
+ const TestPartResult::Type type_;<br>
+ const string substr_;<br>
+<br>
+ GTEST_DISALLOW_COPY_AND_ASSIGN_(SingleFailureChecker);<br>
+};<br>
+<br>
+} // namespace internal<br>
+<br>
+} // namespace testing<br>
+<br>
+// A set of macros for testing Google Test assertions or code that's expected<br>
+// to generate Google Test fatal failures. It verifies that the given<br>
+// statement will cause exactly one fatal Google Test failure with 'substr'<br>
+// being part of the failure message.<br>
+//<br>
+// There are two different versions of this macro. EXPECT_FATAL_FAILURE only<br>
+// affects and considers failures generated in the current thread and<br>
+// EXPECT_FATAL_FAILURE_ON_ALL_THREADS does the same but for all threads.<br>
+//<br>
+// The verification of the assertion is done correctly even when the statement<br>
+// throws an exception or aborts the current function.<br>
+//<br>
+// Known restrictions:<br>
+// - 'statement' cannot reference local non-static variables or<br>
+// non-static members of the current object.<br>
+// - 'statement' cannot return a value.<br>
+// - You cannot stream a failure message to this macro.<br>
+//<br>
+// Note that even though the implementations of the following two<br>
+// macros are much alike, we cannot refactor them to use a common<br>
+// helper macro, due to some peculiarity in how the preprocessor<br>
+// works. The AcceptsMacroThatExpandsToUnprotectedComma test in<br>
+// gtest_unittest.cc will fail to compile if we do that.<br>
+#define EXPECT_FATAL_FAILURE(statement, substr) \<br>
+ do { \<br>
+ class GTestExpectFatalFailureHelper {\<br>
+ public:\<br>
+ static void Execute() { statement; }\<br>
+ };\<br>
+ ::testing::TestPartResultArray gtest_failures;\<br>
+ ::testing::internal::SingleFailureChecker gtest_checker(\<br>
+ >est_failures, ::testing::TestPartResult::kFatalFailure, (substr));\<br>
+ {\<br>
+ ::testing::ScopedFakeTestPartResultReporter gtest_reporter(\<br>
+ ::testing::ScopedFakeTestPartResultReporter:: \<br>
+ INTERCEPT_ONLY_CURRENT_THREAD, >est_failures);\<br>
+ GTestExpectFatalFailureHelper::Execute();\<br>
+ }\<br>
+ } while (::testing::internal::AlwaysFalse())<br>
+<br>
+#define EXPECT_FATAL_FAILURE_ON_ALL_THREADS(statement, substr) \<br>
+ do { \<br>
+ class GTestExpectFatalFailureHelper {\<br>
+ public:\<br>
+ static void Execute() { statement; }\<br>
+ };\<br>
+ ::testing::TestPartResultArray gtest_failures;\<br>
+ ::testing::internal::SingleFailureChecker gtest_checker(\<br>
+ >est_failures, ::testing::TestPartResult::kFatalFailure, (substr));\<br>
+ {\<br>
+ ::testing::ScopedFakeTestPartResultReporter gtest_reporter(\<br>
+ ::testing::ScopedFakeTestPartResultReporter:: \<br>
+ INTERCEPT_ALL_THREADS, >est_failures);\<br>
+ GTestExpectFatalFailureHelper::Execute();\<br>
+ }\<br>
+ } while (::testing::internal::AlwaysFalse())<br>
+<br>
+// A macro for testing Google Test assertions or code that's expected to<br>
+// generate Google Test non-fatal failures. It asserts that the given<br>
+// statement will cause exactly one non-fatal Google Test failure with 'substr'<br>
+// being part of the failure message.<br>
+//<br>
+// There are two different versions of this macro. EXPECT_NONFATAL_FAILURE only<br>
+// affects and considers failures generated in the current thread and<br>
+// EXPECT_NONFATAL_FAILURE_ON_ALL_THREADS does the same but for all threads.<br>
+//<br>
+// 'statement' is allowed to reference local variables and members of<br>
+// the current object.<br>
+//<br>
+// The verification of the assertion is done correctly even when the statement<br>
+// throws an exception or aborts the current function.<br>
+//<br>
+// Known restrictions:<br>
+// - You cannot stream a failure message to this macro.<br>
+//<br>
+// Note that even though the implementations of the following two<br>
+// macros are much alike, we cannot refactor them to use a common<br>
+// helper macro, due to some peculiarity in how the preprocessor<br>
+// works. If we do that, the code won't compile when the user gives<br>
+// EXPECT_NONFATAL_FAILURE() a statement that contains a macro that<br>
+// expands to code containing an unprotected comma. The<br>
+// AcceptsMacroThatExpandsToUnprotectedComma test in gtest_unittest.cc<br>
+// catches that.<br>
+//<br>
+// For the same reason, we have to write<br>
+// if (::testing::internal::AlwaysTrue()) { statement; }<br>
+// instead of<br>
+// GTEST_SUPPRESS_UNREACHABLE_CODE_WARNING_BELOW_(statement)<br>
+// to avoid an MSVC warning on unreachable code.<br>
+#define EXPECT_NONFATAL_FAILURE(statement, substr) \<br>
+ do {\<br>
+ ::testing::TestPartResultArray gtest_failures;\<br>
+ ::testing::internal::SingleFailureChecker gtest_checker(\<br>
+ >est_failures, ::testing::TestPartResult::kNonFatalFailure, \<br>
+ (substr));\<br>
+ {\<br>
+ ::testing::ScopedFakeTestPartResultReporter gtest_reporter(\<br>
+ ::testing::ScopedFakeTestPartResultReporter:: \<br>
+ INTERCEPT_ONLY_CURRENT_THREAD, >est_failures);\<br>
+ if (::testing::internal::AlwaysTrue()) { statement; }\<br>
+ }\<br>
+ } while (::testing::internal::AlwaysFalse())<br>
+<br>
+#define EXPECT_NONFATAL_FAILURE_ON_ALL_THREADS(statement, substr) \<br>
+ do {\<br>
+ ::testing::TestPartResultArray gtest_failures;\<br>
+ ::testing::internal::SingleFailureChecker gtest_checker(\<br>
+ >est_failures, ::testing::TestPartResult::kNonFatalFailure, \<br>
+ (substr));\<br>
+ {\<br>
+ ::testing::ScopedFakeTestPartResultReporter gtest_reporter(\<br>
+ ::testing::ScopedFakeTestPartResultReporter::INTERCEPT_ALL_THREADS, \<br>
+ >est_failures);\<br>
+ if (::testing::internal::AlwaysTrue()) { statement; }\<br>
+ }\<br>
+ } while (::testing::internal::AlwaysFalse())<br>
+<br>
+#endif // GTEST_INCLUDE_GTEST_GTEST_SPI_H_</blockquote><div><br></div><div>Works for me.</div><div><br></div><div>Reviewed-by: Chase Douglas <<a href="mailto:chase.douglas@ubuntu.com">chase.douglas@ubuntu.com</a>> </div>
</div></div>