14b6829f0d28990dd645e16386eb226d0f10c8731shiqian// Copyright 2005, Google Inc.
24b6829f0d28990dd645e16386eb226d0f10c8731shiqian// All rights reserved.
34b6829f0d28990dd645e16386eb226d0f10c8731shiqian//
44b6829f0d28990dd645e16386eb226d0f10c8731shiqian// Redistribution and use in source and binary forms, with or without
54b6829f0d28990dd645e16386eb226d0f10c8731shiqian// modification, are permitted provided that the following conditions are
64b6829f0d28990dd645e16386eb226d0f10c8731shiqian// met:
74b6829f0d28990dd645e16386eb226d0f10c8731shiqian//
84b6829f0d28990dd645e16386eb226d0f10c8731shiqian//     * Redistributions of source code must retain the above copyright
94b6829f0d28990dd645e16386eb226d0f10c8731shiqian// notice, this list of conditions and the following disclaimer.
104b6829f0d28990dd645e16386eb226d0f10c8731shiqian//     * Redistributions in binary form must reproduce the above
114b6829f0d28990dd645e16386eb226d0f10c8731shiqian// copyright notice, this list of conditions and the following disclaimer
124b6829f0d28990dd645e16386eb226d0f10c8731shiqian// in the documentation and/or other materials provided with the
134b6829f0d28990dd645e16386eb226d0f10c8731shiqian// distribution.
144b6829f0d28990dd645e16386eb226d0f10c8731shiqian//     * Neither the name of Google Inc. nor the names of its
154b6829f0d28990dd645e16386eb226d0f10c8731shiqian// contributors may be used to endorse or promote products derived from
164b6829f0d28990dd645e16386eb226d0f10c8731shiqian// this software without specific prior written permission.
174b6829f0d28990dd645e16386eb226d0f10c8731shiqian//
184b6829f0d28990dd645e16386eb226d0f10c8731shiqian// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
194b6829f0d28990dd645e16386eb226d0f10c8731shiqian// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
204b6829f0d28990dd645e16386eb226d0f10c8731shiqian// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
214b6829f0d28990dd645e16386eb226d0f10c8731shiqian// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
224b6829f0d28990dd645e16386eb226d0f10c8731shiqian// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
234b6829f0d28990dd645e16386eb226d0f10c8731shiqian// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
244b6829f0d28990dd645e16386eb226d0f10c8731shiqian// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
254b6829f0d28990dd645e16386eb226d0f10c8731shiqian// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
264b6829f0d28990dd645e16386eb226d0f10c8731shiqian// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
274b6829f0d28990dd645e16386eb226d0f10c8731shiqian// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
284b6829f0d28990dd645e16386eb226d0f10c8731shiqian// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
294b6829f0d28990dd645e16386eb226d0f10c8731shiqian//
304b6829f0d28990dd645e16386eb226d0f10c8731shiqian// Author: wan@google.com (Zhanyong Wan)
314b6829f0d28990dd645e16386eb226d0f10c8731shiqian//
324b6829f0d28990dd645e16386eb226d0f10c8731shiqian// The Google C++ Testing Framework (Google Test)
334b6829f0d28990dd645e16386eb226d0f10c8731shiqian//
344b6829f0d28990dd645e16386eb226d0f10c8731shiqian// This header file defines the public API for Google Test.  It should be
354b6829f0d28990dd645e16386eb226d0f10c8731shiqian// included by any test program that uses Google Test.
364b6829f0d28990dd645e16386eb226d0f10c8731shiqian//
374b6829f0d28990dd645e16386eb226d0f10c8731shiqian// IMPORTANT NOTE: Due to limitation of the C++ language, we have to
384b6829f0d28990dd645e16386eb226d0f10c8731shiqian// leave some internal implementation details in this header file.
394b6829f0d28990dd645e16386eb226d0f10c8731shiqian// They are clearly marked by comments like this:
404b6829f0d28990dd645e16386eb226d0f10c8731shiqian//
414b6829f0d28990dd645e16386eb226d0f10c8731shiqian//   // INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM.
424b6829f0d28990dd645e16386eb226d0f10c8731shiqian//
434b6829f0d28990dd645e16386eb226d0f10c8731shiqian// Such code is NOT meant to be used by a user directly, and is subject
444b6829f0d28990dd645e16386eb226d0f10c8731shiqian// to CHANGE WITHOUT NOTICE.  Therefore DO NOT DEPEND ON IT in a user
454b6829f0d28990dd645e16386eb226d0f10c8731shiqian// program!
464b6829f0d28990dd645e16386eb226d0f10c8731shiqian//
474b6829f0d28990dd645e16386eb226d0f10c8731shiqian// Acknowledgment: Google Test borrowed the idea of automatic test
484b6829f0d28990dd645e16386eb226d0f10c8731shiqian// registration from Barthelemy Dagenais' (barthelemy@prologique.com)
494b6829f0d28990dd645e16386eb226d0f10c8731shiqian// easyUnit framework.
504b6829f0d28990dd645e16386eb226d0f10c8731shiqian
514b6829f0d28990dd645e16386eb226d0f10c8731shiqian#ifndef GTEST_INCLUDE_GTEST_GTEST_H_
524b6829f0d28990dd645e16386eb226d0f10c8731shiqian#define GTEST_INCLUDE_GTEST_GTEST_H_
534b6829f0d28990dd645e16386eb226d0f10c8731shiqian
54e8ff148b4309e115da1c55089dc3b9a241a928dcshiqian#include <limits>
5593fed47dbf8e6bc3d39d3f769cb5039551747257vladlosev#include <ostream>
5693d13a8bbcb70bfd80b0d7ae2bf9aedfc06bf0cczhanyong.wan#include <vector>
5793d13a8bbcb70bfd80b0d7ae2bf9aedfc06bf0cczhanyong.wan
582620c79810d4741922e9fa89050c0af564994f24zhanyong.wan#include "gtest/internal/gtest-internal.h"
592620c79810d4741922e9fa89050c0af564994f24zhanyong.wan#include "gtest/internal/gtest-string.h"
602620c79810d4741922e9fa89050c0af564994f24zhanyong.wan#include "gtest/gtest-death-test.h"
612620c79810d4741922e9fa89050c0af564994f24zhanyong.wan#include "gtest/gtest-message.h"
622620c79810d4741922e9fa89050c0af564994f24zhanyong.wan#include "gtest/gtest-param-test.h"
632620c79810d4741922e9fa89050c0af564994f24zhanyong.wan#include "gtest/gtest-printers.h"
642620c79810d4741922e9fa89050c0af564994f24zhanyong.wan#include "gtest/gtest_prod.h"
652620c79810d4741922e9fa89050c0af564994f24zhanyong.wan#include "gtest/gtest-test-part.h"
662620c79810d4741922e9fa89050c0af564994f24zhanyong.wan#include "gtest/gtest-typed-test.h"
674b6829f0d28990dd645e16386eb226d0f10c8731shiqian
684b6829f0d28990dd645e16386eb226d0f10c8731shiqian// Depending on the platform, different string classes are available.
6947ff745210c71ba79033fb69a7f832aa18b87977zhanyong.wan// On Linux, in addition to ::std::string, Google also makes use of
7047ff745210c71ba79033fb69a7f832aa18b87977zhanyong.wan// class ::string, which has the same interface as ::std::string, but
7147ff745210c71ba79033fb69a7f832aa18b87977zhanyong.wan// has a different implementation.
7247ff745210c71ba79033fb69a7f832aa18b87977zhanyong.wan//
7347ff745210c71ba79033fb69a7f832aa18b87977zhanyong.wan// The user can define GTEST_HAS_GLOBAL_STRING to 1 to indicate that
7447ff745210c71ba79033fb69a7f832aa18b87977zhanyong.wan// ::string is available AND is a distinct type to ::std::string, or
7547ff745210c71ba79033fb69a7f832aa18b87977zhanyong.wan// define it to 0 to indicate otherwise.
764b6829f0d28990dd645e16386eb226d0f10c8731shiqian//
774b6829f0d28990dd645e16386eb226d0f10c8731shiqian// If the user's ::std::string and ::string are the same class due to
7847ff745210c71ba79033fb69a7f832aa18b87977zhanyong.wan// aliasing, he should define GTEST_HAS_GLOBAL_STRING to 0.
794b6829f0d28990dd645e16386eb226d0f10c8731shiqian//
8047ff745210c71ba79033fb69a7f832aa18b87977zhanyong.wan// If the user doesn't define GTEST_HAS_GLOBAL_STRING, it is defined
8147ff745210c71ba79033fb69a7f832aa18b87977zhanyong.wan// heuristically.
824b6829f0d28990dd645e16386eb226d0f10c8731shiqian
834b6829f0d28990dd645e16386eb226d0f10c8731shiqiannamespace testing {
844b6829f0d28990dd645e16386eb226d0f10c8731shiqian
850ebc16a68eea644a9f8a718fbdef95112a4a0ff2zhanyong.wan// Declares the flags.
864b6829f0d28990dd645e16386eb226d0f10c8731shiqian
870ebc16a68eea644a9f8a718fbdef95112a4a0ff2zhanyong.wan// This flag temporary enables the disabled tests.
880ebc16a68eea644a9f8a718fbdef95112a4a0ff2zhanyong.wanGTEST_DECLARE_bool_(also_run_disabled_tests);
890ebc16a68eea644a9f8a718fbdef95112a4a0ff2zhanyong.wan
900ebc16a68eea644a9f8a718fbdef95112a4a0ff2zhanyong.wan// This flag brings the debugger on an assertion failure.
910ebc16a68eea644a9f8a718fbdef95112a4a0ff2zhanyong.wanGTEST_DECLARE_bool_(break_on_failure);
920ebc16a68eea644a9f8a718fbdef95112a4a0ff2zhanyong.wan
93b0fe69fcbfaa44ae05f6f2429d9c4bbb326b5ea5zhanyong.wan// This flag controls whether Google Test catches all test-thrown exceptions
94b0fe69fcbfaa44ae05f6f2429d9c4bbb326b5ea5zhanyong.wan// and logs them as failures.
950ebc16a68eea644a9f8a718fbdef95112a4a0ff2zhanyong.wanGTEST_DECLARE_bool_(catch_exceptions);
960ebc16a68eea644a9f8a718fbdef95112a4a0ff2zhanyong.wan
97b0fe69fcbfaa44ae05f6f2429d9c4bbb326b5ea5zhanyong.wan// This flag enables using colors in terminal output. Available values are
98b0fe69fcbfaa44ae05f6f2429d9c4bbb326b5ea5zhanyong.wan// "yes" to enable colors, "no" (disable colors), or "auto" (the default)
990ebc16a68eea644a9f8a718fbdef95112a4a0ff2zhanyong.wan// to let Google Test decide.
1000ebc16a68eea644a9f8a718fbdef95112a4a0ff2zhanyong.wanGTEST_DECLARE_string_(color);
1010ebc16a68eea644a9f8a718fbdef95112a4a0ff2zhanyong.wan
102b0fe69fcbfaa44ae05f6f2429d9c4bbb326b5ea5zhanyong.wan// This flag sets up the filter to select by name using a glob pattern
1030ebc16a68eea644a9f8a718fbdef95112a4a0ff2zhanyong.wan// the tests to run. If the filter is not given all tests are executed.
1040ebc16a68eea644a9f8a718fbdef95112a4a0ff2zhanyong.wanGTEST_DECLARE_string_(filter);
1050ebc16a68eea644a9f8a718fbdef95112a4a0ff2zhanyong.wan
106b0fe69fcbfaa44ae05f6f2429d9c4bbb326b5ea5zhanyong.wan// This flag causes the Google Test to list tests. None of the tests listed
1070ebc16a68eea644a9f8a718fbdef95112a4a0ff2zhanyong.wan// are actually run if the flag is provided.
1080ebc16a68eea644a9f8a718fbdef95112a4a0ff2zhanyong.wanGTEST_DECLARE_bool_(list_tests);
1090ebc16a68eea644a9f8a718fbdef95112a4a0ff2zhanyong.wan
1100ebc16a68eea644a9f8a718fbdef95112a4a0ff2zhanyong.wan// This flag controls whether Google Test emits a detailed XML report to a file
1110ebc16a68eea644a9f8a718fbdef95112a4a0ff2zhanyong.wan// in addition to its normal textual output.
1120ebc16a68eea644a9f8a718fbdef95112a4a0ff2zhanyong.wanGTEST_DECLARE_string_(output);
1130ebc16a68eea644a9f8a718fbdef95112a4a0ff2zhanyong.wan
114b0fe69fcbfaa44ae05f6f2429d9c4bbb326b5ea5zhanyong.wan// This flags control whether Google Test prints the elapsed time for each
1150ebc16a68eea644a9f8a718fbdef95112a4a0ff2zhanyong.wan// test.
1160ebc16a68eea644a9f8a718fbdef95112a4a0ff2zhanyong.wanGTEST_DECLARE_bool_(print_time);
1170ebc16a68eea644a9f8a718fbdef95112a4a0ff2zhanyong.wan
1189b9794ffb9cfafb94f5def851f76b9a104fcb891zhanyong.wan// This flag specifies the random number seed.
1199b9794ffb9cfafb94f5def851f76b9a104fcb891zhanyong.wanGTEST_DECLARE_int32_(random_seed);
1209b9794ffb9cfafb94f5def851f76b9a104fcb891zhanyong.wan
121b0fe69fcbfaa44ae05f6f2429d9c4bbb326b5ea5zhanyong.wan// This flag sets how many times the tests are repeated. The default value
1220ebc16a68eea644a9f8a718fbdef95112a4a0ff2zhanyong.wan// is 1. If the value is -1 the tests are repeating forever.
1230ebc16a68eea644a9f8a718fbdef95112a4a0ff2zhanyong.wanGTEST_DECLARE_int32_(repeat);
1244b6829f0d28990dd645e16386eb226d0f10c8731shiqian
1254b6829f0d28990dd645e16386eb226d0f10c8731shiqian// This flag controls whether Google Test includes Google Test internal
1264b6829f0d28990dd645e16386eb226d0f10c8731shiqian// stack frames in failure stack traces.
127e44602ec83c65102035ce5304ae8de0cb16e9e56shiqianGTEST_DECLARE_bool_(show_internal_stack_frames);
1284b6829f0d28990dd645e16386eb226d0f10c8731shiqian
129f19450f3ad156e1dea624b4d37e645bef067ab5czhanyong.wan// When this flag is specified, tests' order is randomized on every iteration.
1309b9794ffb9cfafb94f5def851f76b9a104fcb891zhanyong.wanGTEST_DECLARE_bool_(shuffle);
1319b9794ffb9cfafb94f5def851f76b9a104fcb891zhanyong.wan
1320ebc16a68eea644a9f8a718fbdef95112a4a0ff2zhanyong.wan// This flag specifies the maximum number of stack frames to be
1330ebc16a68eea644a9f8a718fbdef95112a4a0ff2zhanyong.wan// printed in a failure message.
1340ebc16a68eea644a9f8a718fbdef95112a4a0ff2zhanyong.wanGTEST_DECLARE_int32_(stack_trace_depth);
1350ebc16a68eea644a9f8a718fbdef95112a4a0ff2zhanyong.wan
136b0fe69fcbfaa44ae05f6f2429d9c4bbb326b5ea5zhanyong.wan// When this flag is specified, a failed assertion will throw an
137b0fe69fcbfaa44ae05f6f2429d9c4bbb326b5ea5zhanyong.wan// exception if exceptions are enabled, or exit the program with a
138b0fe69fcbfaa44ae05f6f2429d9c4bbb326b5ea5zhanyong.wan// non-zero code otherwise.
139b0fe69fcbfaa44ae05f6f2429d9c4bbb326b5ea5zhanyong.wanGTEST_DECLARE_bool_(throw_on_failure);
140b0fe69fcbfaa44ae05f6f2429d9c4bbb326b5ea5zhanyong.wan
141c95489ee7dd54fc6a2cd1d3e890c330718ead714zhanyong.wan// When this flag is set with a "host:port" string, on supported
142c95489ee7dd54fc6a2cd1d3e890c330718ead714zhanyong.wan// platforms test results are streamed to the specified port on
143c95489ee7dd54fc6a2cd1d3e890c330718ead714zhanyong.wan// the specified host machine.
144c95489ee7dd54fc6a2cd1d3e890c330718ead714zhanyong.wanGTEST_DECLARE_string_(stream_result_to);
145c95489ee7dd54fc6a2cd1d3e890c330718ead714zhanyong.wan
1460ebc16a68eea644a9f8a718fbdef95112a4a0ff2zhanyong.wan// The upper limit for valid stack trace depths.
1470ebc16a68eea644a9f8a718fbdef95112a4a0ff2zhanyong.wanconst int kMaxStackTraceDepth = 100;
1480ebc16a68eea644a9f8a718fbdef95112a4a0ff2zhanyong.wan
1494b6829f0d28990dd645e16386eb226d0f10c8731shiqiannamespace internal {
1504b6829f0d28990dd645e16386eb226d0f10c8731shiqian
151a80f23f103e1a008c1df7ecc68eef9aa4ffbdc37zhanyong.wanclass AssertHelper;
152b7ec0f7b3b21338babc9a6ab5a593a40634a8062zhanyong.wanclass DefaultGlobalTestPartResultReporter;
153b7ec0f7b3b21338babc9a6ab5a593a40634a8062zhanyong.wanclass ExecDeathTest;
154f39160b423e8f90902066cf6774e4180667dcbeezhanyong.wanclass NoExecDeathTest;
1551cdc76325936ad3312be0d456b4936c8b8e6bf70zhanyong.wanclass FinalSuccessChecker;
1564b6829f0d28990dd645e16386eb226d0f10c8731shiqianclass GTestFlagSaver;
157a032cfff4091c87a137a8d24ce2eb97c740cf4c8kosak@google.comclass StreamingListenerTest;
1581cdc76325936ad3312be0d456b4936c8b8e6bf70zhanyong.wanclass TestResultAccessor;
159f6d087b78d230d875bf5d8281112662795044680zhanyong.wanclass TestEventListenersAccessor;
1607dcfb6b7375177814c5154e9770efc5406778ad0zhanyong.wanclass TestEventRepeater;
1611cdc76325936ad3312be0d456b4936c8b8e6bf70zhanyong.wanclass WindowsDeathTest;
162a80f23f103e1a008c1df7ecc68eef9aa4ffbdc37zhanyong.wanclass UnitTestImpl* GetUnitTestImpl();
163334aaead71ccf797a18f2128c320f0304e724860zhanyong.wanvoid ReportFailureInUnknownLocation(TestPartResult::Type result_type,
16403c314931649a999b0cf5deb0a434a1009157416jgm@google.com                                    const std::string& message);
1654b6829f0d28990dd645e16386eb226d0f10c8731shiqian
1664b6829f0d28990dd645e16386eb226d0f10c8731shiqian}  // namespace internal
1674b6829f0d28990dd645e16386eb226d0f10c8731shiqian
1685d0c3dc09ece41c649deea59f975d0ff5548424azhanyong.wan// The friend relationship of some of these classes is cyclic.
1695d0c3dc09ece41c649deea59f975d0ff5548424azhanyong.wan// If we don't forward declare them the compiler might confuse the classes
1705d0c3dc09ece41c649deea59f975d0ff5548424azhanyong.wan// in friendship clauses with same named classes on the scope.
1715d0c3dc09ece41c649deea59f975d0ff5548424azhanyong.wanclass Test;
1725d0c3dc09ece41c649deea59f975d0ff5548424azhanyong.wanclass TestCase;
1735d0c3dc09ece41c649deea59f975d0ff5548424azhanyong.wanclass TestInfo;
1745d0c3dc09ece41c649deea59f975d0ff5548424azhanyong.wanclass UnitTest;
1755d0c3dc09ece41c649deea59f975d0ff5548424azhanyong.wan
1764b6829f0d28990dd645e16386eb226d0f10c8731shiqian// A class for indicating whether an assertion was successful.  When
1774b6829f0d28990dd645e16386eb226d0f10c8731shiqian// the assertion wasn't successful, the AssertionResult object
178fbd53a53c1e01dec71c65754cf73282e4759bc40vladlosev// remembers a non-empty message that describes how it failed.
1794b6829f0d28990dd645e16386eb226d0f10c8731shiqian//
180fbd53a53c1e01dec71c65754cf73282e4759bc40vladlosev// To create an instance of this class, use one of the factory functions
1814b6829f0d28990dd645e16386eb226d0f10c8731shiqian// (AssertionSuccess() and AssertionFailure()).
1824b6829f0d28990dd645e16386eb226d0f10c8731shiqian//
183fbd53a53c1e01dec71c65754cf73282e4759bc40vladlosev// This class is useful for two purposes:
184fbd53a53c1e01dec71c65754cf73282e4759bc40vladlosev//   1. Defining predicate functions to be used with Boolean test assertions
185fbd53a53c1e01dec71c65754cf73282e4759bc40vladlosev//      EXPECT_TRUE/EXPECT_FALSE and their ASSERT_ counterparts
186fbd53a53c1e01dec71c65754cf73282e4759bc40vladlosev//   2. Defining predicate-format functions to be
187fbd53a53c1e01dec71c65754cf73282e4759bc40vladlosev//      used with predicate assertions (ASSERT_PRED_FORMAT*, etc).
188fbd53a53c1e01dec71c65754cf73282e4759bc40vladlosev//
189fbd53a53c1e01dec71c65754cf73282e4759bc40vladlosev// For example, if you define IsEven predicate:
190fbd53a53c1e01dec71c65754cf73282e4759bc40vladlosev//
191fbd53a53c1e01dec71c65754cf73282e4759bc40vladlosev//   testing::AssertionResult IsEven(int n) {
192fbd53a53c1e01dec71c65754cf73282e4759bc40vladlosev//     if ((n % 2) == 0)
193fbd53a53c1e01dec71c65754cf73282e4759bc40vladlosev//       return testing::AssertionSuccess();
194fbd53a53c1e01dec71c65754cf73282e4759bc40vladlosev//     else
195fbd53a53c1e01dec71c65754cf73282e4759bc40vladlosev//       return testing::AssertionFailure() << n << " is odd";
196fbd53a53c1e01dec71c65754cf73282e4759bc40vladlosev//   }
197fbd53a53c1e01dec71c65754cf73282e4759bc40vladlosev//
198fbd53a53c1e01dec71c65754cf73282e4759bc40vladlosev// Then the failed expectation EXPECT_TRUE(IsEven(Fib(5)))
199fbd53a53c1e01dec71c65754cf73282e4759bc40vladlosev// will print the message
200fbd53a53c1e01dec71c65754cf73282e4759bc40vladlosev//
201fbd53a53c1e01dec71c65754cf73282e4759bc40vladlosev//   Value of: IsEven(Fib(5))
202fbd53a53c1e01dec71c65754cf73282e4759bc40vladlosev//     Actual: false (5 is odd)
203fbd53a53c1e01dec71c65754cf73282e4759bc40vladlosev//   Expected: true
204fbd53a53c1e01dec71c65754cf73282e4759bc40vladlosev//
205fbd53a53c1e01dec71c65754cf73282e4759bc40vladlosev// instead of a more opaque
206fbd53a53c1e01dec71c65754cf73282e4759bc40vladlosev//
207fbd53a53c1e01dec71c65754cf73282e4759bc40vladlosev//   Value of: IsEven(Fib(5))
208fbd53a53c1e01dec71c65754cf73282e4759bc40vladlosev//     Actual: false
209fbd53a53c1e01dec71c65754cf73282e4759bc40vladlosev//   Expected: true
210fbd53a53c1e01dec71c65754cf73282e4759bc40vladlosev//
211fbd53a53c1e01dec71c65754cf73282e4759bc40vladlosev// in case IsEven is a simple Boolean predicate.
212fbd53a53c1e01dec71c65754cf73282e4759bc40vladlosev//
213fbd53a53c1e01dec71c65754cf73282e4759bc40vladlosev// If you expect your predicate to be reused and want to support informative
214fbd53a53c1e01dec71c65754cf73282e4759bc40vladlosev// messages in EXPECT_FALSE and ASSERT_FALSE (negative assertions show up
215fbd53a53c1e01dec71c65754cf73282e4759bc40vladlosev// about half as often as positive ones in our tests), supply messages for
216fbd53a53c1e01dec71c65754cf73282e4759bc40vladlosev// both success and failure cases:
217fbd53a53c1e01dec71c65754cf73282e4759bc40vladlosev//
218fbd53a53c1e01dec71c65754cf73282e4759bc40vladlosev//   testing::AssertionResult IsEven(int n) {
219fbd53a53c1e01dec71c65754cf73282e4759bc40vladlosev//     if ((n % 2) == 0)
220fbd53a53c1e01dec71c65754cf73282e4759bc40vladlosev//       return testing::AssertionSuccess() << n << " is even";
221fbd53a53c1e01dec71c65754cf73282e4759bc40vladlosev//     else
222fbd53a53c1e01dec71c65754cf73282e4759bc40vladlosev//       return testing::AssertionFailure() << n << " is odd";
223fbd53a53c1e01dec71c65754cf73282e4759bc40vladlosev//   }
224fbd53a53c1e01dec71c65754cf73282e4759bc40vladlosev//
225fbd53a53c1e01dec71c65754cf73282e4759bc40vladlosev// Then a statement EXPECT_FALSE(IsEven(Fib(6))) will print
226fbd53a53c1e01dec71c65754cf73282e4759bc40vladlosev//
227fbd53a53c1e01dec71c65754cf73282e4759bc40vladlosev//   Value of: IsEven(Fib(6))
228fbd53a53c1e01dec71c65754cf73282e4759bc40vladlosev//     Actual: true (8 is even)
229fbd53a53c1e01dec71c65754cf73282e4759bc40vladlosev//   Expected: false
230fbd53a53c1e01dec71c65754cf73282e4759bc40vladlosev//
231fbd53a53c1e01dec71c65754cf73282e4759bc40vladlosev// NB: Predicates that support negative Boolean assertions have reduced
232fbd53a53c1e01dec71c65754cf73282e4759bc40vladlosev// performance in positive ones so be careful not to use them in tests
233fbd53a53c1e01dec71c65754cf73282e4759bc40vladlosev// that have lots (tens of thousands) of positive Boolean assertions.
234fbd53a53c1e01dec71c65754cf73282e4759bc40vladlosev//
235fbd53a53c1e01dec71c65754cf73282e4759bc40vladlosev// To use this class with EXPECT_PRED_FORMAT assertions such as:
2364b6829f0d28990dd645e16386eb226d0f10c8731shiqian//
2374b6829f0d28990dd645e16386eb226d0f10c8731shiqian//   // Verifies that Foo() returns an even number.
2384b6829f0d28990dd645e16386eb226d0f10c8731shiqian//   EXPECT_PRED_FORMAT1(IsEven, Foo());
2394b6829f0d28990dd645e16386eb226d0f10c8731shiqian//
240fbd53a53c1e01dec71c65754cf73282e4759bc40vladlosev// you need to define:
2414b6829f0d28990dd645e16386eb226d0f10c8731shiqian//
2424b6829f0d28990dd645e16386eb226d0f10c8731shiqian//   testing::AssertionResult IsEven(const char* expr, int n) {
243fbd53a53c1e01dec71c65754cf73282e4759bc40vladlosev//     if ((n % 2) == 0)
244fbd53a53c1e01dec71c65754cf73282e4759bc40vladlosev//       return testing::AssertionSuccess();
245fbd53a53c1e01dec71c65754cf73282e4759bc40vladlosev//     else
246fbd53a53c1e01dec71c65754cf73282e4759bc40vladlosev//       return testing::AssertionFailure()
247fbd53a53c1e01dec71c65754cf73282e4759bc40vladlosev//         << "Expected: " << expr << " is even\n  Actual: it's " << n;
2484b6829f0d28990dd645e16386eb226d0f10c8731shiqian//   }
2494b6829f0d28990dd645e16386eb226d0f10c8731shiqian//
2504b6829f0d28990dd645e16386eb226d0f10c8731shiqian// If Foo() returns 5, you will see the following message:
2514b6829f0d28990dd645e16386eb226d0f10c8731shiqian//
2524b6829f0d28990dd645e16386eb226d0f10c8731shiqian//   Expected: Foo() is even
2534b6829f0d28990dd645e16386eb226d0f10c8731shiqian//     Actual: it's 5
254fbd53a53c1e01dec71c65754cf73282e4759bc40vladlosev//
255e4092294d7c1f38f303e8c7f67da31ba3e7e7d9azhanyong.wanclass GTEST_API_ AssertionResult {
2564b6829f0d28990dd645e16386eb226d0f10c8731shiqian public:
257fbd53a53c1e01dec71c65754cf73282e4759bc40vladlosev  // Copy constructor.
258fbd53a53c1e01dec71c65754cf73282e4759bc40vladlosev  // Used in EXPECT_TRUE/FALSE(assertion_result).
259fbd53a53c1e01dec71c65754cf73282e4759bc40vladlosev  AssertionResult(const AssertionResult& other);
260fbd53a53c1e01dec71c65754cf73282e4759bc40vladlosev  // Used in the EXPECT_TRUE/FALSE(bool_expression).
261fbd53a53c1e01dec71c65754cf73282e4759bc40vladlosev  explicit AssertionResult(bool success) : success_(success) {}
2624b6829f0d28990dd645e16386eb226d0f10c8731shiqian
2634b6829f0d28990dd645e16386eb226d0f10c8731shiqian  // Returns true iff the assertion succeeded.
264fbd53a53c1e01dec71c65754cf73282e4759bc40vladlosev  operator bool() const { return success_; }  // NOLINT
265fbd53a53c1e01dec71c65754cf73282e4759bc40vladlosev
266fbd53a53c1e01dec71c65754cf73282e4759bc40vladlosev  // Returns the assertion's negation. Used with EXPECT/ASSERT_FALSE.
267fbd53a53c1e01dec71c65754cf73282e4759bc40vladlosev  AssertionResult operator!() const;
268fbd53a53c1e01dec71c65754cf73282e4759bc40vladlosev
269fbd53a53c1e01dec71c65754cf73282e4759bc40vladlosev  // Returns the text streamed into this AssertionResult. Test assertions
270fbd53a53c1e01dec71c65754cf73282e4759bc40vladlosev  // use it when they fail (i.e., the predicate's outcome doesn't match the
271fbd53a53c1e01dec71c65754cf73282e4759bc40vladlosev  // assertion's expectation). When nothing has been streamed into the
272fbd53a53c1e01dec71c65754cf73282e4759bc40vladlosev  // object, returns an empty string.
273fbd53a53c1e01dec71c65754cf73282e4759bc40vladlosev  const char* message() const {
2741287840af7b9efca823ad2fecb6d905f2c4a3acevladlosev    return message_.get() != NULL ?  message_->c_str() : "";
275fbd53a53c1e01dec71c65754cf73282e4759bc40vladlosev  }
276fbd53a53c1e01dec71c65754cf73282e4759bc40vladlosev  // TODO(vladl@google.com): Remove this after making sure no clients use it.
277fbd53a53c1e01dec71c65754cf73282e4759bc40vladlosev  // Deprecated; please use message() instead.
278fbd53a53c1e01dec71c65754cf73282e4759bc40vladlosev  const char* failure_message() const { return message(); }
2794b6829f0d28990dd645e16386eb226d0f10c8731shiqian
280fbd53a53c1e01dec71c65754cf73282e4759bc40vladlosev  // Streams a custom failure message into this object.
2811287840af7b9efca823ad2fecb6d905f2c4a3acevladlosev  template <typename T> AssertionResult& operator<<(const T& value) {
2821287840af7b9efca823ad2fecb6d905f2c4a3acevladlosev    AppendMessage(Message() << value);
2831287840af7b9efca823ad2fecb6d905f2c4a3acevladlosev    return *this;
2841287840af7b9efca823ad2fecb6d905f2c4a3acevladlosev  }
2851287840af7b9efca823ad2fecb6d905f2c4a3acevladlosev
2861287840af7b9efca823ad2fecb6d905f2c4a3acevladlosev  // Allows streaming basic output manipulators such as endl or flush into
2871287840af7b9efca823ad2fecb6d905f2c4a3acevladlosev  // this object.
2881287840af7b9efca823ad2fecb6d905f2c4a3acevladlosev  AssertionResult& operator<<(
2891287840af7b9efca823ad2fecb6d905f2c4a3acevladlosev      ::std::ostream& (*basic_manipulator)(::std::ostream& stream)) {
2901287840af7b9efca823ad2fecb6d905f2c4a3acevladlosev    AppendMessage(Message() << basic_manipulator);
2911287840af7b9efca823ad2fecb6d905f2c4a3acevladlosev    return *this;
2921287840af7b9efca823ad2fecb6d905f2c4a3acevladlosev  }
2934b6829f0d28990dd645e16386eb226d0f10c8731shiqian
2944b6829f0d28990dd645e16386eb226d0f10c8731shiqian private:
2951287840af7b9efca823ad2fecb6d905f2c4a3acevladlosev  // Appends the contents of message to message_.
2961287840af7b9efca823ad2fecb6d905f2c4a3acevladlosev  void AppendMessage(const Message& a_message) {
2971287840af7b9efca823ad2fecb6d905f2c4a3acevladlosev    if (message_.get() == NULL)
2981287840af7b9efca823ad2fecb6d905f2c4a3acevladlosev      message_.reset(new ::std::string);
2991287840af7b9efca823ad2fecb6d905f2c4a3acevladlosev    message_->append(a_message.GetString().c_str());
3001287840af7b9efca823ad2fecb6d905f2c4a3acevladlosev  }
301fbd53a53c1e01dec71c65754cf73282e4759bc40vladlosev
302fbd53a53c1e01dec71c65754cf73282e4759bc40vladlosev  // Stores result of the assertion predicate.
303fbd53a53c1e01dec71c65754cf73282e4759bc40vladlosev  bool success_;
304fbd53a53c1e01dec71c65754cf73282e4759bc40vladlosev  // Stores the message describing the condition in case the expectation
305fbd53a53c1e01dec71c65754cf73282e4759bc40vladlosev  // construct is not satisfied with the predicate's outcome.
306fbd53a53c1e01dec71c65754cf73282e4759bc40vladlosev  // Referenced via a pointer to avoid taking too much stack frame space
307fbd53a53c1e01dec71c65754cf73282e4759bc40vladlosev  // with test assertions.
3081287840af7b9efca823ad2fecb6d905f2c4a3acevladlosev  internal::scoped_ptr< ::std::string> message_;
309fbd53a53c1e01dec71c65754cf73282e4759bc40vladlosev
3101287840af7b9efca823ad2fecb6d905f2c4a3acevladlosev  GTEST_DISALLOW_ASSIGN_(AssertionResult);
3111287840af7b9efca823ad2fecb6d905f2c4a3acevladlosev};
3124b6829f0d28990dd645e16386eb226d0f10c8731shiqian
3134b6829f0d28990dd645e16386eb226d0f10c8731shiqian// Makes a successful assertion result.
314e4092294d7c1f38f303e8c7f67da31ba3e7e7d9azhanyong.wanGTEST_API_ AssertionResult AssertionSuccess();
3154b6829f0d28990dd645e16386eb226d0f10c8731shiqian
316fbd53a53c1e01dec71c65754cf73282e4759bc40vladlosev// Makes a failed assertion result.
317e4092294d7c1f38f303e8c7f67da31ba3e7e7d9azhanyong.wanGTEST_API_ AssertionResult AssertionFailure();
318fbd53a53c1e01dec71c65754cf73282e4759bc40vladlosev
3194b6829f0d28990dd645e16386eb226d0f10c8731shiqian// Makes a failed assertion result with the given failure message.
320fbd53a53c1e01dec71c65754cf73282e4759bc40vladlosev// Deprecated; use AssertionFailure() << msg.
321e4092294d7c1f38f303e8c7f67da31ba3e7e7d9azhanyong.wanGTEST_API_ AssertionResult AssertionFailure(const Message& msg);
3224b6829f0d28990dd645e16386eb226d0f10c8731shiqian
3234b6829f0d28990dd645e16386eb226d0f10c8731shiqian// The abstract class that all tests inherit from.
3244b6829f0d28990dd645e16386eb226d0f10c8731shiqian//
3254b6829f0d28990dd645e16386eb226d0f10c8731shiqian// In Google Test, a unit test program contains one or many TestCases, and
3264b6829f0d28990dd645e16386eb226d0f10c8731shiqian// each TestCase contains one or many Tests.
3274b6829f0d28990dd645e16386eb226d0f10c8731shiqian//
3284b6829f0d28990dd645e16386eb226d0f10c8731shiqian// When you define a test using the TEST macro, you don't need to
3294b6829f0d28990dd645e16386eb226d0f10c8731shiqian// explicitly derive from Test - the TEST macro automatically does
3304b6829f0d28990dd645e16386eb226d0f10c8731shiqian// this for you.
3314b6829f0d28990dd645e16386eb226d0f10c8731shiqian//
3324b6829f0d28990dd645e16386eb226d0f10c8731shiqian// The only time you derive from Test is when defining a test fixture
3334b6829f0d28990dd645e16386eb226d0f10c8731shiqian// to be used a TEST_F.  For example:
3344b6829f0d28990dd645e16386eb226d0f10c8731shiqian//
3354b6829f0d28990dd645e16386eb226d0f10c8731shiqian//   class FooTest : public testing::Test {
3364b6829f0d28990dd645e16386eb226d0f10c8731shiqian//    protected:
3374b6829f0d28990dd645e16386eb226d0f10c8731shiqian//     virtual void SetUp() { ... }
3384b6829f0d28990dd645e16386eb226d0f10c8731shiqian//     virtual void TearDown() { ... }
3394b6829f0d28990dd645e16386eb226d0f10c8731shiqian//     ...
3404b6829f0d28990dd645e16386eb226d0f10c8731shiqian//   };
3414b6829f0d28990dd645e16386eb226d0f10c8731shiqian//
3424b6829f0d28990dd645e16386eb226d0f10c8731shiqian//   TEST_F(FooTest, Bar) { ... }
3434b6829f0d28990dd645e16386eb226d0f10c8731shiqian//   TEST_F(FooTest, Baz) { ... }
3444b6829f0d28990dd645e16386eb226d0f10c8731shiqian//
3454b6829f0d28990dd645e16386eb226d0f10c8731shiqian// Test is not copyable.
346e4092294d7c1f38f303e8c7f67da31ba3e7e7d9azhanyong.wanclass GTEST_API_ Test {
3474b6829f0d28990dd645e16386eb226d0f10c8731shiqian public:
348739d30448cd8477c7c8802d41d4e0d969a9490dezhanyong.wan  friend class TestInfo;
3494b6829f0d28990dd645e16386eb226d0f10c8731shiqian
3504b6829f0d28990dd645e16386eb226d0f10c8731shiqian  // Defines types for pointers to functions that set up and tear down
3514b6829f0d28990dd645e16386eb226d0f10c8731shiqian  // a test case.
352e8ff148b4309e115da1c55089dc3b9a241a928dcshiqian  typedef internal::SetUpTestCaseFunc SetUpTestCaseFunc;
353e8ff148b4309e115da1c55089dc3b9a241a928dcshiqian  typedef internal::TearDownTestCaseFunc TearDownTestCaseFunc;
3544b6829f0d28990dd645e16386eb226d0f10c8731shiqian
3554b6829f0d28990dd645e16386eb226d0f10c8731shiqian  // The d'tor is virtual as we intend to inherit from Test.
3564b6829f0d28990dd645e16386eb226d0f10c8731shiqian  virtual ~Test();
3574b6829f0d28990dd645e16386eb226d0f10c8731shiqian
358e8ff148b4309e115da1c55089dc3b9a241a928dcshiqian  // Sets up the stuff shared by all tests in this test case.
359e8ff148b4309e115da1c55089dc3b9a241a928dcshiqian  //
360e8ff148b4309e115da1c55089dc3b9a241a928dcshiqian  // Google Test will call Foo::SetUpTestCase() before running the first
361e8ff148b4309e115da1c55089dc3b9a241a928dcshiqian  // test in test case Foo.  Hence a sub-class can define its own
362e8ff148b4309e115da1c55089dc3b9a241a928dcshiqian  // SetUpTestCase() method to shadow the one defined in the super
363e8ff148b4309e115da1c55089dc3b9a241a928dcshiqian  // class.
364e8ff148b4309e115da1c55089dc3b9a241a928dcshiqian  static void SetUpTestCase() {}
365e8ff148b4309e115da1c55089dc3b9a241a928dcshiqian
366e8ff148b4309e115da1c55089dc3b9a241a928dcshiqian  // Tears down the stuff shared by all tests in this test case.
367e8ff148b4309e115da1c55089dc3b9a241a928dcshiqian  //
368e8ff148b4309e115da1c55089dc3b9a241a928dcshiqian  // Google Test will call Foo::TearDownTestCase() after running the last
369e8ff148b4309e115da1c55089dc3b9a241a928dcshiqian  // test in test case Foo.  Hence a sub-class can define its own
370e8ff148b4309e115da1c55089dc3b9a241a928dcshiqian  // TearDownTestCase() method to shadow the one defined in the super
371e8ff148b4309e115da1c55089dc3b9a241a928dcshiqian  // class.
372e8ff148b4309e115da1c55089dc3b9a241a928dcshiqian  static void TearDownTestCase() {}
373e8ff148b4309e115da1c55089dc3b9a241a928dcshiqian
3744b6829f0d28990dd645e16386eb226d0f10c8731shiqian  // Returns true iff the current test has a fatal failure.
3754b6829f0d28990dd645e16386eb226d0f10c8731shiqian  static bool HasFatalFailure();
3764b6829f0d28990dd645e16386eb226d0f10c8731shiqian
3771b171100b3341c01fb377f8202d9eaccbeec1f55zhanyong.wan  // Returns true iff the current test has a non-fatal failure.
3781b171100b3341c01fb377f8202d9eaccbeec1f55zhanyong.wan  static bool HasNonfatalFailure();
3791b171100b3341c01fb377f8202d9eaccbeec1f55zhanyong.wan
3801b171100b3341c01fb377f8202d9eaccbeec1f55zhanyong.wan  // Returns true iff the current test has a (either fatal or
3811b171100b3341c01fb377f8202d9eaccbeec1f55zhanyong.wan  // non-fatal) failure.
3821b171100b3341c01fb377f8202d9eaccbeec1f55zhanyong.wan  static bool HasFailure() { return HasFatalFailure() || HasNonfatalFailure(); }
3831b171100b3341c01fb377f8202d9eaccbeec1f55zhanyong.wan
3844b6829f0d28990dd645e16386eb226d0f10c8731shiqian  // Logs a property for the current test.  Only the last value for a given
3854b6829f0d28990dd645e16386eb226d0f10c8731shiqian  // key is remembered.
3864b6829f0d28990dd645e16386eb226d0f10c8731shiqian  // These are public static so they can be called from utility functions
3874b6829f0d28990dd645e16386eb226d0f10c8731shiqian  // that are not members of the test fixture.
3884b6829f0d28990dd645e16386eb226d0f10c8731shiqian  // The arguments are const char* instead strings, as Google Test is used
3894b6829f0d28990dd645e16386eb226d0f10c8731shiqian  // on platforms where string doesn't compile.
3904b6829f0d28990dd645e16386eb226d0f10c8731shiqian  //
3914b6829f0d28990dd645e16386eb226d0f10c8731shiqian  // Note that a driving consideration for these RecordProperty methods
3924b6829f0d28990dd645e16386eb226d0f10c8731shiqian  // was to produce xml output suited to the Greenspan charting utility,
3934b6829f0d28990dd645e16386eb226d0f10c8731shiqian  // which at present will only chart values that fit in a 32-bit int. It
3944b6829f0d28990dd645e16386eb226d0f10c8731shiqian  // is the user's responsibility to restrict their values to 32-bit ints
3954b6829f0d28990dd645e16386eb226d0f10c8731shiqian  // if they intend them to be used with Greenspan.
3964b6829f0d28990dd645e16386eb226d0f10c8731shiqian  static void RecordProperty(const char* key, const char* value);
3974b6829f0d28990dd645e16386eb226d0f10c8731shiqian  static void RecordProperty(const char* key, int value);
3984b6829f0d28990dd645e16386eb226d0f10c8731shiqian
3994b6829f0d28990dd645e16386eb226d0f10c8731shiqian protected:
4004b6829f0d28990dd645e16386eb226d0f10c8731shiqian  // Creates a Test object.
4014b6829f0d28990dd645e16386eb226d0f10c8731shiqian  Test();
4024b6829f0d28990dd645e16386eb226d0f10c8731shiqian
4034b6829f0d28990dd645e16386eb226d0f10c8731shiqian  // Sets up the test fixture.
4044b6829f0d28990dd645e16386eb226d0f10c8731shiqian  virtual void SetUp();
4054b6829f0d28990dd645e16386eb226d0f10c8731shiqian
4064b6829f0d28990dd645e16386eb226d0f10c8731shiqian  // Tears down the test fixture.
4074b6829f0d28990dd645e16386eb226d0f10c8731shiqian  virtual void TearDown();
4084b6829f0d28990dd645e16386eb226d0f10c8731shiqian
4094b6829f0d28990dd645e16386eb226d0f10c8731shiqian private:
4104b6829f0d28990dd645e16386eb226d0f10c8731shiqian  // Returns true iff the current test has the same fixture class as
4114b6829f0d28990dd645e16386eb226d0f10c8731shiqian  // the first test in the current test case.
4124b6829f0d28990dd645e16386eb226d0f10c8731shiqian  static bool HasSameFixtureClass();
4134b6829f0d28990dd645e16386eb226d0f10c8731shiqian
4144b6829f0d28990dd645e16386eb226d0f10c8731shiqian  // Runs the test after the test fixture has been set up.
4154b6829f0d28990dd645e16386eb226d0f10c8731shiqian  //
4164b6829f0d28990dd645e16386eb226d0f10c8731shiqian  // A sub-class must implement this to define the test logic.
4174b6829f0d28990dd645e16386eb226d0f10c8731shiqian  //
4184b6829f0d28990dd645e16386eb226d0f10c8731shiqian  // DO NOT OVERRIDE THIS FUNCTION DIRECTLY IN A USER PROGRAM.
4194b6829f0d28990dd645e16386eb226d0f10c8731shiqian  // Instead, use the TEST or TEST_F macro.
4204b6829f0d28990dd645e16386eb226d0f10c8731shiqian  virtual void TestBody() = 0;
4214b6829f0d28990dd645e16386eb226d0f10c8731shiqian
4224b6829f0d28990dd645e16386eb226d0f10c8731shiqian  // Sets up, executes, and tears down the test.
4234b6829f0d28990dd645e16386eb226d0f10c8731shiqian  void Run();
4244b6829f0d28990dd645e16386eb226d0f10c8731shiqian
425739d30448cd8477c7c8802d41d4e0d969a9490dezhanyong.wan  // Deletes self.  We deliberately pick an unusual name for this
426739d30448cd8477c7c8802d41d4e0d969a9490dezhanyong.wan  // internal method to avoid clashing with names used in user TESTs.
427739d30448cd8477c7c8802d41d4e0d969a9490dezhanyong.wan  void DeleteSelf_() { delete this; }
428739d30448cd8477c7c8802d41d4e0d969a9490dezhanyong.wan
4294b6829f0d28990dd645e16386eb226d0f10c8731shiqian  // Uses a GTestFlagSaver to save and restore all Google Test flags.
4304b6829f0d28990dd645e16386eb226d0f10c8731shiqian  const internal::GTestFlagSaver* const gtest_flag_saver_;
4314b6829f0d28990dd645e16386eb226d0f10c8731shiqian
4324b6829f0d28990dd645e16386eb226d0f10c8731shiqian  // Often a user mis-spells SetUp() as Setup() and spends a long time
4334b6829f0d28990dd645e16386eb226d0f10c8731shiqian  // wondering why it is never called by Google Test.  The declaration of
4344b6829f0d28990dd645e16386eb226d0f10c8731shiqian  // the following method is solely for catching such an error at
4354b6829f0d28990dd645e16386eb226d0f10c8731shiqian  // compile time:
4364b6829f0d28990dd645e16386eb226d0f10c8731shiqian  //
4374b6829f0d28990dd645e16386eb226d0f10c8731shiqian  //   - The return type is deliberately chosen to be not void, so it
4384b6829f0d28990dd645e16386eb226d0f10c8731shiqian  //   will be a conflict if a user declares void Setup() in his test
4394b6829f0d28990dd645e16386eb226d0f10c8731shiqian  //   fixture.
4404b6829f0d28990dd645e16386eb226d0f10c8731shiqian  //
4414b6829f0d28990dd645e16386eb226d0f10c8731shiqian  //   - This method is private, so it will be another compiler error
4424b6829f0d28990dd645e16386eb226d0f10c8731shiqian  //   if a user calls it from his test fixture.
4434b6829f0d28990dd645e16386eb226d0f10c8731shiqian  //
4444b6829f0d28990dd645e16386eb226d0f10c8731shiqian  // DO NOT OVERRIDE THIS FUNCTION.
4454b6829f0d28990dd645e16386eb226d0f10c8731shiqian  //
4464b6829f0d28990dd645e16386eb226d0f10c8731shiqian  // If you see an error about overriding the following function or
4474b6829f0d28990dd645e16386eb226d0f10c8731shiqian  // about it being private, you have mis-spelled SetUp() as Setup().
4484b6829f0d28990dd645e16386eb226d0f10c8731shiqian  struct Setup_should_be_spelled_SetUp {};
4494b6829f0d28990dd645e16386eb226d0f10c8731shiqian  virtual Setup_should_be_spelled_SetUp* Setup() { return NULL; }
4504b6829f0d28990dd645e16386eb226d0f10c8731shiqian
4514b6829f0d28990dd645e16386eb226d0f10c8731shiqian  // We disallow copying Tests.
452e44602ec83c65102035ce5304ae8de0cb16e9e56shiqian  GTEST_DISALLOW_COPY_AND_ASSIGN_(Test);
4534b6829f0d28990dd645e16386eb226d0f10c8731shiqian};
4544b6829f0d28990dd645e16386eb226d0f10c8731shiqian
4551cdc76325936ad3312be0d456b4936c8b8e6bf70zhanyong.wantypedef internal::TimeInMillis TimeInMillis;
4561cdc76325936ad3312be0d456b4936c8b8e6bf70zhanyong.wan
4574e7e2fcd7ac837bf81d8f120a9a30785d83b3897zhanyong.wan// A copyable object representing a user specified test property which can be
4584e7e2fcd7ac837bf81d8f120a9a30785d83b3897zhanyong.wan// output as a key/value string pair.
4594e7e2fcd7ac837bf81d8f120a9a30785d83b3897zhanyong.wan//
4604e7e2fcd7ac837bf81d8f120a9a30785d83b3897zhanyong.wan// Don't inherit from TestProperty as its destructor is not virtual.
4614e7e2fcd7ac837bf81d8f120a9a30785d83b3897zhanyong.wanclass TestProperty {
4624e7e2fcd7ac837bf81d8f120a9a30785d83b3897zhanyong.wan public:
4634e7e2fcd7ac837bf81d8f120a9a30785d83b3897zhanyong.wan  // C'tor.  TestProperty does NOT have a default constructor.
4644e7e2fcd7ac837bf81d8f120a9a30785d83b3897zhanyong.wan  // Always use this constructor (with parameters) to create a
4654e7e2fcd7ac837bf81d8f120a9a30785d83b3897zhanyong.wan  // TestProperty object.
4667de3401f5f8c2a61476a3623e9802f973764fc27zhanyong.wan  TestProperty(const char* a_key, const char* a_value) :
4677de3401f5f8c2a61476a3623e9802f973764fc27zhanyong.wan    key_(a_key), value_(a_value) {
4684e7e2fcd7ac837bf81d8f120a9a30785d83b3897zhanyong.wan  }
4694e7e2fcd7ac837bf81d8f120a9a30785d83b3897zhanyong.wan
4704e7e2fcd7ac837bf81d8f120a9a30785d83b3897zhanyong.wan  // Gets the user supplied key.
4714e7e2fcd7ac837bf81d8f120a9a30785d83b3897zhanyong.wan  const char* key() const {
4724e7e2fcd7ac837bf81d8f120a9a30785d83b3897zhanyong.wan    return key_.c_str();
4734e7e2fcd7ac837bf81d8f120a9a30785d83b3897zhanyong.wan  }
4744e7e2fcd7ac837bf81d8f120a9a30785d83b3897zhanyong.wan
4754e7e2fcd7ac837bf81d8f120a9a30785d83b3897zhanyong.wan  // Gets the user supplied value.
4764e7e2fcd7ac837bf81d8f120a9a30785d83b3897zhanyong.wan  const char* value() const {
4774e7e2fcd7ac837bf81d8f120a9a30785d83b3897zhanyong.wan    return value_.c_str();
4784e7e2fcd7ac837bf81d8f120a9a30785d83b3897zhanyong.wan  }
4794e7e2fcd7ac837bf81d8f120a9a30785d83b3897zhanyong.wan
4804e7e2fcd7ac837bf81d8f120a9a30785d83b3897zhanyong.wan  // Sets a new value, overriding the one supplied in the constructor.
4814e7e2fcd7ac837bf81d8f120a9a30785d83b3897zhanyong.wan  void SetValue(const char* new_value) {
4824e7e2fcd7ac837bf81d8f120a9a30785d83b3897zhanyong.wan    value_ = new_value;
4834e7e2fcd7ac837bf81d8f120a9a30785d83b3897zhanyong.wan  }
4844e7e2fcd7ac837bf81d8f120a9a30785d83b3897zhanyong.wan
4854e7e2fcd7ac837bf81d8f120a9a30785d83b3897zhanyong.wan private:
4864e7e2fcd7ac837bf81d8f120a9a30785d83b3897zhanyong.wan  // The key supplied by the user.
48703c314931649a999b0cf5deb0a434a1009157416jgm@google.com  std::string key_;
4884e7e2fcd7ac837bf81d8f120a9a30785d83b3897zhanyong.wan  // The value supplied by the user.
48903c314931649a999b0cf5deb0a434a1009157416jgm@google.com  std::string value_;
4904e7e2fcd7ac837bf81d8f120a9a30785d83b3897zhanyong.wan};
4914e7e2fcd7ac837bf81d8f120a9a30785d83b3897zhanyong.wan
492535de1065311cc5e95e84811dc05f513393fa940zhanyong.wan// The result of a single Test.  This includes a list of
493535de1065311cc5e95e84811dc05f513393fa940zhanyong.wan// TestPartResults, a list of TestProperties, a count of how many
494535de1065311cc5e95e84811dc05f513393fa940zhanyong.wan// death tests there are in the Test, and how much time it took to run
495535de1065311cc5e95e84811dc05f513393fa940zhanyong.wan// the Test.
496535de1065311cc5e95e84811dc05f513393fa940zhanyong.wan//
497535de1065311cc5e95e84811dc05f513393fa940zhanyong.wan// TestResult is not copyable.
498e4092294d7c1f38f303e8c7f67da31ba3e7e7d9azhanyong.wanclass GTEST_API_ TestResult {
499535de1065311cc5e95e84811dc05f513393fa940zhanyong.wan public:
500535de1065311cc5e95e84811dc05f513393fa940zhanyong.wan  // Creates an empty TestResult.
501535de1065311cc5e95e84811dc05f513393fa940zhanyong.wan  TestResult();
502535de1065311cc5e95e84811dc05f513393fa940zhanyong.wan
503535de1065311cc5e95e84811dc05f513393fa940zhanyong.wan  // D'tor.  Do not inherit from TestResult.
504535de1065311cc5e95e84811dc05f513393fa940zhanyong.wan  ~TestResult();
505535de1065311cc5e95e84811dc05f513393fa940zhanyong.wan
506535de1065311cc5e95e84811dc05f513393fa940zhanyong.wan  // Gets the number of all test parts.  This is the sum of the number
507535de1065311cc5e95e84811dc05f513393fa940zhanyong.wan  // of successful test parts and the number of failed test parts.
508535de1065311cc5e95e84811dc05f513393fa940zhanyong.wan  int total_part_count() const;
509535de1065311cc5e95e84811dc05f513393fa940zhanyong.wan
5109644db857432db95bc64f5d6db5867b1c7cf1679zhanyong.wan  // Returns the number of the test properties.
5119644db857432db95bc64f5d6db5867b1c7cf1679zhanyong.wan  int test_property_count() const;
5129644db857432db95bc64f5d6db5867b1c7cf1679zhanyong.wan
513535de1065311cc5e95e84811dc05f513393fa940zhanyong.wan  // Returns true iff the test passed (i.e. no test part failed).
514535de1065311cc5e95e84811dc05f513393fa940zhanyong.wan  bool Passed() const { return !Failed(); }
515535de1065311cc5e95e84811dc05f513393fa940zhanyong.wan
516535de1065311cc5e95e84811dc05f513393fa940zhanyong.wan  // Returns true iff the test failed.
5171cdc76325936ad3312be0d456b4936c8b8e6bf70zhanyong.wan  bool Failed() const;
518535de1065311cc5e95e84811dc05f513393fa940zhanyong.wan
519535de1065311cc5e95e84811dc05f513393fa940zhanyong.wan  // Returns true iff the test fatally failed.
520535de1065311cc5e95e84811dc05f513393fa940zhanyong.wan  bool HasFatalFailure() const;
521535de1065311cc5e95e84811dc05f513393fa940zhanyong.wan
522535de1065311cc5e95e84811dc05f513393fa940zhanyong.wan  // Returns true iff the test has a non-fatal failure.
523535de1065311cc5e95e84811dc05f513393fa940zhanyong.wan  bool HasNonfatalFailure() const;
524535de1065311cc5e95e84811dc05f513393fa940zhanyong.wan
525535de1065311cc5e95e84811dc05f513393fa940zhanyong.wan  // Returns the elapsed time, in milliseconds.
526535de1065311cc5e95e84811dc05f513393fa940zhanyong.wan  TimeInMillis elapsed_time() const { return elapsed_time_; }
527535de1065311cc5e95e84811dc05f513393fa940zhanyong.wan
5289644db857432db95bc64f5d6db5867b1c7cf1679zhanyong.wan  // Returns the i-th test part result among all the results. i can range
529449f84de9b0c48289e20f63c6d08a39bcee2021bzhanyong.wan  // from 0 to test_property_count() - 1. If i is not in that range, aborts
530449f84de9b0c48289e20f63c6d08a39bcee2021bzhanyong.wan  // the program.
531449f84de9b0c48289e20f63c6d08a39bcee2021bzhanyong.wan  const TestPartResult& GetTestPartResult(int i) const;
5329644db857432db95bc64f5d6db5867b1c7cf1679zhanyong.wan
5339644db857432db95bc64f5d6db5867b1c7cf1679zhanyong.wan  // Returns the i-th test property. i can range from 0 to
534449f84de9b0c48289e20f63c6d08a39bcee2021bzhanyong.wan  // test_property_count() - 1. If i is not in that range, aborts the
535449f84de9b0c48289e20f63c6d08a39bcee2021bzhanyong.wan  // program.
536449f84de9b0c48289e20f63c6d08a39bcee2021bzhanyong.wan  const TestProperty& GetTestProperty(int i) const;
5379644db857432db95bc64f5d6db5867b1c7cf1679zhanyong.wan
538b7ec0f7b3b21338babc9a6ab5a593a40634a8062zhanyong.wan private:
539fff033497b70e96a5dcadb6ba9570c12b5921d74zhanyong.wan  friend class TestInfo;
540fff033497b70e96a5dcadb6ba9570c12b5921d74zhanyong.wan  friend class UnitTest;
5411cdc76325936ad3312be0d456b4936c8b8e6bf70zhanyong.wan  friend class internal::DefaultGlobalTestPartResultReporter;
5421cdc76325936ad3312be0d456b4936c8b8e6bf70zhanyong.wan  friend class internal::ExecDeathTest;
5431cdc76325936ad3312be0d456b4936c8b8e6bf70zhanyong.wan  friend class internal::TestResultAccessor;
5441cdc76325936ad3312be0d456b4936c8b8e6bf70zhanyong.wan  friend class internal::UnitTestImpl;
5451cdc76325936ad3312be0d456b4936c8b8e6bf70zhanyong.wan  friend class internal::WindowsDeathTest;
546b7ec0f7b3b21338babc9a6ab5a593a40634a8062zhanyong.wan
547a8a582fb926a678d1c4303384bd4dc872f746de8zhanyong.wan  // Gets the vector of TestPartResults.
54893d13a8bbcb70bfd80b0d7ae2bf9aedfc06bf0cczhanyong.wan  const std::vector<TestPartResult>& test_part_results() const {
54993d13a8bbcb70bfd80b0d7ae2bf9aedfc06bf0cczhanyong.wan    return test_part_results_;
550b7ec0f7b3b21338babc9a6ab5a593a40634a8062zhanyong.wan  }
551b7ec0f7b3b21338babc9a6ab5a593a40634a8062zhanyong.wan
552a8a582fb926a678d1c4303384bd4dc872f746de8zhanyong.wan  // Gets the vector of TestProperties.
55393d13a8bbcb70bfd80b0d7ae2bf9aedfc06bf0cczhanyong.wan  const std::vector<TestProperty>& test_properties() const {
55493d13a8bbcb70bfd80b0d7ae2bf9aedfc06bf0cczhanyong.wan    return test_properties_;
555b7ec0f7b3b21338babc9a6ab5a593a40634a8062zhanyong.wan  }
556b7ec0f7b3b21338babc9a6ab5a593a40634a8062zhanyong.wan
557b7ec0f7b3b21338babc9a6ab5a593a40634a8062zhanyong.wan  // Sets the elapsed time.
558b7ec0f7b3b21338babc9a6ab5a593a40634a8062zhanyong.wan  void set_elapsed_time(TimeInMillis elapsed) { elapsed_time_ = elapsed; }
559535de1065311cc5e95e84811dc05f513393fa940zhanyong.wan
560535de1065311cc5e95e84811dc05f513393fa940zhanyong.wan  // Adds a test property to the list. The property is validated and may add
561535de1065311cc5e95e84811dc05f513393fa940zhanyong.wan  // a non-fatal failure if invalid (e.g., if it conflicts with reserved
562535de1065311cc5e95e84811dc05f513393fa940zhanyong.wan  // key names). If a property is already recorded for the same key, the
563535de1065311cc5e95e84811dc05f513393fa940zhanyong.wan  // value will be updated, rather than storing multiple values for the same
564535de1065311cc5e95e84811dc05f513393fa940zhanyong.wan  // key.
5651cdc76325936ad3312be0d456b4936c8b8e6bf70zhanyong.wan  void RecordProperty(const TestProperty& test_property);
566535de1065311cc5e95e84811dc05f513393fa940zhanyong.wan
567535de1065311cc5e95e84811dc05f513393fa940zhanyong.wan  // Adds a failure if the key is a reserved attribute of Google Test
568535de1065311cc5e95e84811dc05f513393fa940zhanyong.wan  // testcase tags.  Returns true if the property is valid.
569535de1065311cc5e95e84811dc05f513393fa940zhanyong.wan  // TODO(russr): Validate attribute names are legal and human readable.
5701cdc76325936ad3312be0d456b4936c8b8e6bf70zhanyong.wan  static bool ValidateTestProperty(const TestProperty& test_property);
571535de1065311cc5e95e84811dc05f513393fa940zhanyong.wan
572b7ec0f7b3b21338babc9a6ab5a593a40634a8062zhanyong.wan  // Adds a test part result to the list.
573b7ec0f7b3b21338babc9a6ab5a593a40634a8062zhanyong.wan  void AddTestPartResult(const TestPartResult& test_part_result);
574b7ec0f7b3b21338babc9a6ab5a593a40634a8062zhanyong.wan
575535de1065311cc5e95e84811dc05f513393fa940zhanyong.wan  // Returns the death test count.
576535de1065311cc5e95e84811dc05f513393fa940zhanyong.wan  int death_test_count() const { return death_test_count_; }
577535de1065311cc5e95e84811dc05f513393fa940zhanyong.wan
578535de1065311cc5e95e84811dc05f513393fa940zhanyong.wan  // Increments the death test count, returning the new count.
579535de1065311cc5e95e84811dc05f513393fa940zhanyong.wan  int increment_death_test_count() { return ++death_test_count_; }
580535de1065311cc5e95e84811dc05f513393fa940zhanyong.wan
581535de1065311cc5e95e84811dc05f513393fa940zhanyong.wan  // Clears the test part results.
582535de1065311cc5e95e84811dc05f513393fa940zhanyong.wan  void ClearTestPartResults();
583535de1065311cc5e95e84811dc05f513393fa940zhanyong.wan
584535de1065311cc5e95e84811dc05f513393fa940zhanyong.wan  // Clears the object.
585535de1065311cc5e95e84811dc05f513393fa940zhanyong.wan  void Clear();
586b7ec0f7b3b21338babc9a6ab5a593a40634a8062zhanyong.wan
587a8a582fb926a678d1c4303384bd4dc872f746de8zhanyong.wan  // Protects mutable state of the property vector and of owned
588a8a582fb926a678d1c4303384bd4dc872f746de8zhanyong.wan  // properties, whose values may be updated.
589535de1065311cc5e95e84811dc05f513393fa940zhanyong.wan  internal::Mutex test_properites_mutex_;
590535de1065311cc5e95e84811dc05f513393fa940zhanyong.wan
591a8a582fb926a678d1c4303384bd4dc872f746de8zhanyong.wan  // The vector of TestPartResults
59293d13a8bbcb70bfd80b0d7ae2bf9aedfc06bf0cczhanyong.wan  std::vector<TestPartResult> test_part_results_;
593a8a582fb926a678d1c4303384bd4dc872f746de8zhanyong.wan  // The vector of TestProperties
59493d13a8bbcb70bfd80b0d7ae2bf9aedfc06bf0cczhanyong.wan  std::vector<TestProperty> test_properties_;
595535de1065311cc5e95e84811dc05f513393fa940zhanyong.wan  // Running count of death tests.
596535de1065311cc5e95e84811dc05f513393fa940zhanyong.wan  int death_test_count_;
597535de1065311cc5e95e84811dc05f513393fa940zhanyong.wan  // The elapsed time, in milliseconds.
598535de1065311cc5e95e84811dc05f513393fa940zhanyong.wan  TimeInMillis elapsed_time_;
599535de1065311cc5e95e84811dc05f513393fa940zhanyong.wan
600535de1065311cc5e95e84811dc05f513393fa940zhanyong.wan  // We disallow copying TestResult.
601535de1065311cc5e95e84811dc05f513393fa940zhanyong.wan  GTEST_DISALLOW_COPY_AND_ASSIGN_(TestResult);
602535de1065311cc5e95e84811dc05f513393fa940zhanyong.wan};  // class TestResult
603535de1065311cc5e95e84811dc05f513393fa940zhanyong.wan
6044b6829f0d28990dd645e16386eb226d0f10c8731shiqian// A TestInfo object stores the following information about a test:
6054b6829f0d28990dd645e16386eb226d0f10c8731shiqian//
6064b6829f0d28990dd645e16386eb226d0f10c8731shiqian//   Test case name
6074b6829f0d28990dd645e16386eb226d0f10c8731shiqian//   Test name
6084b6829f0d28990dd645e16386eb226d0f10c8731shiqian//   Whether the test should be run
6094b6829f0d28990dd645e16386eb226d0f10c8731shiqian//   A function pointer that creates the test object when invoked
6104b6829f0d28990dd645e16386eb226d0f10c8731shiqian//   Test result
6114b6829f0d28990dd645e16386eb226d0f10c8731shiqian//
6124b6829f0d28990dd645e16386eb226d0f10c8731shiqian// The constructor of TestInfo registers itself with the UnitTest
6134b6829f0d28990dd645e16386eb226d0f10c8731shiqian// singleton such that the RUN_ALL_TESTS() macro knows which tests to
6144b6829f0d28990dd645e16386eb226d0f10c8731shiqian// run.
615e4092294d7c1f38f303e8c7f67da31ba3e7e7d9azhanyong.wanclass GTEST_API_ TestInfo {
6164b6829f0d28990dd645e16386eb226d0f10c8731shiqian public:
6174b6829f0d28990dd645e16386eb226d0f10c8731shiqian  // Destructs a TestInfo object.  This function is not virtual, so
6184b6829f0d28990dd645e16386eb226d0f10c8731shiqian  // don't inherit from TestInfo.
6194b6829f0d28990dd645e16386eb226d0f10c8731shiqian  ~TestInfo();
6204b6829f0d28990dd645e16386eb226d0f10c8731shiqian
6214b6829f0d28990dd645e16386eb226d0f10c8731shiqian  // Returns the test case name.
622739d30448cd8477c7c8802d41d4e0d969a9490dezhanyong.wan  const char* test_case_name() const { return test_case_name_.c_str(); }
6234b6829f0d28990dd645e16386eb226d0f10c8731shiqian
6244b6829f0d28990dd645e16386eb226d0f10c8731shiqian  // Returns the test name.
625739d30448cd8477c7c8802d41d4e0d969a9490dezhanyong.wan  const char* name() const { return name_.c_str(); }
6264b6829f0d28990dd645e16386eb226d0f10c8731shiqian
627a33163a3ddbb60a6c45340e436310f78044c1a7dzhanyong.wan  // Returns the name of the parameter type, or NULL if this is not a typed
628a33163a3ddbb60a6c45340e436310f78044c1a7dzhanyong.wan  // or a type-parameterized test.
629a33163a3ddbb60a6c45340e436310f78044c1a7dzhanyong.wan  const char* type_param() const {
630a33163a3ddbb60a6c45340e436310f78044c1a7dzhanyong.wan    if (type_param_.get() != NULL)
631a33163a3ddbb60a6c45340e436310f78044c1a7dzhanyong.wan      return type_param_->c_str();
632a33163a3ddbb60a6c45340e436310f78044c1a7dzhanyong.wan    return NULL;
633a33163a3ddbb60a6c45340e436310f78044c1a7dzhanyong.wan  }
634e8ff148b4309e115da1c55089dc3b9a241a928dcshiqian
635a33163a3ddbb60a6c45340e436310f78044c1a7dzhanyong.wan  // Returns the text representation of the value parameter, or NULL if this
636a33163a3ddbb60a6c45340e436310f78044c1a7dzhanyong.wan  // is not a value-parameterized test.
637a33163a3ddbb60a6c45340e436310f78044c1a7dzhanyong.wan  const char* value_param() const {
638a33163a3ddbb60a6c45340e436310f78044c1a7dzhanyong.wan    if (value_param_.get() != NULL)
639a33163a3ddbb60a6c45340e436310f78044c1a7dzhanyong.wan      return value_param_->c_str();
640a33163a3ddbb60a6c45340e436310f78044c1a7dzhanyong.wan    return NULL;
641a33163a3ddbb60a6c45340e436310f78044c1a7dzhanyong.wan  }
642e8ff148b4309e115da1c55089dc3b9a241a928dcshiqian
64393a87b3a4d608dd0439b1f6c909520be197c88efzhanyong.wan  // Returns true if this test should run, that is if the test is not disabled
64493a87b3a4d608dd0439b1f6c909520be197c88efzhanyong.wan  // (or it is disabled but the also_run_disabled_tests flag has been specified)
64593a87b3a4d608dd0439b1f6c909520be197c88efzhanyong.wan  // and its full name matches the user-specified filter.
6464b6829f0d28990dd645e16386eb226d0f10c8731shiqian  //
6474b6829f0d28990dd645e16386eb226d0f10c8731shiqian  // Google Test allows the user to filter the tests by their full names.
6484b6829f0d28990dd645e16386eb226d0f10c8731shiqian  // The full name of a test Bar in test case Foo is defined as
6494b6829f0d28990dd645e16386eb226d0f10c8731shiqian  // "Foo.Bar".  Only the tests that match the filter will run.
6504b6829f0d28990dd645e16386eb226d0f10c8731shiqian  //
6514b6829f0d28990dd645e16386eb226d0f10c8731shiqian  // A filter is a colon-separated list of glob (not regex) patterns,
6524b6829f0d28990dd645e16386eb226d0f10c8731shiqian  // optionally followed by a '-' and a colon-separated list of
6534b6829f0d28990dd645e16386eb226d0f10c8731shiqian  // negative patterns (tests to exclude).  A test is run if it
6544b6829f0d28990dd645e16386eb226d0f10c8731shiqian  // matches one of the positive patterns and does not match any of
6554b6829f0d28990dd645e16386eb226d0f10c8731shiqian  // the negative patterns.
6564b6829f0d28990dd645e16386eb226d0f10c8731shiqian  //
6574b6829f0d28990dd645e16386eb226d0f10c8731shiqian  // For example, *A*:Foo.* is a filter that matches any string that
6584b6829f0d28990dd645e16386eb226d0f10c8731shiqian  // contains the character 'A' or starts with "Foo.".
659739d30448cd8477c7c8802d41d4e0d969a9490dezhanyong.wan  bool should_run() const { return should_run_; }
6604b6829f0d28990dd645e16386eb226d0f10c8731shiqian
6614b6829f0d28990dd645e16386eb226d0f10c8731shiqian  // Returns the result of the test.
662739d30448cd8477c7c8802d41d4e0d969a9490dezhanyong.wan  const TestResult* result() const { return &result_; }
663b7ec0f7b3b21338babc9a6ab5a593a40634a8062zhanyong.wan
6644b6829f0d28990dd645e16386eb226d0f10c8731shiqian private:
6654cd62602913a032a7aec091d4c8055ff9af95e37zhanyong.wan#if GTEST_HAS_DEATH_TEST
6664b6829f0d28990dd645e16386eb226d0f10c8731shiqian  friend class internal::DefaultDeathTestFactory;
6674b6829f0d28990dd645e16386eb226d0f10c8731shiqian#endif  // GTEST_HAS_DEATH_TEST
668fff033497b70e96a5dcadb6ba9570c12b5921d74zhanyong.wan  friend class Test;
669fff033497b70e96a5dcadb6ba9570c12b5921d74zhanyong.wan  friend class TestCase;
6704b6829f0d28990dd645e16386eb226d0f10c8731shiqian  friend class internal::UnitTestImpl;
671a032cfff4091c87a137a8d24ce2eb97c740cf4c8kosak@google.com  friend class internal::StreamingListenerTest;
672e8ff148b4309e115da1c55089dc3b9a241a928dcshiqian  friend TestInfo* internal::MakeAndRegisterTestInfo(
6733b8388d9490a64a47ed8cbdab2021935b1b2c76ckosak@google.com      const char* test_case_name,
6743b8388d9490a64a47ed8cbdab2021935b1b2c76ckosak@google.com      const char* name,
675a33163a3ddbb60a6c45340e436310f78044c1a7dzhanyong.wan      const char* type_param,
676a33163a3ddbb60a6c45340e436310f78044c1a7dzhanyong.wan      const char* value_param,
677e8ff148b4309e115da1c55089dc3b9a241a928dcshiqian      internal::TypeId fixture_class_id,
678e8ff148b4309e115da1c55089dc3b9a241a928dcshiqian      Test::SetUpTestCaseFunc set_up_tc,
679e8ff148b4309e115da1c55089dc3b9a241a928dcshiqian      Test::TearDownTestCaseFunc tear_down_tc,
680e8ff148b4309e115da1c55089dc3b9a241a928dcshiqian      internal::TestFactoryBase* factory);
6814b6829f0d28990dd645e16386eb226d0f10c8731shiqian
6824834581321d60c17997d65a2360c7674f15f9bbcshiqian  // Constructs a TestInfo object. The newly constructed instance assumes
6834834581321d60c17997d65a2360c7674f15f9bbcshiqian  // ownership of the factory object.
6843b8388d9490a64a47ed8cbdab2021935b1b2c76ckosak@google.com  TestInfo(const std::string& test_case_name,
6853b8388d9490a64a47ed8cbdab2021935b1b2c76ckosak@google.com           const std::string& name,
6863b8388d9490a64a47ed8cbdab2021935b1b2c76ckosak@google.com           const char* a_type_param,   // NULL if not a type-parameterized test
6873b8388d9490a64a47ed8cbdab2021935b1b2c76ckosak@google.com           const char* a_value_param,  // NULL if not a value-parameterized test
6884834581321d60c17997d65a2360c7674f15f9bbcshiqian           internal::TypeId fixture_class_id,
6894834581321d60c17997d65a2360c7674f15f9bbcshiqian           internal::TestFactoryBase* factory);
6904b6829f0d28990dd645e16386eb226d0f10c8731shiqian
691739d30448cd8477c7c8802d41d4e0d969a9490dezhanyong.wan  // Increments the number of death tests encountered in this test so
692739d30448cd8477c7c8802d41d4e0d969a9490dezhanyong.wan  // far.
693739d30448cd8477c7c8802d41d4e0d969a9490dezhanyong.wan  int increment_death_test_count() {
694739d30448cd8477c7c8802d41d4e0d969a9490dezhanyong.wan    return result_.increment_death_test_count();
695739d30448cd8477c7c8802d41d4e0d969a9490dezhanyong.wan  }
696739d30448cd8477c7c8802d41d4e0d969a9490dezhanyong.wan
697739d30448cd8477c7c8802d41d4e0d969a9490dezhanyong.wan  // Creates the test object, runs it, records its result, and then
698739d30448cd8477c7c8802d41d4e0d969a9490dezhanyong.wan  // deletes it.
699739d30448cd8477c7c8802d41d4e0d969a9490dezhanyong.wan  void Run();
700739d30448cd8477c7c8802d41d4e0d969a9490dezhanyong.wan
701739d30448cd8477c7c8802d41d4e0d969a9490dezhanyong.wan  static void ClearTestResult(TestInfo* test_info) {
702739d30448cd8477c7c8802d41d4e0d969a9490dezhanyong.wan    test_info->result_.Clear();
703739d30448cd8477c7c8802d41d4e0d969a9490dezhanyong.wan  }
704739d30448cd8477c7c8802d41d4e0d969a9490dezhanyong.wan
705739d30448cd8477c7c8802d41d4e0d969a9490dezhanyong.wan  // These fields are immutable properties of the test.
706739d30448cd8477c7c8802d41d4e0d969a9490dezhanyong.wan  const std::string test_case_name_;     // Test case name
707739d30448cd8477c7c8802d41d4e0d969a9490dezhanyong.wan  const std::string name_;               // Test name
708a33163a3ddbb60a6c45340e436310f78044c1a7dzhanyong.wan  // Name of the parameter type, or NULL if this is not a typed or a
709a33163a3ddbb60a6c45340e436310f78044c1a7dzhanyong.wan  // type-parameterized test.
710a33163a3ddbb60a6c45340e436310f78044c1a7dzhanyong.wan  const internal::scoped_ptr<const ::std::string> type_param_;
711a33163a3ddbb60a6c45340e436310f78044c1a7dzhanyong.wan  // Text representation of the value parameter, or NULL if this is not a
712a33163a3ddbb60a6c45340e436310f78044c1a7dzhanyong.wan  // value-parameterized test.
713a33163a3ddbb60a6c45340e436310f78044c1a7dzhanyong.wan  const internal::scoped_ptr<const ::std::string> value_param_;
714739d30448cd8477c7c8802d41d4e0d969a9490dezhanyong.wan  const internal::TypeId fixture_class_id_;   // ID of the test fixture class
715739d30448cd8477c7c8802d41d4e0d969a9490dezhanyong.wan  bool should_run_;                 // True iff this test should run
716739d30448cd8477c7c8802d41d4e0d969a9490dezhanyong.wan  bool is_disabled_;                // True iff this test is disabled
717739d30448cd8477c7c8802d41d4e0d969a9490dezhanyong.wan  bool matches_filter_;             // True if this test matches the
718739d30448cd8477c7c8802d41d4e0d969a9490dezhanyong.wan                                    // user-specified filter.
719739d30448cd8477c7c8802d41d4e0d969a9490dezhanyong.wan  internal::TestFactoryBase* const factory_;  // The factory that creates
720739d30448cd8477c7c8802d41d4e0d969a9490dezhanyong.wan                                              // the test object
721739d30448cd8477c7c8802d41d4e0d969a9490dezhanyong.wan
722739d30448cd8477c7c8802d41d4e0d969a9490dezhanyong.wan  // This field is mutable and needs to be reset before running the
723739d30448cd8477c7c8802d41d4e0d969a9490dezhanyong.wan  // test for the second time.
724739d30448cd8477c7c8802d41d4e0d969a9490dezhanyong.wan  TestResult result_;
7254b6829f0d28990dd645e16386eb226d0f10c8731shiqian
726e44602ec83c65102035ce5304ae8de0cb16e9e56shiqian  GTEST_DISALLOW_COPY_AND_ASSIGN_(TestInfo);
7274b6829f0d28990dd645e16386eb226d0f10c8731shiqian};
7284b6829f0d28990dd645e16386eb226d0f10c8731shiqian
729a8a582fb926a678d1c4303384bd4dc872f746de8zhanyong.wan// A test case, which consists of a vector of TestInfos.
730b9a1628577f7ab2eb30a656aeff763909770c9c1zhanyong.wan//
731b9a1628577f7ab2eb30a656aeff763909770c9c1zhanyong.wan// TestCase is not copyable.
732e4092294d7c1f38f303e8c7f67da31ba3e7e7d9azhanyong.wanclass GTEST_API_ TestCase {
733b9a1628577f7ab2eb30a656aeff763909770c9c1zhanyong.wan public:
734b9a1628577f7ab2eb30a656aeff763909770c9c1zhanyong.wan  // Creates a TestCase with the given name.
735b9a1628577f7ab2eb30a656aeff763909770c9c1zhanyong.wan  //
736b9a1628577f7ab2eb30a656aeff763909770c9c1zhanyong.wan  // TestCase does NOT have a default constructor.  Always use this
737b9a1628577f7ab2eb30a656aeff763909770c9c1zhanyong.wan  // constructor to create a TestCase object.
738b9a1628577f7ab2eb30a656aeff763909770c9c1zhanyong.wan  //
739b9a1628577f7ab2eb30a656aeff763909770c9c1zhanyong.wan  // Arguments:
740b9a1628577f7ab2eb30a656aeff763909770c9c1zhanyong.wan  //
741b9a1628577f7ab2eb30a656aeff763909770c9c1zhanyong.wan  //   name:         name of the test case
742a33163a3ddbb60a6c45340e436310f78044c1a7dzhanyong.wan  //   a_type_param: the name of the test's type parameter, or NULL if
743a33163a3ddbb60a6c45340e436310f78044c1a7dzhanyong.wan  //                 this is not a type-parameterized test.
744b9a1628577f7ab2eb30a656aeff763909770c9c1zhanyong.wan  //   set_up_tc:    pointer to the function that sets up the test case
745b9a1628577f7ab2eb30a656aeff763909770c9c1zhanyong.wan  //   tear_down_tc: pointer to the function that tears down the test case
746a33163a3ddbb60a6c45340e436310f78044c1a7dzhanyong.wan  TestCase(const char* name, const char* a_type_param,
747b9a1628577f7ab2eb30a656aeff763909770c9c1zhanyong.wan           Test::SetUpTestCaseFunc set_up_tc,
748b9a1628577f7ab2eb30a656aeff763909770c9c1zhanyong.wan           Test::TearDownTestCaseFunc tear_down_tc);
749b9a1628577f7ab2eb30a656aeff763909770c9c1zhanyong.wan
750b9a1628577f7ab2eb30a656aeff763909770c9c1zhanyong.wan  // Destructor of TestCase.
751b9a1628577f7ab2eb30a656aeff763909770c9c1zhanyong.wan  virtual ~TestCase();
752b9a1628577f7ab2eb30a656aeff763909770c9c1zhanyong.wan
753b9a1628577f7ab2eb30a656aeff763909770c9c1zhanyong.wan  // Gets the name of the TestCase.
754b9a1628577f7ab2eb30a656aeff763909770c9c1zhanyong.wan  const char* name() const { return name_.c_str(); }
755b9a1628577f7ab2eb30a656aeff763909770c9c1zhanyong.wan
756a33163a3ddbb60a6c45340e436310f78044c1a7dzhanyong.wan  // Returns the name of the parameter type, or NULL if this is not a
757a33163a3ddbb60a6c45340e436310f78044c1a7dzhanyong.wan  // type-parameterized test case.
758a33163a3ddbb60a6c45340e436310f78044c1a7dzhanyong.wan  const char* type_param() const {
759a33163a3ddbb60a6c45340e436310f78044c1a7dzhanyong.wan    if (type_param_.get() != NULL)
760a33163a3ddbb60a6c45340e436310f78044c1a7dzhanyong.wan      return type_param_->c_str();
761a33163a3ddbb60a6c45340e436310f78044c1a7dzhanyong.wan    return NULL;
762a33163a3ddbb60a6c45340e436310f78044c1a7dzhanyong.wan  }
763b9a1628577f7ab2eb30a656aeff763909770c9c1zhanyong.wan
764b9a1628577f7ab2eb30a656aeff763909770c9c1zhanyong.wan  // Returns true if any test in this test case should run.
765b9a1628577f7ab2eb30a656aeff763909770c9c1zhanyong.wan  bool should_run() const { return should_run_; }
766b9a1628577f7ab2eb30a656aeff763909770c9c1zhanyong.wan
767b9a1628577f7ab2eb30a656aeff763909770c9c1zhanyong.wan  // Gets the number of successful tests in this test case.
768b9a1628577f7ab2eb30a656aeff763909770c9c1zhanyong.wan  int successful_test_count() const;
769b9a1628577f7ab2eb30a656aeff763909770c9c1zhanyong.wan
770b9a1628577f7ab2eb30a656aeff763909770c9c1zhanyong.wan  // Gets the number of failed tests in this test case.
771b9a1628577f7ab2eb30a656aeff763909770c9c1zhanyong.wan  int failed_test_count() const;
772b9a1628577f7ab2eb30a656aeff763909770c9c1zhanyong.wan
773b9a1628577f7ab2eb30a656aeff763909770c9c1zhanyong.wan  // Gets the number of disabled tests in this test case.
774b9a1628577f7ab2eb30a656aeff763909770c9c1zhanyong.wan  int disabled_test_count() const;
775b9a1628577f7ab2eb30a656aeff763909770c9c1zhanyong.wan
776b9a1628577f7ab2eb30a656aeff763909770c9c1zhanyong.wan  // Get the number of tests in this test case that should run.
777b9a1628577f7ab2eb30a656aeff763909770c9c1zhanyong.wan  int test_to_run_count() const;
778b9a1628577f7ab2eb30a656aeff763909770c9c1zhanyong.wan
779b9a1628577f7ab2eb30a656aeff763909770c9c1zhanyong.wan  // Gets the number of all tests in this test case.
780b9a1628577f7ab2eb30a656aeff763909770c9c1zhanyong.wan  int total_test_count() const;
781b9a1628577f7ab2eb30a656aeff763909770c9c1zhanyong.wan
782b9a1628577f7ab2eb30a656aeff763909770c9c1zhanyong.wan  // Returns true iff the test case passed.
783b9a1628577f7ab2eb30a656aeff763909770c9c1zhanyong.wan  bool Passed() const { return !Failed(); }
784b9a1628577f7ab2eb30a656aeff763909770c9c1zhanyong.wan
785b9a1628577f7ab2eb30a656aeff763909770c9c1zhanyong.wan  // Returns true iff the test case failed.
786b9a1628577f7ab2eb30a656aeff763909770c9c1zhanyong.wan  bool Failed() const { return failed_test_count() > 0; }
787b9a1628577f7ab2eb30a656aeff763909770c9c1zhanyong.wan
788b9a1628577f7ab2eb30a656aeff763909770c9c1zhanyong.wan  // Returns the elapsed time, in milliseconds.
7891cdc76325936ad3312be0d456b4936c8b8e6bf70zhanyong.wan  TimeInMillis elapsed_time() const { return elapsed_time_; }
790b9a1628577f7ab2eb30a656aeff763909770c9c1zhanyong.wan
7919644db857432db95bc64f5d6db5867b1c7cf1679zhanyong.wan  // Returns the i-th test among all the tests. i can range from 0 to
7929644db857432db95bc64f5d6db5867b1c7cf1679zhanyong.wan  // total_test_count() - 1. If i is not in that range, returns NULL.
7939644db857432db95bc64f5d6db5867b1c7cf1679zhanyong.wan  const TestInfo* GetTestInfo(int i) const;
7949644db857432db95bc64f5d6db5867b1c7cf1679zhanyong.wan
795b7ec0f7b3b21338babc9a6ab5a593a40634a8062zhanyong.wan private:
796fff033497b70e96a5dcadb6ba9570c12b5921d74zhanyong.wan  friend class Test;
7971cdc76325936ad3312be0d456b4936c8b8e6bf70zhanyong.wan  friend class internal::UnitTestImpl;
798b7ec0f7b3b21338babc9a6ab5a593a40634a8062zhanyong.wan
799a8a582fb926a678d1c4303384bd4dc872f746de8zhanyong.wan  // Gets the (mutable) vector of TestInfos in this TestCase.
80093d13a8bbcb70bfd80b0d7ae2bf9aedfc06bf0cczhanyong.wan  std::vector<TestInfo*>& test_info_list() { return test_info_list_; }
801b7ec0f7b3b21338babc9a6ab5a593a40634a8062zhanyong.wan
802a8a582fb926a678d1c4303384bd4dc872f746de8zhanyong.wan  // Gets the (immutable) vector of TestInfos in this TestCase.
80393d13a8bbcb70bfd80b0d7ae2bf9aedfc06bf0cczhanyong.wan  const std::vector<TestInfo*>& test_info_list() const {
80493d13a8bbcb70bfd80b0d7ae2bf9aedfc06bf0cczhanyong.wan    return test_info_list_;
805b7ec0f7b3b21338babc9a6ab5a593a40634a8062zhanyong.wan  }
806b7ec0f7b3b21338babc9a6ab5a593a40634a8062zhanyong.wan
807f19450f3ad156e1dea624b4d37e645bef067ab5czhanyong.wan  // Returns the i-th test among all the tests. i can range from 0 to
808f19450f3ad156e1dea624b4d37e645bef067ab5czhanyong.wan  // total_test_count() - 1. If i is not in that range, returns NULL.
809f19450f3ad156e1dea624b4d37e645bef067ab5czhanyong.wan  TestInfo* GetMutableTestInfo(int i);
810f19450f3ad156e1dea624b4d37e645bef067ab5czhanyong.wan
811b7ec0f7b3b21338babc9a6ab5a593a40634a8062zhanyong.wan  // Sets the should_run member.
812b7ec0f7b3b21338babc9a6ab5a593a40634a8062zhanyong.wan  void set_should_run(bool should) { should_run_ = should; }
813b7ec0f7b3b21338babc9a6ab5a593a40634a8062zhanyong.wan
814b9a1628577f7ab2eb30a656aeff763909770c9c1zhanyong.wan  // Adds a TestInfo to this test case.  Will delete the TestInfo upon
815b9a1628577f7ab2eb30a656aeff763909770c9c1zhanyong.wan  // destruction of the TestCase object.
816b9a1628577f7ab2eb30a656aeff763909770c9c1zhanyong.wan  void AddTestInfo(TestInfo * test_info);
817b9a1628577f7ab2eb30a656aeff763909770c9c1zhanyong.wan
818b9a1628577f7ab2eb30a656aeff763909770c9c1zhanyong.wan  // Clears the results of all tests in this test case.
819b9a1628577f7ab2eb30a656aeff763909770c9c1zhanyong.wan  void ClearResult();
820b9a1628577f7ab2eb30a656aeff763909770c9c1zhanyong.wan
821b9a1628577f7ab2eb30a656aeff763909770c9c1zhanyong.wan  // Clears the results of all tests in the given test case.
822b9a1628577f7ab2eb30a656aeff763909770c9c1zhanyong.wan  static void ClearTestCaseResult(TestCase* test_case) {
823b9a1628577f7ab2eb30a656aeff763909770c9c1zhanyong.wan    test_case->ClearResult();
824b9a1628577f7ab2eb30a656aeff763909770c9c1zhanyong.wan  }
825b9a1628577f7ab2eb30a656aeff763909770c9c1zhanyong.wan
826b9a1628577f7ab2eb30a656aeff763909770c9c1zhanyong.wan  // Runs every test in this TestCase.
827b9a1628577f7ab2eb30a656aeff763909770c9c1zhanyong.wan  void Run();
828b9a1628577f7ab2eb30a656aeff763909770c9c1zhanyong.wan
829739d30448cd8477c7c8802d41d4e0d969a9490dezhanyong.wan  // Runs SetUpTestCase() for this TestCase.  This wrapper is needed
830739d30448cd8477c7c8802d41d4e0d969a9490dezhanyong.wan  // for catching exceptions thrown from SetUpTestCase().
831739d30448cd8477c7c8802d41d4e0d969a9490dezhanyong.wan  void RunSetUpTestCase() { (*set_up_tc_)(); }
832739d30448cd8477c7c8802d41d4e0d969a9490dezhanyong.wan
833739d30448cd8477c7c8802d41d4e0d969a9490dezhanyong.wan  // Runs TearDownTestCase() for this TestCase.  This wrapper is
834739d30448cd8477c7c8802d41d4e0d969a9490dezhanyong.wan  // needed for catching exceptions thrown from TearDownTestCase().
835739d30448cd8477c7c8802d41d4e0d969a9490dezhanyong.wan  void RunTearDownTestCase() { (*tear_down_tc_)(); }
836739d30448cd8477c7c8802d41d4e0d969a9490dezhanyong.wan
837b9a1628577f7ab2eb30a656aeff763909770c9c1zhanyong.wan  // Returns true iff test passed.
838739d30448cd8477c7c8802d41d4e0d969a9490dezhanyong.wan  static bool TestPassed(const TestInfo* test_info) {
839739d30448cd8477c7c8802d41d4e0d969a9490dezhanyong.wan    return test_info->should_run() && test_info->result()->Passed();
840739d30448cd8477c7c8802d41d4e0d969a9490dezhanyong.wan  }
841b9a1628577f7ab2eb30a656aeff763909770c9c1zhanyong.wan
842b9a1628577f7ab2eb30a656aeff763909770c9c1zhanyong.wan  // Returns true iff test failed.
843739d30448cd8477c7c8802d41d4e0d969a9490dezhanyong.wan  static bool TestFailed(const TestInfo* test_info) {
844739d30448cd8477c7c8802d41d4e0d969a9490dezhanyong.wan    return test_info->should_run() && test_info->result()->Failed();
845739d30448cd8477c7c8802d41d4e0d969a9490dezhanyong.wan  }
846b9a1628577f7ab2eb30a656aeff763909770c9c1zhanyong.wan
847b9a1628577f7ab2eb30a656aeff763909770c9c1zhanyong.wan  // Returns true iff test is disabled.
848739d30448cd8477c7c8802d41d4e0d969a9490dezhanyong.wan  static bool TestDisabled(const TestInfo* test_info) {
849739d30448cd8477c7c8802d41d4e0d969a9490dezhanyong.wan    return test_info->is_disabled_;
850739d30448cd8477c7c8802d41d4e0d969a9490dezhanyong.wan  }
851b9a1628577f7ab2eb30a656aeff763909770c9c1zhanyong.wan
852b9a1628577f7ab2eb30a656aeff763909770c9c1zhanyong.wan  // Returns true if the given test should run.
853739d30448cd8477c7c8802d41d4e0d969a9490dezhanyong.wan  static bool ShouldRunTest(const TestInfo* test_info) {
854739d30448cd8477c7c8802d41d4e0d969a9490dezhanyong.wan    return test_info->should_run();
855739d30448cd8477c7c8802d41d4e0d969a9490dezhanyong.wan  }
856b9a1628577f7ab2eb30a656aeff763909770c9c1zhanyong.wan
857f19450f3ad156e1dea624b4d37e645bef067ab5czhanyong.wan  // Shuffles the tests in this test case.
858f19450f3ad156e1dea624b4d37e645bef067ab5czhanyong.wan  void ShuffleTests(internal::Random* random);
859f19450f3ad156e1dea624b4d37e645bef067ab5czhanyong.wan
860f19450f3ad156e1dea624b4d37e645bef067ab5czhanyong.wan  // Restores the test order to before the first shuffle.
861f19450f3ad156e1dea624b4d37e645bef067ab5czhanyong.wan  void UnshuffleTests();
862f19450f3ad156e1dea624b4d37e645bef067ab5czhanyong.wan
863b9a1628577f7ab2eb30a656aeff763909770c9c1zhanyong.wan  // Name of the test case.
86403c314931649a999b0cf5deb0a434a1009157416jgm@google.com  std::string name_;
865a33163a3ddbb60a6c45340e436310f78044c1a7dzhanyong.wan  // Name of the parameter type, or NULL if this is not a typed or a
866a33163a3ddbb60a6c45340e436310f78044c1a7dzhanyong.wan  // type-parameterized test.
867a33163a3ddbb60a6c45340e436310f78044c1a7dzhanyong.wan  const internal::scoped_ptr<const ::std::string> type_param_;
868f19450f3ad156e1dea624b4d37e645bef067ab5czhanyong.wan  // The vector of TestInfos in their original order.  It owns the
869f19450f3ad156e1dea624b4d37e645bef067ab5czhanyong.wan  // elements in the vector.
87093d13a8bbcb70bfd80b0d7ae2bf9aedfc06bf0cczhanyong.wan  std::vector<TestInfo*> test_info_list_;
871f19450f3ad156e1dea624b4d37e645bef067ab5czhanyong.wan  // Provides a level of indirection for the test list to allow easy
872f19450f3ad156e1dea624b4d37e645bef067ab5czhanyong.wan  // shuffling and restoring the test order.  The i-th element in this
873f19450f3ad156e1dea624b4d37e645bef067ab5czhanyong.wan  // vector is the index of the i-th test in the shuffled test list.
87493d13a8bbcb70bfd80b0d7ae2bf9aedfc06bf0cczhanyong.wan  std::vector<int> test_indices_;
875b9a1628577f7ab2eb30a656aeff763909770c9c1zhanyong.wan  // Pointer to the function that sets up the test case.
876b9a1628577f7ab2eb30a656aeff763909770c9c1zhanyong.wan  Test::SetUpTestCaseFunc set_up_tc_;
877b9a1628577f7ab2eb30a656aeff763909770c9c1zhanyong.wan  // Pointer to the function that tears down the test case.
878b9a1628577f7ab2eb30a656aeff763909770c9c1zhanyong.wan  Test::TearDownTestCaseFunc tear_down_tc_;
879b9a1628577f7ab2eb30a656aeff763909770c9c1zhanyong.wan  // True iff any test in this test case should run.
880b9a1628577f7ab2eb30a656aeff763909770c9c1zhanyong.wan  bool should_run_;
881b9a1628577f7ab2eb30a656aeff763909770c9c1zhanyong.wan  // Elapsed time, in milliseconds.
8821cdc76325936ad3312be0d456b4936c8b8e6bf70zhanyong.wan  TimeInMillis elapsed_time_;
883b9a1628577f7ab2eb30a656aeff763909770c9c1zhanyong.wan
884b9a1628577f7ab2eb30a656aeff763909770c9c1zhanyong.wan  // We disallow copying TestCases.
885b9a1628577f7ab2eb30a656aeff763909770c9c1zhanyong.wan  GTEST_DISALLOW_COPY_AND_ASSIGN_(TestCase);
886b9a1628577f7ab2eb30a656aeff763909770c9c1zhanyong.wan};
887b9a1628577f7ab2eb30a656aeff763909770c9c1zhanyong.wan
8884b6829f0d28990dd645e16386eb226d0f10c8731shiqian// An Environment object is capable of setting up and tearing down an
8894b6829f0d28990dd645e16386eb226d0f10c8731shiqian// environment.  The user should subclass this to define his own
8904b6829f0d28990dd645e16386eb226d0f10c8731shiqian// environment(s).
8914b6829f0d28990dd645e16386eb226d0f10c8731shiqian//
8924b6829f0d28990dd645e16386eb226d0f10c8731shiqian// An Environment object does the set-up and tear-down in virtual
8934b6829f0d28990dd645e16386eb226d0f10c8731shiqian// methods SetUp() and TearDown() instead of the constructor and the
8944b6829f0d28990dd645e16386eb226d0f10c8731shiqian// destructor, as:
8954b6829f0d28990dd645e16386eb226d0f10c8731shiqian//
8964b6829f0d28990dd645e16386eb226d0f10c8731shiqian//   1. You cannot safely throw from a destructor.  This is a problem
8974b6829f0d28990dd645e16386eb226d0f10c8731shiqian//      as in some cases Google Test is used where exceptions are enabled, and
8984b6829f0d28990dd645e16386eb226d0f10c8731shiqian//      we may want to implement ASSERT_* using exceptions where they are
8994b6829f0d28990dd645e16386eb226d0f10c8731shiqian//      available.
9004b6829f0d28990dd645e16386eb226d0f10c8731shiqian//   2. You cannot use ASSERT_* directly in a constructor or
9014b6829f0d28990dd645e16386eb226d0f10c8731shiqian//      destructor.
9024b6829f0d28990dd645e16386eb226d0f10c8731shiqianclass Environment {
9034b6829f0d28990dd645e16386eb226d0f10c8731shiqian public:
9044b6829f0d28990dd645e16386eb226d0f10c8731shiqian  // The d'tor is virtual as we need to subclass Environment.
9054b6829f0d28990dd645e16386eb226d0f10c8731shiqian  virtual ~Environment() {}
9064b6829f0d28990dd645e16386eb226d0f10c8731shiqian
9074b6829f0d28990dd645e16386eb226d0f10c8731shiqian  // Override this to define how to set up the environment.
9084b6829f0d28990dd645e16386eb226d0f10c8731shiqian  virtual void SetUp() {}
9094b6829f0d28990dd645e16386eb226d0f10c8731shiqian
9104b6829f0d28990dd645e16386eb226d0f10c8731shiqian  // Override this to define how to tear down the environment.
9114b6829f0d28990dd645e16386eb226d0f10c8731shiqian  virtual void TearDown() {}
9124b6829f0d28990dd645e16386eb226d0f10c8731shiqian private:
9134b6829f0d28990dd645e16386eb226d0f10c8731shiqian  // If you see an error about overriding the following function or
9144b6829f0d28990dd645e16386eb226d0f10c8731shiqian  // about it being private, you have mis-spelled SetUp() as Setup().
9154b6829f0d28990dd645e16386eb226d0f10c8731shiqian  struct Setup_should_be_spelled_SetUp {};
9164b6829f0d28990dd645e16386eb226d0f10c8731shiqian  virtual Setup_should_be_spelled_SetUp* Setup() { return NULL; }
9174b6829f0d28990dd645e16386eb226d0f10c8731shiqian};
9184b6829f0d28990dd645e16386eb226d0f10c8731shiqian
919334aaead71ccf797a18f2128c320f0304e724860zhanyong.wan// The interface for tracing execution of tests. The methods are organized in
920334aaead71ccf797a18f2128c320f0304e724860zhanyong.wan// the order the corresponding events are fired.
921fff033497b70e96a5dcadb6ba9570c12b5921d74zhanyong.wanclass TestEventListener {
922f39160b423e8f90902066cf6774e4180667dcbeezhanyong.wan public:
923fff033497b70e96a5dcadb6ba9570c12b5921d74zhanyong.wan  virtual ~TestEventListener() {}
924f39160b423e8f90902066cf6774e4180667dcbeezhanyong.wan
9257dcfb6b7375177814c5154e9770efc5406778ad0zhanyong.wan  // Fired before any test activity starts.
9267dcfb6b7375177814c5154e9770efc5406778ad0zhanyong.wan  virtual void OnTestProgramStart(const UnitTest& unit_test) = 0;
927f39160b423e8f90902066cf6774e4180667dcbeezhanyong.wan
9287dcfb6b7375177814c5154e9770efc5406778ad0zhanyong.wan  // Fired before each iteration of tests starts.  There may be more than
9297dcfb6b7375177814c5154e9770efc5406778ad0zhanyong.wan  // one iteration if GTEST_FLAG(repeat) is set. iteration is the iteration
9307dcfb6b7375177814c5154e9770efc5406778ad0zhanyong.wan  // index, starting from 0.
9317dcfb6b7375177814c5154e9770efc5406778ad0zhanyong.wan  virtual void OnTestIterationStart(const UnitTest& unit_test,
9327dcfb6b7375177814c5154e9770efc5406778ad0zhanyong.wan                                    int iteration) = 0;
9337dcfb6b7375177814c5154e9770efc5406778ad0zhanyong.wan
9347dcfb6b7375177814c5154e9770efc5406778ad0zhanyong.wan  // Fired before environment set-up for each iteration of tests starts.
9357dcfb6b7375177814c5154e9770efc5406778ad0zhanyong.wan  virtual void OnEnvironmentsSetUpStart(const UnitTest& unit_test) = 0;
936f39160b423e8f90902066cf6774e4180667dcbeezhanyong.wan
9377dcfb6b7375177814c5154e9770efc5406778ad0zhanyong.wan  // Fired after environment set-up for each iteration of tests ends.
9387dcfb6b7375177814c5154e9770efc5406778ad0zhanyong.wan  virtual void OnEnvironmentsSetUpEnd(const UnitTest& unit_test) = 0;
939f39160b423e8f90902066cf6774e4180667dcbeezhanyong.wan
9407dcfb6b7375177814c5154e9770efc5406778ad0zhanyong.wan  // Fired before the test case starts.
9417dcfb6b7375177814c5154e9770efc5406778ad0zhanyong.wan  virtual void OnTestCaseStart(const TestCase& test_case) = 0;
942f39160b423e8f90902066cf6774e4180667dcbeezhanyong.wan
9437dcfb6b7375177814c5154e9770efc5406778ad0zhanyong.wan  // Fired before the test starts.
944f39160b423e8f90902066cf6774e4180667dcbeezhanyong.wan  virtual void OnTestStart(const TestInfo& test_info) = 0;
945f39160b423e8f90902066cf6774e4180667dcbeezhanyong.wan
946e974309f2322ed19a0239e6247d806c156e00986vladlosev  // Fired after a failed assertion or a SUCCEED() invocation.
947334aaead71ccf797a18f2128c320f0304e724860zhanyong.wan  virtual void OnTestPartResult(const TestPartResult& test_part_result) = 0;
948334aaead71ccf797a18f2128c320f0304e724860zhanyong.wan
9497dcfb6b7375177814c5154e9770efc5406778ad0zhanyong.wan  // Fired after the test ends.
950f39160b423e8f90902066cf6774e4180667dcbeezhanyong.wan  virtual void OnTestEnd(const TestInfo& test_info) = 0;
951f39160b423e8f90902066cf6774e4180667dcbeezhanyong.wan
952334aaead71ccf797a18f2128c320f0304e724860zhanyong.wan  // Fired after the test case ends.
953334aaead71ccf797a18f2128c320f0304e724860zhanyong.wan  virtual void OnTestCaseEnd(const TestCase& test_case) = 0;
954334aaead71ccf797a18f2128c320f0304e724860zhanyong.wan
955334aaead71ccf797a18f2128c320f0304e724860zhanyong.wan  // Fired before environment tear-down for each iteration of tests starts.
956334aaead71ccf797a18f2128c320f0304e724860zhanyong.wan  virtual void OnEnvironmentsTearDownStart(const UnitTest& unit_test) = 0;
957334aaead71ccf797a18f2128c320f0304e724860zhanyong.wan
958334aaead71ccf797a18f2128c320f0304e724860zhanyong.wan  // Fired after environment tear-down for each iteration of tests ends.
959334aaead71ccf797a18f2128c320f0304e724860zhanyong.wan  virtual void OnEnvironmentsTearDownEnd(const UnitTest& unit_test) = 0;
960334aaead71ccf797a18f2128c320f0304e724860zhanyong.wan
961334aaead71ccf797a18f2128c320f0304e724860zhanyong.wan  // Fired after each iteration of tests finishes.
962334aaead71ccf797a18f2128c320f0304e724860zhanyong.wan  virtual void OnTestIterationEnd(const UnitTest& unit_test,
963334aaead71ccf797a18f2128c320f0304e724860zhanyong.wan                                  int iteration) = 0;
964334aaead71ccf797a18f2128c320f0304e724860zhanyong.wan
965334aaead71ccf797a18f2128c320f0304e724860zhanyong.wan  // Fired after all test activities have ended.
966334aaead71ccf797a18f2128c320f0304e724860zhanyong.wan  virtual void OnTestProgramEnd(const UnitTest& unit_test) = 0;
967f39160b423e8f90902066cf6774e4180667dcbeezhanyong.wan};
968f39160b423e8f90902066cf6774e4180667dcbeezhanyong.wan
969f39160b423e8f90902066cf6774e4180667dcbeezhanyong.wan// The convenience class for users who need to override just one or two
970f39160b423e8f90902066cf6774e4180667dcbeezhanyong.wan// methods and are not concerned that a possible change to a signature of
971fff033497b70e96a5dcadb6ba9570c12b5921d74zhanyong.wan// the methods they override will not be caught during the build.  For
972fff033497b70e96a5dcadb6ba9570c12b5921d74zhanyong.wan// comments about each method please see the definition of TestEventListener
973fff033497b70e96a5dcadb6ba9570c12b5921d74zhanyong.wan// above.
974fff033497b70e96a5dcadb6ba9570c12b5921d74zhanyong.wanclass EmptyTestEventListener : public TestEventListener {
975f39160b423e8f90902066cf6774e4180667dcbeezhanyong.wan public:
9767dcfb6b7375177814c5154e9770efc5406778ad0zhanyong.wan  virtual void OnTestProgramStart(const UnitTest& /*unit_test*/) {}
9777dcfb6b7375177814c5154e9770efc5406778ad0zhanyong.wan  virtual void OnTestIterationStart(const UnitTest& /*unit_test*/,
9787dcfb6b7375177814c5154e9770efc5406778ad0zhanyong.wan                                    int /*iteration*/) {}
9797dcfb6b7375177814c5154e9770efc5406778ad0zhanyong.wan  virtual void OnEnvironmentsSetUpStart(const UnitTest& /*unit_test*/) {}
9807dcfb6b7375177814c5154e9770efc5406778ad0zhanyong.wan  virtual void OnEnvironmentsSetUpEnd(const UnitTest& /*unit_test*/) {}
981f39160b423e8f90902066cf6774e4180667dcbeezhanyong.wan  virtual void OnTestCaseStart(const TestCase& /*test_case*/) {}
982f39160b423e8f90902066cf6774e4180667dcbeezhanyong.wan  virtual void OnTestStart(const TestInfo& /*test_info*/) {}
9837dcfb6b7375177814c5154e9770efc5406778ad0zhanyong.wan  virtual void OnTestPartResult(const TestPartResult& /*test_part_result*/) {}
984334aaead71ccf797a18f2128c320f0304e724860zhanyong.wan  virtual void OnTestEnd(const TestInfo& /*test_info*/) {}
985334aaead71ccf797a18f2128c320f0304e724860zhanyong.wan  virtual void OnTestCaseEnd(const TestCase& /*test_case*/) {}
986334aaead71ccf797a18f2128c320f0304e724860zhanyong.wan  virtual void OnEnvironmentsTearDownStart(const UnitTest& /*unit_test*/) {}
987334aaead71ccf797a18f2128c320f0304e724860zhanyong.wan  virtual void OnEnvironmentsTearDownEnd(const UnitTest& /*unit_test*/) {}
988334aaead71ccf797a18f2128c320f0304e724860zhanyong.wan  virtual void OnTestIterationEnd(const UnitTest& /*unit_test*/,
989334aaead71ccf797a18f2128c320f0304e724860zhanyong.wan                                  int /*iteration*/) {}
990334aaead71ccf797a18f2128c320f0304e724860zhanyong.wan  virtual void OnTestProgramEnd(const UnitTest& /*unit_test*/) {}
991f39160b423e8f90902066cf6774e4180667dcbeezhanyong.wan};
992f39160b423e8f90902066cf6774e4180667dcbeezhanyong.wan
993f6d087b78d230d875bf5d8281112662795044680zhanyong.wan// TestEventListeners lets users add listeners to track events in Google Test.
994e4092294d7c1f38f303e8c7f67da31ba3e7e7d9azhanyong.wanclass GTEST_API_ TestEventListeners {
995f39160b423e8f90902066cf6774e4180667dcbeezhanyong.wan public:
996f6d087b78d230d875bf5d8281112662795044680zhanyong.wan  TestEventListeners();
997f6d087b78d230d875bf5d8281112662795044680zhanyong.wan  ~TestEventListeners();
998f39160b423e8f90902066cf6774e4180667dcbeezhanyong.wan
999f39160b423e8f90902066cf6774e4180667dcbeezhanyong.wan  // Appends an event listener to the end of the list. Google Test assumes
1000f39160b423e8f90902066cf6774e4180667dcbeezhanyong.wan  // the ownership of the listener (i.e. it will delete the listener when
1001f39160b423e8f90902066cf6774e4180667dcbeezhanyong.wan  // the test program finishes).
1002fff033497b70e96a5dcadb6ba9570c12b5921d74zhanyong.wan  void Append(TestEventListener* listener);
1003f39160b423e8f90902066cf6774e4180667dcbeezhanyong.wan
1004f39160b423e8f90902066cf6774e4180667dcbeezhanyong.wan  // Removes the given event listener from the list and returns it.  It then
1005f39160b423e8f90902066cf6774e4180667dcbeezhanyong.wan  // becomes the caller's responsibility to delete the listener. Returns
1006f39160b423e8f90902066cf6774e4180667dcbeezhanyong.wan  // NULL if the listener is not found in the list.
1007fff033497b70e96a5dcadb6ba9570c12b5921d74zhanyong.wan  TestEventListener* Release(TestEventListener* listener);
1008f39160b423e8f90902066cf6774e4180667dcbeezhanyong.wan
1009f39160b423e8f90902066cf6774e4180667dcbeezhanyong.wan  // Returns the standard listener responsible for the default console
1010f39160b423e8f90902066cf6774e4180667dcbeezhanyong.wan  // output.  Can be removed from the listeners list to shut down default
1011f39160b423e8f90902066cf6774e4180667dcbeezhanyong.wan  // console output.  Note that removing this object from the listener list
1012f39160b423e8f90902066cf6774e4180667dcbeezhanyong.wan  // with Release transfers its ownership to the caller and makes this
1013f39160b423e8f90902066cf6774e4180667dcbeezhanyong.wan  // function return NULL the next time.
1014fff033497b70e96a5dcadb6ba9570c12b5921d74zhanyong.wan  TestEventListener* default_result_printer() const {
1015f39160b423e8f90902066cf6774e4180667dcbeezhanyong.wan    return default_result_printer_;
1016f39160b423e8f90902066cf6774e4180667dcbeezhanyong.wan  }
1017f39160b423e8f90902066cf6774e4180667dcbeezhanyong.wan
1018f39160b423e8f90902066cf6774e4180667dcbeezhanyong.wan  // Returns the standard listener responsible for the default XML output
1019f39160b423e8f90902066cf6774e4180667dcbeezhanyong.wan  // controlled by the --gtest_output=xml flag.  Can be removed from the
1020f39160b423e8f90902066cf6774e4180667dcbeezhanyong.wan  // listeners list by users who want to shut down the default XML output
1021f39160b423e8f90902066cf6774e4180667dcbeezhanyong.wan  // controlled by this flag and substitute it with custom one.  Note that
1022f39160b423e8f90902066cf6774e4180667dcbeezhanyong.wan  // removing this object from the listener list with Release transfers its
1023f39160b423e8f90902066cf6774e4180667dcbeezhanyong.wan  // ownership to the caller and makes this function return NULL the next
1024f39160b423e8f90902066cf6774e4180667dcbeezhanyong.wan  // time.
1025fff033497b70e96a5dcadb6ba9570c12b5921d74zhanyong.wan  TestEventListener* default_xml_generator() const {
1026f39160b423e8f90902066cf6774e4180667dcbeezhanyong.wan    return default_xml_generator_;
1027f39160b423e8f90902066cf6774e4180667dcbeezhanyong.wan  }
1028f39160b423e8f90902066cf6774e4180667dcbeezhanyong.wan
1029f39160b423e8f90902066cf6774e4180667dcbeezhanyong.wan private:
1030fff033497b70e96a5dcadb6ba9570c12b5921d74zhanyong.wan  friend class TestCase;
1031739d30448cd8477c7c8802d41d4e0d969a9490dezhanyong.wan  friend class TestInfo;
1032f39160b423e8f90902066cf6774e4180667dcbeezhanyong.wan  friend class internal::DefaultGlobalTestPartResultReporter;
1033f39160b423e8f90902066cf6774e4180667dcbeezhanyong.wan  friend class internal::NoExecDeathTest;
1034f6d087b78d230d875bf5d8281112662795044680zhanyong.wan  friend class internal::TestEventListenersAccessor;
1035f39160b423e8f90902066cf6774e4180667dcbeezhanyong.wan  friend class internal::UnitTestImpl;
1036f39160b423e8f90902066cf6774e4180667dcbeezhanyong.wan
1037fff033497b70e96a5dcadb6ba9570c12b5921d74zhanyong.wan  // Returns repeater that broadcasts the TestEventListener events to all
1038fff033497b70e96a5dcadb6ba9570c12b5921d74zhanyong.wan  // subscribers.
1039fff033497b70e96a5dcadb6ba9570c12b5921d74zhanyong.wan  TestEventListener* repeater();
1040f39160b423e8f90902066cf6774e4180667dcbeezhanyong.wan
1041f39160b423e8f90902066cf6774e4180667dcbeezhanyong.wan  // Sets the default_result_printer attribute to the provided listener.
1042f39160b423e8f90902066cf6774e4180667dcbeezhanyong.wan  // The listener is also added to the listener list and previous
1043f39160b423e8f90902066cf6774e4180667dcbeezhanyong.wan  // default_result_printer is removed from it and deleted. The listener can
1044f39160b423e8f90902066cf6774e4180667dcbeezhanyong.wan  // also be NULL in which case it will not be added to the list. Does
1045f39160b423e8f90902066cf6774e4180667dcbeezhanyong.wan  // nothing if the previous and the current listener objects are the same.
1046fff033497b70e96a5dcadb6ba9570c12b5921d74zhanyong.wan  void SetDefaultResultPrinter(TestEventListener* listener);
1047f39160b423e8f90902066cf6774e4180667dcbeezhanyong.wan
1048f39160b423e8f90902066cf6774e4180667dcbeezhanyong.wan  // Sets the default_xml_generator attribute to the provided listener.  The
1049f39160b423e8f90902066cf6774e4180667dcbeezhanyong.wan  // listener is also added to the listener list and previous
1050f39160b423e8f90902066cf6774e4180667dcbeezhanyong.wan  // default_xml_generator is removed from it and deleted. The listener can
1051f39160b423e8f90902066cf6774e4180667dcbeezhanyong.wan  // also be NULL in which case it will not be added to the list. Does
1052f39160b423e8f90902066cf6774e4180667dcbeezhanyong.wan  // nothing if the previous and the current listener objects are the same.
1053fff033497b70e96a5dcadb6ba9570c12b5921d74zhanyong.wan  void SetDefaultXmlGenerator(TestEventListener* listener);
1054f39160b423e8f90902066cf6774e4180667dcbeezhanyong.wan
1055f39160b423e8f90902066cf6774e4180667dcbeezhanyong.wan  // Controls whether events will be forwarded by the repeater to the
1056f39160b423e8f90902066cf6774e4180667dcbeezhanyong.wan  // listeners in the list.
1057f39160b423e8f90902066cf6774e4180667dcbeezhanyong.wan  bool EventForwardingEnabled() const;
1058f39160b423e8f90902066cf6774e4180667dcbeezhanyong.wan  void SuppressEventForwarding();
1059f39160b423e8f90902066cf6774e4180667dcbeezhanyong.wan
1060f39160b423e8f90902066cf6774e4180667dcbeezhanyong.wan  // The actual list of listeners.
10617dcfb6b7375177814c5154e9770efc5406778ad0zhanyong.wan  internal::TestEventRepeater* repeater_;
1062f39160b423e8f90902066cf6774e4180667dcbeezhanyong.wan  // Listener responsible for the standard result output.
1063fff033497b70e96a5dcadb6ba9570c12b5921d74zhanyong.wan  TestEventListener* default_result_printer_;
1064f39160b423e8f90902066cf6774e4180667dcbeezhanyong.wan  // Listener responsible for the creation of the XML output file.
1065fff033497b70e96a5dcadb6ba9570c12b5921d74zhanyong.wan  TestEventListener* default_xml_generator_;
1066f39160b423e8f90902066cf6774e4180667dcbeezhanyong.wan
1067f6d087b78d230d875bf5d8281112662795044680zhanyong.wan  // We disallow copying TestEventListeners.
1068f6d087b78d230d875bf5d8281112662795044680zhanyong.wan  GTEST_DISALLOW_COPY_AND_ASSIGN_(TestEventListeners);
1069f39160b423e8f90902066cf6774e4180667dcbeezhanyong.wan};
1070f39160b423e8f90902066cf6774e4180667dcbeezhanyong.wan
1071a8a582fb926a678d1c4303384bd4dc872f746de8zhanyong.wan// A UnitTest consists of a vector of TestCases.
10724b6829f0d28990dd645e16386eb226d0f10c8731shiqian//
10734b6829f0d28990dd645e16386eb226d0f10c8731shiqian// This is a singleton class.  The only instance of UnitTest is
10744b6829f0d28990dd645e16386eb226d0f10c8731shiqian// created when UnitTest::GetInstance() is first called.  This
10754b6829f0d28990dd645e16386eb226d0f10c8731shiqian// instance is never deleted.
10764b6829f0d28990dd645e16386eb226d0f10c8731shiqian//
10774b6829f0d28990dd645e16386eb226d0f10c8731shiqian// UnitTest is not copyable.
10784b6829f0d28990dd645e16386eb226d0f10c8731shiqian//
10794b6829f0d28990dd645e16386eb226d0f10c8731shiqian// This class is thread-safe as long as the methods are called
10804b6829f0d28990dd645e16386eb226d0f10c8731shiqian// according to their specification.
1081e4092294d7c1f38f303e8c7f67da31ba3e7e7d9azhanyong.wanclass GTEST_API_ UnitTest {
10824b6829f0d28990dd645e16386eb226d0f10c8731shiqian public:
10834b6829f0d28990dd645e16386eb226d0f10c8731shiqian  // Gets the singleton UnitTest object.  The first time this method
10844b6829f0d28990dd645e16386eb226d0f10c8731shiqian  // is called, a UnitTest object is constructed and returned.
10854b6829f0d28990dd645e16386eb226d0f10c8731shiqian  // Consecutive calls will return the same object.
10864b6829f0d28990dd645e16386eb226d0f10c8731shiqian  static UnitTest* GetInstance();
10874b6829f0d28990dd645e16386eb226d0f10c8731shiqian
10884b6829f0d28990dd645e16386eb226d0f10c8731shiqian  // Runs all tests in this UnitTest object and prints the result.
10894b6829f0d28990dd645e16386eb226d0f10c8731shiqian  // Returns 0 if successful, or 1 otherwise.
10904b6829f0d28990dd645e16386eb226d0f10c8731shiqian  //
10914b6829f0d28990dd645e16386eb226d0f10c8731shiqian  // This method can only be called from the main thread.
10924b6829f0d28990dd645e16386eb226d0f10c8731shiqian  //
10934b6829f0d28990dd645e16386eb226d0f10c8731shiqian  // INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM.
1094e44602ec83c65102035ce5304ae8de0cb16e9e56shiqian  int Run() GTEST_MUST_USE_RESULT_;
10954b6829f0d28990dd645e16386eb226d0f10c8731shiqian
1096c3b4de35a18a1f011e8009d4d794f787f7e4e0c1shiqian  // Returns the working directory when the first TEST() or TEST_F()
1097c3b4de35a18a1f011e8009d4d794f787f7e4e0c1shiqian  // was executed.  The UnitTest object owns the string.
1098c3b4de35a18a1f011e8009d4d794f787f7e4e0c1shiqian  const char* original_working_dir() const;
1099c3b4de35a18a1f011e8009d4d794f787f7e4e0c1shiqian
11004b6829f0d28990dd645e16386eb226d0f10c8731shiqian  // Returns the TestCase object for the test that's currently running,
11014b6829f0d28990dd645e16386eb226d0f10c8731shiqian  // or NULL if no test is running.
1102ee89af8d5f0578d9da31c5295f59f2d2bdebec39vladlosev  const TestCase* current_test_case() const
1103ee89af8d5f0578d9da31c5295f59f2d2bdebec39vladlosev      GTEST_LOCK_EXCLUDED_(mutex_);
11044b6829f0d28990dd645e16386eb226d0f10c8731shiqian
11054b6829f0d28990dd645e16386eb226d0f10c8731shiqian  // Returns the TestInfo object for the test that's currently running,
11064b6829f0d28990dd645e16386eb226d0f10c8731shiqian  // or NULL if no test is running.
1107ee89af8d5f0578d9da31c5295f59f2d2bdebec39vladlosev  const TestInfo* current_test_info() const
1108ee89af8d5f0578d9da31c5295f59f2d2bdebec39vladlosev      GTEST_LOCK_EXCLUDED_(mutex_);
11094b6829f0d28990dd645e16386eb226d0f10c8731shiqian
11109b9794ffb9cfafb94f5def851f76b9a104fcb891zhanyong.wan  // Returns the random seed used at the start of the current test run.
11119b9794ffb9cfafb94f5def851f76b9a104fcb891zhanyong.wan  int random_seed() const;
11129b9794ffb9cfafb94f5def851f76b9a104fcb891zhanyong.wan
11134cd62602913a032a7aec091d4c8055ff9af95e37zhanyong.wan#if GTEST_HAS_PARAM_TEST
1114f904a612d9444ab36c07a8e619c113432e046f49vladlosev  // Returns the ParameterizedTestCaseRegistry object used to keep track of
1115f904a612d9444ab36c07a8e619c113432e046f49vladlosev  // value-parameterized tests and instantiate and register them.
1116a80f23f103e1a008c1df7ecc68eef9aa4ffbdc37zhanyong.wan  //
1117a80f23f103e1a008c1df7ecc68eef9aa4ffbdc37zhanyong.wan  // INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM.
1118ee89af8d5f0578d9da31c5295f59f2d2bdebec39vladlosev  internal::ParameterizedTestCaseRegistry& parameterized_test_registry()
1119ee89af8d5f0578d9da31c5295f59f2d2bdebec39vladlosev      GTEST_LOCK_EXCLUDED_(mutex_);
1120f904a612d9444ab36c07a8e619c113432e046f49vladlosev#endif  // GTEST_HAS_PARAM_TEST
1121f904a612d9444ab36c07a8e619c113432e046f49vladlosev
11229644db857432db95bc64f5d6db5867b1c7cf1679zhanyong.wan  // Gets the number of successful test cases.
11239644db857432db95bc64f5d6db5867b1c7cf1679zhanyong.wan  int successful_test_case_count() const;
11249644db857432db95bc64f5d6db5867b1c7cf1679zhanyong.wan
11259644db857432db95bc64f5d6db5867b1c7cf1679zhanyong.wan  // Gets the number of failed test cases.
11269644db857432db95bc64f5d6db5867b1c7cf1679zhanyong.wan  int failed_test_case_count() const;
11279644db857432db95bc64f5d6db5867b1c7cf1679zhanyong.wan
11289644db857432db95bc64f5d6db5867b1c7cf1679zhanyong.wan  // Gets the number of all test cases.
11299644db857432db95bc64f5d6db5867b1c7cf1679zhanyong.wan  int total_test_case_count() const;
11309644db857432db95bc64f5d6db5867b1c7cf1679zhanyong.wan
11319644db857432db95bc64f5d6db5867b1c7cf1679zhanyong.wan  // Gets the number of all test cases that contain at least one test
11329644db857432db95bc64f5d6db5867b1c7cf1679zhanyong.wan  // that should run.
11339644db857432db95bc64f5d6db5867b1c7cf1679zhanyong.wan  int test_case_to_run_count() const;
11349644db857432db95bc64f5d6db5867b1c7cf1679zhanyong.wan
11359644db857432db95bc64f5d6db5867b1c7cf1679zhanyong.wan  // Gets the number of successful tests.
11369644db857432db95bc64f5d6db5867b1c7cf1679zhanyong.wan  int successful_test_count() const;
11379644db857432db95bc64f5d6db5867b1c7cf1679zhanyong.wan
11389644db857432db95bc64f5d6db5867b1c7cf1679zhanyong.wan  // Gets the number of failed tests.
11399644db857432db95bc64f5d6db5867b1c7cf1679zhanyong.wan  int failed_test_count() const;
11409644db857432db95bc64f5d6db5867b1c7cf1679zhanyong.wan
11419644db857432db95bc64f5d6db5867b1c7cf1679zhanyong.wan  // Gets the number of disabled tests.
11429644db857432db95bc64f5d6db5867b1c7cf1679zhanyong.wan  int disabled_test_count() const;
11439644db857432db95bc64f5d6db5867b1c7cf1679zhanyong.wan
11449644db857432db95bc64f5d6db5867b1c7cf1679zhanyong.wan  // Gets the number of all tests.
11459644db857432db95bc64f5d6db5867b1c7cf1679zhanyong.wan  int total_test_count() const;
11469644db857432db95bc64f5d6db5867b1c7cf1679zhanyong.wan
11479644db857432db95bc64f5d6db5867b1c7cf1679zhanyong.wan  // Gets the number of tests that should run.
11489644db857432db95bc64f5d6db5867b1c7cf1679zhanyong.wan  int test_to_run_count() const;
11499644db857432db95bc64f5d6db5867b1c7cf1679zhanyong.wan
11508883b42f2a78976dde09f31ca72806072e33bd5fvladlosev  // Gets the time of the test program start, in ms from the start of the
11518883b42f2a78976dde09f31ca72806072e33bd5fvladlosev  // UNIX epoch.
11528883b42f2a78976dde09f31ca72806072e33bd5fvladlosev  TimeInMillis start_timestamp() const;
11538883b42f2a78976dde09f31ca72806072e33bd5fvladlosev
11549644db857432db95bc64f5d6db5867b1c7cf1679zhanyong.wan  // Gets the elapsed time, in milliseconds.
11551cdc76325936ad3312be0d456b4936c8b8e6bf70zhanyong.wan  TimeInMillis elapsed_time() const;
11569644db857432db95bc64f5d6db5867b1c7cf1679zhanyong.wan
11579644db857432db95bc64f5d6db5867b1c7cf1679zhanyong.wan  // Returns true iff the unit test passed (i.e. all test cases passed).
11589644db857432db95bc64f5d6db5867b1c7cf1679zhanyong.wan  bool Passed() const;
11599644db857432db95bc64f5d6db5867b1c7cf1679zhanyong.wan
11609644db857432db95bc64f5d6db5867b1c7cf1679zhanyong.wan  // Returns true iff the unit test failed (i.e. some test case failed
11619644db857432db95bc64f5d6db5867b1c7cf1679zhanyong.wan  // or something outside of all tests failed).
11629644db857432db95bc64f5d6db5867b1c7cf1679zhanyong.wan  bool Failed() const;
11639644db857432db95bc64f5d6db5867b1c7cf1679zhanyong.wan
11649644db857432db95bc64f5d6db5867b1c7cf1679zhanyong.wan  // Gets the i-th test case among all the test cases. i can range from 0 to
11659644db857432db95bc64f5d6db5867b1c7cf1679zhanyong.wan  // total_test_case_count() - 1. If i is not in that range, returns NULL.
1166fff033497b70e96a5dcadb6ba9570c12b5921d74zhanyong.wan  const TestCase* GetTestCase(int i) const;
11679644db857432db95bc64f5d6db5867b1c7cf1679zhanyong.wan
1168f39160b423e8f90902066cf6774e4180667dcbeezhanyong.wan  // Returns the list of event listeners that can be used to track events
1169f39160b423e8f90902066cf6774e4180667dcbeezhanyong.wan  // inside Google Test.
1170f6d087b78d230d875bf5d8281112662795044680zhanyong.wan  TestEventListeners& listeners();
1171f39160b423e8f90902066cf6774e4180667dcbeezhanyong.wan
1172fff033497b70e96a5dcadb6ba9570c12b5921d74zhanyong.wan private:
1173fff033497b70e96a5dcadb6ba9570c12b5921d74zhanyong.wan  // Registers and returns a global test environment.  When a test
1174fff033497b70e96a5dcadb6ba9570c12b5921d74zhanyong.wan  // program is run, all global test environments will be set-up in
1175fff033497b70e96a5dcadb6ba9570c12b5921d74zhanyong.wan  // the order they were registered.  After all tests in the program
1176fff033497b70e96a5dcadb6ba9570c12b5921d74zhanyong.wan  // have finished, all global test environments will be torn-down in
1177fff033497b70e96a5dcadb6ba9570c12b5921d74zhanyong.wan  // the *reverse* order they were registered.
1178fff033497b70e96a5dcadb6ba9570c12b5921d74zhanyong.wan  //
1179fff033497b70e96a5dcadb6ba9570c12b5921d74zhanyong.wan  // The UnitTest object takes ownership of the given environment.
1180fff033497b70e96a5dcadb6ba9570c12b5921d74zhanyong.wan  //
1181fff033497b70e96a5dcadb6ba9570c12b5921d74zhanyong.wan  // This method can only be called from the main thread.
1182fff033497b70e96a5dcadb6ba9570c12b5921d74zhanyong.wan  Environment* AddEnvironment(Environment* env);
1183fff033497b70e96a5dcadb6ba9570c12b5921d74zhanyong.wan
1184fff033497b70e96a5dcadb6ba9570c12b5921d74zhanyong.wan  // Adds a TestPartResult to the current TestResult object.  All
1185fff033497b70e96a5dcadb6ba9570c12b5921d74zhanyong.wan  // Google Test assertion macros (e.g. ASSERT_TRUE, EXPECT_EQ, etc)
1186fff033497b70e96a5dcadb6ba9570c12b5921d74zhanyong.wan  // eventually call this to report their results.  The user code
1187fff033497b70e96a5dcadb6ba9570c12b5921d74zhanyong.wan  // should use the assertion macros instead of calling this directly.
1188fff033497b70e96a5dcadb6ba9570c12b5921d74zhanyong.wan  void AddTestPartResult(TestPartResult::Type result_type,
1189fff033497b70e96a5dcadb6ba9570c12b5921d74zhanyong.wan                         const char* file_name,
1190fff033497b70e96a5dcadb6ba9570c12b5921d74zhanyong.wan                         int line_number,
119103c314931649a999b0cf5deb0a434a1009157416jgm@google.com                         const std::string& message,
119203c314931649a999b0cf5deb0a434a1009157416jgm@google.com                         const std::string& os_stack_trace)
1193ee89af8d5f0578d9da31c5295f59f2d2bdebec39vladlosev      GTEST_LOCK_EXCLUDED_(mutex_);
1194fff033497b70e96a5dcadb6ba9570c12b5921d74zhanyong.wan
1195fff033497b70e96a5dcadb6ba9570c12b5921d74zhanyong.wan  // Adds a TestProperty to the current TestResult object. If the result already
1196fff033497b70e96a5dcadb6ba9570c12b5921d74zhanyong.wan  // contains a property with the same key, the value will be updated.
1197fff033497b70e96a5dcadb6ba9570c12b5921d74zhanyong.wan  void RecordPropertyForCurrentTest(const char* key, const char* value);
1198fff033497b70e96a5dcadb6ba9570c12b5921d74zhanyong.wan
1199f19450f3ad156e1dea624b4d37e645bef067ab5czhanyong.wan  // Gets the i-th test case among all the test cases. i can range from 0 to
1200f19450f3ad156e1dea624b4d37e645bef067ab5czhanyong.wan  // total_test_case_count() - 1. If i is not in that range, returns NULL.
1201f19450f3ad156e1dea624b4d37e645bef067ab5czhanyong.wan  TestCase* GetMutableTestCase(int i);
1202f19450f3ad156e1dea624b4d37e645bef067ab5czhanyong.wan
1203fff033497b70e96a5dcadb6ba9570c12b5921d74zhanyong.wan  // Accessors for the implementation object.
1204fff033497b70e96a5dcadb6ba9570c12b5921d74zhanyong.wan  internal::UnitTestImpl* impl() { return impl_; }
1205fff033497b70e96a5dcadb6ba9570c12b5921d74zhanyong.wan  const internal::UnitTestImpl* impl() const { return impl_; }
1206fff033497b70e96a5dcadb6ba9570c12b5921d74zhanyong.wan
1207fff033497b70e96a5dcadb6ba9570c12b5921d74zhanyong.wan  // These classes and funcions are friends as they need to access private
1208fff033497b70e96a5dcadb6ba9570c12b5921d74zhanyong.wan  // members of UnitTest.
1209fff033497b70e96a5dcadb6ba9570c12b5921d74zhanyong.wan  friend class Test;
1210fff033497b70e96a5dcadb6ba9570c12b5921d74zhanyong.wan  friend class internal::AssertHelper;
12114b6829f0d28990dd645e16386eb226d0f10c8731shiqian  friend class internal::ScopedTrace;
1212a032cfff4091c87a137a8d24ce2eb97c740cf4c8kosak@google.com  friend class internal::StreamingListenerTest;
1213a80f23f103e1a008c1df7ecc68eef9aa4ffbdc37zhanyong.wan  friend Environment* AddGlobalTestEnvironment(Environment* env);
1214a80f23f103e1a008c1df7ecc68eef9aa4ffbdc37zhanyong.wan  friend internal::UnitTestImpl* internal::GetUnitTestImpl();
1215a80f23f103e1a008c1df7ecc68eef9aa4ffbdc37zhanyong.wan  friend void internal::ReportFailureInUnknownLocation(
1216334aaead71ccf797a18f2128c320f0304e724860zhanyong.wan      TestPartResult::Type result_type,
121703c314931649a999b0cf5deb0a434a1009157416jgm@google.com      const std::string& message);
12184b6829f0d28990dd645e16386eb226d0f10c8731shiqian
12194b6829f0d28990dd645e16386eb226d0f10c8731shiqian  // Creates an empty UnitTest.
12204b6829f0d28990dd645e16386eb226d0f10c8731shiqian  UnitTest();
12214b6829f0d28990dd645e16386eb226d0f10c8731shiqian
12224b6829f0d28990dd645e16386eb226d0f10c8731shiqian  // D'tor
12234b6829f0d28990dd645e16386eb226d0f10c8731shiqian  virtual ~UnitTest();
12244b6829f0d28990dd645e16386eb226d0f10c8731shiqian
12254b6829f0d28990dd645e16386eb226d0f10c8731shiqian  // Pushes a trace defined by SCOPED_TRACE() on to the per-thread
12264b6829f0d28990dd645e16386eb226d0f10c8731shiqian  // Google Test trace stack.
1227ee89af8d5f0578d9da31c5295f59f2d2bdebec39vladlosev  void PushGTestTrace(const internal::TraceInfo& trace)
1228ee89af8d5f0578d9da31c5295f59f2d2bdebec39vladlosev      GTEST_LOCK_EXCLUDED_(mutex_);
12294b6829f0d28990dd645e16386eb226d0f10c8731shiqian
12304b6829f0d28990dd645e16386eb226d0f10c8731shiqian  // Pops a trace from the per-thread Google Test trace stack.
1231ee89af8d5f0578d9da31c5295f59f2d2bdebec39vladlosev  void PopGTestTrace()
1232ee89af8d5f0578d9da31c5295f59f2d2bdebec39vladlosev      GTEST_LOCK_EXCLUDED_(mutex_);
12334b6829f0d28990dd645e16386eb226d0f10c8731shiqian
12344b6829f0d28990dd645e16386eb226d0f10c8731shiqian  // Protects mutable state in *impl_.  This is mutable as some const
12354b6829f0d28990dd645e16386eb226d0f10c8731shiqian  // methods need to lock it too.
12364b6829f0d28990dd645e16386eb226d0f10c8731shiqian  mutable internal::Mutex mutex_;
12374b6829f0d28990dd645e16386eb226d0f10c8731shiqian
12384b6829f0d28990dd645e16386eb226d0f10c8731shiqian  // Opaque implementation object.  This field is never changed once
12394b6829f0d28990dd645e16386eb226d0f10c8731shiqian  // the object is constructed.  We don't mark it as const here, as
12404b6829f0d28990dd645e16386eb226d0f10c8731shiqian  // doing so will cause a warning in the constructor of UnitTest.
12414b6829f0d28990dd645e16386eb226d0f10c8731shiqian  // Mutable state in *impl_ is protected by mutex_.
12424b6829f0d28990dd645e16386eb226d0f10c8731shiqian  internal::UnitTestImpl* impl_;
12434b6829f0d28990dd645e16386eb226d0f10c8731shiqian
12444b6829f0d28990dd645e16386eb226d0f10c8731shiqian  // We disallow copying UnitTest.
1245e44602ec83c65102035ce5304ae8de0cb16e9e56shiqian  GTEST_DISALLOW_COPY_AND_ASSIGN_(UnitTest);
12464b6829f0d28990dd645e16386eb226d0f10c8731shiqian};
12474b6829f0d28990dd645e16386eb226d0f10c8731shiqian
12484b6829f0d28990dd645e16386eb226d0f10c8731shiqian// A convenient wrapper for adding an environment for the test
12494b6829f0d28990dd645e16386eb226d0f10c8731shiqian// program.
12504b6829f0d28990dd645e16386eb226d0f10c8731shiqian//
12514b6829f0d28990dd645e16386eb226d0f10c8731shiqian// You should call this before RUN_ALL_TESTS() is called, probably in
12524b6829f0d28990dd645e16386eb226d0f10c8731shiqian// main().  If you use gtest_main, you need to call this before main()
12534b6829f0d28990dd645e16386eb226d0f10c8731shiqian// starts for it to take effect.  For example, you can define a global
12544b6829f0d28990dd645e16386eb226d0f10c8731shiqian// variable like this:
12554b6829f0d28990dd645e16386eb226d0f10c8731shiqian//
12564b6829f0d28990dd645e16386eb226d0f10c8731shiqian//   testing::Environment* const foo_env =
12574b6829f0d28990dd645e16386eb226d0f10c8731shiqian//       testing::AddGlobalTestEnvironment(new FooEnvironment);
12584b6829f0d28990dd645e16386eb226d0f10c8731shiqian//
12594b6829f0d28990dd645e16386eb226d0f10c8731shiqian// However, we strongly recommend you to write your own main() and
12604b6829f0d28990dd645e16386eb226d0f10c8731shiqian// call AddGlobalTestEnvironment() there, as relying on initialization
12614b6829f0d28990dd645e16386eb226d0f10c8731shiqian// of global variables makes the code harder to read and may cause
12624b6829f0d28990dd645e16386eb226d0f10c8731shiqian// problems when you register multiple environments from different
12634b6829f0d28990dd645e16386eb226d0f10c8731shiqian// translation units and the environments have dependencies among them
12644b6829f0d28990dd645e16386eb226d0f10c8731shiqian// (remember that the compiler doesn't guarantee the order in which
12654b6829f0d28990dd645e16386eb226d0f10c8731shiqian// global variables from different translation units are initialized).
12664b6829f0d28990dd645e16386eb226d0f10c8731shiqianinline Environment* AddGlobalTestEnvironment(Environment* env) {
12674b6829f0d28990dd645e16386eb226d0f10c8731shiqian  return UnitTest::GetInstance()->AddEnvironment(env);
12684b6829f0d28990dd645e16386eb226d0f10c8731shiqian}
12694b6829f0d28990dd645e16386eb226d0f10c8731shiqian
12704b6829f0d28990dd645e16386eb226d0f10c8731shiqian// Initializes Google Test.  This must be called before calling
12714b6829f0d28990dd645e16386eb226d0f10c8731shiqian// RUN_ALL_TESTS().  In particular, it parses a command line for the
12724b6829f0d28990dd645e16386eb226d0f10c8731shiqian// flags that Google Test recognizes.  Whenever a Google Test flag is
12734b6829f0d28990dd645e16386eb226d0f10c8731shiqian// seen, it is removed from argv, and *argc is decremented.
12744b6829f0d28990dd645e16386eb226d0f10c8731shiqian//
12754b6829f0d28990dd645e16386eb226d0f10c8731shiqian// No value is returned.  Instead, the Google Test flag variables are
12764b6829f0d28990dd645e16386eb226d0f10c8731shiqian// updated.
1277f179f4ee7cc76e7103a726ebf666902b07f89659vladlosev//
1278f179f4ee7cc76e7103a726ebf666902b07f89659vladlosev// Calling the function for the second time has no user-visible effect.
1279e4092294d7c1f38f303e8c7f67da31ba3e7e7d9azhanyong.wanGTEST_API_ void InitGoogleTest(int* argc, char** argv);
12804b6829f0d28990dd645e16386eb226d0f10c8731shiqian
12814b6829f0d28990dd645e16386eb226d0f10c8731shiqian// This overloaded version can be used in Windows programs compiled in
12824b6829f0d28990dd645e16386eb226d0f10c8731shiqian// UNICODE mode.
1283e4092294d7c1f38f303e8c7f67da31ba3e7e7d9azhanyong.wanGTEST_API_ void InitGoogleTest(int* argc, wchar_t** argv);
12844b6829f0d28990dd645e16386eb226d0f10c8731shiqian
12854b6829f0d28990dd645e16386eb226d0f10c8731shiqiannamespace internal {
12864b6829f0d28990dd645e16386eb226d0f10c8731shiqian
12876b625a794e8a0e314f3ccc2f6a892f4a40a7e788zhanyong.wan// FormatForComparison<ToPrint, OtherOperand>::Format(value) formats a
12886b625a794e8a0e314f3ccc2f6a892f4a40a7e788zhanyong.wan// value of type ToPrint that is an operand of a comparison assertion
12896b625a794e8a0e314f3ccc2f6a892f4a40a7e788zhanyong.wan// (e.g. ASSERT_EQ).  OtherOperand is the type of the other operand in
12906b625a794e8a0e314f3ccc2f6a892f4a40a7e788zhanyong.wan// the comparison, and is used to help determine the best way to
12916b625a794e8a0e314f3ccc2f6a892f4a40a7e788zhanyong.wan// format the value.  In particular, when the value is a C string
12926b625a794e8a0e314f3ccc2f6a892f4a40a7e788zhanyong.wan// (char pointer) and the other operand is an STL string object, we
12936b625a794e8a0e314f3ccc2f6a892f4a40a7e788zhanyong.wan// want to format the C string as a string, since we know it is
12946b625a794e8a0e314f3ccc2f6a892f4a40a7e788zhanyong.wan// compared by value with the string object.  If the value is a char
12956b625a794e8a0e314f3ccc2f6a892f4a40a7e788zhanyong.wan// pointer but the other operand is not an STL string object, we don't
12966b625a794e8a0e314f3ccc2f6a892f4a40a7e788zhanyong.wan// know whether the pointer is supposed to point to a NUL-terminated
12976b625a794e8a0e314f3ccc2f6a892f4a40a7e788zhanyong.wan// string, and thus want to print it as a pointer to be safe.
12986b625a794e8a0e314f3ccc2f6a892f4a40a7e788zhanyong.wan//
12996b625a794e8a0e314f3ccc2f6a892f4a40a7e788zhanyong.wan// INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM.
13006b625a794e8a0e314f3ccc2f6a892f4a40a7e788zhanyong.wan
13016b625a794e8a0e314f3ccc2f6a892f4a40a7e788zhanyong.wan// The default case.
13026b625a794e8a0e314f3ccc2f6a892f4a40a7e788zhanyong.wantemplate <typename ToPrint, typename OtherOperand>
13036b625a794e8a0e314f3ccc2f6a892f4a40a7e788zhanyong.wanclass FormatForComparison {
13046b625a794e8a0e314f3ccc2f6a892f4a40a7e788zhanyong.wan public:
13056b625a794e8a0e314f3ccc2f6a892f4a40a7e788zhanyong.wan  static ::std::string Format(const ToPrint& value) {
13066b625a794e8a0e314f3ccc2f6a892f4a40a7e788zhanyong.wan    return ::testing::PrintToString(value);
13076b625a794e8a0e314f3ccc2f6a892f4a40a7e788zhanyong.wan  }
13086b625a794e8a0e314f3ccc2f6a892f4a40a7e788zhanyong.wan};
13096b625a794e8a0e314f3ccc2f6a892f4a40a7e788zhanyong.wan
13106b625a794e8a0e314f3ccc2f6a892f4a40a7e788zhanyong.wan// Array.
13116b625a794e8a0e314f3ccc2f6a892f4a40a7e788zhanyong.wantemplate <typename ToPrint, size_t N, typename OtherOperand>
13126b625a794e8a0e314f3ccc2f6a892f4a40a7e788zhanyong.wanclass FormatForComparison<ToPrint[N], OtherOperand> {
13136b625a794e8a0e314f3ccc2f6a892f4a40a7e788zhanyong.wan public:
13146b625a794e8a0e314f3ccc2f6a892f4a40a7e788zhanyong.wan  static ::std::string Format(const ToPrint* value) {
13156b625a794e8a0e314f3ccc2f6a892f4a40a7e788zhanyong.wan    return FormatForComparison<const ToPrint*, OtherOperand>::Format(value);
13166b625a794e8a0e314f3ccc2f6a892f4a40a7e788zhanyong.wan  }
13176b625a794e8a0e314f3ccc2f6a892f4a40a7e788zhanyong.wan};
13186b625a794e8a0e314f3ccc2f6a892f4a40a7e788zhanyong.wan
13196b625a794e8a0e314f3ccc2f6a892f4a40a7e788zhanyong.wan// By default, print C string as pointers to be safe, as we don't know
13206b625a794e8a0e314f3ccc2f6a892f4a40a7e788zhanyong.wan// whether they actually point to a NUL-terminated string.
13216b625a794e8a0e314f3ccc2f6a892f4a40a7e788zhanyong.wan
13226b625a794e8a0e314f3ccc2f6a892f4a40a7e788zhanyong.wan#define GTEST_IMPL_FORMAT_C_STRING_AS_POINTER_(CharType)                \
13236b625a794e8a0e314f3ccc2f6a892f4a40a7e788zhanyong.wan  template <typename OtherOperand>                                      \
13246b625a794e8a0e314f3ccc2f6a892f4a40a7e788zhanyong.wan  class FormatForComparison<CharType*, OtherOperand> {                  \
13256b625a794e8a0e314f3ccc2f6a892f4a40a7e788zhanyong.wan   public:                                                              \
13266b625a794e8a0e314f3ccc2f6a892f4a40a7e788zhanyong.wan    static ::std::string Format(CharType* value) {                      \
13276b625a794e8a0e314f3ccc2f6a892f4a40a7e788zhanyong.wan      return ::testing::PrintToString(static_cast<const void*>(value)); \
13286b625a794e8a0e314f3ccc2f6a892f4a40a7e788zhanyong.wan    }                                                                   \
13296b625a794e8a0e314f3ccc2f6a892f4a40a7e788zhanyong.wan  }
13306b625a794e8a0e314f3ccc2f6a892f4a40a7e788zhanyong.wan
13316b625a794e8a0e314f3ccc2f6a892f4a40a7e788zhanyong.wanGTEST_IMPL_FORMAT_C_STRING_AS_POINTER_(char);
13326b625a794e8a0e314f3ccc2f6a892f4a40a7e788zhanyong.wanGTEST_IMPL_FORMAT_C_STRING_AS_POINTER_(const char);
13336b625a794e8a0e314f3ccc2f6a892f4a40a7e788zhanyong.wanGTEST_IMPL_FORMAT_C_STRING_AS_POINTER_(wchar_t);
13346b625a794e8a0e314f3ccc2f6a892f4a40a7e788zhanyong.wanGTEST_IMPL_FORMAT_C_STRING_AS_POINTER_(const wchar_t);
13356b625a794e8a0e314f3ccc2f6a892f4a40a7e788zhanyong.wan
13366b625a794e8a0e314f3ccc2f6a892f4a40a7e788zhanyong.wan#undef GTEST_IMPL_FORMAT_C_STRING_AS_POINTER_
13376b625a794e8a0e314f3ccc2f6a892f4a40a7e788zhanyong.wan
13386b625a794e8a0e314f3ccc2f6a892f4a40a7e788zhanyong.wan// If a C string is compared with an STL string object, we know it's meant
13396b625a794e8a0e314f3ccc2f6a892f4a40a7e788zhanyong.wan// to point to a NUL-terminated string, and thus can print it as a string.
13406b625a794e8a0e314f3ccc2f6a892f4a40a7e788zhanyong.wan
13416b625a794e8a0e314f3ccc2f6a892f4a40a7e788zhanyong.wan#define GTEST_IMPL_FORMAT_C_STRING_AS_STRING_(CharType, OtherStringType) \
13426b625a794e8a0e314f3ccc2f6a892f4a40a7e788zhanyong.wan  template <>                                                           \
13436b625a794e8a0e314f3ccc2f6a892f4a40a7e788zhanyong.wan  class FormatForComparison<CharType*, OtherStringType> {               \
13446b625a794e8a0e314f3ccc2f6a892f4a40a7e788zhanyong.wan   public:                                                              \
13456b625a794e8a0e314f3ccc2f6a892f4a40a7e788zhanyong.wan    static ::std::string Format(CharType* value) {                      \
13466b625a794e8a0e314f3ccc2f6a892f4a40a7e788zhanyong.wan      return ::testing::PrintToString(value);                           \
13476b625a794e8a0e314f3ccc2f6a892f4a40a7e788zhanyong.wan    }                                                                   \
13486b625a794e8a0e314f3ccc2f6a892f4a40a7e788zhanyong.wan  }
13496b625a794e8a0e314f3ccc2f6a892f4a40a7e788zhanyong.wan
13506b625a794e8a0e314f3ccc2f6a892f4a40a7e788zhanyong.wanGTEST_IMPL_FORMAT_C_STRING_AS_STRING_(char, ::std::string);
13516b625a794e8a0e314f3ccc2f6a892f4a40a7e788zhanyong.wanGTEST_IMPL_FORMAT_C_STRING_AS_STRING_(const char, ::std::string);
13526b625a794e8a0e314f3ccc2f6a892f4a40a7e788zhanyong.wan
13536b625a794e8a0e314f3ccc2f6a892f4a40a7e788zhanyong.wan#if GTEST_HAS_GLOBAL_STRING
13546b625a794e8a0e314f3ccc2f6a892f4a40a7e788zhanyong.wanGTEST_IMPL_FORMAT_C_STRING_AS_STRING_(char, ::string);
13556b625a794e8a0e314f3ccc2f6a892f4a40a7e788zhanyong.wanGTEST_IMPL_FORMAT_C_STRING_AS_STRING_(const char, ::string);
13566b625a794e8a0e314f3ccc2f6a892f4a40a7e788zhanyong.wan#endif
13576b625a794e8a0e314f3ccc2f6a892f4a40a7e788zhanyong.wan
13586b625a794e8a0e314f3ccc2f6a892f4a40a7e788zhanyong.wan#if GTEST_HAS_GLOBAL_WSTRING
13596b625a794e8a0e314f3ccc2f6a892f4a40a7e788zhanyong.wanGTEST_IMPL_FORMAT_C_STRING_AS_STRING_(wchar_t, ::wstring);
13606b625a794e8a0e314f3ccc2f6a892f4a40a7e788zhanyong.wanGTEST_IMPL_FORMAT_C_STRING_AS_STRING_(const wchar_t, ::wstring);
13616b625a794e8a0e314f3ccc2f6a892f4a40a7e788zhanyong.wan#endif
13626b625a794e8a0e314f3ccc2f6a892f4a40a7e788zhanyong.wan
13636b625a794e8a0e314f3ccc2f6a892f4a40a7e788zhanyong.wan#if GTEST_HAS_STD_WSTRING
13646b625a794e8a0e314f3ccc2f6a892f4a40a7e788zhanyong.wanGTEST_IMPL_FORMAT_C_STRING_AS_STRING_(wchar_t, ::std::wstring);
13656b625a794e8a0e314f3ccc2f6a892f4a40a7e788zhanyong.wanGTEST_IMPL_FORMAT_C_STRING_AS_STRING_(const wchar_t, ::std::wstring);
13666b625a794e8a0e314f3ccc2f6a892f4a40a7e788zhanyong.wan#endif
13676b625a794e8a0e314f3ccc2f6a892f4a40a7e788zhanyong.wan
13686b625a794e8a0e314f3ccc2f6a892f4a40a7e788zhanyong.wan#undef GTEST_IMPL_FORMAT_C_STRING_AS_STRING_
13696b625a794e8a0e314f3ccc2f6a892f4a40a7e788zhanyong.wan
13704b6829f0d28990dd645e16386eb226d0f10c8731shiqian// Formats a comparison assertion (e.g. ASSERT_EQ, EXPECT_LT, and etc)
13714b6829f0d28990dd645e16386eb226d0f10c8731shiqian// operand to be used in a failure message.  The type (but not value)
13724b6829f0d28990dd645e16386eb226d0f10c8731shiqian// of the other operand may affect the format.  This allows us to
13734b6829f0d28990dd645e16386eb226d0f10c8731shiqian// print a char* as a raw pointer when it is compared against another
13746b625a794e8a0e314f3ccc2f6a892f4a40a7e788zhanyong.wan// char* or void*, and print it as a C string when it is compared
13756b625a794e8a0e314f3ccc2f6a892f4a40a7e788zhanyong.wan// against an std::string object, for example.
13764b6829f0d28990dd645e16386eb226d0f10c8731shiqian//
13774b6829f0d28990dd645e16386eb226d0f10c8731shiqian// INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM.
13784b6829f0d28990dd645e16386eb226d0f10c8731shiqiantemplate <typename T1, typename T2>
137903c314931649a999b0cf5deb0a434a1009157416jgm@google.comstd::string FormatForComparisonFailureMessage(
138003c314931649a999b0cf5deb0a434a1009157416jgm@google.com    const T1& value, const T2& /* other_operand */) {
13816b625a794e8a0e314f3ccc2f6a892f4a40a7e788zhanyong.wan  return FormatForComparison<T1, T2>::Format(value);
13824b6829f0d28990dd645e16386eb226d0f10c8731shiqian}
13834b6829f0d28990dd645e16386eb226d0f10c8731shiqian
13844b6829f0d28990dd645e16386eb226d0f10c8731shiqian// The helper function for {ASSERT|EXPECT}_EQ.
13854b6829f0d28990dd645e16386eb226d0f10c8731shiqiantemplate <typename T1, typename T2>
13864b6829f0d28990dd645e16386eb226d0f10c8731shiqianAssertionResult CmpHelperEQ(const char* expected_expression,
13874b6829f0d28990dd645e16386eb226d0f10c8731shiqian                            const char* actual_expression,
13884b6829f0d28990dd645e16386eb226d0f10c8731shiqian                            const T1& expected,
13894b6829f0d28990dd645e16386eb226d0f10c8731shiqian                            const T2& actual) {
1390b0a12f719caf4e37894868334ce41b1864b0be53zhanyong.wan#ifdef _MSC_VER
1391733a54a398766289b74cf3daebe083d7115cf388zhanyong.wan# pragma warning(push)          // Saves the current warning state.
1392733a54a398766289b74cf3daebe083d7115cf388zhanyong.wan# pragma warning(disable:4389)  // Temporarily disables warning on
13936b625a794e8a0e314f3ccc2f6a892f4a40a7e788zhanyong.wan                                // signed/unsigned mismatch.
1394b0a12f719caf4e37894868334ce41b1864b0be53zhanyong.wan#endif
1395b0a12f719caf4e37894868334ce41b1864b0be53zhanyong.wan
13964b6829f0d28990dd645e16386eb226d0f10c8731shiqian  if (expected == actual) {
13974b6829f0d28990dd645e16386eb226d0f10c8731shiqian    return AssertionSuccess();
13984b6829f0d28990dd645e16386eb226d0f10c8731shiqian  }
13994b6829f0d28990dd645e16386eb226d0f10c8731shiqian
1400b0a12f719caf4e37894868334ce41b1864b0be53zhanyong.wan#ifdef _MSC_VER
1401733a54a398766289b74cf3daebe083d7115cf388zhanyong.wan# pragma warning(pop)          // Restores the warning state.
1402b0a12f719caf4e37894868334ce41b1864b0be53zhanyong.wan#endif
1403b0a12f719caf4e37894868334ce41b1864b0be53zhanyong.wan
14044b6829f0d28990dd645e16386eb226d0f10c8731shiqian  return EqFailure(expected_expression,
14054b6829f0d28990dd645e16386eb226d0f10c8731shiqian                   actual_expression,
14064b6829f0d28990dd645e16386eb226d0f10c8731shiqian                   FormatForComparisonFailureMessage(expected, actual),
14074b6829f0d28990dd645e16386eb226d0f10c8731shiqian                   FormatForComparisonFailureMessage(actual, expected),
14084b6829f0d28990dd645e16386eb226d0f10c8731shiqian                   false);
14094b6829f0d28990dd645e16386eb226d0f10c8731shiqian}
14104b6829f0d28990dd645e16386eb226d0f10c8731shiqian
14114b6829f0d28990dd645e16386eb226d0f10c8731shiqian// With this overloaded version, we allow anonymous enums to be used
14124b6829f0d28990dd645e16386eb226d0f10c8731shiqian// in {ASSERT|EXPECT}_EQ when compiled with gcc 4, as anonymous enums
14134b6829f0d28990dd645e16386eb226d0f10c8731shiqian// can be implicitly cast to BiggestInt.
14141ce454985edf37b17f2f266d499c2b7dec339002zhanyong.wanGTEST_API_ AssertionResult CmpHelperEQ(const char* expected_expression,
14151ce454985edf37b17f2f266d499c2b7dec339002zhanyong.wan                                       const char* actual_expression,
14161ce454985edf37b17f2f266d499c2b7dec339002zhanyong.wan                                       BiggestInt expected,
14171ce454985edf37b17f2f266d499c2b7dec339002zhanyong.wan                                       BiggestInt actual);
14184b6829f0d28990dd645e16386eb226d0f10c8731shiqian
14194b6829f0d28990dd645e16386eb226d0f10c8731shiqian// The helper class for {ASSERT|EXPECT}_EQ.  The template argument
14204b6829f0d28990dd645e16386eb226d0f10c8731shiqian// lhs_is_null_literal is true iff the first argument to ASSERT_EQ()
14214b6829f0d28990dd645e16386eb226d0f10c8731shiqian// is a null pointer literal.  The following default implementation is
14224b6829f0d28990dd645e16386eb226d0f10c8731shiqian// for lhs_is_null_literal being false.
14234b6829f0d28990dd645e16386eb226d0f10c8731shiqiantemplate <bool lhs_is_null_literal>
14244b6829f0d28990dd645e16386eb226d0f10c8731shiqianclass EqHelper {
14254b6829f0d28990dd645e16386eb226d0f10c8731shiqian public:
14264b6829f0d28990dd645e16386eb226d0f10c8731shiqian  // This templatized version is for the general case.
14274b6829f0d28990dd645e16386eb226d0f10c8731shiqian  template <typename T1, typename T2>
14284b6829f0d28990dd645e16386eb226d0f10c8731shiqian  static AssertionResult Compare(const char* expected_expression,
14294b6829f0d28990dd645e16386eb226d0f10c8731shiqian                                 const char* actual_expression,
14304b6829f0d28990dd645e16386eb226d0f10c8731shiqian                                 const T1& expected,
14314b6829f0d28990dd645e16386eb226d0f10c8731shiqian                                 const T2& actual) {
14324b6829f0d28990dd645e16386eb226d0f10c8731shiqian    return CmpHelperEQ(expected_expression, actual_expression, expected,
14334b6829f0d28990dd645e16386eb226d0f10c8731shiqian                       actual);
14344b6829f0d28990dd645e16386eb226d0f10c8731shiqian  }
14354b6829f0d28990dd645e16386eb226d0f10c8731shiqian
14364b6829f0d28990dd645e16386eb226d0f10c8731shiqian  // With this overloaded version, we allow anonymous enums to be used
14374b6829f0d28990dd645e16386eb226d0f10c8731shiqian  // in {ASSERT|EXPECT}_EQ when compiled with gcc 4, as anonymous
14384b6829f0d28990dd645e16386eb226d0f10c8731shiqian  // enums can be implicitly cast to BiggestInt.
14394b6829f0d28990dd645e16386eb226d0f10c8731shiqian  //
14404b6829f0d28990dd645e16386eb226d0f10c8731shiqian  // Even though its body looks the same as the above version, we
14414b6829f0d28990dd645e16386eb226d0f10c8731shiqian  // cannot merge the two, as it will make anonymous enums unhappy.
14424b6829f0d28990dd645e16386eb226d0f10c8731shiqian  static AssertionResult Compare(const char* expected_expression,
14434b6829f0d28990dd645e16386eb226d0f10c8731shiqian                                 const char* actual_expression,
14444b6829f0d28990dd645e16386eb226d0f10c8731shiqian                                 BiggestInt expected,
14454b6829f0d28990dd645e16386eb226d0f10c8731shiqian                                 BiggestInt actual) {
14464b6829f0d28990dd645e16386eb226d0f10c8731shiqian    return CmpHelperEQ(expected_expression, actual_expression, expected,
14474b6829f0d28990dd645e16386eb226d0f10c8731shiqian                       actual);
14484b6829f0d28990dd645e16386eb226d0f10c8731shiqian  }
14494b6829f0d28990dd645e16386eb226d0f10c8731shiqian};
14504b6829f0d28990dd645e16386eb226d0f10c8731shiqian
14514b6829f0d28990dd645e16386eb226d0f10c8731shiqian// This specialization is used when the first argument to ASSERT_EQ()
14525e255e0b6affbdc56718d81f0bb5d1b802a1c6c2zhanyong.wan// is a null pointer literal, like NULL, false, or 0.
14534b6829f0d28990dd645e16386eb226d0f10c8731shiqiantemplate <>
14544b6829f0d28990dd645e16386eb226d0f10c8731shiqianclass EqHelper<true> {
14554b6829f0d28990dd645e16386eb226d0f10c8731shiqian public:
14564b6829f0d28990dd645e16386eb226d0f10c8731shiqian  // We define two overloaded versions of Compare().  The first
14574b6829f0d28990dd645e16386eb226d0f10c8731shiqian  // version will be picked when the second argument to ASSERT_EQ() is
14584b6829f0d28990dd645e16386eb226d0f10c8731shiqian  // NOT a pointer, e.g. ASSERT_EQ(0, AnIntFunction()) or
14594b6829f0d28990dd645e16386eb226d0f10c8731shiqian  // EXPECT_EQ(false, a_bool).
14604b6829f0d28990dd645e16386eb226d0f10c8731shiqian  template <typename T1, typename T2>
14615e255e0b6affbdc56718d81f0bb5d1b802a1c6c2zhanyong.wan  static AssertionResult Compare(
14625e255e0b6affbdc56718d81f0bb5d1b802a1c6c2zhanyong.wan      const char* expected_expression,
14635e255e0b6affbdc56718d81f0bb5d1b802a1c6c2zhanyong.wan      const char* actual_expression,
14645e255e0b6affbdc56718d81f0bb5d1b802a1c6c2zhanyong.wan      const T1& expected,
14655e255e0b6affbdc56718d81f0bb5d1b802a1c6c2zhanyong.wan      const T2& actual,
14665e255e0b6affbdc56718d81f0bb5d1b802a1c6c2zhanyong.wan      // The following line prevents this overload from being considered if T2
14675e255e0b6affbdc56718d81f0bb5d1b802a1c6c2zhanyong.wan      // is not a pointer type.  We need this because ASSERT_EQ(NULL, my_ptr)
14685e255e0b6affbdc56718d81f0bb5d1b802a1c6c2zhanyong.wan      // expands to Compare("", "", NULL, my_ptr), which requires a conversion
14695e255e0b6affbdc56718d81f0bb5d1b802a1c6c2zhanyong.wan      // to match the Secret* in the other overload, which would otherwise make
14705e255e0b6affbdc56718d81f0bb5d1b802a1c6c2zhanyong.wan      // this template match better.
14715e255e0b6affbdc56718d81f0bb5d1b802a1c6c2zhanyong.wan      typename EnableIf<!is_pointer<T2>::value>::type* = 0) {
14724b6829f0d28990dd645e16386eb226d0f10c8731shiqian    return CmpHelperEQ(expected_expression, actual_expression, expected,
14734b6829f0d28990dd645e16386eb226d0f10c8731shiqian                       actual);
14744b6829f0d28990dd645e16386eb226d0f10c8731shiqian  }
14754b6829f0d28990dd645e16386eb226d0f10c8731shiqian
14765e255e0b6affbdc56718d81f0bb5d1b802a1c6c2zhanyong.wan  // This version will be picked when the second argument to ASSERT_EQ() is a
14775e255e0b6affbdc56718d81f0bb5d1b802a1c6c2zhanyong.wan  // pointer, e.g. ASSERT_EQ(NULL, a_pointer).
14785e255e0b6affbdc56718d81f0bb5d1b802a1c6c2zhanyong.wan  template <typename T>
14795e255e0b6affbdc56718d81f0bb5d1b802a1c6c2zhanyong.wan  static AssertionResult Compare(
14805e255e0b6affbdc56718d81f0bb5d1b802a1c6c2zhanyong.wan      const char* expected_expression,
14815e255e0b6affbdc56718d81f0bb5d1b802a1c6c2zhanyong.wan      const char* actual_expression,
14825e255e0b6affbdc56718d81f0bb5d1b802a1c6c2zhanyong.wan      // We used to have a second template parameter instead of Secret*.  That
14835e255e0b6affbdc56718d81f0bb5d1b802a1c6c2zhanyong.wan      // template parameter would deduce to 'long', making this a better match
14845e255e0b6affbdc56718d81f0bb5d1b802a1c6c2zhanyong.wan      // than the first overload even without the first overload's EnableIf.
14855e255e0b6affbdc56718d81f0bb5d1b802a1c6c2zhanyong.wan      // Unfortunately, gcc with -Wconversion-null warns when "passing NULL to
14865e255e0b6affbdc56718d81f0bb5d1b802a1c6c2zhanyong.wan      // non-pointer argument" (even a deduced integral argument), so the old
14875e255e0b6affbdc56718d81f0bb5d1b802a1c6c2zhanyong.wan      // implementation caused warnings in user code.
14885e255e0b6affbdc56718d81f0bb5d1b802a1c6c2zhanyong.wan      Secret* /* expected (NULL) */,
14895e255e0b6affbdc56718d81f0bb5d1b802a1c6c2zhanyong.wan      T* actual) {
14904b6829f0d28990dd645e16386eb226d0f10c8731shiqian    // We already know that 'expected' is a null pointer.
14914b6829f0d28990dd645e16386eb226d0f10c8731shiqian    return CmpHelperEQ(expected_expression, actual_expression,
14925e255e0b6affbdc56718d81f0bb5d1b802a1c6c2zhanyong.wan                       static_cast<T*>(NULL), actual);
14934b6829f0d28990dd645e16386eb226d0f10c8731shiqian  }
14944b6829f0d28990dd645e16386eb226d0f10c8731shiqian};
14954b6829f0d28990dd645e16386eb226d0f10c8731shiqian
14964b6829f0d28990dd645e16386eb226d0f10c8731shiqian// A macro for implementing the helper functions needed to implement
14974b6829f0d28990dd645e16386eb226d0f10c8731shiqian// ASSERT_?? and EXPECT_??.  It is here just to avoid copy-and-paste
14984b6829f0d28990dd645e16386eb226d0f10c8731shiqian// of similar code.
14994b6829f0d28990dd645e16386eb226d0f10c8731shiqian//
15004b6829f0d28990dd645e16386eb226d0f10c8731shiqian// For each templatized helper function, we also define an overloaded
15014b6829f0d28990dd645e16386eb226d0f10c8731shiqian// version for BiggestInt in order to reduce code bloat and allow
15024b6829f0d28990dd645e16386eb226d0f10c8731shiqian// anonymous enums to be used with {ASSERT|EXPECT}_?? when compiled
15034b6829f0d28990dd645e16386eb226d0f10c8731shiqian// with gcc 4.
15044b6829f0d28990dd645e16386eb226d0f10c8731shiqian//
15054b6829f0d28990dd645e16386eb226d0f10c8731shiqian// INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM.
1506e44602ec83c65102035ce5304ae8de0cb16e9e56shiqian#define GTEST_IMPL_CMP_HELPER_(op_name, op)\
15074b6829f0d28990dd645e16386eb226d0f10c8731shiqiantemplate <typename T1, typename T2>\
15084b6829f0d28990dd645e16386eb226d0f10c8731shiqianAssertionResult CmpHelper##op_name(const char* expr1, const char* expr2, \
15094b6829f0d28990dd645e16386eb226d0f10c8731shiqian                                   const T1& val1, const T2& val2) {\
15104b6829f0d28990dd645e16386eb226d0f10c8731shiqian  if (val1 op val2) {\
15114b6829f0d28990dd645e16386eb226d0f10c8731shiqian    return AssertionSuccess();\
15124b6829f0d28990dd645e16386eb226d0f10c8731shiqian  } else {\
15133928282cf0323a42e85e7885bddc54409021166fzhanyong.wan    return AssertionFailure() \
15143928282cf0323a42e85e7885bddc54409021166fzhanyong.wan        << "Expected: (" << expr1 << ") " #op " (" << expr2\
15154b6829f0d28990dd645e16386eb226d0f10c8731shiqian        << "), actual: " << FormatForComparisonFailureMessage(val1, val2)\
15164b6829f0d28990dd645e16386eb226d0f10c8731shiqian        << " vs " << FormatForComparisonFailureMessage(val2, val1);\
15174b6829f0d28990dd645e16386eb226d0f10c8731shiqian  }\
15184b6829f0d28990dd645e16386eb226d0f10c8731shiqian}\
15191ce454985edf37b17f2f266d499c2b7dec339002zhanyong.wanGTEST_API_ AssertionResult CmpHelper##op_name(\
15201ce454985edf37b17f2f266d499c2b7dec339002zhanyong.wan    const char* expr1, const char* expr2, BiggestInt val1, BiggestInt val2)
15214b6829f0d28990dd645e16386eb226d0f10c8731shiqian
15224b6829f0d28990dd645e16386eb226d0f10c8731shiqian// INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM.
15234b6829f0d28990dd645e16386eb226d0f10c8731shiqian
15244b6829f0d28990dd645e16386eb226d0f10c8731shiqian// Implements the helper function for {ASSERT|EXPECT}_NE
15251ce454985edf37b17f2f266d499c2b7dec339002zhanyong.wanGTEST_IMPL_CMP_HELPER_(NE, !=);
15264b6829f0d28990dd645e16386eb226d0f10c8731shiqian// Implements the helper function for {ASSERT|EXPECT}_LE
15271ce454985edf37b17f2f266d499c2b7dec339002zhanyong.wanGTEST_IMPL_CMP_HELPER_(LE, <=);
15284b6829f0d28990dd645e16386eb226d0f10c8731shiqian// Implements the helper function for {ASSERT|EXPECT}_LT
152993fed47dbf8e6bc3d39d3f769cb5039551747257vladlosevGTEST_IMPL_CMP_HELPER_(LT, <);
15304b6829f0d28990dd645e16386eb226d0f10c8731shiqian// Implements the helper function for {ASSERT|EXPECT}_GE
15311ce454985edf37b17f2f266d499c2b7dec339002zhanyong.wanGTEST_IMPL_CMP_HELPER_(GE, >=);
15324b6829f0d28990dd645e16386eb226d0f10c8731shiqian// Implements the helper function for {ASSERT|EXPECT}_GT
153393fed47dbf8e6bc3d39d3f769cb5039551747257vladlosevGTEST_IMPL_CMP_HELPER_(GT, >);
15344b6829f0d28990dd645e16386eb226d0f10c8731shiqian
1535e44602ec83c65102035ce5304ae8de0cb16e9e56shiqian#undef GTEST_IMPL_CMP_HELPER_
15364b6829f0d28990dd645e16386eb226d0f10c8731shiqian
15374b6829f0d28990dd645e16386eb226d0f10c8731shiqian// The helper function for {ASSERT|EXPECT}_STREQ.
15384b6829f0d28990dd645e16386eb226d0f10c8731shiqian//
15394b6829f0d28990dd645e16386eb226d0f10c8731shiqian// INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM.
1540e4092294d7c1f38f303e8c7f67da31ba3e7e7d9azhanyong.wanGTEST_API_ AssertionResult CmpHelperSTREQ(const char* expected_expression,
1541e4092294d7c1f38f303e8c7f67da31ba3e7e7d9azhanyong.wan                                          const char* actual_expression,
1542e4092294d7c1f38f303e8c7f67da31ba3e7e7d9azhanyong.wan                                          const char* expected,
1543e4092294d7c1f38f303e8c7f67da31ba3e7e7d9azhanyong.wan                                          const char* actual);
15444b6829f0d28990dd645e16386eb226d0f10c8731shiqian
15454b6829f0d28990dd645e16386eb226d0f10c8731shiqian// The helper function for {ASSERT|EXPECT}_STRCASEEQ.
15464b6829f0d28990dd645e16386eb226d0f10c8731shiqian//
15474b6829f0d28990dd645e16386eb226d0f10c8731shiqian// INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM.
1548e4092294d7c1f38f303e8c7f67da31ba3e7e7d9azhanyong.wanGTEST_API_ AssertionResult CmpHelperSTRCASEEQ(const char* expected_expression,
1549e4092294d7c1f38f303e8c7f67da31ba3e7e7d9azhanyong.wan                                              const char* actual_expression,
1550e4092294d7c1f38f303e8c7f67da31ba3e7e7d9azhanyong.wan                                              const char* expected,
1551e4092294d7c1f38f303e8c7f67da31ba3e7e7d9azhanyong.wan                                              const char* actual);
15524b6829f0d28990dd645e16386eb226d0f10c8731shiqian
15534b6829f0d28990dd645e16386eb226d0f10c8731shiqian// The helper function for {ASSERT|EXPECT}_STRNE.
15544b6829f0d28990dd645e16386eb226d0f10c8731shiqian//
15554b6829f0d28990dd645e16386eb226d0f10c8731shiqian// INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM.
1556e4092294d7c1f38f303e8c7f67da31ba3e7e7d9azhanyong.wanGTEST_API_ AssertionResult CmpHelperSTRNE(const char* s1_expression,
1557e4092294d7c1f38f303e8c7f67da31ba3e7e7d9azhanyong.wan                                          const char* s2_expression,
1558e4092294d7c1f38f303e8c7f67da31ba3e7e7d9azhanyong.wan                                          const char* s1,
1559e4092294d7c1f38f303e8c7f67da31ba3e7e7d9azhanyong.wan                                          const char* s2);
15604b6829f0d28990dd645e16386eb226d0f10c8731shiqian
15614b6829f0d28990dd645e16386eb226d0f10c8731shiqian// The helper function for {ASSERT|EXPECT}_STRCASENE.
15624b6829f0d28990dd645e16386eb226d0f10c8731shiqian//
15634b6829f0d28990dd645e16386eb226d0f10c8731shiqian// INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM.
1564e4092294d7c1f38f303e8c7f67da31ba3e7e7d9azhanyong.wanGTEST_API_ AssertionResult CmpHelperSTRCASENE(const char* s1_expression,
1565e4092294d7c1f38f303e8c7f67da31ba3e7e7d9azhanyong.wan                                              const char* s2_expression,
1566e4092294d7c1f38f303e8c7f67da31ba3e7e7d9azhanyong.wan                                              const char* s1,
1567e4092294d7c1f38f303e8c7f67da31ba3e7e7d9azhanyong.wan                                              const char* s2);
15684b6829f0d28990dd645e16386eb226d0f10c8731shiqian
15694b6829f0d28990dd645e16386eb226d0f10c8731shiqian
15704b6829f0d28990dd645e16386eb226d0f10c8731shiqian// Helper function for *_STREQ on wide strings.
15714b6829f0d28990dd645e16386eb226d0f10c8731shiqian//
15724b6829f0d28990dd645e16386eb226d0f10c8731shiqian// INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM.
1573e4092294d7c1f38f303e8c7f67da31ba3e7e7d9azhanyong.wanGTEST_API_ AssertionResult CmpHelperSTREQ(const char* expected_expression,
1574e4092294d7c1f38f303e8c7f67da31ba3e7e7d9azhanyong.wan                                          const char* actual_expression,
1575e4092294d7c1f38f303e8c7f67da31ba3e7e7d9azhanyong.wan                                          const wchar_t* expected,
1576e4092294d7c1f38f303e8c7f67da31ba3e7e7d9azhanyong.wan                                          const wchar_t* actual);
15774b6829f0d28990dd645e16386eb226d0f10c8731shiqian
15784b6829f0d28990dd645e16386eb226d0f10c8731shiqian// Helper function for *_STRNE on wide strings.
15794b6829f0d28990dd645e16386eb226d0f10c8731shiqian//
15804b6829f0d28990dd645e16386eb226d0f10c8731shiqian// INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM.
1581e4092294d7c1f38f303e8c7f67da31ba3e7e7d9azhanyong.wanGTEST_API_ AssertionResult CmpHelperSTRNE(const char* s1_expression,
1582e4092294d7c1f38f303e8c7f67da31ba3e7e7d9azhanyong.wan                                          const char* s2_expression,
1583e4092294d7c1f38f303e8c7f67da31ba3e7e7d9azhanyong.wan                                          const wchar_t* s1,
1584e4092294d7c1f38f303e8c7f67da31ba3e7e7d9azhanyong.wan                                          const wchar_t* s2);
15854b6829f0d28990dd645e16386eb226d0f10c8731shiqian
15864b6829f0d28990dd645e16386eb226d0f10c8731shiqian}  // namespace internal
15874b6829f0d28990dd645e16386eb226d0f10c8731shiqian
15884b6829f0d28990dd645e16386eb226d0f10c8731shiqian// IsSubstring() and IsNotSubstring() are intended to be used as the
15894b6829f0d28990dd645e16386eb226d0f10c8731shiqian// first argument to {EXPECT,ASSERT}_PRED_FORMAT2(), not by
15904b6829f0d28990dd645e16386eb226d0f10c8731shiqian// themselves.  They check whether needle is a substring of haystack
15914b6829f0d28990dd645e16386eb226d0f10c8731shiqian// (NULL is considered a substring of itself only), and return an
15924b6829f0d28990dd645e16386eb226d0f10c8731shiqian// appropriate error message when they fail.
15934b6829f0d28990dd645e16386eb226d0f10c8731shiqian//
15944b6829f0d28990dd645e16386eb226d0f10c8731shiqian// The {needle,haystack}_expr arguments are the stringified
15954b6829f0d28990dd645e16386eb226d0f10c8731shiqian// expressions that generated the two real arguments.
15961ce454985edf37b17f2f266d499c2b7dec339002zhanyong.wanGTEST_API_ AssertionResult IsSubstring(
15974b6829f0d28990dd645e16386eb226d0f10c8731shiqian    const char* needle_expr, const char* haystack_expr,
15984b6829f0d28990dd645e16386eb226d0f10c8731shiqian    const char* needle, const char* haystack);
15991ce454985edf37b17f2f266d499c2b7dec339002zhanyong.wanGTEST_API_ AssertionResult IsSubstring(
16004b6829f0d28990dd645e16386eb226d0f10c8731shiqian    const char* needle_expr, const char* haystack_expr,
16014b6829f0d28990dd645e16386eb226d0f10c8731shiqian    const wchar_t* needle, const wchar_t* haystack);
16021ce454985edf37b17f2f266d499c2b7dec339002zhanyong.wanGTEST_API_ AssertionResult IsNotSubstring(
16034b6829f0d28990dd645e16386eb226d0f10c8731shiqian    const char* needle_expr, const char* haystack_expr,
16044b6829f0d28990dd645e16386eb226d0f10c8731shiqian    const char* needle, const char* haystack);
16051ce454985edf37b17f2f266d499c2b7dec339002zhanyong.wanGTEST_API_ AssertionResult IsNotSubstring(
16064b6829f0d28990dd645e16386eb226d0f10c8731shiqian    const char* needle_expr, const char* haystack_expr,
16074b6829f0d28990dd645e16386eb226d0f10c8731shiqian    const wchar_t* needle, const wchar_t* haystack);
16081ce454985edf37b17f2f266d499c2b7dec339002zhanyong.wanGTEST_API_ AssertionResult IsSubstring(
16094b6829f0d28990dd645e16386eb226d0f10c8731shiqian    const char* needle_expr, const char* haystack_expr,
16104b6829f0d28990dd645e16386eb226d0f10c8731shiqian    const ::std::string& needle, const ::std::string& haystack);
16111ce454985edf37b17f2f266d499c2b7dec339002zhanyong.wanGTEST_API_ AssertionResult IsNotSubstring(
16124b6829f0d28990dd645e16386eb226d0f10c8731shiqian    const char* needle_expr, const char* haystack_expr,
16134b6829f0d28990dd645e16386eb226d0f10c8731shiqian    const ::std::string& needle, const ::std::string& haystack);
16144b6829f0d28990dd645e16386eb226d0f10c8731shiqian
16154b6829f0d28990dd645e16386eb226d0f10c8731shiqian#if GTEST_HAS_STD_WSTRING
16161ce454985edf37b17f2f266d499c2b7dec339002zhanyong.wanGTEST_API_ AssertionResult IsSubstring(
16174b6829f0d28990dd645e16386eb226d0f10c8731shiqian    const char* needle_expr, const char* haystack_expr,
16184b6829f0d28990dd645e16386eb226d0f10c8731shiqian    const ::std::wstring& needle, const ::std::wstring& haystack);
16191ce454985edf37b17f2f266d499c2b7dec339002zhanyong.wanGTEST_API_ AssertionResult IsNotSubstring(
16204b6829f0d28990dd645e16386eb226d0f10c8731shiqian    const char* needle_expr, const char* haystack_expr,
16214b6829f0d28990dd645e16386eb226d0f10c8731shiqian    const ::std::wstring& needle, const ::std::wstring& haystack);
16224b6829f0d28990dd645e16386eb226d0f10c8731shiqian#endif  // GTEST_HAS_STD_WSTRING
16234b6829f0d28990dd645e16386eb226d0f10c8731shiqian
16244b6829f0d28990dd645e16386eb226d0f10c8731shiqiannamespace internal {
16254b6829f0d28990dd645e16386eb226d0f10c8731shiqian
16264b6829f0d28990dd645e16386eb226d0f10c8731shiqian// Helper template function for comparing floating-points.
16274b6829f0d28990dd645e16386eb226d0f10c8731shiqian//
16284b6829f0d28990dd645e16386eb226d0f10c8731shiqian// Template parameter:
16294b6829f0d28990dd645e16386eb226d0f10c8731shiqian//
16304b6829f0d28990dd645e16386eb226d0f10c8731shiqian//   RawType: the raw floating-point type (either float or double)
16314b6829f0d28990dd645e16386eb226d0f10c8731shiqian//
16324b6829f0d28990dd645e16386eb226d0f10c8731shiqian// INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM.
16334b6829f0d28990dd645e16386eb226d0f10c8731shiqiantemplate <typename RawType>
16344b6829f0d28990dd645e16386eb226d0f10c8731shiqianAssertionResult CmpHelperFloatingPointEQ(const char* expected_expression,
16354b6829f0d28990dd645e16386eb226d0f10c8731shiqian                                         const char* actual_expression,
16364b6829f0d28990dd645e16386eb226d0f10c8731shiqian                                         RawType expected,
16374b6829f0d28990dd645e16386eb226d0f10c8731shiqian                                         RawType actual) {
16384b6829f0d28990dd645e16386eb226d0f10c8731shiqian  const FloatingPoint<RawType> lhs(expected), rhs(actual);
16394b6829f0d28990dd645e16386eb226d0f10c8731shiqian
16404b6829f0d28990dd645e16386eb226d0f10c8731shiqian  if (lhs.AlmostEquals(rhs)) {
16414b6829f0d28990dd645e16386eb226d0f10c8731shiqian    return AssertionSuccess();
16424b6829f0d28990dd645e16386eb226d0f10c8731shiqian  }
16434b6829f0d28990dd645e16386eb226d0f10c8731shiqian
1644b15c73341d7a7abff7a04e1de18de59ba0f34009zhanyong.wan  ::std::stringstream expected_ss;
16454b6829f0d28990dd645e16386eb226d0f10c8731shiqian  expected_ss << std::setprecision(std::numeric_limits<RawType>::digits10 + 2)
16464b6829f0d28990dd645e16386eb226d0f10c8731shiqian              << expected;
16474b6829f0d28990dd645e16386eb226d0f10c8731shiqian
1648b15c73341d7a7abff7a04e1de18de59ba0f34009zhanyong.wan  ::std::stringstream actual_ss;
16494b6829f0d28990dd645e16386eb226d0f10c8731shiqian  actual_ss << std::setprecision(std::numeric_limits<RawType>::digits10 + 2)
16504b6829f0d28990dd645e16386eb226d0f10c8731shiqian            << actual;
16514b6829f0d28990dd645e16386eb226d0f10c8731shiqian
16524b6829f0d28990dd645e16386eb226d0f10c8731shiqian  return EqFailure(expected_expression,
16534b6829f0d28990dd645e16386eb226d0f10c8731shiqian                   actual_expression,
1654b15c73341d7a7abff7a04e1de18de59ba0f34009zhanyong.wan                   StringStreamToString(&expected_ss),
1655b15c73341d7a7abff7a04e1de18de59ba0f34009zhanyong.wan                   StringStreamToString(&actual_ss),
16564b6829f0d28990dd645e16386eb226d0f10c8731shiqian                   false);
16574b6829f0d28990dd645e16386eb226d0f10c8731shiqian}
16584b6829f0d28990dd645e16386eb226d0f10c8731shiqian
16594b6829f0d28990dd645e16386eb226d0f10c8731shiqian// Helper function for implementing ASSERT_NEAR.
16604b6829f0d28990dd645e16386eb226d0f10c8731shiqian//
16614b6829f0d28990dd645e16386eb226d0f10c8731shiqian// INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM.
1662e4092294d7c1f38f303e8c7f67da31ba3e7e7d9azhanyong.wanGTEST_API_ AssertionResult DoubleNearPredFormat(const char* expr1,
1663e4092294d7c1f38f303e8c7f67da31ba3e7e7d9azhanyong.wan                                                const char* expr2,
1664e4092294d7c1f38f303e8c7f67da31ba3e7e7d9azhanyong.wan                                                const char* abs_error_expr,
1665e4092294d7c1f38f303e8c7f67da31ba3e7e7d9azhanyong.wan                                                double val1,
1666e4092294d7c1f38f303e8c7f67da31ba3e7e7d9azhanyong.wan                                                double val2,
1667e4092294d7c1f38f303e8c7f67da31ba3e7e7d9azhanyong.wan                                                double abs_error);
16684b6829f0d28990dd645e16386eb226d0f10c8731shiqian
16694b6829f0d28990dd645e16386eb226d0f10c8731shiqian// INTERNAL IMPLEMENTATION - DO NOT USE IN USER CODE.
16704b6829f0d28990dd645e16386eb226d0f10c8731shiqian// A class that enables one to stream messages to assertion macros
1671e4092294d7c1f38f303e8c7f67da31ba3e7e7d9azhanyong.wanclass GTEST_API_ AssertHelper {
16724b6829f0d28990dd645e16386eb226d0f10c8731shiqian public:
16734b6829f0d28990dd645e16386eb226d0f10c8731shiqian  // Constructor.
1674334aaead71ccf797a18f2128c320f0304e724860zhanyong.wan  AssertHelper(TestPartResult::Type type,
1675334aaead71ccf797a18f2128c320f0304e724860zhanyong.wan               const char* file,
1676334aaead71ccf797a18f2128c320f0304e724860zhanyong.wan               int line,
16774b6829f0d28990dd645e16386eb226d0f10c8731shiqian               const char* message);
1678f39160b423e8f90902066cf6774e4180667dcbeezhanyong.wan  ~AssertHelper();
1679f39160b423e8f90902066cf6774e4180667dcbeezhanyong.wan
16804b6829f0d28990dd645e16386eb226d0f10c8731shiqian  // Message assignment is a semantic trick to enable assertion
1681e44602ec83c65102035ce5304ae8de0cb16e9e56shiqian  // streaming; see the GTEST_MESSAGE_ macro below.
16824b6829f0d28990dd645e16386eb226d0f10c8731shiqian  void operator=(const Message& message) const;
1683f39160b423e8f90902066cf6774e4180667dcbeezhanyong.wan
16844b6829f0d28990dd645e16386eb226d0f10c8731shiqian private:
1685f39160b423e8f90902066cf6774e4180667dcbeezhanyong.wan  // We put our data in a struct so that the size of the AssertHelper class can
1686f39160b423e8f90902066cf6774e4180667dcbeezhanyong.wan  // be as small as possible.  This is important because gcc is incapable of
1687f39160b423e8f90902066cf6774e4180667dcbeezhanyong.wan  // re-using stack space even for temporary variables, so every EXPECT_EQ
1688f39160b423e8f90902066cf6774e4180667dcbeezhanyong.wan  // reserves stack space for another AssertHelper.
1689f39160b423e8f90902066cf6774e4180667dcbeezhanyong.wan  struct AssertHelperData {
1690334aaead71ccf797a18f2128c320f0304e724860zhanyong.wan    AssertHelperData(TestPartResult::Type t,
1691334aaead71ccf797a18f2128c320f0304e724860zhanyong.wan                     const char* srcfile,
1692334aaead71ccf797a18f2128c320f0304e724860zhanyong.wan                     int line_num,
1693f39160b423e8f90902066cf6774e4180667dcbeezhanyong.wan                     const char* msg)
1694f39160b423e8f90902066cf6774e4180667dcbeezhanyong.wan        : type(t), file(srcfile), line(line_num), message(msg) { }
1695f39160b423e8f90902066cf6774e4180667dcbeezhanyong.wan
1696334aaead71ccf797a18f2128c320f0304e724860zhanyong.wan    TestPartResult::Type const type;
169703c314931649a999b0cf5deb0a434a1009157416jgm@google.com    const char* const file;
169803c314931649a999b0cf5deb0a434a1009157416jgm@google.com    int const line;
169903c314931649a999b0cf5deb0a434a1009157416jgm@google.com    std::string const message;
1700f39160b423e8f90902066cf6774e4180667dcbeezhanyong.wan
1701f39160b423e8f90902066cf6774e4180667dcbeezhanyong.wan   private:
1702f39160b423e8f90902066cf6774e4180667dcbeezhanyong.wan    GTEST_DISALLOW_COPY_AND_ASSIGN_(AssertHelperData);
1703f39160b423e8f90902066cf6774e4180667dcbeezhanyong.wan  };
1704f39160b423e8f90902066cf6774e4180667dcbeezhanyong.wan
1705f39160b423e8f90902066cf6774e4180667dcbeezhanyong.wan  AssertHelperData* const data_;
17064b6829f0d28990dd645e16386eb226d0f10c8731shiqian
1707e44602ec83c65102035ce5304ae8de0cb16e9e56shiqian  GTEST_DISALLOW_COPY_AND_ASSIGN_(AssertHelper);
17084b6829f0d28990dd645e16386eb226d0f10c8731shiqian};
17094b6829f0d28990dd645e16386eb226d0f10c8731shiqian
17104b6829f0d28990dd645e16386eb226d0f10c8731shiqian}  // namespace internal
17114b6829f0d28990dd645e16386eb226d0f10c8731shiqian
17124cd62602913a032a7aec091d4c8055ff9af95e37zhanyong.wan#if GTEST_HAS_PARAM_TEST
171371874246c3a054977d94fd4729576484c2af62e9zhanyong.wan// The pure interface class that all value-parameterized tests inherit from.
171471874246c3a054977d94fd4729576484c2af62e9zhanyong.wan// A value-parameterized class must inherit from both ::testing::Test and
171571874246c3a054977d94fd4729576484c2af62e9zhanyong.wan// ::testing::WithParamInterface. In most cases that just means inheriting
171671874246c3a054977d94fd4729576484c2af62e9zhanyong.wan// from ::testing::TestWithParam, but more complicated test hierarchies
171771874246c3a054977d94fd4729576484c2af62e9zhanyong.wan// may need to inherit from Test and WithParamInterface at different levels.
1718f904a612d9444ab36c07a8e619c113432e046f49vladlosev//
171971874246c3a054977d94fd4729576484c2af62e9zhanyong.wan// This interface has support for accessing the test parameter value via
1720f904a612d9444ab36c07a8e619c113432e046f49vladlosev// the GetParam() method.
1721f904a612d9444ab36c07a8e619c113432e046f49vladlosev//
1722f904a612d9444ab36c07a8e619c113432e046f49vladlosev// Use it with one of the parameter generator defining functions, like Range(),
1723f904a612d9444ab36c07a8e619c113432e046f49vladlosev// Values(), ValuesIn(), Bool(), and Combine().
1724f904a612d9444ab36c07a8e619c113432e046f49vladlosev//
1725f904a612d9444ab36c07a8e619c113432e046f49vladlosev// class FooTest : public ::testing::TestWithParam<int> {
1726f904a612d9444ab36c07a8e619c113432e046f49vladlosev//  protected:
1727f904a612d9444ab36c07a8e619c113432e046f49vladlosev//   FooTest() {
1728f904a612d9444ab36c07a8e619c113432e046f49vladlosev//     // Can use GetParam() here.
1729f904a612d9444ab36c07a8e619c113432e046f49vladlosev//   }
1730f904a612d9444ab36c07a8e619c113432e046f49vladlosev//   virtual ~FooTest() {
1731f904a612d9444ab36c07a8e619c113432e046f49vladlosev//     // Can use GetParam() here.
1732f904a612d9444ab36c07a8e619c113432e046f49vladlosev//   }
1733f904a612d9444ab36c07a8e619c113432e046f49vladlosev//   virtual void SetUp() {
1734f904a612d9444ab36c07a8e619c113432e046f49vladlosev//     // Can use GetParam() here.
1735f904a612d9444ab36c07a8e619c113432e046f49vladlosev//   }
1736f904a612d9444ab36c07a8e619c113432e046f49vladlosev//   virtual void TearDown {
1737f904a612d9444ab36c07a8e619c113432e046f49vladlosev//     // Can use GetParam() here.
1738f904a612d9444ab36c07a8e619c113432e046f49vladlosev//   }
1739f904a612d9444ab36c07a8e619c113432e046f49vladlosev// };
1740f904a612d9444ab36c07a8e619c113432e046f49vladlosev// TEST_P(FooTest, DoesBar) {
1741f904a612d9444ab36c07a8e619c113432e046f49vladlosev//   // Can use GetParam() method here.
1742f904a612d9444ab36c07a8e619c113432e046f49vladlosev//   Foo foo;
1743f904a612d9444ab36c07a8e619c113432e046f49vladlosev//   ASSERT_TRUE(foo.DoesBar(GetParam()));
1744f904a612d9444ab36c07a8e619c113432e046f49vladlosev// }
1745f904a612d9444ab36c07a8e619c113432e046f49vladlosev// INSTANTIATE_TEST_CASE_P(OneToTenRange, FooTest, ::testing::Range(1, 10));
1746f904a612d9444ab36c07a8e619c113432e046f49vladlosev
1747f904a612d9444ab36c07a8e619c113432e046f49vladlosevtemplate <typename T>
174871874246c3a054977d94fd4729576484c2af62e9zhanyong.wanclass WithParamInterface {
1749f904a612d9444ab36c07a8e619c113432e046f49vladlosev public:
1750f904a612d9444ab36c07a8e619c113432e046f49vladlosev  typedef T ParamType;
175171874246c3a054977d94fd4729576484c2af62e9zhanyong.wan  virtual ~WithParamInterface() {}
1752f904a612d9444ab36c07a8e619c113432e046f49vladlosev
1753f904a612d9444ab36c07a8e619c113432e046f49vladlosev  // The current parameter value. Is also available in the test fixture's
175471874246c3a054977d94fd4729576484c2af62e9zhanyong.wan  // constructor. This member function is non-static, even though it only
175571874246c3a054977d94fd4729576484c2af62e9zhanyong.wan  // references static data, to reduce the opportunity for incorrect uses
175671874246c3a054977d94fd4729576484c2af62e9zhanyong.wan  // like writing 'WithParamInterface<bool>::GetParam()' for a test that
175771874246c3a054977d94fd4729576484c2af62e9zhanyong.wan  // uses a fixture whose parameter type is int.
1758f904a612d9444ab36c07a8e619c113432e046f49vladlosev  const ParamType& GetParam() const { return *parameter_; }
1759f904a612d9444ab36c07a8e619c113432e046f49vladlosev
1760f904a612d9444ab36c07a8e619c113432e046f49vladlosev private:
1761f904a612d9444ab36c07a8e619c113432e046f49vladlosev  // Sets parameter value. The caller is responsible for making sure the value
1762f904a612d9444ab36c07a8e619c113432e046f49vladlosev  // remains alive and unchanged throughout the current test.
1763f904a612d9444ab36c07a8e619c113432e046f49vladlosev  static void SetParam(const ParamType* parameter) {
1764f904a612d9444ab36c07a8e619c113432e046f49vladlosev    parameter_ = parameter;
1765f904a612d9444ab36c07a8e619c113432e046f49vladlosev  }
1766f904a612d9444ab36c07a8e619c113432e046f49vladlosev
1767f904a612d9444ab36c07a8e619c113432e046f49vladlosev  // Static value used for accessing parameter during a test lifetime.
1768f904a612d9444ab36c07a8e619c113432e046f49vladlosev  static const ParamType* parameter_;
1769f904a612d9444ab36c07a8e619c113432e046f49vladlosev
177071874246c3a054977d94fd4729576484c2af62e9zhanyong.wan  // TestClass must be a subclass of WithParamInterface<T> and Test.
1771f904a612d9444ab36c07a8e619c113432e046f49vladlosev  template <class TestClass> friend class internal::ParameterizedTestFactory;
1772f904a612d9444ab36c07a8e619c113432e046f49vladlosev};
1773f904a612d9444ab36c07a8e619c113432e046f49vladlosev
1774f904a612d9444ab36c07a8e619c113432e046f49vladlosevtemplate <typename T>
177571874246c3a054977d94fd4729576484c2af62e9zhanyong.wanconst T* WithParamInterface<T>::parameter_ = NULL;
177671874246c3a054977d94fd4729576484c2af62e9zhanyong.wan
177771874246c3a054977d94fd4729576484c2af62e9zhanyong.wan// Most value-parameterized classes can ignore the existence of
177871874246c3a054977d94fd4729576484c2af62e9zhanyong.wan// WithParamInterface, and can just inherit from ::testing::TestWithParam.
177971874246c3a054977d94fd4729576484c2af62e9zhanyong.wan
178071874246c3a054977d94fd4729576484c2af62e9zhanyong.wantemplate <typename T>
178171874246c3a054977d94fd4729576484c2af62e9zhanyong.wanclass TestWithParam : public Test, public WithParamInterface<T> {
178271874246c3a054977d94fd4729576484c2af62e9zhanyong.wan};
1783f904a612d9444ab36c07a8e619c113432e046f49vladlosev
1784f904a612d9444ab36c07a8e619c113432e046f49vladlosev#endif  // GTEST_HAS_PARAM_TEST
1785f904a612d9444ab36c07a8e619c113432e046f49vladlosev
17864b6829f0d28990dd645e16386eb226d0f10c8731shiqian// Macros for indicating success/failure in test code.
17874b6829f0d28990dd645e16386eb226d0f10c8731shiqian
17884b6829f0d28990dd645e16386eb226d0f10c8731shiqian// ADD_FAILURE unconditionally adds a failure to the current test.
17894b6829f0d28990dd645e16386eb226d0f10c8731shiqian// SUCCEED generates a success - it doesn't automatically make the
17904b6829f0d28990dd645e16386eb226d0f10c8731shiqian// current test successful, as a test is only successful when it has
17914b6829f0d28990dd645e16386eb226d0f10c8731shiqian// no failure.
17924b6829f0d28990dd645e16386eb226d0f10c8731shiqian//
17934b6829f0d28990dd645e16386eb226d0f10c8731shiqian// EXPECT_* verifies that a certain condition is satisfied.  If not,
17944b6829f0d28990dd645e16386eb226d0f10c8731shiqian// it behaves like ADD_FAILURE.  In particular:
17954b6829f0d28990dd645e16386eb226d0f10c8731shiqian//
17964b6829f0d28990dd645e16386eb226d0f10c8731shiqian//   EXPECT_TRUE  verifies that a Boolean condition is true.
17974b6829f0d28990dd645e16386eb226d0f10c8731shiqian//   EXPECT_FALSE verifies that a Boolean condition is false.
17984b6829f0d28990dd645e16386eb226d0f10c8731shiqian//
17994b6829f0d28990dd645e16386eb226d0f10c8731shiqian// FAIL and ASSERT_* are similar to ADD_FAILURE and EXPECT_*, except
18004b6829f0d28990dd645e16386eb226d0f10c8731shiqian// that they will also abort the current function on failure.  People
18014b6829f0d28990dd645e16386eb226d0f10c8731shiqian// usually want the fail-fast behavior of FAIL and ASSERT_*, but those
18024b6829f0d28990dd645e16386eb226d0f10c8731shiqian// writing data-driven tests often find themselves using ADD_FAILURE
18034b6829f0d28990dd645e16386eb226d0f10c8731shiqian// and EXPECT_* more.
18044b6829f0d28990dd645e16386eb226d0f10c8731shiqian
18054b6829f0d28990dd645e16386eb226d0f10c8731shiqian// Generates a nonfatal failure with a generic message.
1806e44602ec83c65102035ce5304ae8de0cb16e9e56shiqian#define ADD_FAILURE() GTEST_NONFATAL_FAILURE_("Failed")
18074b6829f0d28990dd645e16386eb226d0f10c8731shiqian
1808d5ad2ca82051c34b19af628a3c81131ca65010c2zhanyong.wan// Generates a nonfatal failure at the given source file location with
1809d5ad2ca82051c34b19af628a3c81131ca65010c2zhanyong.wan// a generic message.
1810d5ad2ca82051c34b19af628a3c81131ca65010c2zhanyong.wan#define ADD_FAILURE_AT(file, line) \
1811d5ad2ca82051c34b19af628a3c81131ca65010c2zhanyong.wan  GTEST_MESSAGE_AT_(file, line, "Failed", \
1812d5ad2ca82051c34b19af628a3c81131ca65010c2zhanyong.wan                    ::testing::TestPartResult::kNonFatalFailure)
1813d5ad2ca82051c34b19af628a3c81131ca65010c2zhanyong.wan
18144b6829f0d28990dd645e16386eb226d0f10c8731shiqian// Generates a fatal failure with a generic message.
18151618bb491b881bc39faa3317ee8778847646226dzhanyong.wan#define GTEST_FAIL() GTEST_FATAL_FAILURE_("Failed")
18161618bb491b881bc39faa3317ee8778847646226dzhanyong.wan
18171618bb491b881bc39faa3317ee8778847646226dzhanyong.wan// Define this macro to 1 to omit the definition of FAIL(), which is a
18181618bb491b881bc39faa3317ee8778847646226dzhanyong.wan// generic name and clashes with some other libraries.
18191618bb491b881bc39faa3317ee8778847646226dzhanyong.wan#if !GTEST_DONT_DEFINE_FAIL
1820733a54a398766289b74cf3daebe083d7115cf388zhanyong.wan# define FAIL() GTEST_FAIL()
18211618bb491b881bc39faa3317ee8778847646226dzhanyong.wan#endif
18224b6829f0d28990dd645e16386eb226d0f10c8731shiqian
18234b6829f0d28990dd645e16386eb226d0f10c8731shiqian// Generates a success with a generic message.
18241618bb491b881bc39faa3317ee8778847646226dzhanyong.wan#define GTEST_SUCCEED() GTEST_SUCCESS_("Succeeded")
18251618bb491b881bc39faa3317ee8778847646226dzhanyong.wan
18261618bb491b881bc39faa3317ee8778847646226dzhanyong.wan// Define this macro to 1 to omit the definition of SUCCEED(), which
18271618bb491b881bc39faa3317ee8778847646226dzhanyong.wan// is a generic name and clashes with some other libraries.
18281618bb491b881bc39faa3317ee8778847646226dzhanyong.wan#if !GTEST_DONT_DEFINE_SUCCEED
1829733a54a398766289b74cf3daebe083d7115cf388zhanyong.wan# define SUCCEED() GTEST_SUCCEED()
18301618bb491b881bc39faa3317ee8778847646226dzhanyong.wan#endif
18314b6829f0d28990dd645e16386eb226d0f10c8731shiqian
18329204c8ec6edda2013cd81bf3c93e9cb6c45b9bb5shiqian// Macros for testing exceptions.
18339204c8ec6edda2013cd81bf3c93e9cb6c45b9bb5shiqian//
18349204c8ec6edda2013cd81bf3c93e9cb6c45b9bb5shiqian//    * {ASSERT|EXPECT}_THROW(statement, expected_exception):
18359204c8ec6edda2013cd81bf3c93e9cb6c45b9bb5shiqian//         Tests that the statement throws the expected exception.
18369204c8ec6edda2013cd81bf3c93e9cb6c45b9bb5shiqian//    * {ASSERT|EXPECT}_NO_THROW(statement):
18379204c8ec6edda2013cd81bf3c93e9cb6c45b9bb5shiqian//         Tests that the statement doesn't throw any exception.
18389204c8ec6edda2013cd81bf3c93e9cb6c45b9bb5shiqian//    * {ASSERT|EXPECT}_ANY_THROW(statement):
18399204c8ec6edda2013cd81bf3c93e9cb6c45b9bb5shiqian//         Tests that the statement throws an exception.
18409204c8ec6edda2013cd81bf3c93e9cb6c45b9bb5shiqian
18419204c8ec6edda2013cd81bf3c93e9cb6c45b9bb5shiqian#define EXPECT_THROW(statement, expected_exception) \
1842e44602ec83c65102035ce5304ae8de0cb16e9e56shiqian  GTEST_TEST_THROW_(statement, expected_exception, GTEST_NONFATAL_FAILURE_)
18439204c8ec6edda2013cd81bf3c93e9cb6c45b9bb5shiqian#define EXPECT_NO_THROW(statement) \
1844e44602ec83c65102035ce5304ae8de0cb16e9e56shiqian  GTEST_TEST_NO_THROW_(statement, GTEST_NONFATAL_FAILURE_)
18459204c8ec6edda2013cd81bf3c93e9cb6c45b9bb5shiqian#define EXPECT_ANY_THROW(statement) \
1846e44602ec83c65102035ce5304ae8de0cb16e9e56shiqian  GTEST_TEST_ANY_THROW_(statement, GTEST_NONFATAL_FAILURE_)
18479204c8ec6edda2013cd81bf3c93e9cb6c45b9bb5shiqian#define ASSERT_THROW(statement, expected_exception) \
1848e44602ec83c65102035ce5304ae8de0cb16e9e56shiqian  GTEST_TEST_THROW_(statement, expected_exception, GTEST_FATAL_FAILURE_)
18499204c8ec6edda2013cd81bf3c93e9cb6c45b9bb5shiqian#define ASSERT_NO_THROW(statement) \
1850e44602ec83c65102035ce5304ae8de0cb16e9e56shiqian  GTEST_TEST_NO_THROW_(statement, GTEST_FATAL_FAILURE_)
18519204c8ec6edda2013cd81bf3c93e9cb6c45b9bb5shiqian#define ASSERT_ANY_THROW(statement) \
1852e44602ec83c65102035ce5304ae8de0cb16e9e56shiqian  GTEST_TEST_ANY_THROW_(statement, GTEST_FATAL_FAILURE_)
18539204c8ec6edda2013cd81bf3c93e9cb6c45b9bb5shiqian
1854fbd53a53c1e01dec71c65754cf73282e4759bc40vladlosev// Boolean assertions. Condition can be either a Boolean expression or an
1855fbd53a53c1e01dec71c65754cf73282e4759bc40vladlosev// AssertionResult. For more information on how to use AssertionResult with
1856fbd53a53c1e01dec71c65754cf73282e4759bc40vladlosev// these macros see comments on that class.
18574b6829f0d28990dd645e16386eb226d0f10c8731shiqian#define EXPECT_TRUE(condition) \
1858e44602ec83c65102035ce5304ae8de0cb16e9e56shiqian  GTEST_TEST_BOOLEAN_(condition, #condition, false, true, \
1859e44602ec83c65102035ce5304ae8de0cb16e9e56shiqian                      GTEST_NONFATAL_FAILURE_)
18604b6829f0d28990dd645e16386eb226d0f10c8731shiqian#define EXPECT_FALSE(condition) \
1861e44602ec83c65102035ce5304ae8de0cb16e9e56shiqian  GTEST_TEST_BOOLEAN_(!(condition), #condition, true, false, \
1862e44602ec83c65102035ce5304ae8de0cb16e9e56shiqian                      GTEST_NONFATAL_FAILURE_)
18634b6829f0d28990dd645e16386eb226d0f10c8731shiqian#define ASSERT_TRUE(condition) \
1864e44602ec83c65102035ce5304ae8de0cb16e9e56shiqian  GTEST_TEST_BOOLEAN_(condition, #condition, false, true, \
1865e44602ec83c65102035ce5304ae8de0cb16e9e56shiqian                      GTEST_FATAL_FAILURE_)
18664b6829f0d28990dd645e16386eb226d0f10c8731shiqian#define ASSERT_FALSE(condition) \
1867e44602ec83c65102035ce5304ae8de0cb16e9e56shiqian  GTEST_TEST_BOOLEAN_(!(condition), #condition, true, false, \
1868e44602ec83c65102035ce5304ae8de0cb16e9e56shiqian                      GTEST_FATAL_FAILURE_)
18694b6829f0d28990dd645e16386eb226d0f10c8731shiqian
18704b6829f0d28990dd645e16386eb226d0f10c8731shiqian// Includes the auto-generated header that implements a family of
18714b6829f0d28990dd645e16386eb226d0f10c8731shiqian// generic predicate assertion macros.
18722620c79810d4741922e9fa89050c0af564994f24zhanyong.wan#include "gtest/gtest_pred_impl.h"
18734b6829f0d28990dd645e16386eb226d0f10c8731shiqian
18744b6829f0d28990dd645e16386eb226d0f10c8731shiqian// Macros for testing equalities and inequalities.
18754b6829f0d28990dd645e16386eb226d0f10c8731shiqian//
18764b6829f0d28990dd645e16386eb226d0f10c8731shiqian//    * {ASSERT|EXPECT}_EQ(expected, actual): Tests that expected == actual
18774b6829f0d28990dd645e16386eb226d0f10c8731shiqian//    * {ASSERT|EXPECT}_NE(v1, v2):           Tests that v1 != v2
18784b6829f0d28990dd645e16386eb226d0f10c8731shiqian//    * {ASSERT|EXPECT}_LT(v1, v2):           Tests that v1 < v2
18794b6829f0d28990dd645e16386eb226d0f10c8731shiqian//    * {ASSERT|EXPECT}_LE(v1, v2):           Tests that v1 <= v2
18804b6829f0d28990dd645e16386eb226d0f10c8731shiqian//    * {ASSERT|EXPECT}_GT(v1, v2):           Tests that v1 > v2
18814b6829f0d28990dd645e16386eb226d0f10c8731shiqian//    * {ASSERT|EXPECT}_GE(v1, v2):           Tests that v1 >= v2
18824b6829f0d28990dd645e16386eb226d0f10c8731shiqian//
18834b6829f0d28990dd645e16386eb226d0f10c8731shiqian// When they are not, Google Test prints both the tested expressions and
18844b6829f0d28990dd645e16386eb226d0f10c8731shiqian// their actual values.  The values must be compatible built-in types,
18854b6829f0d28990dd645e16386eb226d0f10c8731shiqian// or you will get a compiler error.  By "compatible" we mean that the
18864b6829f0d28990dd645e16386eb226d0f10c8731shiqian// values can be compared by the respective operator.
18874b6829f0d28990dd645e16386eb226d0f10c8731shiqian//
18884b6829f0d28990dd645e16386eb226d0f10c8731shiqian// Note:
18894b6829f0d28990dd645e16386eb226d0f10c8731shiqian//
18904b6829f0d28990dd645e16386eb226d0f10c8731shiqian//   1. It is possible to make a user-defined type work with
18914b6829f0d28990dd645e16386eb226d0f10c8731shiqian//   {ASSERT|EXPECT}_??(), but that requires overloading the
18924b6829f0d28990dd645e16386eb226d0f10c8731shiqian//   comparison operators and is thus discouraged by the Google C++
18934b6829f0d28990dd645e16386eb226d0f10c8731shiqian//   Usage Guide.  Therefore, you are advised to use the
18944b6829f0d28990dd645e16386eb226d0f10c8731shiqian//   {ASSERT|EXPECT}_TRUE() macro to assert that two objects are
18954b6829f0d28990dd645e16386eb226d0f10c8731shiqian//   equal.
18964b6829f0d28990dd645e16386eb226d0f10c8731shiqian//
18974b6829f0d28990dd645e16386eb226d0f10c8731shiqian//   2. The {ASSERT|EXPECT}_??() macros do pointer comparisons on
18984b6829f0d28990dd645e16386eb226d0f10c8731shiqian//   pointers (in particular, C strings).  Therefore, if you use it
18994b6829f0d28990dd645e16386eb226d0f10c8731shiqian//   with two C strings, you are testing how their locations in memory
19004b6829f0d28990dd645e16386eb226d0f10c8731shiqian//   are related, not how their content is related.  To compare two C
19014b6829f0d28990dd645e16386eb226d0f10c8731shiqian//   strings by content, use {ASSERT|EXPECT}_STR*().
19024b6829f0d28990dd645e16386eb226d0f10c8731shiqian//
19034b6829f0d28990dd645e16386eb226d0f10c8731shiqian//   3. {ASSERT|EXPECT}_EQ(expected, actual) is preferred to
19044b6829f0d28990dd645e16386eb226d0f10c8731shiqian//   {ASSERT|EXPECT}_TRUE(expected == actual), as the former tells you
19054b6829f0d28990dd645e16386eb226d0f10c8731shiqian//   what the actual value is when it fails, and similarly for the
19064b6829f0d28990dd645e16386eb226d0f10c8731shiqian//   other comparisons.
19074b6829f0d28990dd645e16386eb226d0f10c8731shiqian//
19084b6829f0d28990dd645e16386eb226d0f10c8731shiqian//   4. Do not depend on the order in which {ASSERT|EXPECT}_??()
19094b6829f0d28990dd645e16386eb226d0f10c8731shiqian//   evaluate their arguments, which is undefined.
19104b6829f0d28990dd645e16386eb226d0f10c8731shiqian//
19114b6829f0d28990dd645e16386eb226d0f10c8731shiqian//   5. These macros evaluate their arguments exactly once.
19124b6829f0d28990dd645e16386eb226d0f10c8731shiqian//
19134b6829f0d28990dd645e16386eb226d0f10c8731shiqian// Examples:
19144b6829f0d28990dd645e16386eb226d0f10c8731shiqian//
19154b6829f0d28990dd645e16386eb226d0f10c8731shiqian//   EXPECT_NE(5, Foo());
19164b6829f0d28990dd645e16386eb226d0f10c8731shiqian//   EXPECT_EQ(NULL, a_pointer);
19174b6829f0d28990dd645e16386eb226d0f10c8731shiqian//   ASSERT_LT(i, array_size);
19184b6829f0d28990dd645e16386eb226d0f10c8731shiqian//   ASSERT_GT(records.size(), 0) << "There is no record left.";
19194b6829f0d28990dd645e16386eb226d0f10c8731shiqian
19204b6829f0d28990dd645e16386eb226d0f10c8731shiqian#define EXPECT_EQ(expected, actual) \
19214b6829f0d28990dd645e16386eb226d0f10c8731shiqian  EXPECT_PRED_FORMAT2(::testing::internal:: \
1922e44602ec83c65102035ce5304ae8de0cb16e9e56shiqian                      EqHelper<GTEST_IS_NULL_LITERAL_(expected)>::Compare, \
19234b6829f0d28990dd645e16386eb226d0f10c8731shiqian                      expected, actual)
19244b6829f0d28990dd645e16386eb226d0f10c8731shiqian#define EXPECT_NE(expected, actual) \
19254b6829f0d28990dd645e16386eb226d0f10c8731shiqian  EXPECT_PRED_FORMAT2(::testing::internal::CmpHelperNE, expected, actual)
19264b6829f0d28990dd645e16386eb226d0f10c8731shiqian#define EXPECT_LE(val1, val2) \
19274b6829f0d28990dd645e16386eb226d0f10c8731shiqian  EXPECT_PRED_FORMAT2(::testing::internal::CmpHelperLE, val1, val2)
19284b6829f0d28990dd645e16386eb226d0f10c8731shiqian#define EXPECT_LT(val1, val2) \
19294b6829f0d28990dd645e16386eb226d0f10c8731shiqian  EXPECT_PRED_FORMAT2(::testing::internal::CmpHelperLT, val1, val2)
19304b6829f0d28990dd645e16386eb226d0f10c8731shiqian#define EXPECT_GE(val1, val2) \
19314b6829f0d28990dd645e16386eb226d0f10c8731shiqian  EXPECT_PRED_FORMAT2(::testing::internal::CmpHelperGE, val1, val2)
19324b6829f0d28990dd645e16386eb226d0f10c8731shiqian#define EXPECT_GT(val1, val2) \
19334b6829f0d28990dd645e16386eb226d0f10c8731shiqian  EXPECT_PRED_FORMAT2(::testing::internal::CmpHelperGT, val1, val2)
19344b6829f0d28990dd645e16386eb226d0f10c8731shiqian
19350bfb56317785c7960bedee9fda0bb879347e2121zhanyong.wan#define GTEST_ASSERT_EQ(expected, actual) \
19364b6829f0d28990dd645e16386eb226d0f10c8731shiqian  ASSERT_PRED_FORMAT2(::testing::internal:: \
1937e44602ec83c65102035ce5304ae8de0cb16e9e56shiqian                      EqHelper<GTEST_IS_NULL_LITERAL_(expected)>::Compare, \
19384b6829f0d28990dd645e16386eb226d0f10c8731shiqian                      expected, actual)
19390bfb56317785c7960bedee9fda0bb879347e2121zhanyong.wan#define GTEST_ASSERT_NE(val1, val2) \
19404b6829f0d28990dd645e16386eb226d0f10c8731shiqian  ASSERT_PRED_FORMAT2(::testing::internal::CmpHelperNE, val1, val2)
19410bfb56317785c7960bedee9fda0bb879347e2121zhanyong.wan#define GTEST_ASSERT_LE(val1, val2) \
19424b6829f0d28990dd645e16386eb226d0f10c8731shiqian  ASSERT_PRED_FORMAT2(::testing::internal::CmpHelperLE, val1, val2)
19430bfb56317785c7960bedee9fda0bb879347e2121zhanyong.wan#define GTEST_ASSERT_LT(val1, val2) \
19444b6829f0d28990dd645e16386eb226d0f10c8731shiqian  ASSERT_PRED_FORMAT2(::testing::internal::CmpHelperLT, val1, val2)
19450bfb56317785c7960bedee9fda0bb879347e2121zhanyong.wan#define GTEST_ASSERT_GE(val1, val2) \
19464b6829f0d28990dd645e16386eb226d0f10c8731shiqian  ASSERT_PRED_FORMAT2(::testing::internal::CmpHelperGE, val1, val2)
19470bfb56317785c7960bedee9fda0bb879347e2121zhanyong.wan#define GTEST_ASSERT_GT(val1, val2) \
19484b6829f0d28990dd645e16386eb226d0f10c8731shiqian  ASSERT_PRED_FORMAT2(::testing::internal::CmpHelperGT, val1, val2)
19494b6829f0d28990dd645e16386eb226d0f10c8731shiqian
19500bfb56317785c7960bedee9fda0bb879347e2121zhanyong.wan// Define macro GTEST_DONT_DEFINE_ASSERT_XY to 1 to omit the definition of
19510bfb56317785c7960bedee9fda0bb879347e2121zhanyong.wan// ASSERT_XY(), which clashes with some users' own code.
19520bfb56317785c7960bedee9fda0bb879347e2121zhanyong.wan
19530bfb56317785c7960bedee9fda0bb879347e2121zhanyong.wan#if !GTEST_DONT_DEFINE_ASSERT_EQ
1954733a54a398766289b74cf3daebe083d7115cf388zhanyong.wan# define ASSERT_EQ(val1, val2) GTEST_ASSERT_EQ(val1, val2)
19550bfb56317785c7960bedee9fda0bb879347e2121zhanyong.wan#endif
19560bfb56317785c7960bedee9fda0bb879347e2121zhanyong.wan
19570bfb56317785c7960bedee9fda0bb879347e2121zhanyong.wan#if !GTEST_DONT_DEFINE_ASSERT_NE
1958733a54a398766289b74cf3daebe083d7115cf388zhanyong.wan# define ASSERT_NE(val1, val2) GTEST_ASSERT_NE(val1, val2)
19590bfb56317785c7960bedee9fda0bb879347e2121zhanyong.wan#endif
19600bfb56317785c7960bedee9fda0bb879347e2121zhanyong.wan
19610bfb56317785c7960bedee9fda0bb879347e2121zhanyong.wan#if !GTEST_DONT_DEFINE_ASSERT_LE
1962733a54a398766289b74cf3daebe083d7115cf388zhanyong.wan# define ASSERT_LE(val1, val2) GTEST_ASSERT_LE(val1, val2)
19630bfb56317785c7960bedee9fda0bb879347e2121zhanyong.wan#endif
19640bfb56317785c7960bedee9fda0bb879347e2121zhanyong.wan
19650bfb56317785c7960bedee9fda0bb879347e2121zhanyong.wan#if !GTEST_DONT_DEFINE_ASSERT_LT
1966733a54a398766289b74cf3daebe083d7115cf388zhanyong.wan# define ASSERT_LT(val1, val2) GTEST_ASSERT_LT(val1, val2)
19670bfb56317785c7960bedee9fda0bb879347e2121zhanyong.wan#endif
19680bfb56317785c7960bedee9fda0bb879347e2121zhanyong.wan
19690bfb56317785c7960bedee9fda0bb879347e2121zhanyong.wan#if !GTEST_DONT_DEFINE_ASSERT_GE
1970733a54a398766289b74cf3daebe083d7115cf388zhanyong.wan# define ASSERT_GE(val1, val2) GTEST_ASSERT_GE(val1, val2)
19710bfb56317785c7960bedee9fda0bb879347e2121zhanyong.wan#endif
19720bfb56317785c7960bedee9fda0bb879347e2121zhanyong.wan
19730bfb56317785c7960bedee9fda0bb879347e2121zhanyong.wan#if !GTEST_DONT_DEFINE_ASSERT_GT
1974733a54a398766289b74cf3daebe083d7115cf388zhanyong.wan# define ASSERT_GT(val1, val2) GTEST_ASSERT_GT(val1, val2)
19750bfb56317785c7960bedee9fda0bb879347e2121zhanyong.wan#endif
19760bfb56317785c7960bedee9fda0bb879347e2121zhanyong.wan
197703c314931649a999b0cf5deb0a434a1009157416jgm@google.com// C-string Comparisons.  All tests treat NULL and any non-NULL string
19784b6829f0d28990dd645e16386eb226d0f10c8731shiqian// as different.  Two NULLs are equal.
19794b6829f0d28990dd645e16386eb226d0f10c8731shiqian//
19804b6829f0d28990dd645e16386eb226d0f10c8731shiqian//    * {ASSERT|EXPECT}_STREQ(s1, s2):     Tests that s1 == s2
19814b6829f0d28990dd645e16386eb226d0f10c8731shiqian//    * {ASSERT|EXPECT}_STRNE(s1, s2):     Tests that s1 != s2
19824b6829f0d28990dd645e16386eb226d0f10c8731shiqian//    * {ASSERT|EXPECT}_STRCASEEQ(s1, s2): Tests that s1 == s2, ignoring case
19834b6829f0d28990dd645e16386eb226d0f10c8731shiqian//    * {ASSERT|EXPECT}_STRCASENE(s1, s2): Tests that s1 != s2, ignoring case
19844b6829f0d28990dd645e16386eb226d0f10c8731shiqian//
19854b6829f0d28990dd645e16386eb226d0f10c8731shiqian// For wide or narrow string objects, you can use the
19864b6829f0d28990dd645e16386eb226d0f10c8731shiqian// {ASSERT|EXPECT}_??() macros.
19874b6829f0d28990dd645e16386eb226d0f10c8731shiqian//
19884b6829f0d28990dd645e16386eb226d0f10c8731shiqian// Don't depend on the order in which the arguments are evaluated,
19894b6829f0d28990dd645e16386eb226d0f10c8731shiqian// which is undefined.
19904b6829f0d28990dd645e16386eb226d0f10c8731shiqian//
19914b6829f0d28990dd645e16386eb226d0f10c8731shiqian// These macros evaluate their arguments exactly once.
19924b6829f0d28990dd645e16386eb226d0f10c8731shiqian
19934b6829f0d28990dd645e16386eb226d0f10c8731shiqian#define EXPECT_STREQ(expected, actual) \
19944b6829f0d28990dd645e16386eb226d0f10c8731shiqian  EXPECT_PRED_FORMAT2(::testing::internal::CmpHelperSTREQ, expected, actual)
19954b6829f0d28990dd645e16386eb226d0f10c8731shiqian#define EXPECT_STRNE(s1, s2) \
19964b6829f0d28990dd645e16386eb226d0f10c8731shiqian  EXPECT_PRED_FORMAT2(::testing::internal::CmpHelperSTRNE, s1, s2)
19974b6829f0d28990dd645e16386eb226d0f10c8731shiqian#define EXPECT_STRCASEEQ(expected, actual) \
19984b6829f0d28990dd645e16386eb226d0f10c8731shiqian  EXPECT_PRED_FORMAT2(::testing::internal::CmpHelperSTRCASEEQ, expected, actual)
19994b6829f0d28990dd645e16386eb226d0f10c8731shiqian#define EXPECT_STRCASENE(s1, s2)\
20004b6829f0d28990dd645e16386eb226d0f10c8731shiqian  EXPECT_PRED_FORMAT2(::testing::internal::CmpHelperSTRCASENE, s1, s2)
20014b6829f0d28990dd645e16386eb226d0f10c8731shiqian
20024b6829f0d28990dd645e16386eb226d0f10c8731shiqian#define ASSERT_STREQ(expected, actual) \
20034b6829f0d28990dd645e16386eb226d0f10c8731shiqian  ASSERT_PRED_FORMAT2(::testing::internal::CmpHelperSTREQ, expected, actual)
20044b6829f0d28990dd645e16386eb226d0f10c8731shiqian#define ASSERT_STRNE(s1, s2) \
20054b6829f0d28990dd645e16386eb226d0f10c8731shiqian  ASSERT_PRED_FORMAT2(::testing::internal::CmpHelperSTRNE, s1, s2)
20064b6829f0d28990dd645e16386eb226d0f10c8731shiqian#define ASSERT_STRCASEEQ(expected, actual) \
20074b6829f0d28990dd645e16386eb226d0f10c8731shiqian  ASSERT_PRED_FORMAT2(::testing::internal::CmpHelperSTRCASEEQ, expected, actual)
20084b6829f0d28990dd645e16386eb226d0f10c8731shiqian#define ASSERT_STRCASENE(s1, s2)\
20094b6829f0d28990dd645e16386eb226d0f10c8731shiqian  ASSERT_PRED_FORMAT2(::testing::internal::CmpHelperSTRCASENE, s1, s2)
20104b6829f0d28990dd645e16386eb226d0f10c8731shiqian
20114b6829f0d28990dd645e16386eb226d0f10c8731shiqian// Macros for comparing floating-point numbers.
20124b6829f0d28990dd645e16386eb226d0f10c8731shiqian//
20134b6829f0d28990dd645e16386eb226d0f10c8731shiqian//    * {ASSERT|EXPECT}_FLOAT_EQ(expected, actual):
20144b6829f0d28990dd645e16386eb226d0f10c8731shiqian//         Tests that two float values are almost equal.
20154b6829f0d28990dd645e16386eb226d0f10c8731shiqian//    * {ASSERT|EXPECT}_DOUBLE_EQ(expected, actual):
20164b6829f0d28990dd645e16386eb226d0f10c8731shiqian//         Tests that two double values are almost equal.
20174b6829f0d28990dd645e16386eb226d0f10c8731shiqian//    * {ASSERT|EXPECT}_NEAR(v1, v2, abs_error):
20184b6829f0d28990dd645e16386eb226d0f10c8731shiqian//         Tests that v1 and v2 are within the given distance to each other.
20194b6829f0d28990dd645e16386eb226d0f10c8731shiqian//
20204b6829f0d28990dd645e16386eb226d0f10c8731shiqian// Google Test uses ULP-based comparison to automatically pick a default
20214b6829f0d28990dd645e16386eb226d0f10c8731shiqian// error bound that is appropriate for the operands.  See the
20224b6829f0d28990dd645e16386eb226d0f10c8731shiqian// FloatingPoint template class in gtest-internal.h if you are
20234b6829f0d28990dd645e16386eb226d0f10c8731shiqian// interested in the implementation details.
20244b6829f0d28990dd645e16386eb226d0f10c8731shiqian
20254b6829f0d28990dd645e16386eb226d0f10c8731shiqian#define EXPECT_FLOAT_EQ(expected, actual)\
20264b6829f0d28990dd645e16386eb226d0f10c8731shiqian  EXPECT_PRED_FORMAT2(::testing::internal::CmpHelperFloatingPointEQ<float>, \
20274b6829f0d28990dd645e16386eb226d0f10c8731shiqian                      expected, actual)
20284b6829f0d28990dd645e16386eb226d0f10c8731shiqian
20294b6829f0d28990dd645e16386eb226d0f10c8731shiqian#define EXPECT_DOUBLE_EQ(expected, actual)\
20304b6829f0d28990dd645e16386eb226d0f10c8731shiqian  EXPECT_PRED_FORMAT2(::testing::internal::CmpHelperFloatingPointEQ<double>, \
20314b6829f0d28990dd645e16386eb226d0f10c8731shiqian                      expected, actual)
20324b6829f0d28990dd645e16386eb226d0f10c8731shiqian
20334b6829f0d28990dd645e16386eb226d0f10c8731shiqian#define ASSERT_FLOAT_EQ(expected, actual)\
20344b6829f0d28990dd645e16386eb226d0f10c8731shiqian  ASSERT_PRED_FORMAT2(::testing::internal::CmpHelperFloatingPointEQ<float>, \
20354b6829f0d28990dd645e16386eb226d0f10c8731shiqian                      expected, actual)
20364b6829f0d28990dd645e16386eb226d0f10c8731shiqian
20374b6829f0d28990dd645e16386eb226d0f10c8731shiqian#define ASSERT_DOUBLE_EQ(expected, actual)\
20384b6829f0d28990dd645e16386eb226d0f10c8731shiqian  ASSERT_PRED_FORMAT2(::testing::internal::CmpHelperFloatingPointEQ<double>, \
20394b6829f0d28990dd645e16386eb226d0f10c8731shiqian                      expected, actual)
20404b6829f0d28990dd645e16386eb226d0f10c8731shiqian
20414b6829f0d28990dd645e16386eb226d0f10c8731shiqian#define EXPECT_NEAR(val1, val2, abs_error)\
20424b6829f0d28990dd645e16386eb226d0f10c8731shiqian  EXPECT_PRED_FORMAT3(::testing::internal::DoubleNearPredFormat, \
20434b6829f0d28990dd645e16386eb226d0f10c8731shiqian                      val1, val2, abs_error)
20444b6829f0d28990dd645e16386eb226d0f10c8731shiqian
20454b6829f0d28990dd645e16386eb226d0f10c8731shiqian#define ASSERT_NEAR(val1, val2, abs_error)\
20464b6829f0d28990dd645e16386eb226d0f10c8731shiqian  ASSERT_PRED_FORMAT3(::testing::internal::DoubleNearPredFormat, \
20474b6829f0d28990dd645e16386eb226d0f10c8731shiqian                      val1, val2, abs_error)
20484b6829f0d28990dd645e16386eb226d0f10c8731shiqian
20494b6829f0d28990dd645e16386eb226d0f10c8731shiqian// These predicate format functions work on floating-point values, and
20504b6829f0d28990dd645e16386eb226d0f10c8731shiqian// can be used in {ASSERT|EXPECT}_PRED_FORMAT2*(), e.g.
20514b6829f0d28990dd645e16386eb226d0f10c8731shiqian//
20524b6829f0d28990dd645e16386eb226d0f10c8731shiqian//   EXPECT_PRED_FORMAT2(testing::DoubleLE, Foo(), 5.0);
20534b6829f0d28990dd645e16386eb226d0f10c8731shiqian
20544b6829f0d28990dd645e16386eb226d0f10c8731shiqian// Asserts that val1 is less than, or almost equal to, val2.  Fails
20554b6829f0d28990dd645e16386eb226d0f10c8731shiqian// otherwise.  In particular, it fails if either val1 or val2 is NaN.
2056e4092294d7c1f38f303e8c7f67da31ba3e7e7d9azhanyong.wanGTEST_API_ AssertionResult FloatLE(const char* expr1, const char* expr2,
2057e4092294d7c1f38f303e8c7f67da31ba3e7e7d9azhanyong.wan                                   float val1, float val2);
2058e4092294d7c1f38f303e8c7f67da31ba3e7e7d9azhanyong.wanGTEST_API_ AssertionResult DoubleLE(const char* expr1, const char* expr2,
2059e4092294d7c1f38f303e8c7f67da31ba3e7e7d9azhanyong.wan                                    double val1, double val2);
20604b6829f0d28990dd645e16386eb226d0f10c8731shiqian
20614b6829f0d28990dd645e16386eb226d0f10c8731shiqian
20624cd62602913a032a7aec091d4c8055ff9af95e37zhanyong.wan#if GTEST_OS_WINDOWS
20634b6829f0d28990dd645e16386eb226d0f10c8731shiqian
20644b6829f0d28990dd645e16386eb226d0f10c8731shiqian// Macros that test for HRESULT failure and success, these are only useful
20654b6829f0d28990dd645e16386eb226d0f10c8731shiqian// on Windows, and rely on Windows SDK macros and APIs to compile.
20664b6829f0d28990dd645e16386eb226d0f10c8731shiqian//
20674b6829f0d28990dd645e16386eb226d0f10c8731shiqian//    * {ASSERT|EXPECT}_HRESULT_{SUCCEEDED|FAILED}(expr)
20684b6829f0d28990dd645e16386eb226d0f10c8731shiqian//
2069e8ff148b4309e115da1c55089dc3b9a241a928dcshiqian// When expr unexpectedly fails or succeeds, Google Test prints the
2070e8ff148b4309e115da1c55089dc3b9a241a928dcshiqian// expected result and the actual result with both a human-readable
2071e8ff148b4309e115da1c55089dc3b9a241a928dcshiqian// string representation of the error, if available, as well as the
2072e8ff148b4309e115da1c55089dc3b9a241a928dcshiqian// hex result code.
2073733a54a398766289b74cf3daebe083d7115cf388zhanyong.wan# define EXPECT_HRESULT_SUCCEEDED(expr) \
20744b6829f0d28990dd645e16386eb226d0f10c8731shiqian    EXPECT_PRED_FORMAT1(::testing::internal::IsHRESULTSuccess, (expr))
20754b6829f0d28990dd645e16386eb226d0f10c8731shiqian
2076733a54a398766289b74cf3daebe083d7115cf388zhanyong.wan# define ASSERT_HRESULT_SUCCEEDED(expr) \
20774b6829f0d28990dd645e16386eb226d0f10c8731shiqian    ASSERT_PRED_FORMAT1(::testing::internal::IsHRESULTSuccess, (expr))
20784b6829f0d28990dd645e16386eb226d0f10c8731shiqian
2079733a54a398766289b74cf3daebe083d7115cf388zhanyong.wan# define EXPECT_HRESULT_FAILED(expr) \
20804b6829f0d28990dd645e16386eb226d0f10c8731shiqian    EXPECT_PRED_FORMAT1(::testing::internal::IsHRESULTFailure, (expr))
20814b6829f0d28990dd645e16386eb226d0f10c8731shiqian
2082733a54a398766289b74cf3daebe083d7115cf388zhanyong.wan# define ASSERT_HRESULT_FAILED(expr) \
20834b6829f0d28990dd645e16386eb226d0f10c8731shiqian    ASSERT_PRED_FORMAT1(::testing::internal::IsHRESULTFailure, (expr))
20844b6829f0d28990dd645e16386eb226d0f10c8731shiqian
20854b6829f0d28990dd645e16386eb226d0f10c8731shiqian#endif  // GTEST_OS_WINDOWS
20864b6829f0d28990dd645e16386eb226d0f10c8731shiqian
2087e44602ec83c65102035ce5304ae8de0cb16e9e56shiqian// Macros that execute statement and check that it doesn't generate new fatal
2088e44602ec83c65102035ce5304ae8de0cb16e9e56shiqian// failures in the current thread.
2089e44602ec83c65102035ce5304ae8de0cb16e9e56shiqian//
2090e44602ec83c65102035ce5304ae8de0cb16e9e56shiqian//   * {ASSERT|EXPECT}_NO_FATAL_FAILURE(statement);
2091e44602ec83c65102035ce5304ae8de0cb16e9e56shiqian//
2092e44602ec83c65102035ce5304ae8de0cb16e9e56shiqian// Examples:
2093e44602ec83c65102035ce5304ae8de0cb16e9e56shiqian//
2094e44602ec83c65102035ce5304ae8de0cb16e9e56shiqian//   EXPECT_NO_FATAL_FAILURE(Process());
2095e44602ec83c65102035ce5304ae8de0cb16e9e56shiqian//   ASSERT_NO_FATAL_FAILURE(Process()) << "Process() failed";
2096e44602ec83c65102035ce5304ae8de0cb16e9e56shiqian//
2097e44602ec83c65102035ce5304ae8de0cb16e9e56shiqian#define ASSERT_NO_FATAL_FAILURE(statement) \
2098e44602ec83c65102035ce5304ae8de0cb16e9e56shiqian    GTEST_TEST_NO_FATAL_FAILURE_(statement, GTEST_FATAL_FAILURE_)
2099e44602ec83c65102035ce5304ae8de0cb16e9e56shiqian#define EXPECT_NO_FATAL_FAILURE(statement) \
2100e44602ec83c65102035ce5304ae8de0cb16e9e56shiqian    GTEST_TEST_NO_FATAL_FAILURE_(statement, GTEST_NONFATAL_FAILURE_)
21014b6829f0d28990dd645e16386eb226d0f10c8731shiqian
21024b6829f0d28990dd645e16386eb226d0f10c8731shiqian// Causes a trace (including the source file path, the current line
21034b6829f0d28990dd645e16386eb226d0f10c8731shiqian// number, and the given message) to be included in every test failure
21044b6829f0d28990dd645e16386eb226d0f10c8731shiqian// message generated by code in the current scope.  The effect is
21054b6829f0d28990dd645e16386eb226d0f10c8731shiqian// undone when the control leaves the current scope.
21064b6829f0d28990dd645e16386eb226d0f10c8731shiqian//
21074b6829f0d28990dd645e16386eb226d0f10c8731shiqian// The message argument can be anything streamable to std::ostream.
21084b6829f0d28990dd645e16386eb226d0f10c8731shiqian//
21094b6829f0d28990dd645e16386eb226d0f10c8731shiqian// In the implementation, we include the current line number as part
21104b6829f0d28990dd645e16386eb226d0f10c8731shiqian// of the dummy variable name, thus allowing multiple SCOPED_TRACE()s
21114b6829f0d28990dd645e16386eb226d0f10c8731shiqian// to appear in the same block - as long as they are on different
21124b6829f0d28990dd645e16386eb226d0f10c8731shiqian// lines.
21134b6829f0d28990dd645e16386eb226d0f10c8731shiqian#define SCOPED_TRACE(message) \
2114e44602ec83c65102035ce5304ae8de0cb16e9e56shiqian  ::testing::internal::ScopedTrace GTEST_CONCAT_TOKEN_(gtest_trace_, __LINE__)(\
21154b6829f0d28990dd645e16386eb226d0f10c8731shiqian    __FILE__, __LINE__, ::testing::Message() << (message))
21164b6829f0d28990dd645e16386eb226d0f10c8731shiqian
211721d43d1a7e09d960b790d992f1bb4e92c70391d4shiqian// Compile-time assertion for type equality.
211821d43d1a7e09d960b790d992f1bb4e92c70391d4shiqian// StaticAssertTypeEq<type1, type2>() compiles iff type1 and type2 are
211921d43d1a7e09d960b790d992f1bb4e92c70391d4shiqian// the same type.  The value it returns is not interesting.
212021d43d1a7e09d960b790d992f1bb4e92c70391d4shiqian//
212121d43d1a7e09d960b790d992f1bb4e92c70391d4shiqian// Instead of making StaticAssertTypeEq a class template, we make it a
212221d43d1a7e09d960b790d992f1bb4e92c70391d4shiqian// function template that invokes a helper class template.  This
212321d43d1a7e09d960b790d992f1bb4e92c70391d4shiqian// prevents a user from misusing StaticAssertTypeEq<T1, T2> by
212421d43d1a7e09d960b790d992f1bb4e92c70391d4shiqian// defining objects of that type.
212521d43d1a7e09d960b790d992f1bb4e92c70391d4shiqian//
212621d43d1a7e09d960b790d992f1bb4e92c70391d4shiqian// CAVEAT:
212721d43d1a7e09d960b790d992f1bb4e92c70391d4shiqian//
212821d43d1a7e09d960b790d992f1bb4e92c70391d4shiqian// When used inside a method of a class template,
212921d43d1a7e09d960b790d992f1bb4e92c70391d4shiqian// StaticAssertTypeEq<T1, T2>() is effective ONLY IF the method is
213021d43d1a7e09d960b790d992f1bb4e92c70391d4shiqian// instantiated.  For example, given:
213121d43d1a7e09d960b790d992f1bb4e92c70391d4shiqian//
213221d43d1a7e09d960b790d992f1bb4e92c70391d4shiqian//   template <typename T> class Foo {
213321d43d1a7e09d960b790d992f1bb4e92c70391d4shiqian//    public:
213421d43d1a7e09d960b790d992f1bb4e92c70391d4shiqian//     void Bar() { testing::StaticAssertTypeEq<int, T>(); }
213521d43d1a7e09d960b790d992f1bb4e92c70391d4shiqian//   };
213621d43d1a7e09d960b790d992f1bb4e92c70391d4shiqian//
213721d43d1a7e09d960b790d992f1bb4e92c70391d4shiqian// the code:
213821d43d1a7e09d960b790d992f1bb4e92c70391d4shiqian//
213921d43d1a7e09d960b790d992f1bb4e92c70391d4shiqian//   void Test1() { Foo<bool> foo; }
214021d43d1a7e09d960b790d992f1bb4e92c70391d4shiqian//
214121d43d1a7e09d960b790d992f1bb4e92c70391d4shiqian// will NOT generate a compiler error, as Foo<bool>::Bar() is never
214221d43d1a7e09d960b790d992f1bb4e92c70391d4shiqian// actually instantiated.  Instead, you need:
214321d43d1a7e09d960b790d992f1bb4e92c70391d4shiqian//
214421d43d1a7e09d960b790d992f1bb4e92c70391d4shiqian//   void Test2() { Foo<bool> foo; foo.Bar(); }
214521d43d1a7e09d960b790d992f1bb4e92c70391d4shiqian//
214621d43d1a7e09d960b790d992f1bb4e92c70391d4shiqian// to cause a compiler error.
214721d43d1a7e09d960b790d992f1bb4e92c70391d4shiqiantemplate <typename T1, typename T2>
214821d43d1a7e09d960b790d992f1bb4e92c70391d4shiqianbool StaticAssertTypeEq() {
214999f2fd8ac474e2a8cf922410af199fa7a0057f7bzhanyong.wan  (void)internal::StaticAssertTypeEqHelper<T1, T2>();
215021d43d1a7e09d960b790d992f1bb4e92c70391d4shiqian  return true;
215121d43d1a7e09d960b790d992f1bb4e92c70391d4shiqian}
21524b6829f0d28990dd645e16386eb226d0f10c8731shiqian
21534b6829f0d28990dd645e16386eb226d0f10c8731shiqian// Defines a test.
21544b6829f0d28990dd645e16386eb226d0f10c8731shiqian//
21554b6829f0d28990dd645e16386eb226d0f10c8731shiqian// The first parameter is the name of the test case, and the second
21564b6829f0d28990dd645e16386eb226d0f10c8731shiqian// parameter is the name of the test within the test case.
21574b6829f0d28990dd645e16386eb226d0f10c8731shiqian//
21584b6829f0d28990dd645e16386eb226d0f10c8731shiqian// The convention is to end the test case name with "Test".  For
21594b6829f0d28990dd645e16386eb226d0f10c8731shiqian// example, a test case for the Foo class can be named FooTest.
21604b6829f0d28990dd645e16386eb226d0f10c8731shiqian//
21614b6829f0d28990dd645e16386eb226d0f10c8731shiqian// The user should put his test code between braces after using this
21624b6829f0d28990dd645e16386eb226d0f10c8731shiqian// macro.  Example:
21634b6829f0d28990dd645e16386eb226d0f10c8731shiqian//
21644b6829f0d28990dd645e16386eb226d0f10c8731shiqian//   TEST(FooTest, InitializesCorrectly) {
21654b6829f0d28990dd645e16386eb226d0f10c8731shiqian//     Foo foo;
21664b6829f0d28990dd645e16386eb226d0f10c8731shiqian//     EXPECT_TRUE(foo.StatusIsOK());
21674b6829f0d28990dd645e16386eb226d0f10c8731shiqian//   }
21684b6829f0d28990dd645e16386eb226d0f10c8731shiqian
2169fe6a9a48c2a8280439e58c2e9020268a80df89b3shiqian// Note that we call GetTestTypeId() instead of GetTypeId<
2170fe6a9a48c2a8280439e58c2e9020268a80df89b3shiqian// ::testing::Test>() here to get the type ID of testing::Test.  This
2171fe6a9a48c2a8280439e58c2e9020268a80df89b3shiqian// is to work around a suspected linker bug when using Google Test as
2172fe6a9a48c2a8280439e58c2e9020268a80df89b3shiqian// a framework on Mac OS X.  The bug causes GetTypeId<
2173fe6a9a48c2a8280439e58c2e9020268a80df89b3shiqian// ::testing::Test>() to return different values depending on whether
2174fe6a9a48c2a8280439e58c2e9020268a80df89b3shiqian// the call is from the Google Test framework itself or from user test
2175fe6a9a48c2a8280439e58c2e9020268a80df89b3shiqian// code.  GetTestTypeId() is guaranteed to always return the same
2176fe6a9a48c2a8280439e58c2e9020268a80df89b3shiqian// value, as it always calls GetTypeId<>() from the Google Test
2177fe6a9a48c2a8280439e58c2e9020268a80df89b3shiqian// framework.
21781618bb491b881bc39faa3317ee8778847646226dzhanyong.wan#define GTEST_TEST(test_case_name, test_name)\
2179b0a12f719caf4e37894868334ce41b1864b0be53zhanyong.wan  GTEST_TEST_(test_case_name, test_name, \
2180fe6a9a48c2a8280439e58c2e9020268a80df89b3shiqian              ::testing::Test, ::testing::internal::GetTestTypeId())
21814b6829f0d28990dd645e16386eb226d0f10c8731shiqian
21821618bb491b881bc39faa3317ee8778847646226dzhanyong.wan// Define this macro to 1 to omit the definition of TEST(), which
21831618bb491b881bc39faa3317ee8778847646226dzhanyong.wan// is a generic name and clashes with some other libraries.
21841618bb491b881bc39faa3317ee8778847646226dzhanyong.wan#if !GTEST_DONT_DEFINE_TEST
2185733a54a398766289b74cf3daebe083d7115cf388zhanyong.wan# define TEST(test_case_name, test_name) GTEST_TEST(test_case_name, test_name)
21861618bb491b881bc39faa3317ee8778847646226dzhanyong.wan#endif
21874b6829f0d28990dd645e16386eb226d0f10c8731shiqian
21884b6829f0d28990dd645e16386eb226d0f10c8731shiqian// Defines a test that uses a test fixture.
21894b6829f0d28990dd645e16386eb226d0f10c8731shiqian//
21904b6829f0d28990dd645e16386eb226d0f10c8731shiqian// The first parameter is the name of the test fixture class, which
21914b6829f0d28990dd645e16386eb226d0f10c8731shiqian// also doubles as the test case name.  The second parameter is the
21924b6829f0d28990dd645e16386eb226d0f10c8731shiqian// name of the test within the test case.
21934b6829f0d28990dd645e16386eb226d0f10c8731shiqian//
21944b6829f0d28990dd645e16386eb226d0f10c8731shiqian// A test fixture class must be declared earlier.  The user should put
21954b6829f0d28990dd645e16386eb226d0f10c8731shiqian// his test code between braces after using this macro.  Example:
21964b6829f0d28990dd645e16386eb226d0f10c8731shiqian//
21974b6829f0d28990dd645e16386eb226d0f10c8731shiqian//   class FooTest : public testing::Test {
21984b6829f0d28990dd645e16386eb226d0f10c8731shiqian//    protected:
21994b6829f0d28990dd645e16386eb226d0f10c8731shiqian//     virtual void SetUp() { b_.AddElement(3); }
22004b6829f0d28990dd645e16386eb226d0f10c8731shiqian//
22014b6829f0d28990dd645e16386eb226d0f10c8731shiqian//     Foo a_;
22024b6829f0d28990dd645e16386eb226d0f10c8731shiqian//     Foo b_;
22034b6829f0d28990dd645e16386eb226d0f10c8731shiqian//   };
22044b6829f0d28990dd645e16386eb226d0f10c8731shiqian//
22054b6829f0d28990dd645e16386eb226d0f10c8731shiqian//   TEST_F(FooTest, InitializesCorrectly) {
22064b6829f0d28990dd645e16386eb226d0f10c8731shiqian//     EXPECT_TRUE(a_.StatusIsOK());
22074b6829f0d28990dd645e16386eb226d0f10c8731shiqian//   }
22084b6829f0d28990dd645e16386eb226d0f10c8731shiqian//
22094b6829f0d28990dd645e16386eb226d0f10c8731shiqian//   TEST_F(FooTest, ReturnsElementCountCorrectly) {
22104b6829f0d28990dd645e16386eb226d0f10c8731shiqian//     EXPECT_EQ(0, a_.size());
22114b6829f0d28990dd645e16386eb226d0f10c8731shiqian//     EXPECT_EQ(1, b_.size());
22124b6829f0d28990dd645e16386eb226d0f10c8731shiqian//   }
22134b6829f0d28990dd645e16386eb226d0f10c8731shiqian
22144b6829f0d28990dd645e16386eb226d0f10c8731shiqian#define TEST_F(test_fixture, test_name)\
2215b0a12f719caf4e37894868334ce41b1864b0be53zhanyong.wan  GTEST_TEST_(test_fixture, test_name, test_fixture, \
2216fe6a9a48c2a8280439e58c2e9020268a80df89b3shiqian              ::testing::internal::GetTypeId<test_fixture>())
22174b6829f0d28990dd645e16386eb226d0f10c8731shiqian
221899788d751da80001db223de2cd6178046ff5bad0zhanyong.wan}  // namespace testing
221999788d751da80001db223de2cd6178046ff5bad0zhanyong.wan
222099788d751da80001db223de2cd6178046ff5bad0zhanyong.wan// Use this function in main() to run all tests.  It returns 0 if all
22214b6829f0d28990dd645e16386eb226d0f10c8731shiqian// tests are successful, or 1 otherwise.
22224b6829f0d28990dd645e16386eb226d0f10c8731shiqian//
22234b6829f0d28990dd645e16386eb226d0f10c8731shiqian// RUN_ALL_TESTS() should be invoked after the command line has been
22244b6829f0d28990dd645e16386eb226d0f10c8731shiqian// parsed by InitGoogleTest().
222599788d751da80001db223de2cd6178046ff5bad0zhanyong.wan//
222699788d751da80001db223de2cd6178046ff5bad0zhanyong.wan// This function was formerly a macro; thus, it is in the global
222799788d751da80001db223de2cd6178046ff5bad0zhanyong.wan// namespace and has an all-caps name.
222899788d751da80001db223de2cd6178046ff5bad0zhanyong.wanint RUN_ALL_TESTS() GTEST_MUST_USE_RESULT_;
22294b6829f0d28990dd645e16386eb226d0f10c8731shiqian
223099788d751da80001db223de2cd6178046ff5bad0zhanyong.waninline int RUN_ALL_TESTS() {
223199788d751da80001db223de2cd6178046ff5bad0zhanyong.wan  return ::testing::UnitTest::GetInstance()->Run();
223299788d751da80001db223de2cd6178046ff5bad0zhanyong.wan}
22334b6829f0d28990dd645e16386eb226d0f10c8731shiqian
22344b6829f0d28990dd645e16386eb226d0f10c8731shiqian#endif  // GTEST_INCLUDE_GTEST_GTEST_H_
2235