11be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Copyright 2005, Google Inc. 21be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// All rights reserved. 31be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// 41be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Redistribution and use in source and binary forms, with or without 51be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// modification, are permitted provided that the following conditions are 61be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// met: 71be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// 81be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// * Redistributions of source code must retain the above copyright 91be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// notice, this list of conditions and the following disclaimer. 101be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// * Redistributions in binary form must reproduce the above 111be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// copyright notice, this list of conditions and the following disclaimer 121be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// in the documentation and/or other materials provided with the 131be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// distribution. 141be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// * Neither the name of Google Inc. nor the names of its 151be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// contributors may be used to endorse or promote products derived from 161be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// this software without specific prior written permission. 171be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// 181be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 191be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 201be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 211be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 221be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 231be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 241be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 251be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 261be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 271be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 281be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 291be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// 301be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Author: wan@google.com (Zhanyong Wan) 311be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// 321be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// The Google C++ Testing Framework (Google Test) 331be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// 341be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// This header file defines the public API for Google Test. It should be 351be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// included by any test program that uses Google Test. 361be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// 371be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// IMPORTANT NOTE: Due to limitation of the C++ language, we have to 381be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// leave some internal implementation details in this header file. 391be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// They are clearly marked by comments like this: 401be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// 411be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// // INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM. 421be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// 431be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Such code is NOT meant to be used by a user directly, and is subject 441be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// to CHANGE WITHOUT NOTICE. Therefore DO NOT DEPEND ON IT in a user 451be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// program! 461be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// 471be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Acknowledgment: Google Test borrowed the idea of automatic test 481be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// registration from Barthelemy Dagenais' (barthelemy@prologique.com) 491be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// easyUnit framework. 501be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 511be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#ifndef GTEST_INCLUDE_GTEST_GTEST_H_ 521be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#define GTEST_INCLUDE_GTEST_GTEST_H_ 531be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 541be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#include <limits> 5541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot#include <vector> 5641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 5741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot#include "gtest/internal/gtest-internal.h" 5841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot#include "gtest/internal/gtest-string.h" 5941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot#include "gtest/gtest-death-test.h" 6041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot#include "gtest/gtest-message.h" 6141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot#include "gtest/gtest-param-test.h" 6241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot#include "gtest/gtest-printers.h" 6341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot#include "gtest/gtest_prod.h" 6441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot#include "gtest/gtest-test-part.h" 6541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot#include "gtest/gtest-typed-test.h" 661be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 671be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Depending on the platform, different string classes are available. 6841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// On Linux, in addition to ::std::string, Google also makes use of 6941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// class ::string, which has the same interface as ::std::string, but 7041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// has a different implementation. 7141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// 7241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// The user can define GTEST_HAS_GLOBAL_STRING to 1 to indicate that 7341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// ::string is available AND is a distinct type to ::std::string, or 7441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// define it to 0 to indicate otherwise. 751be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// 761be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// If the user's ::std::string and ::string are the same class due to 7741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// aliasing, he should define GTEST_HAS_GLOBAL_STRING to 0. 781be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// 7941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// If the user doesn't define GTEST_HAS_GLOBAL_STRING, it is defined 8041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// heuristically. 811be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 821be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catanianamespace testing { 831be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 841be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Declares the flags. 851be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 861be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// This flag temporary enables the disabled tests. 871be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaGTEST_DECLARE_bool_(also_run_disabled_tests); 881be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 891be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// This flag brings the debugger on an assertion failure. 901be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaGTEST_DECLARE_bool_(break_on_failure); 911be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 921be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// This flag controls whether Google Test catches all test-thrown exceptions 931be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// and logs them as failures. 941be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaGTEST_DECLARE_bool_(catch_exceptions); 951be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 961be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// This flag enables using colors in terminal output. Available values are 971be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// "yes" to enable colors, "no" (disable colors), or "auto" (the default) 981be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// to let Google Test decide. 991be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaGTEST_DECLARE_string_(color); 1001be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 1011be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// This flag sets up the filter to select by name using a glob pattern 1021be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// the tests to run. If the filter is not given all tests are executed. 1031be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaGTEST_DECLARE_string_(filter); 1041be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 1051be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// This flag causes the Google Test to list tests. None of the tests listed 1061be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// are actually run if the flag is provided. 1071be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaGTEST_DECLARE_bool_(list_tests); 1081be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 1091be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// This flag controls whether Google Test emits a detailed XML report to a file 1101be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// in addition to its normal textual output. 1111be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaGTEST_DECLARE_string_(output); 1121be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 1131be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// This flags control whether Google Test prints the elapsed time for each 1141be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// test. 1151be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaGTEST_DECLARE_bool_(print_time); 1161be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 11741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// This flag specifies the random number seed. 11841d0579e8de9ef4ff178fc4991043c61a19943f7Brett ChabotGTEST_DECLARE_int32_(random_seed); 11941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 1201be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// This flag sets how many times the tests are repeated. The default value 1211be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// is 1. If the value is -1 the tests are repeating forever. 1221be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaGTEST_DECLARE_int32_(repeat); 1231be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 1241be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// This flag controls whether Google Test includes Google Test internal 1251be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// stack frames in failure stack traces. 1261be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaGTEST_DECLARE_bool_(show_internal_stack_frames); 1271be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 12841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// When this flag is specified, tests' order is randomized on every iteration. 12941d0579e8de9ef4ff178fc4991043c61a19943f7Brett ChabotGTEST_DECLARE_bool_(shuffle); 13041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 1311be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// This flag specifies the maximum number of stack frames to be 1321be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// printed in a failure message. 1331be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaGTEST_DECLARE_int32_(stack_trace_depth); 1341be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 1351be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// When this flag is specified, a failed assertion will throw an 1361be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// exception if exceptions are enabled, or exit the program with a 1371be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// non-zero code otherwise. 1381be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaGTEST_DECLARE_bool_(throw_on_failure); 1391be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 14041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// When this flag is set with a "host:port" string, on supported 14141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// platforms test results are streamed to the specified port on 14241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// the specified host machine. 14341d0579e8de9ef4ff178fc4991043c61a19943f7Brett ChabotGTEST_DECLARE_string_(stream_result_to); 14441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 1451be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// The upper limit for valid stack trace depths. 1461be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniaconst int kMaxStackTraceDepth = 100; 1471be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 1481be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catanianamespace internal { 1491be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 15041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotclass AssertHelper; 15141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotclass DefaultGlobalTestPartResultReporter; 15241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotclass ExecDeathTest; 15341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotclass NoExecDeathTest; 15441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotclass FinalSuccessChecker; 1551be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniaclass GTestFlagSaver; 15641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotclass TestResultAccessor; 15741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotclass TestEventListenersAccessor; 15841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotclass TestEventRepeater; 15941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotclass WindowsDeathTest; 16041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotclass UnitTestImpl* GetUnitTestImpl(); 16141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotvoid ReportFailureInUnknownLocation(TestPartResult::Type result_type, 16241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot const String& message); 1631be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 1641be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Converts a streamable value to a String. A NULL pointer is 1651be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// converted to "(null)". When the input value is a ::string, 1661be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// ::std::string, ::wstring, or ::std::wstring object, each NUL 1671be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// character in it is replaced with "\\0". 1681be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Declared in gtest-internal.h but defined here, so that it has access 1691be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// to the definition of the Message class, required by the ARM 1701be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// compiler. 1711be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniatemplate <typename T> 1721be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaString StreamableToString(const T& streamable) { 1731be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania return (Message() << streamable).GetString(); 1741be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 1751be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 1761be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} // namespace internal 1771be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 17841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// The friend relationship of some of these classes is cyclic. 17941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// If we don't forward declare them the compiler might confuse the classes 18041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// in friendship clauses with same named classes on the scope. 18141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotclass Test; 18241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotclass TestCase; 18341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotclass TestInfo; 18441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotclass UnitTest; 18541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 1861be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// A class for indicating whether an assertion was successful. When 1871be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// the assertion wasn't successful, the AssertionResult object 18841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// remembers a non-empty message that describes how it failed. 1891be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// 19041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// To create an instance of this class, use one of the factory functions 1911be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// (AssertionSuccess() and AssertionFailure()). 1921be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// 19341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// This class is useful for two purposes: 19441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// 1. Defining predicate functions to be used with Boolean test assertions 19541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// EXPECT_TRUE/EXPECT_FALSE and their ASSERT_ counterparts 19641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// 2. Defining predicate-format functions to be 19741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// used with predicate assertions (ASSERT_PRED_FORMAT*, etc). 19841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// 19941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// For example, if you define IsEven predicate: 20041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// 20141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// testing::AssertionResult IsEven(int n) { 20241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// if ((n % 2) == 0) 20341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// return testing::AssertionSuccess(); 20441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// else 20541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// return testing::AssertionFailure() << n << " is odd"; 20641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// } 20741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// 20841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// Then the failed expectation EXPECT_TRUE(IsEven(Fib(5))) 20941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// will print the message 21041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// 21141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// Value of: IsEven(Fib(5)) 21241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// Actual: false (5 is odd) 21341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// Expected: true 21441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// 21541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// instead of a more opaque 21641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// 21741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// Value of: IsEven(Fib(5)) 21841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// Actual: false 21941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// Expected: true 22041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// 22141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// in case IsEven is a simple Boolean predicate. 22241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// 22341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// If you expect your predicate to be reused and want to support informative 22441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// messages in EXPECT_FALSE and ASSERT_FALSE (negative assertions show up 22541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// about half as often as positive ones in our tests), supply messages for 22641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// both success and failure cases: 22741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// 22841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// testing::AssertionResult IsEven(int n) { 22941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// if ((n % 2) == 0) 23041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// return testing::AssertionSuccess() << n << " is even"; 23141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// else 23241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// return testing::AssertionFailure() << n << " is odd"; 23341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// } 23441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// 23541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// Then a statement EXPECT_FALSE(IsEven(Fib(6))) will print 23641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// 23741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// Value of: IsEven(Fib(6)) 23841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// Actual: true (8 is even) 23941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// Expected: false 24041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// 24141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// NB: Predicates that support negative Boolean assertions have reduced 24241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// performance in positive ones so be careful not to use them in tests 24341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// that have lots (tens of thousands) of positive Boolean assertions. 24441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// 24541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// To use this class with EXPECT_PRED_FORMAT assertions such as: 2461be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// 2471be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// // Verifies that Foo() returns an even number. 2481be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// EXPECT_PRED_FORMAT1(IsEven, Foo()); 2491be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// 25041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// you need to define: 2511be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// 2521be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// testing::AssertionResult IsEven(const char* expr, int n) { 25341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// if ((n % 2) == 0) 25441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// return testing::AssertionSuccess(); 25541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// else 25641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// return testing::AssertionFailure() 25741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// << "Expected: " << expr << " is even\n Actual: it's " << n; 2581be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// } 2591be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// 2601be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// If Foo() returns 5, you will see the following message: 2611be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// 2621be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Expected: Foo() is even 2631be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Actual: it's 5 26441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// 26541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotclass GTEST_API_ AssertionResult { 2661be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania public: 26741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // Copy constructor. 26841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // Used in EXPECT_TRUE/FALSE(assertion_result). 26941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot AssertionResult(const AssertionResult& other); 27041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // Used in the EXPECT_TRUE/FALSE(bool_expression). 27141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot explicit AssertionResult(bool success) : success_(success) {} 2721be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 2731be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // Returns true iff the assertion succeeded. 27441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot operator bool() const { return success_; } // NOLINT 2751be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 27641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // Returns the assertion's negation. Used with EXPECT/ASSERT_FALSE. 27741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot AssertionResult operator!() const; 27841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 27941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // Returns the text streamed into this AssertionResult. Test assertions 28041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // use it when they fail (i.e., the predicate's outcome doesn't match the 28141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // assertion's expectation). When nothing has been streamed into the 28241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // object, returns an empty string. 28341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot const char* message() const { 28441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot return message_.get() != NULL ? message_->c_str() : ""; 28541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot } 28641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // TODO(vladl@google.com): Remove this after making sure no clients use it. 28741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // Deprecated; please use message() instead. 28841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot const char* failure_message() const { return message(); } 28941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 29041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // Streams a custom failure message into this object. 29141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot template <typename T> AssertionResult& operator<<(const T& value) { 29241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot AppendMessage(Message() << value); 29341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot return *this; 29441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot } 29541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 29641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // Allows streaming basic output manipulators such as endl or flush into 29741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // this object. 29841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot AssertionResult& operator<<( 29941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot ::std::ostream& (*basic_manipulator)(::std::ostream& stream)) { 30041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot AppendMessage(Message() << basic_manipulator); 30141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot return *this; 30241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot } 3031be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 3041be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania private: 30541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // Appends the contents of message to message_. 30641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot void AppendMessage(const Message& a_message) { 30741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot if (message_.get() == NULL) 30841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot message_.reset(new ::std::string); 30941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot message_->append(a_message.GetString().c_str()); 31041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot } 3111be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 31241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // Stores result of the assertion predicate. 31341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot bool success_; 31441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // Stores the message describing the condition in case the expectation 31541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // construct is not satisfied with the predicate's outcome. 31641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // Referenced via a pointer to avoid taking too much stack frame space 31741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // with test assertions. 31841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot internal::scoped_ptr< ::std::string> message_; 3191be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 32041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot GTEST_DISALLOW_ASSIGN_(AssertionResult); 3211be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}; 3221be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 3231be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Makes a successful assertion result. 32441d0579e8de9ef4ff178fc4991043c61a19943f7Brett ChabotGTEST_API_ AssertionResult AssertionSuccess(); 32541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 32641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// Makes a failed assertion result. 32741d0579e8de9ef4ff178fc4991043c61a19943f7Brett ChabotGTEST_API_ AssertionResult AssertionFailure(); 3281be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 3291be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Makes a failed assertion result with the given failure message. 33041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// Deprecated; use AssertionFailure() << msg. 33141d0579e8de9ef4ff178fc4991043c61a19943f7Brett ChabotGTEST_API_ AssertionResult AssertionFailure(const Message& msg); 3321be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 3331be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// The abstract class that all tests inherit from. 3341be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// 3351be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// In Google Test, a unit test program contains one or many TestCases, and 3361be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// each TestCase contains one or many Tests. 3371be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// 3381be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// When you define a test using the TEST macro, you don't need to 3391be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// explicitly derive from Test - the TEST macro automatically does 3401be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// this for you. 3411be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// 3421be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// The only time you derive from Test is when defining a test fixture 3431be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// to be used a TEST_F. For example: 3441be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// 3451be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// class FooTest : public testing::Test { 3461be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// protected: 3471be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// virtual void SetUp() { ... } 3481be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// virtual void TearDown() { ... } 3491be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// ... 3501be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// }; 3511be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// 3521be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// TEST_F(FooTest, Bar) { ... } 3531be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// TEST_F(FooTest, Baz) { ... } 3541be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// 3551be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Test is not copyable. 35641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotclass GTEST_API_ Test { 3571be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania public: 35841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot friend class TestInfo; 3591be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 3601be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // Defines types for pointers to functions that set up and tear down 3611be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // a test case. 3621be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania typedef internal::SetUpTestCaseFunc SetUpTestCaseFunc; 3631be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania typedef internal::TearDownTestCaseFunc TearDownTestCaseFunc; 3641be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 3651be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // The d'tor is virtual as we intend to inherit from Test. 3661be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania virtual ~Test(); 3671be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 3681be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // Sets up the stuff shared by all tests in this test case. 3691be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // 3701be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // Google Test will call Foo::SetUpTestCase() before running the first 3711be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // test in test case Foo. Hence a sub-class can define its own 3721be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // SetUpTestCase() method to shadow the one defined in the super 3731be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // class. 3741be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania static void SetUpTestCase() {} 3751be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 3761be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // Tears down the stuff shared by all tests in this test case. 3771be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // 3781be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // Google Test will call Foo::TearDownTestCase() after running the last 3791be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // test in test case Foo. Hence a sub-class can define its own 3801be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // TearDownTestCase() method to shadow the one defined in the super 3811be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // class. 3821be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania static void TearDownTestCase() {} 3831be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 3841be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // Returns true iff the current test has a fatal failure. 3851be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania static bool HasFatalFailure(); 3861be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 38741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // Returns true iff the current test has a non-fatal failure. 38841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot static bool HasNonfatalFailure(); 38941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 39041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // Returns true iff the current test has a (either fatal or 39141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // non-fatal) failure. 39241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot static bool HasFailure() { return HasFatalFailure() || HasNonfatalFailure(); } 39341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 3941be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // Logs a property for the current test. Only the last value for a given 3951be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // key is remembered. 3961be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // These are public static so they can be called from utility functions 3971be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // that are not members of the test fixture. 3981be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // The arguments are const char* instead strings, as Google Test is used 3991be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // on platforms where string doesn't compile. 4001be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // 4011be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // Note that a driving consideration for these RecordProperty methods 4021be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // was to produce xml output suited to the Greenspan charting utility, 4031be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // which at present will only chart values that fit in a 32-bit int. It 4041be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // is the user's responsibility to restrict their values to 32-bit ints 4051be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // if they intend them to be used with Greenspan. 4061be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania static void RecordProperty(const char* key, const char* value); 4071be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania static void RecordProperty(const char* key, int value); 4081be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 4091be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania protected: 4101be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // Creates a Test object. 4111be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania Test(); 4121be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 4131be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // Sets up the test fixture. 4141be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania virtual void SetUp(); 4151be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 4161be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // Tears down the test fixture. 4171be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania virtual void TearDown(); 4181be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 4191be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania private: 4201be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // Returns true iff the current test has the same fixture class as 4211be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // the first test in the current test case. 4221be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania static bool HasSameFixtureClass(); 4231be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 4241be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // Runs the test after the test fixture has been set up. 4251be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // 4261be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // A sub-class must implement this to define the test logic. 4271be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // 4281be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // DO NOT OVERRIDE THIS FUNCTION DIRECTLY IN A USER PROGRAM. 4291be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // Instead, use the TEST or TEST_F macro. 4301be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania virtual void TestBody() = 0; 4311be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 4321be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // Sets up, executes, and tears down the test. 4331be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania void Run(); 4341be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 43541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // Deletes self. We deliberately pick an unusual name for this 43641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // internal method to avoid clashing with names used in user TESTs. 43741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot void DeleteSelf_() { delete this; } 43841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 4391be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // Uses a GTestFlagSaver to save and restore all Google Test flags. 4401be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania const internal::GTestFlagSaver* const gtest_flag_saver_; 4411be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 4421be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // Often a user mis-spells SetUp() as Setup() and spends a long time 4431be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // wondering why it is never called by Google Test. The declaration of 4441be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // the following method is solely for catching such an error at 4451be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // compile time: 4461be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // 4471be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // - The return type is deliberately chosen to be not void, so it 4481be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // will be a conflict if a user declares void Setup() in his test 4491be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // fixture. 4501be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // 4511be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // - This method is private, so it will be another compiler error 4521be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // if a user calls it from his test fixture. 4531be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // 4541be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // DO NOT OVERRIDE THIS FUNCTION. 4551be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // 4561be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // If you see an error about overriding the following function or 4571be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // about it being private, you have mis-spelled SetUp() as Setup(). 4581be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania struct Setup_should_be_spelled_SetUp {}; 4591be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania virtual Setup_should_be_spelled_SetUp* Setup() { return NULL; } 4601be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 4611be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // We disallow copying Tests. 4621be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania GTEST_DISALLOW_COPY_AND_ASSIGN_(Test); 4631be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}; 4641be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 46541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabottypedef internal::TimeInMillis TimeInMillis; 46641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 46741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// A copyable object representing a user specified test property which can be 46841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// output as a key/value string pair. 46941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// 47041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// Don't inherit from TestProperty as its destructor is not virtual. 47141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotclass TestProperty { 47241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot public: 47341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // C'tor. TestProperty does NOT have a default constructor. 47441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // Always use this constructor (with parameters) to create a 47541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // TestProperty object. 47641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot TestProperty(const char* a_key, const char* a_value) : 47741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot key_(a_key), value_(a_value) { 47841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot } 47941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 48041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // Gets the user supplied key. 48141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot const char* key() const { 48241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot return key_.c_str(); 48341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot } 48441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 48541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // Gets the user supplied value. 48641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot const char* value() const { 48741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot return value_.c_str(); 48841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot } 48941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 49041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // Sets a new value, overriding the one supplied in the constructor. 49141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot void SetValue(const char* new_value) { 49241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot value_ = new_value; 49341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot } 49441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 49541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot private: 49641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // The key supplied by the user. 49741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot internal::String key_; 49841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // The value supplied by the user. 49941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot internal::String value_; 50041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot}; 50141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 50241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// The result of a single Test. This includes a list of 50341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// TestPartResults, a list of TestProperties, a count of how many 50441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// death tests there are in the Test, and how much time it took to run 50541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// the Test. 50641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// 50741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// TestResult is not copyable. 50841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotclass GTEST_API_ TestResult { 50941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot public: 51041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // Creates an empty TestResult. 51141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot TestResult(); 51241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 51341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // D'tor. Do not inherit from TestResult. 51441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot ~TestResult(); 51541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 51641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // Gets the number of all test parts. This is the sum of the number 51741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // of successful test parts and the number of failed test parts. 51841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot int total_part_count() const; 51941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 52041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // Returns the number of the test properties. 52141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot int test_property_count() const; 52241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 52341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // Returns true iff the test passed (i.e. no test part failed). 52441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot bool Passed() const { return !Failed(); } 52541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 52641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // Returns true iff the test failed. 52741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot bool Failed() const; 52841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 52941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // Returns true iff the test fatally failed. 53041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot bool HasFatalFailure() const; 53141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 53241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // Returns true iff the test has a non-fatal failure. 53341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot bool HasNonfatalFailure() const; 53441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 53541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // Returns the elapsed time, in milliseconds. 53641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot TimeInMillis elapsed_time() const { return elapsed_time_; } 53741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 53841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // Returns the i-th test part result among all the results. i can range 53941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // from 0 to test_property_count() - 1. If i is not in that range, aborts 54041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // the program. 54141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot const TestPartResult& GetTestPartResult(int i) const; 54241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 54341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // Returns the i-th test property. i can range from 0 to 54441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // test_property_count() - 1. If i is not in that range, aborts the 54541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // program. 54641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot const TestProperty& GetTestProperty(int i) const; 54741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 54841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot private: 54941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot friend class TestInfo; 55041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot friend class UnitTest; 55141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot friend class internal::DefaultGlobalTestPartResultReporter; 55241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot friend class internal::ExecDeathTest; 55341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot friend class internal::TestResultAccessor; 55441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot friend class internal::UnitTestImpl; 55541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot friend class internal::WindowsDeathTest; 55641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 55741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // Gets the vector of TestPartResults. 55841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot const std::vector<TestPartResult>& test_part_results() const { 55941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot return test_part_results_; 56041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot } 56141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 56241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // Gets the vector of TestProperties. 56341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot const std::vector<TestProperty>& test_properties() const { 56441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot return test_properties_; 56541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot } 56641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 56741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // Sets the elapsed time. 56841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot void set_elapsed_time(TimeInMillis elapsed) { elapsed_time_ = elapsed; } 56941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 57041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // Adds a test property to the list. The property is validated and may add 57141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // a non-fatal failure if invalid (e.g., if it conflicts with reserved 57241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // key names). If a property is already recorded for the same key, the 57341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // value will be updated, rather than storing multiple values for the same 57441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // key. 57541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot void RecordProperty(const TestProperty& test_property); 57641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 57741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // Adds a failure if the key is a reserved attribute of Google Test 57841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // testcase tags. Returns true if the property is valid. 57941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // TODO(russr): Validate attribute names are legal and human readable. 58041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot static bool ValidateTestProperty(const TestProperty& test_property); 58141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 58241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // Adds a test part result to the list. 58341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot void AddTestPartResult(const TestPartResult& test_part_result); 58441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 58541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // Returns the death test count. 58641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot int death_test_count() const { return death_test_count_; } 58741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 58841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // Increments the death test count, returning the new count. 58941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot int increment_death_test_count() { return ++death_test_count_; } 59041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 59141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // Clears the test part results. 59241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot void ClearTestPartResults(); 59341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 59441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // Clears the object. 59541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot void Clear(); 59641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 59741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // Protects mutable state of the property vector and of owned 59841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // properties, whose values may be updated. 59941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot internal::Mutex test_properites_mutex_; 60041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 60141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // The vector of TestPartResults 60241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot std::vector<TestPartResult> test_part_results_; 60341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // The vector of TestProperties 60441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot std::vector<TestProperty> test_properties_; 60541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // Running count of death tests. 60641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot int death_test_count_; 60741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // The elapsed time, in milliseconds. 60841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot TimeInMillis elapsed_time_; 60941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 61041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // We disallow copying TestResult. 61141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot GTEST_DISALLOW_COPY_AND_ASSIGN_(TestResult); 61241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot}; // class TestResult 6131be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 6141be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// A TestInfo object stores the following information about a test: 6151be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// 6161be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Test case name 6171be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Test name 6181be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Whether the test should be run 6191be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// A function pointer that creates the test object when invoked 6201be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Test result 6211be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// 6221be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// The constructor of TestInfo registers itself with the UnitTest 6231be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// singleton such that the RUN_ALL_TESTS() macro knows which tests to 6241be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// run. 62541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotclass GTEST_API_ TestInfo { 6261be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania public: 6271be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // Destructs a TestInfo object. This function is not virtual, so 6281be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // don't inherit from TestInfo. 6291be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania ~TestInfo(); 6301be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 6311be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // Returns the test case name. 63241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot const char* test_case_name() const { return test_case_name_.c_str(); } 6331be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 6341be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // Returns the test name. 63541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot const char* name() const { return name_.c_str(); } 63641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 63741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // Returns the name of the parameter type, or NULL if this is not a typed 63841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // or a type-parameterized test. 63941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot const char* type_param() const { 64041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot if (type_param_.get() != NULL) 64141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot return type_param_->c_str(); 64241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot return NULL; 64341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot } 6441be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 64541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // Returns the text representation of the value parameter, or NULL if this 64641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // is not a value-parameterized test. 64741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot const char* value_param() const { 64841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot if (value_param_.get() != NULL) 64941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot return value_param_->c_str(); 65041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot return NULL; 65141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot } 6521be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 65341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // Returns true if this test should run, that is if the test is not disabled 65441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // (or it is disabled but the also_run_disabled_tests flag has been specified) 65541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // and its full name matches the user-specified filter. 6561be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // 6571be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // Google Test allows the user to filter the tests by their full names. 6581be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // The full name of a test Bar in test case Foo is defined as 6591be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // "Foo.Bar". Only the tests that match the filter will run. 6601be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // 6611be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // A filter is a colon-separated list of glob (not regex) patterns, 6621be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // optionally followed by a '-' and a colon-separated list of 6631be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // negative patterns (tests to exclude). A test is run if it 6641be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // matches one of the positive patterns and does not match any of 6651be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // the negative patterns. 6661be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // 6671be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // For example, *A*:Foo.* is a filter that matches any string that 6681be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // contains the character 'A' or starts with "Foo.". 66941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot bool should_run() const { return should_run_; } 6701be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 6711be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // Returns the result of the test. 67241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot const TestResult* result() const { return &result_; } 67341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 6741be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania private: 67541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 6761be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#if GTEST_HAS_DEATH_TEST 6771be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania friend class internal::DefaultDeathTestFactory; 6781be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#endif // GTEST_HAS_DEATH_TEST 6791be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania friend class Test; 6801be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania friend class TestCase; 68141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot friend class internal::UnitTestImpl; 6821be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania friend TestInfo* internal::MakeAndRegisterTestInfo( 6831be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania const char* test_case_name, const char* name, 68441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot const char* type_param, 68541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot const char* value_param, 6861be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania internal::TypeId fixture_class_id, 6871be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania Test::SetUpTestCaseFunc set_up_tc, 6881be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania Test::TearDownTestCaseFunc tear_down_tc, 6891be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania internal::TestFactoryBase* factory); 6901be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 6911be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // Constructs a TestInfo object. The newly constructed instance assumes 6921be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // ownership of the factory object. 6931be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania TestInfo(const char* test_case_name, const char* name, 69441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot const char* a_type_param, 69541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot const char* a_value_param, 6961be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania internal::TypeId fixture_class_id, 6971be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania internal::TestFactoryBase* factory); 6981be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 69941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // Increments the number of death tests encountered in this test so 70041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // far. 70141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot int increment_death_test_count() { 70241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot return result_.increment_death_test_count(); 70341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot } 70441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 70541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // Creates the test object, runs it, records its result, and then 70641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // deletes it. 70741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot void Run(); 70841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 70941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot static void ClearTestResult(TestInfo* test_info) { 71041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot test_info->result_.Clear(); 71141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot } 71241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 71341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // These fields are immutable properties of the test. 71441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot const std::string test_case_name_; // Test case name 71541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot const std::string name_; // Test name 71641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // Name of the parameter type, or NULL if this is not a typed or a 71741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // type-parameterized test. 71841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot const internal::scoped_ptr<const ::std::string> type_param_; 71941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // Text representation of the value parameter, or NULL if this is not a 72041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // value-parameterized test. 72141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot const internal::scoped_ptr<const ::std::string> value_param_; 72241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot const internal::TypeId fixture_class_id_; // ID of the test fixture class 72341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot bool should_run_; // True iff this test should run 72441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot bool is_disabled_; // True iff this test is disabled 72541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot bool matches_filter_; // True if this test matches the 72641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // user-specified filter. 72741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot internal::TestFactoryBase* const factory_; // The factory that creates 72841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // the test object 72941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 73041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // This field is mutable and needs to be reset before running the 73141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // test for the second time. 73241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot TestResult result_; 7331be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 7341be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania GTEST_DISALLOW_COPY_AND_ASSIGN_(TestInfo); 7351be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}; 7361be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 73741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// A test case, which consists of a vector of TestInfos. 73841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// 73941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// TestCase is not copyable. 74041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotclass GTEST_API_ TestCase { 74141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot public: 74241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // Creates a TestCase with the given name. 74341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // 74441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // TestCase does NOT have a default constructor. Always use this 74541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // constructor to create a TestCase object. 74641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // 74741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // Arguments: 74841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // 74941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // name: name of the test case 75041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // a_type_param: the name of the test's type parameter, or NULL if 75141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // this is not a type-parameterized test. 75241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // set_up_tc: pointer to the function that sets up the test case 75341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // tear_down_tc: pointer to the function that tears down the test case 75441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot TestCase(const char* name, const char* a_type_param, 75541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot Test::SetUpTestCaseFunc set_up_tc, 75641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot Test::TearDownTestCaseFunc tear_down_tc); 75741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 75841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // Destructor of TestCase. 75941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot virtual ~TestCase(); 76041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 76141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // Gets the name of the TestCase. 76241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot const char* name() const { return name_.c_str(); } 76341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 76441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // Returns the name of the parameter type, or NULL if this is not a 76541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // type-parameterized test case. 76641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot const char* type_param() const { 76741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot if (type_param_.get() != NULL) 76841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot return type_param_->c_str(); 76941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot return NULL; 77041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot } 77141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 77241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // Returns true if any test in this test case should run. 77341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot bool should_run() const { return should_run_; } 77441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 77541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // Gets the number of successful tests in this test case. 77641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot int successful_test_count() const; 77741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 77841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // Gets the number of failed tests in this test case. 77941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot int failed_test_count() const; 78041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 78141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // Gets the number of disabled tests in this test case. 78241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot int disabled_test_count() const; 78341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 78441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // Get the number of tests in this test case that should run. 78541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot int test_to_run_count() const; 78641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 78741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // Gets the number of all tests in this test case. 78841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot int total_test_count() const; 78941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 79041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // Returns true iff the test case passed. 79141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot bool Passed() const { return !Failed(); } 79241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 79341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // Returns true iff the test case failed. 79441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot bool Failed() const { return failed_test_count() > 0; } 79541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 79641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // Returns the elapsed time, in milliseconds. 79741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot TimeInMillis elapsed_time() const { return elapsed_time_; } 79841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 79941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // Returns the i-th test among all the tests. i can range from 0 to 80041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // total_test_count() - 1. If i is not in that range, returns NULL. 80141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot const TestInfo* GetTestInfo(int i) const; 80241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 80341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot private: 80441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot friend class Test; 80541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot friend class internal::UnitTestImpl; 80641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 80741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // Gets the (mutable) vector of TestInfos in this TestCase. 80841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot std::vector<TestInfo*>& test_info_list() { return test_info_list_; } 80941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 81041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // Gets the (immutable) vector of TestInfos in this TestCase. 81141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot const std::vector<TestInfo*>& test_info_list() const { 81241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot return test_info_list_; 81341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot } 81441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 81541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // Returns the i-th test among all the tests. i can range from 0 to 81641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // total_test_count() - 1. If i is not in that range, returns NULL. 81741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot TestInfo* GetMutableTestInfo(int i); 81841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 81941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // Sets the should_run member. 82041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot void set_should_run(bool should) { should_run_ = should; } 82141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 82241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // Adds a TestInfo to this test case. Will delete the TestInfo upon 82341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // destruction of the TestCase object. 82441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot void AddTestInfo(TestInfo * test_info); 82541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 82641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // Clears the results of all tests in this test case. 82741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot void ClearResult(); 82841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 82941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // Clears the results of all tests in the given test case. 83041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot static void ClearTestCaseResult(TestCase* test_case) { 83141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot test_case->ClearResult(); 83241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot } 83341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 83441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // Runs every test in this TestCase. 83541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot void Run(); 83641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 83741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // Runs SetUpTestCase() for this TestCase. This wrapper is needed 83841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // for catching exceptions thrown from SetUpTestCase(). 83941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot void RunSetUpTestCase() { (*set_up_tc_)(); } 84041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 84141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // Runs TearDownTestCase() for this TestCase. This wrapper is 84241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // needed for catching exceptions thrown from TearDownTestCase(). 84341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot void RunTearDownTestCase() { (*tear_down_tc_)(); } 84441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 84541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // Returns true iff test passed. 84641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot static bool TestPassed(const TestInfo* test_info) { 84741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot return test_info->should_run() && test_info->result()->Passed(); 84841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot } 84941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 85041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // Returns true iff test failed. 85141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot static bool TestFailed(const TestInfo* test_info) { 85241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot return test_info->should_run() && test_info->result()->Failed(); 85341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot } 85441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 85541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // Returns true iff test is disabled. 85641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot static bool TestDisabled(const TestInfo* test_info) { 85741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot return test_info->is_disabled_; 85841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot } 85941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 86041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // Returns true if the given test should run. 86141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot static bool ShouldRunTest(const TestInfo* test_info) { 86241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot return test_info->should_run(); 86341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot } 86441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 86541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // Shuffles the tests in this test case. 86641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot void ShuffleTests(internal::Random* random); 86741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 86841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // Restores the test order to before the first shuffle. 86941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot void UnshuffleTests(); 87041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 87141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // Name of the test case. 87241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot internal::String name_; 87341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // Name of the parameter type, or NULL if this is not a typed or a 87441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // type-parameterized test. 87541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot const internal::scoped_ptr<const ::std::string> type_param_; 87641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // The vector of TestInfos in their original order. It owns the 87741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // elements in the vector. 87841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot std::vector<TestInfo*> test_info_list_; 87941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // Provides a level of indirection for the test list to allow easy 88041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // shuffling and restoring the test order. The i-th element in this 88141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // vector is the index of the i-th test in the shuffled test list. 88241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot std::vector<int> test_indices_; 88341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // Pointer to the function that sets up the test case. 88441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot Test::SetUpTestCaseFunc set_up_tc_; 88541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // Pointer to the function that tears down the test case. 88641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot Test::TearDownTestCaseFunc tear_down_tc_; 88741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // True iff any test in this test case should run. 88841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot bool should_run_; 88941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // Elapsed time, in milliseconds. 89041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot TimeInMillis elapsed_time_; 89141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 89241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // We disallow copying TestCases. 89341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot GTEST_DISALLOW_COPY_AND_ASSIGN_(TestCase); 89441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot}; 89541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 8961be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// An Environment object is capable of setting up and tearing down an 8971be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// environment. The user should subclass this to define his own 8981be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// environment(s). 8991be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// 9001be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// An Environment object does the set-up and tear-down in virtual 9011be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// methods SetUp() and TearDown() instead of the constructor and the 9021be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// destructor, as: 9031be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// 9041be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// 1. You cannot safely throw from a destructor. This is a problem 9051be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// as in some cases Google Test is used where exceptions are enabled, and 9061be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// we may want to implement ASSERT_* using exceptions where they are 9071be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// available. 9081be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// 2. You cannot use ASSERT_* directly in a constructor or 9091be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// destructor. 9101be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniaclass Environment { 9111be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania public: 9121be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // The d'tor is virtual as we need to subclass Environment. 9131be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania virtual ~Environment() {} 9141be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 9151be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // Override this to define how to set up the environment. 9161be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania virtual void SetUp() {} 9171be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 9181be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // Override this to define how to tear down the environment. 9191be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania virtual void TearDown() {} 9201be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania private: 9211be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // If you see an error about overriding the following function or 9221be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // about it being private, you have mis-spelled SetUp() as Setup(). 9231be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania struct Setup_should_be_spelled_SetUp {}; 9241be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania virtual Setup_should_be_spelled_SetUp* Setup() { return NULL; } 9251be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}; 9261be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 92741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// The interface for tracing execution of tests. The methods are organized in 92841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// the order the corresponding events are fired. 92941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotclass TestEventListener { 93041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot public: 93141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot virtual ~TestEventListener() {} 93241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 93341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // Fired before any test activity starts. 93441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot virtual void OnTestProgramStart(const UnitTest& unit_test) = 0; 93541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 93641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // Fired before each iteration of tests starts. There may be more than 93741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // one iteration if GTEST_FLAG(repeat) is set. iteration is the iteration 93841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // index, starting from 0. 93941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot virtual void OnTestIterationStart(const UnitTest& unit_test, 94041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot int iteration) = 0; 94141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 94241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // Fired before environment set-up for each iteration of tests starts. 94341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot virtual void OnEnvironmentsSetUpStart(const UnitTest& unit_test) = 0; 94441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 94541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // Fired after environment set-up for each iteration of tests ends. 94641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot virtual void OnEnvironmentsSetUpEnd(const UnitTest& unit_test) = 0; 94741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 94841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // Fired before the test case starts. 94941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot virtual void OnTestCaseStart(const TestCase& test_case) = 0; 95041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 95141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // Fired before the test starts. 95241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot virtual void OnTestStart(const TestInfo& test_info) = 0; 95341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 95441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // Fired after a failed assertion or a SUCCEED() invocation. 95541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot virtual void OnTestPartResult(const TestPartResult& test_part_result) = 0; 95641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 95741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // Fired after the test ends. 95841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot virtual void OnTestEnd(const TestInfo& test_info) = 0; 95941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 96041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // Fired after the test case ends. 96141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot virtual void OnTestCaseEnd(const TestCase& test_case) = 0; 96241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 96341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // Fired before environment tear-down for each iteration of tests starts. 96441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot virtual void OnEnvironmentsTearDownStart(const UnitTest& unit_test) = 0; 96541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 96641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // Fired after environment tear-down for each iteration of tests ends. 96741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot virtual void OnEnvironmentsTearDownEnd(const UnitTest& unit_test) = 0; 96841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 96941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // Fired after each iteration of tests finishes. 97041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot virtual void OnTestIterationEnd(const UnitTest& unit_test, 97141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot int iteration) = 0; 97241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 97341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // Fired after all test activities have ended. 97441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot virtual void OnTestProgramEnd(const UnitTest& unit_test) = 0; 97541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot}; 97641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 97741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// The convenience class for users who need to override just one or two 97841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// methods and are not concerned that a possible change to a signature of 97941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// the methods they override will not be caught during the build. For 98041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// comments about each method please see the definition of TestEventListener 98141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// above. 98241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotclass EmptyTestEventListener : public TestEventListener { 98341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot public: 98441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot virtual void OnTestProgramStart(const UnitTest& /*unit_test*/) {} 98541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot virtual void OnTestIterationStart(const UnitTest& /*unit_test*/, 98641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot int /*iteration*/) {} 98741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot virtual void OnEnvironmentsSetUpStart(const UnitTest& /*unit_test*/) {} 98841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot virtual void OnEnvironmentsSetUpEnd(const UnitTest& /*unit_test*/) {} 98941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot virtual void OnTestCaseStart(const TestCase& /*test_case*/) {} 99041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot virtual void OnTestStart(const TestInfo& /*test_info*/) {} 99141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot virtual void OnTestPartResult(const TestPartResult& /*test_part_result*/) {} 99241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot virtual void OnTestEnd(const TestInfo& /*test_info*/) {} 99341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot virtual void OnTestCaseEnd(const TestCase& /*test_case*/) {} 99441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot virtual void OnEnvironmentsTearDownStart(const UnitTest& /*unit_test*/) {} 99541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot virtual void OnEnvironmentsTearDownEnd(const UnitTest& /*unit_test*/) {} 99641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot virtual void OnTestIterationEnd(const UnitTest& /*unit_test*/, 99741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot int /*iteration*/) {} 99841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot virtual void OnTestProgramEnd(const UnitTest& /*unit_test*/) {} 99941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot}; 100041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 100141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// TestEventListeners lets users add listeners to track events in Google Test. 100241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotclass GTEST_API_ TestEventListeners { 100341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot public: 100441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot TestEventListeners(); 100541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot ~TestEventListeners(); 100641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 100741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // Appends an event listener to the end of the list. Google Test assumes 100841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // the ownership of the listener (i.e. it will delete the listener when 100941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // the test program finishes). 101041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot void Append(TestEventListener* listener); 101141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 101241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // Removes the given event listener from the list and returns it. It then 101341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // becomes the caller's responsibility to delete the listener. Returns 101441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // NULL if the listener is not found in the list. 101541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot TestEventListener* Release(TestEventListener* listener); 101641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 101741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // Returns the standard listener responsible for the default console 101841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // output. Can be removed from the listeners list to shut down default 101941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // console output. Note that removing this object from the listener list 102041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // with Release transfers its ownership to the caller and makes this 102141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // function return NULL the next time. 102241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot TestEventListener* default_result_printer() const { 102341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot return default_result_printer_; 102441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot } 102541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 102641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // Returns the standard listener responsible for the default XML output 102741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // controlled by the --gtest_output=xml flag. Can be removed from the 102841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // listeners list by users who want to shut down the default XML output 102941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // controlled by this flag and substitute it with custom one. Note that 103041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // removing this object from the listener list with Release transfers its 103141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // ownership to the caller and makes this function return NULL the next 103241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // time. 103341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot TestEventListener* default_xml_generator() const { 103441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot return default_xml_generator_; 103541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot } 103641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 103741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot private: 103841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot friend class TestCase; 103941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot friend class TestInfo; 104041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot friend class internal::DefaultGlobalTestPartResultReporter; 104141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot friend class internal::NoExecDeathTest; 104241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot friend class internal::TestEventListenersAccessor; 104341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot friend class internal::UnitTestImpl; 104441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 104541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // Returns repeater that broadcasts the TestEventListener events to all 104641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // subscribers. 104741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot TestEventListener* repeater(); 104841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 104941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // Sets the default_result_printer attribute to the provided listener. 105041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // The listener is also added to the listener list and previous 105141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // default_result_printer is removed from it and deleted. The listener can 105241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // also be NULL in which case it will not be added to the list. Does 105341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // nothing if the previous and the current listener objects are the same. 105441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot void SetDefaultResultPrinter(TestEventListener* listener); 105541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 105641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // Sets the default_xml_generator attribute to the provided listener. The 105741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // listener is also added to the listener list and previous 105841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // default_xml_generator is removed from it and deleted. The listener can 105941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // also be NULL in which case it will not be added to the list. Does 106041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // nothing if the previous and the current listener objects are the same. 106141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot void SetDefaultXmlGenerator(TestEventListener* listener); 106241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 106341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // Controls whether events will be forwarded by the repeater to the 106441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // listeners in the list. 106541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot bool EventForwardingEnabled() const; 106641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot void SuppressEventForwarding(); 106741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 106841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // The actual list of listeners. 106941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot internal::TestEventRepeater* repeater_; 107041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // Listener responsible for the standard result output. 107141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot TestEventListener* default_result_printer_; 107241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // Listener responsible for the creation of the XML output file. 107341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot TestEventListener* default_xml_generator_; 107441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 107541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // We disallow copying TestEventListeners. 107641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot GTEST_DISALLOW_COPY_AND_ASSIGN_(TestEventListeners); 107741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot}; 107841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 107941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// A UnitTest consists of a vector of TestCases. 10801be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// 10811be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// This is a singleton class. The only instance of UnitTest is 10821be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// created when UnitTest::GetInstance() is first called. This 10831be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// instance is never deleted. 10841be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// 10851be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// UnitTest is not copyable. 10861be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// 10871be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// This class is thread-safe as long as the methods are called 10881be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// according to their specification. 108941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotclass GTEST_API_ UnitTest { 10901be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania public: 10911be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // Gets the singleton UnitTest object. The first time this method 10921be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // is called, a UnitTest object is constructed and returned. 10931be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // Consecutive calls will return the same object. 10941be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania static UnitTest* GetInstance(); 10951be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 10961be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // Runs all tests in this UnitTest object and prints the result. 10971be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // Returns 0 if successful, or 1 otherwise. 10981be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // 10991be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // This method can only be called from the main thread. 11001be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // 11011be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM. 11021be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania int Run() GTEST_MUST_USE_RESULT_; 11031be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 11041be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // Returns the working directory when the first TEST() or TEST_F() 11051be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // was executed. The UnitTest object owns the string. 11061be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania const char* original_working_dir() const; 11071be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 11081be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // Returns the TestCase object for the test that's currently running, 11091be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // or NULL if no test is running. 11101be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania const TestCase* current_test_case() const; 11111be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 11121be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // Returns the TestInfo object for the test that's currently running, 11131be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // or NULL if no test is running. 11141be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania const TestInfo* current_test_info() const; 11151be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 111641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // Returns the random seed used at the start of the current test run. 111741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot int random_seed() const; 111841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 11191be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#if GTEST_HAS_PARAM_TEST 11201be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // Returns the ParameterizedTestCaseRegistry object used to keep track of 11211be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // value-parameterized tests and instantiate and register them. 112241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // 112341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM. 11241be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania internal::ParameterizedTestCaseRegistry& parameterized_test_registry(); 11251be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#endif // GTEST_HAS_PARAM_TEST 11261be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 112741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // Gets the number of successful test cases. 112841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot int successful_test_case_count() const; 112941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 113041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // Gets the number of failed test cases. 113141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot int failed_test_case_count() const; 113241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 113341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // Gets the number of all test cases. 113441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot int total_test_case_count() const; 113541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 113641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // Gets the number of all test cases that contain at least one test 113741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // that should run. 113841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot int test_case_to_run_count() const; 113941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 114041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // Gets the number of successful tests. 114141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot int successful_test_count() const; 114241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 114341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // Gets the number of failed tests. 114441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot int failed_test_count() const; 114541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 114641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // Gets the number of disabled tests. 114741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot int disabled_test_count() const; 114841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 114941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // Gets the number of all tests. 115041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot int total_test_count() const; 115141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 115241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // Gets the number of tests that should run. 115341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot int test_to_run_count() const; 115441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 115541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // Gets the elapsed time, in milliseconds. 115641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot TimeInMillis elapsed_time() const; 115741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 115841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // Returns true iff the unit test passed (i.e. all test cases passed). 115941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot bool Passed() const; 116041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 116141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // Returns true iff the unit test failed (i.e. some test case failed 116241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // or something outside of all tests failed). 116341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot bool Failed() const; 116441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 116541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // Gets the i-th test case among all the test cases. i can range from 0 to 116641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // total_test_case_count() - 1. If i is not in that range, returns NULL. 116741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot const TestCase* GetTestCase(int i) const; 116841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 116941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // Returns the list of event listeners that can be used to track events 117041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // inside Google Test. 117141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot TestEventListeners& listeners(); 117241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 117341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot private: 117441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // Registers and returns a global test environment. When a test 117541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // program is run, all global test environments will be set-up in 117641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // the order they were registered. After all tests in the program 117741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // have finished, all global test environments will be torn-down in 117841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // the *reverse* order they were registered. 117941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // 118041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // The UnitTest object takes ownership of the given environment. 118141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // 118241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // This method can only be called from the main thread. 118341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot Environment* AddEnvironment(Environment* env); 118441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 118541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // Adds a TestPartResult to the current TestResult object. All 118641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // Google Test assertion macros (e.g. ASSERT_TRUE, EXPECT_EQ, etc) 118741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // eventually call this to report their results. The user code 118841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // should use the assertion macros instead of calling this directly. 118941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot void AddTestPartResult(TestPartResult::Type result_type, 119041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot const char* file_name, 119141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot int line_number, 119241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot const internal::String& message, 119341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot const internal::String& os_stack_trace); 119441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 119541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // Adds a TestProperty to the current TestResult object. If the result already 119641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // contains a property with the same key, the value will be updated. 119741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot void RecordPropertyForCurrentTest(const char* key, const char* value); 119841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 119941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // Gets the i-th test case among all the test cases. i can range from 0 to 120041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // total_test_case_count() - 1. If i is not in that range, returns NULL. 120141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot TestCase* GetMutableTestCase(int i); 120241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 12031be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // Accessors for the implementation object. 12041be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania internal::UnitTestImpl* impl() { return impl_; } 12051be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania const internal::UnitTestImpl* impl() const { return impl_; } 120641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 120741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // These classes and funcions are friends as they need to access private 120841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // members of UnitTest. 120941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot friend class Test; 121041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot friend class internal::AssertHelper; 12111be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania friend class internal::ScopedTrace; 121241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot friend Environment* AddGlobalTestEnvironment(Environment* env); 121341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot friend internal::UnitTestImpl* internal::GetUnitTestImpl(); 121441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot friend void internal::ReportFailureInUnknownLocation( 121541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot TestPartResult::Type result_type, 121641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot const internal::String& message); 12171be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 12181be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // Creates an empty UnitTest. 12191be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania UnitTest(); 12201be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 12211be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // D'tor 12221be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania virtual ~UnitTest(); 12231be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 12241be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // Pushes a trace defined by SCOPED_TRACE() on to the per-thread 12251be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // Google Test trace stack. 12261be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania void PushGTestTrace(const internal::TraceInfo& trace); 12271be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 12281be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // Pops a trace from the per-thread Google Test trace stack. 12291be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania void PopGTestTrace(); 12301be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 12311be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // Protects mutable state in *impl_. This is mutable as some const 12321be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // methods need to lock it too. 12331be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania mutable internal::Mutex mutex_; 12341be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 12351be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // Opaque implementation object. This field is never changed once 12361be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // the object is constructed. We don't mark it as const here, as 12371be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // doing so will cause a warning in the constructor of UnitTest. 12381be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // Mutable state in *impl_ is protected by mutex_. 12391be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania internal::UnitTestImpl* impl_; 12401be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 12411be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // We disallow copying UnitTest. 12421be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania GTEST_DISALLOW_COPY_AND_ASSIGN_(UnitTest); 12431be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}; 12441be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 12451be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// A convenient wrapper for adding an environment for the test 12461be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// program. 12471be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// 12481be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// You should call this before RUN_ALL_TESTS() is called, probably in 12491be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// main(). If you use gtest_main, you need to call this before main() 12501be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// starts for it to take effect. For example, you can define a global 12511be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// variable like this: 12521be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// 12531be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// testing::Environment* const foo_env = 12541be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// testing::AddGlobalTestEnvironment(new FooEnvironment); 12551be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// 12561be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// However, we strongly recommend you to write your own main() and 12571be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// call AddGlobalTestEnvironment() there, as relying on initialization 12581be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// of global variables makes the code harder to read and may cause 12591be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// problems when you register multiple environments from different 12601be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// translation units and the environments have dependencies among them 12611be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// (remember that the compiler doesn't guarantee the order in which 12621be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// global variables from different translation units are initialized). 12631be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniainline Environment* AddGlobalTestEnvironment(Environment* env) { 12641be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania return UnitTest::GetInstance()->AddEnvironment(env); 12651be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 12661be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 12671be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Initializes Google Test. This must be called before calling 12681be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// RUN_ALL_TESTS(). In particular, it parses a command line for the 12691be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// flags that Google Test recognizes. Whenever a Google Test flag is 12701be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// seen, it is removed from argv, and *argc is decremented. 12711be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// 12721be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// No value is returned. Instead, the Google Test flag variables are 12731be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// updated. 12741be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// 12751be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Calling the function for the second time has no user-visible effect. 127641d0579e8de9ef4ff178fc4991043c61a19943f7Brett ChabotGTEST_API_ void InitGoogleTest(int* argc, char** argv); 12771be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 12781be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// This overloaded version can be used in Windows programs compiled in 12791be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// UNICODE mode. 128041d0579e8de9ef4ff178fc4991043c61a19943f7Brett ChabotGTEST_API_ void InitGoogleTest(int* argc, wchar_t** argv); 12811be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 12821be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catanianamespace internal { 12831be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 12841be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Formats a comparison assertion (e.g. ASSERT_EQ, EXPECT_LT, and etc) 12851be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// operand to be used in a failure message. The type (but not value) 12861be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// of the other operand may affect the format. This allows us to 12871be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// print a char* as a raw pointer when it is compared against another 12881be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// char*, and print it as a C string when it is compared against an 12891be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// std::string object, for example. 12901be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// 12911be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// The default implementation ignores the type of the other operand. 12921be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Some specialized versions are used to handle formatting wide or 12931be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// narrow C strings. 12941be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// 12951be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM. 12961be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniatemplate <typename T1, typename T2> 12971be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaString FormatForComparisonFailureMessage(const T1& value, 12981be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania const T2& /* other_operand */) { 129941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // C++Builder compiles this incorrectly if the namespace isn't explicitly 130041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // given. 130141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot return ::testing::PrintToString(value); 13021be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 13031be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 13041be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// The helper function for {ASSERT|EXPECT}_EQ. 13051be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniatemplate <typename T1, typename T2> 13061be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaAssertionResult CmpHelperEQ(const char* expected_expression, 13071be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania const char* actual_expression, 13081be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania const T1& expected, 13091be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania const T2& actual) { 13101be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#ifdef _MSC_VER 131141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot# pragma warning(push) // Saves the current warning state. 131241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot# pragma warning(disable:4389) // Temporarily disables warning on 13131be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // signed/unsigned mismatch. 13141be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#endif 13151be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 13161be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania if (expected == actual) { 13171be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania return AssertionSuccess(); 13181be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania } 13191be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 13201be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#ifdef _MSC_VER 132141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot# pragma warning(pop) // Restores the warning state. 13221be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#endif 13231be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 13241be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania return EqFailure(expected_expression, 13251be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania actual_expression, 13261be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania FormatForComparisonFailureMessage(expected, actual), 13271be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania FormatForComparisonFailureMessage(actual, expected), 13281be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania false); 13291be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 13301be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 13311be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// With this overloaded version, we allow anonymous enums to be used 13321be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// in {ASSERT|EXPECT}_EQ when compiled with gcc 4, as anonymous enums 13331be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// can be implicitly cast to BiggestInt. 133441d0579e8de9ef4ff178fc4991043c61a19943f7Brett ChabotGTEST_API_ AssertionResult CmpHelperEQ(const char* expected_expression, 133541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot const char* actual_expression, 133641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot BiggestInt expected, 133741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot BiggestInt actual); 13381be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 13391be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// The helper class for {ASSERT|EXPECT}_EQ. The template argument 13401be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// lhs_is_null_literal is true iff the first argument to ASSERT_EQ() 13411be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// is a null pointer literal. The following default implementation is 13421be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// for lhs_is_null_literal being false. 13431be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniatemplate <bool lhs_is_null_literal> 13441be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniaclass EqHelper { 13451be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania public: 13461be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // This templatized version is for the general case. 13471be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania template <typename T1, typename T2> 13481be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania static AssertionResult Compare(const char* expected_expression, 13491be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania const char* actual_expression, 13501be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania const T1& expected, 13511be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania const T2& actual) { 13521be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania return CmpHelperEQ(expected_expression, actual_expression, expected, 13531be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania actual); 13541be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania } 13551be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 13561be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // With this overloaded version, we allow anonymous enums to be used 13571be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // in {ASSERT|EXPECT}_EQ when compiled with gcc 4, as anonymous 13581be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // enums can be implicitly cast to BiggestInt. 13591be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // 13601be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // Even though its body looks the same as the above version, we 13611be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // cannot merge the two, as it will make anonymous enums unhappy. 13621be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania static AssertionResult Compare(const char* expected_expression, 13631be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania const char* actual_expression, 13641be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania BiggestInt expected, 13651be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania BiggestInt actual) { 13661be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania return CmpHelperEQ(expected_expression, actual_expression, expected, 13671be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania actual); 13681be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania } 13691be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}; 13701be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 13711be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// This specialization is used when the first argument to ASSERT_EQ() 137241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// is a null pointer literal, like NULL, false, or 0. 13731be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniatemplate <> 13741be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniaclass EqHelper<true> { 13751be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania public: 13761be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // We define two overloaded versions of Compare(). The first 13771be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // version will be picked when the second argument to ASSERT_EQ() is 13781be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // NOT a pointer, e.g. ASSERT_EQ(0, AnIntFunction()) or 13791be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // EXPECT_EQ(false, a_bool). 13801be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania template <typename T1, typename T2> 138141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot static AssertionResult Compare( 138241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot const char* expected_expression, 138341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot const char* actual_expression, 138441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot const T1& expected, 138541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot const T2& actual, 138641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // The following line prevents this overload from being considered if T2 138741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // is not a pointer type. We need this because ASSERT_EQ(NULL, my_ptr) 138841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // expands to Compare("", "", NULL, my_ptr), which requires a conversion 138941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // to match the Secret* in the other overload, which would otherwise make 139041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // this template match better. 139141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot typename EnableIf<!is_pointer<T2>::value>::type* = 0) { 13921be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania return CmpHelperEQ(expected_expression, actual_expression, expected, 13931be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania actual); 13941be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania } 13951be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 139641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // This version will be picked when the second argument to ASSERT_EQ() is a 139741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // pointer, e.g. ASSERT_EQ(NULL, a_pointer). 139841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot template <typename T> 139941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot static AssertionResult Compare( 140041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot const char* expected_expression, 140141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot const char* actual_expression, 140241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // We used to have a second template parameter instead of Secret*. That 140341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // template parameter would deduce to 'long', making this a better match 140441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // than the first overload even without the first overload's EnableIf. 140541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // Unfortunately, gcc with -Wconversion-null warns when "passing NULL to 140641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // non-pointer argument" (even a deduced integral argument), so the old 140741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // implementation caused warnings in user code. 140841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot Secret* /* expected (NULL) */, 140941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot T* actual) { 14101be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // We already know that 'expected' is a null pointer. 14111be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania return CmpHelperEQ(expected_expression, actual_expression, 141241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot static_cast<T*>(NULL), actual); 14131be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania } 14141be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}; 14151be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 14161be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// A macro for implementing the helper functions needed to implement 14171be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// ASSERT_?? and EXPECT_??. It is here just to avoid copy-and-paste 14181be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// of similar code. 14191be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// 14201be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// For each templatized helper function, we also define an overloaded 14211be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// version for BiggestInt in order to reduce code bloat and allow 14221be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// anonymous enums to be used with {ASSERT|EXPECT}_?? when compiled 14231be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// with gcc 4. 14241be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// 14251be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM. 14261be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#define GTEST_IMPL_CMP_HELPER_(op_name, op)\ 14271be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniatemplate <typename T1, typename T2>\ 14281be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaAssertionResult CmpHelper##op_name(const char* expr1, const char* expr2, \ 14291be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania const T1& val1, const T2& val2) {\ 14301be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania if (val1 op val2) {\ 14311be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania return AssertionSuccess();\ 14321be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania } else {\ 143341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot return AssertionFailure() \ 143441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot << "Expected: (" << expr1 << ") " #op " (" << expr2\ 14351be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania << "), actual: " << FormatForComparisonFailureMessage(val1, val2)\ 14361be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania << " vs " << FormatForComparisonFailureMessage(val2, val1);\ 14371be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania }\ 14381be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}\ 143941d0579e8de9ef4ff178fc4991043c61a19943f7Brett ChabotGTEST_API_ AssertionResult CmpHelper##op_name(\ 144041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot const char* expr1, const char* expr2, BiggestInt val1, BiggestInt val2) 14411be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 14421be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM. 14431be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 14441be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Implements the helper function for {ASSERT|EXPECT}_NE 144541d0579e8de9ef4ff178fc4991043c61a19943f7Brett ChabotGTEST_IMPL_CMP_HELPER_(NE, !=); 14461be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Implements the helper function for {ASSERT|EXPECT}_LE 144741d0579e8de9ef4ff178fc4991043c61a19943f7Brett ChabotGTEST_IMPL_CMP_HELPER_(LE, <=); 14481be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Implements the helper function for {ASSERT|EXPECT}_LT 144941d0579e8de9ef4ff178fc4991043c61a19943f7Brett ChabotGTEST_IMPL_CMP_HELPER_(LT, < ); 14501be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Implements the helper function for {ASSERT|EXPECT}_GE 145141d0579e8de9ef4ff178fc4991043c61a19943f7Brett ChabotGTEST_IMPL_CMP_HELPER_(GE, >=); 14521be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Implements the helper function for {ASSERT|EXPECT}_GT 145341d0579e8de9ef4ff178fc4991043c61a19943f7Brett ChabotGTEST_IMPL_CMP_HELPER_(GT, > ); 14541be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 14551be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#undef GTEST_IMPL_CMP_HELPER_ 14561be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 14571be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// The helper function for {ASSERT|EXPECT}_STREQ. 14581be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// 14591be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM. 146041d0579e8de9ef4ff178fc4991043c61a19943f7Brett ChabotGTEST_API_ AssertionResult CmpHelperSTREQ(const char* expected_expression, 146141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot const char* actual_expression, 146241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot const char* expected, 146341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot const char* actual); 14641be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 14651be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// The helper function for {ASSERT|EXPECT}_STRCASEEQ. 14661be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// 14671be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM. 146841d0579e8de9ef4ff178fc4991043c61a19943f7Brett ChabotGTEST_API_ AssertionResult CmpHelperSTRCASEEQ(const char* expected_expression, 146941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot const char* actual_expression, 147041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot const char* expected, 147141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot const char* actual); 14721be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 14731be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// The helper function for {ASSERT|EXPECT}_STRNE. 14741be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// 14751be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM. 147641d0579e8de9ef4ff178fc4991043c61a19943f7Brett ChabotGTEST_API_ AssertionResult CmpHelperSTRNE(const char* s1_expression, 147741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot const char* s2_expression, 147841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot const char* s1, 147941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot const char* s2); 14801be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 14811be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// The helper function for {ASSERT|EXPECT}_STRCASENE. 14821be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// 14831be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM. 148441d0579e8de9ef4ff178fc4991043c61a19943f7Brett ChabotGTEST_API_ AssertionResult CmpHelperSTRCASENE(const char* s1_expression, 148541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot const char* s2_expression, 148641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot const char* s1, 148741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot const char* s2); 14881be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 14891be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 14901be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Helper function for *_STREQ on wide strings. 14911be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// 14921be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM. 149341d0579e8de9ef4ff178fc4991043c61a19943f7Brett ChabotGTEST_API_ AssertionResult CmpHelperSTREQ(const char* expected_expression, 149441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot const char* actual_expression, 149541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot const wchar_t* expected, 149641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot const wchar_t* actual); 14971be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 14981be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Helper function for *_STRNE on wide strings. 14991be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// 15001be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM. 150141d0579e8de9ef4ff178fc4991043c61a19943f7Brett ChabotGTEST_API_ AssertionResult CmpHelperSTRNE(const char* s1_expression, 150241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot const char* s2_expression, 150341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot const wchar_t* s1, 150441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot const wchar_t* s2); 15051be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 15061be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} // namespace internal 15071be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 15081be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// IsSubstring() and IsNotSubstring() are intended to be used as the 15091be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// first argument to {EXPECT,ASSERT}_PRED_FORMAT2(), not by 15101be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// themselves. They check whether needle is a substring of haystack 15111be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// (NULL is considered a substring of itself only), and return an 15121be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// appropriate error message when they fail. 15131be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// 15141be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// The {needle,haystack}_expr arguments are the stringified 15151be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// expressions that generated the two real arguments. 151641d0579e8de9ef4ff178fc4991043c61a19943f7Brett ChabotGTEST_API_ AssertionResult IsSubstring( 15171be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania const char* needle_expr, const char* haystack_expr, 15181be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania const char* needle, const char* haystack); 151941d0579e8de9ef4ff178fc4991043c61a19943f7Brett ChabotGTEST_API_ AssertionResult IsSubstring( 15201be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania const char* needle_expr, const char* haystack_expr, 15211be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania const wchar_t* needle, const wchar_t* haystack); 152241d0579e8de9ef4ff178fc4991043c61a19943f7Brett ChabotGTEST_API_ AssertionResult IsNotSubstring( 15231be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania const char* needle_expr, const char* haystack_expr, 15241be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania const char* needle, const char* haystack); 152541d0579e8de9ef4ff178fc4991043c61a19943f7Brett ChabotGTEST_API_ AssertionResult IsNotSubstring( 15261be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania const char* needle_expr, const char* haystack_expr, 15271be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania const wchar_t* needle, const wchar_t* haystack); 152841d0579e8de9ef4ff178fc4991043c61a19943f7Brett ChabotGTEST_API_ AssertionResult IsSubstring( 15291be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania const char* needle_expr, const char* haystack_expr, 15301be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania const ::std::string& needle, const ::std::string& haystack); 153141d0579e8de9ef4ff178fc4991043c61a19943f7Brett ChabotGTEST_API_ AssertionResult IsNotSubstring( 15321be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania const char* needle_expr, const char* haystack_expr, 15331be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania const ::std::string& needle, const ::std::string& haystack); 15341be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 15351be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#if GTEST_HAS_STD_WSTRING 153641d0579e8de9ef4ff178fc4991043c61a19943f7Brett ChabotGTEST_API_ AssertionResult IsSubstring( 15371be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania const char* needle_expr, const char* haystack_expr, 15381be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania const ::std::wstring& needle, const ::std::wstring& haystack); 153941d0579e8de9ef4ff178fc4991043c61a19943f7Brett ChabotGTEST_API_ AssertionResult IsNotSubstring( 15401be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania const char* needle_expr, const char* haystack_expr, 15411be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania const ::std::wstring& needle, const ::std::wstring& haystack); 15421be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#endif // GTEST_HAS_STD_WSTRING 15431be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 15441be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catanianamespace internal { 15451be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 15461be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Helper template function for comparing floating-points. 15471be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// 15481be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Template parameter: 15491be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// 15501be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// RawType: the raw floating-point type (either float or double) 15511be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// 15521be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM. 15531be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniatemplate <typename RawType> 15541be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaAssertionResult CmpHelperFloatingPointEQ(const char* expected_expression, 15551be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania const char* actual_expression, 15561be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania RawType expected, 15571be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania RawType actual) { 15581be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania const FloatingPoint<RawType> lhs(expected), rhs(actual); 15591be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 15601be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania if (lhs.AlmostEquals(rhs)) { 15611be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania return AssertionSuccess(); 15621be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania } 15631be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 156441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot ::std::stringstream expected_ss; 15651be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania expected_ss << std::setprecision(std::numeric_limits<RawType>::digits10 + 2) 15661be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania << expected; 15671be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 156841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot ::std::stringstream actual_ss; 15691be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania actual_ss << std::setprecision(std::numeric_limits<RawType>::digits10 + 2) 15701be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania << actual; 15711be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 15721be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania return EqFailure(expected_expression, 15731be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania actual_expression, 157441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot StringStreamToString(&expected_ss), 157541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot StringStreamToString(&actual_ss), 15761be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania false); 15771be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 15781be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 15791be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Helper function for implementing ASSERT_NEAR. 15801be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// 15811be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM. 158241d0579e8de9ef4ff178fc4991043c61a19943f7Brett ChabotGTEST_API_ AssertionResult DoubleNearPredFormat(const char* expr1, 158341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot const char* expr2, 158441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot const char* abs_error_expr, 158541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot double val1, 158641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot double val2, 158741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot double abs_error); 15881be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 15891be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// INTERNAL IMPLEMENTATION - DO NOT USE IN USER CODE. 15901be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// A class that enables one to stream messages to assertion macros 159141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotclass GTEST_API_ AssertHelper { 15921be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania public: 15931be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // Constructor. 159441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot AssertHelper(TestPartResult::Type type, 159541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot const char* file, 159641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot int line, 15971be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania const char* message); 159841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot ~AssertHelper(); 159941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 16001be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // Message assignment is a semantic trick to enable assertion 16011be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // streaming; see the GTEST_MESSAGE_ macro below. 16021be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania void operator=(const Message& message) const; 160341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 16041be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania private: 160541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // We put our data in a struct so that the size of the AssertHelper class can 160641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // be as small as possible. This is important because gcc is incapable of 160741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // re-using stack space even for temporary variables, so every EXPECT_EQ 160841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // reserves stack space for another AssertHelper. 160941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot struct AssertHelperData { 161041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot AssertHelperData(TestPartResult::Type t, 161141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot const char* srcfile, 161241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot int line_num, 161341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot const char* msg) 161441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot : type(t), file(srcfile), line(line_num), message(msg) { } 161541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 161641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot TestPartResult::Type const type; 161741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot const char* const file; 161841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot int const line; 161941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot String const message; 162041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 162141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot private: 162241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot GTEST_DISALLOW_COPY_AND_ASSIGN_(AssertHelperData); 162341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot }; 162441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 162541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot AssertHelperData* const data_; 16261be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 16271be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania GTEST_DISALLOW_COPY_AND_ASSIGN_(AssertHelper); 16281be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}; 16291be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 16301be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} // namespace internal 16311be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 16321be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#if GTEST_HAS_PARAM_TEST 163341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// The pure interface class that all value-parameterized tests inherit from. 163441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// A value-parameterized class must inherit from both ::testing::Test and 163541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// ::testing::WithParamInterface. In most cases that just means inheriting 163641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// from ::testing::TestWithParam, but more complicated test hierarchies 163741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// may need to inherit from Test and WithParamInterface at different levels. 16381be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// 163941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// This interface has support for accessing the test parameter value via 16401be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// the GetParam() method. 16411be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// 16421be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Use it with one of the parameter generator defining functions, like Range(), 16431be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Values(), ValuesIn(), Bool(), and Combine(). 16441be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// 16451be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// class FooTest : public ::testing::TestWithParam<int> { 16461be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// protected: 16471be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// FooTest() { 16481be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// // Can use GetParam() here. 16491be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// } 16501be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// virtual ~FooTest() { 16511be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// // Can use GetParam() here. 16521be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// } 16531be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// virtual void SetUp() { 16541be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// // Can use GetParam() here. 16551be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// } 16561be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// virtual void TearDown { 16571be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// // Can use GetParam() here. 16581be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// } 16591be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// }; 16601be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// TEST_P(FooTest, DoesBar) { 16611be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// // Can use GetParam() method here. 16621be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Foo foo; 16631be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// ASSERT_TRUE(foo.DoesBar(GetParam())); 16641be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// } 16651be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// INSTANTIATE_TEST_CASE_P(OneToTenRange, FooTest, ::testing::Range(1, 10)); 16661be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 16671be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniatemplate <typename T> 166841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotclass WithParamInterface { 16691be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania public: 16701be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania typedef T ParamType; 167141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot virtual ~WithParamInterface() {} 16721be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 16731be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // The current parameter value. Is also available in the test fixture's 167441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // constructor. This member function is non-static, even though it only 167541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // references static data, to reduce the opportunity for incorrect uses 167641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // like writing 'WithParamInterface<bool>::GetParam()' for a test that 167741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // uses a fixture whose parameter type is int. 16781be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania const ParamType& GetParam() const { return *parameter_; } 16791be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 16801be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania private: 16811be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // Sets parameter value. The caller is responsible for making sure the value 16821be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // remains alive and unchanged throughout the current test. 16831be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania static void SetParam(const ParamType* parameter) { 16841be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania parameter_ = parameter; 16851be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania } 16861be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 16871be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // Static value used for accessing parameter during a test lifetime. 16881be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania static const ParamType* parameter_; 16891be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 169041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // TestClass must be a subclass of WithParamInterface<T> and Test. 16911be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania template <class TestClass> friend class internal::ParameterizedTestFactory; 16921be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}; 16931be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 16941be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniatemplate <typename T> 169541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotconst T* WithParamInterface<T>::parameter_ = NULL; 169641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 169741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// Most value-parameterized classes can ignore the existence of 169841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// WithParamInterface, and can just inherit from ::testing::TestWithParam. 169941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 170041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabottemplate <typename T> 170141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotclass TestWithParam : public Test, public WithParamInterface<T> { 170241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot}; 17031be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 17041be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#endif // GTEST_HAS_PARAM_TEST 17051be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 17061be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Macros for indicating success/failure in test code. 17071be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 17081be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// ADD_FAILURE unconditionally adds a failure to the current test. 17091be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// SUCCEED generates a success - it doesn't automatically make the 17101be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// current test successful, as a test is only successful when it has 17111be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// no failure. 17121be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// 17131be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// EXPECT_* verifies that a certain condition is satisfied. If not, 17141be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// it behaves like ADD_FAILURE. In particular: 17151be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// 17161be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// EXPECT_TRUE verifies that a Boolean condition is true. 17171be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// EXPECT_FALSE verifies that a Boolean condition is false. 17181be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// 17191be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// FAIL and ASSERT_* are similar to ADD_FAILURE and EXPECT_*, except 17201be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// that they will also abort the current function on failure. People 17211be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// usually want the fail-fast behavior of FAIL and ASSERT_*, but those 17221be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// writing data-driven tests often find themselves using ADD_FAILURE 17231be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// and EXPECT_* more. 17241be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// 17251be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Examples: 17261be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// 17271be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// EXPECT_TRUE(server.StatusIsOK()); 17281be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// ASSERT_FALSE(server.HasPendingRequest(port)) 17291be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// << "There are still pending requests " << "on port " << port; 17301be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 17311be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Generates a nonfatal failure with a generic message. 17321be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#define ADD_FAILURE() GTEST_NONFATAL_FAILURE_("Failed") 17331be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 173441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// Generates a nonfatal failure at the given source file location with 173541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// a generic message. 173641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot#define ADD_FAILURE_AT(file, line) \ 173741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot GTEST_MESSAGE_AT_(file, line, "Failed", \ 173841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot ::testing::TestPartResult::kNonFatalFailure) 173941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 17401be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Generates a fatal failure with a generic message. 174141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot#define GTEST_FAIL() GTEST_FATAL_FAILURE_("Failed") 174241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 174341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// Define this macro to 1 to omit the definition of FAIL(), which is a 174441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// generic name and clashes with some other libraries. 174541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot#if !GTEST_DONT_DEFINE_FAIL 174641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot# define FAIL() GTEST_FAIL() 174741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot#endif 17481be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 17491be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Generates a success with a generic message. 175041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot#define GTEST_SUCCEED() GTEST_SUCCESS_("Succeeded") 175141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 175241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// Define this macro to 1 to omit the definition of SUCCEED(), which 175341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// is a generic name and clashes with some other libraries. 175441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot#if !GTEST_DONT_DEFINE_SUCCEED 175541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot# define SUCCEED() GTEST_SUCCEED() 175641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot#endif 17571be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 17581be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Macros for testing exceptions. 17591be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// 17601be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// * {ASSERT|EXPECT}_THROW(statement, expected_exception): 17611be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Tests that the statement throws the expected exception. 17621be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// * {ASSERT|EXPECT}_NO_THROW(statement): 17631be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Tests that the statement doesn't throw any exception. 17641be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// * {ASSERT|EXPECT}_ANY_THROW(statement): 17651be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Tests that the statement throws an exception. 17661be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 17671be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#define EXPECT_THROW(statement, expected_exception) \ 17681be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania GTEST_TEST_THROW_(statement, expected_exception, GTEST_NONFATAL_FAILURE_) 17691be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#define EXPECT_NO_THROW(statement) \ 17701be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania GTEST_TEST_NO_THROW_(statement, GTEST_NONFATAL_FAILURE_) 17711be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#define EXPECT_ANY_THROW(statement) \ 17721be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania GTEST_TEST_ANY_THROW_(statement, GTEST_NONFATAL_FAILURE_) 17731be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#define ASSERT_THROW(statement, expected_exception) \ 17741be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania GTEST_TEST_THROW_(statement, expected_exception, GTEST_FATAL_FAILURE_) 17751be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#define ASSERT_NO_THROW(statement) \ 17761be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania GTEST_TEST_NO_THROW_(statement, GTEST_FATAL_FAILURE_) 17771be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#define ASSERT_ANY_THROW(statement) \ 17781be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania GTEST_TEST_ANY_THROW_(statement, GTEST_FATAL_FAILURE_) 17791be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 178041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// Boolean assertions. Condition can be either a Boolean expression or an 178141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// AssertionResult. For more information on how to use AssertionResult with 178241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// these macros see comments on that class. 17831be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#define EXPECT_TRUE(condition) \ 17841be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania GTEST_TEST_BOOLEAN_(condition, #condition, false, true, \ 17851be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania GTEST_NONFATAL_FAILURE_) 17861be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#define EXPECT_FALSE(condition) \ 17871be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania GTEST_TEST_BOOLEAN_(!(condition), #condition, true, false, \ 17881be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania GTEST_NONFATAL_FAILURE_) 17891be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#define ASSERT_TRUE(condition) \ 17901be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania GTEST_TEST_BOOLEAN_(condition, #condition, false, true, \ 17911be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania GTEST_FATAL_FAILURE_) 17921be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#define ASSERT_FALSE(condition) \ 17931be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania GTEST_TEST_BOOLEAN_(!(condition), #condition, true, false, \ 17941be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania GTEST_FATAL_FAILURE_) 17951be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 17961be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Includes the auto-generated header that implements a family of 17971be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// generic predicate assertion macros. 179841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot#include "gtest/gtest_pred_impl.h" 17991be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 18001be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Macros for testing equalities and inequalities. 18011be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// 18021be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// * {ASSERT|EXPECT}_EQ(expected, actual): Tests that expected == actual 18031be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// * {ASSERT|EXPECT}_NE(v1, v2): Tests that v1 != v2 18041be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// * {ASSERT|EXPECT}_LT(v1, v2): Tests that v1 < v2 18051be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// * {ASSERT|EXPECT}_LE(v1, v2): Tests that v1 <= v2 18061be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// * {ASSERT|EXPECT}_GT(v1, v2): Tests that v1 > v2 18071be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// * {ASSERT|EXPECT}_GE(v1, v2): Tests that v1 >= v2 18081be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// 18091be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// When they are not, Google Test prints both the tested expressions and 18101be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// their actual values. The values must be compatible built-in types, 18111be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// or you will get a compiler error. By "compatible" we mean that the 18121be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// values can be compared by the respective operator. 18131be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// 18141be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Note: 18151be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// 18161be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// 1. It is possible to make a user-defined type work with 18171be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// {ASSERT|EXPECT}_??(), but that requires overloading the 18181be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// comparison operators and is thus discouraged by the Google C++ 18191be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Usage Guide. Therefore, you are advised to use the 18201be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// {ASSERT|EXPECT}_TRUE() macro to assert that two objects are 18211be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// equal. 18221be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// 18231be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// 2. The {ASSERT|EXPECT}_??() macros do pointer comparisons on 18241be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// pointers (in particular, C strings). Therefore, if you use it 18251be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// with two C strings, you are testing how their locations in memory 18261be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// are related, not how their content is related. To compare two C 18271be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// strings by content, use {ASSERT|EXPECT}_STR*(). 18281be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// 18291be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// 3. {ASSERT|EXPECT}_EQ(expected, actual) is preferred to 18301be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// {ASSERT|EXPECT}_TRUE(expected == actual), as the former tells you 18311be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// what the actual value is when it fails, and similarly for the 18321be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// other comparisons. 18331be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// 18341be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// 4. Do not depend on the order in which {ASSERT|EXPECT}_??() 18351be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// evaluate their arguments, which is undefined. 18361be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// 18371be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// 5. These macros evaluate their arguments exactly once. 18381be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// 18391be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Examples: 18401be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// 18411be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// EXPECT_NE(5, Foo()); 18421be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// EXPECT_EQ(NULL, a_pointer); 18431be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// ASSERT_LT(i, array_size); 18441be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// ASSERT_GT(records.size(), 0) << "There is no record left."; 18451be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 18461be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#define EXPECT_EQ(expected, actual) \ 18471be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania EXPECT_PRED_FORMAT2(::testing::internal:: \ 18481be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania EqHelper<GTEST_IS_NULL_LITERAL_(expected)>::Compare, \ 18491be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania expected, actual) 18501be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#define EXPECT_NE(expected, actual) \ 18511be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania EXPECT_PRED_FORMAT2(::testing::internal::CmpHelperNE, expected, actual) 18521be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#define EXPECT_LE(val1, val2) \ 18531be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania EXPECT_PRED_FORMAT2(::testing::internal::CmpHelperLE, val1, val2) 18541be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#define EXPECT_LT(val1, val2) \ 18551be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania EXPECT_PRED_FORMAT2(::testing::internal::CmpHelperLT, val1, val2) 18561be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#define EXPECT_GE(val1, val2) \ 18571be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania EXPECT_PRED_FORMAT2(::testing::internal::CmpHelperGE, val1, val2) 18581be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#define EXPECT_GT(val1, val2) \ 18591be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania EXPECT_PRED_FORMAT2(::testing::internal::CmpHelperGT, val1, val2) 18601be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 186141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot#define GTEST_ASSERT_EQ(expected, actual) \ 18621be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania ASSERT_PRED_FORMAT2(::testing::internal:: \ 18631be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania EqHelper<GTEST_IS_NULL_LITERAL_(expected)>::Compare, \ 18641be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania expected, actual) 186541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot#define GTEST_ASSERT_NE(val1, val2) \ 18661be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania ASSERT_PRED_FORMAT2(::testing::internal::CmpHelperNE, val1, val2) 186741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot#define GTEST_ASSERT_LE(val1, val2) \ 18681be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania ASSERT_PRED_FORMAT2(::testing::internal::CmpHelperLE, val1, val2) 186941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot#define GTEST_ASSERT_LT(val1, val2) \ 18701be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania ASSERT_PRED_FORMAT2(::testing::internal::CmpHelperLT, val1, val2) 187141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot#define GTEST_ASSERT_GE(val1, val2) \ 18721be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania ASSERT_PRED_FORMAT2(::testing::internal::CmpHelperGE, val1, val2) 187341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot#define GTEST_ASSERT_GT(val1, val2) \ 18741be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania ASSERT_PRED_FORMAT2(::testing::internal::CmpHelperGT, val1, val2) 18751be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 187641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// Define macro GTEST_DONT_DEFINE_ASSERT_XY to 1 to omit the definition of 187741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// ASSERT_XY(), which clashes with some users' own code. 187841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 187941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot#if !GTEST_DONT_DEFINE_ASSERT_EQ 188041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot# define ASSERT_EQ(val1, val2) GTEST_ASSERT_EQ(val1, val2) 188141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot#endif 188241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 188341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot#if !GTEST_DONT_DEFINE_ASSERT_NE 188441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot# define ASSERT_NE(val1, val2) GTEST_ASSERT_NE(val1, val2) 188541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot#endif 188641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 188741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot#if !GTEST_DONT_DEFINE_ASSERT_LE 188841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot# define ASSERT_LE(val1, val2) GTEST_ASSERT_LE(val1, val2) 188941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot#endif 189041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 189141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot#if !GTEST_DONT_DEFINE_ASSERT_LT 189241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot# define ASSERT_LT(val1, val2) GTEST_ASSERT_LT(val1, val2) 189341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot#endif 189441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 189541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot#if !GTEST_DONT_DEFINE_ASSERT_GE 189641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot# define ASSERT_GE(val1, val2) GTEST_ASSERT_GE(val1, val2) 189741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot#endif 189841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 189941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot#if !GTEST_DONT_DEFINE_ASSERT_GT 190041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot# define ASSERT_GT(val1, val2) GTEST_ASSERT_GT(val1, val2) 190141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot#endif 190241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 19031be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// C String Comparisons. All tests treat NULL and any non-NULL string 19041be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// as different. Two NULLs are equal. 19051be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// 19061be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// * {ASSERT|EXPECT}_STREQ(s1, s2): Tests that s1 == s2 19071be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// * {ASSERT|EXPECT}_STRNE(s1, s2): Tests that s1 != s2 19081be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// * {ASSERT|EXPECT}_STRCASEEQ(s1, s2): Tests that s1 == s2, ignoring case 19091be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// * {ASSERT|EXPECT}_STRCASENE(s1, s2): Tests that s1 != s2, ignoring case 19101be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// 19111be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// For wide or narrow string objects, you can use the 19121be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// {ASSERT|EXPECT}_??() macros. 19131be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// 19141be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Don't depend on the order in which the arguments are evaluated, 19151be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// which is undefined. 19161be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// 19171be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// These macros evaluate their arguments exactly once. 19181be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 19191be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#define EXPECT_STREQ(expected, actual) \ 19201be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania EXPECT_PRED_FORMAT2(::testing::internal::CmpHelperSTREQ, expected, actual) 19211be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#define EXPECT_STRNE(s1, s2) \ 19221be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania EXPECT_PRED_FORMAT2(::testing::internal::CmpHelperSTRNE, s1, s2) 19231be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#define EXPECT_STRCASEEQ(expected, actual) \ 19241be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania EXPECT_PRED_FORMAT2(::testing::internal::CmpHelperSTRCASEEQ, expected, actual) 19251be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#define EXPECT_STRCASENE(s1, s2)\ 19261be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania EXPECT_PRED_FORMAT2(::testing::internal::CmpHelperSTRCASENE, s1, s2) 19271be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 19281be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#define ASSERT_STREQ(expected, actual) \ 19291be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania ASSERT_PRED_FORMAT2(::testing::internal::CmpHelperSTREQ, expected, actual) 19301be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#define ASSERT_STRNE(s1, s2) \ 19311be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania ASSERT_PRED_FORMAT2(::testing::internal::CmpHelperSTRNE, s1, s2) 19321be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#define ASSERT_STRCASEEQ(expected, actual) \ 19331be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania ASSERT_PRED_FORMAT2(::testing::internal::CmpHelperSTRCASEEQ, expected, actual) 19341be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#define ASSERT_STRCASENE(s1, s2)\ 19351be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania ASSERT_PRED_FORMAT2(::testing::internal::CmpHelperSTRCASENE, s1, s2) 19361be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 19371be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Macros for comparing floating-point numbers. 19381be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// 19391be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// * {ASSERT|EXPECT}_FLOAT_EQ(expected, actual): 19401be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Tests that two float values are almost equal. 19411be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// * {ASSERT|EXPECT}_DOUBLE_EQ(expected, actual): 19421be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Tests that two double values are almost equal. 19431be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// * {ASSERT|EXPECT}_NEAR(v1, v2, abs_error): 19441be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Tests that v1 and v2 are within the given distance to each other. 19451be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// 19461be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Google Test uses ULP-based comparison to automatically pick a default 19471be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// error bound that is appropriate for the operands. See the 19481be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// FloatingPoint template class in gtest-internal.h if you are 19491be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// interested in the implementation details. 19501be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 19511be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#define EXPECT_FLOAT_EQ(expected, actual)\ 19521be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania EXPECT_PRED_FORMAT2(::testing::internal::CmpHelperFloatingPointEQ<float>, \ 19531be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania expected, actual) 19541be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 19551be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#define EXPECT_DOUBLE_EQ(expected, actual)\ 19561be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania EXPECT_PRED_FORMAT2(::testing::internal::CmpHelperFloatingPointEQ<double>, \ 19571be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania expected, actual) 19581be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 19591be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#define ASSERT_FLOAT_EQ(expected, actual)\ 19601be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania ASSERT_PRED_FORMAT2(::testing::internal::CmpHelperFloatingPointEQ<float>, \ 19611be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania expected, actual) 19621be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 19631be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#define ASSERT_DOUBLE_EQ(expected, actual)\ 19641be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania ASSERT_PRED_FORMAT2(::testing::internal::CmpHelperFloatingPointEQ<double>, \ 19651be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania expected, actual) 19661be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 19671be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#define EXPECT_NEAR(val1, val2, abs_error)\ 19681be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania EXPECT_PRED_FORMAT3(::testing::internal::DoubleNearPredFormat, \ 19691be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania val1, val2, abs_error) 19701be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 19711be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#define ASSERT_NEAR(val1, val2, abs_error)\ 19721be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania ASSERT_PRED_FORMAT3(::testing::internal::DoubleNearPredFormat, \ 19731be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania val1, val2, abs_error) 19741be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 19751be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// These predicate format functions work on floating-point values, and 19761be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// can be used in {ASSERT|EXPECT}_PRED_FORMAT2*(), e.g. 19771be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// 19781be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// EXPECT_PRED_FORMAT2(testing::DoubleLE, Foo(), 5.0); 19791be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 19801be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Asserts that val1 is less than, or almost equal to, val2. Fails 19811be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// otherwise. In particular, it fails if either val1 or val2 is NaN. 198241d0579e8de9ef4ff178fc4991043c61a19943f7Brett ChabotGTEST_API_ AssertionResult FloatLE(const char* expr1, const char* expr2, 198341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot float val1, float val2); 198441d0579e8de9ef4ff178fc4991043c61a19943f7Brett ChabotGTEST_API_ AssertionResult DoubleLE(const char* expr1, const char* expr2, 198541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot double val1, double val2); 19861be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 19871be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 19881be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#if GTEST_OS_WINDOWS 19891be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 19901be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Macros that test for HRESULT failure and success, these are only useful 19911be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// on Windows, and rely on Windows SDK macros and APIs to compile. 19921be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// 19931be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// * {ASSERT|EXPECT}_HRESULT_{SUCCEEDED|FAILED}(expr) 19941be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// 19951be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// When expr unexpectedly fails or succeeds, Google Test prints the 19961be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// expected result and the actual result with both a human-readable 19971be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// string representation of the error, if available, as well as the 19981be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// hex result code. 199941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot# define EXPECT_HRESULT_SUCCEEDED(expr) \ 20001be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania EXPECT_PRED_FORMAT1(::testing::internal::IsHRESULTSuccess, (expr)) 20011be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 200241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot# define ASSERT_HRESULT_SUCCEEDED(expr) \ 20031be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania ASSERT_PRED_FORMAT1(::testing::internal::IsHRESULTSuccess, (expr)) 20041be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 200541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot# define EXPECT_HRESULT_FAILED(expr) \ 20061be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania EXPECT_PRED_FORMAT1(::testing::internal::IsHRESULTFailure, (expr)) 20071be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 200841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot# define ASSERT_HRESULT_FAILED(expr) \ 20091be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania ASSERT_PRED_FORMAT1(::testing::internal::IsHRESULTFailure, (expr)) 20101be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 20111be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#endif // GTEST_OS_WINDOWS 20121be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 20131be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Macros that execute statement and check that it doesn't generate new fatal 20141be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// failures in the current thread. 20151be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// 20161be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// * {ASSERT|EXPECT}_NO_FATAL_FAILURE(statement); 20171be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// 20181be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Examples: 20191be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// 20201be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// EXPECT_NO_FATAL_FAILURE(Process()); 20211be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// ASSERT_NO_FATAL_FAILURE(Process()) << "Process() failed"; 20221be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// 20231be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#define ASSERT_NO_FATAL_FAILURE(statement) \ 20241be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania GTEST_TEST_NO_FATAL_FAILURE_(statement, GTEST_FATAL_FAILURE_) 20251be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#define EXPECT_NO_FATAL_FAILURE(statement) \ 20261be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania GTEST_TEST_NO_FATAL_FAILURE_(statement, GTEST_NONFATAL_FAILURE_) 20271be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 20281be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Causes a trace (including the source file path, the current line 20291be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// number, and the given message) to be included in every test failure 20301be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// message generated by code in the current scope. The effect is 20311be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// undone when the control leaves the current scope. 20321be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// 20331be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// The message argument can be anything streamable to std::ostream. 20341be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// 20351be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// In the implementation, we include the current line number as part 20361be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// of the dummy variable name, thus allowing multiple SCOPED_TRACE()s 20371be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// to appear in the same block - as long as they are on different 20381be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// lines. 20391be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#define SCOPED_TRACE(message) \ 20401be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania ::testing::internal::ScopedTrace GTEST_CONCAT_TOKEN_(gtest_trace_, __LINE__)(\ 20411be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania __FILE__, __LINE__, ::testing::Message() << (message)) 20421be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 20431be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Compile-time assertion for type equality. 20441be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// StaticAssertTypeEq<type1, type2>() compiles iff type1 and type2 are 20451be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// the same type. The value it returns is not interesting. 20461be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// 20471be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Instead of making StaticAssertTypeEq a class template, we make it a 20481be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// function template that invokes a helper class template. This 20491be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// prevents a user from misusing StaticAssertTypeEq<T1, T2> by 20501be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// defining objects of that type. 20511be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// 20521be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// CAVEAT: 20531be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// 20541be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// When used inside a method of a class template, 20551be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// StaticAssertTypeEq<T1, T2>() is effective ONLY IF the method is 20561be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// instantiated. For example, given: 20571be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// 20581be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// template <typename T> class Foo { 20591be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// public: 20601be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// void Bar() { testing::StaticAssertTypeEq<int, T>(); } 20611be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// }; 20621be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// 20631be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// the code: 20641be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// 20651be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// void Test1() { Foo<bool> foo; } 20661be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// 20671be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// will NOT generate a compiler error, as Foo<bool>::Bar() is never 20681be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// actually instantiated. Instead, you need: 20691be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// 20701be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// void Test2() { Foo<bool> foo; foo.Bar(); } 20711be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// 20721be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// to cause a compiler error. 20731be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniatemplate <typename T1, typename T2> 20741be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniabool StaticAssertTypeEq() { 207541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot (void)internal::StaticAssertTypeEqHelper<T1, T2>(); 20761be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania return true; 20771be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 20781be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 20791be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Defines a test. 20801be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// 20811be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// The first parameter is the name of the test case, and the second 20821be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// parameter is the name of the test within the test case. 20831be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// 20841be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// The convention is to end the test case name with "Test". For 20851be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// example, a test case for the Foo class can be named FooTest. 20861be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// 20871be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// The user should put his test code between braces after using this 20881be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// macro. Example: 20891be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// 20901be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// TEST(FooTest, InitializesCorrectly) { 20911be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Foo foo; 20921be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// EXPECT_TRUE(foo.StatusIsOK()); 20931be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// } 20941be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 20951be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Note that we call GetTestTypeId() instead of GetTypeId< 20961be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// ::testing::Test>() here to get the type ID of testing::Test. This 20971be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// is to work around a suspected linker bug when using Google Test as 20981be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// a framework on Mac OS X. The bug causes GetTypeId< 20991be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// ::testing::Test>() to return different values depending on whether 21001be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// the call is from the Google Test framework itself or from user test 21011be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// code. GetTestTypeId() is guaranteed to always return the same 21021be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// value, as it always calls GetTypeId<>() from the Google Test 21031be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// framework. 210441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot#define GTEST_TEST(test_case_name, test_name)\ 21051be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania GTEST_TEST_(test_case_name, test_name, \ 21061be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania ::testing::Test, ::testing::internal::GetTestTypeId()) 21071be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 210841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// Define this macro to 1 to omit the definition of TEST(), which 210941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// is a generic name and clashes with some other libraries. 211041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot#if !GTEST_DONT_DEFINE_TEST 211141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot# define TEST(test_case_name, test_name) GTEST_TEST(test_case_name, test_name) 211241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot#endif 21131be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 21141be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Defines a test that uses a test fixture. 21151be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// 21161be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// The first parameter is the name of the test fixture class, which 21171be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// also doubles as the test case name. The second parameter is the 21181be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// name of the test within the test case. 21191be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// 21201be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// A test fixture class must be declared earlier. The user should put 21211be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// his test code between braces after using this macro. Example: 21221be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// 21231be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// class FooTest : public testing::Test { 21241be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// protected: 21251be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// virtual void SetUp() { b_.AddElement(3); } 21261be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// 21271be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Foo a_; 21281be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Foo b_; 21291be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// }; 21301be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// 21311be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// TEST_F(FooTest, InitializesCorrectly) { 21321be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// EXPECT_TRUE(a_.StatusIsOK()); 21331be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// } 21341be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// 21351be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// TEST_F(FooTest, ReturnsElementCountCorrectly) { 21361be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// EXPECT_EQ(0, a_.size()); 21371be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// EXPECT_EQ(1, b_.size()); 21381be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// } 21391be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 21401be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#define TEST_F(test_fixture, test_name)\ 21411be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania GTEST_TEST_(test_fixture, test_name, test_fixture, \ 21421be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania ::testing::internal::GetTypeId<test_fixture>()) 21431be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 21441be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Use this macro in main() to run all tests. It returns 0 if all 21451be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// tests are successful, or 1 otherwise. 21461be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// 21471be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// RUN_ALL_TESTS() should be invoked after the command line has been 21481be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// parsed by InitGoogleTest(). 21491be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 21501be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#define RUN_ALL_TESTS()\ 21511be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania (::testing::UnitTest::GetInstance()->Run()) 21521be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 21531be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} // namespace testing 21541be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 21551be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#endif // GTEST_INCLUDE_GTEST_GTEST_H_ 2156