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/sample10_unittest.cc | 165 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 165 insertions(+) create mode 100644 samples/sample10_unittest.cc (limited to 'samples/sample10_unittest.cc') diff --git a/samples/sample10_unittest.cc b/samples/sample10_unittest.cc new file mode 100644 index 00000000..8cb958f6 --- /dev/null +++ b/samples/sample10_unittest.cc @@ -0,0 +1,165 @@ +// 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 +// a primitive leak checker. + +#include +#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(); + } + +}; + +} // namespace internal +} // namespace testing + +using ::testing::internal::UnitTestAccessor; + +namespace { + +// We will track memory used by this class. +class Water { + public: + // Normal Water declarations go here. + + // operator new and operator delete help us control water allocation. + void* operator new(size_t allocation_size) { + allocated_++; + return malloc(allocation_size); + } + + void operator delete(void* block, size_t allocation_size) { + allocated_--; + free(block); + } + + static int allocated() { return allocated_; } + + private: + static int allocated_; +}; + +int Water::allocated_ = 0; + +// This event listener monitors how many Water objects are created and +// destroyed by each test, and reports a failure if a test leaks some Water +// objects. It does this by comparing the number of live Water objects at +// the beginning of a test and at the end of a test. +class LeakChecker : public EmptyTestEventListener { + private: + // Called before a test starts. + virtual void OnTestStart(const TestInfo& test_info) { + initially_allocated_ = Water::allocated(); + } + + // Called after a test ends. + virtual void OnTestEnd(const TestInfo& test_info) { + int difference = Water::allocated() - initially_allocated_; + + // You can generate a failure in any event handler except + // OnTestPartResult. Just use an appropriate Google Test assertion to do + // it. + EXPECT_TRUE(difference <= 0) + << "Leaked " << difference << " unit(s) of Water!"; + } + + int initially_allocated_; +}; + +TEST(ListenersTest, DoesNotLeak) { + Water* water = new Water; + delete water; +} + +// This should fail when the --check_for_leaks command line flag is +// specified. +TEST(ListenersTest, LeaksWater) { + Water* water = new Water; + EXPECT_TRUE(water != NULL); +} + +} // namespace + +int main(int argc, char **argv) { + InitGoogleTest(&argc, argv); + + bool check_for_leaks = false; + if (argc > 1 && strcmp(argv[1], "--check_for_leaks") == 0 ) + check_for_leaks = true; + else + printf("%s\n", "Run this program with --check_for_leaks to enable " + "custom leak checking in the tests."); + + // If we are given the --check_for_leaks command line flag, installs the + // leak checker. + if (check_for_leaks) { + EventListeners& listeners = UnitTestAccessor::listeners( + UnitTest::GetInstance()); + + // Adds the leak checker to the end of the test event listener list, + // after the default text output printer and the default XML report + // generator. + // + // The order is important - it ensures that failures generated in the + // leak checker's OnTestEnd() method are processed by the text and XML + // printers *before* their OnTestEnd() methods are called, such that + // they are attributed to the right test. Remember that a listener + // receives an OnXyzStart event *after* listeners preceding it in the + // list received that event, and receives an OnXyzEnd event *before* + // listeners preceding it. + // + // We don't need to worry about deleting the new listener later, as + // Google Test will do it. + listeners.Append(new LeakChecker); + } + return RUN_ALL_TESTS(); +} -- 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/sample10_unittest.cc | 28 ++++------------------------ 1 file changed, 4 insertions(+), 24 deletions(-) (limited to 'samples/sample10_unittest.cc') diff --git a/samples/sample10_unittest.cc b/samples/sample10_unittest.cc index 8cb958f6..e1368596 100644 --- a/samples/sample10_unittest.cc +++ b/samples/sample10_unittest.cc @@ -36,33 +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(); - } - -}; - -} // namespace internal -} // namespace testing - -using ::testing::internal::UnitTestAccessor; namespace { @@ -142,8 +123,7 @@ int main(int argc, char **argv) { // If we are given the --check_for_leaks command line flag, installs the // leak checker. if (check_for_leaks) { - EventListeners& listeners = UnitTestAccessor::listeners( - UnitTest::GetInstance()); + EventListeners& listeners = UnitTest::GetInstance()->listeners(); // Adds the leak checker to the end of the test event listener list, // after the default text output printer and the default XML report -- 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/sample10_unittest.cc | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'samples/sample10_unittest.cc') diff --git a/samples/sample10_unittest.cc b/samples/sample10_unittest.cc index e1368596..703ec6ee 100644 --- a/samples/sample10_unittest.cc +++ b/samples/sample10_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; @@ -123,7 +123,7 @@ int main(int argc, char **argv) { // If we are given the --check_for_leaks command line flag, installs the // leak checker. if (check_for_leaks) { - EventListeners& listeners = UnitTest::GetInstance()->listeners(); + TestEventListeners& listeners = UnitTest::GetInstance()->listeners(); // Adds the leak checker to the end of the test event listener list, // after the default text output printer and the default XML report -- 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/sample10_unittest.cc | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'samples/sample10_unittest.cc') diff --git a/samples/sample10_unittest.cc b/samples/sample10_unittest.cc index 703ec6ee..3ad6fd65 100644 --- a/samples/sample10_unittest.cc +++ b/samples/sample10_unittest.cc @@ -58,7 +58,7 @@ class Water { return malloc(allocation_size); } - void operator delete(void* block, size_t allocation_size) { + void operator delete(void* block, size_t /* allocation_size */) { allocated_--; free(block); } @@ -78,12 +78,12 @@ int Water::allocated_ = 0; class LeakChecker : public EmptyTestEventListener { private: // Called before a test starts. - virtual void OnTestStart(const TestInfo& test_info) { + virtual void OnTestStart(const TestInfo& /* test_info */) { initially_allocated_ = Water::allocated(); } // Called after a test ends. - virtual void OnTestEnd(const TestInfo& test_info) { + virtual void OnTestEnd(const TestInfo& /* test_info */) { int difference = Water::allocated() - initially_allocated_; // You can generate a failure in any event handler except -- 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/sample10_unittest.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'samples/sample10_unittest.cc') diff --git a/samples/sample10_unittest.cc b/samples/sample10_unittest.cc index 3ad6fd65..2813d040 100644 --- a/samples/sample10_unittest.cc +++ b/samples/sample10_unittest.cc @@ -34,7 +34,7 @@ #include #include -#include +#include "gtest/gtest.h" using ::testing::EmptyTestEventListener; using ::testing::InitGoogleTest; -- cgit v1.2.3 From 8965a6a0d2165f32e6413594bba6367f271f51e7 Mon Sep 17 00:00:00 2001 From: vladlosev Date: Fri, 4 Nov 2011 17:56:23 +0000 Subject: Improves conformance to the Google C++ Style Guide (by Greg Miller). --- samples/sample10_unittest.cc | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) (limited to 'samples/sample10_unittest.cc') diff --git a/samples/sample10_unittest.cc b/samples/sample10_unittest.cc index 2813d040..0051cd5d 100644 --- a/samples/sample10_unittest.cc +++ b/samples/sample10_unittest.cc @@ -89,8 +89,7 @@ class LeakChecker : public EmptyTestEventListener { // You can generate a failure in any event handler except // OnTestPartResult. Just use an appropriate Google Test assertion to do // it. - EXPECT_TRUE(difference <= 0) - << "Leaked " << difference << " unit(s) of Water!"; + EXPECT_LE(difference, 0) << "Leaked " << difference << " unit(s) of Water!"; } int initially_allocated_; -- cgit v1.2.3