From b2ee82ebf9b8f1be859d08611b768ae6c0700090 Mon Sep 17 00:00:00 2001 From: "zhanyong.wan" Date: Fri, 11 Sep 2009 06:59:42 +0000 Subject: Improves EXPECT_DEATH_IF_SUPPORTED to allow streaming of messages and enforcing the validity of arguments (by Vlad Losev); adds samples for the event listener API (by Vlad Losev); simplifies the tests using EXPECT_DEATH_IF_SUPPORTED (by Zhanyong Wan). --- samples/sample9_unittest.cc | 188 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 188 insertions(+) create mode 100644 samples/sample9_unittest.cc (limited to 'samples/sample9_unittest.cc') diff --git a/samples/sample9_unittest.cc b/samples/sample9_unittest.cc new file mode 100644 index 00000000..50ac9415 --- /dev/null +++ b/samples/sample9_unittest.cc @@ -0,0 +1,188 @@ +// Copyright 2009 Google Inc. All Rights Reserved. +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are +// met: +// +// * Redistributions of source code must retain the above copyright +// notice, this list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above +// copyright notice, this list of conditions and the following disclaimer +// in the documentation and/or other materials provided with the +// distribution. +// * Neither the name of Google Inc. nor the names of its +// contributors may be used to endorse or promote products derived from +// this software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +// +// Author: vladl@google.com (Vlad Losev) + +// This sample shows how to use Google Test listener API to implement +// an alternative console output and how to use the UnitTest reflection API +// to enumerate test cases and tests and to inspect their results. + +#include + +#include + +using ::testing::InitGoogleTest; +using ::testing::Test; +using ::testing::TestInfo; +using ::testing::TestPartResult; +using ::testing::UnitTest; +using ::testing::internal::EmptyTestEventListener; +using ::testing::internal::EventListeners; +using ::testing::internal::TestCase; + +namespace testing { +namespace internal { + +// TODO(vladl@google.com): Get rid of the accessor class once the API is +// published. +class UnitTestAccessor { + public: + static bool Passed(const UnitTest& unit_test) { return unit_test.Passed(); } + static EventListeners& listeners(UnitTest* unit_test) { + return unit_test->listeners(); + } + static int GetTotalTestCaseCount(const UnitTest& unit_test) { + return unit_test.total_test_case_count(); + } + static const TestCase* GetTestCase(const UnitTest& unit_test, int index) { + return unit_test.GetTestCase(index); + } +}; + +} // namespace internal +} // namespace testing + +using ::testing::internal::UnitTestAccessor; + +namespace { + +// Provides alternative output mode which produces minimal amount of +// information about tests. +class TersePrinter : public EmptyTestEventListener { + private: + // Called before any test activity starts. + virtual void OnTestProgramStart(const UnitTest& unit_test) {} + + // Called after all test activities have ended. + virtual void OnTestProgramEnd(const UnitTest& unit_test) { + fprintf(stdout, + "TEST %s\n", + UnitTestAccessor::Passed(unit_test) ? "PASSED" : "FAILED"); + fflush(stdout); + } + + // Called before a test starts. + virtual void OnTestStart(const TestInfo& test_info) { + fprintf(stdout, + "*** Test %s.%s starting.\n", + test_info.test_case_name(), + test_info.name()); + fflush(stdout); + } + + // Called after a test ends. + virtual void OnTestEnd(const TestInfo& test_info) { + fprintf(stdout, + "*** Test %s.%s ending.\n", + test_info.test_case_name(), + test_info.name()); + fflush(stdout); + } + + // Called after a failed assertion or a SUCCESS(). + virtual void OnNewTestPartResult(const TestPartResult& test_part_result) { + fprintf(stdout, + "%s in %s:%d\n%s\n", + test_part_result.failed() ? "*** Failure" : "Success", + test_part_result.file_name(), + test_part_result.line_number(), + test_part_result.summary()); + fflush(stdout); + } +}; // class TersePrinter + +TEST(CustomOutputTest, PrintsMessage) { + printf("Printing something from the test body...\n"); +} + +TEST(CustomOutputTest, Succeeds) { + SUCCEED() << "SUCCEED() has been invoked from here"; +} + +TEST(CustomOutputTest, Fails) { + EXPECT_EQ(1, 2) + << "This test fails in order to demonstrate alternative failure messages"; +} + +} // namespace + +int main(int argc, char **argv) { + InitGoogleTest(&argc, argv); + + bool terse_output = false; + if (argc > 1 && strcmp(argv[1], "--terse_output") == 0 ) + terse_output = true; + else + printf("%s\n", "Run this program with --terse_output to change the way " + "it prints its output."); + + // If we are given the --terse_output command line flag, suppresses the + // standard output and attaches own result printer. + if (terse_output) { + EventListeners& listeners = UnitTestAccessor::listeners( + UnitTest::GetInstance()); + + // Removes the default console output listener from the list so it will + // not receive events from Google Test and won't print any output. Since + // this operation transfers ownership of the listener to the caller we + // have to delete it as well. + delete listeners.Release(listeners.default_result_printer()); + + // Adds the custom output listener to the list. It will now receive + // events from Google Test and print the alternative output. We don't + // have to worry about deleting it since Google Test assumes ownership + // over it after adding it to the list. + listeners.Append(new TersePrinter); + } + int ret_val = RUN_ALL_TESTS(); + + // This is an example of using the UnitTest reflection API to inspect test + // results. Here we discount failures from the tests we expected to fail. + int unexpectedly_failed_tests = 0; + for (int i = 0; + i < UnitTestAccessor::GetTotalTestCaseCount(*UnitTest::GetInstance()); + ++i) { + const TestCase* test_case = UnitTestAccessor::GetTestCase( + *UnitTest::GetInstance(), i); + for (int j = 0; j < test_case->total_test_count(); ++j) { + const TestInfo* test_info = test_case->GetTestInfo(j); + // Counts failed tests that were not meant to fail (those without + // 'Fails' in the name). + if (test_info->result()->Failed() && + strcmp(test_info->name(), "Fails") != 0) { + unexpectedly_failed_tests++; + } + } + } + + // Test that were meant to fail should not affect the test program outcome. + if (unexpectedly_failed_tests == 0) + ret_val = 0; + + return ret_val; +} -- cgit v1.2.3 From f43e4ff3ad12ace9d423cc3ce02feadb8f24fe67 Mon Sep 17 00:00:00 2001 From: "zhanyong.wan" Date: Thu, 17 Sep 2009 19:12:30 +0000 Subject: Renames the methods in the event listener API, and changes the order of *End events (by Vlad Losev). --- samples/sample9_unittest.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'samples/sample9_unittest.cc') diff --git a/samples/sample9_unittest.cc b/samples/sample9_unittest.cc index 50ac9415..8ef70c7c 100644 --- a/samples/sample9_unittest.cc +++ b/samples/sample9_unittest.cc @@ -105,7 +105,7 @@ class TersePrinter : public EmptyTestEventListener { } // Called after a failed assertion or a SUCCESS(). - virtual void OnNewTestPartResult(const TestPartResult& test_part_result) { + virtual void OnTestPartResult(const TestPartResult& test_part_result) { fprintf(stdout, "%s in %s:%d\n%s\n", test_part_result.failed() ? "*** Failure" : "Success", -- cgit v1.2.3 From e5373af0cb9cae249e7bc618cae3483397332895 Mon Sep 17 00:00:00 2001 From: "zhanyong.wan" Date: Fri, 18 Sep 2009 18:16:20 +0000 Subject: Renames the TestPartResult type enums and adjusts the order of methods in the event listener interface (by Vlad Losev). --- samples/sample9_unittest.cc | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) (limited to 'samples/sample9_unittest.cc') diff --git a/samples/sample9_unittest.cc b/samples/sample9_unittest.cc index 8ef70c7c..82297516 100644 --- a/samples/sample9_unittest.cc +++ b/samples/sample9_unittest.cc @@ -95,15 +95,6 @@ class TersePrinter : public EmptyTestEventListener { fflush(stdout); } - // Called after a test ends. - virtual void OnTestEnd(const TestInfo& test_info) { - fprintf(stdout, - "*** Test %s.%s ending.\n", - test_info.test_case_name(), - test_info.name()); - fflush(stdout); - } - // Called after a failed assertion or a SUCCESS(). virtual void OnTestPartResult(const TestPartResult& test_part_result) { fprintf(stdout, @@ -114,6 +105,15 @@ class TersePrinter : public EmptyTestEventListener { test_part_result.summary()); fflush(stdout); } + + // Called after a test ends. + virtual void OnTestEnd(const TestInfo& test_info) { + fprintf(stdout, + "*** Test %s.%s ending.\n", + test_info.test_case_name(), + test_info.name()); + fflush(stdout); + } }; // class TersePrinter TEST(CustomOutputTest, PrintsMessage) { -- cgit v1.2.3 From b50ef44a3527d958270ff1f08cb99e3ac633bd17 Mon Sep 17 00:00:00 2001 From: "zhanyong.wan" Date: Thu, 24 Sep 2009 21:15:59 +0000 Subject: Publishes the even listener API (by Vlad Losev); adds OS-indicating macros to simplify gtest code (by Zhanyong Wan). --- samples/sample9_unittest.cc | 54 +++++++++++---------------------------------- 1 file changed, 13 insertions(+), 41 deletions(-) (limited to 'samples/sample9_unittest.cc') diff --git a/samples/sample9_unittest.cc b/samples/sample9_unittest.cc index 82297516..9bf865ec 100644 --- a/samples/sample9_unittest.cc +++ b/samples/sample9_unittest.cc @@ -36,38 +36,14 @@ #include +using ::testing::EmptyTestEventListener; +using ::testing::EventListeners; using ::testing::InitGoogleTest; using ::testing::Test; +using ::testing::TestCase; using ::testing::TestInfo; using ::testing::TestPartResult; using ::testing::UnitTest; -using ::testing::internal::EmptyTestEventListener; -using ::testing::internal::EventListeners; -using ::testing::internal::TestCase; - -namespace testing { -namespace internal { - -// TODO(vladl@google.com): Get rid of the accessor class once the API is -// published. -class UnitTestAccessor { - public: - static bool Passed(const UnitTest& unit_test) { return unit_test.Passed(); } - static EventListeners& listeners(UnitTest* unit_test) { - return unit_test->listeners(); - } - static int GetTotalTestCaseCount(const UnitTest& unit_test) { - return unit_test.total_test_case_count(); - } - static const TestCase* GetTestCase(const UnitTest& unit_test, int index) { - return unit_test.GetTestCase(index); - } -}; - -} // namespace internal -} // namespace testing - -using ::testing::internal::UnitTestAccessor; namespace { @@ -80,9 +56,7 @@ class TersePrinter : public EmptyTestEventListener { // Called after all test activities have ended. virtual void OnTestProgramEnd(const UnitTest& unit_test) { - fprintf(stdout, - "TEST %s\n", - UnitTestAccessor::Passed(unit_test) ? "PASSED" : "FAILED"); + fprintf(stdout, "TEST %s\n", unit_test.Passed() ? "PASSED" : "FAILED"); fflush(stdout); } @@ -141,11 +115,12 @@ int main(int argc, char **argv) { printf("%s\n", "Run this program with --terse_output to change the way " "it prints its output."); + UnitTest& unit_test = *UnitTest::GetInstance(); + // If we are given the --terse_output command line flag, suppresses the // standard output and attaches own result printer. if (terse_output) { - EventListeners& listeners = UnitTestAccessor::listeners( - UnitTest::GetInstance()); + EventListeners& listeners = unit_test.listeners(); // Removes the default console output listener from the list so it will // not receive events from Google Test and won't print any output. Since @@ -164,17 +139,14 @@ int main(int argc, char **argv) { // This is an example of using the UnitTest reflection API to inspect test // results. Here we discount failures from the tests we expected to fail. int unexpectedly_failed_tests = 0; - for (int i = 0; - i < UnitTestAccessor::GetTotalTestCaseCount(*UnitTest::GetInstance()); - ++i) { - const TestCase* test_case = UnitTestAccessor::GetTestCase( - *UnitTest::GetInstance(), i); - for (int j = 0; j < test_case->total_test_count(); ++j) { - const TestInfo* test_info = test_case->GetTestInfo(j); + for (int i = 0; i < unit_test.total_test_case_count(); ++i) { + const TestCase& test_case = *unit_test.GetTestCase(i); + for (int j = 0; j < test_case.total_test_count(); ++j) { + const TestInfo& test_info = *test_case.GetTestInfo(j); // Counts failed tests that were not meant to fail (those without // 'Fails' in the name). - if (test_info->result()->Failed() && - strcmp(test_info->name(), "Fails") != 0) { + if (test_info.result()->Failed() && + strcmp(test_info.name(), "Fails") != 0) { unexpectedly_failed_tests++; } } -- cgit v1.2.3 From f8b268ee86ca74bba3276352f1e7de53d1336c3e Mon Sep 17 00:00:00 2001 From: "zhanyong.wan" Date: Wed, 30 Sep 2009 20:23:50 +0000 Subject: Makes gtest compile cleanly with MSVC's /W4 (by Zhanyong Wan). Renames EventListenrs to TestEventListeners (by Zhanyong Wan). Fixes invalid characters in XML report (by Vlad Losev). Refacotrs SConscript (by Vlad Losev). --- samples/sample9_unittest.cc | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'samples/sample9_unittest.cc') diff --git a/samples/sample9_unittest.cc b/samples/sample9_unittest.cc index 9bf865ec..8944c47b 100644 --- a/samples/sample9_unittest.cc +++ b/samples/sample9_unittest.cc @@ -37,10 +37,10 @@ #include using ::testing::EmptyTestEventListener; -using ::testing::EventListeners; using ::testing::InitGoogleTest; using ::testing::Test; using ::testing::TestCase; +using ::testing::TestEventListeners; using ::testing::TestInfo; using ::testing::TestPartResult; using ::testing::UnitTest; @@ -120,7 +120,7 @@ int main(int argc, char **argv) { // If we are given the --terse_output command line flag, suppresses the // standard output and attaches own result printer. if (terse_output) { - EventListeners& listeners = unit_test.listeners(); + TestEventListeners& listeners = unit_test.listeners(); // Removes the default console output listener from the list so it will // not receive events from Google Test and won't print any output. Since -- cgit v1.2.3 From 1d6df4be08e71c8a9d600799cc17bfd7d62838fc Mon Sep 17 00:00:00 2001 From: "zhanyong.wan" Date: Tue, 29 Dec 2009 19:45:33 +0000 Subject: Adds an experimental CMake build script; makes the samples compile without warnings on Windows. --- samples/sample9_unittest.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'samples/sample9_unittest.cc') diff --git a/samples/sample9_unittest.cc b/samples/sample9_unittest.cc index 8944c47b..d828ef4d 100644 --- a/samples/sample9_unittest.cc +++ b/samples/sample9_unittest.cc @@ -52,7 +52,7 @@ namespace { class TersePrinter : public EmptyTestEventListener { private: // Called before any test activity starts. - virtual void OnTestProgramStart(const UnitTest& unit_test) {} + virtual void OnTestProgramStart(const UnitTest& /* unit_test */) {} // Called after all test activities have ended. virtual void OnTestProgramEnd(const UnitTest& unit_test) { -- cgit v1.2.3 From 38e1465902692b70ed11f670c8d335dbded5522f Mon Sep 17 00:00:00 2001 From: vladlosev Date: Mon, 31 May 2010 23:30:01 +0000 Subject: Fixes a wrong comment for OnTestPartResult(). --- samples/sample9_unittest.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'samples/sample9_unittest.cc') diff --git a/samples/sample9_unittest.cc b/samples/sample9_unittest.cc index d828ef4d..37726300 100644 --- a/samples/sample9_unittest.cc +++ b/samples/sample9_unittest.cc @@ -69,7 +69,7 @@ class TersePrinter : public EmptyTestEventListener { fflush(stdout); } - // Called after a failed assertion or a SUCCESS(). + // Called after a failed assertion or a SUCCEED() invocation. virtual void OnTestPartResult(const TestPartResult& test_part_result) { fprintf(stdout, "%s in %s:%d\n%s\n", -- cgit v1.2.3 From dac3e879c56a50696a36f53e1e5e353e48fa665f Mon Sep 17 00:00:00 2001 From: "zhanyong.wan" Date: Tue, 14 Sep 2010 05:35:59 +0000 Subject: Include gtest headers as user headers instead of system headers. --- samples/sample9_unittest.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'samples/sample9_unittest.cc') diff --git a/samples/sample9_unittest.cc b/samples/sample9_unittest.cc index 37726300..b2e2079b 100644 --- a/samples/sample9_unittest.cc +++ b/samples/sample9_unittest.cc @@ -34,7 +34,7 @@ #include -#include +#include "gtest/gtest.h" using ::testing::EmptyTestEventListener; using ::testing::InitGoogleTest; -- cgit v1.2.3