gtest.h revision 1b362b15af34006e6a11974088a46d42b903418e
11b362b15af34006e6a11974088a46d42b903418eJohann// Copyright 2005, Google Inc.
21b362b15af34006e6a11974088a46d42b903418eJohann// All rights reserved.
31b362b15af34006e6a11974088a46d42b903418eJohann//
41b362b15af34006e6a11974088a46d42b903418eJohann// Redistribution and use in source and binary forms, with or without
51b362b15af34006e6a11974088a46d42b903418eJohann// modification, are permitted provided that the following conditions are
61b362b15af34006e6a11974088a46d42b903418eJohann// met:
71b362b15af34006e6a11974088a46d42b903418eJohann//
81b362b15af34006e6a11974088a46d42b903418eJohann//     * Redistributions of source code must retain the above copyright
91b362b15af34006e6a11974088a46d42b903418eJohann// notice, this list of conditions and the following disclaimer.
101b362b15af34006e6a11974088a46d42b903418eJohann//     * Redistributions in binary form must reproduce the above
111b362b15af34006e6a11974088a46d42b903418eJohann// copyright notice, this list of conditions and the following disclaimer
121b362b15af34006e6a11974088a46d42b903418eJohann// in the documentation and/or other materials provided with the
131b362b15af34006e6a11974088a46d42b903418eJohann// distribution.
141b362b15af34006e6a11974088a46d42b903418eJohann//     * Neither the name of Google Inc. nor the names of its
151b362b15af34006e6a11974088a46d42b903418eJohann// contributors may be used to endorse or promote products derived from
161b362b15af34006e6a11974088a46d42b903418eJohann// this software without specific prior written permission.
171b362b15af34006e6a11974088a46d42b903418eJohann//
181b362b15af34006e6a11974088a46d42b903418eJohann// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
191b362b15af34006e6a11974088a46d42b903418eJohann// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
201b362b15af34006e6a11974088a46d42b903418eJohann// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
211b362b15af34006e6a11974088a46d42b903418eJohann// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
221b362b15af34006e6a11974088a46d42b903418eJohann// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
231b362b15af34006e6a11974088a46d42b903418eJohann// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
241b362b15af34006e6a11974088a46d42b903418eJohann// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
251b362b15af34006e6a11974088a46d42b903418eJohann// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
261b362b15af34006e6a11974088a46d42b903418eJohann// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
271b362b15af34006e6a11974088a46d42b903418eJohann// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
281b362b15af34006e6a11974088a46d42b903418eJohann// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
291b362b15af34006e6a11974088a46d42b903418eJohann//
301b362b15af34006e6a11974088a46d42b903418eJohann// Author: wan@google.com (Zhanyong Wan)
311b362b15af34006e6a11974088a46d42b903418eJohann//
321b362b15af34006e6a11974088a46d42b903418eJohann// The Google C++ Testing Framework (Google Test)
331b362b15af34006e6a11974088a46d42b903418eJohann//
341b362b15af34006e6a11974088a46d42b903418eJohann// This header file defines the public API for Google Test.  It should be
351b362b15af34006e6a11974088a46d42b903418eJohann// included by any test program that uses Google Test.
361b362b15af34006e6a11974088a46d42b903418eJohann//
371b362b15af34006e6a11974088a46d42b903418eJohann// IMPORTANT NOTE: Due to limitation of the C++ language, we have to
381b362b15af34006e6a11974088a46d42b903418eJohann// leave some internal implementation details in this header file.
391b362b15af34006e6a11974088a46d42b903418eJohann// They are clearly marked by comments like this:
401b362b15af34006e6a11974088a46d42b903418eJohann//
411b362b15af34006e6a11974088a46d42b903418eJohann//   // INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM.
421b362b15af34006e6a11974088a46d42b903418eJohann//
431b362b15af34006e6a11974088a46d42b903418eJohann// Such code is NOT meant to be used by a user directly, and is subject
441b362b15af34006e6a11974088a46d42b903418eJohann// to CHANGE WITHOUT NOTICE.  Therefore DO NOT DEPEND ON IT in a user
451b362b15af34006e6a11974088a46d42b903418eJohann// program!
461b362b15af34006e6a11974088a46d42b903418eJohann//
471b362b15af34006e6a11974088a46d42b903418eJohann// Acknowledgment: Google Test borrowed the idea of automatic test
481b362b15af34006e6a11974088a46d42b903418eJohann// registration from Barthelemy Dagenais' (barthelemy@prologique.com)
491b362b15af34006e6a11974088a46d42b903418eJohann// easyUnit framework.
501b362b15af34006e6a11974088a46d42b903418eJohann
511b362b15af34006e6a11974088a46d42b903418eJohann#ifndef GTEST_INCLUDE_GTEST_GTEST_H_
521b362b15af34006e6a11974088a46d42b903418eJohann#define GTEST_INCLUDE_GTEST_GTEST_H_
531b362b15af34006e6a11974088a46d42b903418eJohann
541b362b15af34006e6a11974088a46d42b903418eJohann#include <limits>
551b362b15af34006e6a11974088a46d42b903418eJohann#include <vector>
561b362b15af34006e6a11974088a46d42b903418eJohann
571b362b15af34006e6a11974088a46d42b903418eJohann#include "gtest/internal/gtest-internal.h"
581b362b15af34006e6a11974088a46d42b903418eJohann#include "gtest/internal/gtest-string.h"
591b362b15af34006e6a11974088a46d42b903418eJohann#include "gtest/gtest-death-test.h"
601b362b15af34006e6a11974088a46d42b903418eJohann#include "gtest/gtest-message.h"
611b362b15af34006e6a11974088a46d42b903418eJohann#include "gtest/gtest-param-test.h"
621b362b15af34006e6a11974088a46d42b903418eJohann#include "gtest/gtest-printers.h"
631b362b15af34006e6a11974088a46d42b903418eJohann#include "gtest/gtest_prod.h"
641b362b15af34006e6a11974088a46d42b903418eJohann#include "gtest/gtest-test-part.h"
651b362b15af34006e6a11974088a46d42b903418eJohann#include "gtest/gtest-typed-test.h"
661b362b15af34006e6a11974088a46d42b903418eJohann
671b362b15af34006e6a11974088a46d42b903418eJohann// Depending on the platform, different string classes are available.
681b362b15af34006e6a11974088a46d42b903418eJohann// On Linux, in addition to ::std::string, Google also makes use of
691b362b15af34006e6a11974088a46d42b903418eJohann// class ::string, which has the same interface as ::std::string, but
701b362b15af34006e6a11974088a46d42b903418eJohann// has a different implementation.
711b362b15af34006e6a11974088a46d42b903418eJohann//
721b362b15af34006e6a11974088a46d42b903418eJohann// The user can define GTEST_HAS_GLOBAL_STRING to 1 to indicate that
731b362b15af34006e6a11974088a46d42b903418eJohann// ::string is available AND is a distinct type to ::std::string, or
741b362b15af34006e6a11974088a46d42b903418eJohann// define it to 0 to indicate otherwise.
751b362b15af34006e6a11974088a46d42b903418eJohann//
761b362b15af34006e6a11974088a46d42b903418eJohann// If the user's ::std::string and ::string are the same class due to
771b362b15af34006e6a11974088a46d42b903418eJohann// aliasing, he should define GTEST_HAS_GLOBAL_STRING to 0.
781b362b15af34006e6a11974088a46d42b903418eJohann//
791b362b15af34006e6a11974088a46d42b903418eJohann// If the user doesn't define GTEST_HAS_GLOBAL_STRING, it is defined
801b362b15af34006e6a11974088a46d42b903418eJohann// heuristically.
811b362b15af34006e6a11974088a46d42b903418eJohann
821b362b15af34006e6a11974088a46d42b903418eJohannnamespace testing {
831b362b15af34006e6a11974088a46d42b903418eJohann
841b362b15af34006e6a11974088a46d42b903418eJohann// Declares the flags.
851b362b15af34006e6a11974088a46d42b903418eJohann
861b362b15af34006e6a11974088a46d42b903418eJohann// This flag temporary enables the disabled tests.
871b362b15af34006e6a11974088a46d42b903418eJohannGTEST_DECLARE_bool_(also_run_disabled_tests);
881b362b15af34006e6a11974088a46d42b903418eJohann
891b362b15af34006e6a11974088a46d42b903418eJohann// This flag brings the debugger on an assertion failure.
901b362b15af34006e6a11974088a46d42b903418eJohannGTEST_DECLARE_bool_(break_on_failure);
911b362b15af34006e6a11974088a46d42b903418eJohann
921b362b15af34006e6a11974088a46d42b903418eJohann// This flag controls whether Google Test catches all test-thrown exceptions
931b362b15af34006e6a11974088a46d42b903418eJohann// and logs them as failures.
941b362b15af34006e6a11974088a46d42b903418eJohannGTEST_DECLARE_bool_(catch_exceptions);
951b362b15af34006e6a11974088a46d42b903418eJohann
961b362b15af34006e6a11974088a46d42b903418eJohann// This flag enables using colors in terminal output. Available values are
971b362b15af34006e6a11974088a46d42b903418eJohann// "yes" to enable colors, "no" (disable colors), or "auto" (the default)
981b362b15af34006e6a11974088a46d42b903418eJohann// to let Google Test decide.
991b362b15af34006e6a11974088a46d42b903418eJohannGTEST_DECLARE_string_(color);
1001b362b15af34006e6a11974088a46d42b903418eJohann
1011b362b15af34006e6a11974088a46d42b903418eJohann// This flag sets up the filter to select by name using a glob pattern
1021b362b15af34006e6a11974088a46d42b903418eJohann// the tests to run. If the filter is not given all tests are executed.
1031b362b15af34006e6a11974088a46d42b903418eJohannGTEST_DECLARE_string_(filter);
1041b362b15af34006e6a11974088a46d42b903418eJohann
1051b362b15af34006e6a11974088a46d42b903418eJohann// This flag causes the Google Test to list tests. None of the tests listed
1061b362b15af34006e6a11974088a46d42b903418eJohann// are actually run if the flag is provided.
1071b362b15af34006e6a11974088a46d42b903418eJohannGTEST_DECLARE_bool_(list_tests);
1081b362b15af34006e6a11974088a46d42b903418eJohann
1091b362b15af34006e6a11974088a46d42b903418eJohann// This flag controls whether Google Test emits a detailed XML report to a file
1101b362b15af34006e6a11974088a46d42b903418eJohann// in addition to its normal textual output.
1111b362b15af34006e6a11974088a46d42b903418eJohannGTEST_DECLARE_string_(output);
1121b362b15af34006e6a11974088a46d42b903418eJohann
1131b362b15af34006e6a11974088a46d42b903418eJohann// This flags control whether Google Test prints the elapsed time for each
1141b362b15af34006e6a11974088a46d42b903418eJohann// test.
1151b362b15af34006e6a11974088a46d42b903418eJohannGTEST_DECLARE_bool_(print_time);
1161b362b15af34006e6a11974088a46d42b903418eJohann
1171b362b15af34006e6a11974088a46d42b903418eJohann// This flag specifies the random number seed.
1181b362b15af34006e6a11974088a46d42b903418eJohannGTEST_DECLARE_int32_(random_seed);
1191b362b15af34006e6a11974088a46d42b903418eJohann
1201b362b15af34006e6a11974088a46d42b903418eJohann// This flag sets how many times the tests are repeated. The default value
1211b362b15af34006e6a11974088a46d42b903418eJohann// is 1. If the value is -1 the tests are repeating forever.
1221b362b15af34006e6a11974088a46d42b903418eJohannGTEST_DECLARE_int32_(repeat);
1231b362b15af34006e6a11974088a46d42b903418eJohann
1241b362b15af34006e6a11974088a46d42b903418eJohann// This flag controls whether Google Test includes Google Test internal
1251b362b15af34006e6a11974088a46d42b903418eJohann// stack frames in failure stack traces.
1261b362b15af34006e6a11974088a46d42b903418eJohannGTEST_DECLARE_bool_(show_internal_stack_frames);
1271b362b15af34006e6a11974088a46d42b903418eJohann
1281b362b15af34006e6a11974088a46d42b903418eJohann// When this flag is specified, tests' order is randomized on every iteration.
1291b362b15af34006e6a11974088a46d42b903418eJohannGTEST_DECLARE_bool_(shuffle);
1301b362b15af34006e6a11974088a46d42b903418eJohann
1311b362b15af34006e6a11974088a46d42b903418eJohann// This flag specifies the maximum number of stack frames to be
1321b362b15af34006e6a11974088a46d42b903418eJohann// printed in a failure message.
1331b362b15af34006e6a11974088a46d42b903418eJohannGTEST_DECLARE_int32_(stack_trace_depth);
1341b362b15af34006e6a11974088a46d42b903418eJohann
1351b362b15af34006e6a11974088a46d42b903418eJohann// When this flag is specified, a failed assertion will throw an
1361b362b15af34006e6a11974088a46d42b903418eJohann// exception if exceptions are enabled, or exit the program with a
1371b362b15af34006e6a11974088a46d42b903418eJohann// non-zero code otherwise.
1381b362b15af34006e6a11974088a46d42b903418eJohannGTEST_DECLARE_bool_(throw_on_failure);
1391b362b15af34006e6a11974088a46d42b903418eJohann
1401b362b15af34006e6a11974088a46d42b903418eJohann// When this flag is set with a "host:port" string, on supported
1411b362b15af34006e6a11974088a46d42b903418eJohann// platforms test results are streamed to the specified port on
1421b362b15af34006e6a11974088a46d42b903418eJohann// the specified host machine.
1431b362b15af34006e6a11974088a46d42b903418eJohannGTEST_DECLARE_string_(stream_result_to);
1441b362b15af34006e6a11974088a46d42b903418eJohann
1451b362b15af34006e6a11974088a46d42b903418eJohann// The upper limit for valid stack trace depths.
1461b362b15af34006e6a11974088a46d42b903418eJohannconst int kMaxStackTraceDepth = 100;
1471b362b15af34006e6a11974088a46d42b903418eJohann
1481b362b15af34006e6a11974088a46d42b903418eJohannnamespace internal {
1491b362b15af34006e6a11974088a46d42b903418eJohann
1501b362b15af34006e6a11974088a46d42b903418eJohannclass AssertHelper;
1511b362b15af34006e6a11974088a46d42b903418eJohannclass DefaultGlobalTestPartResultReporter;
1521b362b15af34006e6a11974088a46d42b903418eJohannclass ExecDeathTest;
1531b362b15af34006e6a11974088a46d42b903418eJohannclass NoExecDeathTest;
1541b362b15af34006e6a11974088a46d42b903418eJohannclass FinalSuccessChecker;
1551b362b15af34006e6a11974088a46d42b903418eJohannclass GTestFlagSaver;
1561b362b15af34006e6a11974088a46d42b903418eJohannclass TestResultAccessor;
1571b362b15af34006e6a11974088a46d42b903418eJohannclass TestEventListenersAccessor;
1581b362b15af34006e6a11974088a46d42b903418eJohannclass TestEventRepeater;
1591b362b15af34006e6a11974088a46d42b903418eJohannclass WindowsDeathTest;
1601b362b15af34006e6a11974088a46d42b903418eJohannclass UnitTestImpl* GetUnitTestImpl();
1611b362b15af34006e6a11974088a46d42b903418eJohannvoid ReportFailureInUnknownLocation(TestPartResult::Type result_type,
1621b362b15af34006e6a11974088a46d42b903418eJohann                                    const String& message);
1631b362b15af34006e6a11974088a46d42b903418eJohann
1641b362b15af34006e6a11974088a46d42b903418eJohann// Converts a streamable value to a String.  A NULL pointer is
1651b362b15af34006e6a11974088a46d42b903418eJohann// converted to "(null)".  When the input value is a ::string,
1661b362b15af34006e6a11974088a46d42b903418eJohann// ::std::string, ::wstring, or ::std::wstring object, each NUL
1671b362b15af34006e6a11974088a46d42b903418eJohann// character in it is replaced with "\\0".
1681b362b15af34006e6a11974088a46d42b903418eJohann// Declared in gtest-internal.h but defined here, so that it has access
1691b362b15af34006e6a11974088a46d42b903418eJohann// to the definition of the Message class, required by the ARM
1701b362b15af34006e6a11974088a46d42b903418eJohann// compiler.
1711b362b15af34006e6a11974088a46d42b903418eJohanntemplate <typename T>
1721b362b15af34006e6a11974088a46d42b903418eJohannString StreamableToString(const T& streamable) {
1731b362b15af34006e6a11974088a46d42b903418eJohann  return (Message() << streamable).GetString();
1741b362b15af34006e6a11974088a46d42b903418eJohann}
1751b362b15af34006e6a11974088a46d42b903418eJohann
1761b362b15af34006e6a11974088a46d42b903418eJohann}  // namespace internal
1771b362b15af34006e6a11974088a46d42b903418eJohann
1781b362b15af34006e6a11974088a46d42b903418eJohann// The friend relationship of some of these classes is cyclic.
1791b362b15af34006e6a11974088a46d42b903418eJohann// If we don't forward declare them the compiler might confuse the classes
1801b362b15af34006e6a11974088a46d42b903418eJohann// in friendship clauses with same named classes on the scope.
1811b362b15af34006e6a11974088a46d42b903418eJohannclass Test;
1821b362b15af34006e6a11974088a46d42b903418eJohannclass TestCase;
1831b362b15af34006e6a11974088a46d42b903418eJohannclass TestInfo;
1841b362b15af34006e6a11974088a46d42b903418eJohannclass UnitTest;
1851b362b15af34006e6a11974088a46d42b903418eJohann
1861b362b15af34006e6a11974088a46d42b903418eJohann// A class for indicating whether an assertion was successful.  When
1871b362b15af34006e6a11974088a46d42b903418eJohann// the assertion wasn't successful, the AssertionResult object
1881b362b15af34006e6a11974088a46d42b903418eJohann// remembers a non-empty message that describes how it failed.
1891b362b15af34006e6a11974088a46d42b903418eJohann//
1901b362b15af34006e6a11974088a46d42b903418eJohann// To create an instance of this class, use one of the factory functions
1911b362b15af34006e6a11974088a46d42b903418eJohann// (AssertionSuccess() and AssertionFailure()).
1921b362b15af34006e6a11974088a46d42b903418eJohann//
1931b362b15af34006e6a11974088a46d42b903418eJohann// This class is useful for two purposes:
1941b362b15af34006e6a11974088a46d42b903418eJohann//   1. Defining predicate functions to be used with Boolean test assertions
1951b362b15af34006e6a11974088a46d42b903418eJohann//      EXPECT_TRUE/EXPECT_FALSE and their ASSERT_ counterparts
1961b362b15af34006e6a11974088a46d42b903418eJohann//   2. Defining predicate-format functions to be
1971b362b15af34006e6a11974088a46d42b903418eJohann//      used with predicate assertions (ASSERT_PRED_FORMAT*, etc).
1981b362b15af34006e6a11974088a46d42b903418eJohann//
1991b362b15af34006e6a11974088a46d42b903418eJohann// For example, if you define IsEven predicate:
2001b362b15af34006e6a11974088a46d42b903418eJohann//
2011b362b15af34006e6a11974088a46d42b903418eJohann//   testing::AssertionResult IsEven(int n) {
2021b362b15af34006e6a11974088a46d42b903418eJohann//     if ((n % 2) == 0)
2031b362b15af34006e6a11974088a46d42b903418eJohann//       return testing::AssertionSuccess();
2041b362b15af34006e6a11974088a46d42b903418eJohann//     else
2051b362b15af34006e6a11974088a46d42b903418eJohann//       return testing::AssertionFailure() << n << " is odd";
2061b362b15af34006e6a11974088a46d42b903418eJohann//   }
2071b362b15af34006e6a11974088a46d42b903418eJohann//
2081b362b15af34006e6a11974088a46d42b903418eJohann// Then the failed expectation EXPECT_TRUE(IsEven(Fib(5)))
2091b362b15af34006e6a11974088a46d42b903418eJohann// will print the message
2101b362b15af34006e6a11974088a46d42b903418eJohann//
2111b362b15af34006e6a11974088a46d42b903418eJohann//   Value of: IsEven(Fib(5))
2121b362b15af34006e6a11974088a46d42b903418eJohann//     Actual: false (5 is odd)
2131b362b15af34006e6a11974088a46d42b903418eJohann//   Expected: true
2141b362b15af34006e6a11974088a46d42b903418eJohann//
2151b362b15af34006e6a11974088a46d42b903418eJohann// instead of a more opaque
2161b362b15af34006e6a11974088a46d42b903418eJohann//
2171b362b15af34006e6a11974088a46d42b903418eJohann//   Value of: IsEven(Fib(5))
2181b362b15af34006e6a11974088a46d42b903418eJohann//     Actual: false
2191b362b15af34006e6a11974088a46d42b903418eJohann//   Expected: true
2201b362b15af34006e6a11974088a46d42b903418eJohann//
2211b362b15af34006e6a11974088a46d42b903418eJohann// in case IsEven is a simple Boolean predicate.
2221b362b15af34006e6a11974088a46d42b903418eJohann//
2231b362b15af34006e6a11974088a46d42b903418eJohann// If you expect your predicate to be reused and want to support informative
2241b362b15af34006e6a11974088a46d42b903418eJohann// messages in EXPECT_FALSE and ASSERT_FALSE (negative assertions show up
2251b362b15af34006e6a11974088a46d42b903418eJohann// about half as often as positive ones in our tests), supply messages for
2261b362b15af34006e6a11974088a46d42b903418eJohann// both success and failure cases:
2271b362b15af34006e6a11974088a46d42b903418eJohann//
2281b362b15af34006e6a11974088a46d42b903418eJohann//   testing::AssertionResult IsEven(int n) {
2291b362b15af34006e6a11974088a46d42b903418eJohann//     if ((n % 2) == 0)
2301b362b15af34006e6a11974088a46d42b903418eJohann//       return testing::AssertionSuccess() << n << " is even";
2311b362b15af34006e6a11974088a46d42b903418eJohann//     else
2321b362b15af34006e6a11974088a46d42b903418eJohann//       return testing::AssertionFailure() << n << " is odd";
2331b362b15af34006e6a11974088a46d42b903418eJohann//   }
2341b362b15af34006e6a11974088a46d42b903418eJohann//
2351b362b15af34006e6a11974088a46d42b903418eJohann// Then a statement EXPECT_FALSE(IsEven(Fib(6))) will print
2361b362b15af34006e6a11974088a46d42b903418eJohann//
2371b362b15af34006e6a11974088a46d42b903418eJohann//   Value of: IsEven(Fib(6))
2381b362b15af34006e6a11974088a46d42b903418eJohann//     Actual: true (8 is even)
2391b362b15af34006e6a11974088a46d42b903418eJohann//   Expected: false
2401b362b15af34006e6a11974088a46d42b903418eJohann//
2411b362b15af34006e6a11974088a46d42b903418eJohann// NB: Predicates that support negative Boolean assertions have reduced
2421b362b15af34006e6a11974088a46d42b903418eJohann// performance in positive ones so be careful not to use them in tests
2431b362b15af34006e6a11974088a46d42b903418eJohann// that have lots (tens of thousands) of positive Boolean assertions.
2441b362b15af34006e6a11974088a46d42b903418eJohann//
2451b362b15af34006e6a11974088a46d42b903418eJohann// To use this class with EXPECT_PRED_FORMAT assertions such as:
2461b362b15af34006e6a11974088a46d42b903418eJohann//
2471b362b15af34006e6a11974088a46d42b903418eJohann//   // Verifies that Foo() returns an even number.
2481b362b15af34006e6a11974088a46d42b903418eJohann//   EXPECT_PRED_FORMAT1(IsEven, Foo());
2491b362b15af34006e6a11974088a46d42b903418eJohann//
2501b362b15af34006e6a11974088a46d42b903418eJohann// you need to define:
2511b362b15af34006e6a11974088a46d42b903418eJohann//
2521b362b15af34006e6a11974088a46d42b903418eJohann//   testing::AssertionResult IsEven(const char* expr, int n) {
2531b362b15af34006e6a11974088a46d42b903418eJohann//     if ((n % 2) == 0)
2541b362b15af34006e6a11974088a46d42b903418eJohann//       return testing::AssertionSuccess();
2551b362b15af34006e6a11974088a46d42b903418eJohann//     else
2561b362b15af34006e6a11974088a46d42b903418eJohann//       return testing::AssertionFailure()
2571b362b15af34006e6a11974088a46d42b903418eJohann//         << "Expected: " << expr << " is even\n  Actual: it's " << n;
2581b362b15af34006e6a11974088a46d42b903418eJohann//   }
2591b362b15af34006e6a11974088a46d42b903418eJohann//
2601b362b15af34006e6a11974088a46d42b903418eJohann// If Foo() returns 5, you will see the following message:
2611b362b15af34006e6a11974088a46d42b903418eJohann//
2621b362b15af34006e6a11974088a46d42b903418eJohann//   Expected: Foo() is even
2631b362b15af34006e6a11974088a46d42b903418eJohann//     Actual: it's 5
2641b362b15af34006e6a11974088a46d42b903418eJohann//
2651b362b15af34006e6a11974088a46d42b903418eJohannclass GTEST_API_ AssertionResult {
2661b362b15af34006e6a11974088a46d42b903418eJohann public:
2671b362b15af34006e6a11974088a46d42b903418eJohann  // Copy constructor.
2681b362b15af34006e6a11974088a46d42b903418eJohann  // Used in EXPECT_TRUE/FALSE(assertion_result).
2691b362b15af34006e6a11974088a46d42b903418eJohann  AssertionResult(const AssertionResult& other);
2701b362b15af34006e6a11974088a46d42b903418eJohann  // Used in the EXPECT_TRUE/FALSE(bool_expression).
2711b362b15af34006e6a11974088a46d42b903418eJohann  explicit AssertionResult(bool success) : success_(success) {}
2721b362b15af34006e6a11974088a46d42b903418eJohann
2731b362b15af34006e6a11974088a46d42b903418eJohann  // Returns true iff the assertion succeeded.
2741b362b15af34006e6a11974088a46d42b903418eJohann  operator bool() const { return success_; }  // NOLINT
2751b362b15af34006e6a11974088a46d42b903418eJohann
2761b362b15af34006e6a11974088a46d42b903418eJohann  // Returns the assertion's negation. Used with EXPECT/ASSERT_FALSE.
2771b362b15af34006e6a11974088a46d42b903418eJohann  AssertionResult operator!() const;
2781b362b15af34006e6a11974088a46d42b903418eJohann
2791b362b15af34006e6a11974088a46d42b903418eJohann  // Returns the text streamed into this AssertionResult. Test assertions
2801b362b15af34006e6a11974088a46d42b903418eJohann  // use it when they fail (i.e., the predicate's outcome doesn't match the
2811b362b15af34006e6a11974088a46d42b903418eJohann  // assertion's expectation). When nothing has been streamed into the
2821b362b15af34006e6a11974088a46d42b903418eJohann  // object, returns an empty string.
2831b362b15af34006e6a11974088a46d42b903418eJohann  const char* message() const {
2841b362b15af34006e6a11974088a46d42b903418eJohann    return message_.get() != NULL ?  message_->c_str() : "";
2851b362b15af34006e6a11974088a46d42b903418eJohann  }
2861b362b15af34006e6a11974088a46d42b903418eJohann  // TODO(vladl@google.com): Remove this after making sure no clients use it.
2871b362b15af34006e6a11974088a46d42b903418eJohann  // Deprecated; please use message() instead.
2881b362b15af34006e6a11974088a46d42b903418eJohann  const char* failure_message() const { return message(); }
2891b362b15af34006e6a11974088a46d42b903418eJohann
2901b362b15af34006e6a11974088a46d42b903418eJohann  // Streams a custom failure message into this object.
2911b362b15af34006e6a11974088a46d42b903418eJohann  template <typename T> AssertionResult& operator<<(const T& value) {
2921b362b15af34006e6a11974088a46d42b903418eJohann    AppendMessage(Message() << value);
2931b362b15af34006e6a11974088a46d42b903418eJohann    return *this;
2941b362b15af34006e6a11974088a46d42b903418eJohann  }
2951b362b15af34006e6a11974088a46d42b903418eJohann
2961b362b15af34006e6a11974088a46d42b903418eJohann  // Allows streaming basic output manipulators such as endl or flush into
2971b362b15af34006e6a11974088a46d42b903418eJohann  // this object.
2981b362b15af34006e6a11974088a46d42b903418eJohann  AssertionResult& operator<<(
2991b362b15af34006e6a11974088a46d42b903418eJohann      ::std::ostream& (*basic_manipulator)(::std::ostream& stream)) {
3001b362b15af34006e6a11974088a46d42b903418eJohann    AppendMessage(Message() << basic_manipulator);
3011b362b15af34006e6a11974088a46d42b903418eJohann    return *this;
3021b362b15af34006e6a11974088a46d42b903418eJohann  }
3031b362b15af34006e6a11974088a46d42b903418eJohann
3041b362b15af34006e6a11974088a46d42b903418eJohann private:
3051b362b15af34006e6a11974088a46d42b903418eJohann  // Appends the contents of message to message_.
3061b362b15af34006e6a11974088a46d42b903418eJohann  void AppendMessage(const Message& a_message) {
3071b362b15af34006e6a11974088a46d42b903418eJohann    if (message_.get() == NULL)
3081b362b15af34006e6a11974088a46d42b903418eJohann      message_.reset(new ::std::string);
3091b362b15af34006e6a11974088a46d42b903418eJohann    message_->append(a_message.GetString().c_str());
3101b362b15af34006e6a11974088a46d42b903418eJohann  }
3111b362b15af34006e6a11974088a46d42b903418eJohann
3121b362b15af34006e6a11974088a46d42b903418eJohann  // Stores result of the assertion predicate.
3131b362b15af34006e6a11974088a46d42b903418eJohann  bool success_;
3141b362b15af34006e6a11974088a46d42b903418eJohann  // Stores the message describing the condition in case the expectation
3151b362b15af34006e6a11974088a46d42b903418eJohann  // construct is not satisfied with the predicate's outcome.
3161b362b15af34006e6a11974088a46d42b903418eJohann  // Referenced via a pointer to avoid taking too much stack frame space
3171b362b15af34006e6a11974088a46d42b903418eJohann  // with test assertions.
3181b362b15af34006e6a11974088a46d42b903418eJohann  internal::scoped_ptr< ::std::string> message_;
3191b362b15af34006e6a11974088a46d42b903418eJohann
3201b362b15af34006e6a11974088a46d42b903418eJohann  GTEST_DISALLOW_ASSIGN_(AssertionResult);
3211b362b15af34006e6a11974088a46d42b903418eJohann};
3221b362b15af34006e6a11974088a46d42b903418eJohann
3231b362b15af34006e6a11974088a46d42b903418eJohann// Makes a successful assertion result.
3241b362b15af34006e6a11974088a46d42b903418eJohannGTEST_API_ AssertionResult AssertionSuccess();
3251b362b15af34006e6a11974088a46d42b903418eJohann
3261b362b15af34006e6a11974088a46d42b903418eJohann// Makes a failed assertion result.
3271b362b15af34006e6a11974088a46d42b903418eJohannGTEST_API_ AssertionResult AssertionFailure();
3281b362b15af34006e6a11974088a46d42b903418eJohann
3291b362b15af34006e6a11974088a46d42b903418eJohann// Makes a failed assertion result with the given failure message.
3301b362b15af34006e6a11974088a46d42b903418eJohann// Deprecated; use AssertionFailure() << msg.
3311b362b15af34006e6a11974088a46d42b903418eJohannGTEST_API_ AssertionResult AssertionFailure(const Message& msg);
3321b362b15af34006e6a11974088a46d42b903418eJohann
3331b362b15af34006e6a11974088a46d42b903418eJohann// The abstract class that all tests inherit from.
3341b362b15af34006e6a11974088a46d42b903418eJohann//
3351b362b15af34006e6a11974088a46d42b903418eJohann// In Google Test, a unit test program contains one or many TestCases, and
3361b362b15af34006e6a11974088a46d42b903418eJohann// each TestCase contains one or many Tests.
3371b362b15af34006e6a11974088a46d42b903418eJohann//
3381b362b15af34006e6a11974088a46d42b903418eJohann// When you define a test using the TEST macro, you don't need to
3391b362b15af34006e6a11974088a46d42b903418eJohann// explicitly derive from Test - the TEST macro automatically does
3401b362b15af34006e6a11974088a46d42b903418eJohann// this for you.
3411b362b15af34006e6a11974088a46d42b903418eJohann//
3421b362b15af34006e6a11974088a46d42b903418eJohann// The only time you derive from Test is when defining a test fixture
3431b362b15af34006e6a11974088a46d42b903418eJohann// to be used a TEST_F.  For example:
3441b362b15af34006e6a11974088a46d42b903418eJohann//
3451b362b15af34006e6a11974088a46d42b903418eJohann//   class FooTest : public testing::Test {
3461b362b15af34006e6a11974088a46d42b903418eJohann//    protected:
3471b362b15af34006e6a11974088a46d42b903418eJohann//     virtual void SetUp() { ... }
3481b362b15af34006e6a11974088a46d42b903418eJohann//     virtual void TearDown() { ... }
3491b362b15af34006e6a11974088a46d42b903418eJohann//     ...
3501b362b15af34006e6a11974088a46d42b903418eJohann//   };
3511b362b15af34006e6a11974088a46d42b903418eJohann//
3521b362b15af34006e6a11974088a46d42b903418eJohann//   TEST_F(FooTest, Bar) { ... }
3531b362b15af34006e6a11974088a46d42b903418eJohann//   TEST_F(FooTest, Baz) { ... }
3541b362b15af34006e6a11974088a46d42b903418eJohann//
3551b362b15af34006e6a11974088a46d42b903418eJohann// Test is not copyable.
3561b362b15af34006e6a11974088a46d42b903418eJohannclass GTEST_API_ Test {
3571b362b15af34006e6a11974088a46d42b903418eJohann public:
3581b362b15af34006e6a11974088a46d42b903418eJohann  friend class TestInfo;
3591b362b15af34006e6a11974088a46d42b903418eJohann
3601b362b15af34006e6a11974088a46d42b903418eJohann  // Defines types for pointers to functions that set up and tear down
3611b362b15af34006e6a11974088a46d42b903418eJohann  // a test case.
3621b362b15af34006e6a11974088a46d42b903418eJohann  typedef internal::SetUpTestCaseFunc SetUpTestCaseFunc;
3631b362b15af34006e6a11974088a46d42b903418eJohann  typedef internal::TearDownTestCaseFunc TearDownTestCaseFunc;
3641b362b15af34006e6a11974088a46d42b903418eJohann
3651b362b15af34006e6a11974088a46d42b903418eJohann  // The d'tor is virtual as we intend to inherit from Test.
3661b362b15af34006e6a11974088a46d42b903418eJohann  virtual ~Test();
3671b362b15af34006e6a11974088a46d42b903418eJohann
3681b362b15af34006e6a11974088a46d42b903418eJohann  // Sets up the stuff shared by all tests in this test case.
3691b362b15af34006e6a11974088a46d42b903418eJohann  //
3701b362b15af34006e6a11974088a46d42b903418eJohann  // Google Test will call Foo::SetUpTestCase() before running the first
3711b362b15af34006e6a11974088a46d42b903418eJohann  // test in test case Foo.  Hence a sub-class can define its own
3721b362b15af34006e6a11974088a46d42b903418eJohann  // SetUpTestCase() method to shadow the one defined in the super
3731b362b15af34006e6a11974088a46d42b903418eJohann  // class.
3741b362b15af34006e6a11974088a46d42b903418eJohann  static void SetUpTestCase() {}
3751b362b15af34006e6a11974088a46d42b903418eJohann
3761b362b15af34006e6a11974088a46d42b903418eJohann  // Tears down the stuff shared by all tests in this test case.
3771b362b15af34006e6a11974088a46d42b903418eJohann  //
3781b362b15af34006e6a11974088a46d42b903418eJohann  // Google Test will call Foo::TearDownTestCase() after running the last
3791b362b15af34006e6a11974088a46d42b903418eJohann  // test in test case Foo.  Hence a sub-class can define its own
3801b362b15af34006e6a11974088a46d42b903418eJohann  // TearDownTestCase() method to shadow the one defined in the super
3811b362b15af34006e6a11974088a46d42b903418eJohann  // class.
3821b362b15af34006e6a11974088a46d42b903418eJohann  static void TearDownTestCase() {}
3831b362b15af34006e6a11974088a46d42b903418eJohann
3841b362b15af34006e6a11974088a46d42b903418eJohann  // Returns true iff the current test has a fatal failure.
3851b362b15af34006e6a11974088a46d42b903418eJohann  static bool HasFatalFailure();
3861b362b15af34006e6a11974088a46d42b903418eJohann
3871b362b15af34006e6a11974088a46d42b903418eJohann  // Returns true iff the current test has a non-fatal failure.
3881b362b15af34006e6a11974088a46d42b903418eJohann  static bool HasNonfatalFailure();
3891b362b15af34006e6a11974088a46d42b903418eJohann
3901b362b15af34006e6a11974088a46d42b903418eJohann  // Returns true iff the current test has a (either fatal or
3911b362b15af34006e6a11974088a46d42b903418eJohann  // non-fatal) failure.
3921b362b15af34006e6a11974088a46d42b903418eJohann  static bool HasFailure() { return HasFatalFailure() || HasNonfatalFailure(); }
3931b362b15af34006e6a11974088a46d42b903418eJohann
3941b362b15af34006e6a11974088a46d42b903418eJohann  // Logs a property for the current test.  Only the last value for a given
3951b362b15af34006e6a11974088a46d42b903418eJohann  // key is remembered.
3961b362b15af34006e6a11974088a46d42b903418eJohann  // These are public static so they can be called from utility functions
3971b362b15af34006e6a11974088a46d42b903418eJohann  // that are not members of the test fixture.
3981b362b15af34006e6a11974088a46d42b903418eJohann  // The arguments are const char* instead strings, as Google Test is used
3991b362b15af34006e6a11974088a46d42b903418eJohann  // on platforms where string doesn't compile.
4001b362b15af34006e6a11974088a46d42b903418eJohann  //
4011b362b15af34006e6a11974088a46d42b903418eJohann  // Note that a driving consideration for these RecordProperty methods
4021b362b15af34006e6a11974088a46d42b903418eJohann  // was to produce xml output suited to the Greenspan charting utility,
4031b362b15af34006e6a11974088a46d42b903418eJohann  // which at present will only chart values that fit in a 32-bit int. It
4041b362b15af34006e6a11974088a46d42b903418eJohann  // is the user's responsibility to restrict their values to 32-bit ints
4051b362b15af34006e6a11974088a46d42b903418eJohann  // if they intend them to be used with Greenspan.
4061b362b15af34006e6a11974088a46d42b903418eJohann  static void RecordProperty(const char* key, const char* value);
4071b362b15af34006e6a11974088a46d42b903418eJohann  static void RecordProperty(const char* key, int value);
4081b362b15af34006e6a11974088a46d42b903418eJohann
4091b362b15af34006e6a11974088a46d42b903418eJohann protected:
4101b362b15af34006e6a11974088a46d42b903418eJohann  // Creates a Test object.
4111b362b15af34006e6a11974088a46d42b903418eJohann  Test();
4121b362b15af34006e6a11974088a46d42b903418eJohann
4131b362b15af34006e6a11974088a46d42b903418eJohann  // Sets up the test fixture.
4141b362b15af34006e6a11974088a46d42b903418eJohann  virtual void SetUp();
4151b362b15af34006e6a11974088a46d42b903418eJohann
4161b362b15af34006e6a11974088a46d42b903418eJohann  // Tears down the test fixture.
4171b362b15af34006e6a11974088a46d42b903418eJohann  virtual void TearDown();
4181b362b15af34006e6a11974088a46d42b903418eJohann
4191b362b15af34006e6a11974088a46d42b903418eJohann private:
4201b362b15af34006e6a11974088a46d42b903418eJohann  // Returns true iff the current test has the same fixture class as
4211b362b15af34006e6a11974088a46d42b903418eJohann  // the first test in the current test case.
4221b362b15af34006e6a11974088a46d42b903418eJohann  static bool HasSameFixtureClass();
4231b362b15af34006e6a11974088a46d42b903418eJohann
4241b362b15af34006e6a11974088a46d42b903418eJohann  // Runs the test after the test fixture has been set up.
4251b362b15af34006e6a11974088a46d42b903418eJohann  //
4261b362b15af34006e6a11974088a46d42b903418eJohann  // A sub-class must implement this to define the test logic.
4271b362b15af34006e6a11974088a46d42b903418eJohann  //
4281b362b15af34006e6a11974088a46d42b903418eJohann  // DO NOT OVERRIDE THIS FUNCTION DIRECTLY IN A USER PROGRAM.
4291b362b15af34006e6a11974088a46d42b903418eJohann  // Instead, use the TEST or TEST_F macro.
4301b362b15af34006e6a11974088a46d42b903418eJohann  virtual void TestBody() = 0;
4311b362b15af34006e6a11974088a46d42b903418eJohann
4321b362b15af34006e6a11974088a46d42b903418eJohann  // Sets up, executes, and tears down the test.
4331b362b15af34006e6a11974088a46d42b903418eJohann  void Run();
4341b362b15af34006e6a11974088a46d42b903418eJohann
4351b362b15af34006e6a11974088a46d42b903418eJohann  // Deletes self.  We deliberately pick an unusual name for this
4361b362b15af34006e6a11974088a46d42b903418eJohann  // internal method to avoid clashing with names used in user TESTs.
4371b362b15af34006e6a11974088a46d42b903418eJohann  void DeleteSelf_() { delete this; }
4381b362b15af34006e6a11974088a46d42b903418eJohann
4391b362b15af34006e6a11974088a46d42b903418eJohann  // Uses a GTestFlagSaver to save and restore all Google Test flags.
4401b362b15af34006e6a11974088a46d42b903418eJohann  const internal::GTestFlagSaver* const gtest_flag_saver_;
4411b362b15af34006e6a11974088a46d42b903418eJohann
4421b362b15af34006e6a11974088a46d42b903418eJohann  // Often a user mis-spells SetUp() as Setup() and spends a long time
4431b362b15af34006e6a11974088a46d42b903418eJohann  // wondering why it is never called by Google Test.  The declaration of
4441b362b15af34006e6a11974088a46d42b903418eJohann  // the following method is solely for catching such an error at
4451b362b15af34006e6a11974088a46d42b903418eJohann  // compile time:
4461b362b15af34006e6a11974088a46d42b903418eJohann  //
4471b362b15af34006e6a11974088a46d42b903418eJohann  //   - The return type is deliberately chosen to be not void, so it
4481b362b15af34006e6a11974088a46d42b903418eJohann  //   will be a conflict if a user declares void Setup() in his test
4491b362b15af34006e6a11974088a46d42b903418eJohann  //   fixture.
4501b362b15af34006e6a11974088a46d42b903418eJohann  //
4511b362b15af34006e6a11974088a46d42b903418eJohann  //   - This method is private, so it will be another compiler error
4521b362b15af34006e6a11974088a46d42b903418eJohann  //   if a user calls it from his test fixture.
4531b362b15af34006e6a11974088a46d42b903418eJohann  //
4541b362b15af34006e6a11974088a46d42b903418eJohann  // DO NOT OVERRIDE THIS FUNCTION.
4551b362b15af34006e6a11974088a46d42b903418eJohann  //
4561b362b15af34006e6a11974088a46d42b903418eJohann  // If you see an error about overriding the following function or
4571b362b15af34006e6a11974088a46d42b903418eJohann  // about it being private, you have mis-spelled SetUp() as Setup().
4581b362b15af34006e6a11974088a46d42b903418eJohann  struct Setup_should_be_spelled_SetUp {};
4591b362b15af34006e6a11974088a46d42b903418eJohann  virtual Setup_should_be_spelled_SetUp* Setup() { return NULL; }
4601b362b15af34006e6a11974088a46d42b903418eJohann
4611b362b15af34006e6a11974088a46d42b903418eJohann  // We disallow copying Tests.
4621b362b15af34006e6a11974088a46d42b903418eJohann  GTEST_DISALLOW_COPY_AND_ASSIGN_(Test);
4631b362b15af34006e6a11974088a46d42b903418eJohann};
4641b362b15af34006e6a11974088a46d42b903418eJohann
4651b362b15af34006e6a11974088a46d42b903418eJohanntypedef internal::TimeInMillis TimeInMillis;
4661b362b15af34006e6a11974088a46d42b903418eJohann
4671b362b15af34006e6a11974088a46d42b903418eJohann// A copyable object representing a user specified test property which can be
4681b362b15af34006e6a11974088a46d42b903418eJohann// output as a key/value string pair.
4691b362b15af34006e6a11974088a46d42b903418eJohann//
4701b362b15af34006e6a11974088a46d42b903418eJohann// Don't inherit from TestProperty as its destructor is not virtual.
4711b362b15af34006e6a11974088a46d42b903418eJohannclass TestProperty {
4721b362b15af34006e6a11974088a46d42b903418eJohann public:
4731b362b15af34006e6a11974088a46d42b903418eJohann  // C'tor.  TestProperty does NOT have a default constructor.
4741b362b15af34006e6a11974088a46d42b903418eJohann  // Always use this constructor (with parameters) to create a
4751b362b15af34006e6a11974088a46d42b903418eJohann  // TestProperty object.
4761b362b15af34006e6a11974088a46d42b903418eJohann  TestProperty(const char* a_key, const char* a_value) :
4771b362b15af34006e6a11974088a46d42b903418eJohann    key_(a_key), value_(a_value) {
4781b362b15af34006e6a11974088a46d42b903418eJohann  }
4791b362b15af34006e6a11974088a46d42b903418eJohann
4801b362b15af34006e6a11974088a46d42b903418eJohann  // Gets the user supplied key.
4811b362b15af34006e6a11974088a46d42b903418eJohann  const char* key() const {
4821b362b15af34006e6a11974088a46d42b903418eJohann    return key_.c_str();
4831b362b15af34006e6a11974088a46d42b903418eJohann  }
4841b362b15af34006e6a11974088a46d42b903418eJohann
4851b362b15af34006e6a11974088a46d42b903418eJohann  // Gets the user supplied value.
4861b362b15af34006e6a11974088a46d42b903418eJohann  const char* value() const {
4871b362b15af34006e6a11974088a46d42b903418eJohann    return value_.c_str();
4881b362b15af34006e6a11974088a46d42b903418eJohann  }
4891b362b15af34006e6a11974088a46d42b903418eJohann
4901b362b15af34006e6a11974088a46d42b903418eJohann  // Sets a new value, overriding the one supplied in the constructor.
4911b362b15af34006e6a11974088a46d42b903418eJohann  void SetValue(const char* new_value) {
4921b362b15af34006e6a11974088a46d42b903418eJohann    value_ = new_value;
4931b362b15af34006e6a11974088a46d42b903418eJohann  }
4941b362b15af34006e6a11974088a46d42b903418eJohann
4951b362b15af34006e6a11974088a46d42b903418eJohann private:
4961b362b15af34006e6a11974088a46d42b903418eJohann  // The key supplied by the user.
4971b362b15af34006e6a11974088a46d42b903418eJohann  internal::String key_;
4981b362b15af34006e6a11974088a46d42b903418eJohann  // The value supplied by the user.
4991b362b15af34006e6a11974088a46d42b903418eJohann  internal::String value_;
5001b362b15af34006e6a11974088a46d42b903418eJohann};
5011b362b15af34006e6a11974088a46d42b903418eJohann
5021b362b15af34006e6a11974088a46d42b903418eJohann// The result of a single Test.  This includes a list of
5031b362b15af34006e6a11974088a46d42b903418eJohann// TestPartResults, a list of TestProperties, a count of how many
5041b362b15af34006e6a11974088a46d42b903418eJohann// death tests there are in the Test, and how much time it took to run
5051b362b15af34006e6a11974088a46d42b903418eJohann// the Test.
5061b362b15af34006e6a11974088a46d42b903418eJohann//
5071b362b15af34006e6a11974088a46d42b903418eJohann// TestResult is not copyable.
5081b362b15af34006e6a11974088a46d42b903418eJohannclass GTEST_API_ TestResult {
5091b362b15af34006e6a11974088a46d42b903418eJohann public:
5101b362b15af34006e6a11974088a46d42b903418eJohann  // Creates an empty TestResult.
5111b362b15af34006e6a11974088a46d42b903418eJohann  TestResult();
5121b362b15af34006e6a11974088a46d42b903418eJohann
5131b362b15af34006e6a11974088a46d42b903418eJohann  // D'tor.  Do not inherit from TestResult.
5141b362b15af34006e6a11974088a46d42b903418eJohann  ~TestResult();
5151b362b15af34006e6a11974088a46d42b903418eJohann
5161b362b15af34006e6a11974088a46d42b903418eJohann  // Gets the number of all test parts.  This is the sum of the number
5171b362b15af34006e6a11974088a46d42b903418eJohann  // of successful test parts and the number of failed test parts.
5181b362b15af34006e6a11974088a46d42b903418eJohann  int total_part_count() const;
5191b362b15af34006e6a11974088a46d42b903418eJohann
5201b362b15af34006e6a11974088a46d42b903418eJohann  // Returns the number of the test properties.
5211b362b15af34006e6a11974088a46d42b903418eJohann  int test_property_count() const;
5221b362b15af34006e6a11974088a46d42b903418eJohann
5231b362b15af34006e6a11974088a46d42b903418eJohann  // Returns true iff the test passed (i.e. no test part failed).
5241b362b15af34006e6a11974088a46d42b903418eJohann  bool Passed() const { return !Failed(); }
5251b362b15af34006e6a11974088a46d42b903418eJohann
5261b362b15af34006e6a11974088a46d42b903418eJohann  // Returns true iff the test failed.
5271b362b15af34006e6a11974088a46d42b903418eJohann  bool Failed() const;
5281b362b15af34006e6a11974088a46d42b903418eJohann
5291b362b15af34006e6a11974088a46d42b903418eJohann  // Returns true iff the test fatally failed.
5301b362b15af34006e6a11974088a46d42b903418eJohann  bool HasFatalFailure() const;
5311b362b15af34006e6a11974088a46d42b903418eJohann
5321b362b15af34006e6a11974088a46d42b903418eJohann  // Returns true iff the test has a non-fatal failure.
5331b362b15af34006e6a11974088a46d42b903418eJohann  bool HasNonfatalFailure() const;
5341b362b15af34006e6a11974088a46d42b903418eJohann
5351b362b15af34006e6a11974088a46d42b903418eJohann  // Returns the elapsed time, in milliseconds.
5361b362b15af34006e6a11974088a46d42b903418eJohann  TimeInMillis elapsed_time() const { return elapsed_time_; }
5371b362b15af34006e6a11974088a46d42b903418eJohann
5381b362b15af34006e6a11974088a46d42b903418eJohann  // Returns the i-th test part result among all the results. i can range
5391b362b15af34006e6a11974088a46d42b903418eJohann  // from 0 to test_property_count() - 1. If i is not in that range, aborts
5401b362b15af34006e6a11974088a46d42b903418eJohann  // the program.
5411b362b15af34006e6a11974088a46d42b903418eJohann  const TestPartResult& GetTestPartResult(int i) const;
5421b362b15af34006e6a11974088a46d42b903418eJohann
5431b362b15af34006e6a11974088a46d42b903418eJohann  // Returns the i-th test property. i can range from 0 to
5441b362b15af34006e6a11974088a46d42b903418eJohann  // test_property_count() - 1. If i is not in that range, aborts the
5451b362b15af34006e6a11974088a46d42b903418eJohann  // program.
5461b362b15af34006e6a11974088a46d42b903418eJohann  const TestProperty& GetTestProperty(int i) const;
5471b362b15af34006e6a11974088a46d42b903418eJohann
5481b362b15af34006e6a11974088a46d42b903418eJohann private:
5491b362b15af34006e6a11974088a46d42b903418eJohann  friend class TestInfo;
5501b362b15af34006e6a11974088a46d42b903418eJohann  friend class UnitTest;
5511b362b15af34006e6a11974088a46d42b903418eJohann  friend class internal::DefaultGlobalTestPartResultReporter;
5521b362b15af34006e6a11974088a46d42b903418eJohann  friend class internal::ExecDeathTest;
5531b362b15af34006e6a11974088a46d42b903418eJohann  friend class internal::TestResultAccessor;
5541b362b15af34006e6a11974088a46d42b903418eJohann  friend class internal::UnitTestImpl;
5551b362b15af34006e6a11974088a46d42b903418eJohann  friend class internal::WindowsDeathTest;
5561b362b15af34006e6a11974088a46d42b903418eJohann
5571b362b15af34006e6a11974088a46d42b903418eJohann  // Gets the vector of TestPartResults.
5581b362b15af34006e6a11974088a46d42b903418eJohann  const std::vector<TestPartResult>& test_part_results() const {
5591b362b15af34006e6a11974088a46d42b903418eJohann    return test_part_results_;
5601b362b15af34006e6a11974088a46d42b903418eJohann  }
5611b362b15af34006e6a11974088a46d42b903418eJohann
5621b362b15af34006e6a11974088a46d42b903418eJohann  // Gets the vector of TestProperties.
5631b362b15af34006e6a11974088a46d42b903418eJohann  const std::vector<TestProperty>& test_properties() const {
5641b362b15af34006e6a11974088a46d42b903418eJohann    return test_properties_;
5651b362b15af34006e6a11974088a46d42b903418eJohann  }
5661b362b15af34006e6a11974088a46d42b903418eJohann
5671b362b15af34006e6a11974088a46d42b903418eJohann  // Sets the elapsed time.
5681b362b15af34006e6a11974088a46d42b903418eJohann  void set_elapsed_time(TimeInMillis elapsed) { elapsed_time_ = elapsed; }
5691b362b15af34006e6a11974088a46d42b903418eJohann
5701b362b15af34006e6a11974088a46d42b903418eJohann  // Adds a test property to the list. The property is validated and may add
5711b362b15af34006e6a11974088a46d42b903418eJohann  // a non-fatal failure if invalid (e.g., if it conflicts with reserved
5721b362b15af34006e6a11974088a46d42b903418eJohann  // key names). If a property is already recorded for the same key, the
5731b362b15af34006e6a11974088a46d42b903418eJohann  // value will be updated, rather than storing multiple values for the same
5741b362b15af34006e6a11974088a46d42b903418eJohann  // key.
5751b362b15af34006e6a11974088a46d42b903418eJohann  void RecordProperty(const TestProperty& test_property);
5761b362b15af34006e6a11974088a46d42b903418eJohann
5771b362b15af34006e6a11974088a46d42b903418eJohann  // Adds a failure if the key is a reserved attribute of Google Test
5781b362b15af34006e6a11974088a46d42b903418eJohann  // testcase tags.  Returns true if the property is valid.
5791b362b15af34006e6a11974088a46d42b903418eJohann  // TODO(russr): Validate attribute names are legal and human readable.
5801b362b15af34006e6a11974088a46d42b903418eJohann  static bool ValidateTestProperty(const TestProperty& test_property);
5811b362b15af34006e6a11974088a46d42b903418eJohann
5821b362b15af34006e6a11974088a46d42b903418eJohann  // Adds a test part result to the list.
5831b362b15af34006e6a11974088a46d42b903418eJohann  void AddTestPartResult(const TestPartResult& test_part_result);
5841b362b15af34006e6a11974088a46d42b903418eJohann
5851b362b15af34006e6a11974088a46d42b903418eJohann  // Returns the death test count.
5861b362b15af34006e6a11974088a46d42b903418eJohann  int death_test_count() const { return death_test_count_; }
5871b362b15af34006e6a11974088a46d42b903418eJohann
5881b362b15af34006e6a11974088a46d42b903418eJohann  // Increments the death test count, returning the new count.
5891b362b15af34006e6a11974088a46d42b903418eJohann  int increment_death_test_count() { return ++death_test_count_; }
5901b362b15af34006e6a11974088a46d42b903418eJohann
5911b362b15af34006e6a11974088a46d42b903418eJohann  // Clears the test part results.
5921b362b15af34006e6a11974088a46d42b903418eJohann  void ClearTestPartResults();
5931b362b15af34006e6a11974088a46d42b903418eJohann
5941b362b15af34006e6a11974088a46d42b903418eJohann  // Clears the object.
5951b362b15af34006e6a11974088a46d42b903418eJohann  void Clear();
5961b362b15af34006e6a11974088a46d42b903418eJohann
5971b362b15af34006e6a11974088a46d42b903418eJohann  // Protects mutable state of the property vector and of owned
5981b362b15af34006e6a11974088a46d42b903418eJohann  // properties, whose values may be updated.
5991b362b15af34006e6a11974088a46d42b903418eJohann  internal::Mutex test_properites_mutex_;
6001b362b15af34006e6a11974088a46d42b903418eJohann
6011b362b15af34006e6a11974088a46d42b903418eJohann  // The vector of TestPartResults
6021b362b15af34006e6a11974088a46d42b903418eJohann  std::vector<TestPartResult> test_part_results_;
6031b362b15af34006e6a11974088a46d42b903418eJohann  // The vector of TestProperties
6041b362b15af34006e6a11974088a46d42b903418eJohann  std::vector<TestProperty> test_properties_;
6051b362b15af34006e6a11974088a46d42b903418eJohann  // Running count of death tests.
6061b362b15af34006e6a11974088a46d42b903418eJohann  int death_test_count_;
6071b362b15af34006e6a11974088a46d42b903418eJohann  // The elapsed time, in milliseconds.
6081b362b15af34006e6a11974088a46d42b903418eJohann  TimeInMillis elapsed_time_;
6091b362b15af34006e6a11974088a46d42b903418eJohann
6101b362b15af34006e6a11974088a46d42b903418eJohann  // We disallow copying TestResult.
6111b362b15af34006e6a11974088a46d42b903418eJohann  GTEST_DISALLOW_COPY_AND_ASSIGN_(TestResult);
6121b362b15af34006e6a11974088a46d42b903418eJohann};  // class TestResult
6131b362b15af34006e6a11974088a46d42b903418eJohann
6141b362b15af34006e6a11974088a46d42b903418eJohann// A TestInfo object stores the following information about a test:
6151b362b15af34006e6a11974088a46d42b903418eJohann//
6161b362b15af34006e6a11974088a46d42b903418eJohann//   Test case name
6171b362b15af34006e6a11974088a46d42b903418eJohann//   Test name
6181b362b15af34006e6a11974088a46d42b903418eJohann//   Whether the test should be run
6191b362b15af34006e6a11974088a46d42b903418eJohann//   A function pointer that creates the test object when invoked
6201b362b15af34006e6a11974088a46d42b903418eJohann//   Test result
6211b362b15af34006e6a11974088a46d42b903418eJohann//
6221b362b15af34006e6a11974088a46d42b903418eJohann// The constructor of TestInfo registers itself with the UnitTest
6231b362b15af34006e6a11974088a46d42b903418eJohann// singleton such that the RUN_ALL_TESTS() macro knows which tests to
6241b362b15af34006e6a11974088a46d42b903418eJohann// run.
6251b362b15af34006e6a11974088a46d42b903418eJohannclass GTEST_API_ TestInfo {
6261b362b15af34006e6a11974088a46d42b903418eJohann public:
6271b362b15af34006e6a11974088a46d42b903418eJohann  // Destructs a TestInfo object.  This function is not virtual, so
6281b362b15af34006e6a11974088a46d42b903418eJohann  // don't inherit from TestInfo.
6291b362b15af34006e6a11974088a46d42b903418eJohann  ~TestInfo();
6301b362b15af34006e6a11974088a46d42b903418eJohann
6311b362b15af34006e6a11974088a46d42b903418eJohann  // Returns the test case name.
6321b362b15af34006e6a11974088a46d42b903418eJohann  const char* test_case_name() const { return test_case_name_.c_str(); }
6331b362b15af34006e6a11974088a46d42b903418eJohann
6341b362b15af34006e6a11974088a46d42b903418eJohann  // Returns the test name.
6351b362b15af34006e6a11974088a46d42b903418eJohann  const char* name() const { return name_.c_str(); }
6361b362b15af34006e6a11974088a46d42b903418eJohann
6371b362b15af34006e6a11974088a46d42b903418eJohann  // Returns the name of the parameter type, or NULL if this is not a typed
6381b362b15af34006e6a11974088a46d42b903418eJohann  // or a type-parameterized test.
6391b362b15af34006e6a11974088a46d42b903418eJohann  const char* type_param() const {
6401b362b15af34006e6a11974088a46d42b903418eJohann    if (type_param_.get() != NULL)
6411b362b15af34006e6a11974088a46d42b903418eJohann      return type_param_->c_str();
6421b362b15af34006e6a11974088a46d42b903418eJohann    return NULL;
6431b362b15af34006e6a11974088a46d42b903418eJohann  }
6441b362b15af34006e6a11974088a46d42b903418eJohann
6451b362b15af34006e6a11974088a46d42b903418eJohann  // Returns the text representation of the value parameter, or NULL if this
6461b362b15af34006e6a11974088a46d42b903418eJohann  // is not a value-parameterized test.
6471b362b15af34006e6a11974088a46d42b903418eJohann  const char* value_param() const {
6481b362b15af34006e6a11974088a46d42b903418eJohann    if (value_param_.get() != NULL)
6491b362b15af34006e6a11974088a46d42b903418eJohann      return value_param_->c_str();
6501b362b15af34006e6a11974088a46d42b903418eJohann    return NULL;
6511b362b15af34006e6a11974088a46d42b903418eJohann  }
6521b362b15af34006e6a11974088a46d42b903418eJohann
6531b362b15af34006e6a11974088a46d42b903418eJohann  // Returns true if this test should run, that is if the test is not disabled
6541b362b15af34006e6a11974088a46d42b903418eJohann  // (or it is disabled but the also_run_disabled_tests flag has been specified)
6551b362b15af34006e6a11974088a46d42b903418eJohann  // and its full name matches the user-specified filter.
6561b362b15af34006e6a11974088a46d42b903418eJohann  //
6571b362b15af34006e6a11974088a46d42b903418eJohann  // Google Test allows the user to filter the tests by their full names.
6581b362b15af34006e6a11974088a46d42b903418eJohann  // The full name of a test Bar in test case Foo is defined as
6591b362b15af34006e6a11974088a46d42b903418eJohann  // "Foo.Bar".  Only the tests that match the filter will run.
6601b362b15af34006e6a11974088a46d42b903418eJohann  //
6611b362b15af34006e6a11974088a46d42b903418eJohann  // A filter is a colon-separated list of glob (not regex) patterns,
6621b362b15af34006e6a11974088a46d42b903418eJohann  // optionally followed by a '-' and a colon-separated list of
6631b362b15af34006e6a11974088a46d42b903418eJohann  // negative patterns (tests to exclude).  A test is run if it
6641b362b15af34006e6a11974088a46d42b903418eJohann  // matches one of the positive patterns and does not match any of
6651b362b15af34006e6a11974088a46d42b903418eJohann  // the negative patterns.
6661b362b15af34006e6a11974088a46d42b903418eJohann  //
6671b362b15af34006e6a11974088a46d42b903418eJohann  // For example, *A*:Foo.* is a filter that matches any string that
6681b362b15af34006e6a11974088a46d42b903418eJohann  // contains the character 'A' or starts with "Foo.".
6691b362b15af34006e6a11974088a46d42b903418eJohann  bool should_run() const { return should_run_; }
6701b362b15af34006e6a11974088a46d42b903418eJohann
6711b362b15af34006e6a11974088a46d42b903418eJohann  // Returns the result of the test.
6721b362b15af34006e6a11974088a46d42b903418eJohann  const TestResult* result() const { return &result_; }
6731b362b15af34006e6a11974088a46d42b903418eJohann
6741b362b15af34006e6a11974088a46d42b903418eJohann private:
6751b362b15af34006e6a11974088a46d42b903418eJohann
6761b362b15af34006e6a11974088a46d42b903418eJohann#if GTEST_HAS_DEATH_TEST
6771b362b15af34006e6a11974088a46d42b903418eJohann  friend class internal::DefaultDeathTestFactory;
6781b362b15af34006e6a11974088a46d42b903418eJohann#endif  // GTEST_HAS_DEATH_TEST
6791b362b15af34006e6a11974088a46d42b903418eJohann  friend class Test;
6801b362b15af34006e6a11974088a46d42b903418eJohann  friend class TestCase;
6811b362b15af34006e6a11974088a46d42b903418eJohann  friend class internal::UnitTestImpl;
6821b362b15af34006e6a11974088a46d42b903418eJohann  friend TestInfo* internal::MakeAndRegisterTestInfo(
6831b362b15af34006e6a11974088a46d42b903418eJohann      const char* test_case_name, const char* name,
6841b362b15af34006e6a11974088a46d42b903418eJohann      const char* type_param,
6851b362b15af34006e6a11974088a46d42b903418eJohann      const char* value_param,
6861b362b15af34006e6a11974088a46d42b903418eJohann      internal::TypeId fixture_class_id,
6871b362b15af34006e6a11974088a46d42b903418eJohann      Test::SetUpTestCaseFunc set_up_tc,
6881b362b15af34006e6a11974088a46d42b903418eJohann      Test::TearDownTestCaseFunc tear_down_tc,
6891b362b15af34006e6a11974088a46d42b903418eJohann      internal::TestFactoryBase* factory);
6901b362b15af34006e6a11974088a46d42b903418eJohann
6911b362b15af34006e6a11974088a46d42b903418eJohann  // Constructs a TestInfo object. The newly constructed instance assumes
6921b362b15af34006e6a11974088a46d42b903418eJohann  // ownership of the factory object.
6931b362b15af34006e6a11974088a46d42b903418eJohann  TestInfo(const char* test_case_name, const char* name,
6941b362b15af34006e6a11974088a46d42b903418eJohann           const char* a_type_param,
6951b362b15af34006e6a11974088a46d42b903418eJohann           const char* a_value_param,
6961b362b15af34006e6a11974088a46d42b903418eJohann           internal::TypeId fixture_class_id,
6971b362b15af34006e6a11974088a46d42b903418eJohann           internal::TestFactoryBase* factory);
6981b362b15af34006e6a11974088a46d42b903418eJohann
6991b362b15af34006e6a11974088a46d42b903418eJohann  // Increments the number of death tests encountered in this test so
7001b362b15af34006e6a11974088a46d42b903418eJohann  // far.
7011b362b15af34006e6a11974088a46d42b903418eJohann  int increment_death_test_count() {
7021b362b15af34006e6a11974088a46d42b903418eJohann    return result_.increment_death_test_count();
7031b362b15af34006e6a11974088a46d42b903418eJohann  }
7041b362b15af34006e6a11974088a46d42b903418eJohann
7051b362b15af34006e6a11974088a46d42b903418eJohann  // Creates the test object, runs it, records its result, and then
7061b362b15af34006e6a11974088a46d42b903418eJohann  // deletes it.
7071b362b15af34006e6a11974088a46d42b903418eJohann  void Run();
7081b362b15af34006e6a11974088a46d42b903418eJohann
7091b362b15af34006e6a11974088a46d42b903418eJohann  static void ClearTestResult(TestInfo* test_info) {
7101b362b15af34006e6a11974088a46d42b903418eJohann    test_info->result_.Clear();
7111b362b15af34006e6a11974088a46d42b903418eJohann  }
7121b362b15af34006e6a11974088a46d42b903418eJohann
7131b362b15af34006e6a11974088a46d42b903418eJohann  // These fields are immutable properties of the test.
7141b362b15af34006e6a11974088a46d42b903418eJohann  const std::string test_case_name_;     // Test case name
7151b362b15af34006e6a11974088a46d42b903418eJohann  const std::string name_;               // Test name
7161b362b15af34006e6a11974088a46d42b903418eJohann  // Name of the parameter type, or NULL if this is not a typed or a
7171b362b15af34006e6a11974088a46d42b903418eJohann  // type-parameterized test.
7181b362b15af34006e6a11974088a46d42b903418eJohann  const internal::scoped_ptr<const ::std::string> type_param_;
7191b362b15af34006e6a11974088a46d42b903418eJohann  // Text representation of the value parameter, or NULL if this is not a
7201b362b15af34006e6a11974088a46d42b903418eJohann  // value-parameterized test.
7211b362b15af34006e6a11974088a46d42b903418eJohann  const internal::scoped_ptr<const ::std::string> value_param_;
7221b362b15af34006e6a11974088a46d42b903418eJohann  const internal::TypeId fixture_class_id_;   // ID of the test fixture class
7231b362b15af34006e6a11974088a46d42b903418eJohann  bool should_run_;                 // True iff this test should run
7241b362b15af34006e6a11974088a46d42b903418eJohann  bool is_disabled_;                // True iff this test is disabled
7251b362b15af34006e6a11974088a46d42b903418eJohann  bool matches_filter_;             // True if this test matches the
7261b362b15af34006e6a11974088a46d42b903418eJohann                                    // user-specified filter.
7271b362b15af34006e6a11974088a46d42b903418eJohann  internal::TestFactoryBase* const factory_;  // The factory that creates
7281b362b15af34006e6a11974088a46d42b903418eJohann                                              // the test object
7291b362b15af34006e6a11974088a46d42b903418eJohann
7301b362b15af34006e6a11974088a46d42b903418eJohann  // This field is mutable and needs to be reset before running the
7311b362b15af34006e6a11974088a46d42b903418eJohann  // test for the second time.
7321b362b15af34006e6a11974088a46d42b903418eJohann  TestResult result_;
7331b362b15af34006e6a11974088a46d42b903418eJohann
7341b362b15af34006e6a11974088a46d42b903418eJohann  GTEST_DISALLOW_COPY_AND_ASSIGN_(TestInfo);
7351b362b15af34006e6a11974088a46d42b903418eJohann};
7361b362b15af34006e6a11974088a46d42b903418eJohann
7371b362b15af34006e6a11974088a46d42b903418eJohann// A test case, which consists of a vector of TestInfos.
7381b362b15af34006e6a11974088a46d42b903418eJohann//
7391b362b15af34006e6a11974088a46d42b903418eJohann// TestCase is not copyable.
7401b362b15af34006e6a11974088a46d42b903418eJohannclass GTEST_API_ TestCase {
7411b362b15af34006e6a11974088a46d42b903418eJohann public:
7421b362b15af34006e6a11974088a46d42b903418eJohann  // Creates a TestCase with the given name.
7431b362b15af34006e6a11974088a46d42b903418eJohann  //
7441b362b15af34006e6a11974088a46d42b903418eJohann  // TestCase does NOT have a default constructor.  Always use this
7451b362b15af34006e6a11974088a46d42b903418eJohann  // constructor to create a TestCase object.
7461b362b15af34006e6a11974088a46d42b903418eJohann  //
7471b362b15af34006e6a11974088a46d42b903418eJohann  // Arguments:
7481b362b15af34006e6a11974088a46d42b903418eJohann  //
7491b362b15af34006e6a11974088a46d42b903418eJohann  //   name:         name of the test case
7501b362b15af34006e6a11974088a46d42b903418eJohann  //   a_type_param: the name of the test's type parameter, or NULL if
7511b362b15af34006e6a11974088a46d42b903418eJohann  //                 this is not a type-parameterized test.
7521b362b15af34006e6a11974088a46d42b903418eJohann  //   set_up_tc:    pointer to the function that sets up the test case
7531b362b15af34006e6a11974088a46d42b903418eJohann  //   tear_down_tc: pointer to the function that tears down the test case
7541b362b15af34006e6a11974088a46d42b903418eJohann  TestCase(const char* name, const char* a_type_param,
7551b362b15af34006e6a11974088a46d42b903418eJohann           Test::SetUpTestCaseFunc set_up_tc,
7561b362b15af34006e6a11974088a46d42b903418eJohann           Test::TearDownTestCaseFunc tear_down_tc);
7571b362b15af34006e6a11974088a46d42b903418eJohann
7581b362b15af34006e6a11974088a46d42b903418eJohann  // Destructor of TestCase.
7591b362b15af34006e6a11974088a46d42b903418eJohann  virtual ~TestCase();
7601b362b15af34006e6a11974088a46d42b903418eJohann
7611b362b15af34006e6a11974088a46d42b903418eJohann  // Gets the name of the TestCase.
7621b362b15af34006e6a11974088a46d42b903418eJohann  const char* name() const { return name_.c_str(); }
7631b362b15af34006e6a11974088a46d42b903418eJohann
7641b362b15af34006e6a11974088a46d42b903418eJohann  // Returns the name of the parameter type, or NULL if this is not a
7651b362b15af34006e6a11974088a46d42b903418eJohann  // type-parameterized test case.
7661b362b15af34006e6a11974088a46d42b903418eJohann  const char* type_param() const {
7671b362b15af34006e6a11974088a46d42b903418eJohann    if (type_param_.get() != NULL)
7681b362b15af34006e6a11974088a46d42b903418eJohann      return type_param_->c_str();
7691b362b15af34006e6a11974088a46d42b903418eJohann    return NULL;
7701b362b15af34006e6a11974088a46d42b903418eJohann  }
7711b362b15af34006e6a11974088a46d42b903418eJohann
7721b362b15af34006e6a11974088a46d42b903418eJohann  // Returns true if any test in this test case should run.
7731b362b15af34006e6a11974088a46d42b903418eJohann  bool should_run() const { return should_run_; }
7741b362b15af34006e6a11974088a46d42b903418eJohann
7751b362b15af34006e6a11974088a46d42b903418eJohann  // Gets the number of successful tests in this test case.
7761b362b15af34006e6a11974088a46d42b903418eJohann  int successful_test_count() const;
7771b362b15af34006e6a11974088a46d42b903418eJohann
7781b362b15af34006e6a11974088a46d42b903418eJohann  // Gets the number of failed tests in this test case.
7791b362b15af34006e6a11974088a46d42b903418eJohann  int failed_test_count() const;
7801b362b15af34006e6a11974088a46d42b903418eJohann
7811b362b15af34006e6a11974088a46d42b903418eJohann  // Gets the number of disabled tests in this test case.
7821b362b15af34006e6a11974088a46d42b903418eJohann  int disabled_test_count() const;
7831b362b15af34006e6a11974088a46d42b903418eJohann
7841b362b15af34006e6a11974088a46d42b903418eJohann  // Get the number of tests in this test case that should run.
7851b362b15af34006e6a11974088a46d42b903418eJohann  int test_to_run_count() const;
7861b362b15af34006e6a11974088a46d42b903418eJohann
7871b362b15af34006e6a11974088a46d42b903418eJohann  // Gets the number of all tests in this test case.
7881b362b15af34006e6a11974088a46d42b903418eJohann  int total_test_count() const;
7891b362b15af34006e6a11974088a46d42b903418eJohann
7901b362b15af34006e6a11974088a46d42b903418eJohann  // Returns true iff the test case passed.
7911b362b15af34006e6a11974088a46d42b903418eJohann  bool Passed() const { return !Failed(); }
7921b362b15af34006e6a11974088a46d42b903418eJohann
7931b362b15af34006e6a11974088a46d42b903418eJohann  // Returns true iff the test case failed.
7941b362b15af34006e6a11974088a46d42b903418eJohann  bool Failed() const { return failed_test_count() > 0; }
7951b362b15af34006e6a11974088a46d42b903418eJohann
7961b362b15af34006e6a11974088a46d42b903418eJohann  // Returns the elapsed time, in milliseconds.
7971b362b15af34006e6a11974088a46d42b903418eJohann  TimeInMillis elapsed_time() const { return elapsed_time_; }
7981b362b15af34006e6a11974088a46d42b903418eJohann
7991b362b15af34006e6a11974088a46d42b903418eJohann  // Returns the i-th test among all the tests. i can range from 0 to
8001b362b15af34006e6a11974088a46d42b903418eJohann  // total_test_count() - 1. If i is not in that range, returns NULL.
8011b362b15af34006e6a11974088a46d42b903418eJohann  const TestInfo* GetTestInfo(int i) const;
8021b362b15af34006e6a11974088a46d42b903418eJohann
8031b362b15af34006e6a11974088a46d42b903418eJohann private:
8041b362b15af34006e6a11974088a46d42b903418eJohann  friend class Test;
8051b362b15af34006e6a11974088a46d42b903418eJohann  friend class internal::UnitTestImpl;
8061b362b15af34006e6a11974088a46d42b903418eJohann
8071b362b15af34006e6a11974088a46d42b903418eJohann  // Gets the (mutable) vector of TestInfos in this TestCase.
8081b362b15af34006e6a11974088a46d42b903418eJohann  std::vector<TestInfo*>& test_info_list() { return test_info_list_; }
8091b362b15af34006e6a11974088a46d42b903418eJohann
8101b362b15af34006e6a11974088a46d42b903418eJohann  // Gets the (immutable) vector of TestInfos in this TestCase.
8111b362b15af34006e6a11974088a46d42b903418eJohann  const std::vector<TestInfo*>& test_info_list() const {
8121b362b15af34006e6a11974088a46d42b903418eJohann    return test_info_list_;
8131b362b15af34006e6a11974088a46d42b903418eJohann  }
8141b362b15af34006e6a11974088a46d42b903418eJohann
8151b362b15af34006e6a11974088a46d42b903418eJohann  // Returns the i-th test among all the tests. i can range from 0 to
8161b362b15af34006e6a11974088a46d42b903418eJohann  // total_test_count() - 1. If i is not in that range, returns NULL.
8171b362b15af34006e6a11974088a46d42b903418eJohann  TestInfo* GetMutableTestInfo(int i);
8181b362b15af34006e6a11974088a46d42b903418eJohann
8191b362b15af34006e6a11974088a46d42b903418eJohann  // Sets the should_run member.
8201b362b15af34006e6a11974088a46d42b903418eJohann  void set_should_run(bool should) { should_run_ = should; }
8211b362b15af34006e6a11974088a46d42b903418eJohann
8221b362b15af34006e6a11974088a46d42b903418eJohann  // Adds a TestInfo to this test case.  Will delete the TestInfo upon
8231b362b15af34006e6a11974088a46d42b903418eJohann  // destruction of the TestCase object.
8241b362b15af34006e6a11974088a46d42b903418eJohann  void AddTestInfo(TestInfo * test_info);
8251b362b15af34006e6a11974088a46d42b903418eJohann
8261b362b15af34006e6a11974088a46d42b903418eJohann  // Clears the results of all tests in this test case.
8271b362b15af34006e6a11974088a46d42b903418eJohann  void ClearResult();
8281b362b15af34006e6a11974088a46d42b903418eJohann
8291b362b15af34006e6a11974088a46d42b903418eJohann  // Clears the results of all tests in the given test case.
8301b362b15af34006e6a11974088a46d42b903418eJohann  static void ClearTestCaseResult(TestCase* test_case) {
8311b362b15af34006e6a11974088a46d42b903418eJohann    test_case->ClearResult();
8321b362b15af34006e6a11974088a46d42b903418eJohann  }
8331b362b15af34006e6a11974088a46d42b903418eJohann
8341b362b15af34006e6a11974088a46d42b903418eJohann  // Runs every test in this TestCase.
8351b362b15af34006e6a11974088a46d42b903418eJohann  void Run();
8361b362b15af34006e6a11974088a46d42b903418eJohann
8371b362b15af34006e6a11974088a46d42b903418eJohann  // Runs SetUpTestCase() for this TestCase.  This wrapper is needed
8381b362b15af34006e6a11974088a46d42b903418eJohann  // for catching exceptions thrown from SetUpTestCase().
8391b362b15af34006e6a11974088a46d42b903418eJohann  void RunSetUpTestCase() { (*set_up_tc_)(); }
8401b362b15af34006e6a11974088a46d42b903418eJohann
8411b362b15af34006e6a11974088a46d42b903418eJohann  // Runs TearDownTestCase() for this TestCase.  This wrapper is
8421b362b15af34006e6a11974088a46d42b903418eJohann  // needed for catching exceptions thrown from TearDownTestCase().
8431b362b15af34006e6a11974088a46d42b903418eJohann  void RunTearDownTestCase() { (*tear_down_tc_)(); }
8441b362b15af34006e6a11974088a46d42b903418eJohann
8451b362b15af34006e6a11974088a46d42b903418eJohann  // Returns true iff test passed.
8461b362b15af34006e6a11974088a46d42b903418eJohann  static bool TestPassed(const TestInfo* test_info) {
8471b362b15af34006e6a11974088a46d42b903418eJohann    return test_info->should_run() && test_info->result()->Passed();
8481b362b15af34006e6a11974088a46d42b903418eJohann  }
8491b362b15af34006e6a11974088a46d42b903418eJohann
8501b362b15af34006e6a11974088a46d42b903418eJohann  // Returns true iff test failed.
8511b362b15af34006e6a11974088a46d42b903418eJohann  static bool TestFailed(const TestInfo* test_info) {
8521b362b15af34006e6a11974088a46d42b903418eJohann    return test_info->should_run() && test_info->result()->Failed();
8531b362b15af34006e6a11974088a46d42b903418eJohann  }
8541b362b15af34006e6a11974088a46d42b903418eJohann
8551b362b15af34006e6a11974088a46d42b903418eJohann  // Returns true iff test is disabled.
8561b362b15af34006e6a11974088a46d42b903418eJohann  static bool TestDisabled(const TestInfo* test_info) {
8571b362b15af34006e6a11974088a46d42b903418eJohann    return test_info->is_disabled_;
8581b362b15af34006e6a11974088a46d42b903418eJohann  }
8591b362b15af34006e6a11974088a46d42b903418eJohann
8601b362b15af34006e6a11974088a46d42b903418eJohann  // Returns true if the given test should run.
8611b362b15af34006e6a11974088a46d42b903418eJohann  static bool ShouldRunTest(const TestInfo* test_info) {
8621b362b15af34006e6a11974088a46d42b903418eJohann    return test_info->should_run();
8631b362b15af34006e6a11974088a46d42b903418eJohann  }
8641b362b15af34006e6a11974088a46d42b903418eJohann
8651b362b15af34006e6a11974088a46d42b903418eJohann  // Shuffles the tests in this test case.
8661b362b15af34006e6a11974088a46d42b903418eJohann  void ShuffleTests(internal::Random* random);
8671b362b15af34006e6a11974088a46d42b903418eJohann
8681b362b15af34006e6a11974088a46d42b903418eJohann  // Restores the test order to before the first shuffle.
8691b362b15af34006e6a11974088a46d42b903418eJohann  void UnshuffleTests();
8701b362b15af34006e6a11974088a46d42b903418eJohann
8711b362b15af34006e6a11974088a46d42b903418eJohann  // Name of the test case.
8721b362b15af34006e6a11974088a46d42b903418eJohann  internal::String name_;
8731b362b15af34006e6a11974088a46d42b903418eJohann  // Name of the parameter type, or NULL if this is not a typed or a
8741b362b15af34006e6a11974088a46d42b903418eJohann  // type-parameterized test.
8751b362b15af34006e6a11974088a46d42b903418eJohann  const internal::scoped_ptr<const ::std::string> type_param_;
8761b362b15af34006e6a11974088a46d42b903418eJohann  // The vector of TestInfos in their original order.  It owns the
8771b362b15af34006e6a11974088a46d42b903418eJohann  // elements in the vector.
8781b362b15af34006e6a11974088a46d42b903418eJohann  std::vector<TestInfo*> test_info_list_;
8791b362b15af34006e6a11974088a46d42b903418eJohann  // Provides a level of indirection for the test list to allow easy
8801b362b15af34006e6a11974088a46d42b903418eJohann  // shuffling and restoring the test order.  The i-th element in this
8811b362b15af34006e6a11974088a46d42b903418eJohann  // vector is the index of the i-th test in the shuffled test list.
8821b362b15af34006e6a11974088a46d42b903418eJohann  std::vector<int> test_indices_;
8831b362b15af34006e6a11974088a46d42b903418eJohann  // Pointer to the function that sets up the test case.
8841b362b15af34006e6a11974088a46d42b903418eJohann  Test::SetUpTestCaseFunc set_up_tc_;
8851b362b15af34006e6a11974088a46d42b903418eJohann  // Pointer to the function that tears down the test case.
8861b362b15af34006e6a11974088a46d42b903418eJohann  Test::TearDownTestCaseFunc tear_down_tc_;
8871b362b15af34006e6a11974088a46d42b903418eJohann  // True iff any test in this test case should run.
8881b362b15af34006e6a11974088a46d42b903418eJohann  bool should_run_;
8891b362b15af34006e6a11974088a46d42b903418eJohann  // Elapsed time, in milliseconds.
8901b362b15af34006e6a11974088a46d42b903418eJohann  TimeInMillis elapsed_time_;
8911b362b15af34006e6a11974088a46d42b903418eJohann
8921b362b15af34006e6a11974088a46d42b903418eJohann  // We disallow copying TestCases.
8931b362b15af34006e6a11974088a46d42b903418eJohann  GTEST_DISALLOW_COPY_AND_ASSIGN_(TestCase);
8941b362b15af34006e6a11974088a46d42b903418eJohann};
8951b362b15af34006e6a11974088a46d42b903418eJohann
8961b362b15af34006e6a11974088a46d42b903418eJohann// An Environment object is capable of setting up and tearing down an
8971b362b15af34006e6a11974088a46d42b903418eJohann// environment.  The user should subclass this to define his own
8981b362b15af34006e6a11974088a46d42b903418eJohann// environment(s).
8991b362b15af34006e6a11974088a46d42b903418eJohann//
9001b362b15af34006e6a11974088a46d42b903418eJohann// An Environment object does the set-up and tear-down in virtual
9011b362b15af34006e6a11974088a46d42b903418eJohann// methods SetUp() and TearDown() instead of the constructor and the
9021b362b15af34006e6a11974088a46d42b903418eJohann// destructor, as:
9031b362b15af34006e6a11974088a46d42b903418eJohann//
9041b362b15af34006e6a11974088a46d42b903418eJohann//   1. You cannot safely throw from a destructor.  This is a problem
9051b362b15af34006e6a11974088a46d42b903418eJohann//      as in some cases Google Test is used where exceptions are enabled, and
9061b362b15af34006e6a11974088a46d42b903418eJohann//      we may want to implement ASSERT_* using exceptions where they are
9071b362b15af34006e6a11974088a46d42b903418eJohann//      available.
9081b362b15af34006e6a11974088a46d42b903418eJohann//   2. You cannot use ASSERT_* directly in a constructor or
9091b362b15af34006e6a11974088a46d42b903418eJohann//      destructor.
9101b362b15af34006e6a11974088a46d42b903418eJohannclass Environment {
9111b362b15af34006e6a11974088a46d42b903418eJohann public:
9121b362b15af34006e6a11974088a46d42b903418eJohann  // The d'tor is virtual as we need to subclass Environment.
9131b362b15af34006e6a11974088a46d42b903418eJohann  virtual ~Environment() {}
9141b362b15af34006e6a11974088a46d42b903418eJohann
9151b362b15af34006e6a11974088a46d42b903418eJohann  // Override this to define how to set up the environment.
9161b362b15af34006e6a11974088a46d42b903418eJohann  virtual void SetUp() {}
9171b362b15af34006e6a11974088a46d42b903418eJohann
9181b362b15af34006e6a11974088a46d42b903418eJohann  // Override this to define how to tear down the environment.
9191b362b15af34006e6a11974088a46d42b903418eJohann  virtual void TearDown() {}
9201b362b15af34006e6a11974088a46d42b903418eJohann private:
9211b362b15af34006e6a11974088a46d42b903418eJohann  // If you see an error about overriding the following function or
9221b362b15af34006e6a11974088a46d42b903418eJohann  // about it being private, you have mis-spelled SetUp() as Setup().
9231b362b15af34006e6a11974088a46d42b903418eJohann  struct Setup_should_be_spelled_SetUp {};
9241b362b15af34006e6a11974088a46d42b903418eJohann  virtual Setup_should_be_spelled_SetUp* Setup() { return NULL; }
9251b362b15af34006e6a11974088a46d42b903418eJohann};
9261b362b15af34006e6a11974088a46d42b903418eJohann
9271b362b15af34006e6a11974088a46d42b903418eJohann// The interface for tracing execution of tests. The methods are organized in
9281b362b15af34006e6a11974088a46d42b903418eJohann// the order the corresponding events are fired.
9291b362b15af34006e6a11974088a46d42b903418eJohannclass TestEventListener {
9301b362b15af34006e6a11974088a46d42b903418eJohann public:
9311b362b15af34006e6a11974088a46d42b903418eJohann  virtual ~TestEventListener() {}
9321b362b15af34006e6a11974088a46d42b903418eJohann
9331b362b15af34006e6a11974088a46d42b903418eJohann  // Fired before any test activity starts.
9341b362b15af34006e6a11974088a46d42b903418eJohann  virtual void OnTestProgramStart(const UnitTest& unit_test) = 0;
9351b362b15af34006e6a11974088a46d42b903418eJohann
9361b362b15af34006e6a11974088a46d42b903418eJohann  // Fired before each iteration of tests starts.  There may be more than
9371b362b15af34006e6a11974088a46d42b903418eJohann  // one iteration if GTEST_FLAG(repeat) is set. iteration is the iteration
9381b362b15af34006e6a11974088a46d42b903418eJohann  // index, starting from 0.
9391b362b15af34006e6a11974088a46d42b903418eJohann  virtual void OnTestIterationStart(const UnitTest& unit_test,
9401b362b15af34006e6a11974088a46d42b903418eJohann                                    int iteration) = 0;
9411b362b15af34006e6a11974088a46d42b903418eJohann
9421b362b15af34006e6a11974088a46d42b903418eJohann  // Fired before environment set-up for each iteration of tests starts.
9431b362b15af34006e6a11974088a46d42b903418eJohann  virtual void OnEnvironmentsSetUpStart(const UnitTest& unit_test) = 0;
9441b362b15af34006e6a11974088a46d42b903418eJohann
9451b362b15af34006e6a11974088a46d42b903418eJohann  // Fired after environment set-up for each iteration of tests ends.
9461b362b15af34006e6a11974088a46d42b903418eJohann  virtual void OnEnvironmentsSetUpEnd(const UnitTest& unit_test) = 0;
9471b362b15af34006e6a11974088a46d42b903418eJohann
9481b362b15af34006e6a11974088a46d42b903418eJohann  // Fired before the test case starts.
9491b362b15af34006e6a11974088a46d42b903418eJohann  virtual void OnTestCaseStart(const TestCase& test_case) = 0;
9501b362b15af34006e6a11974088a46d42b903418eJohann
9511b362b15af34006e6a11974088a46d42b903418eJohann  // Fired before the test starts.
9521b362b15af34006e6a11974088a46d42b903418eJohann  virtual void OnTestStart(const TestInfo& test_info) = 0;
9531b362b15af34006e6a11974088a46d42b903418eJohann
9541b362b15af34006e6a11974088a46d42b903418eJohann  // Fired after a failed assertion or a SUCCEED() invocation.
9551b362b15af34006e6a11974088a46d42b903418eJohann  virtual void OnTestPartResult(const TestPartResult& test_part_result) = 0;
9561b362b15af34006e6a11974088a46d42b903418eJohann
9571b362b15af34006e6a11974088a46d42b903418eJohann  // Fired after the test ends.
9581b362b15af34006e6a11974088a46d42b903418eJohann  virtual void OnTestEnd(const TestInfo& test_info) = 0;
9591b362b15af34006e6a11974088a46d42b903418eJohann
9601b362b15af34006e6a11974088a46d42b903418eJohann  // Fired after the test case ends.
9611b362b15af34006e6a11974088a46d42b903418eJohann  virtual void OnTestCaseEnd(const TestCase& test_case) = 0;
9621b362b15af34006e6a11974088a46d42b903418eJohann
9631b362b15af34006e6a11974088a46d42b903418eJohann  // Fired before environment tear-down for each iteration of tests starts.
9641b362b15af34006e6a11974088a46d42b903418eJohann  virtual void OnEnvironmentsTearDownStart(const UnitTest& unit_test) = 0;
9651b362b15af34006e6a11974088a46d42b903418eJohann
9661b362b15af34006e6a11974088a46d42b903418eJohann  // Fired after environment tear-down for each iteration of tests ends.
9671b362b15af34006e6a11974088a46d42b903418eJohann  virtual void OnEnvironmentsTearDownEnd(const UnitTest& unit_test) = 0;
9681b362b15af34006e6a11974088a46d42b903418eJohann
9691b362b15af34006e6a11974088a46d42b903418eJohann  // Fired after each iteration of tests finishes.
9701b362b15af34006e6a11974088a46d42b903418eJohann  virtual void OnTestIterationEnd(const UnitTest& unit_test,
9711b362b15af34006e6a11974088a46d42b903418eJohann                                  int iteration) = 0;
9721b362b15af34006e6a11974088a46d42b903418eJohann
9731b362b15af34006e6a11974088a46d42b903418eJohann  // Fired after all test activities have ended.
9741b362b15af34006e6a11974088a46d42b903418eJohann  virtual void OnTestProgramEnd(const UnitTest& unit_test) = 0;
9751b362b15af34006e6a11974088a46d42b903418eJohann};
9761b362b15af34006e6a11974088a46d42b903418eJohann
9771b362b15af34006e6a11974088a46d42b903418eJohann// The convenience class for users who need to override just one or two
9781b362b15af34006e6a11974088a46d42b903418eJohann// methods and are not concerned that a possible change to a signature of
9791b362b15af34006e6a11974088a46d42b903418eJohann// the methods they override will not be caught during the build.  For
9801b362b15af34006e6a11974088a46d42b903418eJohann// comments about each method please see the definition of TestEventListener
9811b362b15af34006e6a11974088a46d42b903418eJohann// above.
9821b362b15af34006e6a11974088a46d42b903418eJohannclass EmptyTestEventListener : public TestEventListener {
9831b362b15af34006e6a11974088a46d42b903418eJohann public:
9841b362b15af34006e6a11974088a46d42b903418eJohann  virtual void OnTestProgramStart(const UnitTest& /*unit_test*/) {}
9851b362b15af34006e6a11974088a46d42b903418eJohann  virtual void OnTestIterationStart(const UnitTest& /*unit_test*/,
9861b362b15af34006e6a11974088a46d42b903418eJohann                                    int /*iteration*/) {}
9871b362b15af34006e6a11974088a46d42b903418eJohann  virtual void OnEnvironmentsSetUpStart(const UnitTest& /*unit_test*/) {}
9881b362b15af34006e6a11974088a46d42b903418eJohann  virtual void OnEnvironmentsSetUpEnd(const UnitTest& /*unit_test*/) {}
9891b362b15af34006e6a11974088a46d42b903418eJohann  virtual void OnTestCaseStart(const TestCase& /*test_case*/) {}
9901b362b15af34006e6a11974088a46d42b903418eJohann  virtual void OnTestStart(const TestInfo& /*test_info*/) {}
9911b362b15af34006e6a11974088a46d42b903418eJohann  virtual void OnTestPartResult(const TestPartResult& /*test_part_result*/) {}
9921b362b15af34006e6a11974088a46d42b903418eJohann  virtual void OnTestEnd(const TestInfo& /*test_info*/) {}
9931b362b15af34006e6a11974088a46d42b903418eJohann  virtual void OnTestCaseEnd(const TestCase& /*test_case*/) {}
9941b362b15af34006e6a11974088a46d42b903418eJohann  virtual void OnEnvironmentsTearDownStart(const UnitTest& /*unit_test*/) {}
9951b362b15af34006e6a11974088a46d42b903418eJohann  virtual void OnEnvironmentsTearDownEnd(const UnitTest& /*unit_test*/) {}
9961b362b15af34006e6a11974088a46d42b903418eJohann  virtual void OnTestIterationEnd(const UnitTest& /*unit_test*/,
9971b362b15af34006e6a11974088a46d42b903418eJohann                                  int /*iteration*/) {}
9981b362b15af34006e6a11974088a46d42b903418eJohann  virtual void OnTestProgramEnd(const UnitTest& /*unit_test*/) {}
9991b362b15af34006e6a11974088a46d42b903418eJohann};
10001b362b15af34006e6a11974088a46d42b903418eJohann
10011b362b15af34006e6a11974088a46d42b903418eJohann// TestEventListeners lets users add listeners to track events in Google Test.
10021b362b15af34006e6a11974088a46d42b903418eJohannclass GTEST_API_ TestEventListeners {
10031b362b15af34006e6a11974088a46d42b903418eJohann public:
10041b362b15af34006e6a11974088a46d42b903418eJohann  TestEventListeners();
10051b362b15af34006e6a11974088a46d42b903418eJohann  ~TestEventListeners();
10061b362b15af34006e6a11974088a46d42b903418eJohann
10071b362b15af34006e6a11974088a46d42b903418eJohann  // Appends an event listener to the end of the list. Google Test assumes
10081b362b15af34006e6a11974088a46d42b903418eJohann  // the ownership of the listener (i.e. it will delete the listener when
10091b362b15af34006e6a11974088a46d42b903418eJohann  // the test program finishes).
10101b362b15af34006e6a11974088a46d42b903418eJohann  void Append(TestEventListener* listener);
10111b362b15af34006e6a11974088a46d42b903418eJohann
10121b362b15af34006e6a11974088a46d42b903418eJohann  // Removes the given event listener from the list and returns it.  It then
10131b362b15af34006e6a11974088a46d42b903418eJohann  // becomes the caller's responsibility to delete the listener. Returns
10141b362b15af34006e6a11974088a46d42b903418eJohann  // NULL if the listener is not found in the list.
10151b362b15af34006e6a11974088a46d42b903418eJohann  TestEventListener* Release(TestEventListener* listener);
10161b362b15af34006e6a11974088a46d42b903418eJohann
10171b362b15af34006e6a11974088a46d42b903418eJohann  // Returns the standard listener responsible for the default console
10181b362b15af34006e6a11974088a46d42b903418eJohann  // output.  Can be removed from the listeners list to shut down default
10191b362b15af34006e6a11974088a46d42b903418eJohann  // console output.  Note that removing this object from the listener list
10201b362b15af34006e6a11974088a46d42b903418eJohann  // with Release transfers its ownership to the caller and makes this
10211b362b15af34006e6a11974088a46d42b903418eJohann  // function return NULL the next time.
10221b362b15af34006e6a11974088a46d42b903418eJohann  TestEventListener* default_result_printer() const {
10231b362b15af34006e6a11974088a46d42b903418eJohann    return default_result_printer_;
10241b362b15af34006e6a11974088a46d42b903418eJohann  }
10251b362b15af34006e6a11974088a46d42b903418eJohann
10261b362b15af34006e6a11974088a46d42b903418eJohann  // Returns the standard listener responsible for the default XML output
10271b362b15af34006e6a11974088a46d42b903418eJohann  // controlled by the --gtest_output=xml flag.  Can be removed from the
10281b362b15af34006e6a11974088a46d42b903418eJohann  // listeners list by users who want to shut down the default XML output
10291b362b15af34006e6a11974088a46d42b903418eJohann  // controlled by this flag and substitute it with custom one.  Note that
10301b362b15af34006e6a11974088a46d42b903418eJohann  // removing this object from the listener list with Release transfers its
10311b362b15af34006e6a11974088a46d42b903418eJohann  // ownership to the caller and makes this function return NULL the next
10321b362b15af34006e6a11974088a46d42b903418eJohann  // time.
10331b362b15af34006e6a11974088a46d42b903418eJohann  TestEventListener* default_xml_generator() const {
10341b362b15af34006e6a11974088a46d42b903418eJohann    return default_xml_generator_;
10351b362b15af34006e6a11974088a46d42b903418eJohann  }
10361b362b15af34006e6a11974088a46d42b903418eJohann
10371b362b15af34006e6a11974088a46d42b903418eJohann private:
10381b362b15af34006e6a11974088a46d42b903418eJohann  friend class TestCase;
10391b362b15af34006e6a11974088a46d42b903418eJohann  friend class TestInfo;
10401b362b15af34006e6a11974088a46d42b903418eJohann  friend class internal::DefaultGlobalTestPartResultReporter;
10411b362b15af34006e6a11974088a46d42b903418eJohann  friend class internal::NoExecDeathTest;
10421b362b15af34006e6a11974088a46d42b903418eJohann  friend class internal::TestEventListenersAccessor;
10431b362b15af34006e6a11974088a46d42b903418eJohann  friend class internal::UnitTestImpl;
10441b362b15af34006e6a11974088a46d42b903418eJohann
10451b362b15af34006e6a11974088a46d42b903418eJohann  // Returns repeater that broadcasts the TestEventListener events to all
10461b362b15af34006e6a11974088a46d42b903418eJohann  // subscribers.
10471b362b15af34006e6a11974088a46d42b903418eJohann  TestEventListener* repeater();
10481b362b15af34006e6a11974088a46d42b903418eJohann
10491b362b15af34006e6a11974088a46d42b903418eJohann  // Sets the default_result_printer attribute to the provided listener.
10501b362b15af34006e6a11974088a46d42b903418eJohann  // The listener is also added to the listener list and previous
10511b362b15af34006e6a11974088a46d42b903418eJohann  // default_result_printer is removed from it and deleted. The listener can
10521b362b15af34006e6a11974088a46d42b903418eJohann  // also be NULL in which case it will not be added to the list. Does
10531b362b15af34006e6a11974088a46d42b903418eJohann  // nothing if the previous and the current listener objects are the same.
10541b362b15af34006e6a11974088a46d42b903418eJohann  void SetDefaultResultPrinter(TestEventListener* listener);
10551b362b15af34006e6a11974088a46d42b903418eJohann
10561b362b15af34006e6a11974088a46d42b903418eJohann  // Sets the default_xml_generator attribute to the provided listener.  The
10571b362b15af34006e6a11974088a46d42b903418eJohann  // listener is also added to the listener list and previous
10581b362b15af34006e6a11974088a46d42b903418eJohann  // default_xml_generator is removed from it and deleted. The listener can
10591b362b15af34006e6a11974088a46d42b903418eJohann  // also be NULL in which case it will not be added to the list. Does
10601b362b15af34006e6a11974088a46d42b903418eJohann  // nothing if the previous and the current listener objects are the same.
10611b362b15af34006e6a11974088a46d42b903418eJohann  void SetDefaultXmlGenerator(TestEventListener* listener);
10621b362b15af34006e6a11974088a46d42b903418eJohann
10631b362b15af34006e6a11974088a46d42b903418eJohann  // Controls whether events will be forwarded by the repeater to the
10641b362b15af34006e6a11974088a46d42b903418eJohann  // listeners in the list.
10651b362b15af34006e6a11974088a46d42b903418eJohann  bool EventForwardingEnabled() const;
10661b362b15af34006e6a11974088a46d42b903418eJohann  void SuppressEventForwarding();
10671b362b15af34006e6a11974088a46d42b903418eJohann
10681b362b15af34006e6a11974088a46d42b903418eJohann  // The actual list of listeners.
10691b362b15af34006e6a11974088a46d42b903418eJohann  internal::TestEventRepeater* repeater_;
10701b362b15af34006e6a11974088a46d42b903418eJohann  // Listener responsible for the standard result output.
10711b362b15af34006e6a11974088a46d42b903418eJohann  TestEventListener* default_result_printer_;
10721b362b15af34006e6a11974088a46d42b903418eJohann  // Listener responsible for the creation of the XML output file.
10731b362b15af34006e6a11974088a46d42b903418eJohann  TestEventListener* default_xml_generator_;
10741b362b15af34006e6a11974088a46d42b903418eJohann
10751b362b15af34006e6a11974088a46d42b903418eJohann  // We disallow copying TestEventListeners.
10761b362b15af34006e6a11974088a46d42b903418eJohann  GTEST_DISALLOW_COPY_AND_ASSIGN_(TestEventListeners);
10771b362b15af34006e6a11974088a46d42b903418eJohann};
10781b362b15af34006e6a11974088a46d42b903418eJohann
10791b362b15af34006e6a11974088a46d42b903418eJohann// A UnitTest consists of a vector of TestCases.
10801b362b15af34006e6a11974088a46d42b903418eJohann//
10811b362b15af34006e6a11974088a46d42b903418eJohann// This is a singleton class.  The only instance of UnitTest is
10821b362b15af34006e6a11974088a46d42b903418eJohann// created when UnitTest::GetInstance() is first called.  This
10831b362b15af34006e6a11974088a46d42b903418eJohann// instance is never deleted.
10841b362b15af34006e6a11974088a46d42b903418eJohann//
10851b362b15af34006e6a11974088a46d42b903418eJohann// UnitTest is not copyable.
10861b362b15af34006e6a11974088a46d42b903418eJohann//
10871b362b15af34006e6a11974088a46d42b903418eJohann// This class is thread-safe as long as the methods are called
10881b362b15af34006e6a11974088a46d42b903418eJohann// according to their specification.
10891b362b15af34006e6a11974088a46d42b903418eJohannclass GTEST_API_ UnitTest {
10901b362b15af34006e6a11974088a46d42b903418eJohann public:
10911b362b15af34006e6a11974088a46d42b903418eJohann  // Gets the singleton UnitTest object.  The first time this method
10921b362b15af34006e6a11974088a46d42b903418eJohann  // is called, a UnitTest object is constructed and returned.
10931b362b15af34006e6a11974088a46d42b903418eJohann  // Consecutive calls will return the same object.
10941b362b15af34006e6a11974088a46d42b903418eJohann  static UnitTest* GetInstance();
10951b362b15af34006e6a11974088a46d42b903418eJohann
10961b362b15af34006e6a11974088a46d42b903418eJohann  // Runs all tests in this UnitTest object and prints the result.
10971b362b15af34006e6a11974088a46d42b903418eJohann  // Returns 0 if successful, or 1 otherwise.
10981b362b15af34006e6a11974088a46d42b903418eJohann  //
10991b362b15af34006e6a11974088a46d42b903418eJohann  // This method can only be called from the main thread.
11001b362b15af34006e6a11974088a46d42b903418eJohann  //
11011b362b15af34006e6a11974088a46d42b903418eJohann  // INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM.
11021b362b15af34006e6a11974088a46d42b903418eJohann  int Run() GTEST_MUST_USE_RESULT_;
11031b362b15af34006e6a11974088a46d42b903418eJohann
11041b362b15af34006e6a11974088a46d42b903418eJohann  // Returns the working directory when the first TEST() or TEST_F()
11051b362b15af34006e6a11974088a46d42b903418eJohann  // was executed.  The UnitTest object owns the string.
11061b362b15af34006e6a11974088a46d42b903418eJohann  const char* original_working_dir() const;
11071b362b15af34006e6a11974088a46d42b903418eJohann
11081b362b15af34006e6a11974088a46d42b903418eJohann  // Returns the TestCase object for the test that's currently running,
11091b362b15af34006e6a11974088a46d42b903418eJohann  // or NULL if no test is running.
11101b362b15af34006e6a11974088a46d42b903418eJohann  const TestCase* current_test_case() const;
11111b362b15af34006e6a11974088a46d42b903418eJohann
11121b362b15af34006e6a11974088a46d42b903418eJohann  // Returns the TestInfo object for the test that's currently running,
11131b362b15af34006e6a11974088a46d42b903418eJohann  // or NULL if no test is running.
11141b362b15af34006e6a11974088a46d42b903418eJohann  const TestInfo* current_test_info() const;
11151b362b15af34006e6a11974088a46d42b903418eJohann
11161b362b15af34006e6a11974088a46d42b903418eJohann  // Returns the random seed used at the start of the current test run.
11171b362b15af34006e6a11974088a46d42b903418eJohann  int random_seed() const;
11181b362b15af34006e6a11974088a46d42b903418eJohann
11191b362b15af34006e6a11974088a46d42b903418eJohann#if GTEST_HAS_PARAM_TEST
11201b362b15af34006e6a11974088a46d42b903418eJohann  // Returns the ParameterizedTestCaseRegistry object used to keep track of
11211b362b15af34006e6a11974088a46d42b903418eJohann  // value-parameterized tests and instantiate and register them.
11221b362b15af34006e6a11974088a46d42b903418eJohann  //
11231b362b15af34006e6a11974088a46d42b903418eJohann  // INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM.
11241b362b15af34006e6a11974088a46d42b903418eJohann  internal::ParameterizedTestCaseRegistry& parameterized_test_registry();
11251b362b15af34006e6a11974088a46d42b903418eJohann#endif  // GTEST_HAS_PARAM_TEST
11261b362b15af34006e6a11974088a46d42b903418eJohann
11271b362b15af34006e6a11974088a46d42b903418eJohann  // Gets the number of successful test cases.
11281b362b15af34006e6a11974088a46d42b903418eJohann  int successful_test_case_count() const;
11291b362b15af34006e6a11974088a46d42b903418eJohann
11301b362b15af34006e6a11974088a46d42b903418eJohann  // Gets the number of failed test cases.
11311b362b15af34006e6a11974088a46d42b903418eJohann  int failed_test_case_count() const;
11321b362b15af34006e6a11974088a46d42b903418eJohann
11331b362b15af34006e6a11974088a46d42b903418eJohann  // Gets the number of all test cases.
11341b362b15af34006e6a11974088a46d42b903418eJohann  int total_test_case_count() const;
11351b362b15af34006e6a11974088a46d42b903418eJohann
11361b362b15af34006e6a11974088a46d42b903418eJohann  // Gets the number of all test cases that contain at least one test
11371b362b15af34006e6a11974088a46d42b903418eJohann  // that should run.
11381b362b15af34006e6a11974088a46d42b903418eJohann  int test_case_to_run_count() const;
11391b362b15af34006e6a11974088a46d42b903418eJohann
11401b362b15af34006e6a11974088a46d42b903418eJohann  // Gets the number of successful tests.
11411b362b15af34006e6a11974088a46d42b903418eJohann  int successful_test_count() const;
11421b362b15af34006e6a11974088a46d42b903418eJohann
11431b362b15af34006e6a11974088a46d42b903418eJohann  // Gets the number of failed tests.
11441b362b15af34006e6a11974088a46d42b903418eJohann  int failed_test_count() const;
11451b362b15af34006e6a11974088a46d42b903418eJohann
11461b362b15af34006e6a11974088a46d42b903418eJohann  // Gets the number of disabled tests.
11471b362b15af34006e6a11974088a46d42b903418eJohann  int disabled_test_count() const;
11481b362b15af34006e6a11974088a46d42b903418eJohann
11491b362b15af34006e6a11974088a46d42b903418eJohann  // Gets the number of all tests.
11501b362b15af34006e6a11974088a46d42b903418eJohann  int total_test_count() const;
11511b362b15af34006e6a11974088a46d42b903418eJohann
11521b362b15af34006e6a11974088a46d42b903418eJohann  // Gets the number of tests that should run.
11531b362b15af34006e6a11974088a46d42b903418eJohann  int test_to_run_count() const;
11541b362b15af34006e6a11974088a46d42b903418eJohann
11551b362b15af34006e6a11974088a46d42b903418eJohann  // Gets the elapsed time, in milliseconds.
11561b362b15af34006e6a11974088a46d42b903418eJohann  TimeInMillis elapsed_time() const;
11571b362b15af34006e6a11974088a46d42b903418eJohann
11581b362b15af34006e6a11974088a46d42b903418eJohann  // Returns true iff the unit test passed (i.e. all test cases passed).
11591b362b15af34006e6a11974088a46d42b903418eJohann  bool Passed() const;
11601b362b15af34006e6a11974088a46d42b903418eJohann
11611b362b15af34006e6a11974088a46d42b903418eJohann  // Returns true iff the unit test failed (i.e. some test case failed
11621b362b15af34006e6a11974088a46d42b903418eJohann  // or something outside of all tests failed).
11631b362b15af34006e6a11974088a46d42b903418eJohann  bool Failed() const;
11641b362b15af34006e6a11974088a46d42b903418eJohann
11651b362b15af34006e6a11974088a46d42b903418eJohann  // Gets the i-th test case among all the test cases. i can range from 0 to
11661b362b15af34006e6a11974088a46d42b903418eJohann  // total_test_case_count() - 1. If i is not in that range, returns NULL.
11671b362b15af34006e6a11974088a46d42b903418eJohann  const TestCase* GetTestCase(int i) const;
11681b362b15af34006e6a11974088a46d42b903418eJohann
11691b362b15af34006e6a11974088a46d42b903418eJohann  // Returns the list of event listeners that can be used to track events
11701b362b15af34006e6a11974088a46d42b903418eJohann  // inside Google Test.
11711b362b15af34006e6a11974088a46d42b903418eJohann  TestEventListeners& listeners();
11721b362b15af34006e6a11974088a46d42b903418eJohann
11731b362b15af34006e6a11974088a46d42b903418eJohann private:
11741b362b15af34006e6a11974088a46d42b903418eJohann  // Registers and returns a global test environment.  When a test
11751b362b15af34006e6a11974088a46d42b903418eJohann  // program is run, all global test environments will be set-up in
11761b362b15af34006e6a11974088a46d42b903418eJohann  // the order they were registered.  After all tests in the program
11771b362b15af34006e6a11974088a46d42b903418eJohann  // have finished, all global test environments will be torn-down in
11781b362b15af34006e6a11974088a46d42b903418eJohann  // the *reverse* order they were registered.
11791b362b15af34006e6a11974088a46d42b903418eJohann  //
11801b362b15af34006e6a11974088a46d42b903418eJohann  // The UnitTest object takes ownership of the given environment.
11811b362b15af34006e6a11974088a46d42b903418eJohann  //
11821b362b15af34006e6a11974088a46d42b903418eJohann  // This method can only be called from the main thread.
11831b362b15af34006e6a11974088a46d42b903418eJohann  Environment* AddEnvironment(Environment* env);
11841b362b15af34006e6a11974088a46d42b903418eJohann
11851b362b15af34006e6a11974088a46d42b903418eJohann  // Adds a TestPartResult to the current TestResult object.  All
11861b362b15af34006e6a11974088a46d42b903418eJohann  // Google Test assertion macros (e.g. ASSERT_TRUE, EXPECT_EQ, etc)
11871b362b15af34006e6a11974088a46d42b903418eJohann  // eventually call this to report their results.  The user code
11881b362b15af34006e6a11974088a46d42b903418eJohann  // should use the assertion macros instead of calling this directly.
11891b362b15af34006e6a11974088a46d42b903418eJohann  void AddTestPartResult(TestPartResult::Type result_type,
11901b362b15af34006e6a11974088a46d42b903418eJohann                         const char* file_name,
11911b362b15af34006e6a11974088a46d42b903418eJohann                         int line_number,
11921b362b15af34006e6a11974088a46d42b903418eJohann                         const internal::String& message,
11931b362b15af34006e6a11974088a46d42b903418eJohann                         const internal::String& os_stack_trace);
11941b362b15af34006e6a11974088a46d42b903418eJohann
11951b362b15af34006e6a11974088a46d42b903418eJohann  // Adds a TestProperty to the current TestResult object. If the result already
11961b362b15af34006e6a11974088a46d42b903418eJohann  // contains a property with the same key, the value will be updated.
11971b362b15af34006e6a11974088a46d42b903418eJohann  void RecordPropertyForCurrentTest(const char* key, const char* value);
11981b362b15af34006e6a11974088a46d42b903418eJohann
11991b362b15af34006e6a11974088a46d42b903418eJohann  // Gets the i-th test case among all the test cases. i can range from 0 to
12001b362b15af34006e6a11974088a46d42b903418eJohann  // total_test_case_count() - 1. If i is not in that range, returns NULL.
12011b362b15af34006e6a11974088a46d42b903418eJohann  TestCase* GetMutableTestCase(int i);
12021b362b15af34006e6a11974088a46d42b903418eJohann
12031b362b15af34006e6a11974088a46d42b903418eJohann  // Accessors for the implementation object.
12041b362b15af34006e6a11974088a46d42b903418eJohann  internal::UnitTestImpl* impl() { return impl_; }
12051b362b15af34006e6a11974088a46d42b903418eJohann  const internal::UnitTestImpl* impl() const { return impl_; }
12061b362b15af34006e6a11974088a46d42b903418eJohann
12071b362b15af34006e6a11974088a46d42b903418eJohann  // These classes and funcions are friends as they need to access private
12081b362b15af34006e6a11974088a46d42b903418eJohann  // members of UnitTest.
12091b362b15af34006e6a11974088a46d42b903418eJohann  friend class Test;
12101b362b15af34006e6a11974088a46d42b903418eJohann  friend class internal::AssertHelper;
12111b362b15af34006e6a11974088a46d42b903418eJohann  friend class internal::ScopedTrace;
12121b362b15af34006e6a11974088a46d42b903418eJohann  friend Environment* AddGlobalTestEnvironment(Environment* env);
12131b362b15af34006e6a11974088a46d42b903418eJohann  friend internal::UnitTestImpl* internal::GetUnitTestImpl();
12141b362b15af34006e6a11974088a46d42b903418eJohann  friend void internal::ReportFailureInUnknownLocation(
12151b362b15af34006e6a11974088a46d42b903418eJohann      TestPartResult::Type result_type,
12161b362b15af34006e6a11974088a46d42b903418eJohann      const internal::String& message);
12171b362b15af34006e6a11974088a46d42b903418eJohann
12181b362b15af34006e6a11974088a46d42b903418eJohann  // Creates an empty UnitTest.
12191b362b15af34006e6a11974088a46d42b903418eJohann  UnitTest();
12201b362b15af34006e6a11974088a46d42b903418eJohann
12211b362b15af34006e6a11974088a46d42b903418eJohann  // D'tor
12221b362b15af34006e6a11974088a46d42b903418eJohann  virtual ~UnitTest();
12231b362b15af34006e6a11974088a46d42b903418eJohann
12241b362b15af34006e6a11974088a46d42b903418eJohann  // Pushes a trace defined by SCOPED_TRACE() on to the per-thread
12251b362b15af34006e6a11974088a46d42b903418eJohann  // Google Test trace stack.
12261b362b15af34006e6a11974088a46d42b903418eJohann  void PushGTestTrace(const internal::TraceInfo& trace);
12271b362b15af34006e6a11974088a46d42b903418eJohann
12281b362b15af34006e6a11974088a46d42b903418eJohann  // Pops a trace from the per-thread Google Test trace stack.
12291b362b15af34006e6a11974088a46d42b903418eJohann  void PopGTestTrace();
12301b362b15af34006e6a11974088a46d42b903418eJohann
12311b362b15af34006e6a11974088a46d42b903418eJohann  // Protects mutable state in *impl_.  This is mutable as some const
12321b362b15af34006e6a11974088a46d42b903418eJohann  // methods need to lock it too.
12331b362b15af34006e6a11974088a46d42b903418eJohann  mutable internal::Mutex mutex_;
12341b362b15af34006e6a11974088a46d42b903418eJohann
12351b362b15af34006e6a11974088a46d42b903418eJohann  // Opaque implementation object.  This field is never changed once
12361b362b15af34006e6a11974088a46d42b903418eJohann  // the object is constructed.  We don't mark it as const here, as
12371b362b15af34006e6a11974088a46d42b903418eJohann  // doing so will cause a warning in the constructor of UnitTest.
12381b362b15af34006e6a11974088a46d42b903418eJohann  // Mutable state in *impl_ is protected by mutex_.
12391b362b15af34006e6a11974088a46d42b903418eJohann  internal::UnitTestImpl* impl_;
12401b362b15af34006e6a11974088a46d42b903418eJohann
12411b362b15af34006e6a11974088a46d42b903418eJohann  // We disallow copying UnitTest.
12421b362b15af34006e6a11974088a46d42b903418eJohann  GTEST_DISALLOW_COPY_AND_ASSIGN_(UnitTest);
12431b362b15af34006e6a11974088a46d42b903418eJohann};
12441b362b15af34006e6a11974088a46d42b903418eJohann
12451b362b15af34006e6a11974088a46d42b903418eJohann// A convenient wrapper for adding an environment for the test
12461b362b15af34006e6a11974088a46d42b903418eJohann// program.
12471b362b15af34006e6a11974088a46d42b903418eJohann//
12481b362b15af34006e6a11974088a46d42b903418eJohann// You should call this before RUN_ALL_TESTS() is called, probably in
12491b362b15af34006e6a11974088a46d42b903418eJohann// main().  If you use gtest_main, you need to call this before main()
12501b362b15af34006e6a11974088a46d42b903418eJohann// starts for it to take effect.  For example, you can define a global
12511b362b15af34006e6a11974088a46d42b903418eJohann// variable like this:
12521b362b15af34006e6a11974088a46d42b903418eJohann//
12531b362b15af34006e6a11974088a46d42b903418eJohann//   testing::Environment* const foo_env =
12541b362b15af34006e6a11974088a46d42b903418eJohann//       testing::AddGlobalTestEnvironment(new FooEnvironment);
12551b362b15af34006e6a11974088a46d42b903418eJohann//
12561b362b15af34006e6a11974088a46d42b903418eJohann// However, we strongly recommend you to write your own main() and
12571b362b15af34006e6a11974088a46d42b903418eJohann// call AddGlobalTestEnvironment() there, as relying on initialization
12581b362b15af34006e6a11974088a46d42b903418eJohann// of global variables makes the code harder to read and may cause
12591b362b15af34006e6a11974088a46d42b903418eJohann// problems when you register multiple environments from different
12601b362b15af34006e6a11974088a46d42b903418eJohann// translation units and the environments have dependencies among them
12611b362b15af34006e6a11974088a46d42b903418eJohann// (remember that the compiler doesn't guarantee the order in which
12621b362b15af34006e6a11974088a46d42b903418eJohann// global variables from different translation units are initialized).
12631b362b15af34006e6a11974088a46d42b903418eJohanninline Environment* AddGlobalTestEnvironment(Environment* env) {
12641b362b15af34006e6a11974088a46d42b903418eJohann  return UnitTest::GetInstance()->AddEnvironment(env);
12651b362b15af34006e6a11974088a46d42b903418eJohann}
12661b362b15af34006e6a11974088a46d42b903418eJohann
12671b362b15af34006e6a11974088a46d42b903418eJohann// Initializes Google Test.  This must be called before calling
12681b362b15af34006e6a11974088a46d42b903418eJohann// RUN_ALL_TESTS().  In particular, it parses a command line for the
12691b362b15af34006e6a11974088a46d42b903418eJohann// flags that Google Test recognizes.  Whenever a Google Test flag is
12701b362b15af34006e6a11974088a46d42b903418eJohann// seen, it is removed from argv, and *argc is decremented.
12711b362b15af34006e6a11974088a46d42b903418eJohann//
12721b362b15af34006e6a11974088a46d42b903418eJohann// No value is returned.  Instead, the Google Test flag variables are
12731b362b15af34006e6a11974088a46d42b903418eJohann// updated.
12741b362b15af34006e6a11974088a46d42b903418eJohann//
12751b362b15af34006e6a11974088a46d42b903418eJohann// Calling the function for the second time has no user-visible effect.
12761b362b15af34006e6a11974088a46d42b903418eJohannGTEST_API_ void InitGoogleTest(int* argc, char** argv);
12771b362b15af34006e6a11974088a46d42b903418eJohann
12781b362b15af34006e6a11974088a46d42b903418eJohann// This overloaded version can be used in Windows programs compiled in
12791b362b15af34006e6a11974088a46d42b903418eJohann// UNICODE mode.
12801b362b15af34006e6a11974088a46d42b903418eJohannGTEST_API_ void InitGoogleTest(int* argc, wchar_t** argv);
12811b362b15af34006e6a11974088a46d42b903418eJohann
12821b362b15af34006e6a11974088a46d42b903418eJohannnamespace internal {
12831b362b15af34006e6a11974088a46d42b903418eJohann
12841b362b15af34006e6a11974088a46d42b903418eJohann// Formats a comparison assertion (e.g. ASSERT_EQ, EXPECT_LT, and etc)
12851b362b15af34006e6a11974088a46d42b903418eJohann// operand to be used in a failure message.  The type (but not value)
12861b362b15af34006e6a11974088a46d42b903418eJohann// of the other operand may affect the format.  This allows us to
12871b362b15af34006e6a11974088a46d42b903418eJohann// print a char* as a raw pointer when it is compared against another
12881b362b15af34006e6a11974088a46d42b903418eJohann// char*, and print it as a C string when it is compared against an
12891b362b15af34006e6a11974088a46d42b903418eJohann// std::string object, for example.
12901b362b15af34006e6a11974088a46d42b903418eJohann//
12911b362b15af34006e6a11974088a46d42b903418eJohann// The default implementation ignores the type of the other operand.
12921b362b15af34006e6a11974088a46d42b903418eJohann// Some specialized versions are used to handle formatting wide or
12931b362b15af34006e6a11974088a46d42b903418eJohann// narrow C strings.
12941b362b15af34006e6a11974088a46d42b903418eJohann//
12951b362b15af34006e6a11974088a46d42b903418eJohann// INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM.
12961b362b15af34006e6a11974088a46d42b903418eJohanntemplate <typename T1, typename T2>
12971b362b15af34006e6a11974088a46d42b903418eJohannString FormatForComparisonFailureMessage(const T1& value,
12981b362b15af34006e6a11974088a46d42b903418eJohann                                         const T2& /* other_operand */) {
12991b362b15af34006e6a11974088a46d42b903418eJohann  // C++Builder compiles this incorrectly if the namespace isn't explicitly
13001b362b15af34006e6a11974088a46d42b903418eJohann  // given.
13011b362b15af34006e6a11974088a46d42b903418eJohann  return ::testing::PrintToString(value);
13021b362b15af34006e6a11974088a46d42b903418eJohann}
13031b362b15af34006e6a11974088a46d42b903418eJohann
13041b362b15af34006e6a11974088a46d42b903418eJohann// The helper function for {ASSERT|EXPECT}_EQ.
13051b362b15af34006e6a11974088a46d42b903418eJohanntemplate <typename T1, typename T2>
13061b362b15af34006e6a11974088a46d42b903418eJohannAssertionResult CmpHelperEQ(const char* expected_expression,
13071b362b15af34006e6a11974088a46d42b903418eJohann                            const char* actual_expression,
13081b362b15af34006e6a11974088a46d42b903418eJohann                            const T1& expected,
13091b362b15af34006e6a11974088a46d42b903418eJohann                            const T2& actual) {
13101b362b15af34006e6a11974088a46d42b903418eJohann#ifdef _MSC_VER
13111b362b15af34006e6a11974088a46d42b903418eJohann# pragma warning(push)          // Saves the current warning state.
13121b362b15af34006e6a11974088a46d42b903418eJohann# pragma warning(disable:4389)  // Temporarily disables warning on
13131b362b15af34006e6a11974088a46d42b903418eJohann                               // signed/unsigned mismatch.
13141b362b15af34006e6a11974088a46d42b903418eJohann#endif
13151b362b15af34006e6a11974088a46d42b903418eJohann
13161b362b15af34006e6a11974088a46d42b903418eJohann  if (expected == actual) {
13171b362b15af34006e6a11974088a46d42b903418eJohann    return AssertionSuccess();
13181b362b15af34006e6a11974088a46d42b903418eJohann  }
13191b362b15af34006e6a11974088a46d42b903418eJohann
13201b362b15af34006e6a11974088a46d42b903418eJohann#ifdef _MSC_VER
13211b362b15af34006e6a11974088a46d42b903418eJohann# pragma warning(pop)          // Restores the warning state.
13221b362b15af34006e6a11974088a46d42b903418eJohann#endif
13231b362b15af34006e6a11974088a46d42b903418eJohann
13241b362b15af34006e6a11974088a46d42b903418eJohann  return EqFailure(expected_expression,
13251b362b15af34006e6a11974088a46d42b903418eJohann                   actual_expression,
13261b362b15af34006e6a11974088a46d42b903418eJohann                   FormatForComparisonFailureMessage(expected, actual),
13271b362b15af34006e6a11974088a46d42b903418eJohann                   FormatForComparisonFailureMessage(actual, expected),
13281b362b15af34006e6a11974088a46d42b903418eJohann                   false);
13291b362b15af34006e6a11974088a46d42b903418eJohann}
13301b362b15af34006e6a11974088a46d42b903418eJohann
13311b362b15af34006e6a11974088a46d42b903418eJohann// With this overloaded version, we allow anonymous enums to be used
13321b362b15af34006e6a11974088a46d42b903418eJohann// in {ASSERT|EXPECT}_EQ when compiled with gcc 4, as anonymous enums
13331b362b15af34006e6a11974088a46d42b903418eJohann// can be implicitly cast to BiggestInt.
13341b362b15af34006e6a11974088a46d42b903418eJohannGTEST_API_ AssertionResult CmpHelperEQ(const char* expected_expression,
13351b362b15af34006e6a11974088a46d42b903418eJohann                                       const char* actual_expression,
13361b362b15af34006e6a11974088a46d42b903418eJohann                                       BiggestInt expected,
13371b362b15af34006e6a11974088a46d42b903418eJohann                                       BiggestInt actual);
13381b362b15af34006e6a11974088a46d42b903418eJohann
13391b362b15af34006e6a11974088a46d42b903418eJohann// The helper class for {ASSERT|EXPECT}_EQ.  The template argument
13401b362b15af34006e6a11974088a46d42b903418eJohann// lhs_is_null_literal is true iff the first argument to ASSERT_EQ()
13411b362b15af34006e6a11974088a46d42b903418eJohann// is a null pointer literal.  The following default implementation is
13421b362b15af34006e6a11974088a46d42b903418eJohann// for lhs_is_null_literal being false.
13431b362b15af34006e6a11974088a46d42b903418eJohanntemplate <bool lhs_is_null_literal>
13441b362b15af34006e6a11974088a46d42b903418eJohannclass EqHelper {
13451b362b15af34006e6a11974088a46d42b903418eJohann public:
13461b362b15af34006e6a11974088a46d42b903418eJohann  // This templatized version is for the general case.
13471b362b15af34006e6a11974088a46d42b903418eJohann  template <typename T1, typename T2>
13481b362b15af34006e6a11974088a46d42b903418eJohann  static AssertionResult Compare(const char* expected_expression,
13491b362b15af34006e6a11974088a46d42b903418eJohann                                 const char* actual_expression,
13501b362b15af34006e6a11974088a46d42b903418eJohann                                 const T1& expected,
13511b362b15af34006e6a11974088a46d42b903418eJohann                                 const T2& actual) {
13521b362b15af34006e6a11974088a46d42b903418eJohann    return CmpHelperEQ(expected_expression, actual_expression, expected,
13531b362b15af34006e6a11974088a46d42b903418eJohann                       actual);
13541b362b15af34006e6a11974088a46d42b903418eJohann  }
13551b362b15af34006e6a11974088a46d42b903418eJohann
13561b362b15af34006e6a11974088a46d42b903418eJohann  // With this overloaded version, we allow anonymous enums to be used
13571b362b15af34006e6a11974088a46d42b903418eJohann  // in {ASSERT|EXPECT}_EQ when compiled with gcc 4, as anonymous
13581b362b15af34006e6a11974088a46d42b903418eJohann  // enums can be implicitly cast to BiggestInt.
13591b362b15af34006e6a11974088a46d42b903418eJohann  //
13601b362b15af34006e6a11974088a46d42b903418eJohann  // Even though its body looks the same as the above version, we
13611b362b15af34006e6a11974088a46d42b903418eJohann  // cannot merge the two, as it will make anonymous enums unhappy.
13621b362b15af34006e6a11974088a46d42b903418eJohann  static AssertionResult Compare(const char* expected_expression,
13631b362b15af34006e6a11974088a46d42b903418eJohann                                 const char* actual_expression,
13641b362b15af34006e6a11974088a46d42b903418eJohann                                 BiggestInt expected,
13651b362b15af34006e6a11974088a46d42b903418eJohann                                 BiggestInt actual) {
13661b362b15af34006e6a11974088a46d42b903418eJohann    return CmpHelperEQ(expected_expression, actual_expression, expected,
13671b362b15af34006e6a11974088a46d42b903418eJohann                       actual);
13681b362b15af34006e6a11974088a46d42b903418eJohann  }
13691b362b15af34006e6a11974088a46d42b903418eJohann};
13701b362b15af34006e6a11974088a46d42b903418eJohann
13711b362b15af34006e6a11974088a46d42b903418eJohann// This specialization is used when the first argument to ASSERT_EQ()
13721b362b15af34006e6a11974088a46d42b903418eJohann// is a null pointer literal, like NULL, false, or 0.
13731b362b15af34006e6a11974088a46d42b903418eJohanntemplate <>
13741b362b15af34006e6a11974088a46d42b903418eJohannclass EqHelper<true> {
13751b362b15af34006e6a11974088a46d42b903418eJohann public:
13761b362b15af34006e6a11974088a46d42b903418eJohann  // We define two overloaded versions of Compare().  The first
13771b362b15af34006e6a11974088a46d42b903418eJohann  // version will be picked when the second argument to ASSERT_EQ() is
13781b362b15af34006e6a11974088a46d42b903418eJohann  // NOT a pointer, e.g. ASSERT_EQ(0, AnIntFunction()) or
13791b362b15af34006e6a11974088a46d42b903418eJohann  // EXPECT_EQ(false, a_bool).
13801b362b15af34006e6a11974088a46d42b903418eJohann  template <typename T1, typename T2>
13811b362b15af34006e6a11974088a46d42b903418eJohann  static AssertionResult Compare(
13821b362b15af34006e6a11974088a46d42b903418eJohann      const char* expected_expression,
13831b362b15af34006e6a11974088a46d42b903418eJohann      const char* actual_expression,
13841b362b15af34006e6a11974088a46d42b903418eJohann      const T1& expected,
13851b362b15af34006e6a11974088a46d42b903418eJohann      const T2& actual,
13861b362b15af34006e6a11974088a46d42b903418eJohann      // The following line prevents this overload from being considered if T2
13871b362b15af34006e6a11974088a46d42b903418eJohann      // is not a pointer type.  We need this because ASSERT_EQ(NULL, my_ptr)
13881b362b15af34006e6a11974088a46d42b903418eJohann      // expands to Compare("", "", NULL, my_ptr), which requires a conversion
13891b362b15af34006e6a11974088a46d42b903418eJohann      // to match the Secret* in the other overload, which would otherwise make
13901b362b15af34006e6a11974088a46d42b903418eJohann      // this template match better.
13911b362b15af34006e6a11974088a46d42b903418eJohann      typename EnableIf<!is_pointer<T2>::value>::type* = 0) {
13921b362b15af34006e6a11974088a46d42b903418eJohann    return CmpHelperEQ(expected_expression, actual_expression, expected,
13931b362b15af34006e6a11974088a46d42b903418eJohann                       actual);
13941b362b15af34006e6a11974088a46d42b903418eJohann  }
13951b362b15af34006e6a11974088a46d42b903418eJohann
13961b362b15af34006e6a11974088a46d42b903418eJohann  // This version will be picked when the second argument to ASSERT_EQ() is a
13971b362b15af34006e6a11974088a46d42b903418eJohann  // pointer, e.g. ASSERT_EQ(NULL, a_pointer).
13981b362b15af34006e6a11974088a46d42b903418eJohann  template <typename T>
13991b362b15af34006e6a11974088a46d42b903418eJohann  static AssertionResult Compare(
14001b362b15af34006e6a11974088a46d42b903418eJohann      const char* expected_expression,
14011b362b15af34006e6a11974088a46d42b903418eJohann      const char* actual_expression,
14021b362b15af34006e6a11974088a46d42b903418eJohann      // We used to have a second template parameter instead of Secret*.  That
14031b362b15af34006e6a11974088a46d42b903418eJohann      // template parameter would deduce to 'long', making this a better match
14041b362b15af34006e6a11974088a46d42b903418eJohann      // than the first overload even without the first overload's EnableIf.
14051b362b15af34006e6a11974088a46d42b903418eJohann      // Unfortunately, gcc with -Wconversion-null warns when "passing NULL to
14061b362b15af34006e6a11974088a46d42b903418eJohann      // non-pointer argument" (even a deduced integral argument), so the old
14071b362b15af34006e6a11974088a46d42b903418eJohann      // implementation caused warnings in user code.
14081b362b15af34006e6a11974088a46d42b903418eJohann      Secret* /* expected (NULL) */,
14091b362b15af34006e6a11974088a46d42b903418eJohann      T* actual) {
14101b362b15af34006e6a11974088a46d42b903418eJohann    // We already know that 'expected' is a null pointer.
14111b362b15af34006e6a11974088a46d42b903418eJohann    return CmpHelperEQ(expected_expression, actual_expression,
14121b362b15af34006e6a11974088a46d42b903418eJohann                       static_cast<T*>(NULL), actual);
14131b362b15af34006e6a11974088a46d42b903418eJohann  }
14141b362b15af34006e6a11974088a46d42b903418eJohann};
14151b362b15af34006e6a11974088a46d42b903418eJohann
14161b362b15af34006e6a11974088a46d42b903418eJohann// A macro for implementing the helper functions needed to implement
14171b362b15af34006e6a11974088a46d42b903418eJohann// ASSERT_?? and EXPECT_??.  It is here just to avoid copy-and-paste
14181b362b15af34006e6a11974088a46d42b903418eJohann// of similar code.
14191b362b15af34006e6a11974088a46d42b903418eJohann//
14201b362b15af34006e6a11974088a46d42b903418eJohann// For each templatized helper function, we also define an overloaded
14211b362b15af34006e6a11974088a46d42b903418eJohann// version for BiggestInt in order to reduce code bloat and allow
14221b362b15af34006e6a11974088a46d42b903418eJohann// anonymous enums to be used with {ASSERT|EXPECT}_?? when compiled
14231b362b15af34006e6a11974088a46d42b903418eJohann// with gcc 4.
14241b362b15af34006e6a11974088a46d42b903418eJohann//
14251b362b15af34006e6a11974088a46d42b903418eJohann// INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM.
14261b362b15af34006e6a11974088a46d42b903418eJohann#define GTEST_IMPL_CMP_HELPER_(op_name, op)\
14271b362b15af34006e6a11974088a46d42b903418eJohanntemplate <typename T1, typename T2>\
14281b362b15af34006e6a11974088a46d42b903418eJohannAssertionResult CmpHelper##op_name(const char* expr1, const char* expr2, \
14291b362b15af34006e6a11974088a46d42b903418eJohann                                   const T1& val1, const T2& val2) {\
14301b362b15af34006e6a11974088a46d42b903418eJohann  if (val1 op val2) {\
14311b362b15af34006e6a11974088a46d42b903418eJohann    return AssertionSuccess();\
14321b362b15af34006e6a11974088a46d42b903418eJohann  } else {\
14331b362b15af34006e6a11974088a46d42b903418eJohann    return AssertionFailure() \
14341b362b15af34006e6a11974088a46d42b903418eJohann        << "Expected: (" << expr1 << ") " #op " (" << expr2\
14351b362b15af34006e6a11974088a46d42b903418eJohann        << "), actual: " << FormatForComparisonFailureMessage(val1, val2)\
14361b362b15af34006e6a11974088a46d42b903418eJohann        << " vs " << FormatForComparisonFailureMessage(val2, val1);\
14371b362b15af34006e6a11974088a46d42b903418eJohann  }\
14381b362b15af34006e6a11974088a46d42b903418eJohann}\
14391b362b15af34006e6a11974088a46d42b903418eJohannGTEST_API_ AssertionResult CmpHelper##op_name(\
14401b362b15af34006e6a11974088a46d42b903418eJohann    const char* expr1, const char* expr2, BiggestInt val1, BiggestInt val2)
14411b362b15af34006e6a11974088a46d42b903418eJohann
14421b362b15af34006e6a11974088a46d42b903418eJohann// INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM.
14431b362b15af34006e6a11974088a46d42b903418eJohann
14441b362b15af34006e6a11974088a46d42b903418eJohann// Implements the helper function for {ASSERT|EXPECT}_NE
14451b362b15af34006e6a11974088a46d42b903418eJohannGTEST_IMPL_CMP_HELPER_(NE, !=);
14461b362b15af34006e6a11974088a46d42b903418eJohann// Implements the helper function for {ASSERT|EXPECT}_LE
14471b362b15af34006e6a11974088a46d42b903418eJohannGTEST_IMPL_CMP_HELPER_(LE, <=);
14481b362b15af34006e6a11974088a46d42b903418eJohann// Implements the helper function for {ASSERT|EXPECT}_LT
14491b362b15af34006e6a11974088a46d42b903418eJohannGTEST_IMPL_CMP_HELPER_(LT, < );
14501b362b15af34006e6a11974088a46d42b903418eJohann// Implements the helper function for {ASSERT|EXPECT}_GE
14511b362b15af34006e6a11974088a46d42b903418eJohannGTEST_IMPL_CMP_HELPER_(GE, >=);
14521b362b15af34006e6a11974088a46d42b903418eJohann// Implements the helper function for {ASSERT|EXPECT}_GT
14531b362b15af34006e6a11974088a46d42b903418eJohannGTEST_IMPL_CMP_HELPER_(GT, > );
14541b362b15af34006e6a11974088a46d42b903418eJohann
14551b362b15af34006e6a11974088a46d42b903418eJohann#undef GTEST_IMPL_CMP_HELPER_
14561b362b15af34006e6a11974088a46d42b903418eJohann
14571b362b15af34006e6a11974088a46d42b903418eJohann// The helper function for {ASSERT|EXPECT}_STREQ.
14581b362b15af34006e6a11974088a46d42b903418eJohann//
14591b362b15af34006e6a11974088a46d42b903418eJohann// INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM.
14601b362b15af34006e6a11974088a46d42b903418eJohannGTEST_API_ AssertionResult CmpHelperSTREQ(const char* expected_expression,
14611b362b15af34006e6a11974088a46d42b903418eJohann                                          const char* actual_expression,
14621b362b15af34006e6a11974088a46d42b903418eJohann                                          const char* expected,
14631b362b15af34006e6a11974088a46d42b903418eJohann                                          const char* actual);
14641b362b15af34006e6a11974088a46d42b903418eJohann
14651b362b15af34006e6a11974088a46d42b903418eJohann// The helper function for {ASSERT|EXPECT}_STRCASEEQ.
14661b362b15af34006e6a11974088a46d42b903418eJohann//
14671b362b15af34006e6a11974088a46d42b903418eJohann// INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM.
14681b362b15af34006e6a11974088a46d42b903418eJohannGTEST_API_ AssertionResult CmpHelperSTRCASEEQ(const char* expected_expression,
14691b362b15af34006e6a11974088a46d42b903418eJohann                                              const char* actual_expression,
14701b362b15af34006e6a11974088a46d42b903418eJohann                                              const char* expected,
14711b362b15af34006e6a11974088a46d42b903418eJohann                                              const char* actual);
14721b362b15af34006e6a11974088a46d42b903418eJohann
14731b362b15af34006e6a11974088a46d42b903418eJohann// The helper function for {ASSERT|EXPECT}_STRNE.
14741b362b15af34006e6a11974088a46d42b903418eJohann//
14751b362b15af34006e6a11974088a46d42b903418eJohann// INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM.
14761b362b15af34006e6a11974088a46d42b903418eJohannGTEST_API_ AssertionResult CmpHelperSTRNE(const char* s1_expression,
14771b362b15af34006e6a11974088a46d42b903418eJohann                                          const char* s2_expression,
14781b362b15af34006e6a11974088a46d42b903418eJohann                                          const char* s1,
14791b362b15af34006e6a11974088a46d42b903418eJohann                                          const char* s2);
14801b362b15af34006e6a11974088a46d42b903418eJohann
14811b362b15af34006e6a11974088a46d42b903418eJohann// The helper function for {ASSERT|EXPECT}_STRCASENE.
14821b362b15af34006e6a11974088a46d42b903418eJohann//
14831b362b15af34006e6a11974088a46d42b903418eJohann// INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM.
14841b362b15af34006e6a11974088a46d42b903418eJohannGTEST_API_ AssertionResult CmpHelperSTRCASENE(const char* s1_expression,
14851b362b15af34006e6a11974088a46d42b903418eJohann                                              const char* s2_expression,
14861b362b15af34006e6a11974088a46d42b903418eJohann                                              const char* s1,
14871b362b15af34006e6a11974088a46d42b903418eJohann                                              const char* s2);
14881b362b15af34006e6a11974088a46d42b903418eJohann
14891b362b15af34006e6a11974088a46d42b903418eJohann
14901b362b15af34006e6a11974088a46d42b903418eJohann// Helper function for *_STREQ on wide strings.
14911b362b15af34006e6a11974088a46d42b903418eJohann//
14921b362b15af34006e6a11974088a46d42b903418eJohann// INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM.
14931b362b15af34006e6a11974088a46d42b903418eJohannGTEST_API_ AssertionResult CmpHelperSTREQ(const char* expected_expression,
14941b362b15af34006e6a11974088a46d42b903418eJohann                                          const char* actual_expression,
14951b362b15af34006e6a11974088a46d42b903418eJohann                                          const wchar_t* expected,
14961b362b15af34006e6a11974088a46d42b903418eJohann                                          const wchar_t* actual);
14971b362b15af34006e6a11974088a46d42b903418eJohann
14981b362b15af34006e6a11974088a46d42b903418eJohann// Helper function for *_STRNE on wide strings.
14991b362b15af34006e6a11974088a46d42b903418eJohann//
15001b362b15af34006e6a11974088a46d42b903418eJohann// INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM.
15011b362b15af34006e6a11974088a46d42b903418eJohannGTEST_API_ AssertionResult CmpHelperSTRNE(const char* s1_expression,
15021b362b15af34006e6a11974088a46d42b903418eJohann                                          const char* s2_expression,
15031b362b15af34006e6a11974088a46d42b903418eJohann                                          const wchar_t* s1,
15041b362b15af34006e6a11974088a46d42b903418eJohann                                          const wchar_t* s2);
15051b362b15af34006e6a11974088a46d42b903418eJohann
15061b362b15af34006e6a11974088a46d42b903418eJohann}  // namespace internal
15071b362b15af34006e6a11974088a46d42b903418eJohann
15081b362b15af34006e6a11974088a46d42b903418eJohann// IsSubstring() and IsNotSubstring() are intended to be used as the
15091b362b15af34006e6a11974088a46d42b903418eJohann// first argument to {EXPECT,ASSERT}_PRED_FORMAT2(), not by
15101b362b15af34006e6a11974088a46d42b903418eJohann// themselves.  They check whether needle is a substring of haystack
15111b362b15af34006e6a11974088a46d42b903418eJohann// (NULL is considered a substring of itself only), and return an
15121b362b15af34006e6a11974088a46d42b903418eJohann// appropriate error message when they fail.
15131b362b15af34006e6a11974088a46d42b903418eJohann//
15141b362b15af34006e6a11974088a46d42b903418eJohann// The {needle,haystack}_expr arguments are the stringified
15151b362b15af34006e6a11974088a46d42b903418eJohann// expressions that generated the two real arguments.
15161b362b15af34006e6a11974088a46d42b903418eJohannGTEST_API_ AssertionResult IsSubstring(
15171b362b15af34006e6a11974088a46d42b903418eJohann    const char* needle_expr, const char* haystack_expr,
15181b362b15af34006e6a11974088a46d42b903418eJohann    const char* needle, const char* haystack);
15191b362b15af34006e6a11974088a46d42b903418eJohannGTEST_API_ AssertionResult IsSubstring(
15201b362b15af34006e6a11974088a46d42b903418eJohann    const char* needle_expr, const char* haystack_expr,
15211b362b15af34006e6a11974088a46d42b903418eJohann    const wchar_t* needle, const wchar_t* haystack);
15221b362b15af34006e6a11974088a46d42b903418eJohannGTEST_API_ AssertionResult IsNotSubstring(
15231b362b15af34006e6a11974088a46d42b903418eJohann    const char* needle_expr, const char* haystack_expr,
15241b362b15af34006e6a11974088a46d42b903418eJohann    const char* needle, const char* haystack);
15251b362b15af34006e6a11974088a46d42b903418eJohannGTEST_API_ AssertionResult IsNotSubstring(
15261b362b15af34006e6a11974088a46d42b903418eJohann    const char* needle_expr, const char* haystack_expr,
15271b362b15af34006e6a11974088a46d42b903418eJohann    const wchar_t* needle, const wchar_t* haystack);
15281b362b15af34006e6a11974088a46d42b903418eJohannGTEST_API_ AssertionResult IsSubstring(
15291b362b15af34006e6a11974088a46d42b903418eJohann    const char* needle_expr, const char* haystack_expr,
15301b362b15af34006e6a11974088a46d42b903418eJohann    const ::std::string& needle, const ::std::string& haystack);
15311b362b15af34006e6a11974088a46d42b903418eJohannGTEST_API_ AssertionResult IsNotSubstring(
15321b362b15af34006e6a11974088a46d42b903418eJohann    const char* needle_expr, const char* haystack_expr,
15331b362b15af34006e6a11974088a46d42b903418eJohann    const ::std::string& needle, const ::std::string& haystack);
15341b362b15af34006e6a11974088a46d42b903418eJohann
15351b362b15af34006e6a11974088a46d42b903418eJohann#if GTEST_HAS_STD_WSTRING
15361b362b15af34006e6a11974088a46d42b903418eJohannGTEST_API_ AssertionResult IsSubstring(
15371b362b15af34006e6a11974088a46d42b903418eJohann    const char* needle_expr, const char* haystack_expr,
15381b362b15af34006e6a11974088a46d42b903418eJohann    const ::std::wstring& needle, const ::std::wstring& haystack);
15391b362b15af34006e6a11974088a46d42b903418eJohannGTEST_API_ AssertionResult IsNotSubstring(
15401b362b15af34006e6a11974088a46d42b903418eJohann    const char* needle_expr, const char* haystack_expr,
15411b362b15af34006e6a11974088a46d42b903418eJohann    const ::std::wstring& needle, const ::std::wstring& haystack);
15421b362b15af34006e6a11974088a46d42b903418eJohann#endif  // GTEST_HAS_STD_WSTRING
15431b362b15af34006e6a11974088a46d42b903418eJohann
15441b362b15af34006e6a11974088a46d42b903418eJohannnamespace internal {
15451b362b15af34006e6a11974088a46d42b903418eJohann
15461b362b15af34006e6a11974088a46d42b903418eJohann// Helper template function for comparing floating-points.
15471b362b15af34006e6a11974088a46d42b903418eJohann//
15481b362b15af34006e6a11974088a46d42b903418eJohann// Template parameter:
15491b362b15af34006e6a11974088a46d42b903418eJohann//
15501b362b15af34006e6a11974088a46d42b903418eJohann//   RawType: the raw floating-point type (either float or double)
15511b362b15af34006e6a11974088a46d42b903418eJohann//
15521b362b15af34006e6a11974088a46d42b903418eJohann// INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM.
15531b362b15af34006e6a11974088a46d42b903418eJohanntemplate <typename RawType>
15541b362b15af34006e6a11974088a46d42b903418eJohannAssertionResult CmpHelperFloatingPointEQ(const char* expected_expression,
15551b362b15af34006e6a11974088a46d42b903418eJohann                                         const char* actual_expression,
15561b362b15af34006e6a11974088a46d42b903418eJohann                                         RawType expected,
15571b362b15af34006e6a11974088a46d42b903418eJohann                                         RawType actual) {
15581b362b15af34006e6a11974088a46d42b903418eJohann  const FloatingPoint<RawType> lhs(expected), rhs(actual);
15591b362b15af34006e6a11974088a46d42b903418eJohann
15601b362b15af34006e6a11974088a46d42b903418eJohann  if (lhs.AlmostEquals(rhs)) {
15611b362b15af34006e6a11974088a46d42b903418eJohann    return AssertionSuccess();
15621b362b15af34006e6a11974088a46d42b903418eJohann  }
15631b362b15af34006e6a11974088a46d42b903418eJohann
15641b362b15af34006e6a11974088a46d42b903418eJohann  ::std::stringstream expected_ss;
15651b362b15af34006e6a11974088a46d42b903418eJohann  expected_ss << std::setprecision(std::numeric_limits<RawType>::digits10 + 2)
15661b362b15af34006e6a11974088a46d42b903418eJohann              << expected;
15671b362b15af34006e6a11974088a46d42b903418eJohann
15681b362b15af34006e6a11974088a46d42b903418eJohann  ::std::stringstream actual_ss;
15691b362b15af34006e6a11974088a46d42b903418eJohann  actual_ss << std::setprecision(std::numeric_limits<RawType>::digits10 + 2)
15701b362b15af34006e6a11974088a46d42b903418eJohann            << actual;
15711b362b15af34006e6a11974088a46d42b903418eJohann
15721b362b15af34006e6a11974088a46d42b903418eJohann  return EqFailure(expected_expression,
15731b362b15af34006e6a11974088a46d42b903418eJohann                   actual_expression,
15741b362b15af34006e6a11974088a46d42b903418eJohann                   StringStreamToString(&expected_ss),
15751b362b15af34006e6a11974088a46d42b903418eJohann                   StringStreamToString(&actual_ss),
15761b362b15af34006e6a11974088a46d42b903418eJohann                   false);
15771b362b15af34006e6a11974088a46d42b903418eJohann}
15781b362b15af34006e6a11974088a46d42b903418eJohann
15791b362b15af34006e6a11974088a46d42b903418eJohann// Helper function for implementing ASSERT_NEAR.
15801b362b15af34006e6a11974088a46d42b903418eJohann//
15811b362b15af34006e6a11974088a46d42b903418eJohann// INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM.
15821b362b15af34006e6a11974088a46d42b903418eJohannGTEST_API_ AssertionResult DoubleNearPredFormat(const char* expr1,
15831b362b15af34006e6a11974088a46d42b903418eJohann                                                const char* expr2,
15841b362b15af34006e6a11974088a46d42b903418eJohann                                                const char* abs_error_expr,
15851b362b15af34006e6a11974088a46d42b903418eJohann                                                double val1,
15861b362b15af34006e6a11974088a46d42b903418eJohann                                                double val2,
15871b362b15af34006e6a11974088a46d42b903418eJohann                                                double abs_error);
15881b362b15af34006e6a11974088a46d42b903418eJohann
15891b362b15af34006e6a11974088a46d42b903418eJohann// INTERNAL IMPLEMENTATION - DO NOT USE IN USER CODE.
15901b362b15af34006e6a11974088a46d42b903418eJohann// A class that enables one to stream messages to assertion macros
15911b362b15af34006e6a11974088a46d42b903418eJohannclass GTEST_API_ AssertHelper {
15921b362b15af34006e6a11974088a46d42b903418eJohann public:
15931b362b15af34006e6a11974088a46d42b903418eJohann  // Constructor.
15941b362b15af34006e6a11974088a46d42b903418eJohann  AssertHelper(TestPartResult::Type type,
15951b362b15af34006e6a11974088a46d42b903418eJohann               const char* file,
15961b362b15af34006e6a11974088a46d42b903418eJohann               int line,
15971b362b15af34006e6a11974088a46d42b903418eJohann               const char* message);
15981b362b15af34006e6a11974088a46d42b903418eJohann  ~AssertHelper();
15991b362b15af34006e6a11974088a46d42b903418eJohann
16001b362b15af34006e6a11974088a46d42b903418eJohann  // Message assignment is a semantic trick to enable assertion
16011b362b15af34006e6a11974088a46d42b903418eJohann  // streaming; see the GTEST_MESSAGE_ macro below.
16021b362b15af34006e6a11974088a46d42b903418eJohann  void operator=(const Message& message) const;
16031b362b15af34006e6a11974088a46d42b903418eJohann
16041b362b15af34006e6a11974088a46d42b903418eJohann private:
16051b362b15af34006e6a11974088a46d42b903418eJohann  // We put our data in a struct so that the size of the AssertHelper class can
16061b362b15af34006e6a11974088a46d42b903418eJohann  // be as small as possible.  This is important because gcc is incapable of
16071b362b15af34006e6a11974088a46d42b903418eJohann  // re-using stack space even for temporary variables, so every EXPECT_EQ
16081b362b15af34006e6a11974088a46d42b903418eJohann  // reserves stack space for another AssertHelper.
16091b362b15af34006e6a11974088a46d42b903418eJohann  struct AssertHelperData {
16101b362b15af34006e6a11974088a46d42b903418eJohann    AssertHelperData(TestPartResult::Type t,
16111b362b15af34006e6a11974088a46d42b903418eJohann                     const char* srcfile,
16121b362b15af34006e6a11974088a46d42b903418eJohann                     int line_num,
16131b362b15af34006e6a11974088a46d42b903418eJohann                     const char* msg)
16141b362b15af34006e6a11974088a46d42b903418eJohann        : type(t), file(srcfile), line(line_num), message(msg) { }
16151b362b15af34006e6a11974088a46d42b903418eJohann
16161b362b15af34006e6a11974088a46d42b903418eJohann    TestPartResult::Type const type;
16171b362b15af34006e6a11974088a46d42b903418eJohann    const char*        const file;
16181b362b15af34006e6a11974088a46d42b903418eJohann    int                const line;
16191b362b15af34006e6a11974088a46d42b903418eJohann    String             const message;
16201b362b15af34006e6a11974088a46d42b903418eJohann
16211b362b15af34006e6a11974088a46d42b903418eJohann   private:
16221b362b15af34006e6a11974088a46d42b903418eJohann    GTEST_DISALLOW_COPY_AND_ASSIGN_(AssertHelperData);
16231b362b15af34006e6a11974088a46d42b903418eJohann  };
16241b362b15af34006e6a11974088a46d42b903418eJohann
16251b362b15af34006e6a11974088a46d42b903418eJohann  AssertHelperData* const data_;
16261b362b15af34006e6a11974088a46d42b903418eJohann
16271b362b15af34006e6a11974088a46d42b903418eJohann  GTEST_DISALLOW_COPY_AND_ASSIGN_(AssertHelper);
16281b362b15af34006e6a11974088a46d42b903418eJohann};
16291b362b15af34006e6a11974088a46d42b903418eJohann
16301b362b15af34006e6a11974088a46d42b903418eJohann}  // namespace internal
16311b362b15af34006e6a11974088a46d42b903418eJohann
16321b362b15af34006e6a11974088a46d42b903418eJohann#if GTEST_HAS_PARAM_TEST
16331b362b15af34006e6a11974088a46d42b903418eJohann// The pure interface class that all value-parameterized tests inherit from.
16341b362b15af34006e6a11974088a46d42b903418eJohann// A value-parameterized class must inherit from both ::testing::Test and
16351b362b15af34006e6a11974088a46d42b903418eJohann// ::testing::WithParamInterface. In most cases that just means inheriting
16361b362b15af34006e6a11974088a46d42b903418eJohann// from ::testing::TestWithParam, but more complicated test hierarchies
16371b362b15af34006e6a11974088a46d42b903418eJohann// may need to inherit from Test and WithParamInterface at different levels.
16381b362b15af34006e6a11974088a46d42b903418eJohann//
16391b362b15af34006e6a11974088a46d42b903418eJohann// This interface has support for accessing the test parameter value via
16401b362b15af34006e6a11974088a46d42b903418eJohann// the GetParam() method.
16411b362b15af34006e6a11974088a46d42b903418eJohann//
16421b362b15af34006e6a11974088a46d42b903418eJohann// Use it with one of the parameter generator defining functions, like Range(),
16431b362b15af34006e6a11974088a46d42b903418eJohann// Values(), ValuesIn(), Bool(), and Combine().
16441b362b15af34006e6a11974088a46d42b903418eJohann//
16451b362b15af34006e6a11974088a46d42b903418eJohann// class FooTest : public ::testing::TestWithParam<int> {
16461b362b15af34006e6a11974088a46d42b903418eJohann//  protected:
16471b362b15af34006e6a11974088a46d42b903418eJohann//   FooTest() {
16481b362b15af34006e6a11974088a46d42b903418eJohann//     // Can use GetParam() here.
16491b362b15af34006e6a11974088a46d42b903418eJohann//   }
16501b362b15af34006e6a11974088a46d42b903418eJohann//   virtual ~FooTest() {
16511b362b15af34006e6a11974088a46d42b903418eJohann//     // Can use GetParam() here.
16521b362b15af34006e6a11974088a46d42b903418eJohann//   }
16531b362b15af34006e6a11974088a46d42b903418eJohann//   virtual void SetUp() {
16541b362b15af34006e6a11974088a46d42b903418eJohann//     // Can use GetParam() here.
16551b362b15af34006e6a11974088a46d42b903418eJohann//   }
16561b362b15af34006e6a11974088a46d42b903418eJohann//   virtual void TearDown {
16571b362b15af34006e6a11974088a46d42b903418eJohann//     // Can use GetParam() here.
16581b362b15af34006e6a11974088a46d42b903418eJohann//   }
16591b362b15af34006e6a11974088a46d42b903418eJohann// };
16601b362b15af34006e6a11974088a46d42b903418eJohann// TEST_P(FooTest, DoesBar) {
16611b362b15af34006e6a11974088a46d42b903418eJohann//   // Can use GetParam() method here.
16621b362b15af34006e6a11974088a46d42b903418eJohann//   Foo foo;
16631b362b15af34006e6a11974088a46d42b903418eJohann//   ASSERT_TRUE(foo.DoesBar(GetParam()));
16641b362b15af34006e6a11974088a46d42b903418eJohann// }
16651b362b15af34006e6a11974088a46d42b903418eJohann// INSTANTIATE_TEST_CASE_P(OneToTenRange, FooTest, ::testing::Range(1, 10));
16661b362b15af34006e6a11974088a46d42b903418eJohann
16671b362b15af34006e6a11974088a46d42b903418eJohanntemplate <typename T>
16681b362b15af34006e6a11974088a46d42b903418eJohannclass WithParamInterface {
16691b362b15af34006e6a11974088a46d42b903418eJohann public:
16701b362b15af34006e6a11974088a46d42b903418eJohann  typedef T ParamType;
16711b362b15af34006e6a11974088a46d42b903418eJohann  virtual ~WithParamInterface() {}
16721b362b15af34006e6a11974088a46d42b903418eJohann
16731b362b15af34006e6a11974088a46d42b903418eJohann  // The current parameter value. Is also available in the test fixture's
16741b362b15af34006e6a11974088a46d42b903418eJohann  // constructor. This member function is non-static, even though it only
16751b362b15af34006e6a11974088a46d42b903418eJohann  // references static data, to reduce the opportunity for incorrect uses
16761b362b15af34006e6a11974088a46d42b903418eJohann  // like writing 'WithParamInterface<bool>::GetParam()' for a test that
16771b362b15af34006e6a11974088a46d42b903418eJohann  // uses a fixture whose parameter type is int.
16781b362b15af34006e6a11974088a46d42b903418eJohann  const ParamType& GetParam() const { return *parameter_; }
16791b362b15af34006e6a11974088a46d42b903418eJohann
16801b362b15af34006e6a11974088a46d42b903418eJohann private:
16811b362b15af34006e6a11974088a46d42b903418eJohann  // Sets parameter value. The caller is responsible for making sure the value
16821b362b15af34006e6a11974088a46d42b903418eJohann  // remains alive and unchanged throughout the current test.
16831b362b15af34006e6a11974088a46d42b903418eJohann  static void SetParam(const ParamType* parameter) {
16841b362b15af34006e6a11974088a46d42b903418eJohann    parameter_ = parameter;
16851b362b15af34006e6a11974088a46d42b903418eJohann  }
16861b362b15af34006e6a11974088a46d42b903418eJohann
16871b362b15af34006e6a11974088a46d42b903418eJohann  // Static value used for accessing parameter during a test lifetime.
16881b362b15af34006e6a11974088a46d42b903418eJohann  static const ParamType* parameter_;
16891b362b15af34006e6a11974088a46d42b903418eJohann
16901b362b15af34006e6a11974088a46d42b903418eJohann  // TestClass must be a subclass of WithParamInterface<T> and Test.
16911b362b15af34006e6a11974088a46d42b903418eJohann  template <class TestClass> friend class internal::ParameterizedTestFactory;
16921b362b15af34006e6a11974088a46d42b903418eJohann};
16931b362b15af34006e6a11974088a46d42b903418eJohann
16941b362b15af34006e6a11974088a46d42b903418eJohanntemplate <typename T>
16951b362b15af34006e6a11974088a46d42b903418eJohannconst T* WithParamInterface<T>::parameter_ = NULL;
16961b362b15af34006e6a11974088a46d42b903418eJohann
16971b362b15af34006e6a11974088a46d42b903418eJohann// Most value-parameterized classes can ignore the existence of
16981b362b15af34006e6a11974088a46d42b903418eJohann// WithParamInterface, and can just inherit from ::testing::TestWithParam.
16991b362b15af34006e6a11974088a46d42b903418eJohann
17001b362b15af34006e6a11974088a46d42b903418eJohanntemplate <typename T>
17011b362b15af34006e6a11974088a46d42b903418eJohannclass TestWithParam : public Test, public WithParamInterface<T> {
17021b362b15af34006e6a11974088a46d42b903418eJohann};
17031b362b15af34006e6a11974088a46d42b903418eJohann
17041b362b15af34006e6a11974088a46d42b903418eJohann#endif  // GTEST_HAS_PARAM_TEST
17051b362b15af34006e6a11974088a46d42b903418eJohann
17061b362b15af34006e6a11974088a46d42b903418eJohann// Macros for indicating success/failure in test code.
17071b362b15af34006e6a11974088a46d42b903418eJohann
17081b362b15af34006e6a11974088a46d42b903418eJohann// ADD_FAILURE unconditionally adds a failure to the current test.
17091b362b15af34006e6a11974088a46d42b903418eJohann// SUCCEED generates a success - it doesn't automatically make the
17101b362b15af34006e6a11974088a46d42b903418eJohann// current test successful, as a test is only successful when it has
17111b362b15af34006e6a11974088a46d42b903418eJohann// no failure.
17121b362b15af34006e6a11974088a46d42b903418eJohann//
17131b362b15af34006e6a11974088a46d42b903418eJohann// EXPECT_* verifies that a certain condition is satisfied.  If not,
17141b362b15af34006e6a11974088a46d42b903418eJohann// it behaves like ADD_FAILURE.  In particular:
17151b362b15af34006e6a11974088a46d42b903418eJohann//
17161b362b15af34006e6a11974088a46d42b903418eJohann//   EXPECT_TRUE  verifies that a Boolean condition is true.
17171b362b15af34006e6a11974088a46d42b903418eJohann//   EXPECT_FALSE verifies that a Boolean condition is false.
17181b362b15af34006e6a11974088a46d42b903418eJohann//
17191b362b15af34006e6a11974088a46d42b903418eJohann// FAIL and ASSERT_* are similar to ADD_FAILURE and EXPECT_*, except
17201b362b15af34006e6a11974088a46d42b903418eJohann// that they will also abort the current function on failure.  People
17211b362b15af34006e6a11974088a46d42b903418eJohann// usually want the fail-fast behavior of FAIL and ASSERT_*, but those
17221b362b15af34006e6a11974088a46d42b903418eJohann// writing data-driven tests often find themselves using ADD_FAILURE
17231b362b15af34006e6a11974088a46d42b903418eJohann// and EXPECT_* more.
17241b362b15af34006e6a11974088a46d42b903418eJohann//
17251b362b15af34006e6a11974088a46d42b903418eJohann// Examples:
17261b362b15af34006e6a11974088a46d42b903418eJohann//
17271b362b15af34006e6a11974088a46d42b903418eJohann//   EXPECT_TRUE(server.StatusIsOK());
17281b362b15af34006e6a11974088a46d42b903418eJohann//   ASSERT_FALSE(server.HasPendingRequest(port))
17291b362b15af34006e6a11974088a46d42b903418eJohann//       << "There are still pending requests " << "on port " << port;
17301b362b15af34006e6a11974088a46d42b903418eJohann
17311b362b15af34006e6a11974088a46d42b903418eJohann// Generates a nonfatal failure with a generic message.
17321b362b15af34006e6a11974088a46d42b903418eJohann#define ADD_FAILURE() GTEST_NONFATAL_FAILURE_("Failed")
17331b362b15af34006e6a11974088a46d42b903418eJohann
17341b362b15af34006e6a11974088a46d42b903418eJohann// Generates a nonfatal failure at the given source file location with
17351b362b15af34006e6a11974088a46d42b903418eJohann// a generic message.
17361b362b15af34006e6a11974088a46d42b903418eJohann#define ADD_FAILURE_AT(file, line) \
17371b362b15af34006e6a11974088a46d42b903418eJohann  GTEST_MESSAGE_AT_(file, line, "Failed", \
17381b362b15af34006e6a11974088a46d42b903418eJohann                    ::testing::TestPartResult::kNonFatalFailure)
17391b362b15af34006e6a11974088a46d42b903418eJohann
17401b362b15af34006e6a11974088a46d42b903418eJohann// Generates a fatal failure with a generic message.
17411b362b15af34006e6a11974088a46d42b903418eJohann#define GTEST_FAIL() GTEST_FATAL_FAILURE_("Failed")
17421b362b15af34006e6a11974088a46d42b903418eJohann
17431b362b15af34006e6a11974088a46d42b903418eJohann// Define this macro to 1 to omit the definition of FAIL(), which is a
17441b362b15af34006e6a11974088a46d42b903418eJohann// generic name and clashes with some other libraries.
17451b362b15af34006e6a11974088a46d42b903418eJohann#if !GTEST_DONT_DEFINE_FAIL
17461b362b15af34006e6a11974088a46d42b903418eJohann# define FAIL() GTEST_FAIL()
17471b362b15af34006e6a11974088a46d42b903418eJohann#endif
17481b362b15af34006e6a11974088a46d42b903418eJohann
17491b362b15af34006e6a11974088a46d42b903418eJohann// Generates a success with a generic message.
17501b362b15af34006e6a11974088a46d42b903418eJohann#define GTEST_SUCCEED() GTEST_SUCCESS_("Succeeded")
17511b362b15af34006e6a11974088a46d42b903418eJohann
17521b362b15af34006e6a11974088a46d42b903418eJohann// Define this macro to 1 to omit the definition of SUCCEED(), which
17531b362b15af34006e6a11974088a46d42b903418eJohann// is a generic name and clashes with some other libraries.
17541b362b15af34006e6a11974088a46d42b903418eJohann#if !GTEST_DONT_DEFINE_SUCCEED
17551b362b15af34006e6a11974088a46d42b903418eJohann# define SUCCEED() GTEST_SUCCEED()
17561b362b15af34006e6a11974088a46d42b903418eJohann#endif
17571b362b15af34006e6a11974088a46d42b903418eJohann
17581b362b15af34006e6a11974088a46d42b903418eJohann// Macros for testing exceptions.
17591b362b15af34006e6a11974088a46d42b903418eJohann//
17601b362b15af34006e6a11974088a46d42b903418eJohann//    * {ASSERT|EXPECT}_THROW(statement, expected_exception):
17611b362b15af34006e6a11974088a46d42b903418eJohann//         Tests that the statement throws the expected exception.
17621b362b15af34006e6a11974088a46d42b903418eJohann//    * {ASSERT|EXPECT}_NO_THROW(statement):
17631b362b15af34006e6a11974088a46d42b903418eJohann//         Tests that the statement doesn't throw any exception.
17641b362b15af34006e6a11974088a46d42b903418eJohann//    * {ASSERT|EXPECT}_ANY_THROW(statement):
17651b362b15af34006e6a11974088a46d42b903418eJohann//         Tests that the statement throws an exception.
17661b362b15af34006e6a11974088a46d42b903418eJohann
17671b362b15af34006e6a11974088a46d42b903418eJohann#define EXPECT_THROW(statement, expected_exception) \
17681b362b15af34006e6a11974088a46d42b903418eJohann  GTEST_TEST_THROW_(statement, expected_exception, GTEST_NONFATAL_FAILURE_)
17691b362b15af34006e6a11974088a46d42b903418eJohann#define EXPECT_NO_THROW(statement) \
17701b362b15af34006e6a11974088a46d42b903418eJohann  GTEST_TEST_NO_THROW_(statement, GTEST_NONFATAL_FAILURE_)
17711b362b15af34006e6a11974088a46d42b903418eJohann#define EXPECT_ANY_THROW(statement) \
17721b362b15af34006e6a11974088a46d42b903418eJohann  GTEST_TEST_ANY_THROW_(statement, GTEST_NONFATAL_FAILURE_)
17731b362b15af34006e6a11974088a46d42b903418eJohann#define ASSERT_THROW(statement, expected_exception) \
17741b362b15af34006e6a11974088a46d42b903418eJohann  GTEST_TEST_THROW_(statement, expected_exception, GTEST_FATAL_FAILURE_)
17751b362b15af34006e6a11974088a46d42b903418eJohann#define ASSERT_NO_THROW(statement) \
17761b362b15af34006e6a11974088a46d42b903418eJohann  GTEST_TEST_NO_THROW_(statement, GTEST_FATAL_FAILURE_)
17771b362b15af34006e6a11974088a46d42b903418eJohann#define ASSERT_ANY_THROW(statement) \
17781b362b15af34006e6a11974088a46d42b903418eJohann  GTEST_TEST_ANY_THROW_(statement, GTEST_FATAL_FAILURE_)
17791b362b15af34006e6a11974088a46d42b903418eJohann
17801b362b15af34006e6a11974088a46d42b903418eJohann// Boolean assertions. Condition can be either a Boolean expression or an
17811b362b15af34006e6a11974088a46d42b903418eJohann// AssertionResult. For more information on how to use AssertionResult with
17821b362b15af34006e6a11974088a46d42b903418eJohann// these macros see comments on that class.
17831b362b15af34006e6a11974088a46d42b903418eJohann#define EXPECT_TRUE(condition) \
17841b362b15af34006e6a11974088a46d42b903418eJohann  GTEST_TEST_BOOLEAN_(condition, #condition, false, true, \
17851b362b15af34006e6a11974088a46d42b903418eJohann                      GTEST_NONFATAL_FAILURE_)
17861b362b15af34006e6a11974088a46d42b903418eJohann#define EXPECT_FALSE(condition) \
17871b362b15af34006e6a11974088a46d42b903418eJohann  GTEST_TEST_BOOLEAN_(!(condition), #condition, true, false, \
17881b362b15af34006e6a11974088a46d42b903418eJohann                      GTEST_NONFATAL_FAILURE_)
17891b362b15af34006e6a11974088a46d42b903418eJohann#define ASSERT_TRUE(condition) \
17901b362b15af34006e6a11974088a46d42b903418eJohann  GTEST_TEST_BOOLEAN_(condition, #condition, false, true, \
17911b362b15af34006e6a11974088a46d42b903418eJohann                      GTEST_FATAL_FAILURE_)
17921b362b15af34006e6a11974088a46d42b903418eJohann#define ASSERT_FALSE(condition) \
17931b362b15af34006e6a11974088a46d42b903418eJohann  GTEST_TEST_BOOLEAN_(!(condition), #condition, true, false, \
17941b362b15af34006e6a11974088a46d42b903418eJohann                      GTEST_FATAL_FAILURE_)
17951b362b15af34006e6a11974088a46d42b903418eJohann
17961b362b15af34006e6a11974088a46d42b903418eJohann// Includes the auto-generated header that implements a family of
17971b362b15af34006e6a11974088a46d42b903418eJohann// generic predicate assertion macros.
17981b362b15af34006e6a11974088a46d42b903418eJohann#include "gtest/gtest_pred_impl.h"
17991b362b15af34006e6a11974088a46d42b903418eJohann
18001b362b15af34006e6a11974088a46d42b903418eJohann// Macros for testing equalities and inequalities.
18011b362b15af34006e6a11974088a46d42b903418eJohann//
18021b362b15af34006e6a11974088a46d42b903418eJohann//    * {ASSERT|EXPECT}_EQ(expected, actual): Tests that expected == actual
18031b362b15af34006e6a11974088a46d42b903418eJohann//    * {ASSERT|EXPECT}_NE(v1, v2):           Tests that v1 != v2
18041b362b15af34006e6a11974088a46d42b903418eJohann//    * {ASSERT|EXPECT}_LT(v1, v2):           Tests that v1 < v2
18051b362b15af34006e6a11974088a46d42b903418eJohann//    * {ASSERT|EXPECT}_LE(v1, v2):           Tests that v1 <= v2
18061b362b15af34006e6a11974088a46d42b903418eJohann//    * {ASSERT|EXPECT}_GT(v1, v2):           Tests that v1 > v2
18071b362b15af34006e6a11974088a46d42b903418eJohann//    * {ASSERT|EXPECT}_GE(v1, v2):           Tests that v1 >= v2
18081b362b15af34006e6a11974088a46d42b903418eJohann//
18091b362b15af34006e6a11974088a46d42b903418eJohann// When they are not, Google Test prints both the tested expressions and
18101b362b15af34006e6a11974088a46d42b903418eJohann// their actual values.  The values must be compatible built-in types,
18111b362b15af34006e6a11974088a46d42b903418eJohann// or you will get a compiler error.  By "compatible" we mean that the
18121b362b15af34006e6a11974088a46d42b903418eJohann// values can be compared by the respective operator.
18131b362b15af34006e6a11974088a46d42b903418eJohann//
18141b362b15af34006e6a11974088a46d42b903418eJohann// Note:
18151b362b15af34006e6a11974088a46d42b903418eJohann//
18161b362b15af34006e6a11974088a46d42b903418eJohann//   1. It is possible to make a user-defined type work with
18171b362b15af34006e6a11974088a46d42b903418eJohann//   {ASSERT|EXPECT}_??(), but that requires overloading the
18181b362b15af34006e6a11974088a46d42b903418eJohann//   comparison operators and is thus discouraged by the Google C++
18191b362b15af34006e6a11974088a46d42b903418eJohann//   Usage Guide.  Therefore, you are advised to use the
18201b362b15af34006e6a11974088a46d42b903418eJohann//   {ASSERT|EXPECT}_TRUE() macro to assert that two objects are
18211b362b15af34006e6a11974088a46d42b903418eJohann//   equal.
18221b362b15af34006e6a11974088a46d42b903418eJohann//
18231b362b15af34006e6a11974088a46d42b903418eJohann//   2. The {ASSERT|EXPECT}_??() macros do pointer comparisons on
18241b362b15af34006e6a11974088a46d42b903418eJohann//   pointers (in particular, C strings).  Therefore, if you use it
18251b362b15af34006e6a11974088a46d42b903418eJohann//   with two C strings, you are testing how their locations in memory
18261b362b15af34006e6a11974088a46d42b903418eJohann//   are related, not how their content is related.  To compare two C
18271b362b15af34006e6a11974088a46d42b903418eJohann//   strings by content, use {ASSERT|EXPECT}_STR*().
18281b362b15af34006e6a11974088a46d42b903418eJohann//
18291b362b15af34006e6a11974088a46d42b903418eJohann//   3. {ASSERT|EXPECT}_EQ(expected, actual) is preferred to
18301b362b15af34006e6a11974088a46d42b903418eJohann//   {ASSERT|EXPECT}_TRUE(expected == actual), as the former tells you
18311b362b15af34006e6a11974088a46d42b903418eJohann//   what the actual value is when it fails, and similarly for the
18321b362b15af34006e6a11974088a46d42b903418eJohann//   other comparisons.
18331b362b15af34006e6a11974088a46d42b903418eJohann//
18341b362b15af34006e6a11974088a46d42b903418eJohann//   4. Do not depend on the order in which {ASSERT|EXPECT}_??()
18351b362b15af34006e6a11974088a46d42b903418eJohann//   evaluate their arguments, which is undefined.
18361b362b15af34006e6a11974088a46d42b903418eJohann//
18371b362b15af34006e6a11974088a46d42b903418eJohann//   5. These macros evaluate their arguments exactly once.
18381b362b15af34006e6a11974088a46d42b903418eJohann//
18391b362b15af34006e6a11974088a46d42b903418eJohann// Examples:
18401b362b15af34006e6a11974088a46d42b903418eJohann//
18411b362b15af34006e6a11974088a46d42b903418eJohann//   EXPECT_NE(5, Foo());
18421b362b15af34006e6a11974088a46d42b903418eJohann//   EXPECT_EQ(NULL, a_pointer);
18431b362b15af34006e6a11974088a46d42b903418eJohann//   ASSERT_LT(i, array_size);
18441b362b15af34006e6a11974088a46d42b903418eJohann//   ASSERT_GT(records.size(), 0) << "There is no record left.";
18451b362b15af34006e6a11974088a46d42b903418eJohann
18461b362b15af34006e6a11974088a46d42b903418eJohann#define EXPECT_EQ(expected, actual) \
18471b362b15af34006e6a11974088a46d42b903418eJohann  EXPECT_PRED_FORMAT2(::testing::internal:: \
18481b362b15af34006e6a11974088a46d42b903418eJohann                      EqHelper<GTEST_IS_NULL_LITERAL_(expected)>::Compare, \
18491b362b15af34006e6a11974088a46d42b903418eJohann                      expected, actual)
18501b362b15af34006e6a11974088a46d42b903418eJohann#define EXPECT_NE(expected, actual) \
18511b362b15af34006e6a11974088a46d42b903418eJohann  EXPECT_PRED_FORMAT2(::testing::internal::CmpHelperNE, expected, actual)
18521b362b15af34006e6a11974088a46d42b903418eJohann#define EXPECT_LE(val1, val2) \
18531b362b15af34006e6a11974088a46d42b903418eJohann  EXPECT_PRED_FORMAT2(::testing::internal::CmpHelperLE, val1, val2)
18541b362b15af34006e6a11974088a46d42b903418eJohann#define EXPECT_LT(val1, val2) \
18551b362b15af34006e6a11974088a46d42b903418eJohann  EXPECT_PRED_FORMAT2(::testing::internal::CmpHelperLT, val1, val2)
18561b362b15af34006e6a11974088a46d42b903418eJohann#define EXPECT_GE(val1, val2) \
18571b362b15af34006e6a11974088a46d42b903418eJohann  EXPECT_PRED_FORMAT2(::testing::internal::CmpHelperGE, val1, val2)
18581b362b15af34006e6a11974088a46d42b903418eJohann#define EXPECT_GT(val1, val2) \
18591b362b15af34006e6a11974088a46d42b903418eJohann  EXPECT_PRED_FORMAT2(::testing::internal::CmpHelperGT, val1, val2)
18601b362b15af34006e6a11974088a46d42b903418eJohann
18611b362b15af34006e6a11974088a46d42b903418eJohann#define GTEST_ASSERT_EQ(expected, actual) \
18621b362b15af34006e6a11974088a46d42b903418eJohann  ASSERT_PRED_FORMAT2(::testing::internal:: \
18631b362b15af34006e6a11974088a46d42b903418eJohann                      EqHelper<GTEST_IS_NULL_LITERAL_(expected)>::Compare, \
18641b362b15af34006e6a11974088a46d42b903418eJohann                      expected, actual)
18651b362b15af34006e6a11974088a46d42b903418eJohann#define GTEST_ASSERT_NE(val1, val2) \
18661b362b15af34006e6a11974088a46d42b903418eJohann  ASSERT_PRED_FORMAT2(::testing::internal::CmpHelperNE, val1, val2)
18671b362b15af34006e6a11974088a46d42b903418eJohann#define GTEST_ASSERT_LE(val1, val2) \
18681b362b15af34006e6a11974088a46d42b903418eJohann  ASSERT_PRED_FORMAT2(::testing::internal::CmpHelperLE, val1, val2)
18691b362b15af34006e6a11974088a46d42b903418eJohann#define GTEST_ASSERT_LT(val1, val2) \
18701b362b15af34006e6a11974088a46d42b903418eJohann  ASSERT_PRED_FORMAT2(::testing::internal::CmpHelperLT, val1, val2)
18711b362b15af34006e6a11974088a46d42b903418eJohann#define GTEST_ASSERT_GE(val1, val2) \
18721b362b15af34006e6a11974088a46d42b903418eJohann  ASSERT_PRED_FORMAT2(::testing::internal::CmpHelperGE, val1, val2)
18731b362b15af34006e6a11974088a46d42b903418eJohann#define GTEST_ASSERT_GT(val1, val2) \
18741b362b15af34006e6a11974088a46d42b903418eJohann  ASSERT_PRED_FORMAT2(::testing::internal::CmpHelperGT, val1, val2)
18751b362b15af34006e6a11974088a46d42b903418eJohann
18761b362b15af34006e6a11974088a46d42b903418eJohann// Define macro GTEST_DONT_DEFINE_ASSERT_XY to 1 to omit the definition of
18771b362b15af34006e6a11974088a46d42b903418eJohann// ASSERT_XY(), which clashes with some users' own code.
18781b362b15af34006e6a11974088a46d42b903418eJohann
18791b362b15af34006e6a11974088a46d42b903418eJohann#if !GTEST_DONT_DEFINE_ASSERT_EQ
18801b362b15af34006e6a11974088a46d42b903418eJohann# define ASSERT_EQ(val1, val2) GTEST_ASSERT_EQ(val1, val2)
18811b362b15af34006e6a11974088a46d42b903418eJohann#endif
18821b362b15af34006e6a11974088a46d42b903418eJohann
18831b362b15af34006e6a11974088a46d42b903418eJohann#if !GTEST_DONT_DEFINE_ASSERT_NE
18841b362b15af34006e6a11974088a46d42b903418eJohann# define ASSERT_NE(val1, val2) GTEST_ASSERT_NE(val1, val2)
18851b362b15af34006e6a11974088a46d42b903418eJohann#endif
18861b362b15af34006e6a11974088a46d42b903418eJohann
18871b362b15af34006e6a11974088a46d42b903418eJohann#if !GTEST_DONT_DEFINE_ASSERT_LE
18881b362b15af34006e6a11974088a46d42b903418eJohann# define ASSERT_LE(val1, val2) GTEST_ASSERT_LE(val1, val2)
18891b362b15af34006e6a11974088a46d42b903418eJohann#endif
18901b362b15af34006e6a11974088a46d42b903418eJohann
18911b362b15af34006e6a11974088a46d42b903418eJohann#if !GTEST_DONT_DEFINE_ASSERT_LT
18921b362b15af34006e6a11974088a46d42b903418eJohann# define ASSERT_LT(val1, val2) GTEST_ASSERT_LT(val1, val2)
18931b362b15af34006e6a11974088a46d42b903418eJohann#endif
18941b362b15af34006e6a11974088a46d42b903418eJohann
18951b362b15af34006e6a11974088a46d42b903418eJohann#if !GTEST_DONT_DEFINE_ASSERT_GE
18961b362b15af34006e6a11974088a46d42b903418eJohann# define ASSERT_GE(val1, val2) GTEST_ASSERT_GE(val1, val2)
18971b362b15af34006e6a11974088a46d42b903418eJohann#endif
18981b362b15af34006e6a11974088a46d42b903418eJohann
18991b362b15af34006e6a11974088a46d42b903418eJohann#if !GTEST_DONT_DEFINE_ASSERT_GT
19001b362b15af34006e6a11974088a46d42b903418eJohann# define ASSERT_GT(val1, val2) GTEST_ASSERT_GT(val1, val2)
19011b362b15af34006e6a11974088a46d42b903418eJohann#endif
19021b362b15af34006e6a11974088a46d42b903418eJohann
19031b362b15af34006e6a11974088a46d42b903418eJohann// C String Comparisons.  All tests treat NULL and any non-NULL string
19041b362b15af34006e6a11974088a46d42b903418eJohann// as different.  Two NULLs are equal.
19051b362b15af34006e6a11974088a46d42b903418eJohann//
19061b362b15af34006e6a11974088a46d42b903418eJohann//    * {ASSERT|EXPECT}_STREQ(s1, s2):     Tests that s1 == s2
19071b362b15af34006e6a11974088a46d42b903418eJohann//    * {ASSERT|EXPECT}_STRNE(s1, s2):     Tests that s1 != s2
19081b362b15af34006e6a11974088a46d42b903418eJohann//    * {ASSERT|EXPECT}_STRCASEEQ(s1, s2): Tests that s1 == s2, ignoring case
19091b362b15af34006e6a11974088a46d42b903418eJohann//    * {ASSERT|EXPECT}_STRCASENE(s1, s2): Tests that s1 != s2, ignoring case
19101b362b15af34006e6a11974088a46d42b903418eJohann//
19111b362b15af34006e6a11974088a46d42b903418eJohann// For wide or narrow string objects, you can use the
19121b362b15af34006e6a11974088a46d42b903418eJohann// {ASSERT|EXPECT}_??() macros.
19131b362b15af34006e6a11974088a46d42b903418eJohann//
19141b362b15af34006e6a11974088a46d42b903418eJohann// Don't depend on the order in which the arguments are evaluated,
19151b362b15af34006e6a11974088a46d42b903418eJohann// which is undefined.
19161b362b15af34006e6a11974088a46d42b903418eJohann//
19171b362b15af34006e6a11974088a46d42b903418eJohann// These macros evaluate their arguments exactly once.
19181b362b15af34006e6a11974088a46d42b903418eJohann
19191b362b15af34006e6a11974088a46d42b903418eJohann#define EXPECT_STREQ(expected, actual) \
19201b362b15af34006e6a11974088a46d42b903418eJohann  EXPECT_PRED_FORMAT2(::testing::internal::CmpHelperSTREQ, expected, actual)
19211b362b15af34006e6a11974088a46d42b903418eJohann#define EXPECT_STRNE(s1, s2) \
19221b362b15af34006e6a11974088a46d42b903418eJohann  EXPECT_PRED_FORMAT2(::testing::internal::CmpHelperSTRNE, s1, s2)
19231b362b15af34006e6a11974088a46d42b903418eJohann#define EXPECT_STRCASEEQ(expected, actual) \
19241b362b15af34006e6a11974088a46d42b903418eJohann  EXPECT_PRED_FORMAT2(::testing::internal::CmpHelperSTRCASEEQ, expected, actual)
19251b362b15af34006e6a11974088a46d42b903418eJohann#define EXPECT_STRCASENE(s1, s2)\
19261b362b15af34006e6a11974088a46d42b903418eJohann  EXPECT_PRED_FORMAT2(::testing::internal::CmpHelperSTRCASENE, s1, s2)
19271b362b15af34006e6a11974088a46d42b903418eJohann
19281b362b15af34006e6a11974088a46d42b903418eJohann#define ASSERT_STREQ(expected, actual) \
19291b362b15af34006e6a11974088a46d42b903418eJohann  ASSERT_PRED_FORMAT2(::testing::internal::CmpHelperSTREQ, expected, actual)
19301b362b15af34006e6a11974088a46d42b903418eJohann#define ASSERT_STRNE(s1, s2) \
19311b362b15af34006e6a11974088a46d42b903418eJohann  ASSERT_PRED_FORMAT2(::testing::internal::CmpHelperSTRNE, s1, s2)
19321b362b15af34006e6a11974088a46d42b903418eJohann#define ASSERT_STRCASEEQ(expected, actual) \
19331b362b15af34006e6a11974088a46d42b903418eJohann  ASSERT_PRED_FORMAT2(::testing::internal::CmpHelperSTRCASEEQ, expected, actual)
19341b362b15af34006e6a11974088a46d42b903418eJohann#define ASSERT_STRCASENE(s1, s2)\
19351b362b15af34006e6a11974088a46d42b903418eJohann  ASSERT_PRED_FORMAT2(::testing::internal::CmpHelperSTRCASENE, s1, s2)
19361b362b15af34006e6a11974088a46d42b903418eJohann
19371b362b15af34006e6a11974088a46d42b903418eJohann// Macros for comparing floating-point numbers.
19381b362b15af34006e6a11974088a46d42b903418eJohann//
19391b362b15af34006e6a11974088a46d42b903418eJohann//    * {ASSERT|EXPECT}_FLOAT_EQ(expected, actual):
19401b362b15af34006e6a11974088a46d42b903418eJohann//         Tests that two float values are almost equal.
19411b362b15af34006e6a11974088a46d42b903418eJohann//    * {ASSERT|EXPECT}_DOUBLE_EQ(expected, actual):
19421b362b15af34006e6a11974088a46d42b903418eJohann//         Tests that two double values are almost equal.
19431b362b15af34006e6a11974088a46d42b903418eJohann//    * {ASSERT|EXPECT}_NEAR(v1, v2, abs_error):
19441b362b15af34006e6a11974088a46d42b903418eJohann//         Tests that v1 and v2 are within the given distance to each other.
19451b362b15af34006e6a11974088a46d42b903418eJohann//
19461b362b15af34006e6a11974088a46d42b903418eJohann// Google Test uses ULP-based comparison to automatically pick a default
19471b362b15af34006e6a11974088a46d42b903418eJohann// error bound that is appropriate for the operands.  See the
19481b362b15af34006e6a11974088a46d42b903418eJohann// FloatingPoint template class in gtest-internal.h if you are
19491b362b15af34006e6a11974088a46d42b903418eJohann// interested in the implementation details.
19501b362b15af34006e6a11974088a46d42b903418eJohann
19511b362b15af34006e6a11974088a46d42b903418eJohann#define EXPECT_FLOAT_EQ(expected, actual)\
19521b362b15af34006e6a11974088a46d42b903418eJohann  EXPECT_PRED_FORMAT2(::testing::internal::CmpHelperFloatingPointEQ<float>, \
19531b362b15af34006e6a11974088a46d42b903418eJohann                      expected, actual)
19541b362b15af34006e6a11974088a46d42b903418eJohann
19551b362b15af34006e6a11974088a46d42b903418eJohann#define EXPECT_DOUBLE_EQ(expected, actual)\
19561b362b15af34006e6a11974088a46d42b903418eJohann  EXPECT_PRED_FORMAT2(::testing::internal::CmpHelperFloatingPointEQ<double>, \
19571b362b15af34006e6a11974088a46d42b903418eJohann                      expected, actual)
19581b362b15af34006e6a11974088a46d42b903418eJohann
19591b362b15af34006e6a11974088a46d42b903418eJohann#define ASSERT_FLOAT_EQ(expected, actual)\
19601b362b15af34006e6a11974088a46d42b903418eJohann  ASSERT_PRED_FORMAT2(::testing::internal::CmpHelperFloatingPointEQ<float>, \
19611b362b15af34006e6a11974088a46d42b903418eJohann                      expected, actual)
19621b362b15af34006e6a11974088a46d42b903418eJohann
19631b362b15af34006e6a11974088a46d42b903418eJohann#define ASSERT_DOUBLE_EQ(expected, actual)\
19641b362b15af34006e6a11974088a46d42b903418eJohann  ASSERT_PRED_FORMAT2(::testing::internal::CmpHelperFloatingPointEQ<double>, \
19651b362b15af34006e6a11974088a46d42b903418eJohann                      expected, actual)
19661b362b15af34006e6a11974088a46d42b903418eJohann
19671b362b15af34006e6a11974088a46d42b903418eJohann#define EXPECT_NEAR(val1, val2, abs_error)\
19681b362b15af34006e6a11974088a46d42b903418eJohann  EXPECT_PRED_FORMAT3(::testing::internal::DoubleNearPredFormat, \
19691b362b15af34006e6a11974088a46d42b903418eJohann                      val1, val2, abs_error)
19701b362b15af34006e6a11974088a46d42b903418eJohann
19711b362b15af34006e6a11974088a46d42b903418eJohann#define ASSERT_NEAR(val1, val2, abs_error)\
19721b362b15af34006e6a11974088a46d42b903418eJohann  ASSERT_PRED_FORMAT3(::testing::internal::DoubleNearPredFormat, \
19731b362b15af34006e6a11974088a46d42b903418eJohann                      val1, val2, abs_error)
19741b362b15af34006e6a11974088a46d42b903418eJohann
19751b362b15af34006e6a11974088a46d42b903418eJohann// These predicate format functions work on floating-point values, and
19761b362b15af34006e6a11974088a46d42b903418eJohann// can be used in {ASSERT|EXPECT}_PRED_FORMAT2*(), e.g.
19771b362b15af34006e6a11974088a46d42b903418eJohann//
19781b362b15af34006e6a11974088a46d42b903418eJohann//   EXPECT_PRED_FORMAT2(testing::DoubleLE, Foo(), 5.0);
19791b362b15af34006e6a11974088a46d42b903418eJohann
19801b362b15af34006e6a11974088a46d42b903418eJohann// Asserts that val1 is less than, or almost equal to, val2.  Fails
19811b362b15af34006e6a11974088a46d42b903418eJohann// otherwise.  In particular, it fails if either val1 or val2 is NaN.
19821b362b15af34006e6a11974088a46d42b903418eJohannGTEST_API_ AssertionResult FloatLE(const char* expr1, const char* expr2,
19831b362b15af34006e6a11974088a46d42b903418eJohann                                   float val1, float val2);
19841b362b15af34006e6a11974088a46d42b903418eJohannGTEST_API_ AssertionResult DoubleLE(const char* expr1, const char* expr2,
19851b362b15af34006e6a11974088a46d42b903418eJohann                                    double val1, double val2);
19861b362b15af34006e6a11974088a46d42b903418eJohann
19871b362b15af34006e6a11974088a46d42b903418eJohann
19881b362b15af34006e6a11974088a46d42b903418eJohann#if GTEST_OS_WINDOWS
19891b362b15af34006e6a11974088a46d42b903418eJohann
19901b362b15af34006e6a11974088a46d42b903418eJohann// Macros that test for HRESULT failure and success, these are only useful
19911b362b15af34006e6a11974088a46d42b903418eJohann// on Windows, and rely on Windows SDK macros and APIs to compile.
19921b362b15af34006e6a11974088a46d42b903418eJohann//
19931b362b15af34006e6a11974088a46d42b903418eJohann//    * {ASSERT|EXPECT}_HRESULT_{SUCCEEDED|FAILED}(expr)
19941b362b15af34006e6a11974088a46d42b903418eJohann//
19951b362b15af34006e6a11974088a46d42b903418eJohann// When expr unexpectedly fails or succeeds, Google Test prints the
19961b362b15af34006e6a11974088a46d42b903418eJohann// expected result and the actual result with both a human-readable
19971b362b15af34006e6a11974088a46d42b903418eJohann// string representation of the error, if available, as well as the
19981b362b15af34006e6a11974088a46d42b903418eJohann// hex result code.
19991b362b15af34006e6a11974088a46d42b903418eJohann# define EXPECT_HRESULT_SUCCEEDED(expr) \
20001b362b15af34006e6a11974088a46d42b903418eJohann    EXPECT_PRED_FORMAT1(::testing::internal::IsHRESULTSuccess, (expr))
20011b362b15af34006e6a11974088a46d42b903418eJohann
20021b362b15af34006e6a11974088a46d42b903418eJohann# define ASSERT_HRESULT_SUCCEEDED(expr) \
20031b362b15af34006e6a11974088a46d42b903418eJohann    ASSERT_PRED_FORMAT1(::testing::internal::IsHRESULTSuccess, (expr))
20041b362b15af34006e6a11974088a46d42b903418eJohann
20051b362b15af34006e6a11974088a46d42b903418eJohann# define EXPECT_HRESULT_FAILED(expr) \
20061b362b15af34006e6a11974088a46d42b903418eJohann    EXPECT_PRED_FORMAT1(::testing::internal::IsHRESULTFailure, (expr))
20071b362b15af34006e6a11974088a46d42b903418eJohann
20081b362b15af34006e6a11974088a46d42b903418eJohann# define ASSERT_HRESULT_FAILED(expr) \
20091b362b15af34006e6a11974088a46d42b903418eJohann    ASSERT_PRED_FORMAT1(::testing::internal::IsHRESULTFailure, (expr))
20101b362b15af34006e6a11974088a46d42b903418eJohann
20111b362b15af34006e6a11974088a46d42b903418eJohann#endif  // GTEST_OS_WINDOWS
20121b362b15af34006e6a11974088a46d42b903418eJohann
20131b362b15af34006e6a11974088a46d42b903418eJohann// Macros that execute statement and check that it doesn't generate new fatal
20141b362b15af34006e6a11974088a46d42b903418eJohann// failures in the current thread.
20151b362b15af34006e6a11974088a46d42b903418eJohann//
20161b362b15af34006e6a11974088a46d42b903418eJohann//   * {ASSERT|EXPECT}_NO_FATAL_FAILURE(statement);
20171b362b15af34006e6a11974088a46d42b903418eJohann//
20181b362b15af34006e6a11974088a46d42b903418eJohann// Examples:
20191b362b15af34006e6a11974088a46d42b903418eJohann//
20201b362b15af34006e6a11974088a46d42b903418eJohann//   EXPECT_NO_FATAL_FAILURE(Process());
20211b362b15af34006e6a11974088a46d42b903418eJohann//   ASSERT_NO_FATAL_FAILURE(Process()) << "Process() failed";
20221b362b15af34006e6a11974088a46d42b903418eJohann//
20231b362b15af34006e6a11974088a46d42b903418eJohann#define ASSERT_NO_FATAL_FAILURE(statement) \
20241b362b15af34006e6a11974088a46d42b903418eJohann    GTEST_TEST_NO_FATAL_FAILURE_(statement, GTEST_FATAL_FAILURE_)
20251b362b15af34006e6a11974088a46d42b903418eJohann#define EXPECT_NO_FATAL_FAILURE(statement) \
20261b362b15af34006e6a11974088a46d42b903418eJohann    GTEST_TEST_NO_FATAL_FAILURE_(statement, GTEST_NONFATAL_FAILURE_)
20271b362b15af34006e6a11974088a46d42b903418eJohann
20281b362b15af34006e6a11974088a46d42b903418eJohann// Causes a trace (including the source file path, the current line
20291b362b15af34006e6a11974088a46d42b903418eJohann// number, and the given message) to be included in every test failure
20301b362b15af34006e6a11974088a46d42b903418eJohann// message generated by code in the current scope.  The effect is
20311b362b15af34006e6a11974088a46d42b903418eJohann// undone when the control leaves the current scope.
20321b362b15af34006e6a11974088a46d42b903418eJohann//
20331b362b15af34006e6a11974088a46d42b903418eJohann// The message argument can be anything streamable to std::ostream.
20341b362b15af34006e6a11974088a46d42b903418eJohann//
20351b362b15af34006e6a11974088a46d42b903418eJohann// In the implementation, we include the current line number as part
20361b362b15af34006e6a11974088a46d42b903418eJohann// of the dummy variable name, thus allowing multiple SCOPED_TRACE()s
20371b362b15af34006e6a11974088a46d42b903418eJohann// to appear in the same block - as long as they are on different
20381b362b15af34006e6a11974088a46d42b903418eJohann// lines.
20391b362b15af34006e6a11974088a46d42b903418eJohann#define SCOPED_TRACE(message) \
20401b362b15af34006e6a11974088a46d42b903418eJohann  ::testing::internal::ScopedTrace GTEST_CONCAT_TOKEN_(gtest_trace_, __LINE__)(\
20411b362b15af34006e6a11974088a46d42b903418eJohann    __FILE__, __LINE__, ::testing::Message() << (message))
20421b362b15af34006e6a11974088a46d42b903418eJohann
20431b362b15af34006e6a11974088a46d42b903418eJohann// Compile-time assertion for type equality.
20441b362b15af34006e6a11974088a46d42b903418eJohann// StaticAssertTypeEq<type1, type2>() compiles iff type1 and type2 are
20451b362b15af34006e6a11974088a46d42b903418eJohann// the same type.  The value it returns is not interesting.
20461b362b15af34006e6a11974088a46d42b903418eJohann//
20471b362b15af34006e6a11974088a46d42b903418eJohann// Instead of making StaticAssertTypeEq a class template, we make it a
20481b362b15af34006e6a11974088a46d42b903418eJohann// function template that invokes a helper class template.  This
20491b362b15af34006e6a11974088a46d42b903418eJohann// prevents a user from misusing StaticAssertTypeEq<T1, T2> by
20501b362b15af34006e6a11974088a46d42b903418eJohann// defining objects of that type.
20511b362b15af34006e6a11974088a46d42b903418eJohann//
20521b362b15af34006e6a11974088a46d42b903418eJohann// CAVEAT:
20531b362b15af34006e6a11974088a46d42b903418eJohann//
20541b362b15af34006e6a11974088a46d42b903418eJohann// When used inside a method of a class template,
20551b362b15af34006e6a11974088a46d42b903418eJohann// StaticAssertTypeEq<T1, T2>() is effective ONLY IF the method is
20561b362b15af34006e6a11974088a46d42b903418eJohann// instantiated.  For example, given:
20571b362b15af34006e6a11974088a46d42b903418eJohann//
20581b362b15af34006e6a11974088a46d42b903418eJohann//   template <typename T> class Foo {
20591b362b15af34006e6a11974088a46d42b903418eJohann//    public:
20601b362b15af34006e6a11974088a46d42b903418eJohann//     void Bar() { testing::StaticAssertTypeEq<int, T>(); }
20611b362b15af34006e6a11974088a46d42b903418eJohann//   };
20621b362b15af34006e6a11974088a46d42b903418eJohann//
20631b362b15af34006e6a11974088a46d42b903418eJohann// the code:
20641b362b15af34006e6a11974088a46d42b903418eJohann//
20651b362b15af34006e6a11974088a46d42b903418eJohann//   void Test1() { Foo<bool> foo; }
20661b362b15af34006e6a11974088a46d42b903418eJohann//
20671b362b15af34006e6a11974088a46d42b903418eJohann// will NOT generate a compiler error, as Foo<bool>::Bar() is never
20681b362b15af34006e6a11974088a46d42b903418eJohann// actually instantiated.  Instead, you need:
20691b362b15af34006e6a11974088a46d42b903418eJohann//
20701b362b15af34006e6a11974088a46d42b903418eJohann//   void Test2() { Foo<bool> foo; foo.Bar(); }
20711b362b15af34006e6a11974088a46d42b903418eJohann//
20721b362b15af34006e6a11974088a46d42b903418eJohann// to cause a compiler error.
20731b362b15af34006e6a11974088a46d42b903418eJohanntemplate <typename T1, typename T2>
20741b362b15af34006e6a11974088a46d42b903418eJohannbool StaticAssertTypeEq() {
20751b362b15af34006e6a11974088a46d42b903418eJohann  (void)internal::StaticAssertTypeEqHelper<T1, T2>();
20761b362b15af34006e6a11974088a46d42b903418eJohann  return true;
20771b362b15af34006e6a11974088a46d42b903418eJohann}
20781b362b15af34006e6a11974088a46d42b903418eJohann
20791b362b15af34006e6a11974088a46d42b903418eJohann// Defines a test.
20801b362b15af34006e6a11974088a46d42b903418eJohann//
20811b362b15af34006e6a11974088a46d42b903418eJohann// The first parameter is the name of the test case, and the second
20821b362b15af34006e6a11974088a46d42b903418eJohann// parameter is the name of the test within the test case.
20831b362b15af34006e6a11974088a46d42b903418eJohann//
20841b362b15af34006e6a11974088a46d42b903418eJohann// The convention is to end the test case name with "Test".  For
20851b362b15af34006e6a11974088a46d42b903418eJohann// example, a test case for the Foo class can be named FooTest.
20861b362b15af34006e6a11974088a46d42b903418eJohann//
20871b362b15af34006e6a11974088a46d42b903418eJohann// The user should put his test code between braces after using this
20881b362b15af34006e6a11974088a46d42b903418eJohann// macro.  Example:
20891b362b15af34006e6a11974088a46d42b903418eJohann//
20901b362b15af34006e6a11974088a46d42b903418eJohann//   TEST(FooTest, InitializesCorrectly) {
20911b362b15af34006e6a11974088a46d42b903418eJohann//     Foo foo;
20921b362b15af34006e6a11974088a46d42b903418eJohann//     EXPECT_TRUE(foo.StatusIsOK());
20931b362b15af34006e6a11974088a46d42b903418eJohann//   }
20941b362b15af34006e6a11974088a46d42b903418eJohann
20951b362b15af34006e6a11974088a46d42b903418eJohann// Note that we call GetTestTypeId() instead of GetTypeId<
20961b362b15af34006e6a11974088a46d42b903418eJohann// ::testing::Test>() here to get the type ID of testing::Test.  This
20971b362b15af34006e6a11974088a46d42b903418eJohann// is to work around a suspected linker bug when using Google Test as
20981b362b15af34006e6a11974088a46d42b903418eJohann// a framework on Mac OS X.  The bug causes GetTypeId<
20991b362b15af34006e6a11974088a46d42b903418eJohann// ::testing::Test>() to return different values depending on whether
21001b362b15af34006e6a11974088a46d42b903418eJohann// the call is from the Google Test framework itself or from user test
21011b362b15af34006e6a11974088a46d42b903418eJohann// code.  GetTestTypeId() is guaranteed to always return the same
21021b362b15af34006e6a11974088a46d42b903418eJohann// value, as it always calls GetTypeId<>() from the Google Test
21031b362b15af34006e6a11974088a46d42b903418eJohann// framework.
21041b362b15af34006e6a11974088a46d42b903418eJohann#define GTEST_TEST(test_case_name, test_name)\
21051b362b15af34006e6a11974088a46d42b903418eJohann  GTEST_TEST_(test_case_name, test_name, \
21061b362b15af34006e6a11974088a46d42b903418eJohann              ::testing::Test, ::testing::internal::GetTestTypeId())
21071b362b15af34006e6a11974088a46d42b903418eJohann
21081b362b15af34006e6a11974088a46d42b903418eJohann// Define this macro to 1 to omit the definition of TEST(), which
21091b362b15af34006e6a11974088a46d42b903418eJohann// is a generic name and clashes with some other libraries.
21101b362b15af34006e6a11974088a46d42b903418eJohann#if !GTEST_DONT_DEFINE_TEST
21111b362b15af34006e6a11974088a46d42b903418eJohann# define TEST(test_case_name, test_name) GTEST_TEST(test_case_name, test_name)
21121b362b15af34006e6a11974088a46d42b903418eJohann#endif
21131b362b15af34006e6a11974088a46d42b903418eJohann
21141b362b15af34006e6a11974088a46d42b903418eJohann// Defines a test that uses a test fixture.
21151b362b15af34006e6a11974088a46d42b903418eJohann//
21161b362b15af34006e6a11974088a46d42b903418eJohann// The first parameter is the name of the test fixture class, which
21171b362b15af34006e6a11974088a46d42b903418eJohann// also doubles as the test case name.  The second parameter is the
21181b362b15af34006e6a11974088a46d42b903418eJohann// name of the test within the test case.
21191b362b15af34006e6a11974088a46d42b903418eJohann//
21201b362b15af34006e6a11974088a46d42b903418eJohann// A test fixture class must be declared earlier.  The user should put
21211b362b15af34006e6a11974088a46d42b903418eJohann// his test code between braces after using this macro.  Example:
21221b362b15af34006e6a11974088a46d42b903418eJohann//
21231b362b15af34006e6a11974088a46d42b903418eJohann//   class FooTest : public testing::Test {
21241b362b15af34006e6a11974088a46d42b903418eJohann//    protected:
21251b362b15af34006e6a11974088a46d42b903418eJohann//     virtual void SetUp() { b_.AddElement(3); }
21261b362b15af34006e6a11974088a46d42b903418eJohann//
21271b362b15af34006e6a11974088a46d42b903418eJohann//     Foo a_;
21281b362b15af34006e6a11974088a46d42b903418eJohann//     Foo b_;
21291b362b15af34006e6a11974088a46d42b903418eJohann//   };
21301b362b15af34006e6a11974088a46d42b903418eJohann//
21311b362b15af34006e6a11974088a46d42b903418eJohann//   TEST_F(FooTest, InitializesCorrectly) {
21321b362b15af34006e6a11974088a46d42b903418eJohann//     EXPECT_TRUE(a_.StatusIsOK());
21331b362b15af34006e6a11974088a46d42b903418eJohann//   }
21341b362b15af34006e6a11974088a46d42b903418eJohann//
21351b362b15af34006e6a11974088a46d42b903418eJohann//   TEST_F(FooTest, ReturnsElementCountCorrectly) {
21361b362b15af34006e6a11974088a46d42b903418eJohann//     EXPECT_EQ(0, a_.size());
21371b362b15af34006e6a11974088a46d42b903418eJohann//     EXPECT_EQ(1, b_.size());
21381b362b15af34006e6a11974088a46d42b903418eJohann//   }
21391b362b15af34006e6a11974088a46d42b903418eJohann
21401b362b15af34006e6a11974088a46d42b903418eJohann#define TEST_F(test_fixture, test_name)\
21411b362b15af34006e6a11974088a46d42b903418eJohann  GTEST_TEST_(test_fixture, test_name, test_fixture, \
21421b362b15af34006e6a11974088a46d42b903418eJohann              ::testing::internal::GetTypeId<test_fixture>())
21431b362b15af34006e6a11974088a46d42b903418eJohann
21441b362b15af34006e6a11974088a46d42b903418eJohann// Use this macro in main() to run all tests.  It returns 0 if all
21451b362b15af34006e6a11974088a46d42b903418eJohann// tests are successful, or 1 otherwise.
21461b362b15af34006e6a11974088a46d42b903418eJohann//
21471b362b15af34006e6a11974088a46d42b903418eJohann// RUN_ALL_TESTS() should be invoked after the command line has been
21481b362b15af34006e6a11974088a46d42b903418eJohann// parsed by InitGoogleTest().
21491b362b15af34006e6a11974088a46d42b903418eJohann
21501b362b15af34006e6a11974088a46d42b903418eJohann#define RUN_ALL_TESTS()\
21511b362b15af34006e6a11974088a46d42b903418eJohann  (::testing::UnitTest::GetInstance()->Run())
21521b362b15af34006e6a11974088a46d42b903418eJohann
21531b362b15af34006e6a11974088a46d42b903418eJohann}  // namespace testing
21541b362b15af34006e6a11974088a46d42b903418eJohann
21551b362b15af34006e6a11974088a46d42b903418eJohann#endif  // GTEST_INCLUDE_GTEST_GTEST_H_
2156