gtest.h revision 3d000e7dd14c3185b9e27a6c38a67288b4d10431
13d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// Copyright 2005, Google Inc.
23d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// All rights reserved.
33d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick//
43d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// Redistribution and use in source and binary forms, with or without
53d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// modification, are permitted provided that the following conditions are
63d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// met:
73d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick//
83d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick//     * Redistributions of source code must retain the above copyright
93d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// notice, this list of conditions and the following disclaimer.
103d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick//     * Redistributions in binary form must reproduce the above
113d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// copyright notice, this list of conditions and the following disclaimer
123d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// in the documentation and/or other materials provided with the
133d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// distribution.
143d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick//     * Neither the name of Google Inc. nor the names of its
153d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// contributors may be used to endorse or promote products derived from
163d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// this software without specific prior written permission.
173d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick//
183d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
193d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
203d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
213d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
223d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
233d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
243d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
253d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
263d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
273d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
283d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
293d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick//
303d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// Author: wan@google.com (Zhanyong Wan)
313d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick//
323d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// The Google C++ Testing Framework (Google Test)
333d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick//
343d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// This header file defines the public API for Google Test.  It should be
353d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// included by any test program that uses Google Test.
363d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick//
373d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// IMPORTANT NOTE: Due to limitation of the C++ language, we have to
383d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// leave some internal implementation details in this header file.
393d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// They are clearly marked by comments like this:
403d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick//
413d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick//   // INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM.
423d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick//
433d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// Such code is NOT meant to be used by a user directly, and is subject
443d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// to CHANGE WITHOUT NOTICE.  Therefore DO NOT DEPEND ON IT in a user
453d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// program!
463d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick//
473d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// Acknowledgment: Google Test borrowed the idea of automatic test
483d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// registration from Barthelemy Dagenais' (barthelemy@prologique.com)
493d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// easyUnit framework.
503d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick
513d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick#ifndef GTEST_INCLUDE_GTEST_GTEST_H_
523d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick#define GTEST_INCLUDE_GTEST_GTEST_H_
533d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick
543d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick#include <limits>
553d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick#include <vector>
563d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick
573d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick#include "gtest/internal/gtest-internal.h"
583d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick#include "gtest/internal/gtest-string.h"
593d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick#include "gtest/gtest-death-test.h"
603d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick#include "gtest/gtest-message.h"
613d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick#include "gtest/gtest-param-test.h"
623d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick#include "gtest/gtest-printers.h"
633d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick#include "gtest/gtest_prod.h"
643d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick#include "gtest/gtest-test-part.h"
653d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick#include "gtest/gtest-typed-test.h"
663d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick
673d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// Depending on the platform, different string classes are available.
683d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// On Linux, in addition to ::std::string, Google also makes use of
693d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// class ::string, which has the same interface as ::std::string, but
703d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// has a different implementation.
713d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick//
723d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// The user can define GTEST_HAS_GLOBAL_STRING to 1 to indicate that
733d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// ::string is available AND is a distinct type to ::std::string, or
743d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// define it to 0 to indicate otherwise.
753d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick//
763d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// If the user's ::std::string and ::string are the same class due to
773d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// aliasing, he should define GTEST_HAS_GLOBAL_STRING to 0.
783d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick//
793d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// If the user doesn't define GTEST_HAS_GLOBAL_STRING, it is defined
803d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// heuristically.
813d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick
823d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanicknamespace testing {
833d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick
843d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// Declares the flags.
853d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick
863d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// This flag temporary enables the disabled tests.
873d000e7dd14c3185b9e27a6c38a67288b4d10431Ian RomanickGTEST_DECLARE_bool_(also_run_disabled_tests);
883d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick
893d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// This flag brings the debugger on an assertion failure.
903d000e7dd14c3185b9e27a6c38a67288b4d10431Ian RomanickGTEST_DECLARE_bool_(break_on_failure);
913d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick
923d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// This flag controls whether Google Test catches all test-thrown exceptions
933d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// and logs them as failures.
943d000e7dd14c3185b9e27a6c38a67288b4d10431Ian RomanickGTEST_DECLARE_bool_(catch_exceptions);
953d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick
963d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// This flag enables using colors in terminal output. Available values are
973d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// "yes" to enable colors, "no" (disable colors), or "auto" (the default)
983d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// to let Google Test decide.
993d000e7dd14c3185b9e27a6c38a67288b4d10431Ian RomanickGTEST_DECLARE_string_(color);
1003d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick
1013d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// This flag sets up the filter to select by name using a glob pattern
1023d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// the tests to run. If the filter is not given all tests are executed.
1033d000e7dd14c3185b9e27a6c38a67288b4d10431Ian RomanickGTEST_DECLARE_string_(filter);
1043d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick
1053d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// This flag causes the Google Test to list tests. None of the tests listed
1063d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// are actually run if the flag is provided.
1073d000e7dd14c3185b9e27a6c38a67288b4d10431Ian RomanickGTEST_DECLARE_bool_(list_tests);
1083d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick
1093d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// This flag controls whether Google Test emits a detailed XML report to a file
1103d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// in addition to its normal textual output.
1113d000e7dd14c3185b9e27a6c38a67288b4d10431Ian RomanickGTEST_DECLARE_string_(output);
1123d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick
1133d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// This flags control whether Google Test prints the elapsed time for each
1143d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// test.
1153d000e7dd14c3185b9e27a6c38a67288b4d10431Ian RomanickGTEST_DECLARE_bool_(print_time);
1163d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick
1173d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// This flag specifies the random number seed.
1183d000e7dd14c3185b9e27a6c38a67288b4d10431Ian RomanickGTEST_DECLARE_int32_(random_seed);
1193d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick
1203d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// This flag sets how many times the tests are repeated. The default value
1213d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// is 1. If the value is -1 the tests are repeating forever.
1223d000e7dd14c3185b9e27a6c38a67288b4d10431Ian RomanickGTEST_DECLARE_int32_(repeat);
1233d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick
1243d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// This flag controls whether Google Test includes Google Test internal
1253d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// stack frames in failure stack traces.
1263d000e7dd14c3185b9e27a6c38a67288b4d10431Ian RomanickGTEST_DECLARE_bool_(show_internal_stack_frames);
1273d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick
1283d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// When this flag is specified, tests' order is randomized on every iteration.
1293d000e7dd14c3185b9e27a6c38a67288b4d10431Ian RomanickGTEST_DECLARE_bool_(shuffle);
1303d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick
1313d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// This flag specifies the maximum number of stack frames to be
1323d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// printed in a failure message.
1333d000e7dd14c3185b9e27a6c38a67288b4d10431Ian RomanickGTEST_DECLARE_int32_(stack_trace_depth);
1343d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick
1353d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// When this flag is specified, a failed assertion will throw an
1363d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// exception if exceptions are enabled, or exit the program with a
1373d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// non-zero code otherwise.
1383d000e7dd14c3185b9e27a6c38a67288b4d10431Ian RomanickGTEST_DECLARE_bool_(throw_on_failure);
1393d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick
1403d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// When this flag is set with a "host:port" string, on supported
1413d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// platforms test results are streamed to the specified port on
1423d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// the specified host machine.
1433d000e7dd14c3185b9e27a6c38a67288b4d10431Ian RomanickGTEST_DECLARE_string_(stream_result_to);
1443d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick
1453d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// The upper limit for valid stack trace depths.
1463d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanickconst int kMaxStackTraceDepth = 100;
1473d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick
1483d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanicknamespace internal {
1493d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick
1503d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanickclass AssertHelper;
1513d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanickclass DefaultGlobalTestPartResultReporter;
1523d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanickclass ExecDeathTest;
1533d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanickclass NoExecDeathTest;
1543d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanickclass FinalSuccessChecker;
1553d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanickclass GTestFlagSaver;
1563d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanickclass TestResultAccessor;
1573d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanickclass TestEventListenersAccessor;
1583d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanickclass TestEventRepeater;
1593d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanickclass WindowsDeathTest;
1603d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanickclass UnitTestImpl* GetUnitTestImpl();
1613d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanickvoid ReportFailureInUnknownLocation(TestPartResult::Type result_type,
1623d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick                                    const String& message);
1633d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick
1643d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// Converts a streamable value to a String.  A NULL pointer is
1653d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// converted to "(null)".  When the input value is a ::string,
1663d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// ::std::string, ::wstring, or ::std::wstring object, each NUL
1673d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// character in it is replaced with "\\0".
1683d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// Declared in gtest-internal.h but defined here, so that it has access
1693d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// to the definition of the Message class, required by the ARM
1703d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// compiler.
1713d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanicktemplate <typename T>
1723d000e7dd14c3185b9e27a6c38a67288b4d10431Ian RomanickString StreamableToString(const T& streamable) {
1733d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  return (Message() << streamable).GetString();
1743d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick}
1753d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick
1763d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick}  // namespace internal
1773d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick
1783d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// The friend relationship of some of these classes is cyclic.
1793d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// If we don't forward declare them the compiler might confuse the classes
1803d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// in friendship clauses with same named classes on the scope.
1813d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanickclass Test;
1823d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanickclass TestCase;
1833d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanickclass TestInfo;
1843d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanickclass UnitTest;
1853d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick
1863d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// A class for indicating whether an assertion was successful.  When
1873d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// the assertion wasn't successful, the AssertionResult object
1883d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// remembers a non-empty message that describes how it failed.
1893d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick//
1903d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// To create an instance of this class, use one of the factory functions
1913d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// (AssertionSuccess() and AssertionFailure()).
1923d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick//
1933d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// This class is useful for two purposes:
1943d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick//   1. Defining predicate functions to be used with Boolean test assertions
1953d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick//      EXPECT_TRUE/EXPECT_FALSE and their ASSERT_ counterparts
1963d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick//   2. Defining predicate-format functions to be
1973d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick//      used with predicate assertions (ASSERT_PRED_FORMAT*, etc).
1983d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick//
1993d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// For example, if you define IsEven predicate:
2003d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick//
2013d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick//   testing::AssertionResult IsEven(int n) {
2023d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick//     if ((n % 2) == 0)
2033d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick//       return testing::AssertionSuccess();
2043d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick//     else
2053d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick//       return testing::AssertionFailure() << n << " is odd";
2063d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick//   }
2073d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick//
2083d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// Then the failed expectation EXPECT_TRUE(IsEven(Fib(5)))
2093d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// will print the message
2103d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick//
2113d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick//   Value of: IsEven(Fib(5))
2123d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick//     Actual: false (5 is odd)
2133d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick//   Expected: true
2143d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick//
2153d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// instead of a more opaque
2163d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick//
2173d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick//   Value of: IsEven(Fib(5))
2183d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick//     Actual: false
2193d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick//   Expected: true
2203d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick//
2213d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// in case IsEven is a simple Boolean predicate.
2223d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick//
2233d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// If you expect your predicate to be reused and want to support informative
2243d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// messages in EXPECT_FALSE and ASSERT_FALSE (negative assertions show up
2253d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// about half as often as positive ones in our tests), supply messages for
2263d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// both success and failure cases:
2273d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick//
2283d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick//   testing::AssertionResult IsEven(int n) {
2293d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick//     if ((n % 2) == 0)
2303d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick//       return testing::AssertionSuccess() << n << " is even";
2313d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick//     else
2323d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick//       return testing::AssertionFailure() << n << " is odd";
2333d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick//   }
2343d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick//
2353d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// Then a statement EXPECT_FALSE(IsEven(Fib(6))) will print
2363d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick//
2373d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick//   Value of: IsEven(Fib(6))
2383d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick//     Actual: true (8 is even)
2393d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick//   Expected: false
2403d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick//
2413d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// NB: Predicates that support negative Boolean assertions have reduced
2423d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// performance in positive ones so be careful not to use them in tests
2433d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// that have lots (tens of thousands) of positive Boolean assertions.
2443d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick//
2453d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// To use this class with EXPECT_PRED_FORMAT assertions such as:
2463d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick//
2473d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick//   // Verifies that Foo() returns an even number.
2483d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick//   EXPECT_PRED_FORMAT1(IsEven, Foo());
2493d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick//
2503d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// you need to define:
2513d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick//
2523d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick//   testing::AssertionResult IsEven(const char* expr, int n) {
2533d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick//     if ((n % 2) == 0)
2543d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick//       return testing::AssertionSuccess();
2553d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick//     else
2563d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick//       return testing::AssertionFailure()
2573d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick//         << "Expected: " << expr << " is even\n  Actual: it's " << n;
2583d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick//   }
2593d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick//
2603d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// If Foo() returns 5, you will see the following message:
2613d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick//
2623d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick//   Expected: Foo() is even
2633d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick//     Actual: it's 5
2643d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick//
2653d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanickclass GTEST_API_ AssertionResult {
2663d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick public:
2673d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // Copy constructor.
2683d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // Used in EXPECT_TRUE/FALSE(assertion_result).
2693d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  AssertionResult(const AssertionResult& other);
2703d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // Used in the EXPECT_TRUE/FALSE(bool_expression).
2713d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  explicit AssertionResult(bool success) : success_(success) {}
2723d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick
2733d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // Returns true iff the assertion succeeded.
2743d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  operator bool() const { return success_; }  // NOLINT
2753d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick
2763d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // Returns the assertion's negation. Used with EXPECT/ASSERT_FALSE.
2773d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  AssertionResult operator!() const;
2783d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick
2793d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // Returns the text streamed into this AssertionResult. Test assertions
2803d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // use it when they fail (i.e., the predicate's outcome doesn't match the
2813d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // assertion's expectation). When nothing has been streamed into the
2823d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // object, returns an empty string.
2833d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  const char* message() const {
2843d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick    return message_.get() != NULL ?  message_->c_str() : "";
2853d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  }
2863d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // TODO(vladl@google.com): Remove this after making sure no clients use it.
2873d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // Deprecated; please use message() instead.
2883d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  const char* failure_message() const { return message(); }
2893d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick
2903d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // Streams a custom failure message into this object.
2913d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  template <typename T> AssertionResult& operator<<(const T& value) {
2923d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick    AppendMessage(Message() << value);
2933d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick    return *this;
2943d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  }
2953d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick
2963d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // Allows streaming basic output manipulators such as endl or flush into
2973d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // this object.
2983d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  AssertionResult& operator<<(
2993d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick      ::std::ostream& (*basic_manipulator)(::std::ostream& stream)) {
3003d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick    AppendMessage(Message() << basic_manipulator);
3013d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick    return *this;
3023d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  }
3033d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick
3043d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick private:
3053d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // Appends the contents of message to message_.
3063d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  void AppendMessage(const Message& a_message) {
3073d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick    if (message_.get() == NULL)
3083d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick      message_.reset(new ::std::string);
3093d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick    message_->append(a_message.GetString().c_str());
3103d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  }
3113d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick
3123d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // Stores result of the assertion predicate.
3133d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  bool success_;
3143d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // Stores the message describing the condition in case the expectation
3153d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // construct is not satisfied with the predicate's outcome.
3163d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // Referenced via a pointer to avoid taking too much stack frame space
3173d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // with test assertions.
3183d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  internal::scoped_ptr< ::std::string> message_;
3193d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick
3203d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  GTEST_DISALLOW_ASSIGN_(AssertionResult);
3213d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick};
3223d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick
3233d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// Makes a successful assertion result.
3243d000e7dd14c3185b9e27a6c38a67288b4d10431Ian RomanickGTEST_API_ AssertionResult AssertionSuccess();
3253d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick
3263d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// Makes a failed assertion result.
3273d000e7dd14c3185b9e27a6c38a67288b4d10431Ian RomanickGTEST_API_ AssertionResult AssertionFailure();
3283d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick
3293d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// Makes a failed assertion result with the given failure message.
3303d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// Deprecated; use AssertionFailure() << msg.
3313d000e7dd14c3185b9e27a6c38a67288b4d10431Ian RomanickGTEST_API_ AssertionResult AssertionFailure(const Message& msg);
3323d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick
3333d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// The abstract class that all tests inherit from.
3343d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick//
3353d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// In Google Test, a unit test program contains one or many TestCases, and
3363d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// each TestCase contains one or many Tests.
3373d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick//
3383d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// When you define a test using the TEST macro, you don't need to
3393d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// explicitly derive from Test - the TEST macro automatically does
3403d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// this for you.
3413d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick//
3423d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// The only time you derive from Test is when defining a test fixture
3433d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// to be used a TEST_F.  For example:
3443d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick//
3453d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick//   class FooTest : public testing::Test {
3463d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick//    protected:
3473d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick//     virtual void SetUp() { ... }
3483d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick//     virtual void TearDown() { ... }
3493d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick//     ...
3503d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick//   };
3513d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick//
3523d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick//   TEST_F(FooTest, Bar) { ... }
3533d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick//   TEST_F(FooTest, Baz) { ... }
3543d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick//
3553d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// Test is not copyable.
3563d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanickclass GTEST_API_ Test {
3573d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick public:
3583d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  friend class TestInfo;
3593d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick
3603d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // Defines types for pointers to functions that set up and tear down
3613d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // a test case.
3623d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  typedef internal::SetUpTestCaseFunc SetUpTestCaseFunc;
3633d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  typedef internal::TearDownTestCaseFunc TearDownTestCaseFunc;
3643d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick
3653d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // The d'tor is virtual as we intend to inherit from Test.
3663d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  virtual ~Test();
3673d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick
3683d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // Sets up the stuff shared by all tests in this test case.
3693d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  //
3703d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // Google Test will call Foo::SetUpTestCase() before running the first
3713d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // test in test case Foo.  Hence a sub-class can define its own
3723d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // SetUpTestCase() method to shadow the one defined in the super
3733d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // class.
3743d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  static void SetUpTestCase() {}
3753d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick
3763d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // Tears down the stuff shared by all tests in this test case.
3773d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  //
3783d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // Google Test will call Foo::TearDownTestCase() after running the last
3793d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // test in test case Foo.  Hence a sub-class can define its own
3803d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // TearDownTestCase() method to shadow the one defined in the super
3813d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // class.
3823d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  static void TearDownTestCase() {}
3833d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick
3843d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // Returns true iff the current test has a fatal failure.
3853d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  static bool HasFatalFailure();
3863d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick
3873d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // Returns true iff the current test has a non-fatal failure.
3883d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  static bool HasNonfatalFailure();
3893d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick
3903d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // Returns true iff the current test has a (either fatal or
3913d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // non-fatal) failure.
3923d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  static bool HasFailure() { return HasFatalFailure() || HasNonfatalFailure(); }
3933d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick
3943d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // Logs a property for the current test.  Only the last value for a given
3953d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // key is remembered.
3963d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // These are public static so they can be called from utility functions
3973d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // that are not members of the test fixture.
3983d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // The arguments are const char* instead strings, as Google Test is used
3993d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // on platforms where string doesn't compile.
4003d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  //
4013d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // Note that a driving consideration for these RecordProperty methods
4023d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // was to produce xml output suited to the Greenspan charting utility,
4033d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // which at present will only chart values that fit in a 32-bit int. It
4043d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // is the user's responsibility to restrict their values to 32-bit ints
4053d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // if they intend them to be used with Greenspan.
4063d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  static void RecordProperty(const char* key, const char* value);
4073d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  static void RecordProperty(const char* key, int value);
4083d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick
4093d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick protected:
4103d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // Creates a Test object.
4113d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  Test();
4123d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick
4133d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // Sets up the test fixture.
4143d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  virtual void SetUp();
4153d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick
4163d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // Tears down the test fixture.
4173d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  virtual void TearDown();
4183d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick
4193d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick private:
4203d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // Returns true iff the current test has the same fixture class as
4213d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // the first test in the current test case.
4223d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  static bool HasSameFixtureClass();
4233d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick
4243d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // Runs the test after the test fixture has been set up.
4253d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  //
4263d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // A sub-class must implement this to define the test logic.
4273d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  //
4283d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // DO NOT OVERRIDE THIS FUNCTION DIRECTLY IN A USER PROGRAM.
4293d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // Instead, use the TEST or TEST_F macro.
4303d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  virtual void TestBody() = 0;
4313d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick
4323d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // Sets up, executes, and tears down the test.
4333d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  void Run();
4343d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick
4353d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // Deletes self.  We deliberately pick an unusual name for this
4363d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // internal method to avoid clashing with names used in user TESTs.
4373d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  void DeleteSelf_() { delete this; }
4383d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick
4393d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // Uses a GTestFlagSaver to save and restore all Google Test flags.
4403d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  const internal::GTestFlagSaver* const gtest_flag_saver_;
4413d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick
4423d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // Often a user mis-spells SetUp() as Setup() and spends a long time
4433d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // wondering why it is never called by Google Test.  The declaration of
4443d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // the following method is solely for catching such an error at
4453d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // compile time:
4463d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  //
4473d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  //   - The return type is deliberately chosen to be not void, so it
4483d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  //   will be a conflict if a user declares void Setup() in his test
4493d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  //   fixture.
4503d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  //
4513d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  //   - This method is private, so it will be another compiler error
4523d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  //   if a user calls it from his test fixture.
4533d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  //
4543d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // DO NOT OVERRIDE THIS FUNCTION.
4553d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  //
4563d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // If you see an error about overriding the following function or
4573d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // about it being private, you have mis-spelled SetUp() as Setup().
4583d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  struct Setup_should_be_spelled_SetUp {};
4593d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  virtual Setup_should_be_spelled_SetUp* Setup() { return NULL; }
4603d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick
4613d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // We disallow copying Tests.
4623d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  GTEST_DISALLOW_COPY_AND_ASSIGN_(Test);
4633d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick};
4643d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick
4653d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanicktypedef internal::TimeInMillis TimeInMillis;
4663d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick
4673d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// A copyable object representing a user specified test property which can be
4683d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// output as a key/value string pair.
4693d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick//
4703d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// Don't inherit from TestProperty as its destructor is not virtual.
4713d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanickclass TestProperty {
4723d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick public:
4733d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // C'tor.  TestProperty does NOT have a default constructor.
4743d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // Always use this constructor (with parameters) to create a
4753d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // TestProperty object.
4763d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  TestProperty(const char* a_key, const char* a_value) :
4773d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick    key_(a_key), value_(a_value) {
4783d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  }
4793d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick
4803d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // Gets the user supplied key.
4813d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  const char* key() const {
4823d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick    return key_.c_str();
4833d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  }
4843d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick
4853d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // Gets the user supplied value.
4863d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  const char* value() const {
4873d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick    return value_.c_str();
4883d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  }
4893d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick
4903d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // Sets a new value, overriding the one supplied in the constructor.
4913d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  void SetValue(const char* new_value) {
4923d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick    value_ = new_value;
4933d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  }
4943d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick
4953d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick private:
4963d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // The key supplied by the user.
4973d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  internal::String key_;
4983d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // The value supplied by the user.
4993d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  internal::String value_;
5003d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick};
5013d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick
5023d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// The result of a single Test.  This includes a list of
5033d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// TestPartResults, a list of TestProperties, a count of how many
5043d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// death tests there are in the Test, and how much time it took to run
5053d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// the Test.
5063d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick//
5073d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// TestResult is not copyable.
5083d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanickclass GTEST_API_ TestResult {
5093d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick public:
5103d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // Creates an empty TestResult.
5113d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  TestResult();
5123d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick
5133d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // D'tor.  Do not inherit from TestResult.
5143d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  ~TestResult();
5153d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick
5163d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // Gets the number of all test parts.  This is the sum of the number
5173d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // of successful test parts and the number of failed test parts.
5183d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  int total_part_count() const;
5193d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick
5203d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // Returns the number of the test properties.
5213d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  int test_property_count() const;
5223d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick
5233d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // Returns true iff the test passed (i.e. no test part failed).
5243d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  bool Passed() const { return !Failed(); }
5253d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick
5263d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // Returns true iff the test failed.
5273d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  bool Failed() const;
5283d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick
5293d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // Returns true iff the test fatally failed.
5303d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  bool HasFatalFailure() const;
5313d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick
5323d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // Returns true iff the test has a non-fatal failure.
5333d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  bool HasNonfatalFailure() const;
5343d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick
5353d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // Returns the elapsed time, in milliseconds.
5363d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  TimeInMillis elapsed_time() const { return elapsed_time_; }
5373d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick
5383d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // Returns the i-th test part result among all the results. i can range
5393d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // from 0 to test_property_count() - 1. If i is not in that range, aborts
5403d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // the program.
5413d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  const TestPartResult& GetTestPartResult(int i) const;
5423d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick
5433d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // Returns the i-th test property. i can range from 0 to
5443d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // test_property_count() - 1. If i is not in that range, aborts the
5453d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // program.
5463d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  const TestProperty& GetTestProperty(int i) const;
5473d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick
5483d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick private:
5493d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  friend class TestInfo;
5503d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  friend class UnitTest;
5513d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  friend class internal::DefaultGlobalTestPartResultReporter;
5523d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  friend class internal::ExecDeathTest;
5533d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  friend class internal::TestResultAccessor;
5543d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  friend class internal::UnitTestImpl;
5553d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  friend class internal::WindowsDeathTest;
5563d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick
5573d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // Gets the vector of TestPartResults.
5583d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  const std::vector<TestPartResult>& test_part_results() const {
5593d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick    return test_part_results_;
5603d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  }
5613d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick
5623d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // Gets the vector of TestProperties.
5633d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  const std::vector<TestProperty>& test_properties() const {
5643d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick    return test_properties_;
5653d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  }
5663d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick
5673d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // Sets the elapsed time.
5683d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  void set_elapsed_time(TimeInMillis elapsed) { elapsed_time_ = elapsed; }
5693d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick
5703d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // Adds a test property to the list. The property is validated and may add
5713d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // a non-fatal failure if invalid (e.g., if it conflicts with reserved
5723d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // key names). If a property is already recorded for the same key, the
5733d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // value will be updated, rather than storing multiple values for the same
5743d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // key.
5753d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  void RecordProperty(const TestProperty& test_property);
5763d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick
5773d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // Adds a failure if the key is a reserved attribute of Google Test
5783d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // testcase tags.  Returns true if the property is valid.
5793d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // TODO(russr): Validate attribute names are legal and human readable.
5803d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  static bool ValidateTestProperty(const TestProperty& test_property);
5813d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick
5823d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // Adds a test part result to the list.
5833d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  void AddTestPartResult(const TestPartResult& test_part_result);
5843d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick
5853d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // Returns the death test count.
5863d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  int death_test_count() const { return death_test_count_; }
5873d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick
5883d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // Increments the death test count, returning the new count.
5893d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  int increment_death_test_count() { return ++death_test_count_; }
5903d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick
5913d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // Clears the test part results.
5923d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  void ClearTestPartResults();
5933d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick
5943d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // Clears the object.
5953d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  void Clear();
5963d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick
5973d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // Protects mutable state of the property vector and of owned
5983d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // properties, whose values may be updated.
5993d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  internal::Mutex test_properites_mutex_;
6003d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick
6013d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // The vector of TestPartResults
6023d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  std::vector<TestPartResult> test_part_results_;
6033d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // The vector of TestProperties
6043d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  std::vector<TestProperty> test_properties_;
6053d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // Running count of death tests.
6063d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  int death_test_count_;
6073d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // The elapsed time, in milliseconds.
6083d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  TimeInMillis elapsed_time_;
6093d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick
6103d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // We disallow copying TestResult.
6113d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  GTEST_DISALLOW_COPY_AND_ASSIGN_(TestResult);
6123d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick};  // class TestResult
6133d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick
6143d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// A TestInfo object stores the following information about a test:
6153d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick//
6163d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick//   Test case name
6173d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick//   Test name
6183d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick//   Whether the test should be run
6193d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick//   A function pointer that creates the test object when invoked
6203d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick//   Test result
6213d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick//
6223d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// The constructor of TestInfo registers itself with the UnitTest
6233d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// singleton such that the RUN_ALL_TESTS() macro knows which tests to
6243d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// run.
6253d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanickclass GTEST_API_ TestInfo {
6263d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick public:
6273d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // Destructs a TestInfo object.  This function is not virtual, so
6283d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // don't inherit from TestInfo.
6293d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  ~TestInfo();
6303d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick
6313d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // Returns the test case name.
6323d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  const char* test_case_name() const { return test_case_name_.c_str(); }
6333d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick
6343d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // Returns the test name.
6353d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  const char* name() const { return name_.c_str(); }
6363d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick
6373d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // Returns the name of the parameter type, or NULL if this is not a typed
6383d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // or a type-parameterized test.
6393d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  const char* type_param() const {
6403d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick    if (type_param_.get() != NULL)
6413d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick      return type_param_->c_str();
6423d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick    return NULL;
6433d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  }
6443d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick
6453d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // Returns the text representation of the value parameter, or NULL if this
6463d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // is not a value-parameterized test.
6473d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  const char* value_param() const {
6483d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick    if (value_param_.get() != NULL)
6493d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick      return value_param_->c_str();
6503d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick    return NULL;
6513d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  }
6523d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick
6533d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // Returns true if this test should run, that is if the test is not disabled
6543d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // (or it is disabled but the also_run_disabled_tests flag has been specified)
6553d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // and its full name matches the user-specified filter.
6563d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  //
6573d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // Google Test allows the user to filter the tests by their full names.
6583d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // The full name of a test Bar in test case Foo is defined as
6593d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // "Foo.Bar".  Only the tests that match the filter will run.
6603d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  //
6613d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // A filter is a colon-separated list of glob (not regex) patterns,
6623d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // optionally followed by a '-' and a colon-separated list of
6633d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // negative patterns (tests to exclude).  A test is run if it
6643d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // matches one of the positive patterns and does not match any of
6653d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // the negative patterns.
6663d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  //
6673d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // For example, *A*:Foo.* is a filter that matches any string that
6683d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // contains the character 'A' or starts with "Foo.".
6693d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  bool should_run() const { return should_run_; }
6703d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick
6713d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // Returns the result of the test.
6723d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  const TestResult* result() const { return &result_; }
6733d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick
6743d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick private:
6753d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick
6763d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick#if GTEST_HAS_DEATH_TEST
6773d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  friend class internal::DefaultDeathTestFactory;
6783d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick#endif  // GTEST_HAS_DEATH_TEST
6793d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  friend class Test;
6803d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  friend class TestCase;
6813d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  friend class internal::UnitTestImpl;
6823d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  friend TestInfo* internal::MakeAndRegisterTestInfo(
6833d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick      const char* test_case_name, const char* name,
6843d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick      const char* type_param,
6853d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick      const char* value_param,
6863d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick      internal::TypeId fixture_class_id,
6873d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick      Test::SetUpTestCaseFunc set_up_tc,
6883d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick      Test::TearDownTestCaseFunc tear_down_tc,
6893d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick      internal::TestFactoryBase* factory);
6903d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick
6913d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // Constructs a TestInfo object. The newly constructed instance assumes
6923d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // ownership of the factory object.
6933d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  TestInfo(const char* test_case_name, const char* name,
6943d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick           const char* a_type_param,
6953d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick           const char* a_value_param,
6963d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick           internal::TypeId fixture_class_id,
6973d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick           internal::TestFactoryBase* factory);
6983d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick
6993d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // Increments the number of death tests encountered in this test so
7003d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // far.
7013d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  int increment_death_test_count() {
7023d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick    return result_.increment_death_test_count();
7033d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  }
7043d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick
7053d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // Creates the test object, runs it, records its result, and then
7063d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // deletes it.
7073d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  void Run();
7083d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick
7093d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  static void ClearTestResult(TestInfo* test_info) {
7103d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick    test_info->result_.Clear();
7113d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  }
7123d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick
7133d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // These fields are immutable properties of the test.
7143d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  const std::string test_case_name_;     // Test case name
7153d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  const std::string name_;               // Test name
7163d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // Name of the parameter type, or NULL if this is not a typed or a
7173d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // type-parameterized test.
7183d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  const internal::scoped_ptr<const ::std::string> type_param_;
7193d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // Text representation of the value parameter, or NULL if this is not a
7203d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // value-parameterized test.
7213d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  const internal::scoped_ptr<const ::std::string> value_param_;
7223d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  const internal::TypeId fixture_class_id_;   // ID of the test fixture class
7233d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  bool should_run_;                 // True iff this test should run
7243d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  bool is_disabled_;                // True iff this test is disabled
7253d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  bool matches_filter_;             // True if this test matches the
7263d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick                                    // user-specified filter.
7273d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  internal::TestFactoryBase* const factory_;  // The factory that creates
7283d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick                                              // the test object
7293d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick
7303d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // This field is mutable and needs to be reset before running the
7313d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // test for the second time.
7323d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  TestResult result_;
7333d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick
7343d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  GTEST_DISALLOW_COPY_AND_ASSIGN_(TestInfo);
7353d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick};
7363d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick
7373d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// A test case, which consists of a vector of TestInfos.
7383d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick//
7393d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// TestCase is not copyable.
7403d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanickclass GTEST_API_ TestCase {
7413d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick public:
7423d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // Creates a TestCase with the given name.
7433d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  //
7443d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // TestCase does NOT have a default constructor.  Always use this
7453d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // constructor to create a TestCase object.
7463d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  //
7473d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // Arguments:
7483d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  //
7493d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  //   name:         name of the test case
7503d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  //   a_type_param: the name of the test's type parameter, or NULL if
7513d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  //                 this is not a type-parameterized test.
7523d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  //   set_up_tc:    pointer to the function that sets up the test case
7533d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  //   tear_down_tc: pointer to the function that tears down the test case
7543d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  TestCase(const char* name, const char* a_type_param,
7553d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick           Test::SetUpTestCaseFunc set_up_tc,
7563d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick           Test::TearDownTestCaseFunc tear_down_tc);
7573d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick
7583d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // Destructor of TestCase.
7593d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  virtual ~TestCase();
7603d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick
7613d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // Gets the name of the TestCase.
7623d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  const char* name() const { return name_.c_str(); }
7633d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick
7643d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // Returns the name of the parameter type, or NULL if this is not a
7653d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // type-parameterized test case.
7663d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  const char* type_param() const {
7673d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick    if (type_param_.get() != NULL)
7683d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick      return type_param_->c_str();
7693d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick    return NULL;
7703d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  }
7713d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick
7723d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // Returns true if any test in this test case should run.
7733d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  bool should_run() const { return should_run_; }
7743d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick
7753d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // Gets the number of successful tests in this test case.
7763d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  int successful_test_count() const;
7773d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick
7783d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // Gets the number of failed tests in this test case.
7793d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  int failed_test_count() const;
7803d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick
7813d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // Gets the number of disabled tests in this test case.
7823d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  int disabled_test_count() const;
7833d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick
7843d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // Get the number of tests in this test case that should run.
7853d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  int test_to_run_count() const;
7863d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick
7873d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // Gets the number of all tests in this test case.
7883d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  int total_test_count() const;
7893d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick
7903d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // Returns true iff the test case passed.
7913d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  bool Passed() const { return !Failed(); }
7923d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick
7933d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // Returns true iff the test case failed.
7943d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  bool Failed() const { return failed_test_count() > 0; }
7953d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick
7963d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // Returns the elapsed time, in milliseconds.
7973d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  TimeInMillis elapsed_time() const { return elapsed_time_; }
7983d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick
7993d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // Returns the i-th test among all the tests. i can range from 0 to
8003d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // total_test_count() - 1. If i is not in that range, returns NULL.
8013d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  const TestInfo* GetTestInfo(int i) const;
8023d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick
8033d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick private:
8043d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  friend class Test;
8053d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  friend class internal::UnitTestImpl;
8063d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick
8073d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // Gets the (mutable) vector of TestInfos in this TestCase.
8083d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  std::vector<TestInfo*>& test_info_list() { return test_info_list_; }
8093d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick
8103d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // Gets the (immutable) vector of TestInfos in this TestCase.
8113d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  const std::vector<TestInfo*>& test_info_list() const {
8123d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick    return test_info_list_;
8133d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  }
8143d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick
8153d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // Returns the i-th test among all the tests. i can range from 0 to
8163d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // total_test_count() - 1. If i is not in that range, returns NULL.
8173d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  TestInfo* GetMutableTestInfo(int i);
8183d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick
8193d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // Sets the should_run member.
8203d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  void set_should_run(bool should) { should_run_ = should; }
8213d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick
8223d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // Adds a TestInfo to this test case.  Will delete the TestInfo upon
8233d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // destruction of the TestCase object.
8243d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  void AddTestInfo(TestInfo * test_info);
8253d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick
8263d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // Clears the results of all tests in this test case.
8273d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  void ClearResult();
8283d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick
8293d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // Clears the results of all tests in the given test case.
8303d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  static void ClearTestCaseResult(TestCase* test_case) {
8313d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick    test_case->ClearResult();
8323d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  }
8333d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick
8343d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // Runs every test in this TestCase.
8353d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  void Run();
8363d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick
8373d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // Runs SetUpTestCase() for this TestCase.  This wrapper is needed
8383d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // for catching exceptions thrown from SetUpTestCase().
8393d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  void RunSetUpTestCase() { (*set_up_tc_)(); }
8403d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick
8413d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // Runs TearDownTestCase() for this TestCase.  This wrapper is
8423d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // needed for catching exceptions thrown from TearDownTestCase().
8433d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  void RunTearDownTestCase() { (*tear_down_tc_)(); }
8443d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick
8453d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // Returns true iff test passed.
8463d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  static bool TestPassed(const TestInfo* test_info) {
8473d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick    return test_info->should_run() && test_info->result()->Passed();
8483d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  }
8493d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick
8503d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // Returns true iff test failed.
8513d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  static bool TestFailed(const TestInfo* test_info) {
8523d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick    return test_info->should_run() && test_info->result()->Failed();
8533d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  }
8543d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick
8553d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // Returns true iff test is disabled.
8563d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  static bool TestDisabled(const TestInfo* test_info) {
8573d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick    return test_info->is_disabled_;
8583d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  }
8593d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick
8603d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // Returns true if the given test should run.
8613d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  static bool ShouldRunTest(const TestInfo* test_info) {
8623d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick    return test_info->should_run();
8633d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  }
8643d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick
8653d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // Shuffles the tests in this test case.
8663d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  void ShuffleTests(internal::Random* random);
8673d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick
8683d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // Restores the test order to before the first shuffle.
8693d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  void UnshuffleTests();
8703d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick
8713d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // Name of the test case.
8723d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  internal::String name_;
8733d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // Name of the parameter type, or NULL if this is not a typed or a
8743d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // type-parameterized test.
8753d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  const internal::scoped_ptr<const ::std::string> type_param_;
8763d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // The vector of TestInfos in their original order.  It owns the
8773d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // elements in the vector.
8783d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  std::vector<TestInfo*> test_info_list_;
8793d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // Provides a level of indirection for the test list to allow easy
8803d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // shuffling and restoring the test order.  The i-th element in this
8813d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // vector is the index of the i-th test in the shuffled test list.
8823d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  std::vector<int> test_indices_;
8833d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // Pointer to the function that sets up the test case.
8843d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  Test::SetUpTestCaseFunc set_up_tc_;
8853d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // Pointer to the function that tears down the test case.
8863d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  Test::TearDownTestCaseFunc tear_down_tc_;
8873d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // True iff any test in this test case should run.
8883d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  bool should_run_;
8893d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // Elapsed time, in milliseconds.
8903d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  TimeInMillis elapsed_time_;
8913d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick
8923d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // We disallow copying TestCases.
8933d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  GTEST_DISALLOW_COPY_AND_ASSIGN_(TestCase);
8943d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick};
8953d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick
8963d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// An Environment object is capable of setting up and tearing down an
8973d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// environment.  The user should subclass this to define his own
8983d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// environment(s).
8993d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick//
9003d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// An Environment object does the set-up and tear-down in virtual
9013d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// methods SetUp() and TearDown() instead of the constructor and the
9023d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// destructor, as:
9033d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick//
9043d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick//   1. You cannot safely throw from a destructor.  This is a problem
9053d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick//      as in some cases Google Test is used where exceptions are enabled, and
9063d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick//      we may want to implement ASSERT_* using exceptions where they are
9073d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick//      available.
9083d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick//   2. You cannot use ASSERT_* directly in a constructor or
9093d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick//      destructor.
9103d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanickclass Environment {
9113d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick public:
9123d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // The d'tor is virtual as we need to subclass Environment.
9133d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  virtual ~Environment() {}
9143d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick
9153d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // Override this to define how to set up the environment.
9163d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  virtual void SetUp() {}
9173d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick
9183d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // Override this to define how to tear down the environment.
9193d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  virtual void TearDown() {}
9203d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick private:
9213d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // If you see an error about overriding the following function or
9223d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // about it being private, you have mis-spelled SetUp() as Setup().
9233d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  struct Setup_should_be_spelled_SetUp {};
9243d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  virtual Setup_should_be_spelled_SetUp* Setup() { return NULL; }
9253d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick};
9263d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick
9273d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// The interface for tracing execution of tests. The methods are organized in
9283d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// the order the corresponding events are fired.
9293d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanickclass TestEventListener {
9303d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick public:
9313d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  virtual ~TestEventListener() {}
9323d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick
9333d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // Fired before any test activity starts.
9343d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  virtual void OnTestProgramStart(const UnitTest& unit_test) = 0;
9353d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick
9363d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // Fired before each iteration of tests starts.  There may be more than
9373d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // one iteration if GTEST_FLAG(repeat) is set. iteration is the iteration
9383d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // index, starting from 0.
9393d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  virtual void OnTestIterationStart(const UnitTest& unit_test,
9403d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick                                    int iteration) = 0;
9413d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick
9423d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // Fired before environment set-up for each iteration of tests starts.
9433d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  virtual void OnEnvironmentsSetUpStart(const UnitTest& unit_test) = 0;
9443d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick
9453d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // Fired after environment set-up for each iteration of tests ends.
9463d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  virtual void OnEnvironmentsSetUpEnd(const UnitTest& unit_test) = 0;
9473d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick
9483d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // Fired before the test case starts.
9493d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  virtual void OnTestCaseStart(const TestCase& test_case) = 0;
9503d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick
9513d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // Fired before the test starts.
9523d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  virtual void OnTestStart(const TestInfo& test_info) = 0;
9533d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick
9543d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // Fired after a failed assertion or a SUCCEED() invocation.
9553d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  virtual void OnTestPartResult(const TestPartResult& test_part_result) = 0;
9563d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick
9573d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // Fired after the test ends.
9583d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  virtual void OnTestEnd(const TestInfo& test_info) = 0;
9593d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick
9603d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // Fired after the test case ends.
9613d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  virtual void OnTestCaseEnd(const TestCase& test_case) = 0;
9623d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick
9633d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // Fired before environment tear-down for each iteration of tests starts.
9643d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  virtual void OnEnvironmentsTearDownStart(const UnitTest& unit_test) = 0;
9653d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick
9663d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // Fired after environment tear-down for each iteration of tests ends.
9673d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  virtual void OnEnvironmentsTearDownEnd(const UnitTest& unit_test) = 0;
9683d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick
9693d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // Fired after each iteration of tests finishes.
9703d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  virtual void OnTestIterationEnd(const UnitTest& unit_test,
9713d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick                                  int iteration) = 0;
9723d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick
9733d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // Fired after all test activities have ended.
9743d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  virtual void OnTestProgramEnd(const UnitTest& unit_test) = 0;
9753d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick};
9763d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick
9773d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// The convenience class for users who need to override just one or two
9783d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// methods and are not concerned that a possible change to a signature of
9793d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// the methods they override will not be caught during the build.  For
9803d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// comments about each method please see the definition of TestEventListener
9813d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// above.
9823d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanickclass EmptyTestEventListener : public TestEventListener {
9833d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick public:
9843d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  virtual void OnTestProgramStart(const UnitTest& /*unit_test*/) {}
9853d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  virtual void OnTestIterationStart(const UnitTest& /*unit_test*/,
9863d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick                                    int /*iteration*/) {}
9873d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  virtual void OnEnvironmentsSetUpStart(const UnitTest& /*unit_test*/) {}
9883d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  virtual void OnEnvironmentsSetUpEnd(const UnitTest& /*unit_test*/) {}
9893d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  virtual void OnTestCaseStart(const TestCase& /*test_case*/) {}
9903d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  virtual void OnTestStart(const TestInfo& /*test_info*/) {}
9913d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  virtual void OnTestPartResult(const TestPartResult& /*test_part_result*/) {}
9923d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  virtual void OnTestEnd(const TestInfo& /*test_info*/) {}
9933d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  virtual void OnTestCaseEnd(const TestCase& /*test_case*/) {}
9943d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  virtual void OnEnvironmentsTearDownStart(const UnitTest& /*unit_test*/) {}
9953d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  virtual void OnEnvironmentsTearDownEnd(const UnitTest& /*unit_test*/) {}
9963d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  virtual void OnTestIterationEnd(const UnitTest& /*unit_test*/,
9973d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick                                  int /*iteration*/) {}
9983d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  virtual void OnTestProgramEnd(const UnitTest& /*unit_test*/) {}
9993d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick};
10003d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick
10013d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// TestEventListeners lets users add listeners to track events in Google Test.
10023d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanickclass GTEST_API_ TestEventListeners {
10033d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick public:
10043d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  TestEventListeners();
10053d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  ~TestEventListeners();
10063d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick
10073d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // Appends an event listener to the end of the list. Google Test assumes
10083d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // the ownership of the listener (i.e. it will delete the listener when
10093d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // the test program finishes).
10103d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  void Append(TestEventListener* listener);
10113d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick
10123d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // Removes the given event listener from the list and returns it.  It then
10133d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // becomes the caller's responsibility to delete the listener. Returns
10143d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // NULL if the listener is not found in the list.
10153d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  TestEventListener* Release(TestEventListener* listener);
10163d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick
10173d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // Returns the standard listener responsible for the default console
10183d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // output.  Can be removed from the listeners list to shut down default
10193d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // console output.  Note that removing this object from the listener list
10203d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // with Release transfers its ownership to the caller and makes this
10213d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // function return NULL the next time.
10223d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  TestEventListener* default_result_printer() const {
10233d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick    return default_result_printer_;
10243d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  }
10253d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick
10263d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // Returns the standard listener responsible for the default XML output
10273d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // controlled by the --gtest_output=xml flag.  Can be removed from the
10283d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // listeners list by users who want to shut down the default XML output
10293d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // controlled by this flag and substitute it with custom one.  Note that
10303d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // removing this object from the listener list with Release transfers its
10313d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // ownership to the caller and makes this function return NULL the next
10323d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // time.
10333d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  TestEventListener* default_xml_generator() const {
10343d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick    return default_xml_generator_;
10353d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  }
10363d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick
10373d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick private:
10383d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  friend class TestCase;
10393d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  friend class TestInfo;
10403d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  friend class internal::DefaultGlobalTestPartResultReporter;
10413d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  friend class internal::NoExecDeathTest;
10423d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  friend class internal::TestEventListenersAccessor;
10433d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  friend class internal::UnitTestImpl;
10443d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick
10453d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // Returns repeater that broadcasts the TestEventListener events to all
10463d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // subscribers.
10473d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  TestEventListener* repeater();
10483d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick
10493d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // Sets the default_result_printer attribute to the provided listener.
10503d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // The listener is also added to the listener list and previous
10513d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // default_result_printer is removed from it and deleted. The listener can
10523d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // also be NULL in which case it will not be added to the list. Does
10533d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // nothing if the previous and the current listener objects are the same.
10543d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  void SetDefaultResultPrinter(TestEventListener* listener);
10553d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick
10563d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // Sets the default_xml_generator attribute to the provided listener.  The
10573d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // listener is also added to the listener list and previous
10583d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // default_xml_generator is removed from it and deleted. The listener can
10593d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // also be NULL in which case it will not be added to the list. Does
10603d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // nothing if the previous and the current listener objects are the same.
10613d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  void SetDefaultXmlGenerator(TestEventListener* listener);
10623d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick
10633d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // Controls whether events will be forwarded by the repeater to the
10643d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // listeners in the list.
10653d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  bool EventForwardingEnabled() const;
10663d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  void SuppressEventForwarding();
10673d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick
10683d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // The actual list of listeners.
10693d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  internal::TestEventRepeater* repeater_;
10703d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // Listener responsible for the standard result output.
10713d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  TestEventListener* default_result_printer_;
10723d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // Listener responsible for the creation of the XML output file.
10733d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  TestEventListener* default_xml_generator_;
10743d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick
10753d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // We disallow copying TestEventListeners.
10763d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  GTEST_DISALLOW_COPY_AND_ASSIGN_(TestEventListeners);
10773d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick};
10783d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick
10793d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// A UnitTest consists of a vector of TestCases.
10803d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick//
10813d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// This is a singleton class.  The only instance of UnitTest is
10823d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// created when UnitTest::GetInstance() is first called.  This
10833d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// instance is never deleted.
10843d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick//
10853d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// UnitTest is not copyable.
10863d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick//
10873d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// This class is thread-safe as long as the methods are called
10883d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// according to their specification.
10893d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanickclass GTEST_API_ UnitTest {
10903d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick public:
10913d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // Gets the singleton UnitTest object.  The first time this method
10923d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // is called, a UnitTest object is constructed and returned.
10933d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // Consecutive calls will return the same object.
10943d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  static UnitTest* GetInstance();
10953d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick
10963d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // Runs all tests in this UnitTest object and prints the result.
10973d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // Returns 0 if successful, or 1 otherwise.
10983d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  //
10993d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // This method can only be called from the main thread.
11003d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  //
11013d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM.
11023d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  int Run() GTEST_MUST_USE_RESULT_;
11033d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick
11043d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // Returns the working directory when the first TEST() or TEST_F()
11053d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // was executed.  The UnitTest object owns the string.
11063d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  const char* original_working_dir() const;
11073d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick
11083d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // Returns the TestCase object for the test that's currently running,
11093d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // or NULL if no test is running.
11103d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  const TestCase* current_test_case() const;
11113d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick
11123d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // Returns the TestInfo object for the test that's currently running,
11133d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // or NULL if no test is running.
11143d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  const TestInfo* current_test_info() const;
11153d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick
11163d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // Returns the random seed used at the start of the current test run.
11173d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  int random_seed() const;
11183d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick
11193d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick#if GTEST_HAS_PARAM_TEST
11203d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // Returns the ParameterizedTestCaseRegistry object used to keep track of
11213d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // value-parameterized tests and instantiate and register them.
11223d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  //
11233d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM.
11243d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  internal::ParameterizedTestCaseRegistry& parameterized_test_registry();
11253d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick#endif  // GTEST_HAS_PARAM_TEST
11263d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick
11273d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // Gets the number of successful test cases.
11283d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  int successful_test_case_count() const;
11293d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick
11303d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // Gets the number of failed test cases.
11313d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  int failed_test_case_count() const;
11323d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick
11333d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // Gets the number of all test cases.
11343d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  int total_test_case_count() const;
11353d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick
11363d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // Gets the number of all test cases that contain at least one test
11373d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // that should run.
11383d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  int test_case_to_run_count() const;
11393d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick
11403d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // Gets the number of successful tests.
11413d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  int successful_test_count() const;
11423d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick
11433d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // Gets the number of failed tests.
11443d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  int failed_test_count() const;
11453d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick
11463d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // Gets the number of disabled tests.
11473d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  int disabled_test_count() const;
11483d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick
11493d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // Gets the number of all tests.
11503d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  int total_test_count() const;
11513d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick
11523d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // Gets the number of tests that should run.
11533d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  int test_to_run_count() const;
11543d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick
11553d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // Gets the elapsed time, in milliseconds.
11563d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  TimeInMillis elapsed_time() const;
11573d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick
11583d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // Returns true iff the unit test passed (i.e. all test cases passed).
11593d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  bool Passed() const;
11603d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick
11613d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // Returns true iff the unit test failed (i.e. some test case failed
11623d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // or something outside of all tests failed).
11633d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  bool Failed() const;
11643d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick
11653d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // Gets the i-th test case among all the test cases. i can range from 0 to
11663d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // total_test_case_count() - 1. If i is not in that range, returns NULL.
11673d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  const TestCase* GetTestCase(int i) const;
11683d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick
11693d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // Returns the list of event listeners that can be used to track events
11703d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // inside Google Test.
11713d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  TestEventListeners& listeners();
11723d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick
11733d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick private:
11743d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // Registers and returns a global test environment.  When a test
11753d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // program is run, all global test environments will be set-up in
11763d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // the order they were registered.  After all tests in the program
11773d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // have finished, all global test environments will be torn-down in
11783d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // the *reverse* order they were registered.
11793d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  //
11803d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // The UnitTest object takes ownership of the given environment.
11813d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  //
11823d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // This method can only be called from the main thread.
11833d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  Environment* AddEnvironment(Environment* env);
11843d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick
11853d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // Adds a TestPartResult to the current TestResult object.  All
11863d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // Google Test assertion macros (e.g. ASSERT_TRUE, EXPECT_EQ, etc)
11873d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // eventually call this to report their results.  The user code
11883d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // should use the assertion macros instead of calling this directly.
11893d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  void AddTestPartResult(TestPartResult::Type result_type,
11903d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick                         const char* file_name,
11913d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick                         int line_number,
11923d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick                         const internal::String& message,
11933d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick                         const internal::String& os_stack_trace);
11943d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick
11953d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // Adds a TestProperty to the current TestResult object. If the result already
11963d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // contains a property with the same key, the value will be updated.
11973d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  void RecordPropertyForCurrentTest(const char* key, const char* value);
11983d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick
11993d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // Gets the i-th test case among all the test cases. i can range from 0 to
12003d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // total_test_case_count() - 1. If i is not in that range, returns NULL.
12013d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  TestCase* GetMutableTestCase(int i);
12023d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick
12033d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // Accessors for the implementation object.
12043d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  internal::UnitTestImpl* impl() { return impl_; }
12053d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  const internal::UnitTestImpl* impl() const { return impl_; }
12063d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick
12073d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // These classes and funcions are friends as they need to access private
12083d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // members of UnitTest.
12093d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  friend class Test;
12103d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  friend class internal::AssertHelper;
12113d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  friend class internal::ScopedTrace;
12123d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  friend Environment* AddGlobalTestEnvironment(Environment* env);
12133d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  friend internal::UnitTestImpl* internal::GetUnitTestImpl();
12143d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  friend void internal::ReportFailureInUnknownLocation(
12153d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick      TestPartResult::Type result_type,
12163d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick      const internal::String& message);
12173d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick
12183d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // Creates an empty UnitTest.
12193d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  UnitTest();
12203d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick
12213d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // D'tor
12223d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  virtual ~UnitTest();
12233d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick
12243d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // Pushes a trace defined by SCOPED_TRACE() on to the per-thread
12253d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // Google Test trace stack.
12263d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  void PushGTestTrace(const internal::TraceInfo& trace);
12273d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick
12283d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // Pops a trace from the per-thread Google Test trace stack.
12293d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  void PopGTestTrace();
12303d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick
12313d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // Protects mutable state in *impl_.  This is mutable as some const
12323d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // methods need to lock it too.
12333d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  mutable internal::Mutex mutex_;
12343d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick
12353d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // Opaque implementation object.  This field is never changed once
12363d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // the object is constructed.  We don't mark it as const here, as
12373d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // doing so will cause a warning in the constructor of UnitTest.
12383d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // Mutable state in *impl_ is protected by mutex_.
12393d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  internal::UnitTestImpl* impl_;
12403d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick
12413d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // We disallow copying UnitTest.
12423d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  GTEST_DISALLOW_COPY_AND_ASSIGN_(UnitTest);
12433d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick};
12443d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick
12453d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// A convenient wrapper for adding an environment for the test
12463d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// program.
12473d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick//
12483d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// You should call this before RUN_ALL_TESTS() is called, probably in
12493d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// main().  If you use gtest_main, you need to call this before main()
12503d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// starts for it to take effect.  For example, you can define a global
12513d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// variable like this:
12523d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick//
12533d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick//   testing::Environment* const foo_env =
12543d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick//       testing::AddGlobalTestEnvironment(new FooEnvironment);
12553d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick//
12563d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// However, we strongly recommend you to write your own main() and
12573d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// call AddGlobalTestEnvironment() there, as relying on initialization
12583d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// of global variables makes the code harder to read and may cause
12593d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// problems when you register multiple environments from different
12603d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// translation units and the environments have dependencies among them
12613d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// (remember that the compiler doesn't guarantee the order in which
12623d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// global variables from different translation units are initialized).
12633d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanickinline Environment* AddGlobalTestEnvironment(Environment* env) {
12643d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  return UnitTest::GetInstance()->AddEnvironment(env);
12653d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick}
12663d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick
12673d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// Initializes Google Test.  This must be called before calling
12683d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// RUN_ALL_TESTS().  In particular, it parses a command line for the
12693d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// flags that Google Test recognizes.  Whenever a Google Test flag is
12703d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// seen, it is removed from argv, and *argc is decremented.
12713d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick//
12723d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// No value is returned.  Instead, the Google Test flag variables are
12733d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// updated.
12743d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick//
12753d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// Calling the function for the second time has no user-visible effect.
12763d000e7dd14c3185b9e27a6c38a67288b4d10431Ian RomanickGTEST_API_ void InitGoogleTest(int* argc, char** argv);
12773d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick
12783d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// This overloaded version can be used in Windows programs compiled in
12793d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// UNICODE mode.
12803d000e7dd14c3185b9e27a6c38a67288b4d10431Ian RomanickGTEST_API_ void InitGoogleTest(int* argc, wchar_t** argv);
12813d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick
12823d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanicknamespace internal {
12833d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick
12843d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// Formats a comparison assertion (e.g. ASSERT_EQ, EXPECT_LT, and etc)
12853d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// operand to be used in a failure message.  The type (but not value)
12863d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// of the other operand may affect the format.  This allows us to
12873d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// print a char* as a raw pointer when it is compared against another
12883d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// char*, and print it as a C string when it is compared against an
12893d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// std::string object, for example.
12903d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick//
12913d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// The default implementation ignores the type of the other operand.
12923d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// Some specialized versions are used to handle formatting wide or
12933d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// narrow C strings.
12943d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick//
12953d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM.
12963d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanicktemplate <typename T1, typename T2>
12973d000e7dd14c3185b9e27a6c38a67288b4d10431Ian RomanickString FormatForComparisonFailureMessage(const T1& value,
12983d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick                                         const T2& /* other_operand */) {
12993d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // C++Builder compiles this incorrectly if the namespace isn't explicitly
13003d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // given.
13013d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  return ::testing::PrintToString(value);
13023d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick}
13033d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick
13043d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// The helper function for {ASSERT|EXPECT}_EQ.
13053d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanicktemplate <typename T1, typename T2>
13063d000e7dd14c3185b9e27a6c38a67288b4d10431Ian RomanickAssertionResult CmpHelperEQ(const char* expected_expression,
13073d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick                            const char* actual_expression,
13083d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick                            const T1& expected,
13093d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick                            const T2& actual) {
13103d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick#ifdef _MSC_VER
13113d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick# pragma warning(push)          // Saves the current warning state.
13123d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick# pragma warning(disable:4389)  // Temporarily disables warning on
13133d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick                               // signed/unsigned mismatch.
13143d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick#endif
13153d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick
13163d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  if (expected == actual) {
13173d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick    return AssertionSuccess();
13183d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  }
13193d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick
13203d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick#ifdef _MSC_VER
13213d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick# pragma warning(pop)          // Restores the warning state.
13223d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick#endif
13233d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick
13243d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  return EqFailure(expected_expression,
13253d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick                   actual_expression,
13263d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick                   FormatForComparisonFailureMessage(expected, actual),
13273d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick                   FormatForComparisonFailureMessage(actual, expected),
13283d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick                   false);
13293d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick}
13303d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick
13313d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// With this overloaded version, we allow anonymous enums to be used
13323d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// in {ASSERT|EXPECT}_EQ when compiled with gcc 4, as anonymous enums
13333d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// can be implicitly cast to BiggestInt.
13343d000e7dd14c3185b9e27a6c38a67288b4d10431Ian RomanickGTEST_API_ AssertionResult CmpHelperEQ(const char* expected_expression,
13353d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick                                       const char* actual_expression,
13363d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick                                       BiggestInt expected,
13373d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick                                       BiggestInt actual);
13383d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick
13393d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// The helper class for {ASSERT|EXPECT}_EQ.  The template argument
13403d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// lhs_is_null_literal is true iff the first argument to ASSERT_EQ()
13413d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// is a null pointer literal.  The following default implementation is
13423d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// for lhs_is_null_literal being false.
13433d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanicktemplate <bool lhs_is_null_literal>
13443d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanickclass EqHelper {
13453d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick public:
13463d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // This templatized version is for the general case.
13473d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  template <typename T1, typename T2>
13483d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  static AssertionResult Compare(const char* expected_expression,
13493d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick                                 const char* actual_expression,
13503d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick                                 const T1& expected,
13513d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick                                 const T2& actual) {
13523d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick    return CmpHelperEQ(expected_expression, actual_expression, expected,
13533d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick                       actual);
13543d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  }
13553d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick
13563d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // With this overloaded version, we allow anonymous enums to be used
13573d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // in {ASSERT|EXPECT}_EQ when compiled with gcc 4, as anonymous
13583d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // enums can be implicitly cast to BiggestInt.
13593d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  //
13603d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // Even though its body looks the same as the above version, we
13613d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // cannot merge the two, as it will make anonymous enums unhappy.
13623d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  static AssertionResult Compare(const char* expected_expression,
13633d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick                                 const char* actual_expression,
13643d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick                                 BiggestInt expected,
13653d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick                                 BiggestInt actual) {
13663d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick    return CmpHelperEQ(expected_expression, actual_expression, expected,
13673d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick                       actual);
13683d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  }
13693d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick};
13703d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick
13713d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// This specialization is used when the first argument to ASSERT_EQ()
13723d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// is a null pointer literal, like NULL, false, or 0.
13733d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanicktemplate <>
13743d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanickclass EqHelper<true> {
13753d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick public:
13763d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // We define two overloaded versions of Compare().  The first
13773d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // version will be picked when the second argument to ASSERT_EQ() is
13783d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // NOT a pointer, e.g. ASSERT_EQ(0, AnIntFunction()) or
13793d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // EXPECT_EQ(false, a_bool).
13803d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  template <typename T1, typename T2>
13813d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  static AssertionResult Compare(
13823d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick      const char* expected_expression,
13833d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick      const char* actual_expression,
13843d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick      const T1& expected,
13853d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick      const T2& actual,
13863d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick      // The following line prevents this overload from being considered if T2
13873d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick      // is not a pointer type.  We need this because ASSERT_EQ(NULL, my_ptr)
13883d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick      // expands to Compare("", "", NULL, my_ptr), which requires a conversion
13893d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick      // to match the Secret* in the other overload, which would otherwise make
13903d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick      // this template match better.
13913d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick      typename EnableIf<!is_pointer<T2>::value>::type* = 0) {
13923d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick    return CmpHelperEQ(expected_expression, actual_expression, expected,
13933d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick                       actual);
13943d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  }
13953d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick
13963d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // This version will be picked when the second argument to ASSERT_EQ() is a
13973d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // pointer, e.g. ASSERT_EQ(NULL, a_pointer).
13983d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  template <typename T>
13993d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  static AssertionResult Compare(
14003d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick      const char* expected_expression,
14013d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick      const char* actual_expression,
14023d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick      // We used to have a second template parameter instead of Secret*.  That
14033d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick      // template parameter would deduce to 'long', making this a better match
14043d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick      // than the first overload even without the first overload's EnableIf.
14053d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick      // Unfortunately, gcc with -Wconversion-null warns when "passing NULL to
14063d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick      // non-pointer argument" (even a deduced integral argument), so the old
14073d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick      // implementation caused warnings in user code.
14083d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick      Secret* /* expected (NULL) */,
14093d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick      T* actual) {
14103d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick    // We already know that 'expected' is a null pointer.
14113d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick    return CmpHelperEQ(expected_expression, actual_expression,
14123d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick                       static_cast<T*>(NULL), actual);
14133d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  }
14143d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick};
14153d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick
14163d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// A macro for implementing the helper functions needed to implement
14173d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// ASSERT_?? and EXPECT_??.  It is here just to avoid copy-and-paste
14183d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// of similar code.
14193d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick//
14203d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// For each templatized helper function, we also define an overloaded
14213d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// version for BiggestInt in order to reduce code bloat and allow
14223d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// anonymous enums to be used with {ASSERT|EXPECT}_?? when compiled
14233d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// with gcc 4.
14243d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick//
14253d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM.
14263d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick#define GTEST_IMPL_CMP_HELPER_(op_name, op)\
14273d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanicktemplate <typename T1, typename T2>\
14283d000e7dd14c3185b9e27a6c38a67288b4d10431Ian RomanickAssertionResult CmpHelper##op_name(const char* expr1, const char* expr2, \
14293d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick                                   const T1& val1, const T2& val2) {\
14303d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  if (val1 op val2) {\
14313d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick    return AssertionSuccess();\
14323d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  } else {\
14333d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick    return AssertionFailure() \
14343d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick        << "Expected: (" << expr1 << ") " #op " (" << expr2\
14353d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick        << "), actual: " << FormatForComparisonFailureMessage(val1, val2)\
14363d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick        << " vs " << FormatForComparisonFailureMessage(val2, val1);\
14373d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  }\
14383d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick}\
14393d000e7dd14c3185b9e27a6c38a67288b4d10431Ian RomanickGTEST_API_ AssertionResult CmpHelper##op_name(\
14403d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick    const char* expr1, const char* expr2, BiggestInt val1, BiggestInt val2)
14413d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick
14423d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM.
14433d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick
14443d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// Implements the helper function for {ASSERT|EXPECT}_NE
14453d000e7dd14c3185b9e27a6c38a67288b4d10431Ian RomanickGTEST_IMPL_CMP_HELPER_(NE, !=);
14463d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// Implements the helper function for {ASSERT|EXPECT}_LE
14473d000e7dd14c3185b9e27a6c38a67288b4d10431Ian RomanickGTEST_IMPL_CMP_HELPER_(LE, <=);
14483d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// Implements the helper function for {ASSERT|EXPECT}_LT
14493d000e7dd14c3185b9e27a6c38a67288b4d10431Ian RomanickGTEST_IMPL_CMP_HELPER_(LT, < );
14503d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// Implements the helper function for {ASSERT|EXPECT}_GE
14513d000e7dd14c3185b9e27a6c38a67288b4d10431Ian RomanickGTEST_IMPL_CMP_HELPER_(GE, >=);
14523d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// Implements the helper function for {ASSERT|EXPECT}_GT
14533d000e7dd14c3185b9e27a6c38a67288b4d10431Ian RomanickGTEST_IMPL_CMP_HELPER_(GT, > );
14543d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick
14553d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick#undef GTEST_IMPL_CMP_HELPER_
14563d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick
14573d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// The helper function for {ASSERT|EXPECT}_STREQ.
14583d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick//
14593d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM.
14603d000e7dd14c3185b9e27a6c38a67288b4d10431Ian RomanickGTEST_API_ AssertionResult CmpHelperSTREQ(const char* expected_expression,
14613d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick                                          const char* actual_expression,
14623d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick                                          const char* expected,
14633d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick                                          const char* actual);
14643d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick
14653d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// The helper function for {ASSERT|EXPECT}_STRCASEEQ.
14663d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick//
14673d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM.
14683d000e7dd14c3185b9e27a6c38a67288b4d10431Ian RomanickGTEST_API_ AssertionResult CmpHelperSTRCASEEQ(const char* expected_expression,
14693d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick                                              const char* actual_expression,
14703d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick                                              const char* expected,
14713d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick                                              const char* actual);
14723d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick
14733d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// The helper function for {ASSERT|EXPECT}_STRNE.
14743d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick//
14753d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM.
14763d000e7dd14c3185b9e27a6c38a67288b4d10431Ian RomanickGTEST_API_ AssertionResult CmpHelperSTRNE(const char* s1_expression,
14773d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick                                          const char* s2_expression,
14783d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick                                          const char* s1,
14793d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick                                          const char* s2);
14803d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick
14813d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// The helper function for {ASSERT|EXPECT}_STRCASENE.
14823d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick//
14833d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM.
14843d000e7dd14c3185b9e27a6c38a67288b4d10431Ian RomanickGTEST_API_ AssertionResult CmpHelperSTRCASENE(const char* s1_expression,
14853d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick                                              const char* s2_expression,
14863d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick                                              const char* s1,
14873d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick                                              const char* s2);
14883d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick
14893d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick
14903d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// Helper function for *_STREQ on wide strings.
14913d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick//
14923d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM.
14933d000e7dd14c3185b9e27a6c38a67288b4d10431Ian RomanickGTEST_API_ AssertionResult CmpHelperSTREQ(const char* expected_expression,
14943d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick                                          const char* actual_expression,
14953d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick                                          const wchar_t* expected,
14963d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick                                          const wchar_t* actual);
14973d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick
14983d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// Helper function for *_STRNE on wide strings.
14993d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick//
15003d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM.
15013d000e7dd14c3185b9e27a6c38a67288b4d10431Ian RomanickGTEST_API_ AssertionResult CmpHelperSTRNE(const char* s1_expression,
15023d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick                                          const char* s2_expression,
15033d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick                                          const wchar_t* s1,
15043d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick                                          const wchar_t* s2);
15053d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick
15063d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick}  // namespace internal
15073d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick
15083d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// IsSubstring() and IsNotSubstring() are intended to be used as the
15093d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// first argument to {EXPECT,ASSERT}_PRED_FORMAT2(), not by
15103d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// themselves.  They check whether needle is a substring of haystack
15113d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// (NULL is considered a substring of itself only), and return an
15123d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// appropriate error message when they fail.
15133d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick//
15143d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// The {needle,haystack}_expr arguments are the stringified
15153d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// expressions that generated the two real arguments.
15163d000e7dd14c3185b9e27a6c38a67288b4d10431Ian RomanickGTEST_API_ AssertionResult IsSubstring(
15173d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick    const char* needle_expr, const char* haystack_expr,
15183d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick    const char* needle, const char* haystack);
15193d000e7dd14c3185b9e27a6c38a67288b4d10431Ian RomanickGTEST_API_ AssertionResult IsSubstring(
15203d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick    const char* needle_expr, const char* haystack_expr,
15213d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick    const wchar_t* needle, const wchar_t* haystack);
15223d000e7dd14c3185b9e27a6c38a67288b4d10431Ian RomanickGTEST_API_ AssertionResult IsNotSubstring(
15233d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick    const char* needle_expr, const char* haystack_expr,
15243d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick    const char* needle, const char* haystack);
15253d000e7dd14c3185b9e27a6c38a67288b4d10431Ian RomanickGTEST_API_ AssertionResult IsNotSubstring(
15263d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick    const char* needle_expr, const char* haystack_expr,
15273d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick    const wchar_t* needle, const wchar_t* haystack);
15283d000e7dd14c3185b9e27a6c38a67288b4d10431Ian RomanickGTEST_API_ AssertionResult IsSubstring(
15293d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick    const char* needle_expr, const char* haystack_expr,
15303d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick    const ::std::string& needle, const ::std::string& haystack);
15313d000e7dd14c3185b9e27a6c38a67288b4d10431Ian RomanickGTEST_API_ AssertionResult IsNotSubstring(
15323d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick    const char* needle_expr, const char* haystack_expr,
15333d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick    const ::std::string& needle, const ::std::string& haystack);
15343d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick
15353d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick#if GTEST_HAS_STD_WSTRING
15363d000e7dd14c3185b9e27a6c38a67288b4d10431Ian RomanickGTEST_API_ AssertionResult IsSubstring(
15373d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick    const char* needle_expr, const char* haystack_expr,
15383d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick    const ::std::wstring& needle, const ::std::wstring& haystack);
15393d000e7dd14c3185b9e27a6c38a67288b4d10431Ian RomanickGTEST_API_ AssertionResult IsNotSubstring(
15403d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick    const char* needle_expr, const char* haystack_expr,
15413d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick    const ::std::wstring& needle, const ::std::wstring& haystack);
15423d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick#endif  // GTEST_HAS_STD_WSTRING
15433d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick
15443d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanicknamespace internal {
15453d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick
15463d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// Helper template function for comparing floating-points.
15473d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick//
15483d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// Template parameter:
15493d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick//
15503d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick//   RawType: the raw floating-point type (either float or double)
15513d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick//
15523d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM.
15533d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanicktemplate <typename RawType>
15543d000e7dd14c3185b9e27a6c38a67288b4d10431Ian RomanickAssertionResult CmpHelperFloatingPointEQ(const char* expected_expression,
15553d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick                                         const char* actual_expression,
15563d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick                                         RawType expected,
15573d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick                                         RawType actual) {
15583d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  const FloatingPoint<RawType> lhs(expected), rhs(actual);
15593d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick
15603d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  if (lhs.AlmostEquals(rhs)) {
15613d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick    return AssertionSuccess();
15623d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  }
15633d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick
15643d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  ::std::stringstream expected_ss;
15653d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  expected_ss << std::setprecision(std::numeric_limits<RawType>::digits10 + 2)
15663d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick              << expected;
15673d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick
15683d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  ::std::stringstream actual_ss;
15693d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  actual_ss << std::setprecision(std::numeric_limits<RawType>::digits10 + 2)
15703d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick            << actual;
15713d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick
15723d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  return EqFailure(expected_expression,
15733d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick                   actual_expression,
15743d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick                   StringStreamToString(&expected_ss),
15753d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick                   StringStreamToString(&actual_ss),
15763d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick                   false);
15773d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick}
15783d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick
15793d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// Helper function for implementing ASSERT_NEAR.
15803d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick//
15813d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM.
15823d000e7dd14c3185b9e27a6c38a67288b4d10431Ian RomanickGTEST_API_ AssertionResult DoubleNearPredFormat(const char* expr1,
15833d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick                                                const char* expr2,
15843d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick                                                const char* abs_error_expr,
15853d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick                                                double val1,
15863d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick                                                double val2,
15873d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick                                                double abs_error);
15883d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick
15893d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// INTERNAL IMPLEMENTATION - DO NOT USE IN USER CODE.
15903d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// A class that enables one to stream messages to assertion macros
15913d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanickclass GTEST_API_ AssertHelper {
15923d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick public:
15933d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // Constructor.
15943d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  AssertHelper(TestPartResult::Type type,
15953d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick               const char* file,
15963d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick               int line,
15973d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick               const char* message);
15983d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  ~AssertHelper();
15993d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick
16003d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // Message assignment is a semantic trick to enable assertion
16013d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // streaming; see the GTEST_MESSAGE_ macro below.
16023d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  void operator=(const Message& message) const;
16033d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick
16043d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick private:
16053d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // We put our data in a struct so that the size of the AssertHelper class can
16063d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // be as small as possible.  This is important because gcc is incapable of
16073d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // re-using stack space even for temporary variables, so every EXPECT_EQ
16083d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // reserves stack space for another AssertHelper.
16093d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  struct AssertHelperData {
16103d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick    AssertHelperData(TestPartResult::Type t,
16113d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick                     const char* srcfile,
16123d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick                     int line_num,
16133d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick                     const char* msg)
16143d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick        : type(t), file(srcfile), line(line_num), message(msg) { }
16153d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick
16163d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick    TestPartResult::Type const type;
16173d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick    const char*        const file;
16183d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick    int                const line;
16193d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick    String             const message;
16203d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick
16213d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick   private:
16223d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick    GTEST_DISALLOW_COPY_AND_ASSIGN_(AssertHelperData);
16233d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  };
16243d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick
16253d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  AssertHelperData* const data_;
16263d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick
16273d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  GTEST_DISALLOW_COPY_AND_ASSIGN_(AssertHelper);
16283d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick};
16293d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick
16303d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick}  // namespace internal
16313d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick
16323d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick#if GTEST_HAS_PARAM_TEST
16333d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// The pure interface class that all value-parameterized tests inherit from.
16343d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// A value-parameterized class must inherit from both ::testing::Test and
16353d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// ::testing::WithParamInterface. In most cases that just means inheriting
16363d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// from ::testing::TestWithParam, but more complicated test hierarchies
16373d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// may need to inherit from Test and WithParamInterface at different levels.
16383d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick//
16393d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// This interface has support for accessing the test parameter value via
16403d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// the GetParam() method.
16413d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick//
16423d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// Use it with one of the parameter generator defining functions, like Range(),
16433d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// Values(), ValuesIn(), Bool(), and Combine().
16443d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick//
16453d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// class FooTest : public ::testing::TestWithParam<int> {
16463d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick//  protected:
16473d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick//   FooTest() {
16483d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick//     // Can use GetParam() here.
16493d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick//   }
16503d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick//   virtual ~FooTest() {
16513d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick//     // Can use GetParam() here.
16523d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick//   }
16533d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick//   virtual void SetUp() {
16543d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick//     // Can use GetParam() here.
16553d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick//   }
16563d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick//   virtual void TearDown {
16573d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick//     // Can use GetParam() here.
16583d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick//   }
16593d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// };
16603d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// TEST_P(FooTest, DoesBar) {
16613d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick//   // Can use GetParam() method here.
16623d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick//   Foo foo;
16633d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick//   ASSERT_TRUE(foo.DoesBar(GetParam()));
16643d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// }
16653d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// INSTANTIATE_TEST_CASE_P(OneToTenRange, FooTest, ::testing::Range(1, 10));
16663d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick
16673d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanicktemplate <typename T>
16683d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanickclass WithParamInterface {
16693d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick public:
16703d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  typedef T ParamType;
16713d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  virtual ~WithParamInterface() {}
16723d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick
16733d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // The current parameter value. Is also available in the test fixture's
16743d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // constructor. This member function is non-static, even though it only
16753d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // references static data, to reduce the opportunity for incorrect uses
16763d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // like writing 'WithParamInterface<bool>::GetParam()' for a test that
16773d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // uses a fixture whose parameter type is int.
16783d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  const ParamType& GetParam() const { return *parameter_; }
16793d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick
16803d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick private:
16813d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // Sets parameter value. The caller is responsible for making sure the value
16823d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // remains alive and unchanged throughout the current test.
16833d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  static void SetParam(const ParamType* parameter) {
16843d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick    parameter_ = parameter;
16853d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  }
16863d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick
16873d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // Static value used for accessing parameter during a test lifetime.
16883d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  static const ParamType* parameter_;
16893d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick
16903d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  // TestClass must be a subclass of WithParamInterface<T> and Test.
16913d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  template <class TestClass> friend class internal::ParameterizedTestFactory;
16923d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick};
16933d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick
16943d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanicktemplate <typename T>
16953d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanickconst T* WithParamInterface<T>::parameter_ = NULL;
16963d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick
16973d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// Most value-parameterized classes can ignore the existence of
16983d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// WithParamInterface, and can just inherit from ::testing::TestWithParam.
16993d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick
17003d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanicktemplate <typename T>
17013d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanickclass TestWithParam : public Test, public WithParamInterface<T> {
17023d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick};
17033d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick
17043d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick#endif  // GTEST_HAS_PARAM_TEST
17053d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick
17063d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// Macros for indicating success/failure in test code.
17073d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick
17083d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// ADD_FAILURE unconditionally adds a failure to the current test.
17093d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// SUCCEED generates a success - it doesn't automatically make the
17103d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// current test successful, as a test is only successful when it has
17113d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// no failure.
17123d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick//
17133d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// EXPECT_* verifies that a certain condition is satisfied.  If not,
17143d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// it behaves like ADD_FAILURE.  In particular:
17153d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick//
17163d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick//   EXPECT_TRUE  verifies that a Boolean condition is true.
17173d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick//   EXPECT_FALSE verifies that a Boolean condition is false.
17183d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick//
17193d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// FAIL and ASSERT_* are similar to ADD_FAILURE and EXPECT_*, except
17203d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// that they will also abort the current function on failure.  People
17213d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// usually want the fail-fast behavior of FAIL and ASSERT_*, but those
17223d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// writing data-driven tests often find themselves using ADD_FAILURE
17233d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// and EXPECT_* more.
17243d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick//
17253d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// Examples:
17263d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick//
17273d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick//   EXPECT_TRUE(server.StatusIsOK());
17283d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick//   ASSERT_FALSE(server.HasPendingRequest(port))
17293d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick//       << "There are still pending requests " << "on port " << port;
17303d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick
17313d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// Generates a nonfatal failure with a generic message.
17323d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick#define ADD_FAILURE() GTEST_NONFATAL_FAILURE_("Failed")
17333d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick
17343d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// Generates a nonfatal failure at the given source file location with
17353d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// a generic message.
17363d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick#define ADD_FAILURE_AT(file, line) \
17373d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  GTEST_MESSAGE_AT_(file, line, "Failed", \
17383d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick                    ::testing::TestPartResult::kNonFatalFailure)
17393d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick
17403d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// Generates a fatal failure with a generic message.
17413d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick#define GTEST_FAIL() GTEST_FATAL_FAILURE_("Failed")
17423d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick
17433d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// Define this macro to 1 to omit the definition of FAIL(), which is a
17443d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// generic name and clashes with some other libraries.
17453d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick#if !GTEST_DONT_DEFINE_FAIL
17463d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick# define FAIL() GTEST_FAIL()
17473d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick#endif
17483d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick
17493d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// Generates a success with a generic message.
17503d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick#define GTEST_SUCCEED() GTEST_SUCCESS_("Succeeded")
17513d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick
17523d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// Define this macro to 1 to omit the definition of SUCCEED(), which
17533d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// is a generic name and clashes with some other libraries.
17543d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick#if !GTEST_DONT_DEFINE_SUCCEED
17553d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick# define SUCCEED() GTEST_SUCCEED()
17563d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick#endif
17573d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick
17583d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// Macros for testing exceptions.
17593d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick//
17603d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick//    * {ASSERT|EXPECT}_THROW(statement, expected_exception):
17613d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick//         Tests that the statement throws the expected exception.
17623d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick//    * {ASSERT|EXPECT}_NO_THROW(statement):
17633d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick//         Tests that the statement doesn't throw any exception.
17643d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick//    * {ASSERT|EXPECT}_ANY_THROW(statement):
17653d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick//         Tests that the statement throws an exception.
17663d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick
17673d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick#define EXPECT_THROW(statement, expected_exception) \
17683d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  GTEST_TEST_THROW_(statement, expected_exception, GTEST_NONFATAL_FAILURE_)
17693d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick#define EXPECT_NO_THROW(statement) \
17703d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  GTEST_TEST_NO_THROW_(statement, GTEST_NONFATAL_FAILURE_)
17713d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick#define EXPECT_ANY_THROW(statement) \
17723d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  GTEST_TEST_ANY_THROW_(statement, GTEST_NONFATAL_FAILURE_)
17733d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick#define ASSERT_THROW(statement, expected_exception) \
17743d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  GTEST_TEST_THROW_(statement, expected_exception, GTEST_FATAL_FAILURE_)
17753d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick#define ASSERT_NO_THROW(statement) \
17763d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  GTEST_TEST_NO_THROW_(statement, GTEST_FATAL_FAILURE_)
17773d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick#define ASSERT_ANY_THROW(statement) \
17783d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  GTEST_TEST_ANY_THROW_(statement, GTEST_FATAL_FAILURE_)
17793d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick
17803d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// Boolean assertions. Condition can be either a Boolean expression or an
17813d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// AssertionResult. For more information on how to use AssertionResult with
17823d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// these macros see comments on that class.
17833d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick#define EXPECT_TRUE(condition) \
17843d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  GTEST_TEST_BOOLEAN_(condition, #condition, false, true, \
17853d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick                      GTEST_NONFATAL_FAILURE_)
17863d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick#define EXPECT_FALSE(condition) \
17873d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  GTEST_TEST_BOOLEAN_(!(condition), #condition, true, false, \
17883d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick                      GTEST_NONFATAL_FAILURE_)
17893d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick#define ASSERT_TRUE(condition) \
17903d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  GTEST_TEST_BOOLEAN_(condition, #condition, false, true, \
17913d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick                      GTEST_FATAL_FAILURE_)
17923d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick#define ASSERT_FALSE(condition) \
17933d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  GTEST_TEST_BOOLEAN_(!(condition), #condition, true, false, \
17943d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick                      GTEST_FATAL_FAILURE_)
17953d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick
17963d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// Includes the auto-generated header that implements a family of
17973d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// generic predicate assertion macros.
17983d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick#include "gtest/gtest_pred_impl.h"
17993d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick
18003d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// Macros for testing equalities and inequalities.
18013d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick//
18023d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick//    * {ASSERT|EXPECT}_EQ(expected, actual): Tests that expected == actual
18033d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick//    * {ASSERT|EXPECT}_NE(v1, v2):           Tests that v1 != v2
18043d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick//    * {ASSERT|EXPECT}_LT(v1, v2):           Tests that v1 < v2
18053d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick//    * {ASSERT|EXPECT}_LE(v1, v2):           Tests that v1 <= v2
18063d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick//    * {ASSERT|EXPECT}_GT(v1, v2):           Tests that v1 > v2
18073d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick//    * {ASSERT|EXPECT}_GE(v1, v2):           Tests that v1 >= v2
18083d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick//
18093d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// When they are not, Google Test prints both the tested expressions and
18103d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// their actual values.  The values must be compatible built-in types,
18113d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// or you will get a compiler error.  By "compatible" we mean that the
18123d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// values can be compared by the respective operator.
18133d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick//
18143d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// Note:
18153d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick//
18163d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick//   1. It is possible to make a user-defined type work with
18173d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick//   {ASSERT|EXPECT}_??(), but that requires overloading the
18183d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick//   comparison operators and is thus discouraged by the Google C++
18193d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick//   Usage Guide.  Therefore, you are advised to use the
18203d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick//   {ASSERT|EXPECT}_TRUE() macro to assert that two objects are
18213d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick//   equal.
18223d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick//
18233d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick//   2. The {ASSERT|EXPECT}_??() macros do pointer comparisons on
18243d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick//   pointers (in particular, C strings).  Therefore, if you use it
18253d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick//   with two C strings, you are testing how their locations in memory
18263d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick//   are related, not how their content is related.  To compare two C
18273d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick//   strings by content, use {ASSERT|EXPECT}_STR*().
18283d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick//
18293d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick//   3. {ASSERT|EXPECT}_EQ(expected, actual) is preferred to
18303d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick//   {ASSERT|EXPECT}_TRUE(expected == actual), as the former tells you
18313d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick//   what the actual value is when it fails, and similarly for the
18323d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick//   other comparisons.
18333d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick//
18343d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick//   4. Do not depend on the order in which {ASSERT|EXPECT}_??()
18353d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick//   evaluate their arguments, which is undefined.
18363d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick//
18373d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick//   5. These macros evaluate their arguments exactly once.
18383d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick//
18393d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// Examples:
18403d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick//
18413d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick//   EXPECT_NE(5, Foo());
18423d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick//   EXPECT_EQ(NULL, a_pointer);
18433d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick//   ASSERT_LT(i, array_size);
18443d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick//   ASSERT_GT(records.size(), 0) << "There is no record left.";
18453d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick
18463d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick#define EXPECT_EQ(expected, actual) \
18473d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  EXPECT_PRED_FORMAT2(::testing::internal:: \
18483d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick                      EqHelper<GTEST_IS_NULL_LITERAL_(expected)>::Compare, \
18493d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick                      expected, actual)
18503d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick#define EXPECT_NE(expected, actual) \
18513d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  EXPECT_PRED_FORMAT2(::testing::internal::CmpHelperNE, expected, actual)
18523d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick#define EXPECT_LE(val1, val2) \
18533d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  EXPECT_PRED_FORMAT2(::testing::internal::CmpHelperLE, val1, val2)
18543d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick#define EXPECT_LT(val1, val2) \
18553d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  EXPECT_PRED_FORMAT2(::testing::internal::CmpHelperLT, val1, val2)
18563d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick#define EXPECT_GE(val1, val2) \
18573d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  EXPECT_PRED_FORMAT2(::testing::internal::CmpHelperGE, val1, val2)
18583d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick#define EXPECT_GT(val1, val2) \
18593d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  EXPECT_PRED_FORMAT2(::testing::internal::CmpHelperGT, val1, val2)
18603d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick
18613d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick#define GTEST_ASSERT_EQ(expected, actual) \
18623d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  ASSERT_PRED_FORMAT2(::testing::internal:: \
18633d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick                      EqHelper<GTEST_IS_NULL_LITERAL_(expected)>::Compare, \
18643d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick                      expected, actual)
18653d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick#define GTEST_ASSERT_NE(val1, val2) \
18663d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  ASSERT_PRED_FORMAT2(::testing::internal::CmpHelperNE, val1, val2)
18673d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick#define GTEST_ASSERT_LE(val1, val2) \
18683d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  ASSERT_PRED_FORMAT2(::testing::internal::CmpHelperLE, val1, val2)
18693d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick#define GTEST_ASSERT_LT(val1, val2) \
18703d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  ASSERT_PRED_FORMAT2(::testing::internal::CmpHelperLT, val1, val2)
18713d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick#define GTEST_ASSERT_GE(val1, val2) \
18723d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  ASSERT_PRED_FORMAT2(::testing::internal::CmpHelperGE, val1, val2)
18733d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick#define GTEST_ASSERT_GT(val1, val2) \
18743d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  ASSERT_PRED_FORMAT2(::testing::internal::CmpHelperGT, val1, val2)
18753d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick
18763d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// Define macro GTEST_DONT_DEFINE_ASSERT_XY to 1 to omit the definition of
18773d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// ASSERT_XY(), which clashes with some users' own code.
18783d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick
18793d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick#if !GTEST_DONT_DEFINE_ASSERT_EQ
18803d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick# define ASSERT_EQ(val1, val2) GTEST_ASSERT_EQ(val1, val2)
18813d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick#endif
18823d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick
18833d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick#if !GTEST_DONT_DEFINE_ASSERT_NE
18843d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick# define ASSERT_NE(val1, val2) GTEST_ASSERT_NE(val1, val2)
18853d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick#endif
18863d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick
18873d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick#if !GTEST_DONT_DEFINE_ASSERT_LE
18883d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick# define ASSERT_LE(val1, val2) GTEST_ASSERT_LE(val1, val2)
18893d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick#endif
18903d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick
18913d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick#if !GTEST_DONT_DEFINE_ASSERT_LT
18923d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick# define ASSERT_LT(val1, val2) GTEST_ASSERT_LT(val1, val2)
18933d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick#endif
18943d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick
18953d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick#if !GTEST_DONT_DEFINE_ASSERT_GE
18963d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick# define ASSERT_GE(val1, val2) GTEST_ASSERT_GE(val1, val2)
18973d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick#endif
18983d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick
18993d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick#if !GTEST_DONT_DEFINE_ASSERT_GT
19003d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick# define ASSERT_GT(val1, val2) GTEST_ASSERT_GT(val1, val2)
19013d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick#endif
19023d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick
19033d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// C String Comparisons.  All tests treat NULL and any non-NULL string
19043d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// as different.  Two NULLs are equal.
19053d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick//
19063d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick//    * {ASSERT|EXPECT}_STREQ(s1, s2):     Tests that s1 == s2
19073d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick//    * {ASSERT|EXPECT}_STRNE(s1, s2):     Tests that s1 != s2
19083d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick//    * {ASSERT|EXPECT}_STRCASEEQ(s1, s2): Tests that s1 == s2, ignoring case
19093d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick//    * {ASSERT|EXPECT}_STRCASENE(s1, s2): Tests that s1 != s2, ignoring case
19103d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick//
19113d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// For wide or narrow string objects, you can use the
19123d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// {ASSERT|EXPECT}_??() macros.
19133d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick//
19143d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// Don't depend on the order in which the arguments are evaluated,
19153d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// which is undefined.
19163d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick//
19173d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// These macros evaluate their arguments exactly once.
19183d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick
19193d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick#define EXPECT_STREQ(expected, actual) \
19203d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  EXPECT_PRED_FORMAT2(::testing::internal::CmpHelperSTREQ, expected, actual)
19213d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick#define EXPECT_STRNE(s1, s2) \
19223d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  EXPECT_PRED_FORMAT2(::testing::internal::CmpHelperSTRNE, s1, s2)
19233d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick#define EXPECT_STRCASEEQ(expected, actual) \
19243d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  EXPECT_PRED_FORMAT2(::testing::internal::CmpHelperSTRCASEEQ, expected, actual)
19253d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick#define EXPECT_STRCASENE(s1, s2)\
19263d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  EXPECT_PRED_FORMAT2(::testing::internal::CmpHelperSTRCASENE, s1, s2)
19273d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick
19283d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick#define ASSERT_STREQ(expected, actual) \
19293d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  ASSERT_PRED_FORMAT2(::testing::internal::CmpHelperSTREQ, expected, actual)
19303d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick#define ASSERT_STRNE(s1, s2) \
19313d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  ASSERT_PRED_FORMAT2(::testing::internal::CmpHelperSTRNE, s1, s2)
19323d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick#define ASSERT_STRCASEEQ(expected, actual) \
19333d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  ASSERT_PRED_FORMAT2(::testing::internal::CmpHelperSTRCASEEQ, expected, actual)
19343d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick#define ASSERT_STRCASENE(s1, s2)\
19353d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  ASSERT_PRED_FORMAT2(::testing::internal::CmpHelperSTRCASENE, s1, s2)
19363d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick
19373d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// Macros for comparing floating-point numbers.
19383d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick//
19393d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick//    * {ASSERT|EXPECT}_FLOAT_EQ(expected, actual):
19403d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick//         Tests that two float values are almost equal.
19413d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick//    * {ASSERT|EXPECT}_DOUBLE_EQ(expected, actual):
19423d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick//         Tests that two double values are almost equal.
19433d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick//    * {ASSERT|EXPECT}_NEAR(v1, v2, abs_error):
19443d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick//         Tests that v1 and v2 are within the given distance to each other.
19453d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick//
19463d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// Google Test uses ULP-based comparison to automatically pick a default
19473d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// error bound that is appropriate for the operands.  See the
19483d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// FloatingPoint template class in gtest-internal.h if you are
19493d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// interested in the implementation details.
19503d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick
19513d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick#define EXPECT_FLOAT_EQ(expected, actual)\
19523d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  EXPECT_PRED_FORMAT2(::testing::internal::CmpHelperFloatingPointEQ<float>, \
19533d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick                      expected, actual)
19543d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick
19553d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick#define EXPECT_DOUBLE_EQ(expected, actual)\
19563d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  EXPECT_PRED_FORMAT2(::testing::internal::CmpHelperFloatingPointEQ<double>, \
19573d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick                      expected, actual)
19583d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick
19593d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick#define ASSERT_FLOAT_EQ(expected, actual)\
19603d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  ASSERT_PRED_FORMAT2(::testing::internal::CmpHelperFloatingPointEQ<float>, \
19613d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick                      expected, actual)
19623d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick
19633d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick#define ASSERT_DOUBLE_EQ(expected, actual)\
19643d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  ASSERT_PRED_FORMAT2(::testing::internal::CmpHelperFloatingPointEQ<double>, \
19653d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick                      expected, actual)
19663d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick
19673d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick#define EXPECT_NEAR(val1, val2, abs_error)\
19683d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  EXPECT_PRED_FORMAT3(::testing::internal::DoubleNearPredFormat, \
19693d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick                      val1, val2, abs_error)
19703d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick
19713d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick#define ASSERT_NEAR(val1, val2, abs_error)\
19723d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  ASSERT_PRED_FORMAT3(::testing::internal::DoubleNearPredFormat, \
19733d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick                      val1, val2, abs_error)
19743d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick
19753d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// These predicate format functions work on floating-point values, and
19763d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// can be used in {ASSERT|EXPECT}_PRED_FORMAT2*(), e.g.
19773d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick//
19783d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick//   EXPECT_PRED_FORMAT2(testing::DoubleLE, Foo(), 5.0);
19793d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick
19803d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// Asserts that val1 is less than, or almost equal to, val2.  Fails
19813d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// otherwise.  In particular, it fails if either val1 or val2 is NaN.
19823d000e7dd14c3185b9e27a6c38a67288b4d10431Ian RomanickGTEST_API_ AssertionResult FloatLE(const char* expr1, const char* expr2,
19833d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick                                   float val1, float val2);
19843d000e7dd14c3185b9e27a6c38a67288b4d10431Ian RomanickGTEST_API_ AssertionResult DoubleLE(const char* expr1, const char* expr2,
19853d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick                                    double val1, double val2);
19863d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick
19873d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick
19883d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick#if GTEST_OS_WINDOWS
19893d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick
19903d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// Macros that test for HRESULT failure and success, these are only useful
19913d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// on Windows, and rely on Windows SDK macros and APIs to compile.
19923d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick//
19933d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick//    * {ASSERT|EXPECT}_HRESULT_{SUCCEEDED|FAILED}(expr)
19943d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick//
19953d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// When expr unexpectedly fails or succeeds, Google Test prints the
19963d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// expected result and the actual result with both a human-readable
19973d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// string representation of the error, if available, as well as the
19983d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// hex result code.
19993d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick# define EXPECT_HRESULT_SUCCEEDED(expr) \
20003d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick    EXPECT_PRED_FORMAT1(::testing::internal::IsHRESULTSuccess, (expr))
20013d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick
20023d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick# define ASSERT_HRESULT_SUCCEEDED(expr) \
20033d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick    ASSERT_PRED_FORMAT1(::testing::internal::IsHRESULTSuccess, (expr))
20043d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick
20053d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick# define EXPECT_HRESULT_FAILED(expr) \
20063d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick    EXPECT_PRED_FORMAT1(::testing::internal::IsHRESULTFailure, (expr))
20073d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick
20083d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick# define ASSERT_HRESULT_FAILED(expr) \
20093d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick    ASSERT_PRED_FORMAT1(::testing::internal::IsHRESULTFailure, (expr))
20103d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick
20113d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick#endif  // GTEST_OS_WINDOWS
20123d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick
20133d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// Macros that execute statement and check that it doesn't generate new fatal
20143d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// failures in the current thread.
20153d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick//
20163d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick//   * {ASSERT|EXPECT}_NO_FATAL_FAILURE(statement);
20173d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick//
20183d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// Examples:
20193d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick//
20203d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick//   EXPECT_NO_FATAL_FAILURE(Process());
20213d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick//   ASSERT_NO_FATAL_FAILURE(Process()) << "Process() failed";
20223d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick//
20233d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick#define ASSERT_NO_FATAL_FAILURE(statement) \
20243d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick    GTEST_TEST_NO_FATAL_FAILURE_(statement, GTEST_FATAL_FAILURE_)
20253d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick#define EXPECT_NO_FATAL_FAILURE(statement) \
20263d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick    GTEST_TEST_NO_FATAL_FAILURE_(statement, GTEST_NONFATAL_FAILURE_)
20273d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick
20283d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// Causes a trace (including the source file path, the current line
20293d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// number, and the given message) to be included in every test failure
20303d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// message generated by code in the current scope.  The effect is
20313d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// undone when the control leaves the current scope.
20323d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick//
20333d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// The message argument can be anything streamable to std::ostream.
20343d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick//
20353d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// In the implementation, we include the current line number as part
20363d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// of the dummy variable name, thus allowing multiple SCOPED_TRACE()s
20373d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// to appear in the same block - as long as they are on different
20383d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// lines.
20393d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick#define SCOPED_TRACE(message) \
20403d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  ::testing::internal::ScopedTrace GTEST_CONCAT_TOKEN_(gtest_trace_, __LINE__)(\
20413d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick    __FILE__, __LINE__, ::testing::Message() << (message))
20423d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick
20433d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// Compile-time assertion for type equality.
20443d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// StaticAssertTypeEq<type1, type2>() compiles iff type1 and type2 are
20453d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// the same type.  The value it returns is not interesting.
20463d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick//
20473d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// Instead of making StaticAssertTypeEq a class template, we make it a
20483d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// function template that invokes a helper class template.  This
20493d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// prevents a user from misusing StaticAssertTypeEq<T1, T2> by
20503d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// defining objects of that type.
20513d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick//
20523d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// CAVEAT:
20533d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick//
20543d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// When used inside a method of a class template,
20553d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// StaticAssertTypeEq<T1, T2>() is effective ONLY IF the method is
20563d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// instantiated.  For example, given:
20573d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick//
20583d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick//   template <typename T> class Foo {
20593d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick//    public:
20603d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick//     void Bar() { testing::StaticAssertTypeEq<int, T>(); }
20613d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick//   };
20623d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick//
20633d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// the code:
20643d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick//
20653d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick//   void Test1() { Foo<bool> foo; }
20663d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick//
20673d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// will NOT generate a compiler error, as Foo<bool>::Bar() is never
20683d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// actually instantiated.  Instead, you need:
20693d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick//
20703d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick//   void Test2() { Foo<bool> foo; foo.Bar(); }
20713d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick//
20723d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// to cause a compiler error.
20733d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanicktemplate <typename T1, typename T2>
20743d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanickbool StaticAssertTypeEq() {
20753d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  (void)internal::StaticAssertTypeEqHelper<T1, T2>();
20763d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  return true;
20773d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick}
20783d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick
20793d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// Defines a test.
20803d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick//
20813d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// The first parameter is the name of the test case, and the second
20823d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// parameter is the name of the test within the test case.
20833d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick//
20843d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// The convention is to end the test case name with "Test".  For
20853d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// example, a test case for the Foo class can be named FooTest.
20863d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick//
20873d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// The user should put his test code between braces after using this
20883d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// macro.  Example:
20893d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick//
20903d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick//   TEST(FooTest, InitializesCorrectly) {
20913d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick//     Foo foo;
20923d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick//     EXPECT_TRUE(foo.StatusIsOK());
20933d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick//   }
20943d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick
20953d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// Note that we call GetTestTypeId() instead of GetTypeId<
20963d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// ::testing::Test>() here to get the type ID of testing::Test.  This
20973d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// is to work around a suspected linker bug when using Google Test as
20983d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// a framework on Mac OS X.  The bug causes GetTypeId<
20993d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// ::testing::Test>() to return different values depending on whether
21003d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// the call is from the Google Test framework itself or from user test
21013d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// code.  GetTestTypeId() is guaranteed to always return the same
21023d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// value, as it always calls GetTypeId<>() from the Google Test
21033d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// framework.
21043d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick#define GTEST_TEST(test_case_name, test_name)\
21053d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  GTEST_TEST_(test_case_name, test_name, \
21063d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick              ::testing::Test, ::testing::internal::GetTestTypeId())
21073d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick
21083d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// Define this macro to 1 to omit the definition of TEST(), which
21093d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// is a generic name and clashes with some other libraries.
21103d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick#if !GTEST_DONT_DEFINE_TEST
21113d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick# define TEST(test_case_name, test_name) GTEST_TEST(test_case_name, test_name)
21123d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick#endif
21133d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick
21143d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// Defines a test that uses a test fixture.
21153d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick//
21163d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// The first parameter is the name of the test fixture class, which
21173d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// also doubles as the test case name.  The second parameter is the
21183d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// name of the test within the test case.
21193d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick//
21203d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// A test fixture class must be declared earlier.  The user should put
21213d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// his test code between braces after using this macro.  Example:
21223d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick//
21233d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick//   class FooTest : public testing::Test {
21243d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick//    protected:
21253d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick//     virtual void SetUp() { b_.AddElement(3); }
21263d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick//
21273d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick//     Foo a_;
21283d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick//     Foo b_;
21293d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick//   };
21303d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick//
21313d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick//   TEST_F(FooTest, InitializesCorrectly) {
21323d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick//     EXPECT_TRUE(a_.StatusIsOK());
21333d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick//   }
21343d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick//
21353d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick//   TEST_F(FooTest, ReturnsElementCountCorrectly) {
21363d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick//     EXPECT_EQ(0, a_.size());
21373d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick//     EXPECT_EQ(1, b_.size());
21383d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick//   }
21393d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick
21403d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick#define TEST_F(test_fixture, test_name)\
21413d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  GTEST_TEST_(test_fixture, test_name, test_fixture, \
21423d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick              ::testing::internal::GetTypeId<test_fixture>())
21433d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick
21443d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// Use this macro in main() to run all tests.  It returns 0 if all
21453d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// tests are successful, or 1 otherwise.
21463d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick//
21473d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// RUN_ALL_TESTS() should be invoked after the command line has been
21483d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick// parsed by InitGoogleTest().
21493d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick
21503d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick#define RUN_ALL_TESTS()\
21513d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick  (::testing::UnitTest::GetInstance()->Run())
21523d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick
21533d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick}  // namespace testing
21543d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick
21553d000e7dd14c3185b9e27a6c38a67288b4d10431Ian Romanick#endif  // GTEST_INCLUDE_GTEST_GTEST_H_
2156