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 3441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot#include "gtest/gtest.h" 3541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot#include "gtest/gtest-spi.h" 361be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 371be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#include <ctype.h> 381be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#include <math.h> 391be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#include <stdarg.h> 401be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#include <stdio.h> 411be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#include <stdlib.h> 421be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#include <wchar.h> 431be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#include <wctype.h> 441be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 4541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot#include <algorithm> 4641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot#include <ostream> // NOLINT 4741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot#include <sstream> 4841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot#include <vector> 4941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 501be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#if GTEST_OS_LINUX 511be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 521be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// TODO(kenton@google.com): Use autoconf to detect availability of 531be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// gettimeofday(). 5441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot# define GTEST_HAS_GETTIMEOFDAY_ 1 551be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 5641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot# include <fcntl.h> // NOLINT 5741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot# include <limits.h> // NOLINT 5841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot# include <sched.h> // NOLINT 591be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Declares vsnprintf(). This header is not available on Windows. 6041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot# include <strings.h> // NOLINT 6141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot# include <sys/mman.h> // NOLINT 6241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot# include <sys/time.h> // NOLINT 6341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot# include <unistd.h> // NOLINT 6441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot# include <string> 651be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 661be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#elif GTEST_OS_SYMBIAN 6741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot# define GTEST_HAS_GETTIMEOFDAY_ 1 6841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot# include <sys/time.h> // NOLINT 691be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 701be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#elif GTEST_OS_ZOS 7141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot# define GTEST_HAS_GETTIMEOFDAY_ 1 7241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot# include <sys/time.h> // NOLINT 731be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 741be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// On z/OS we additionally need strings.h for strcasecmp. 7541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot# include <strings.h> // NOLINT 761be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 7741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot#elif GTEST_OS_WINDOWS_MOBILE // We are on Windows CE. 781be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 7941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot# include <windows.h> // NOLINT 801be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 811be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#elif GTEST_OS_WINDOWS // We are on Windows proper. 821be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 8341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot# include <io.h> // NOLINT 8441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot# include <sys/timeb.h> // NOLINT 8541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot# include <sys/types.h> // NOLINT 8641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot# include <sys/stat.h> // NOLINT 871be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 8841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot# if GTEST_OS_WINDOWS_MINGW 891be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// MinGW has gettimeofday() but not _ftime64(). 901be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// TODO(kenton@google.com): Use autoconf to detect availability of 911be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// gettimeofday(). 921be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// TODO(kenton@google.com): There are other ways to get the time on 931be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Windows, like GetTickCount() or GetSystemTimeAsFileTime(). MinGW 941be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// supports these. consider using them instead. 9541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot# define GTEST_HAS_GETTIMEOFDAY_ 1 9641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot# include <sys/time.h> // NOLINT 9741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot# endif // GTEST_OS_WINDOWS_MINGW 981be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 991be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// cpplint thinks that the header is already included, so we want to 1001be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// silence it. 10141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot# include <windows.h> // NOLINT 1021be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 1031be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#else 1041be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 1051be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Assume other platforms have gettimeofday(). 1061be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// TODO(kenton@google.com): Use autoconf to detect availability of 1071be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// gettimeofday(). 10841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot# define GTEST_HAS_GETTIMEOFDAY_ 1 1091be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 1101be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// cpplint thinks that the header is already included, so we want to 1111be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// silence it. 11241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot# include <sys/time.h> // NOLINT 11341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot# include <unistd.h> // NOLINT 1141be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 1151be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#endif // GTEST_OS_LINUX 1161be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 1171be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#if GTEST_HAS_EXCEPTIONS 11841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot# include <stdexcept> 11941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot#endif 12041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 12141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot#if GTEST_CAN_STREAM_RESULTS_ 12241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot# include <arpa/inet.h> // NOLINT 12341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot# include <netdb.h> // NOLINT 1241be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#endif 1251be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 1261be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Indicates that this translation unit is part of Google Test's 1271be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// implementation. It must come before gtest-internal-inl.h is 1281be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// included, or there will be a compiler error. This trick is to 1291be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// prevent a user from accidentally including gtest-internal-inl.h in 1301be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// his code. 1311be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#define GTEST_IMPLEMENTATION_ 1 1321be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#include "src/gtest-internal-inl.h" 1331be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#undef GTEST_IMPLEMENTATION_ 1341be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 1351be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#if GTEST_OS_WINDOWS 13641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot# define vsnprintf _vsnprintf 1371be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#endif // GTEST_OS_WINDOWS 1381be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 1391be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catanianamespace testing { 1401be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 14141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotusing internal::CountIf; 14241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotusing internal::ForEach; 14341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotusing internal::GetElementOr; 14441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotusing internal::Shuffle; 14541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 1461be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Constants. 1471be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 1481be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// A test whose test case name or test name matches this filter is 1491be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// disabled and not run. 1501be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniastatic const char kDisableTestFilter[] = "DISABLED_*:*/DISABLED_*"; 1511be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 1521be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// A test case whose name matches this filter is considered a death 1531be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// test case and will be run before test cases whose name doesn't 1541be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// match this filter. 1551be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniastatic const char kDeathTestCaseFilter[] = "*DeathTest:*DeathTest/*"; 1561be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 1571be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// A test filter that matches everything. 1581be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniastatic const char kUniversalFilter[] = "*"; 1591be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 1601be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// The default output file for XML output. 1611be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniastatic const char kDefaultOutputFile[] = "test_detail.xml"; 1621be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 1631be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// The environment variable name for the test shard index. 1641be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniastatic const char kTestShardIndex[] = "GTEST_SHARD_INDEX"; 1651be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// The environment variable name for the total number of test shards. 1661be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniastatic const char kTestTotalShards[] = "GTEST_TOTAL_SHARDS"; 1671be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// The environment variable name for the test shard status file. 1681be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniastatic const char kTestShardStatusFile[] = "GTEST_SHARD_STATUS_FILE"; 1691be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 1701be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catanianamespace internal { 1711be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 1721be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// The text used in failure messages to indicate the start of the 1731be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// stack trace. 1741be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniaconst char kStackTraceMarker[] = "\nStack trace:\n"; 1751be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 17641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// g_help_flag is true iff the --help flag or an equivalent form is 17741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// specified on the command line. 17841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotbool g_help_flag = false; 17941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 1801be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} // namespace internal 1811be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 1821be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaGTEST_DEFINE_bool_( 1831be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania also_run_disabled_tests, 1841be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania internal::BoolFromGTestEnv("also_run_disabled_tests", false), 1851be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania "Run disabled tests too, in addition to the tests normally being run."); 1861be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 1871be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaGTEST_DEFINE_bool_( 1881be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania break_on_failure, 1891be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania internal::BoolFromGTestEnv("break_on_failure", false), 1901be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania "True iff a failed assertion should be a debugger break-point."); 1911be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 1921be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaGTEST_DEFINE_bool_( 1931be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania catch_exceptions, 19441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot internal::BoolFromGTestEnv("catch_exceptions", true), 1951be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania "True iff " GTEST_NAME_ 1961be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania " should catch exceptions and treat them as test failures."); 1971be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 1981be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaGTEST_DEFINE_string_( 1991be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania color, 2001be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania internal::StringFromGTestEnv("color", "auto"), 2011be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania "Whether to use colors in the output. Valid values: yes, no, " 2021be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania "and auto. 'auto' means to use colors if the output is " 2031be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania "being sent to a terminal and the TERM environment variable " 20441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot "is set to xterm, xterm-color, xterm-256color, linux or cygwin."); 2051be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 2061be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaGTEST_DEFINE_string_( 2071be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania filter, 2081be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania internal::StringFromGTestEnv("filter", kUniversalFilter), 2091be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania "A colon-separated list of glob (not regex) patterns " 2101be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania "for filtering the tests to run, optionally followed by a " 2111be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania "'-' and a : separated list of negative patterns (tests to " 2121be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania "exclude). A test is run if it matches one of the positive " 2131be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania "patterns and does not match any of the negative patterns."); 2141be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 2151be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaGTEST_DEFINE_bool_(list_tests, false, 2161be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania "List all tests without running them."); 2171be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 2181be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaGTEST_DEFINE_string_( 2191be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania output, 2201be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania internal::StringFromGTestEnv("output", ""), 2211be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania "A format (currently must be \"xml\"), optionally followed " 2221be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania "by a colon and an output file name or directory. A directory " 2231be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania "is indicated by a trailing pathname separator. " 2241be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania "Examples: \"xml:filename.xml\", \"xml::directoryname/\". " 2251be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania "If a directory is specified, output files will be created " 2261be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania "within that directory, with file-names based on the test " 2271be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania "executable's name and, if necessary, made unique by adding " 2281be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania "digits."); 2291be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 2301be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaGTEST_DEFINE_bool_( 2311be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania print_time, 23241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot internal::BoolFromGTestEnv("print_time", true), 2331be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania "True iff " GTEST_NAME_ 2341be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania " should display elapsed time in text output."); 2351be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 2361be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaGTEST_DEFINE_int32_( 23741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot random_seed, 23841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot internal::Int32FromGTestEnv("random_seed", 0), 23941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot "Random number seed to use when shuffling test orders. Must be in range " 24041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot "[1, 99999], or 0 to use a seed based on the current time."); 24141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 24241d0579e8de9ef4ff178fc4991043c61a19943f7Brett ChabotGTEST_DEFINE_int32_( 2431be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania repeat, 2441be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania internal::Int32FromGTestEnv("repeat", 1), 2451be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania "How many times to repeat each test. Specify a negative number " 2461be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania "for repeating forever. Useful for shaking out flaky tests."); 2471be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 24841d0579e8de9ef4ff178fc4991043c61a19943f7Brett ChabotGTEST_DEFINE_bool_( 24941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot show_internal_stack_frames, false, 25041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot "True iff " GTEST_NAME_ " should include internal stack frames when " 25141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot "printing test failure stack traces."); 25241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 25341d0579e8de9ef4ff178fc4991043c61a19943f7Brett ChabotGTEST_DEFINE_bool_( 25441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot shuffle, 25541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot internal::BoolFromGTestEnv("shuffle", false), 25641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot "True iff " GTEST_NAME_ 25741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot " should randomize tests' order on every run."); 25841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 2591be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaGTEST_DEFINE_int32_( 2601be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania stack_trace_depth, 26141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot internal::Int32FromGTestEnv("stack_trace_depth", kMaxStackTraceDepth), 2621be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania "The maximum number of stack frames to print when an " 2631be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania "assertion fails. The valid range is 0 through 100, inclusive."); 2641be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 26541d0579e8de9ef4ff178fc4991043c61a19943f7Brett ChabotGTEST_DEFINE_string_( 26641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot stream_result_to, 26741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot internal::StringFromGTestEnv("stream_result_to", ""), 26841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot "This flag specifies the host name and the port number on which to stream " 26941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot "test results. Example: \"localhost:555\". The flag is effective only on " 27041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot "Linux."); 2711be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 2721be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaGTEST_DEFINE_bool_( 2731be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania throw_on_failure, 2741be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania internal::BoolFromGTestEnv("throw_on_failure", false), 2751be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania "When this flag is specified, a failed assertion will throw an exception " 2761be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania "if exceptions are enabled or exit the program with a non-zero code " 2771be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania "otherwise."); 2781be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 2791be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catanianamespace internal { 2801be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 28141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// Generates a random number from [0, range), using a Linear 28241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// Congruential Generator (LCG). Crashes if 'range' is 0 or greater 28341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// than kMaxRange. 28441d0579e8de9ef4ff178fc4991043c61a19943f7Brett ChabotUInt32 Random::Generate(UInt32 range) { 28541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // These constants are the same as are used in glibc's rand(3). 28641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot state_ = (1103515245U*state_ + 12345U) % kMaxRange; 28741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 28841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot GTEST_CHECK_(range > 0) 28941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot << "Cannot generate a number in the range [0, 0)."; 29041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot GTEST_CHECK_(range <= kMaxRange) 29141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot << "Generation of a number in [0, " << range << ") was requested, " 29241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot << "but this can only generate numbers in [0, " << kMaxRange << ")."; 29341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 29441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // Converting via modulus introduces a bit of downward bias, but 29541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // it's simple, and a linear congruential generator isn't too good 29641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // to begin with. 29741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot return state_ % range; 29841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot} 2991be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 3001be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// GTestIsInitialized() returns true iff the user has initialized 3011be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Google Test. Useful for catching the user mistake of not initializing 3021be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Google Test before calling RUN_ALL_TESTS(). 3031be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// 3041be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// A user must call testing::InitGoogleTest() to initialize Google 3051be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Test. g_init_gtest_count is set to the number of times 3061be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// InitGoogleTest() has been called. We don't protect this variable 3071be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// under a mutex as it is only accessed in the main thread. 3081be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniaint g_init_gtest_count = 0; 3091be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniastatic bool GTestIsInitialized() { return g_init_gtest_count != 0; } 3101be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 31141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// Iterates over a vector of TestCases, keeping a running sum of the 3121be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// results of calling a given int-returning method on each. 3131be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Returns the sum. 31441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotstatic int SumOverTestCaseList(const std::vector<TestCase*>& case_list, 3151be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania int (TestCase::*method)() const) { 3161be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania int sum = 0; 31741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot for (size_t i = 0; i < case_list.size(); i++) { 31841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot sum += (case_list[i]->*method)(); 3191be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania } 3201be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania return sum; 3211be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 3221be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 3231be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Returns true iff the test case passed. 3241be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniastatic bool TestCasePassed(const TestCase* test_case) { 3251be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania return test_case->should_run() && test_case->Passed(); 3261be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 3271be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 3281be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Returns true iff the test case failed. 3291be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniastatic bool TestCaseFailed(const TestCase* test_case) { 3301be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania return test_case->should_run() && test_case->Failed(); 3311be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 3321be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 3331be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Returns true iff test_case contains at least one test that should 3341be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// run. 3351be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniastatic bool ShouldRunTestCase(const TestCase* test_case) { 3361be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania return test_case->should_run(); 3371be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 3381be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 3391be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// AssertHelper constructor. 34041d0579e8de9ef4ff178fc4991043c61a19943f7Brett ChabotAssertHelper::AssertHelper(TestPartResult::Type type, 34141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot const char* file, 34241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot int line, 34341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot const char* message) 34441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot : data_(new AssertHelperData(type, file, line, message)) { 34541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot} 34641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 34741d0579e8de9ef4ff178fc4991043c61a19943f7Brett ChabotAssertHelper::~AssertHelper() { 34841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot delete data_; 3491be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 3501be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 3511be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Message assignment, for assertion streaming support. 3521be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniavoid AssertHelper::operator=(const Message& message) const { 3531be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania UnitTest::GetInstance()-> 35441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot AddTestPartResult(data_->type, data_->file, data_->line, 35541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot AppendUserMessage(data_->message, message), 3561be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania UnitTest::GetInstance()->impl() 3571be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania ->CurrentOsStackTraceExceptTop(1) 3581be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // Skips the stack frame for this function itself. 3591be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania ); // NOLINT 3601be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 3611be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 3621be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Mutex for linked pointers. 36341d0579e8de9ef4ff178fc4991043c61a19943f7Brett ChabotGTEST_DEFINE_STATIC_MUTEX_(g_linked_ptr_mutex); 3641be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 3651be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Application pathname gotten in InitGoogleTest. 3661be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaString g_executable_path; 3671be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 3681be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Returns the current application's name, removing directory path if that 3691be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// is present. 3701be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaFilePath GetCurrentExecutableName() { 3711be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania FilePath result; 3721be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 37341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot#if GTEST_OS_WINDOWS 3741be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania result.Set(FilePath(g_executable_path).RemoveExtension("exe")); 3751be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#else 3761be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania result.Set(FilePath(g_executable_path)); 37741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot#endif // GTEST_OS_WINDOWS 3781be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 3791be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania return result.RemoveDirectoryName(); 3801be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 3811be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 3821be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Functions for processing the gtest_output flag. 3831be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 3841be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Returns the output format, or "" for normal printed output. 3851be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaString UnitTestOptions::GetOutputFormat() { 3861be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania const char* const gtest_output_flag = GTEST_FLAG(output).c_str(); 3871be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania if (gtest_output_flag == NULL) return String(""); 3881be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 3891be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania const char* const colon = strchr(gtest_output_flag, ':'); 3901be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania return (colon == NULL) ? 3911be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania String(gtest_output_flag) : 3921be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania String(gtest_output_flag, colon - gtest_output_flag); 3931be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 3941be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 3951be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Returns the name of the requested output file, or the default if none 3961be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// was explicitly specified. 3971be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaString UnitTestOptions::GetAbsolutePathToOutputFile() { 3981be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania const char* const gtest_output_flag = GTEST_FLAG(output).c_str(); 3991be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania if (gtest_output_flag == NULL) 4001be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania return String(""); 4011be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 4021be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania const char* const colon = strchr(gtest_output_flag, ':'); 4031be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania if (colon == NULL) 4041be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania return String(internal::FilePath::ConcatPaths( 4051be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania internal::FilePath( 4061be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania UnitTest::GetInstance()->original_working_dir()), 4071be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania internal::FilePath(kDefaultOutputFile)).ToString() ); 4081be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 4091be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania internal::FilePath output_name(colon + 1); 4101be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania if (!output_name.IsAbsolutePath()) 4111be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // TODO(wan@google.com): on Windows \some\path is not an absolute 4121be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // path (as its meaning depends on the current drive), yet the 4131be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // following logic for turning it into an absolute path is wrong. 4141be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // Fix it. 4151be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania output_name = internal::FilePath::ConcatPaths( 4161be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania internal::FilePath(UnitTest::GetInstance()->original_working_dir()), 4171be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania internal::FilePath(colon + 1)); 4181be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 4191be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania if (!output_name.IsDirectory()) 4201be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania return output_name.ToString(); 4211be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 4221be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania internal::FilePath result(internal::FilePath::GenerateUniqueFileName( 4231be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania output_name, internal::GetCurrentExecutableName(), 4241be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania GetOutputFormat().c_str())); 4251be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania return result.ToString(); 4261be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 4271be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 4281be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Returns true iff the wildcard pattern matches the string. The 4291be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// first ':' or '\0' character in pattern marks the end of it. 4301be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// 4311be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// This recursive algorithm isn't very efficient, but is clear and 4321be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// works well enough for matching test names, which are short. 4331be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniabool UnitTestOptions::PatternMatchesString(const char *pattern, 4341be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania const char *str) { 4351be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania switch (*pattern) { 4361be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania case '\0': 4371be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania case ':': // Either ':' or '\0' marks the end of the pattern. 4381be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania return *str == '\0'; 4391be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania case '?': // Matches any single character. 4401be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania return *str != '\0' && PatternMatchesString(pattern + 1, str + 1); 4411be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania case '*': // Matches any string (possibly empty) of characters. 4421be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania return (*str != '\0' && PatternMatchesString(pattern, str + 1)) || 4431be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania PatternMatchesString(pattern + 1, str); 4441be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania default: // Non-special character. Matches itself. 4451be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania return *pattern == *str && 4461be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania PatternMatchesString(pattern + 1, str + 1); 4471be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania } 4481be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 4491be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 4501be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniabool UnitTestOptions::MatchesFilter(const String& name, const char* filter) { 4511be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania const char *cur_pattern = filter; 4521be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania for (;;) { 4531be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania if (PatternMatchesString(cur_pattern, name.c_str())) { 4541be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania return true; 4551be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania } 4561be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 4571be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // Finds the next pattern in the filter. 4581be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania cur_pattern = strchr(cur_pattern, ':'); 4591be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 4601be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // Returns if no more pattern can be found. 4611be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania if (cur_pattern == NULL) { 4621be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania return false; 4631be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania } 4641be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 4651be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // Skips the pattern separater (the ':' character). 4661be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania cur_pattern++; 4671be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania } 4681be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 4691be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 4701be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// TODO(keithray): move String function implementations to gtest-string.cc. 4711be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 4721be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Returns true iff the user-specified filter matches the test case 4731be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// name and the test name. 4741be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniabool UnitTestOptions::FilterMatchesTest(const String &test_case_name, 4751be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania const String &test_name) { 4761be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania const String& full_name = String::Format("%s.%s", 4771be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania test_case_name.c_str(), 4781be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania test_name.c_str()); 4791be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 4801be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // Split --gtest_filter at '-', if there is one, to separate into 4811be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // positive filter and negative filter portions 4821be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania const char* const p = GTEST_FLAG(filter).c_str(); 4831be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania const char* const dash = strchr(p, '-'); 4841be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania String positive; 4851be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania String negative; 4861be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania if (dash == NULL) { 4871be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania positive = GTEST_FLAG(filter).c_str(); // Whole string is a positive filter 4881be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania negative = String(""); 4891be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania } else { 49041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot positive = String(p, dash - p); // Everything up to the dash 4911be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania negative = String(dash+1); // Everything after the dash 4921be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania if (positive.empty()) { 4931be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // Treat '-test1' as the same as '*-test1' 4941be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania positive = kUniversalFilter; 4951be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania } 4961be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania } 4971be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 4981be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // A filter is a colon-separated list of patterns. It matches a 4991be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // test if any pattern in it matches the test. 5001be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania return (MatchesFilter(full_name, positive.c_str()) && 5011be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania !MatchesFilter(full_name, negative.c_str())); 5021be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 5031be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 50441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot#if GTEST_HAS_SEH 5051be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Returns EXCEPTION_EXECUTE_HANDLER if Google Test should handle the 5061be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// given SEH exception, or EXCEPTION_CONTINUE_SEARCH otherwise. 5071be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// This function is useful as an __except condition. 5081be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniaint UnitTestOptions::GTestShouldProcessSEH(DWORD exception_code) { 50941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // Google Test should handle a SEH exception if: 5101be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // 1. the user wants it to, AND 51141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // 2. this is not a breakpoint exception, AND 51241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // 3. this is not a C++ exception (VC++ implements them via SEH, 51341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // apparently). 51441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // 51541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // SEH exception code for C++ exceptions. 51641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // (see http://support.microsoft.com/kb/185294 for more information). 51741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot const DWORD kCxxExceptionCode = 0xe06d7363; 5181be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 51941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot bool should_handle = true; 5201be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 52141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot if (!GTEST_FLAG(catch_exceptions)) 52241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot should_handle = false; 52341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot else if (exception_code == EXCEPTION_BREAKPOINT) 52441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot should_handle = false; 52541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot else if (exception_code == kCxxExceptionCode) 52641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot should_handle = false; 5271be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 52841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot return should_handle ? EXCEPTION_EXECUTE_HANDLER : EXCEPTION_CONTINUE_SEARCH; 52941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot} 53041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot#endif // GTEST_HAS_SEH 5311be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 53241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot} // namespace internal 5331be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 5341be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// The c'tor sets this object as the test part result reporter used by 5351be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Google Test. The 'result' parameter specifies where to report the 5361be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// results. Intercepts only failures from the current thread. 5371be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaScopedFakeTestPartResultReporter::ScopedFakeTestPartResultReporter( 5381be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania TestPartResultArray* result) 5391be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania : intercept_mode_(INTERCEPT_ONLY_CURRENT_THREAD), 5401be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania result_(result) { 5411be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania Init(); 5421be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 5431be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 5441be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// The c'tor sets this object as the test part result reporter used by 5451be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Google Test. The 'result' parameter specifies where to report the 5461be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// results. 5471be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaScopedFakeTestPartResultReporter::ScopedFakeTestPartResultReporter( 5481be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania InterceptMode intercept_mode, TestPartResultArray* result) 5491be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania : intercept_mode_(intercept_mode), 5501be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania result_(result) { 5511be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania Init(); 5521be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 5531be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 5541be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniavoid ScopedFakeTestPartResultReporter::Init() { 55541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot internal::UnitTestImpl* const impl = internal::GetUnitTestImpl(); 5561be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania if (intercept_mode_ == INTERCEPT_ALL_THREADS) { 5571be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania old_reporter_ = impl->GetGlobalTestPartResultReporter(); 5581be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania impl->SetGlobalTestPartResultReporter(this); 5591be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania } else { 5601be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania old_reporter_ = impl->GetTestPartResultReporterForCurrentThread(); 5611be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania impl->SetTestPartResultReporterForCurrentThread(this); 5621be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania } 5631be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 5641be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 5651be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// The d'tor restores the test part result reporter used by Google Test 5661be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// before. 5671be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaScopedFakeTestPartResultReporter::~ScopedFakeTestPartResultReporter() { 56841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot internal::UnitTestImpl* const impl = internal::GetUnitTestImpl(); 5691be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania if (intercept_mode_ == INTERCEPT_ALL_THREADS) { 5701be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania impl->SetGlobalTestPartResultReporter(old_reporter_); 5711be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania } else { 5721be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania impl->SetTestPartResultReporterForCurrentThread(old_reporter_); 5731be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania } 5741be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 5751be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 5761be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Increments the test part result count and remembers the result. 5771be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// This method is from the TestPartResultReporterInterface interface. 5781be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniavoid ScopedFakeTestPartResultReporter::ReportTestPartResult( 5791be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania const TestPartResult& result) { 5801be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania result_->Append(result); 5811be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 5821be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 5831be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catanianamespace internal { 5841be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 5851be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Returns the type ID of ::testing::Test. We should always call this 5861be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// instead of GetTypeId< ::testing::Test>() to get the type ID of 5871be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// testing::Test. This is to work around a suspected linker bug when 5881be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// using Google Test as a framework on Mac OS X. The bug causes 5891be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// GetTypeId< ::testing::Test>() to return different values depending 5901be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// on whether the call is from the Google Test framework itself or 5911be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// from user test code. GetTestTypeId() is guaranteed to always 5921be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// return the same value, as it always calls GetTypeId<>() from the 5931be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// gtest.cc, which is within the Google Test framework. 5941be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaTypeId GetTestTypeId() { 5951be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania return GetTypeId<Test>(); 5961be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 5971be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 5981be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// The value of GetTestTypeId() as seen from within the Google Test 5991be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// library. This is solely for testing GetTestTypeId(). 6001be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniaextern const TypeId kTestTypeIdInGoogleTest = GetTestTypeId(); 6011be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 6021be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// This predicate-formatter checks that 'results' contains a test part 6031be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// failure of the given type and that the failure message contains the 6041be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// given substring. 6051be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaAssertionResult HasOneFailure(const char* /* results_expr */, 6061be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania const char* /* type_expr */, 6071be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania const char* /* substr_expr */, 6081be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania const TestPartResultArray& results, 60941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot TestPartResult::Type type, 61041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot const string& substr) { 61141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot const String expected(type == TestPartResult::kFatalFailure ? 61241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot "1 fatal failure" : 61341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot "1 non-fatal failure"); 6141be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania Message msg; 6151be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania if (results.size() != 1) { 6161be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania msg << "Expected: " << expected << "\n" 6171be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania << " Actual: " << results.size() << " failures"; 6181be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania for (int i = 0; i < results.size(); i++) { 6191be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania msg << "\n" << results.GetTestPartResult(i); 6201be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania } 62141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot return AssertionFailure() << msg; 6221be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania } 6231be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 6241be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania const TestPartResult& r = results.GetTestPartResult(0); 6251be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania if (r.type() != type) { 62641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot return AssertionFailure() << "Expected: " << expected << "\n" 62741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot << " Actual:\n" 62841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot << r; 6291be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania } 6301be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 63141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot if (strstr(r.message(), substr.c_str()) == NULL) { 63241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot return AssertionFailure() << "Expected: " << expected << " containing \"" 63341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot << substr << "\"\n" 63441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot << " Actual:\n" 63541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot << r; 6361be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania } 6371be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 6381be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania return AssertionSuccess(); 6391be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 6401be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 6411be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// The constructor of SingleFailureChecker remembers where to look up 6421be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// test part results, what type of failure we expect, and what 6431be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// substring the failure message should contain. 6441be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaSingleFailureChecker:: SingleFailureChecker( 6451be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania const TestPartResultArray* results, 64641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot TestPartResult::Type type, 64741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot const string& substr) 6481be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania : results_(results), 6491be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania type_(type), 6501be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania substr_(substr) {} 6511be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 6521be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// The destructor of SingleFailureChecker verifies that the given 6531be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// TestPartResultArray contains exactly one failure that has the given 6541be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// type and contains the given substring. If that's not the case, a 6551be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// non-fatal failure will be generated. 6561be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaSingleFailureChecker::~SingleFailureChecker() { 65741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot EXPECT_PRED_FORMAT3(HasOneFailure, *results_, type_, substr_); 6581be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 6591be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 6601be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaDefaultGlobalTestPartResultReporter::DefaultGlobalTestPartResultReporter( 6611be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania UnitTestImpl* unit_test) : unit_test_(unit_test) {} 6621be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 6631be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniavoid DefaultGlobalTestPartResultReporter::ReportTestPartResult( 6641be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania const TestPartResult& result) { 6651be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania unit_test_->current_test_result()->AddTestPartResult(result); 66641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot unit_test_->listeners()->repeater()->OnTestPartResult(result); 6671be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 6681be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 6691be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaDefaultPerThreadTestPartResultReporter::DefaultPerThreadTestPartResultReporter( 6701be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania UnitTestImpl* unit_test) : unit_test_(unit_test) {} 6711be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 6721be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniavoid DefaultPerThreadTestPartResultReporter::ReportTestPartResult( 6731be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania const TestPartResult& result) { 6741be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania unit_test_->GetGlobalTestPartResultReporter()->ReportTestPartResult(result); 6751be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 6761be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 6771be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Returns the global test part result reporter. 6781be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaTestPartResultReporterInterface* 6791be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaUnitTestImpl::GetGlobalTestPartResultReporter() { 6801be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania internal::MutexLock lock(&global_test_part_result_reporter_mutex_); 6811be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania return global_test_part_result_repoter_; 6821be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 6831be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 6841be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Sets the global test part result reporter. 6851be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniavoid UnitTestImpl::SetGlobalTestPartResultReporter( 6861be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania TestPartResultReporterInterface* reporter) { 6871be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania internal::MutexLock lock(&global_test_part_result_reporter_mutex_); 6881be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania global_test_part_result_repoter_ = reporter; 6891be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 6901be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 6911be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Returns the test part result reporter for the current thread. 6921be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaTestPartResultReporterInterface* 6931be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaUnitTestImpl::GetTestPartResultReporterForCurrentThread() { 6941be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania return per_thread_test_part_result_reporter_.get(); 6951be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 6961be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 6971be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Sets the test part result reporter for the current thread. 6981be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniavoid UnitTestImpl::SetTestPartResultReporterForCurrentThread( 6991be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania TestPartResultReporterInterface* reporter) { 7001be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania per_thread_test_part_result_reporter_.set(reporter); 7011be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 7021be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 7031be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Gets the number of successful test cases. 7041be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniaint UnitTestImpl::successful_test_case_count() const { 70541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot return CountIf(test_cases_, TestCasePassed); 7061be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 7071be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 7081be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Gets the number of failed test cases. 7091be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniaint UnitTestImpl::failed_test_case_count() const { 71041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot return CountIf(test_cases_, TestCaseFailed); 7111be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 7121be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 7131be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Gets the number of all test cases. 7141be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniaint UnitTestImpl::total_test_case_count() const { 71541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot return static_cast<int>(test_cases_.size()); 7161be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 7171be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 7181be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Gets the number of all test cases that contain at least one test 7191be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// that should run. 7201be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniaint UnitTestImpl::test_case_to_run_count() const { 72141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot return CountIf(test_cases_, ShouldRunTestCase); 7221be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 7231be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 7241be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Gets the number of successful tests. 7251be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniaint UnitTestImpl::successful_test_count() const { 7261be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania return SumOverTestCaseList(test_cases_, &TestCase::successful_test_count); 7271be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 7281be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 7291be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Gets the number of failed tests. 7301be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniaint UnitTestImpl::failed_test_count() const { 7311be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania return SumOverTestCaseList(test_cases_, &TestCase::failed_test_count); 7321be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 7331be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 7341be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Gets the number of disabled tests. 7351be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniaint UnitTestImpl::disabled_test_count() const { 7361be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania return SumOverTestCaseList(test_cases_, &TestCase::disabled_test_count); 7371be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 7381be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 7391be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Gets the number of all tests. 7401be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniaint UnitTestImpl::total_test_count() const { 7411be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania return SumOverTestCaseList(test_cases_, &TestCase::total_test_count); 7421be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 7431be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 7441be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Gets the number of tests that should run. 7451be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniaint UnitTestImpl::test_to_run_count() const { 7461be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania return SumOverTestCaseList(test_cases_, &TestCase::test_to_run_count); 7471be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 7481be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 7491be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Returns the current OS stack trace as a String. 7501be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// 7511be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// The maximum number of stack frames to be included is specified by 7521be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// the gtest_stack_trace_depth flag. The skip_count parameter 7531be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// specifies the number of top frames to be skipped, which doesn't 7541be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// count against the number of frames to be included. 7551be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// 7561be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// For example, if Foo() calls Bar(), which in turn calls 7571be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// CurrentOsStackTraceExceptTop(1), Foo() will be included in the 7581be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// trace but Bar() and CurrentOsStackTraceExceptTop() won't. 7591be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaString UnitTestImpl::CurrentOsStackTraceExceptTop(int skip_count) { 7601be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania (void)skip_count; 7611be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania return String(""); 7621be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 7631be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 76441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// Returns the current time in milliseconds. 76541d0579e8de9ef4ff178fc4991043c61a19943f7Brett ChabotTimeInMillis GetTimeInMillis() { 76641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot#if GTEST_OS_WINDOWS_MOBILE || defined(__BORLANDC__) 76741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // Difference between 1970-01-01 and 1601-01-01 in milliseconds. 7681be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // http://analogous.blogspot.com/2005/04/epoch.html 76941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot const TimeInMillis kJavaEpochToWinFileTimeDelta = 77041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot static_cast<TimeInMillis>(116444736UL) * 100000UL; 7711be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania const DWORD kTenthMicrosInMilliSecond = 10000; 7721be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 7731be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania SYSTEMTIME now_systime; 7741be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania FILETIME now_filetime; 7751be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania ULARGE_INTEGER now_int64; 7761be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // TODO(kenton@google.com): Shouldn't this just use 7771be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // GetSystemTimeAsFileTime()? 7781be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania GetSystemTime(&now_systime); 7791be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania if (SystemTimeToFileTime(&now_systime, &now_filetime)) { 7801be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania now_int64.LowPart = now_filetime.dwLowDateTime; 7811be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania now_int64.HighPart = now_filetime.dwHighDateTime; 7821be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania now_int64.QuadPart = (now_int64.QuadPart / kTenthMicrosInMilliSecond) - 7831be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania kJavaEpochToWinFileTimeDelta; 7841be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania return now_int64.QuadPart; 7851be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania } 7861be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania return 0; 7871be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#elif GTEST_OS_WINDOWS && !GTEST_HAS_GETTIMEOFDAY_ 7881be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania __timeb64 now; 78941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 79041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot# ifdef _MSC_VER 79141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 7921be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // MSVC 8 deprecates _ftime64(), so we want to suppress warning 4996 7931be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // (deprecated function) there. 7941be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // TODO(kenton@google.com): Use GetTickCount()? Or use 7951be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // SystemTimeToFileTime() 79641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot# pragma warning(push) // Saves the current warning state. 79741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot# pragma warning(disable:4996) // Temporarily disables warning 4996. 7981be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania _ftime64(&now); 79941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot# pragma warning(pop) // Restores the warning state. 80041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot# else 80141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 8021be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania _ftime64(&now); 80341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 80441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot# endif // _MSC_VER 80541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 8061be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania return static_cast<TimeInMillis>(now.time) * 1000 + now.millitm; 8071be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#elif GTEST_HAS_GETTIMEOFDAY_ 8081be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania struct timeval now; 8091be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania gettimeofday(&now, NULL); 8101be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania return static_cast<TimeInMillis>(now.tv_sec) * 1000 + now.tv_usec / 1000; 8111be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#else 81241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot# error "Don't know how to get the current time on your system." 8131be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#endif 8141be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 8151be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 8161be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Utilities 8171be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 8181be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// class String 8191be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 8201be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Returns the input enclosed in double quotes if it's not NULL; 8211be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// otherwise returns "(null)". For example, "\"Hello\"" is returned 8221be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// for input "Hello". 8231be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// 8241be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// This is useful for printing a C string in the syntax of a literal. 8251be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// 8261be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Known issue: escape sequences are not handled yet. 8271be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaString String::ShowCStringQuoted(const char* c_str) { 8281be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania return c_str ? String::Format("\"%s\"", c_str) : String("(null)"); 8291be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 8301be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 8311be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Copies at most length characters from str into a newly-allocated 8321be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// piece of memory of size length+1. The memory is allocated with new[]. 8331be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// A terminating null byte is written to the memory, and a pointer to it 8341be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// is returned. If str is NULL, NULL is returned. 8351be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniastatic char* CloneString(const char* str, size_t length) { 8361be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania if (str == NULL) { 8371be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania return NULL; 8381be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania } else { 8391be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania char* const clone = new char[length + 1]; 84041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot posix::StrNCpy(clone, str, length); 8411be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania clone[length] = '\0'; 8421be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania return clone; 8431be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania } 8441be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 8451be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 8461be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Clones a 0-terminated C string, allocating memory using new. The 8471be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// caller is responsible for deleting[] the return value. Returns the 8481be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// cloned string, or NULL if the input is NULL. 8491be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniaconst char * String::CloneCString(const char* c_str) { 8501be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania return (c_str == NULL) ? 8511be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania NULL : CloneString(c_str, strlen(c_str)); 8521be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 8531be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 85441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot#if GTEST_OS_WINDOWS_MOBILE 8551be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Creates a UTF-16 wide string from the given ANSI string, allocating 8561be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// memory using new. The caller is responsible for deleting the return 8571be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// value using delete[]. Returns the wide string, or NULL if the 8581be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// input is NULL. 8591be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaLPCWSTR String::AnsiToUtf16(const char* ansi) { 8601be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania if (!ansi) return NULL; 8611be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania const int length = strlen(ansi); 8621be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania const int unicode_length = 8631be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania MultiByteToWideChar(CP_ACP, 0, ansi, length, 8641be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania NULL, 0); 8651be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania WCHAR* unicode = new WCHAR[unicode_length + 1]; 8661be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania MultiByteToWideChar(CP_ACP, 0, ansi, length, 8671be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania unicode, unicode_length); 8681be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania unicode[unicode_length] = 0; 8691be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania return unicode; 8701be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 8711be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 8721be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Creates an ANSI string from the given wide string, allocating 8731be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// memory using new. The caller is responsible for deleting the return 8741be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// value using delete[]. Returns the ANSI string, or NULL if the 8751be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// input is NULL. 8761be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniaconst char* String::Utf16ToAnsi(LPCWSTR utf16_str) { 8771be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania if (!utf16_str) return NULL; 8781be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania const int ansi_length = 8791be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania WideCharToMultiByte(CP_ACP, 0, utf16_str, -1, 8801be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania NULL, 0, NULL, NULL); 8811be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania char* ansi = new char[ansi_length + 1]; 8821be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania WideCharToMultiByte(CP_ACP, 0, utf16_str, -1, 8831be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania ansi, ansi_length, NULL, NULL); 8841be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania ansi[ansi_length] = 0; 8851be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania return ansi; 8861be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 8871be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 88841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot#endif // GTEST_OS_WINDOWS_MOBILE 8891be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 8901be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Compares two C strings. Returns true iff they have the same content. 8911be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// 8921be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Unlike strcmp(), this function can handle NULL argument(s). A NULL 8931be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// C string is considered different to any non-NULL C string, 8941be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// including the empty string. 8951be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniabool String::CStringEquals(const char * lhs, const char * rhs) { 8961be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania if ( lhs == NULL ) return rhs == NULL; 8971be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 8981be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania if ( rhs == NULL ) return false; 8991be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 9001be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania return strcmp(lhs, rhs) == 0; 9011be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 9021be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 9031be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#if GTEST_HAS_STD_WSTRING || GTEST_HAS_GLOBAL_WSTRING 9041be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 9051be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Converts an array of wide chars to a narrow string using the UTF-8 9061be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// encoding, and streams the result to the given Message object. 90741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotstatic void StreamWideCharsToMessage(const wchar_t* wstr, size_t length, 9081be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania Message* msg) { 9091be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // TODO(wan): consider allowing a testing::String object to 9101be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // contain '\0'. This will make it behave more like std::string, 9111be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // and will allow ToUtf8String() to return the correct encoding 9121be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // for '\0' s.t. we can get rid of the conditional here (and in 9131be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // several other places). 91441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot for (size_t i = 0; i != length; ) { // NOLINT 9151be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania if (wstr[i] != L'\0') { 91641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot *msg << WideStringToUtf8(wstr + i, static_cast<int>(length - i)); 91741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot while (i != length && wstr[i] != L'\0') 9181be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania i++; 9191be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania } else { 9201be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania *msg << '\0'; 9211be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania i++; 9221be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania } 9231be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania } 9241be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 9251be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 9261be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#endif // GTEST_HAS_STD_WSTRING || GTEST_HAS_GLOBAL_WSTRING 9271be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 9281be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} // namespace internal 9291be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 9301be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#if GTEST_HAS_STD_WSTRING 9311be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Converts the given wide string to a narrow string using the UTF-8 9321be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// encoding, and streams the result to this Message object. 9331be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaMessage& Message::operator <<(const ::std::wstring& wstr) { 9341be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania internal::StreamWideCharsToMessage(wstr.c_str(), wstr.length(), this); 9351be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania return *this; 9361be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 9371be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#endif // GTEST_HAS_STD_WSTRING 9381be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 9391be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#if GTEST_HAS_GLOBAL_WSTRING 9401be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Converts the given wide string to a narrow string using the UTF-8 9411be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// encoding, and streams the result to this Message object. 9421be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaMessage& Message::operator <<(const ::wstring& wstr) { 9431be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania internal::StreamWideCharsToMessage(wstr.c_str(), wstr.length(), this); 9441be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania return *this; 9451be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 9461be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#endif // GTEST_HAS_GLOBAL_WSTRING 9471be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 94841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// AssertionResult constructors. 94941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// Used in EXPECT_TRUE/FALSE(assertion_result). 95041d0579e8de9ef4ff178fc4991043c61a19943f7Brett ChabotAssertionResult::AssertionResult(const AssertionResult& other) 95141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot : success_(other.success_), 95241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot message_(other.message_.get() != NULL ? 95341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot new ::std::string(*other.message_) : 95441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot static_cast< ::std::string*>(NULL)) { 9551be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 9561be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 95741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// Returns the assertion's negation. Used with EXPECT/ASSERT_FALSE. 95841d0579e8de9ef4ff178fc4991043c61a19943f7Brett ChabotAssertionResult AssertionResult::operator!() const { 95941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot AssertionResult negation(!success_); 96041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot if (message_.get() != NULL) 96141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot negation << *message_; 96241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot return negation; 9631be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 9641be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 9651be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Makes a successful assertion result. 9661be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaAssertionResult AssertionSuccess() { 96741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot return AssertionResult(true); 9681be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 9691be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 97041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// Makes a failed assertion result. 97141d0579e8de9ef4ff178fc4991043c61a19943f7Brett ChabotAssertionResult AssertionFailure() { 97241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot return AssertionResult(false); 97341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot} 9741be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 9751be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Makes a failed assertion result with the given failure message. 97641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// Deprecated; use AssertionFailure() << message. 9771be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaAssertionResult AssertionFailure(const Message& message) { 97841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot return AssertionFailure() << message; 9791be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 9801be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 9811be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catanianamespace internal { 9821be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 9831be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Constructs and returns the message for an equality assertion 9841be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// (e.g. ASSERT_EQ, EXPECT_STREQ, etc) failure. 9851be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// 9861be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// The first four parameters are the expressions used in the assertion 9871be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// and their values, as strings. For example, for ASSERT_EQ(foo, bar) 9881be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// where foo is 5 and bar is 6, we have: 9891be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// 9901be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// expected_expression: "foo" 9911be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// actual_expression: "bar" 9921be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// expected_value: "5" 9931be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// actual_value: "6" 9941be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// 9951be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// The ignoring_case parameter is true iff the assertion is a 9961be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// *_STRCASEEQ*. When it's true, the string " (ignoring case)" will 9971be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// be inserted into the message. 9981be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaAssertionResult EqFailure(const char* expected_expression, 9991be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania const char* actual_expression, 10001be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania const String& expected_value, 10011be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania const String& actual_value, 10021be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania bool ignoring_case) { 10031be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania Message msg; 10041be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania msg << "Value of: " << actual_expression; 10051be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania if (actual_value != actual_expression) { 10061be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania msg << "\n Actual: " << actual_value; 10071be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania } 10081be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 10091be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania msg << "\nExpected: " << expected_expression; 10101be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania if (ignoring_case) { 10111be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania msg << " (ignoring case)"; 10121be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania } 10131be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania if (expected_value != expected_expression) { 10141be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania msg << "\nWhich is: " << expected_value; 10151be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania } 10161be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 101741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot return AssertionFailure() << msg; 10181be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 10191be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 102041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// Constructs a failure message for Boolean assertions such as EXPECT_TRUE. 102141d0579e8de9ef4ff178fc4991043c61a19943f7Brett ChabotString GetBoolAssertionFailureMessage(const AssertionResult& assertion_result, 102241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot const char* expression_text, 102341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot const char* actual_predicate_value, 102441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot const char* expected_predicate_value) { 102541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot const char* actual_message = assertion_result.message(); 102641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot Message msg; 102741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot msg << "Value of: " << expression_text 102841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot << "\n Actual: " << actual_predicate_value; 102941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot if (actual_message[0] != '\0') 103041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot msg << " (" << actual_message << ")"; 103141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot msg << "\nExpected: " << expected_predicate_value; 103241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot return msg.GetString(); 103341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot} 10341be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 10351be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Helper function for implementing ASSERT_NEAR. 10361be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaAssertionResult DoubleNearPredFormat(const char* expr1, 10371be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania const char* expr2, 10381be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania const char* abs_error_expr, 10391be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania double val1, 10401be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania double val2, 10411be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania double abs_error) { 10421be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania const double diff = fabs(val1 - val2); 10431be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania if (diff <= abs_error) return AssertionSuccess(); 10441be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 10451be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // TODO(wan): do not print the value of an expression if it's 10461be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // already a literal. 104741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot return AssertionFailure() 104841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot << "The difference between " << expr1 << " and " << expr2 10491be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania << " is " << diff << ", which exceeds " << abs_error_expr << ", where\n" 10501be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania << expr1 << " evaluates to " << val1 << ",\n" 10511be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania << expr2 << " evaluates to " << val2 << ", and\n" 10521be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania << abs_error_expr << " evaluates to " << abs_error << "."; 10531be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 10541be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 10551be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 10561be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Helper template for implementing FloatLE() and DoubleLE(). 10571be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniatemplate <typename RawType> 10581be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaAssertionResult FloatingPointLE(const char* expr1, 10591be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania const char* expr2, 10601be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania RawType val1, 10611be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania RawType val2) { 10621be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // Returns success if val1 is less than val2, 10631be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania if (val1 < val2) { 10641be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania return AssertionSuccess(); 10651be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania } 10661be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 10671be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // or if val1 is almost equal to val2. 10681be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania const FloatingPoint<RawType> lhs(val1), rhs(val2); 10691be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania if (lhs.AlmostEquals(rhs)) { 10701be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania return AssertionSuccess(); 10711be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania } 10721be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 10731be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // Note that the above two checks will both fail if either val1 or 10741be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // val2 is NaN, as the IEEE floating-point standard requires that 10751be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // any predicate involving a NaN must return false. 10761be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 107741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot ::std::stringstream val1_ss; 10781be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania val1_ss << std::setprecision(std::numeric_limits<RawType>::digits10 + 2) 10791be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania << val1; 10801be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 108141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot ::std::stringstream val2_ss; 10821be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania val2_ss << std::setprecision(std::numeric_limits<RawType>::digits10 + 2) 10831be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania << val2; 10841be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 108541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot return AssertionFailure() 108641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot << "Expected: (" << expr1 << ") <= (" << expr2 << ")\n" 108741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot << " Actual: " << StringStreamToString(&val1_ss) << " vs " 108841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot << StringStreamToString(&val2_ss); 10891be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 10901be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 10911be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} // namespace internal 10921be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 10931be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Asserts that val1 is less than, or almost equal to, val2. Fails 10941be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// otherwise. In particular, it fails if either val1 or val2 is NaN. 10951be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaAssertionResult FloatLE(const char* expr1, const char* expr2, 10961be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania float val1, float val2) { 10971be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania return internal::FloatingPointLE<float>(expr1, expr2, val1, val2); 10981be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 10991be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 11001be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Asserts that val1 is less than, or almost equal to, val2. Fails 11011be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// otherwise. In particular, it fails if either val1 or val2 is NaN. 11021be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaAssertionResult DoubleLE(const char* expr1, const char* expr2, 11031be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania double val1, double val2) { 11041be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania return internal::FloatingPointLE<double>(expr1, expr2, val1, val2); 11051be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 11061be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 11071be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catanianamespace internal { 11081be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 11091be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// The helper function for {ASSERT|EXPECT}_EQ with int or enum 11101be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// arguments. 11111be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaAssertionResult CmpHelperEQ(const char* expected_expression, 11121be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania const char* actual_expression, 11131be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania BiggestInt expected, 11141be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania BiggestInt actual) { 11151be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania if (expected == actual) { 11161be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania return AssertionSuccess(); 11171be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania } 11181be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 11191be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania return EqFailure(expected_expression, 11201be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania actual_expression, 11211be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania FormatForComparisonFailureMessage(expected, actual), 11221be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania FormatForComparisonFailureMessage(actual, expected), 11231be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania false); 11241be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 11251be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 11261be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// A macro for implementing the helper functions needed to implement 11271be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// ASSERT_?? and EXPECT_?? with integer or enum arguments. It is here 11281be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// just to avoid copy-and-paste of similar code. 11291be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#define GTEST_IMPL_CMP_HELPER_(op_name, op)\ 11301be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaAssertionResult CmpHelper##op_name(const char* expr1, const char* expr2, \ 11311be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania BiggestInt val1, BiggestInt val2) {\ 11321be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania if (val1 op val2) {\ 11331be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania return AssertionSuccess();\ 11341be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania } else {\ 113541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot return AssertionFailure() \ 113641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot << "Expected: (" << expr1 << ") " #op " (" << expr2\ 11371be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania << "), actual: " << FormatForComparisonFailureMessage(val1, val2)\ 11381be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania << " vs " << FormatForComparisonFailureMessage(val2, val1);\ 11391be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania }\ 11401be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 11411be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 11421be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Implements the helper function for {ASSERT|EXPECT}_NE with int or 11431be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// enum arguments. 11441be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaGTEST_IMPL_CMP_HELPER_(NE, !=) 11451be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Implements the helper function for {ASSERT|EXPECT}_LE with int or 11461be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// enum arguments. 11471be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaGTEST_IMPL_CMP_HELPER_(LE, <=) 11481be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Implements the helper function for {ASSERT|EXPECT}_LT with int or 11491be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// enum arguments. 11501be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaGTEST_IMPL_CMP_HELPER_(LT, < ) 11511be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Implements the helper function for {ASSERT|EXPECT}_GE with int or 11521be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// enum arguments. 11531be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaGTEST_IMPL_CMP_HELPER_(GE, >=) 11541be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Implements the helper function for {ASSERT|EXPECT}_GT with int or 11551be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// enum arguments. 11561be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaGTEST_IMPL_CMP_HELPER_(GT, > ) 11571be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 11581be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#undef GTEST_IMPL_CMP_HELPER_ 11591be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 11601be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// The helper function for {ASSERT|EXPECT}_STREQ. 11611be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaAssertionResult CmpHelperSTREQ(const char* expected_expression, 11621be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania const char* actual_expression, 11631be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania const char* expected, 11641be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania const char* actual) { 11651be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania if (String::CStringEquals(expected, actual)) { 11661be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania return AssertionSuccess(); 11671be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania } 11681be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 11691be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania return EqFailure(expected_expression, 11701be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania actual_expression, 11711be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania String::ShowCStringQuoted(expected), 11721be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania String::ShowCStringQuoted(actual), 11731be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania false); 11741be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 11751be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 11761be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// The helper function for {ASSERT|EXPECT}_STRCASEEQ. 11771be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaAssertionResult CmpHelperSTRCASEEQ(const char* expected_expression, 11781be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania const char* actual_expression, 11791be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania const char* expected, 11801be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania const char* actual) { 11811be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania if (String::CaseInsensitiveCStringEquals(expected, actual)) { 11821be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania return AssertionSuccess(); 11831be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania } 11841be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 11851be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania return EqFailure(expected_expression, 11861be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania actual_expression, 11871be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania String::ShowCStringQuoted(expected), 11881be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania String::ShowCStringQuoted(actual), 11891be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania true); 11901be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 11911be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 11921be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// The helper function for {ASSERT|EXPECT}_STRNE. 11931be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaAssertionResult CmpHelperSTRNE(const char* s1_expression, 11941be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania const char* s2_expression, 11951be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania const char* s1, 11961be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania const char* s2) { 11971be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania if (!String::CStringEquals(s1, s2)) { 11981be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania return AssertionSuccess(); 11991be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania } else { 120041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot return AssertionFailure() << "Expected: (" << s1_expression << ") != (" 120141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot << s2_expression << "), actual: \"" 120241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot << s1 << "\" vs \"" << s2 << "\""; 12031be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania } 12041be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 12051be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 12061be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// The helper function for {ASSERT|EXPECT}_STRCASENE. 12071be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaAssertionResult CmpHelperSTRCASENE(const char* s1_expression, 12081be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania const char* s2_expression, 12091be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania const char* s1, 12101be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania const char* s2) { 12111be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania if (!String::CaseInsensitiveCStringEquals(s1, s2)) { 12121be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania return AssertionSuccess(); 12131be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania } else { 121441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot return AssertionFailure() 121541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot << "Expected: (" << s1_expression << ") != (" 12161be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania << s2_expression << ") (ignoring case), actual: \"" 12171be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania << s1 << "\" vs \"" << s2 << "\""; 12181be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania } 12191be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 12201be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 12211be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} // namespace internal 12221be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 12231be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catanianamespace { 12241be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 12251be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Helper functions for implementing IsSubString() and IsNotSubstring(). 12261be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 12271be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// This group of overloaded functions return true iff needle is a 12281be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// substring of haystack. NULL is considered a substring of itself 12291be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// only. 12301be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 12311be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniabool IsSubstringPred(const char* needle, const char* haystack) { 12321be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania if (needle == NULL || haystack == NULL) 12331be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania return needle == haystack; 12341be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 12351be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania return strstr(haystack, needle) != NULL; 12361be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 12371be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 12381be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniabool IsSubstringPred(const wchar_t* needle, const wchar_t* haystack) { 12391be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania if (needle == NULL || haystack == NULL) 12401be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania return needle == haystack; 12411be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 12421be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania return wcsstr(haystack, needle) != NULL; 12431be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 12441be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 12451be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// StringType here can be either ::std::string or ::std::wstring. 12461be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniatemplate <typename StringType> 12471be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniabool IsSubstringPred(const StringType& needle, 12481be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania const StringType& haystack) { 12491be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania return haystack.find(needle) != StringType::npos; 12501be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 12511be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 12521be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// This function implements either IsSubstring() or IsNotSubstring(), 12531be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// depending on the value of the expected_to_be_substring parameter. 12541be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// StringType here can be const char*, const wchar_t*, ::std::string, 12551be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// or ::std::wstring. 12561be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniatemplate <typename StringType> 12571be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaAssertionResult IsSubstringImpl( 12581be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania bool expected_to_be_substring, 12591be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania const char* needle_expr, const char* haystack_expr, 12601be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania const StringType& needle, const StringType& haystack) { 12611be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania if (IsSubstringPred(needle, haystack) == expected_to_be_substring) 12621be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania return AssertionSuccess(); 12631be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 12641be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania const bool is_wide_string = sizeof(needle[0]) > 1; 12651be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania const char* const begin_string_quote = is_wide_string ? "L\"" : "\""; 126641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot return AssertionFailure() 12671be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania << "Value of: " << needle_expr << "\n" 12681be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania << " Actual: " << begin_string_quote << needle << "\"\n" 12691be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania << "Expected: " << (expected_to_be_substring ? "" : "not ") 12701be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania << "a substring of " << haystack_expr << "\n" 127141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot << "Which is: " << begin_string_quote << haystack << "\""; 12721be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 12731be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 12741be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} // namespace 12751be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 12761be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// IsSubstring() and IsNotSubstring() check whether needle is a 12771be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// substring of haystack (NULL is considered a substring of itself 12781be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// only), and return an appropriate error message when they fail. 12791be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 12801be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaAssertionResult IsSubstring( 12811be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania const char* needle_expr, const char* haystack_expr, 12821be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania const char* needle, const char* haystack) { 12831be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania return IsSubstringImpl(true, needle_expr, haystack_expr, needle, haystack); 12841be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 12851be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 12861be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaAssertionResult IsSubstring( 12871be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania const char* needle_expr, const char* haystack_expr, 12881be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania const wchar_t* needle, const wchar_t* haystack) { 12891be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania return IsSubstringImpl(true, needle_expr, haystack_expr, needle, haystack); 12901be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 12911be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 12921be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaAssertionResult IsNotSubstring( 12931be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania const char* needle_expr, const char* haystack_expr, 12941be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania const char* needle, const char* haystack) { 12951be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania return IsSubstringImpl(false, needle_expr, haystack_expr, needle, haystack); 12961be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 12971be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 12981be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaAssertionResult IsNotSubstring( 12991be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania const char* needle_expr, const char* haystack_expr, 13001be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania const wchar_t* needle, const wchar_t* haystack) { 13011be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania return IsSubstringImpl(false, needle_expr, haystack_expr, needle, haystack); 13021be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 13031be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 13041be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaAssertionResult IsSubstring( 13051be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania const char* needle_expr, const char* haystack_expr, 13061be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania const ::std::string& needle, const ::std::string& haystack) { 13071be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania return IsSubstringImpl(true, needle_expr, haystack_expr, needle, haystack); 13081be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 13091be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 13101be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaAssertionResult IsNotSubstring( 13111be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania const char* needle_expr, const char* haystack_expr, 13121be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania const ::std::string& needle, const ::std::string& haystack) { 13131be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania return IsSubstringImpl(false, needle_expr, haystack_expr, needle, haystack); 13141be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 13151be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 13161be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#if GTEST_HAS_STD_WSTRING 13171be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaAssertionResult IsSubstring( 13181be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania const char* needle_expr, const char* haystack_expr, 13191be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania const ::std::wstring& needle, const ::std::wstring& haystack) { 13201be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania return IsSubstringImpl(true, needle_expr, haystack_expr, needle, haystack); 13211be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 13221be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 13231be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaAssertionResult IsNotSubstring( 13241be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania const char* needle_expr, const char* haystack_expr, 13251be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania const ::std::wstring& needle, const ::std::wstring& haystack) { 13261be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania return IsSubstringImpl(false, needle_expr, haystack_expr, needle, haystack); 13271be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 13281be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#endif // GTEST_HAS_STD_WSTRING 13291be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 13301be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catanianamespace internal { 13311be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 13321be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#if GTEST_OS_WINDOWS 13331be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 13341be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catanianamespace { 13351be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 13361be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Helper function for IsHRESULT{SuccessFailure} predicates 13371be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaAssertionResult HRESULTFailureHelper(const char* expr, 13381be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania const char* expected, 13391be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania long hr) { // NOLINT 134041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot# if GTEST_OS_WINDOWS_MOBILE 134141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 13421be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // Windows CE doesn't support FormatMessage. 13431be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania const char error_text[] = ""; 134441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 134541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot# else 134641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 13471be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // Looks up the human-readable system message for the HRESULT code 13481be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // and since we're not passing any params to FormatMessage, we don't 13491be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // want inserts expanded. 13501be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania const DWORD kFlags = FORMAT_MESSAGE_FROM_SYSTEM | 13511be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania FORMAT_MESSAGE_IGNORE_INSERTS; 13521be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania const DWORD kBufSize = 4096; // String::Format can't exceed this length. 13531be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // Gets the system's human readable message string for this HRESULT. 13541be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania char error_text[kBufSize] = { '\0' }; 13551be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania DWORD message_length = ::FormatMessageA(kFlags, 13561be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 0, // no source, we're asking system 13571be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania hr, // the error 13581be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 0, // no line width restrictions 13591be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania error_text, // output buffer 13601be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania kBufSize, // buf size 13611be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania NULL); // no arguments for inserts 13621be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // Trims tailing white space (FormatMessage leaves a trailing cr-lf) 136341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot for (; message_length && IsSpace(error_text[message_length - 1]); 13641be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania --message_length) { 13651be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania error_text[message_length - 1] = '\0'; 13661be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania } 136741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 136841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot# endif // GTEST_OS_WINDOWS_MOBILE 13691be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 13701be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania const String error_hex(String::Format("0x%08X ", hr)); 137141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot return ::testing::AssertionFailure() 137241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot << "Expected: " << expr << " " << expected << ".\n" 13731be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania << " Actual: " << error_hex << error_text << "\n"; 13741be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 13751be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 13761be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} // namespace 13771be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 13781be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaAssertionResult IsHRESULTSuccess(const char* expr, long hr) { // NOLINT 13791be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania if (SUCCEEDED(hr)) { 13801be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania return AssertionSuccess(); 13811be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania } 13821be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania return HRESULTFailureHelper(expr, "succeeds", hr); 13831be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 13841be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 13851be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaAssertionResult IsHRESULTFailure(const char* expr, long hr) { // NOLINT 13861be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania if (FAILED(hr)) { 13871be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania return AssertionSuccess(); 13881be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania } 13891be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania return HRESULTFailureHelper(expr, "fails", hr); 13901be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 13911be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 13921be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#endif // GTEST_OS_WINDOWS 13931be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 13941be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Utility functions for encoding Unicode text (wide strings) in 13951be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// UTF-8. 13961be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 13971be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// A Unicode code-point can have upto 21 bits, and is encoded in UTF-8 13981be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// like this: 13991be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// 14001be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Code-point length Encoding 14011be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// 0 - 7 bits 0xxxxxxx 14021be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// 8 - 11 bits 110xxxxx 10xxxxxx 14031be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// 12 - 16 bits 1110xxxx 10xxxxxx 10xxxxxx 14041be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// 17 - 21 bits 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx 14051be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 14061be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// The maximum code-point a one-byte UTF-8 sequence can represent. 14071be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniaconst UInt32 kMaxCodePoint1 = (static_cast<UInt32>(1) << 7) - 1; 14081be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 14091be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// The maximum code-point a two-byte UTF-8 sequence can represent. 14101be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniaconst UInt32 kMaxCodePoint2 = (static_cast<UInt32>(1) << (5 + 6)) - 1; 14111be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 14121be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// The maximum code-point a three-byte UTF-8 sequence can represent. 14131be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniaconst UInt32 kMaxCodePoint3 = (static_cast<UInt32>(1) << (4 + 2*6)) - 1; 14141be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 14151be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// The maximum code-point a four-byte UTF-8 sequence can represent. 14161be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniaconst UInt32 kMaxCodePoint4 = (static_cast<UInt32>(1) << (3 + 3*6)) - 1; 14171be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 14181be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Chops off the n lowest bits from a bit pattern. Returns the n 14191be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// lowest bits. As a side effect, the original bit pattern will be 14201be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// shifted to the right by n bits. 14211be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniainline UInt32 ChopLowBits(UInt32* bits, int n) { 14221be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania const UInt32 low_bits = *bits & ((static_cast<UInt32>(1) << n) - 1); 14231be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania *bits >>= n; 14241be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania return low_bits; 14251be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 14261be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 14271be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Converts a Unicode code point to a narrow string in UTF-8 encoding. 14281be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// code_point parameter is of type UInt32 because wchar_t may not be 14291be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// wide enough to contain a code point. 14301be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// The output buffer str must containt at least 32 characters. 14311be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// The function returns the address of the output buffer. 14321be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// If the code_point is not a valid Unicode code point 14331be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// (i.e. outside of Unicode range U+0 to U+10FFFF) it will be output 14341be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// as '(Invalid Unicode 0xXXXXXXXX)'. 14351be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniachar* CodePointToUtf8(UInt32 code_point, char* str) { 14361be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania if (code_point <= kMaxCodePoint1) { 14371be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania str[1] = '\0'; 14381be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania str[0] = static_cast<char>(code_point); // 0xxxxxxx 14391be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania } else if (code_point <= kMaxCodePoint2) { 14401be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania str[2] = '\0'; 14411be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania str[1] = static_cast<char>(0x80 | ChopLowBits(&code_point, 6)); // 10xxxxxx 14421be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania str[0] = static_cast<char>(0xC0 | code_point); // 110xxxxx 14431be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania } else if (code_point <= kMaxCodePoint3) { 14441be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania str[3] = '\0'; 14451be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania str[2] = static_cast<char>(0x80 | ChopLowBits(&code_point, 6)); // 10xxxxxx 14461be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania str[1] = static_cast<char>(0x80 | ChopLowBits(&code_point, 6)); // 10xxxxxx 14471be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania str[0] = static_cast<char>(0xE0 | code_point); // 1110xxxx 14481be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania } else if (code_point <= kMaxCodePoint4) { 14491be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania str[4] = '\0'; 14501be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania str[3] = static_cast<char>(0x80 | ChopLowBits(&code_point, 6)); // 10xxxxxx 14511be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania str[2] = static_cast<char>(0x80 | ChopLowBits(&code_point, 6)); // 10xxxxxx 14521be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania str[1] = static_cast<char>(0x80 | ChopLowBits(&code_point, 6)); // 10xxxxxx 14531be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania str[0] = static_cast<char>(0xF0 | code_point); // 11110xxx 14541be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania } else { 14551be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // The longest string String::Format can produce when invoked 14561be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // with these parameters is 28 character long (not including 14571be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // the terminating nul character). We are asking for 32 character 14581be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // buffer just in case. This is also enough for strncpy to 14591be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // null-terminate the destination string. 146041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot posix::StrNCpy( 146141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot str, String::Format("(Invalid Unicode 0x%X)", code_point).c_str(), 32); 14621be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania str[31] = '\0'; // Makes sure no change in the format to strncpy leaves 14631be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // the result unterminated. 14641be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania } 14651be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania return str; 14661be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 14671be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 14681be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// The following two functions only make sense if the the system 14691be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// uses UTF-16 for wide string encoding. All supported systems 14701be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// with 16 bit wchar_t (Windows, Cygwin, Symbian OS) do use UTF-16. 14711be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 14721be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Determines if the arguments constitute UTF-16 surrogate pair 14731be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// and thus should be combined into a single Unicode code point 14741be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// using CreateCodePointFromUtf16SurrogatePair. 14751be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniainline bool IsUtf16SurrogatePair(wchar_t first, wchar_t second) { 14761be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania return sizeof(wchar_t) == 2 && 14771be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania (first & 0xFC00) == 0xD800 && (second & 0xFC00) == 0xDC00; 14781be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 14791be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 14801be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Creates a Unicode code point from UTF16 surrogate pair. 14811be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniainline UInt32 CreateCodePointFromUtf16SurrogatePair(wchar_t first, 14821be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania wchar_t second) { 14831be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania const UInt32 mask = (1 << 10) - 1; 14841be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania return (sizeof(wchar_t) == 2) ? 14851be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania (((first & mask) << 10) | (second & mask)) + 0x10000 : 14861be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // This function should not be called when the condition is 14871be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // false, but we provide a sensible default in case it is. 14881be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania static_cast<UInt32>(first); 14891be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 14901be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 14911be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Converts a wide string to a narrow string in UTF-8 encoding. 14921be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// The wide string is assumed to have the following encoding: 14931be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// UTF-16 if sizeof(wchar_t) == 2 (on Windows, Cygwin, Symbian OS) 14941be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// UTF-32 if sizeof(wchar_t) == 4 (on Linux) 14951be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Parameter str points to a null-terminated wide string. 14961be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Parameter num_chars may additionally limit the number 14971be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// of wchar_t characters processed. -1 is used when the entire string 14981be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// should be processed. 14991be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// If the string contains code points that are not valid Unicode code points 15001be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// (i.e. outside of Unicode range U+0 to U+10FFFF) they will be output 15011be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// as '(Invalid Unicode 0xXXXXXXXX)'. If the string is in UTF16 encoding 15021be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// and contains invalid UTF-16 surrogate pairs, values in those pairs 15031be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// will be encoded as individual Unicode characters from Basic Normal Plane. 15041be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaString WideStringToUtf8(const wchar_t* str, int num_chars) { 15051be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania if (num_chars == -1) 15061be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania num_chars = static_cast<int>(wcslen(str)); 15071be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 150841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot ::std::stringstream stream; 15091be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania for (int i = 0; i < num_chars; ++i) { 15101be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania UInt32 unicode_code_point; 15111be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 15121be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania if (str[i] == L'\0') { 15131be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania break; 15141be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania } else if (i + 1 < num_chars && IsUtf16SurrogatePair(str[i], str[i + 1])) { 15151be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania unicode_code_point = CreateCodePointFromUtf16SurrogatePair(str[i], 15161be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania str[i + 1]); 15171be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania i++; 15181be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania } else { 15191be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania unicode_code_point = static_cast<UInt32>(str[i]); 15201be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania } 15211be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 15221be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania char buffer[32]; // CodePointToUtf8 requires a buffer this big. 15231be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania stream << CodePointToUtf8(unicode_code_point, buffer); 15241be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania } 152541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot return StringStreamToString(&stream); 15261be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 15271be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 15281be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Converts a wide C string to a String using the UTF-8 encoding. 15291be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// NULL will be converted to "(null)". 15301be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaString String::ShowWideCString(const wchar_t * wide_c_str) { 15311be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania if (wide_c_str == NULL) return String("(null)"); 15321be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 15331be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania return String(internal::WideStringToUtf8(wide_c_str, -1).c_str()); 15341be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 15351be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 15361be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Similar to ShowWideCString(), except that this function encloses 15371be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// the converted string in double quotes. 15381be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaString String::ShowWideCStringQuoted(const wchar_t* wide_c_str) { 15391be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania if (wide_c_str == NULL) return String("(null)"); 15401be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 15411be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania return String::Format("L\"%s\"", 15421be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania String::ShowWideCString(wide_c_str).c_str()); 15431be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 15441be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 15451be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Compares two wide C strings. Returns true iff they have the same 15461be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// content. 15471be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// 15481be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Unlike wcscmp(), this function can handle NULL argument(s). A NULL 15491be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// C string is considered different to any non-NULL C string, 15501be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// including the empty string. 15511be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniabool String::WideCStringEquals(const wchar_t * lhs, const wchar_t * rhs) { 15521be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania if (lhs == NULL) return rhs == NULL; 15531be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 15541be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania if (rhs == NULL) return false; 15551be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 15561be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania return wcscmp(lhs, rhs) == 0; 15571be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 15581be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 15591be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Helper function for *_STREQ on wide strings. 15601be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaAssertionResult CmpHelperSTREQ(const char* expected_expression, 15611be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania const char* actual_expression, 15621be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania const wchar_t* expected, 15631be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania const wchar_t* actual) { 15641be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania if (String::WideCStringEquals(expected, actual)) { 15651be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania return AssertionSuccess(); 15661be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania } 15671be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 15681be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania return EqFailure(expected_expression, 15691be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania actual_expression, 15701be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania String::ShowWideCStringQuoted(expected), 15711be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania String::ShowWideCStringQuoted(actual), 15721be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania false); 15731be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 15741be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 15751be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Helper function for *_STRNE on wide strings. 15761be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaAssertionResult CmpHelperSTRNE(const char* s1_expression, 15771be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania const char* s2_expression, 15781be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania const wchar_t* s1, 15791be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania const wchar_t* s2) { 15801be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania if (!String::WideCStringEquals(s1, s2)) { 15811be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania return AssertionSuccess(); 15821be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania } 15831be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 158441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot return AssertionFailure() << "Expected: (" << s1_expression << ") != (" 158541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot << s2_expression << "), actual: " 158641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot << String::ShowWideCStringQuoted(s1) 158741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot << " vs " << String::ShowWideCStringQuoted(s2); 15881be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 15891be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 15901be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Compares two C strings, ignoring case. Returns true iff they have 15911be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// the same content. 15921be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// 15931be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Unlike strcasecmp(), this function can handle NULL argument(s). A 15941be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// NULL C string is considered different to any non-NULL C string, 15951be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// including the empty string. 15961be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniabool String::CaseInsensitiveCStringEquals(const char * lhs, const char * rhs) { 159741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot if (lhs == NULL) 159841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot return rhs == NULL; 159941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot if (rhs == NULL) 160041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot return false; 160141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot return posix::StrCaseCmp(lhs, rhs) == 0; 16021be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 16031be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 16041be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // Compares two wide C strings, ignoring case. Returns true iff they 16051be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // have the same content. 16061be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // 16071be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // Unlike wcscasecmp(), this function can handle NULL argument(s). 16081be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // A NULL C string is considered different to any non-NULL wide C string, 16091be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // including the empty string. 16101be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // NB: The implementations on different platforms slightly differ. 16111be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // On windows, this method uses _wcsicmp which compares according to LC_CTYPE 16121be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // environment variable. On GNU platform this method uses wcscasecmp 16131be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // which compares according to LC_CTYPE category of the current locale. 16141be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // On MacOS X, it uses towlower, which also uses LC_CTYPE category of the 16151be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // current locale. 16161be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniabool String::CaseInsensitiveWideCStringEquals(const wchar_t* lhs, 16171be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania const wchar_t* rhs) { 161841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot if (lhs == NULL) return rhs == NULL; 16191be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 162041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot if (rhs == NULL) return false; 16211be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 16221be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#if GTEST_OS_WINDOWS 16231be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania return _wcsicmp(lhs, rhs) == 0; 162441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot#elif GTEST_OS_LINUX && !GTEST_OS_LINUX_ANDROID 16251be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania return wcscasecmp(lhs, rhs) == 0; 16261be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#else 162741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // Android, Mac OS X and Cygwin don't define wcscasecmp. 162841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // Other unknown OSes may not define it either. 16291be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania wint_t left, right; 16301be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania do { 16311be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania left = towlower(*lhs++); 16321be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania right = towlower(*rhs++); 16331be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania } while (left && left == right); 16341be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania return left == right; 16351be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#endif // OS selector 16361be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 16371be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 16381be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Compares this with another String. 16391be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Returns < 0 if this is less than rhs, 0 if this is equal to rhs, or > 0 16401be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// if this is greater than rhs. 16411be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniaint String::Compare(const String & rhs) const { 164241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot const char* const lhs_c_str = c_str(); 164341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot const char* const rhs_c_str = rhs.c_str(); 164441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 164541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot if (lhs_c_str == NULL) { 164641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot return rhs_c_str == NULL ? 0 : -1; // NULL < anything except NULL 164741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot } else if (rhs_c_str == NULL) { 164841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot return 1; 16491be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania } 16501be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 165141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot const size_t shorter_str_len = 165241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot length() <= rhs.length() ? length() : rhs.length(); 165341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot for (size_t i = 0; i != shorter_str_len; i++) { 165441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot if (lhs_c_str[i] < rhs_c_str[i]) { 165541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot return -1; 165641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot } else if (lhs_c_str[i] > rhs_c_str[i]) { 165741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot return 1; 165841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot } 165941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot } 166041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot return (length() < rhs.length()) ? -1 : 166141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot (length() > rhs.length()) ? 1 : 0; 16621be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 16631be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 16641be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Returns true iff this String ends with the given suffix. *Any* 16651be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// String is considered to end with a NULL or empty suffix. 16661be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniabool String::EndsWith(const char* suffix) const { 16671be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania if (suffix == NULL || CStringEquals(suffix, "")) return true; 16681be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 166941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot if (c_str() == NULL) return false; 16701be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 167141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot const size_t this_len = strlen(c_str()); 16721be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania const size_t suffix_len = strlen(suffix); 16731be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania return (this_len >= suffix_len) && 167441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot CStringEquals(c_str() + this_len - suffix_len, suffix); 16751be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 16761be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 16771be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Returns true iff this String ends with the given suffix, ignoring case. 16781be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Any String is considered to end with a NULL or empty suffix. 16791be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniabool String::EndsWithCaseInsensitive(const char* suffix) const { 16801be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania if (suffix == NULL || CStringEquals(suffix, "")) return true; 16811be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 168241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot if (c_str() == NULL) return false; 16831be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 168441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot const size_t this_len = strlen(c_str()); 16851be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania const size_t suffix_len = strlen(suffix); 16861be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania return (this_len >= suffix_len) && 168741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot CaseInsensitiveCStringEquals(c_str() + this_len - suffix_len, suffix); 16881be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 16891be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 16901be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Formats a list of arguments to a String, using the same format 16911be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// spec string as for printf. 16921be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// 16931be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// We do not use the StringPrintf class as it is not universally 16941be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// available. 16951be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// 16961be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// The result is limited to 4096 characters (including the tailing 0). 169741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// If 4096 characters are not enough to format the input, or if 169841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// there's an error, "<formatting error or buffer exceeded>" is 169941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// returned. 17001be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaString String::Format(const char * format, ...) { 17011be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania va_list args; 17021be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania va_start(args, format); 17031be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 17041be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania char buffer[4096]; 170541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot const int kBufferSize = sizeof(buffer)/sizeof(buffer[0]); 170641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 17071be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // MSVC 8 deprecates vsnprintf(), so we want to suppress warning 17081be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // 4996 (deprecated function) there. 170941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot#ifdef _MSC_VER // We are using MSVC. 171041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot# pragma warning(push) // Saves the current warning state. 171141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot# pragma warning(disable:4996) // Temporarily disables warning 4996. 171241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 171341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot const int size = vsnprintf(buffer, kBufferSize, format, args); 171441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 171541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot# pragma warning(pop) // Restores the warning state. 171641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot#else // We are not using MSVC. 171741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot const int size = vsnprintf(buffer, kBufferSize, format, args); 171841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot#endif // _MSC_VER 17191be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania va_end(args); 17201be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 172141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // vsnprintf()'s behavior is not portable. When the buffer is not 172241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // big enough, it returns a negative value in MSVC, and returns the 172341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // needed buffer size on Linux. When there is an output error, it 172441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // always returns a negative value. For simplicity, we lump the two 172541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // error cases together. 172641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot if (size < 0 || size >= kBufferSize) { 172741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot return String("<formatting error or buffer exceeded>"); 172841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot } else { 172941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot return String(buffer, size); 173041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot } 17311be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 17321be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 173341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// Converts the buffer in a stringstream to a String, converting NUL 17341be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// bytes to "\\0" along the way. 173541d0579e8de9ef4ff178fc4991043c61a19943f7Brett ChabotString StringStreamToString(::std::stringstream* ss) { 17361be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania const ::std::string& str = ss->str(); 17371be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania const char* const start = str.c_str(); 17381be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania const char* const end = start + str.length(); 17391be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 174041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // We need to use a helper stringstream to do this transformation 17411be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // because String doesn't support push_back(). 174241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot ::std::stringstream helper; 17431be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania for (const char* ch = start; ch != end; ++ch) { 17441be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania if (*ch == '\0') { 17451be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania helper << "\\0"; // Replaces NUL with "\\0"; 17461be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania } else { 17471be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania helper.put(*ch); 17481be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania } 17491be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania } 17501be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 17511be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania return String(helper.str().c_str()); 17521be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 17531be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 17541be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Appends the user-supplied message to the Google-Test-generated message. 17551be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaString AppendUserMessage(const String& gtest_msg, 17561be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania const Message& user_msg) { 17571be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // Appends the user message if it's non-empty. 17581be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania const String user_msg_string = user_msg.GetString(); 17591be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania if (user_msg_string.empty()) { 17601be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania return gtest_msg; 17611be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania } 17621be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 17631be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania Message msg; 17641be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania msg << gtest_msg << "\n" << user_msg_string; 17651be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 17661be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania return msg.GetString(); 17671be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 17681be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 176941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot} // namespace internal 177041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 17711be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// class TestResult 17721be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 17731be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Creates an empty TestResult. 17741be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaTestResult::TestResult() 17751be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania : death_test_count_(0), 17761be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania elapsed_time_(0) { 17771be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 17781be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 17791be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// D'tor. 17801be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaTestResult::~TestResult() { 17811be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 17821be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 178341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// Returns the i-th test part result among all the results. i can 178441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// range from 0 to total_part_count() - 1. If i is not in that range, 178541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// aborts the program. 178641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotconst TestPartResult& TestResult::GetTestPartResult(int i) const { 178741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot if (i < 0 || i >= total_part_count()) 178841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot internal::posix::Abort(); 178941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot return test_part_results_.at(i); 179041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot} 179141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 179241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// Returns the i-th test property. i can range from 0 to 179341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// test_property_count() - 1. If i is not in that range, aborts the 179441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// program. 179541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotconst TestProperty& TestResult::GetTestProperty(int i) const { 179641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot if (i < 0 || i >= test_property_count()) 179741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot internal::posix::Abort(); 179841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot return test_properties_.at(i); 179941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot} 180041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 180141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// Clears the test part results. 180241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotvoid TestResult::ClearTestPartResults() { 180341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot test_part_results_.clear(); 180441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot} 180541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 18061be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Adds a test part result to the list. 18071be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniavoid TestResult::AddTestPartResult(const TestPartResult& test_part_result) { 180841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot test_part_results_.push_back(test_part_result); 18091be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 18101be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 18111be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Adds a test property to the list. If a property with the same key as the 18121be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// supplied property is already represented, the value of this test_property 18131be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// replaces the old value for that key. 18141be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniavoid TestResult::RecordProperty(const TestProperty& test_property) { 18151be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania if (!ValidateTestProperty(test_property)) { 18161be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania return; 18171be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania } 181841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot internal::MutexLock lock(&test_properites_mutex_); 181941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot const std::vector<TestProperty>::iterator property_with_matching_key = 182041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot std::find_if(test_properties_.begin(), test_properties_.end(), 182141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot internal::TestPropertyKeyIs(test_property.key())); 182241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot if (property_with_matching_key == test_properties_.end()) { 182341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot test_properties_.push_back(test_property); 18241be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania return; 18251be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania } 182641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot property_with_matching_key->SetValue(test_property.value()); 18271be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 18281be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 18291be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Adds a failure if the key is a reserved attribute of Google Test 18301be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// testcase tags. Returns true if the property is valid. 18311be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniabool TestResult::ValidateTestProperty(const TestProperty& test_property) { 183241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot internal::String key(test_property.key()); 18331be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania if (key == "name" || key == "status" || key == "time" || key == "classname") { 18341be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania ADD_FAILURE() 18351be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania << "Reserved key used in RecordProperty(): " 18361be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania << key 18371be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania << " ('name', 'status', 'time', and 'classname' are reserved by " 18381be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania << GTEST_NAME_ << ")"; 18391be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania return false; 18401be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania } 18411be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania return true; 18421be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 18431be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 18441be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Clears the object. 18451be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniavoid TestResult::Clear() { 184641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot test_part_results_.clear(); 184741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot test_properties_.clear(); 18481be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania death_test_count_ = 0; 18491be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania elapsed_time_ = 0; 18501be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 18511be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 185241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// Returns true iff the test failed. 185341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotbool TestResult::Failed() const { 185441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot for (int i = 0; i < total_part_count(); ++i) { 185541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot if (GetTestPartResult(i).failed()) 185641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot return true; 185741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot } 185841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot return false; 18591be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 18601be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 18611be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Returns true iff the test part fatally failed. 186241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotstatic bool TestPartFatallyFailed(const TestPartResult& result) { 18631be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania return result.fatally_failed(); 18641be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 18651be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 18661be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Returns true iff the test fatally failed. 18671be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniabool TestResult::HasFatalFailure() const { 186841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot return CountIf(test_part_results_, TestPartFatallyFailed) > 0; 186941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot} 187041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 187141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// Returns true iff the test part non-fatally failed. 187241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotstatic bool TestPartNonfatallyFailed(const TestPartResult& result) { 187341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot return result.nonfatally_failed(); 187441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot} 187541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 187641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// Returns true iff the test has a non-fatal failure. 187741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotbool TestResult::HasNonfatalFailure() const { 187841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot return CountIf(test_part_results_, TestPartNonfatallyFailed) > 0; 18791be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 18801be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 18811be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Gets the number of all test parts. This is the sum of the number 18821be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// of successful test parts and the number of failed test parts. 18831be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniaint TestResult::total_part_count() const { 188441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot return static_cast<int>(test_part_results_.size()); 18851be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 18861be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 188741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// Returns the number of the test properties. 188841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotint TestResult::test_property_count() const { 188941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot return static_cast<int>(test_properties_.size()); 189041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot} 18911be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 18921be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// class Test 18931be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 18941be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Creates a Test object. 18951be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 18961be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// The c'tor saves the values of all Google Test flags. 18971be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaTest::Test() 18981be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania : gtest_flag_saver_(new internal::GTestFlagSaver) { 18991be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 19001be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 19011be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// The d'tor restores the values of all Google Test flags. 19021be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaTest::~Test() { 19031be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania delete gtest_flag_saver_; 19041be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 19051be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 19061be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Sets up the test fixture. 19071be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// 19081be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// A sub-class may override this. 19091be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniavoid Test::SetUp() { 19101be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 19111be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 19121be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Tears down the test fixture. 19131be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// 19141be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// A sub-class may override this. 19151be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniavoid Test::TearDown() { 19161be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 19171be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 19181be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Allows user supplied key value pairs to be recorded for later output. 19191be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniavoid Test::RecordProperty(const char* key, const char* value) { 19201be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania UnitTest::GetInstance()->RecordPropertyForCurrentTest(key, value); 19211be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 19221be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 19231be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Allows user supplied key value pairs to be recorded for later output. 19241be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniavoid Test::RecordProperty(const char* key, int value) { 19251be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania Message value_message; 19261be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania value_message << value; 19271be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania RecordProperty(key, value_message.GetString().c_str()); 19281be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 19291be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 193041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotnamespace internal { 19311be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 193241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotvoid ReportFailureInUnknownLocation(TestPartResult::Type result_type, 193341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot const String& message) { 193441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // This function is a friend of UnitTest and as such has access to 193541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // AddTestPartResult. 193641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot UnitTest::GetInstance()->AddTestPartResult( 193741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot result_type, 193841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot NULL, // No info about the source file where the exception occurred. 193941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot -1, // We have no info on which line caused the exception. 194041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot message, 194141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot String()); // No stack trace, either. 19421be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 19431be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 194441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot} // namespace internal 19451be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 19461be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Google Test requires all tests in the same test case to use the same test 19471be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// fixture class. This function checks if the current test has the 19481be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// same fixture class as the first test in the current test case. If 19491be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// yes, it returns true; otherwise it generates a Google Test failure and 19501be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// returns false. 19511be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniabool Test::HasSameFixtureClass() { 19521be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania internal::UnitTestImpl* const impl = internal::GetUnitTestImpl(); 19531be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania const TestCase* const test_case = impl->current_test_case(); 19541be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 19551be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // Info about the first test in the current test case. 195641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot const TestInfo* const first_test_info = test_case->test_info_list()[0]; 195741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot const internal::TypeId first_fixture_id = first_test_info->fixture_class_id_; 19581be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania const char* const first_test_name = first_test_info->name(); 19591be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 19601be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // Info about the current test. 196141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot const TestInfo* const this_test_info = impl->current_test_info(); 196241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot const internal::TypeId this_fixture_id = this_test_info->fixture_class_id_; 19631be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania const char* const this_test_name = this_test_info->name(); 19641be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 19651be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania if (this_fixture_id != first_fixture_id) { 19661be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // Is the first test defined using TEST? 19671be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania const bool first_is_TEST = first_fixture_id == internal::GetTestTypeId(); 19681be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // Is this test defined using TEST? 19691be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania const bool this_is_TEST = this_fixture_id == internal::GetTestTypeId(); 19701be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 19711be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania if (first_is_TEST || this_is_TEST) { 19721be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // The user mixed TEST and TEST_F in this test case - we'll tell 19731be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // him/her how to fix it. 19741be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 19751be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // Gets the name of the TEST and the name of the TEST_F. Note 19761be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // that first_is_TEST and this_is_TEST cannot both be true, as 19771be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // the fixture IDs are different for the two tests. 19781be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania const char* const TEST_name = 19791be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania first_is_TEST ? first_test_name : this_test_name; 19801be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania const char* const TEST_F_name = 19811be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania first_is_TEST ? this_test_name : first_test_name; 19821be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 19831be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania ADD_FAILURE() 19841be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania << "All tests in the same test case must use the same test fixture\n" 19851be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania << "class, so mixing TEST_F and TEST in the same test case is\n" 19861be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania << "illegal. In test case " << this_test_info->test_case_name() 19871be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania << ",\n" 19881be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania << "test " << TEST_F_name << " is defined using TEST_F but\n" 19891be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania << "test " << TEST_name << " is defined using TEST. You probably\n" 19901be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania << "want to change the TEST to TEST_F or move it to another test\n" 19911be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania << "case."; 19921be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania } else { 19931be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // The user defined two fixture classes with the same name in 19941be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // two namespaces - we'll tell him/her how to fix it. 19951be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania ADD_FAILURE() 19961be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania << "All tests in the same test case must use the same test fixture\n" 19971be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania << "class. However, in test case " 19981be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania << this_test_info->test_case_name() << ",\n" 19991be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania << "you defined test " << first_test_name 20001be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania << " and test " << this_test_name << "\n" 20011be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania << "using two different test fixture classes. This can happen if\n" 20021be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania << "the two classes are from different namespaces or translation\n" 20031be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania << "units and have the same name. You should probably rename one\n" 20041be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania << "of the classes to put the tests into different test cases."; 20051be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania } 20061be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania return false; 20071be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania } 20081be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 20091be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania return true; 20101be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 20111be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 201241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot#if GTEST_HAS_SEH 20131be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 201441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// Adds an "exception thrown" fatal failure to the current test. This 201541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// function returns its result via an output parameter pointer because VC++ 201641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// prohibits creation of objects with destructors on stack in functions 201741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// using __try (see error C2712). 201841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotstatic internal::String* FormatSehExceptionMessage(DWORD exception_code, 201941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot const char* location) { 202041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot Message message; 202141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot message << "SEH exception with code 0x" << std::setbase(16) << 202241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot exception_code << std::setbase(10) << " thrown in " << location << "."; 20231be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 202441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot return new internal::String(message.GetString()); 202541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot} 202641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 202741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot#endif // GTEST_HAS_SEH 202841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 202941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot#if GTEST_HAS_EXCEPTIONS 203041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 203141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// Adds an "exception thrown" fatal failure to the current test. 203241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotstatic internal::String FormatCxxExceptionMessage(const char* description, 203341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot const char* location) { 203441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot Message message; 203541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot if (description != NULL) { 203641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot message << "C++ exception with description \"" << description << "\""; 203741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot } else { 203841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot message << "Unknown C++ exception"; 20391be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania } 204041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot message << " thrown in " << location << "."; 20411be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 204241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot return message.GetString(); 204341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot} 204441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 204541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotstatic internal::String PrintTestPartResultToString( 204641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot const TestPartResult& test_part_result); 204741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 204841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// A failed Google Test assertion will throw an exception of this type when 204941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// GTEST_FLAG(throw_on_failure) is true (if exceptions are enabled). We 205041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// derive it from std::runtime_error, which is for errors presumably 205141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// detectable only at run time. Since std::runtime_error inherits from 205241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// std::exception, many testing frameworks know how to extract and print the 205341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// message inside it. 205441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotclass GoogleTestFailureException : public ::std::runtime_error { 205541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot public: 205641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot explicit GoogleTestFailureException(const TestPartResult& failure) 205741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot : ::std::runtime_error(PrintTestPartResultToString(failure).c_str()) {} 205841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot}; 205941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot#endif // GTEST_HAS_EXCEPTIONS 206041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 206141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotnamespace internal { 206241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// We put these helper functions in the internal namespace as IBM's xlC 206341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// compiler rejects the code if they were declared static. 206441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 206541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// Runs the given method and handles SEH exceptions it throws, when 206641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// SEH is supported; returns the 0-value for type Result in case of an 206741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// SEH exception. (Microsoft compilers cannot handle SEH and C++ 206841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// exceptions in the same function. Therefore, we provide a separate 206941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// wrapper function for handling SEH exceptions.) 207041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabottemplate <class T, typename Result> 207141d0579e8de9ef4ff178fc4991043c61a19943f7Brett ChabotResult HandleSehExceptionsInMethodIfSupported( 207241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot T* object, Result (T::*method)(), const char* location) { 207341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot#if GTEST_HAS_SEH 20741be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania __try { 207541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot return (object->*method)(); 207641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot } __except (internal::UnitTestOptions::GTestShouldProcessSEH( // NOLINT 20771be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania GetExceptionCode())) { 207841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // We create the exception message on the heap because VC++ prohibits 207941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // creation of objects with destructors on stack in functions using __try 208041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // (see error C2712). 208141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot internal::String* exception_message = FormatSehExceptionMessage( 208241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot GetExceptionCode(), location); 208341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot internal::ReportFailureInUnknownLocation(TestPartResult::kFatalFailure, 208441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot *exception_message); 208541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot delete exception_message; 208641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot return static_cast<Result>(0); 208741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot } 208841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot#else 208941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot (void)location; 209041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot return (object->*method)(); 209141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot#endif // GTEST_HAS_SEH 209241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot} 209341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 209441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// Runs the given method and catches and reports C++ and/or SEH-style 209541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// exceptions, if they are supported; returns the 0-value for type 209641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// Result in case of an SEH exception. 209741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabottemplate <class T, typename Result> 209841d0579e8de9ef4ff178fc4991043c61a19943f7Brett ChabotResult HandleExceptionsInMethodIfSupported( 209941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot T* object, Result (T::*method)(), const char* location) { 210041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // NOTE: The user code can affect the way in which Google Test handles 210141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // exceptions by setting GTEST_FLAG(catch_exceptions), but only before 210241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // RUN_ALL_TESTS() starts. It is technically possible to check the flag 210341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // after the exception is caught and either report or re-throw the 210441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // exception based on the flag's value: 210541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // 210641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // try { 210741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // // Perform the test method. 210841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // } catch (...) { 210941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // if (GTEST_FLAG(catch_exceptions)) 211041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // // Report the exception as failure. 211141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // else 211241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // throw; // Re-throws the original exception. 211341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // } 211441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // 211541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // However, the purpose of this flag is to allow the program to drop into 211641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // the debugger when the exception is thrown. On most platforms, once the 211741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // control enters the catch block, the exception origin information is 211841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // lost and the debugger will stop the program at the point of the 211941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // re-throw in this function -- instead of at the point of the original 212041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // throw statement in the code under test. For this reason, we perform 212141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // the check early, sacrificing the ability to affect Google Test's 212241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // exception handling in the method where the exception is thrown. 212341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot if (internal::GetUnitTestImpl()->catch_exceptions()) { 212441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot#if GTEST_HAS_EXCEPTIONS 212541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot try { 212641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot return HandleSehExceptionsInMethodIfSupported(object, method, location); 212741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot } catch (const GoogleTestFailureException&) { // NOLINT 212841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // This exception doesn't originate in code under test. It makes no 212941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // sense to report it as a test failure. 213041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot throw; 213141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot } catch (const std::exception& e) { // NOLINT 213241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot internal::ReportFailureInUnknownLocation( 213341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot TestPartResult::kFatalFailure, 213441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot FormatCxxExceptionMessage(e.what(), location)); 213541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot } catch (...) { // NOLINT 213641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot internal::ReportFailureInUnknownLocation( 213741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot TestPartResult::kFatalFailure, 213841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot FormatCxxExceptionMessage(NULL, location)); 213941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot } 214041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot return static_cast<Result>(0); 214141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot#else 214241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot return HandleSehExceptionsInMethodIfSupported(object, method, location); 214341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot#endif // GTEST_HAS_EXCEPTIONS 214441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot } else { 214541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot return (object->*method)(); 21461be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania } 214741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot} 21481be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 214941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot} // namespace internal 215041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 215141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// Runs the test and updates the test result. 215241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotvoid Test::Run() { 215341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot if (!HasSameFixtureClass()) return; 21541be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 215541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot internal::UnitTestImpl* const impl = internal::GetUnitTestImpl(); 215641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot impl->os_stack_trace_getter()->UponLeavingGTest(); 215741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot internal::HandleExceptionsInMethodIfSupported(this, &Test::SetUp, "SetUp()"); 21581be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // We will run the test only if SetUp() was successful. 21591be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania if (!HasFatalFailure()) { 21601be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania impl->os_stack_trace_getter()->UponLeavingGTest(); 216141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot internal::HandleExceptionsInMethodIfSupported( 216241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot this, &Test::TestBody, "the test body"); 21631be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania } 21641be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 21651be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // However, we want to clean up as much as possible. Hence we will 21661be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // always call TearDown(), even if SetUp() or the test body has 21671be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // failed. 21681be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania impl->os_stack_trace_getter()->UponLeavingGTest(); 216941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot internal::HandleExceptionsInMethodIfSupported( 217041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot this, &Test::TearDown, "TearDown()"); 21711be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 21721be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 21731be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Returns true iff the current test has a fatal failure. 21741be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniabool Test::HasFatalFailure() { 21751be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania return internal::GetUnitTestImpl()->current_test_result()->HasFatalFailure(); 21761be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 21771be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 217841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// Returns true iff the current test has a non-fatal failure. 217941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotbool Test::HasNonfatalFailure() { 218041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot return internal::GetUnitTestImpl()->current_test_result()-> 218141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot HasNonfatalFailure(); 218241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot} 218341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 21841be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// class TestInfo 21851be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 21861be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Constructs a TestInfo object. It assumes ownership of the test factory 218741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// object. 218841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// TODO(vladl@google.com): Make a_test_case_name and a_name const string&'s 218941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// to signify they cannot be NULLs. 219041d0579e8de9ef4ff178fc4991043c61a19943f7Brett ChabotTestInfo::TestInfo(const char* a_test_case_name, 219141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot const char* a_name, 219241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot const char* a_type_param, 219341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot const char* a_value_param, 21941be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania internal::TypeId fixture_class_id, 219541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot internal::TestFactoryBase* factory) 219641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot : test_case_name_(a_test_case_name), 219741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot name_(a_name), 219841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot type_param_(a_type_param ? new std::string(a_type_param) : NULL), 219941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot value_param_(a_value_param ? new std::string(a_value_param) : NULL), 220041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot fixture_class_id_(fixture_class_id), 220141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot should_run_(false), 220241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot is_disabled_(false), 220341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot matches_filter_(false), 220441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot factory_(factory), 220541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot result_() {} 22061be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 22071be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Destructs a TestInfo object. 220841d0579e8de9ef4ff178fc4991043c61a19943f7Brett ChabotTestInfo::~TestInfo() { delete factory_; } 22091be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 22101be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catanianamespace internal { 22111be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 22121be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Creates a new TestInfo object and registers it with Google Test; 22131be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// returns the created object. 22141be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// 22151be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Arguments: 22161be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// 22171be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// test_case_name: name of the test case 22181be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// name: name of the test 221941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// type_param: the name of the test's type parameter, or NULL if 222041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// this is not a typed or a type-parameterized test. 222141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// value_param: text representation of the test's value parameter, 222241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// or NULL if this is not a value-parameterized test. 22231be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// fixture_class_id: ID of the test fixture class 22241be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// set_up_tc: pointer to the function that sets up the test case 22251be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// tear_down_tc: pointer to the function that tears down the test case 22261be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// factory: pointer to the factory that creates a test object. 22271be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// The newly created TestInfo instance will assume 22281be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// ownership of the factory object. 22291be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaTestInfo* MakeAndRegisterTestInfo( 22301be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania const char* test_case_name, const char* name, 223141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot const char* type_param, 223241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot const char* value_param, 22331be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania TypeId fixture_class_id, 22341be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania SetUpTestCaseFunc set_up_tc, 22351be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania TearDownTestCaseFunc tear_down_tc, 22361be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania TestFactoryBase* factory) { 22371be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania TestInfo* const test_info = 223841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot new TestInfo(test_case_name, name, type_param, value_param, 22391be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania fixture_class_id, factory); 22401be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania GetUnitTestImpl()->AddTestInfo(set_up_tc, tear_down_tc, test_info); 22411be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania return test_info; 22421be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 22431be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 22441be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#if GTEST_HAS_PARAM_TEST 22451be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniavoid ReportInvalidTestCaseType(const char* test_case_name, 22461be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania const char* file, int line) { 22471be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania Message errors; 22481be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania errors 22491be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania << "Attempted redefinition of test case " << test_case_name << ".\n" 22501be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania << "All tests in the same test case must use the same test fixture\n" 22511be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania << "class. However, in test case " << test_case_name << ", you tried\n" 22521be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania << "to define a test using a fixture class different from the one\n" 22531be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania << "used earlier. This can happen if the two fixture classes are\n" 22541be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania << "from different namespaces and have the same name. You should\n" 22551be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania << "probably rename one of the classes to put the tests into different\n" 22561be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania << "test cases."; 22571be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 22581be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania fprintf(stderr, "%s %s", FormatFileLocation(file, line).c_str(), 22591be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania errors.GetString().c_str()); 22601be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 22611be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#endif // GTEST_HAS_PARAM_TEST 22621be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 22631be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} // namespace internal 22641be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 22651be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catanianamespace { 22661be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 22671be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// A predicate that checks the test name of a TestInfo against a known 22681be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// value. 22691be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// 22701be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// This is used for implementation of the TestCase class only. We put 22711be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// it in the anonymous namespace to prevent polluting the outer 22721be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// namespace. 22731be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// 22741be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// TestNameIs is copyable. 22751be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniaclass TestNameIs { 22761be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania public: 22771be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // Constructor. 22781be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // 22791be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // TestNameIs has NO default constructor. 22801be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania explicit TestNameIs(const char* name) 22811be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania : name_(name) {} 22821be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 22831be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // Returns true iff the test name of test_info matches name_. 22841be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania bool operator()(const TestInfo * test_info) const { 22851be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania return test_info && internal::String(test_info->name()).Compare(name_) == 0; 22861be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania } 22871be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 22881be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania private: 22891be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania internal::String name_; 22901be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}; 22911be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 22921be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} // namespace 22931be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 22941be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catanianamespace internal { 22951be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 22961be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// This method expands all parameterized tests registered with macros TEST_P 22971be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// and INSTANTIATE_TEST_CASE_P into regular tests and registers those. 22981be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// This will be done just once during the program runtime. 22991be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniavoid UnitTestImpl::RegisterParameterizedTests() { 23001be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#if GTEST_HAS_PARAM_TEST 23011be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania if (!parameterized_tests_registered_) { 23021be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania parameterized_test_registry_.RegisterTests(); 23031be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania parameterized_tests_registered_ = true; 23041be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania } 23051be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#endif 23061be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 23071be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 230841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot} // namespace internal 230941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 23101be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Creates the test object, runs it, records its result, and then 23111be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// deletes it. 231241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotvoid TestInfo::Run() { 23131be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania if (!should_run_) return; 23141be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 23151be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // Tells UnitTest where to store test result. 231641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot internal::UnitTestImpl* const impl = internal::GetUnitTestImpl(); 231741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot impl->set_current_test_info(this); 23181be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 231941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot TestEventListener* repeater = UnitTest::GetInstance()->listeners().repeater(); 23201be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 232141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // Notifies the unit test event listeners that a test is about to start. 232241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot repeater->OnTestStart(*this); 23231be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 232441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot const TimeInMillis start = internal::GetTimeInMillis(); 23251be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 232641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot impl->os_stack_trace_getter()->UponLeavingGTest(); 23271be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 23281be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // Creates the test object. 232941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot Test* const test = internal::HandleExceptionsInMethodIfSupported( 233041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot factory_, &internal::TestFactoryBase::CreateTest, 233141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot "the test fixture's constructor"); 233241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 233341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // Runs the test only if the test object was created and its 233441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // constructor didn't generate a fatal failure. 233541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot if ((test != NULL) && !Test::HasFatalFailure()) { 233641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // This doesn't throw as all user code that can throw are wrapped into 233741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // exception handling code. 23381be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania test->Run(); 23391be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania } 23401be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 23411be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // Deletes the test object. 23421be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania impl->os_stack_trace_getter()->UponLeavingGTest(); 234341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot internal::HandleExceptionsInMethodIfSupported( 234441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot test, &Test::DeleteSelf_, "the test fixture's destructor"); 23451be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 234641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot result_.set_elapsed_time(internal::GetTimeInMillis() - start); 23471be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 23481be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // Notifies the unit test event listener that a test has just finished. 234941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot repeater->OnTestEnd(*this); 23501be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 23511be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // Tells UnitTest to stop associating assertion results to this 23521be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // test. 23531be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania impl->set_current_test_info(NULL); 23541be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 23551be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 23561be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// class TestCase 23571be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 23581be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Gets the number of successful tests in this test case. 23591be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniaint TestCase::successful_test_count() const { 236041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot return CountIf(test_info_list_, TestPassed); 23611be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 23621be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 23631be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Gets the number of failed tests in this test case. 23641be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniaint TestCase::failed_test_count() const { 236541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot return CountIf(test_info_list_, TestFailed); 23661be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 23671be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 23681be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniaint TestCase::disabled_test_count() const { 236941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot return CountIf(test_info_list_, TestDisabled); 23701be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 23711be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 23721be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Get the number of tests in this test case that should run. 23731be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniaint TestCase::test_to_run_count() const { 237441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot return CountIf(test_info_list_, ShouldRunTest); 23751be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 23761be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 23771be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Gets the number of all tests. 23781be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniaint TestCase::total_test_count() const { 237941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot return static_cast<int>(test_info_list_.size()); 23801be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 23811be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 23821be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Creates a TestCase with the given name. 23831be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// 23841be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Arguments: 23851be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// 23861be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// name: name of the test case 238741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// a_type_param: the name of the test case's type parameter, or NULL if 238841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// this is not a typed or a type-parameterized test case. 23891be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// set_up_tc: pointer to the function that sets up the test case 23901be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// tear_down_tc: pointer to the function that tears down the test case 239141d0579e8de9ef4ff178fc4991043c61a19943f7Brett ChabotTestCase::TestCase(const char* a_name, const char* a_type_param, 23921be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania Test::SetUpTestCaseFunc set_up_tc, 23931be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania Test::TearDownTestCaseFunc tear_down_tc) 239441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot : name_(a_name), 239541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot type_param_(a_type_param ? new std::string(a_type_param) : NULL), 23961be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania set_up_tc_(set_up_tc), 23971be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania tear_down_tc_(tear_down_tc), 23981be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania should_run_(false), 23991be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania elapsed_time_(0) { 24001be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 24011be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 24021be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Destructor of TestCase. 24031be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaTestCase::~TestCase() { 24041be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // Deletes every Test in the collection. 240541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot ForEach(test_info_list_, internal::Delete<TestInfo>); 240641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot} 24071be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 240841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// Returns the i-th test among all the tests. i can range from 0 to 240941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// total_test_count() - 1. If i is not in that range, returns NULL. 241041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotconst TestInfo* TestCase::GetTestInfo(int i) const { 241141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot const int index = GetElementOr(test_indices_, i, -1); 241241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot return index < 0 ? NULL : test_info_list_[index]; 241341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot} 241441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 241541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// Returns the i-th test among all the tests. i can range from 0 to 241641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// total_test_count() - 1. If i is not in that range, returns NULL. 241741d0579e8de9ef4ff178fc4991043c61a19943f7Brett ChabotTestInfo* TestCase::GetMutableTestInfo(int i) { 241841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot const int index = GetElementOr(test_indices_, i, -1); 241941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot return index < 0 ? NULL : test_info_list_[index]; 24201be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 24211be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 24221be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Adds a test to this test case. Will delete the test upon 24231be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// destruction of the TestCase object. 24241be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniavoid TestCase::AddTestInfo(TestInfo * test_info) { 242541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot test_info_list_.push_back(test_info); 242641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot test_indices_.push_back(static_cast<int>(test_indices_.size())); 24271be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 24281be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 24291be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Runs every test in this TestCase. 24301be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniavoid TestCase::Run() { 24311be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania if (!should_run_) return; 24321be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 24331be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania internal::UnitTestImpl* const impl = internal::GetUnitTestImpl(); 24341be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania impl->set_current_test_case(this); 24351be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 243641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot TestEventListener* repeater = UnitTest::GetInstance()->listeners().repeater(); 24371be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 243841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot repeater->OnTestCaseStart(*this); 24391be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania impl->os_stack_trace_getter()->UponLeavingGTest(); 244041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot internal::HandleExceptionsInMethodIfSupported( 244141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot this, &TestCase::RunSetUpTestCase, "SetUpTestCase()"); 24421be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 24431be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania const internal::TimeInMillis start = internal::GetTimeInMillis(); 244441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot for (int i = 0; i < total_test_count(); i++) { 244541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot GetMutableTestInfo(i)->Run(); 244641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot } 24471be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania elapsed_time_ = internal::GetTimeInMillis() - start; 24481be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 24491be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania impl->os_stack_trace_getter()->UponLeavingGTest(); 245041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot internal::HandleExceptionsInMethodIfSupported( 245141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot this, &TestCase::RunTearDownTestCase, "TearDownTestCase()"); 245241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 245341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot repeater->OnTestCaseEnd(*this); 24541be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania impl->set_current_test_case(NULL); 24551be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 24561be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 24571be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Clears the results of all tests in this test case. 24581be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniavoid TestCase::ClearResult() { 245941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot ForEach(test_info_list_, TestInfo::ClearTestResult); 24601be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 24611be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 246241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// Shuffles the tests in this test case. 246341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotvoid TestCase::ShuffleTests(internal::Random* random) { 246441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot Shuffle(random, &test_indices_); 24651be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 24661be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 246741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// Restores the test order to before the first shuffle. 246841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotvoid TestCase::UnshuffleTests() { 246941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot for (size_t i = 0; i < test_indices_.size(); i++) { 247041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot test_indices_[i] = static_cast<int>(i); 247141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot } 247241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot} 24731be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 24741be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Formats a countable noun. Depending on its quantity, either the 24751be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// singular form or the plural form is used. e.g. 24761be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// 24771be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// FormatCountableNoun(1, "formula", "formuli") returns "1 formula". 24781be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// FormatCountableNoun(5, "book", "books") returns "5 books". 24791be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniastatic internal::String FormatCountableNoun(int count, 24801be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania const char * singular_form, 24811be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania const char * plural_form) { 24821be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania return internal::String::Format("%d %s", count, 24831be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania count == 1 ? singular_form : plural_form); 24841be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 24851be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 24861be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Formats the count of tests. 24871be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniastatic internal::String FormatTestCount(int test_count) { 24881be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania return FormatCountableNoun(test_count, "test", "tests"); 24891be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 24901be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 24911be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Formats the count of test cases. 24921be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniastatic internal::String FormatTestCaseCount(int test_case_count) { 24931be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania return FormatCountableNoun(test_case_count, "test case", "test cases"); 24941be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 24951be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 249641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// Converts a TestPartResult::Type enum to human-friendly string 249741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// representation. Both kNonFatalFailure and kFatalFailure are translated 249841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// to "Failure", as the user usually doesn't care about the difference 249941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// between the two when viewing the test result. 250041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotstatic const char * TestPartResultTypeToString(TestPartResult::Type type) { 25011be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania switch (type) { 250241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot case TestPartResult::kSuccess: 25031be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania return "Success"; 25041be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 250541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot case TestPartResult::kNonFatalFailure: 250641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot case TestPartResult::kFatalFailure: 25071be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#ifdef _MSC_VER 25081be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania return "error: "; 25091be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#else 25101be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania return "Failure\n"; 25111be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#endif 251241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot default: 251341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot return "Unknown result type"; 25141be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania } 25151be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 25161be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 25171be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Prints a TestPartResult to a String. 25181be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniastatic internal::String PrintTestPartResultToString( 25191be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania const TestPartResult& test_part_result) { 25201be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania return (Message() 25211be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania << internal::FormatFileLocation(test_part_result.file_name(), 25221be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania test_part_result.line_number()) 25231be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania << " " << TestPartResultTypeToString(test_part_result.type()) 25241be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania << test_part_result.message()).GetString(); 25251be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 25261be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 25271be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Prints a TestPartResult. 252841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotstatic void PrintTestPartResult(const TestPartResult& test_part_result) { 252941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot const internal::String& result = 253041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot PrintTestPartResultToString(test_part_result); 253141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot printf("%s\n", result.c_str()); 25321be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania fflush(stdout); 253341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // If the test program runs in Visual Studio or a debugger, the 253441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // following statements add the test part result message to the Output 253541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // window such that the user can double-click on it to jump to the 253641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // corresponding source code location; otherwise they do nothing. 253741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot#if GTEST_OS_WINDOWS && !GTEST_OS_WINDOWS_MOBILE 253841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // We don't call OutputDebugString*() on Windows Mobile, as printing 253941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // to stdout is done by OutputDebugString() there already - we don't 254041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // want the same message printed twice. 254141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot ::OutputDebugStringA(result.c_str()); 254241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot ::OutputDebugStringA("\n"); 254341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot#endif 25441be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 25451be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 25461be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// class PrettyUnitTestResultPrinter 25471be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 25481be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catanianamespace internal { 25491be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 25501be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniaenum GTestColor { 25511be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania COLOR_DEFAULT, 25521be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania COLOR_RED, 25531be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania COLOR_GREEN, 25541be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania COLOR_YELLOW 25551be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}; 25561be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 255741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot#if GTEST_OS_WINDOWS && !GTEST_OS_WINDOWS_MOBILE 25581be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 25591be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Returns the character attribute for the given color. 25601be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaWORD GetColorAttribute(GTestColor color) { 25611be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania switch (color) { 25621be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania case COLOR_RED: return FOREGROUND_RED; 25631be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania case COLOR_GREEN: return FOREGROUND_GREEN; 25641be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania case COLOR_YELLOW: return FOREGROUND_RED | FOREGROUND_GREEN; 25651be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania default: return 0; 25661be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania } 25671be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 25681be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 25691be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#else 25701be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 25711be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Returns the ANSI color code for the given color. COLOR_DEFAULT is 25721be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// an invalid input. 25731be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniaconst char* GetAnsiColorCode(GTestColor color) { 25741be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania switch (color) { 25751be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania case COLOR_RED: return "1"; 25761be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania case COLOR_GREEN: return "2"; 25771be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania case COLOR_YELLOW: return "3"; 25781be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania default: return NULL; 25791be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania }; 25801be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 25811be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 258241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot#endif // GTEST_OS_WINDOWS && !GTEST_OS_WINDOWS_MOBILE 25831be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 25841be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Returns true iff Google Test should use colors in the output. 25851be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniabool ShouldUseColor(bool stdout_is_tty) { 25861be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania const char* const gtest_color = GTEST_FLAG(color).c_str(); 25871be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 25881be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania if (String::CaseInsensitiveCStringEquals(gtest_color, "auto")) { 25891be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#if GTEST_OS_WINDOWS 25901be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // On Windows the TERM variable is usually not set, but the 25911be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // console there does support colors. 25921be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania return stdout_is_tty; 25931be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#else 25941be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // On non-Windows platforms, we rely on the TERM variable. 259541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot const char* const term = posix::GetEnv("TERM"); 25961be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania const bool term_supports_color = 25971be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania String::CStringEquals(term, "xterm") || 25981be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania String::CStringEquals(term, "xterm-color") || 259941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot String::CStringEquals(term, "xterm-256color") || 260041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot String::CStringEquals(term, "screen") || 260141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot String::CStringEquals(term, "linux") || 26021be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania String::CStringEquals(term, "cygwin"); 26031be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania return stdout_is_tty && term_supports_color; 26041be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#endif // GTEST_OS_WINDOWS 26051be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania } 26061be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 26071be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania return String::CaseInsensitiveCStringEquals(gtest_color, "yes") || 26081be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania String::CaseInsensitiveCStringEquals(gtest_color, "true") || 26091be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania String::CaseInsensitiveCStringEquals(gtest_color, "t") || 26101be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania String::CStringEquals(gtest_color, "1"); 26111be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // We take "yes", "true", "t", and "1" as meaning "yes". If the 26121be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // value is neither one of these nor "auto", we treat it as "no" to 26131be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // be conservative. 26141be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 26151be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 26161be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Helpers for printing colored strings to stdout. Note that on Windows, we 26171be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// cannot simply emit special characters and have the terminal change colors. 26181be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// This routine must actually emit the characters rather than return a string 26191be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// that would be colored when printed, as can be done on Linux. 26201be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniavoid ColoredPrintf(GTestColor color, const char* fmt, ...) { 26211be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania va_list args; 26221be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania va_start(args, fmt); 26231be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 262441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot#if GTEST_OS_WINDOWS_MOBILE || GTEST_OS_SYMBIAN || GTEST_OS_ZOS 26251be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania const bool use_color = false; 26261be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#else 26271be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania static const bool in_color_mode = 262841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot ShouldUseColor(posix::IsATTY(posix::FileNo(stdout)) != 0); 26291be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania const bool use_color = in_color_mode && (color != COLOR_DEFAULT); 263041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot#endif // GTEST_OS_WINDOWS_MOBILE || GTEST_OS_SYMBIAN || GTEST_OS_ZOS 26311be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // The '!= 0' comparison is necessary to satisfy MSVC 7.1. 26321be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 26331be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania if (!use_color) { 26341be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania vprintf(fmt, args); 26351be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania va_end(args); 26361be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania return; 26371be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania } 26381be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 263941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot#if GTEST_OS_WINDOWS && !GTEST_OS_WINDOWS_MOBILE 26401be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania const HANDLE stdout_handle = GetStdHandle(STD_OUTPUT_HANDLE); 26411be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 26421be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // Gets the current text color. 26431be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania CONSOLE_SCREEN_BUFFER_INFO buffer_info; 26441be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania GetConsoleScreenBufferInfo(stdout_handle, &buffer_info); 26451be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania const WORD old_color_attrs = buffer_info.wAttributes; 26461be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 264741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // We need to flush the stream buffers into the console before each 264841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // SetConsoleTextAttribute call lest it affect the text that is already 264941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // printed but has not yet reached the console. 265041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot fflush(stdout); 26511be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania SetConsoleTextAttribute(stdout_handle, 26521be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania GetColorAttribute(color) | FOREGROUND_INTENSITY); 26531be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania vprintf(fmt, args); 26541be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 265541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot fflush(stdout); 26561be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // Restores the text color. 26571be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania SetConsoleTextAttribute(stdout_handle, old_color_attrs); 26581be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#else 26591be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania printf("\033[0;3%sm", GetAnsiColorCode(color)); 26601be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania vprintf(fmt, args); 26611be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania printf("\033[m"); // Resets the terminal to default. 266241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot#endif // GTEST_OS_WINDOWS && !GTEST_OS_WINDOWS_MOBILE 26631be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania va_end(args); 26641be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 26651be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 266641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotvoid PrintFullTestCommentIfPresent(const TestInfo& test_info) { 266741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot const char* const type_param = test_info.type_param(); 266841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot const char* const value_param = test_info.value_param(); 26691be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 267041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot if (type_param != NULL || value_param != NULL) { 267141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot printf(", where "); 267241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot if (type_param != NULL) { 267341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot printf("TypeParam = %s", type_param); 267441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot if (value_param != NULL) 267541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot printf(" and "); 267641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot } 267741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot if (value_param != NULL) { 267841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot printf("GetParam() = %s", value_param); 267941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot } 268041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot } 268141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot} 26821be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 268341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// This class implements the TestEventListener interface. 26841be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// 26851be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Class PrettyUnitTestResultPrinter is copyable. 268641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotclass PrettyUnitTestResultPrinter : public TestEventListener { 26871be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania public: 26881be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania PrettyUnitTestResultPrinter() {} 26891be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania static void PrintTestName(const char * test_case, const char * test) { 26901be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania printf("%s.%s", test_case, test); 26911be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania } 26921be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 269341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // The following methods override what's in the TestEventListener class. 269441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot virtual void OnTestProgramStart(const UnitTest& /*unit_test*/) {} 269541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot virtual void OnTestIterationStart(const UnitTest& unit_test, int iteration); 269641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot virtual void OnEnvironmentsSetUpStart(const UnitTest& unit_test); 269741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot virtual void OnEnvironmentsSetUpEnd(const UnitTest& /*unit_test*/) {} 269841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot virtual void OnTestCaseStart(const TestCase& test_case); 269941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot virtual void OnTestStart(const TestInfo& test_info); 270041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot virtual void OnTestPartResult(const TestPartResult& result); 270141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot virtual void OnTestEnd(const TestInfo& test_info); 270241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot virtual void OnTestCaseEnd(const TestCase& test_case); 270341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot virtual void OnEnvironmentsTearDownStart(const UnitTest& unit_test); 270441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot virtual void OnEnvironmentsTearDownEnd(const UnitTest& /*unit_test*/) {} 270541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot virtual void OnTestIterationEnd(const UnitTest& unit_test, int iteration); 270641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot virtual void OnTestProgramEnd(const UnitTest& /*unit_test*/) {} 27071be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 27081be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania private: 270941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot static void PrintFailedTests(const UnitTest& unit_test); 271041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 27111be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania internal::String test_case_name_; 27121be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}; 27131be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 271441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // Fired before each iteration of tests starts. 271541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotvoid PrettyUnitTestResultPrinter::OnTestIterationStart( 271641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot const UnitTest& unit_test, int iteration) { 271741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot if (GTEST_FLAG(repeat) != 1) 271841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot printf("\nRepeating all tests (iteration %d) . . .\n\n", iteration + 1); 271941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 272041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot const char* const filter = GTEST_FLAG(filter).c_str(); 27211be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 27221be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // Prints the filter if it's not *. This reminds the user that some 27231be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // tests may be skipped. 27241be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania if (!internal::String::CStringEquals(filter, kUniversalFilter)) { 27251be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania ColoredPrintf(COLOR_YELLOW, 27261be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania "Note: %s filter = %s\n", GTEST_NAME_, filter); 27271be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania } 27281be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 27291be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania if (internal::ShouldShard(kTestTotalShards, kTestShardIndex, false)) { 273041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot const Int32 shard_index = Int32FromEnvOrDie(kTestShardIndex, -1); 273141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot ColoredPrintf(COLOR_YELLOW, 273241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot "Note: This is test shard %d of %s.\n", 273341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot static_cast<int>(shard_index) + 1, 273441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot internal::posix::GetEnv(kTestTotalShards)); 273541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot } 273641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 273741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot if (GTEST_FLAG(shuffle)) { 27381be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania ColoredPrintf(COLOR_YELLOW, 273941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot "Note: Randomizing tests' orders with a seed of %d .\n", 274041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot unit_test.random_seed()); 27411be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania } 27421be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 27431be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania ColoredPrintf(COLOR_GREEN, "[==========] "); 27441be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania printf("Running %s from %s.\n", 274541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot FormatTestCount(unit_test.test_to_run_count()).c_str(), 274641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot FormatTestCaseCount(unit_test.test_case_to_run_count()).c_str()); 27471be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania fflush(stdout); 27481be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 27491be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 275041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotvoid PrettyUnitTestResultPrinter::OnEnvironmentsSetUpStart( 275141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot const UnitTest& /*unit_test*/) { 27521be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania ColoredPrintf(COLOR_GREEN, "[----------] "); 27531be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania printf("Global test environment set-up.\n"); 27541be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania fflush(stdout); 27551be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 27561be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 275741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotvoid PrettyUnitTestResultPrinter::OnTestCaseStart(const TestCase& test_case) { 275841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot test_case_name_ = test_case.name(); 27591be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania const internal::String counts = 276041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot FormatCountableNoun(test_case.test_to_run_count(), "test", "tests"); 27611be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania ColoredPrintf(COLOR_GREEN, "[----------] "); 27621be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania printf("%s from %s", counts.c_str(), test_case_name_.c_str()); 276341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot if (test_case.type_param() == NULL) { 27641be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania printf("\n"); 27651be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania } else { 276641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot printf(", where TypeParam = %s\n", test_case.type_param()); 27671be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania } 27681be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania fflush(stdout); 27691be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 27701be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 277141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotvoid PrettyUnitTestResultPrinter::OnTestStart(const TestInfo& test_info) { 277241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot ColoredPrintf(COLOR_GREEN, "[ RUN ] "); 277341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot PrintTestName(test_case_name_.c_str(), test_info.name()); 277441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot printf("\n"); 27751be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania fflush(stdout); 27761be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 27771be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 277841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// Called after an assertion failure. 277941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotvoid PrettyUnitTestResultPrinter::OnTestPartResult( 278041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot const TestPartResult& result) { 278141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // If the test part succeeded, we don't need to do anything. 278241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot if (result.type() == TestPartResult::kSuccess) 278341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot return; 278441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 278541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // Print failure message from the assertion (e.g. expected this and got that). 278641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot PrintTestPartResult(result); 27871be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania fflush(stdout); 27881be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 27891be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 279041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotvoid PrettyUnitTestResultPrinter::OnTestEnd(const TestInfo& test_info) { 279141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot if (test_info.result()->Passed()) { 27921be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania ColoredPrintf(COLOR_GREEN, "[ OK ] "); 27931be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania } else { 27941be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania ColoredPrintf(COLOR_RED, "[ FAILED ] "); 27951be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania } 279641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot PrintTestName(test_case_name_.c_str(), test_info.name()); 279741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot if (test_info.result()->Failed()) 279841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot PrintFullTestCommentIfPresent(test_info); 279941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 28001be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania if (GTEST_FLAG(print_time)) { 28011be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania printf(" (%s ms)\n", internal::StreamableToString( 280241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot test_info.result()->elapsed_time()).c_str()); 28031be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania } else { 28041be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania printf("\n"); 28051be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania } 28061be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania fflush(stdout); 28071be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 28081be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 280941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotvoid PrettyUnitTestResultPrinter::OnTestCaseEnd(const TestCase& test_case) { 281041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot if (!GTEST_FLAG(print_time)) return; 28111be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 281241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot test_case_name_ = test_case.name(); 281341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot const internal::String counts = 281441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot FormatCountableNoun(test_case.test_to_run_count(), "test", "tests"); 281541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot ColoredPrintf(COLOR_GREEN, "[----------] "); 281641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot printf("%s from %s (%s ms total)\n\n", 281741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot counts.c_str(), test_case_name_.c_str(), 281841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot internal::StreamableToString(test_case.elapsed_time()).c_str()); 28191be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania fflush(stdout); 28201be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 28211be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 282241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotvoid PrettyUnitTestResultPrinter::OnEnvironmentsTearDownStart( 282341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot const UnitTest& /*unit_test*/) { 28241be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania ColoredPrintf(COLOR_GREEN, "[----------] "); 28251be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania printf("Global test environment tear-down\n"); 28261be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania fflush(stdout); 28271be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 28281be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 28291be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Internal helper for printing the list of failed tests. 283041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotvoid PrettyUnitTestResultPrinter::PrintFailedTests(const UnitTest& unit_test) { 283141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot const int failed_test_count = unit_test.failed_test_count(); 28321be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania if (failed_test_count == 0) { 28331be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania return; 28341be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania } 28351be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 283641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot for (int i = 0; i < unit_test.total_test_case_count(); ++i) { 283741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot const TestCase& test_case = *unit_test.GetTestCase(i); 283841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot if (!test_case.should_run() || (test_case.failed_test_count() == 0)) { 28391be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania continue; 28401be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania } 284141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot for (int j = 0; j < test_case.total_test_count(); ++j) { 284241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot const TestInfo& test_info = *test_case.GetTestInfo(j); 284341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot if (!test_info.should_run() || test_info.result()->Passed()) { 28441be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania continue; 28451be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania } 28461be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania ColoredPrintf(COLOR_RED, "[ FAILED ] "); 284741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot printf("%s.%s", test_case.name(), test_info.name()); 284841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot PrintFullTestCommentIfPresent(test_info); 284941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot printf("\n"); 28501be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania } 28511be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania } 28521be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 28531be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 285441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotvoid PrettyUnitTestResultPrinter::OnTestIterationEnd(const UnitTest& unit_test, 285541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot int /*iteration*/) { 28561be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania ColoredPrintf(COLOR_GREEN, "[==========] "); 28571be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania printf("%s from %s ran.", 285841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot FormatTestCount(unit_test.test_to_run_count()).c_str(), 285941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot FormatTestCaseCount(unit_test.test_case_to_run_count()).c_str()); 28601be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania if (GTEST_FLAG(print_time)) { 28611be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania printf(" (%s ms total)", 286241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot internal::StreamableToString(unit_test.elapsed_time()).c_str()); 28631be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania } 28641be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania printf("\n"); 28651be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania ColoredPrintf(COLOR_GREEN, "[ PASSED ] "); 286641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot printf("%s.\n", FormatTestCount(unit_test.successful_test_count()).c_str()); 28671be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 286841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot int num_failures = unit_test.failed_test_count(); 286941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot if (!unit_test.Passed()) { 287041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot const int failed_test_count = unit_test.failed_test_count(); 28711be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania ColoredPrintf(COLOR_RED, "[ FAILED ] "); 28721be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania printf("%s, listed below:\n", FormatTestCount(failed_test_count).c_str()); 287341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot PrintFailedTests(unit_test); 28741be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania printf("\n%2d FAILED %s\n", num_failures, 28751be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania num_failures == 1 ? "TEST" : "TESTS"); 28761be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania } 28771be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 287841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot int num_disabled = unit_test.disabled_test_count(); 28791be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania if (num_disabled && !GTEST_FLAG(also_run_disabled_tests)) { 28801be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania if (!num_failures) { 28811be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania printf("\n"); // Add a spacer if no FAILURE banner is displayed. 28821be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania } 28831be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania ColoredPrintf(COLOR_YELLOW, 28841be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania " YOU HAVE %d DISABLED %s\n\n", 28851be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania num_disabled, 28861be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania num_disabled == 1 ? "TEST" : "TESTS"); 28871be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania } 28881be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // Ensure that Google Test output is printed before, e.g., heapchecker output. 28891be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania fflush(stdout); 28901be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 28911be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 28921be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// End PrettyUnitTestResultPrinter 28931be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 289441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// class TestEventRepeater 28951be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// 28961be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// This class forwards events to other event listeners. 289741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotclass TestEventRepeater : public TestEventListener { 28981be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania public: 289941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot TestEventRepeater() : forwarding_enabled_(true) {} 290041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot virtual ~TestEventRepeater(); 290141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot void Append(TestEventListener *listener); 290241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot TestEventListener* Release(TestEventListener* listener); 290341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 290441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // Controls whether events will be forwarded to listeners_. Set to false 290541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // in death test child processes. 290641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot bool forwarding_enabled() const { return forwarding_enabled_; } 290741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot void set_forwarding_enabled(bool enable) { forwarding_enabled_ = enable; } 290841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 290941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot virtual void OnTestProgramStart(const UnitTest& unit_test); 291041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot virtual void OnTestIterationStart(const UnitTest& unit_test, int iteration); 291141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot virtual void OnEnvironmentsSetUpStart(const UnitTest& unit_test); 291241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot virtual void OnEnvironmentsSetUpEnd(const UnitTest& unit_test); 291341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot virtual void OnTestCaseStart(const TestCase& test_case); 291441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot virtual void OnTestStart(const TestInfo& test_info); 291541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot virtual void OnTestPartResult(const TestPartResult& result); 291641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot virtual void OnTestEnd(const TestInfo& test_info); 291741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot virtual void OnTestCaseEnd(const TestCase& test_case); 291841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot virtual void OnEnvironmentsTearDownStart(const UnitTest& unit_test); 291941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot virtual void OnEnvironmentsTearDownEnd(const UnitTest& unit_test); 292041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot virtual void OnTestIterationEnd(const UnitTest& unit_test, int iteration); 292141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot virtual void OnTestProgramEnd(const UnitTest& unit_test); 29221be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 29231be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania private: 292441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // Controls whether events will be forwarded to listeners_. Set to false 292541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // in death test child processes. 292641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot bool forwarding_enabled_; 292741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // The list of listeners that receive events. 292841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot std::vector<TestEventListener*> listeners_; 29291be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 293041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot GTEST_DISALLOW_COPY_AND_ASSIGN_(TestEventRepeater); 29311be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}; 29321be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 293341d0579e8de9ef4ff178fc4991043c61a19943f7Brett ChabotTestEventRepeater::~TestEventRepeater() { 293441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot ForEach(listeners_, Delete<TestEventListener>); 29351be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 29361be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 293741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotvoid TestEventRepeater::Append(TestEventListener *listener) { 293841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot listeners_.push_back(listener); 293941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot} 294041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 294141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// TODO(vladl@google.com): Factor the search functionality into Vector::Find. 294241d0579e8de9ef4ff178fc4991043c61a19943f7Brett ChabotTestEventListener* TestEventRepeater::Release(TestEventListener *listener) { 294341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot for (size_t i = 0; i < listeners_.size(); ++i) { 294441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot if (listeners_[i] == listener) { 294541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot listeners_.erase(listeners_.begin() + i); 294641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot return listener; 294741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot } 294841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot } 294941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 295041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot return NULL; 29511be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 29521be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 295341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// Since most methods are very similar, use macros to reduce boilerplate. 295441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// This defines a member that forwards the call to all listeners. 29551be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#define GTEST_REPEATER_METHOD_(Name, Type) \ 295641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotvoid TestEventRepeater::Name(const Type& parameter) { \ 295741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot if (forwarding_enabled_) { \ 295841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot for (size_t i = 0; i < listeners_.size(); i++) { \ 295941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot listeners_[i]->Name(parameter); \ 296041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot } \ 296141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot } \ 296241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot} 296341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// This defines a member that forwards the call to all listeners in reverse 296441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// order. 296541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot#define GTEST_REVERSE_REPEATER_METHOD_(Name, Type) \ 296641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotvoid TestEventRepeater::Name(const Type& parameter) { \ 296741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot if (forwarding_enabled_) { \ 296841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot for (int i = static_cast<int>(listeners_.size()) - 1; i >= 0; i--) { \ 296941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot listeners_[i]->Name(parameter); \ 297041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot } \ 29711be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania } \ 29721be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 29731be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 297441d0579e8de9ef4ff178fc4991043c61a19943f7Brett ChabotGTEST_REPEATER_METHOD_(OnTestProgramStart, UnitTest) 297541d0579e8de9ef4ff178fc4991043c61a19943f7Brett ChabotGTEST_REPEATER_METHOD_(OnEnvironmentsSetUpStart, UnitTest) 29761be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaGTEST_REPEATER_METHOD_(OnTestCaseStart, TestCase) 29771be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaGTEST_REPEATER_METHOD_(OnTestStart, TestInfo) 297841d0579e8de9ef4ff178fc4991043c61a19943f7Brett ChabotGTEST_REPEATER_METHOD_(OnTestPartResult, TestPartResult) 297941d0579e8de9ef4ff178fc4991043c61a19943f7Brett ChabotGTEST_REPEATER_METHOD_(OnEnvironmentsTearDownStart, UnitTest) 298041d0579e8de9ef4ff178fc4991043c61a19943f7Brett ChabotGTEST_REVERSE_REPEATER_METHOD_(OnEnvironmentsSetUpEnd, UnitTest) 298141d0579e8de9ef4ff178fc4991043c61a19943f7Brett ChabotGTEST_REVERSE_REPEATER_METHOD_(OnEnvironmentsTearDownEnd, UnitTest) 298241d0579e8de9ef4ff178fc4991043c61a19943f7Brett ChabotGTEST_REVERSE_REPEATER_METHOD_(OnTestEnd, TestInfo) 298341d0579e8de9ef4ff178fc4991043c61a19943f7Brett ChabotGTEST_REVERSE_REPEATER_METHOD_(OnTestCaseEnd, TestCase) 298441d0579e8de9ef4ff178fc4991043c61a19943f7Brett ChabotGTEST_REVERSE_REPEATER_METHOD_(OnTestProgramEnd, UnitTest) 29851be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 29861be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#undef GTEST_REPEATER_METHOD_ 298741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot#undef GTEST_REVERSE_REPEATER_METHOD_ 29881be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 298941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotvoid TestEventRepeater::OnTestIterationStart(const UnitTest& unit_test, 299041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot int iteration) { 299141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot if (forwarding_enabled_) { 299241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot for (size_t i = 0; i < listeners_.size(); i++) { 299341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot listeners_[i]->OnTestIterationStart(unit_test, iteration); 299441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot } 299541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot } 299641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot} 299741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 299841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotvoid TestEventRepeater::OnTestIterationEnd(const UnitTest& unit_test, 299941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot int iteration) { 300041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot if (forwarding_enabled_) { 300141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot for (int i = static_cast<int>(listeners_.size()) - 1; i >= 0; i--) { 300241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot listeners_[i]->OnTestIterationEnd(unit_test, iteration); 300341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot } 300441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot } 300541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot} 300641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 300741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// End TestEventRepeater 30081be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 30091be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// This class generates an XML output file. 301041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotclass XmlUnitTestResultPrinter : public EmptyTestEventListener { 30111be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania public: 30121be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania explicit XmlUnitTestResultPrinter(const char* output_file); 30131be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 301441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot virtual void OnTestIterationEnd(const UnitTest& unit_test, int iteration); 30151be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 30161be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania private: 30171be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // Is c a whitespace character that is normalized to a space character 30181be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // when it appears in an XML attribute value? 30191be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania static bool IsNormalizableWhitespace(char c) { 30201be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania return c == 0x9 || c == 0xA || c == 0xD; 30211be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania } 30221be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 30231be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // May c appear in a well-formed XML document? 30241be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania static bool IsValidXmlCharacter(char c) { 30251be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania return IsNormalizableWhitespace(c) || c >= 0x20; 30261be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania } 30271be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 30281be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // Returns an XML-escaped copy of the input string str. If 30291be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // is_attribute is true, the text is meant to appear as an attribute 30301be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // value, and normalizable whitespace is preserved by replacing it 30311be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // with character references. 303241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot static String EscapeXml(const char* str, bool is_attribute); 303341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 303441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // Returns the given string with all characters invalid in XML removed. 303541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot static string RemoveInvalidXmlCharacters(const string& str); 30361be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 30371be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // Convenience wrapper around EscapeXml when str is an attribute value. 303841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot static String EscapeXmlAttribute(const char* str) { 30391be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania return EscapeXml(str, true); 30401be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania } 30411be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 30421be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // Convenience wrapper around EscapeXml when str is not an attribute value. 304341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot static String EscapeXmlText(const char* str) { return EscapeXml(str, false); } 30441be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 304541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // Streams an XML CDATA section, escaping invalid CDATA sequences as needed. 304641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot static void OutputXmlCDataSection(::std::ostream* stream, const char* data); 304741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 304841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // Streams an XML representation of a TestInfo object. 304941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot static void OutputXmlTestInfo(::std::ostream* stream, 305041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot const char* test_case_name, 305141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot const TestInfo& test_info); 30521be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 30531be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // Prints an XML representation of a TestCase object 305441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot static void PrintXmlTestCase(FILE* out, const TestCase& test_case); 30551be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 30561be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // Prints an XML summary of unit_test to output stream out. 305741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot static void PrintXmlUnitTest(FILE* out, const UnitTest& unit_test); 30581be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 30591be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // Produces a string representing the test properties in a result as space 30601be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // delimited XML attributes based on the property key="value" pairs. 30611be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // When the String is not empty, it includes a space at the beginning, 30621be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // to delimit this attribute from prior attributes. 306341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot static String TestPropertiesAsXmlAttributes(const TestResult& result); 30641be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 30651be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // The output file. 306641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot const String output_file_; 30671be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 30681be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania GTEST_DISALLOW_COPY_AND_ASSIGN_(XmlUnitTestResultPrinter); 30691be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}; 30701be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 30711be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Creates a new XmlUnitTestResultPrinter. 30721be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaXmlUnitTestResultPrinter::XmlUnitTestResultPrinter(const char* output_file) 30731be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania : output_file_(output_file) { 30741be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania if (output_file_.c_str() == NULL || output_file_.empty()) { 30751be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania fprintf(stderr, "XML output file may not be null\n"); 30761be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania fflush(stderr); 30771be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania exit(EXIT_FAILURE); 30781be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania } 30791be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 30801be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 30811be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Called after the unit test ends. 308241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotvoid XmlUnitTestResultPrinter::OnTestIterationEnd(const UnitTest& unit_test, 308341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot int /*iteration*/) { 30841be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania FILE* xmlout = NULL; 308541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot FilePath output_file(output_file_); 308641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot FilePath output_dir(output_file.RemoveFileName()); 30871be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 30881be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania if (output_dir.CreateDirectoriesRecursively()) { 308941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot xmlout = posix::FOpen(output_file_.c_str(), "w"); 30901be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania } 30911be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania if (xmlout == NULL) { 30921be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // TODO(wan): report the reason of the failure. 30931be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // 30941be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // We don't do it for now as: 30951be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // 30961be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // 1. There is no urgent need for it. 30971be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // 2. It's a bit involved to make the errno variable thread-safe on 30981be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // all three operating systems (Linux, Windows, and Mac OS). 30991be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // 3. To interpret the meaning of errno in a thread-safe way, 31001be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // we need the strerror_r() function, which is not available on 31011be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // Windows. 31021be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania fprintf(stderr, 31031be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania "Unable to open file \"%s\"\n", 31041be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania output_file_.c_str()); 31051be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania fflush(stderr); 31061be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania exit(EXIT_FAILURE); 31071be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania } 31081be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania PrintXmlUnitTest(xmlout, unit_test); 31091be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania fclose(xmlout); 31101be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 31111be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 31121be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Returns an XML-escaped copy of the input string str. If is_attribute 31131be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// is true, the text is meant to appear as an attribute value, and 31141be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// normalizable whitespace is preserved by replacing it with character 31151be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// references. 31161be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// 31171be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Invalid XML characters in str, if any, are stripped from the output. 31181be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// It is expected that most, if not all, of the text processed by this 31191be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// module will consist of ordinary English text. 31201be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// If this module is ever modified to produce version 1.1 XML output, 31211be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// most invalid characters can be retained using character references. 31221be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// TODO(wan): It might be nice to have a minimally invasive, human-readable 31231be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// escaping scheme for invalid characters, rather than dropping them. 312441d0579e8de9ef4ff178fc4991043c61a19943f7Brett ChabotString XmlUnitTestResultPrinter::EscapeXml(const char* str, bool is_attribute) { 31251be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania Message m; 31261be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 31271be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania if (str != NULL) { 31281be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania for (const char* src = str; *src; ++src) { 31291be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania switch (*src) { 31301be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania case '<': 31311be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania m << "<"; 31321be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania break; 31331be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania case '>': 31341be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania m << ">"; 31351be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania break; 31361be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania case '&': 31371be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania m << "&"; 31381be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania break; 31391be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania case '\'': 31401be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania if (is_attribute) 31411be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania m << "'"; 31421be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania else 31431be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania m << '\''; 31441be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania break; 31451be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania case '"': 31461be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania if (is_attribute) 31471be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania m << """; 31481be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania else 31491be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania m << '"'; 31501be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania break; 31511be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania default: 31521be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania if (IsValidXmlCharacter(*src)) { 31531be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania if (is_attribute && IsNormalizableWhitespace(*src)) 315441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot m << String::Format("&#x%02X;", unsigned(*src)); 31551be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania else 31561be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania m << *src; 31571be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania } 31581be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania break; 31591be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania } 31601be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania } 31611be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania } 31621be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 31631be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania return m.GetString(); 31641be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 31651be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 316641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// Returns the given string with all characters invalid in XML removed. 316741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// Currently invalid characters are dropped from the string. An 316841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// alternative is to replace them with certain characters such as . or ?. 316941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotstring XmlUnitTestResultPrinter::RemoveInvalidXmlCharacters(const string& str) { 317041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot string output; 317141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot output.reserve(str.size()); 317241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot for (string::const_iterator it = str.begin(); it != str.end(); ++it) 317341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot if (IsValidXmlCharacter(*it)) 317441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot output.push_back(*it); 317541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 317641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot return output; 317741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot} 31781be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 31791be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// The following routines generate an XML representation of a UnitTest 31801be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// object. 31811be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// 31821be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// This is how Google Test concepts map to the DTD: 31831be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// 318441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// <testsuites name="AllTests"> <-- corresponds to a UnitTest object 31851be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// <testsuite name="testcase-name"> <-- corresponds to a TestCase object 31861be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// <testcase name="test-name"> <-- corresponds to a TestInfo object 31871be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// <failure message="...">...</failure> 31881be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// <failure message="...">...</failure> 31891be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// <failure message="...">...</failure> 31901be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// <-- individual assertion failures 31911be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// </testcase> 31921be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// </testsuite> 319341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// </testsuites> 31941be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 319541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// Formats the given time in milliseconds as seconds. 319641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotstd::string FormatTimeInMillisAsSeconds(TimeInMillis ms) { 319741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot ::std::stringstream ss; 319841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot ss << ms/1000.0; 319941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot return ss.str(); 32001be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 32011be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 320241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// Streams an XML CDATA section, escaping invalid CDATA sequences as needed. 320341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotvoid XmlUnitTestResultPrinter::OutputXmlCDataSection(::std::ostream* stream, 320441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot const char* data) { 320541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot const char* segment = data; 320641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot *stream << "<![CDATA["; 320741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot for (;;) { 320841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot const char* const next_segment = strstr(segment, "]]>"); 320941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot if (next_segment != NULL) { 321041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot stream->write( 321141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot segment, static_cast<std::streamsize>(next_segment - segment)); 321241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot *stream << "]]>]]><![CDATA["; 321341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot segment = next_segment + strlen("]]>"); 321441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot } else { 321541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot *stream << segment; 321641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot break; 321741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot } 321841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot } 321941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot *stream << "]]>"; 322041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot} 32211be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 32221be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Prints an XML representation of a TestInfo object. 32231be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// TODO(wan): There is also value in printing properties with the plain printer. 322441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotvoid XmlUnitTestResultPrinter::OutputXmlTestInfo(::std::ostream* stream, 322541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot const char* test_case_name, 322641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot const TestInfo& test_info) { 322741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot const TestResult& result = *test_info.result(); 322841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot *stream << " <testcase name=\"" 322941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot << EscapeXmlAttribute(test_info.name()).c_str() << "\""; 323041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 323141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot if (test_info.value_param() != NULL) { 323241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot *stream << " value_param=\"" << EscapeXmlAttribute(test_info.value_param()) 323341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot << "\""; 323441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot } 323541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot if (test_info.type_param() != NULL) { 323641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot *stream << " type_param=\"" << EscapeXmlAttribute(test_info.type_param()) 323741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot << "\""; 323841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot } 323941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 324041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot *stream << " status=\"" 324141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot << (test_info.should_run() ? "run" : "notrun") 324241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot << "\" time=\"" 324341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot << FormatTimeInMillisAsSeconds(result.elapsed_time()) 324441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot << "\" classname=\"" << EscapeXmlAttribute(test_case_name).c_str() 324541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot << "\"" << TestPropertiesAsXmlAttributes(result).c_str(); 32461be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 32471be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania int failures = 0; 324841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot for (int i = 0; i < result.total_part_count(); ++i) { 324941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot const TestPartResult& part = result.GetTestPartResult(i); 32501be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania if (part.failed()) { 32511be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania if (++failures == 1) 325241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot *stream << ">\n"; 325341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot *stream << " <failure message=\"" 325441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot << EscapeXmlAttribute(part.summary()).c_str() 325541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot << "\" type=\"\">"; 325641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot const string location = internal::FormatCompilerIndependentFileLocation( 325741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot part.file_name(), part.line_number()); 325841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot const string message = location + "\n" + part.message(); 325941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot OutputXmlCDataSection(stream, 326041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot RemoveInvalidXmlCharacters(message).c_str()); 326141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot *stream << "</failure>\n"; 32621be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania } 32631be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania } 32641be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 32651be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania if (failures == 0) 326641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot *stream << " />\n"; 32671be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania else 326841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot *stream << " </testcase>\n"; 32691be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 32701be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 32711be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Prints an XML representation of a TestCase object 32721be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniavoid XmlUnitTestResultPrinter::PrintXmlTestCase(FILE* out, 327341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot const TestCase& test_case) { 32741be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania fprintf(out, 32751be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania " <testsuite name=\"%s\" tests=\"%d\" failures=\"%d\" " 32761be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania "disabled=\"%d\" ", 327741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot EscapeXmlAttribute(test_case.name()).c_str(), 327841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot test_case.total_test_count(), 327941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot test_case.failed_test_count(), 328041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot test_case.disabled_test_count()); 32811be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania fprintf(out, 32821be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania "errors=\"0\" time=\"%s\">\n", 328341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot FormatTimeInMillisAsSeconds(test_case.elapsed_time()).c_str()); 328441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot for (int i = 0; i < test_case.total_test_count(); ++i) { 328541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot ::std::stringstream stream; 328641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot OutputXmlTestInfo(&stream, test_case.name(), *test_case.GetTestInfo(i)); 328741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot fprintf(out, "%s", StringStreamToString(&stream).c_str()); 32881be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania } 32891be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania fprintf(out, " </testsuite>\n"); 32901be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 32911be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 32921be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Prints an XML summary of unit_test to output stream out. 32931be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniavoid XmlUnitTestResultPrinter::PrintXmlUnitTest(FILE* out, 329441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot const UnitTest& unit_test) { 32951be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania fprintf(out, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"); 32961be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania fprintf(out, 329741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot "<testsuites tests=\"%d\" failures=\"%d\" disabled=\"%d\" " 32981be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania "errors=\"0\" time=\"%s\" ", 329941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot unit_test.total_test_count(), 330041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot unit_test.failed_test_count(), 330141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot unit_test.disabled_test_count(), 330241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot FormatTimeInMillisAsSeconds(unit_test.elapsed_time()).c_str()); 330341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot if (GTEST_FLAG(shuffle)) { 330441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot fprintf(out, "random_seed=\"%d\" ", unit_test.random_seed()); 33051be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania } 330641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot fprintf(out, "name=\"AllTests\">\n"); 330741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot for (int i = 0; i < unit_test.total_test_case_count(); ++i) 330841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot PrintXmlTestCase(out, *unit_test.GetTestCase(i)); 330941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot fprintf(out, "</testsuites>\n"); 33101be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 33111be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 33121be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Produces a string representing the test properties in a result as space 33131be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// delimited XML attributes based on the property key="value" pairs. 331441d0579e8de9ef4ff178fc4991043c61a19943f7Brett ChabotString XmlUnitTestResultPrinter::TestPropertiesAsXmlAttributes( 331541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot const TestResult& result) { 33161be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania Message attributes; 331741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot for (int i = 0; i < result.test_property_count(); ++i) { 331841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot const TestProperty& property = result.GetTestProperty(i); 33191be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania attributes << " " << property.key() << "=" 33201be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania << "\"" << EscapeXmlAttribute(property.value()) << "\""; 33211be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania } 33221be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania return attributes.GetString(); 33231be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 33241be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 33251be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// End XmlUnitTestResultPrinter 33261be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 332741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot#if GTEST_CAN_STREAM_RESULTS_ 332841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 332941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// Streams test results to the given port on the given host machine. 333041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotclass StreamingListener : public EmptyTestEventListener { 333141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot public: 333241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // Escapes '=', '&', '%', and '\n' characters in str as "%xx". 333341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot static string UrlEncode(const char* str); 333441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 333541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot StreamingListener(const string& host, const string& port) 333641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot : sockfd_(-1), host_name_(host), port_num_(port) { 333741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot MakeConnection(); 333841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot Send("gtest_streaming_protocol_version=1.0\n"); 333941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot } 334041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 334141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot virtual ~StreamingListener() { 334241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot if (sockfd_ != -1) 334341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot CloseConnection(); 334441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot } 334541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 334641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot void OnTestProgramStart(const UnitTest& /* unit_test */) { 334741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot Send("event=TestProgramStart\n"); 334841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot } 334941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 335041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot void OnTestProgramEnd(const UnitTest& unit_test) { 335141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // Note that Google Test current only report elapsed time for each 335241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // test iteration, not for the entire test program. 335341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot Send(String::Format("event=TestProgramEnd&passed=%d\n", 335441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot unit_test.Passed())); 335541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 335641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // Notify the streaming server to stop. 335741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot CloseConnection(); 335841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot } 335941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 336041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot void OnTestIterationStart(const UnitTest& /* unit_test */, int iteration) { 336141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot Send(String::Format("event=TestIterationStart&iteration=%d\n", 336241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot iteration)); 336341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot } 336441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 336541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot void OnTestIterationEnd(const UnitTest& unit_test, int /* iteration */) { 336641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot Send(String::Format("event=TestIterationEnd&passed=%d&elapsed_time=%sms\n", 336741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot unit_test.Passed(), 336841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot StreamableToString(unit_test.elapsed_time()).c_str())); 336941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot } 337041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 337141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot void OnTestCaseStart(const TestCase& test_case) { 337241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot Send(String::Format("event=TestCaseStart&name=%s\n", test_case.name())); 337341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot } 337441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 337541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot void OnTestCaseEnd(const TestCase& test_case) { 337641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot Send(String::Format("event=TestCaseEnd&passed=%d&elapsed_time=%sms\n", 337741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot test_case.Passed(), 337841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot StreamableToString(test_case.elapsed_time()).c_str())); 337941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot } 338041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 338141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot void OnTestStart(const TestInfo& test_info) { 338241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot Send(String::Format("event=TestStart&name=%s\n", test_info.name())); 338341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot } 338441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 338541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot void OnTestEnd(const TestInfo& test_info) { 338641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot Send(String::Format( 338741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot "event=TestEnd&passed=%d&elapsed_time=%sms\n", 338841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot (test_info.result())->Passed(), 338941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot StreamableToString((test_info.result())->elapsed_time()).c_str())); 339041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot } 339141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 339241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot void OnTestPartResult(const TestPartResult& test_part_result) { 339341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot const char* file_name = test_part_result.file_name(); 339441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot if (file_name == NULL) 339541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot file_name = ""; 339641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot Send(String::Format("event=TestPartResult&file=%s&line=%d&message=", 339741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot UrlEncode(file_name).c_str(), 339841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot test_part_result.line_number())); 339941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot Send(UrlEncode(test_part_result.message()) + "\n"); 340041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot } 340141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 340241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot private: 340341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // Creates a client socket and connects to the server. 340441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot void MakeConnection(); 340541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 340641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // Closes the socket. 340741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot void CloseConnection() { 340841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot GTEST_CHECK_(sockfd_ != -1) 340941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot << "CloseConnection() can be called only when there is a connection."; 341041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 341141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot close(sockfd_); 341241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot sockfd_ = -1; 341341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot } 341441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 341541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // Sends a string to the socket. 341641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot void Send(const string& message) { 341741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot GTEST_CHECK_(sockfd_ != -1) 341841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot << "Send() can be called only when there is a connection."; 341941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 342041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot const int len = static_cast<int>(message.length()); 342141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot if (write(sockfd_, message.c_str(), len) != len) { 342241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot GTEST_LOG_(WARNING) 342341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot << "stream_result_to: failed to stream to " 342441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot << host_name_ << ":" << port_num_; 342541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot } 342641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot } 342741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 342841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot int sockfd_; // socket file descriptor 342941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot const string host_name_; 343041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot const string port_num_; 343141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 343241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot GTEST_DISALLOW_COPY_AND_ASSIGN_(StreamingListener); 343341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot}; // class StreamingListener 343441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 343541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// Checks if str contains '=', '&', '%' or '\n' characters. If yes, 343641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// replaces them by "%xx" where xx is their hexadecimal value. For 343741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// example, replaces "=" with "%3D". This algorithm is O(strlen(str)) 343841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// in both time and space -- important as the input str may contain an 343941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// arbitrarily long test failure message and stack trace. 344041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotstring StreamingListener::UrlEncode(const char* str) { 344141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot string result; 344241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot result.reserve(strlen(str) + 1); 344341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot for (char ch = *str; ch != '\0'; ch = *++str) { 344441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot switch (ch) { 344541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot case '%': 344641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot case '=': 344741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot case '&': 344841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot case '\n': 344941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot result.append(String::Format("%%%02x", static_cast<unsigned char>(ch))); 345041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot break; 345141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot default: 345241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot result.push_back(ch); 345341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot break; 345441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot } 345541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot } 345641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot return result; 345741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot} 345841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 345941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotvoid StreamingListener::MakeConnection() { 346041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot GTEST_CHECK_(sockfd_ == -1) 346141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot << "MakeConnection() can't be called when there is already a connection."; 346241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 346341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot addrinfo hints; 346441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot memset(&hints, 0, sizeof(hints)); 346541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot hints.ai_family = AF_UNSPEC; // To allow both IPv4 and IPv6 addresses. 346641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot hints.ai_socktype = SOCK_STREAM; 346741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot addrinfo* servinfo = NULL; 346841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 346941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // Use the getaddrinfo() to get a linked list of IP addresses for 347041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // the given host name. 347141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot const int error_num = getaddrinfo( 347241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot host_name_.c_str(), port_num_.c_str(), &hints, &servinfo); 347341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot if (error_num != 0) { 347441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot GTEST_LOG_(WARNING) << "stream_result_to: getaddrinfo() failed: " 347541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot << gai_strerror(error_num); 347641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot } 347741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 347841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // Loop through all the results and connect to the first we can. 347941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot for (addrinfo* cur_addr = servinfo; sockfd_ == -1 && cur_addr != NULL; 348041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot cur_addr = cur_addr->ai_next) { 348141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot sockfd_ = socket( 348241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot cur_addr->ai_family, cur_addr->ai_socktype, cur_addr->ai_protocol); 348341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot if (sockfd_ != -1) { 348441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // Connect the client socket to the server socket. 348541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot if (connect(sockfd_, cur_addr->ai_addr, cur_addr->ai_addrlen) == -1) { 348641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot close(sockfd_); 348741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot sockfd_ = -1; 348841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot } 348941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot } 349041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot } 349141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 349241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot freeaddrinfo(servinfo); // all done with this structure 349341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 349441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot if (sockfd_ == -1) { 349541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot GTEST_LOG_(WARNING) << "stream_result_to: failed to connect to " 349641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot << host_name_ << ":" << port_num_; 349741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot } 349841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot} 349941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 350041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// End of class Streaming Listener 350141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot#endif // GTEST_CAN_STREAM_RESULTS__ 35021be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 35031be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Class ScopedTrace 35041be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 35051be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Pushes the given source file location and message onto a per-thread 35061be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// trace stack maintained by Google Test. 35071be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// L < UnitTest::mutex_ 35081be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaScopedTrace::ScopedTrace(const char* file, int line, const Message& message) { 35091be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania TraceInfo trace; 35101be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania trace.file = file; 35111be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania trace.line = line; 35121be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania trace.message = message.GetString(); 35131be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 35141be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania UnitTest::GetInstance()->PushGTestTrace(trace); 35151be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 35161be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 35171be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Pops the info pushed by the c'tor. 35181be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// L < UnitTest::mutex_ 35191be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaScopedTrace::~ScopedTrace() { 35201be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania UnitTest::GetInstance()->PopGTestTrace(); 35211be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 35221be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 35231be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 35241be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// class OsStackTraceGetter 35251be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 35261be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Returns the current OS stack trace as a String. Parameters: 35271be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// 35281be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// max_depth - the maximum number of stack frames to be included 35291be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// in the trace. 35301be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// skip_count - the number of top frames to be skipped; doesn't count 35311be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// against max_depth. 35321be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// 35331be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// L < mutex_ 35341be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// We use "L < mutex_" to denote that the function may acquire mutex_. 35351be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaString OsStackTraceGetter::CurrentStackTrace(int, int) { 35361be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania return String(""); 35371be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 35381be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 35391be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// L < mutex_ 35401be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniavoid OsStackTraceGetter::UponLeavingGTest() { 35411be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 35421be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 35431be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniaconst char* const 35441be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaOsStackTraceGetter::kElidedFramesMarker = 35451be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania "... " GTEST_NAME_ " internal frames ..."; 35461be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 35471be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} // namespace internal 35481be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 354941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// class TestEventListeners 355041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 355141d0579e8de9ef4ff178fc4991043c61a19943f7Brett ChabotTestEventListeners::TestEventListeners() 355241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot : repeater_(new internal::TestEventRepeater()), 355341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot default_result_printer_(NULL), 355441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot default_xml_generator_(NULL) { 355541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot} 355641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 355741d0579e8de9ef4ff178fc4991043c61a19943f7Brett ChabotTestEventListeners::~TestEventListeners() { delete repeater_; } 355841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 355941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// Returns the standard listener responsible for the default console 356041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// output. Can be removed from the listeners list to shut down default 356141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// console output. Note that removing this object from the listener list 356241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// with Release transfers its ownership to the user. 356341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotvoid TestEventListeners::Append(TestEventListener* listener) { 356441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot repeater_->Append(listener); 356541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot} 356641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 356741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// Removes the given event listener from the list and returns it. It then 356841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// becomes the caller's responsibility to delete the listener. Returns 356941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// NULL if the listener is not found in the list. 357041d0579e8de9ef4ff178fc4991043c61a19943f7Brett ChabotTestEventListener* TestEventListeners::Release(TestEventListener* listener) { 357141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot if (listener == default_result_printer_) 357241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot default_result_printer_ = NULL; 357341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot else if (listener == default_xml_generator_) 357441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot default_xml_generator_ = NULL; 357541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot return repeater_->Release(listener); 357641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot} 357741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 357841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// Returns repeater that broadcasts the TestEventListener events to all 357941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// subscribers. 358041d0579e8de9ef4ff178fc4991043c61a19943f7Brett ChabotTestEventListener* TestEventListeners::repeater() { return repeater_; } 358141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 358241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// Sets the default_result_printer attribute to the provided listener. 358341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// The listener is also added to the listener list and previous 358441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// default_result_printer is removed from it and deleted. The listener can 358541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// also be NULL in which case it will not be added to the list. Does 358641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// nothing if the previous and the current listener objects are the same. 358741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotvoid TestEventListeners::SetDefaultResultPrinter(TestEventListener* listener) { 358841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot if (default_result_printer_ != listener) { 358941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // It is an error to pass this method a listener that is already in the 359041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // list. 359141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot delete Release(default_result_printer_); 359241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot default_result_printer_ = listener; 359341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot if (listener != NULL) 359441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot Append(listener); 359541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot } 359641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot} 359741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 359841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// Sets the default_xml_generator attribute to the provided listener. The 359941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// listener is also added to the listener list and previous 360041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// default_xml_generator is removed from it and deleted. The listener can 360141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// also be NULL in which case it will not be added to the list. Does 360241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// nothing if the previous and the current listener objects are the same. 360341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotvoid TestEventListeners::SetDefaultXmlGenerator(TestEventListener* listener) { 360441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot if (default_xml_generator_ != listener) { 360541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // It is an error to pass this method a listener that is already in the 360641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // list. 360741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot delete Release(default_xml_generator_); 360841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot default_xml_generator_ = listener; 360941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot if (listener != NULL) 361041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot Append(listener); 361141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot } 361241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot} 361341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 361441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// Controls whether events will be forwarded by the repeater to the 361541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// listeners in the list. 361641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotbool TestEventListeners::EventForwardingEnabled() const { 361741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot return repeater_->forwarding_enabled(); 361841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot} 361941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 362041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotvoid TestEventListeners::SuppressEventForwarding() { 362141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot repeater_->set_forwarding_enabled(false); 362241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot} 362341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 36241be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// class UnitTest 36251be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 36261be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Gets the singleton UnitTest object. The first time this method is 36271be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// called, a UnitTest object is constructed and returned. Consecutive 36281be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// calls will return the same object. 36291be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// 36301be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// We don't protect this under mutex_ as a user is not supposed to 36311be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// call this before main() starts, from which point on the return 36321be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// value will never change. 36331be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaUnitTest * UnitTest::GetInstance() { 36341be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // When compiled with MSVC 7.1 in optimized mode, destroying the 36351be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // UnitTest object upon exiting the program messes up the exit code, 36361be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // causing successful tests to appear failed. We have to use a 36371be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // different implementation in this case to bypass the compiler bug. 36381be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // This implementation makes the compiler happy, at the cost of 36391be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // leaking the UnitTest object. 364041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 364141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // CodeGear C++Builder insists on a public destructor for the 364241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // default implementation. Use this implementation to keep good OO 364341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // design with private destructor. 364441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 364541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot#if (_MSC_VER == 1310 && !defined(_DEBUG)) || defined(__BORLANDC__) 36461be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania static UnitTest* const instance = new UnitTest; 36471be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania return instance; 36481be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#else 36491be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania static UnitTest instance; 36501be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania return &instance; 365141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot#endif // (_MSC_VER == 1310 && !defined(_DEBUG)) || defined(__BORLANDC__) 365241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot} 365341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 365441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// Gets the number of successful test cases. 365541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotint UnitTest::successful_test_case_count() const { 365641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot return impl()->successful_test_case_count(); 365741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot} 365841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 365941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// Gets the number of failed test cases. 366041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotint UnitTest::failed_test_case_count() const { 366141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot return impl()->failed_test_case_count(); 366241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot} 366341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 366441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// Gets the number of all test cases. 366541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotint UnitTest::total_test_case_count() const { 366641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot return impl()->total_test_case_count(); 366741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot} 366841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 366941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// Gets the number of all test cases that contain at least one test 367041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// that should run. 367141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotint UnitTest::test_case_to_run_count() const { 367241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot return impl()->test_case_to_run_count(); 367341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot} 367441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 367541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// Gets the number of successful tests. 367641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotint UnitTest::successful_test_count() const { 367741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot return impl()->successful_test_count(); 367841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot} 367941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 368041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// Gets the number of failed tests. 368141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotint UnitTest::failed_test_count() const { return impl()->failed_test_count(); } 368241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 368341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// Gets the number of disabled tests. 368441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotint UnitTest::disabled_test_count() const { 368541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot return impl()->disabled_test_count(); 368641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot} 368741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 368841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// Gets the number of all tests. 368941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotint UnitTest::total_test_count() const { return impl()->total_test_count(); } 369041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 369141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// Gets the number of tests that should run. 369241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotint UnitTest::test_to_run_count() const { return impl()->test_to_run_count(); } 369341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 369441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// Gets the elapsed time, in milliseconds. 369541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotinternal::TimeInMillis UnitTest::elapsed_time() const { 369641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot return impl()->elapsed_time(); 369741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot} 369841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 369941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// Returns true iff the unit test passed (i.e. all test cases passed). 370041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotbool UnitTest::Passed() const { return impl()->Passed(); } 370141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 370241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// Returns true iff the unit test failed (i.e. some test case failed 370341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// or something outside of all tests failed). 370441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotbool UnitTest::Failed() const { return impl()->Failed(); } 370541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 370641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// Gets the i-th test case among all the test cases. i can range from 0 to 370741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// total_test_case_count() - 1. If i is not in that range, returns NULL. 370841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotconst TestCase* UnitTest::GetTestCase(int i) const { 370941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot return impl()->GetTestCase(i); 371041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot} 371141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 371241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// Gets the i-th test case among all the test cases. i can range from 0 to 371341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// total_test_case_count() - 1. If i is not in that range, returns NULL. 371441d0579e8de9ef4ff178fc4991043c61a19943f7Brett ChabotTestCase* UnitTest::GetMutableTestCase(int i) { 371541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot return impl()->GetMutableTestCase(i); 371641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot} 371741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 371841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// Returns the list of event listeners that can be used to track events 371941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// inside Google Test. 372041d0579e8de9ef4ff178fc4991043c61a19943f7Brett ChabotTestEventListeners& UnitTest::listeners() { 372141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot return *impl()->listeners(); 37221be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 37231be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 37241be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Registers and returns a global test environment. When a test 37251be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// program is run, all global test environments will be set-up in the 37261be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// order they were registered. After all tests in the program have 37271be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// finished, all global test environments will be torn-down in the 37281be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// *reverse* order they were registered. 37291be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// 37301be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// The UnitTest object takes ownership of the given environment. 37311be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// 37321be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// We don't protect this under mutex_, as we only support calling it 37331be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// from the main thread. 37341be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaEnvironment* UnitTest::AddEnvironment(Environment* env) { 37351be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania if (env == NULL) { 37361be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania return NULL; 37371be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania } 37381be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 373941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot impl_->environments().push_back(env); 37401be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania return env; 37411be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 37421be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 37431be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Adds a TestPartResult to the current TestResult object. All Google Test 37441be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// assertion macros (e.g. ASSERT_TRUE, EXPECT_EQ, etc) eventually call 37451be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// this to report their results. The user code should use the 37461be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// assertion macros instead of calling this directly. 37471be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// L < mutex_ 374841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotvoid UnitTest::AddTestPartResult(TestPartResult::Type result_type, 37491be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania const char* file_name, 37501be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania int line_number, 37511be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania const internal::String& message, 37521be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania const internal::String& os_stack_trace) { 37531be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania Message msg; 37541be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania msg << message; 37551be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 37561be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania internal::MutexLock lock(&mutex_); 375741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot if (impl_->gtest_trace_stack().size() > 0) { 37581be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania msg << "\n" << GTEST_NAME_ << " trace:"; 37591be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 376041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot for (int i = static_cast<int>(impl_->gtest_trace_stack().size()); 376141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot i > 0; --i) { 376241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot const internal::TraceInfo& trace = impl_->gtest_trace_stack()[i - 1]; 376341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot msg << "\n" << internal::FormatFileLocation(trace.file, trace.line) 376441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot << " " << trace.message; 37651be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania } 37661be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania } 37671be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 37681be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania if (os_stack_trace.c_str() != NULL && !os_stack_trace.empty()) { 37691be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania msg << internal::kStackTraceMarker << os_stack_trace; 37701be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania } 37711be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 37721be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania const TestPartResult result = 37731be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania TestPartResult(result_type, file_name, line_number, 37741be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania msg.GetString().c_str()); 37751be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania impl_->GetTestPartResultReporterForCurrentThread()-> 37761be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania ReportTestPartResult(result); 37771be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 377841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot if (result_type != TestPartResult::kSuccess) { 377941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // gtest_break_on_failure takes precedence over 378041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // gtest_throw_on_failure. This allows a user to set the latter 37811be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // in the code (perhaps in order to use Google Test assertions 37821be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // with another testing framework) and specify the former on the 37831be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // command line for debugging. 37841be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania if (GTEST_FLAG(break_on_failure)) { 378541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot#if GTEST_OS_WINDOWS 378641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // Using DebugBreak on Windows allows gtest to still break into a debugger 378741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // when a failure happens and both the --gtest_break_on_failure and 378841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // the --gtest_catch_exceptions flags are specified. 378941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot DebugBreak(); 379041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot#else 379141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // Dereference NULL through a volatile pointer to prevent the compiler 379241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // from removing. We use this rather than abort() or __builtin_trap() for 379341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // portability: Symbian doesn't implement abort() well, and some debuggers 379441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // don't correctly trap abort(). 379541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot *static_cast<volatile int*>(NULL) = 1; 379641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot#endif // GTEST_OS_WINDOWS 37971be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania } else if (GTEST_FLAG(throw_on_failure)) { 37981be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#if GTEST_HAS_EXCEPTIONS 37991be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania throw GoogleTestFailureException(result); 38001be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#else 38011be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // We cannot call abort() as it generates a pop-up in debug mode 38021be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // that cannot be suppressed in VC 7.1 or below. 38031be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania exit(1); 38041be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#endif 38051be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania } 38061be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania } 38071be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 38081be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 38091be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Creates and adds a property to the current TestResult. If a property matching 38101be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// the supplied value already exists, updates its value instead. 38111be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniavoid UnitTest::RecordPropertyForCurrentTest(const char* key, 38121be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania const char* value) { 381341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot const TestProperty test_property(key, value); 38141be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania impl_->current_test_result()->RecordProperty(test_property); 38151be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 38161be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 38171be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Runs all tests in this UnitTest object and prints the result. 38181be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Returns 0 if successful, or 1 otherwise. 38191be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// 38201be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// We don't protect this under mutex_, as we only support calling it 38211be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// from the main thread. 38221be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniaint UnitTest::Run() { 382341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // Captures the value of GTEST_FLAG(catch_exceptions). This value will be 382441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // used for the duration of the program. 382541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot impl()->set_catch_exceptions(GTEST_FLAG(catch_exceptions)); 38261be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 382741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot#if GTEST_HAS_SEH 38281be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania const bool in_death_test_child_process = 382941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot internal::GTEST_FLAG(internal_run_death_test).length() > 0; 38301be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 38311be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // Either the user wants Google Test to catch exceptions thrown by the 38321be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // tests or this is executing in the context of death test child 38331be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // process. In either case the user does not want to see pop-up dialogs 383441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // about crashes - they are expected. 383541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot if (impl()->catch_exceptions() || in_death_test_child_process) { 383641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 383741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot# if !GTEST_OS_WINDOWS_MOBILE 38381be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // SetErrorMode doesn't exist on CE. 38391be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania SetErrorMode(SEM_FAILCRITICALERRORS | SEM_NOALIGNMENTFAULTEXCEPT | 38401be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania SEM_NOGPFAULTERRORBOX | SEM_NOOPENFILEERRORBOX); 384141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot# endif // !GTEST_OS_WINDOWS_MOBILE 38421be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 384341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot# if (defined(_MSC_VER) || GTEST_OS_WINDOWS_MINGW) && !GTEST_OS_WINDOWS_MOBILE 38441be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // Death test children can be terminated with _abort(). On Windows, 38451be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // _abort() can show a dialog with a warning message. This forces the 38461be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // abort message to go to stderr instead. 38471be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania _set_error_mode(_OUT_TO_STDERR); 384841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot# endif 38491be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 385041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot# if _MSC_VER >= 1400 && !GTEST_OS_WINDOWS_MOBILE 38511be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // In the debug version, Visual Studio pops up a separate dialog 38521be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // offering a choice to debug the aborted program. We need to suppress 38531be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // this dialog or it will pop up for every EXPECT/ASSERT_DEATH statement 38541be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // executed. Google Test will notify the user of any unexpected 38551be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // failure via stderr. 385641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // 38571be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // VC++ doesn't define _set_abort_behavior() prior to the version 8.0. 38581be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // Users of prior VC versions shall suffer the agony and pain of 38591be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // clicking through the countless debug dialogs. 38601be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // TODO(vladl@google.com): find a way to suppress the abort dialog() in the 38611be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // debug mode when compiled with VC 7.1 or lower. 38621be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania if (!GTEST_FLAG(break_on_failure)) 38631be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania _set_abort_behavior( 38641be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 0x0, // Clear the following flags: 38651be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania _WRITE_ABORT_MSG | _CALL_REPORTFAULT); // pop-up window, core dump. 386641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot# endif 38671be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 38681be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania } 386941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot#endif // GTEST_HAS_SEH 38701be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 387141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot return internal::HandleExceptionsInMethodIfSupported( 387241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot impl(), 387341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot &internal::UnitTestImpl::RunAllTests, 387441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot "auxiliary test code (environments or event listeners)") ? 0 : 1; 38751be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 38761be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 38771be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Returns the working directory when the first TEST() or TEST_F() was 38781be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// executed. 38791be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniaconst char* UnitTest::original_working_dir() const { 38801be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania return impl_->original_working_dir_.c_str(); 38811be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 38821be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 38831be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Returns the TestCase object for the test that's currently running, 38841be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// or NULL if no test is running. 38851be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// L < mutex_ 38861be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniaconst TestCase* UnitTest::current_test_case() const { 38871be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania internal::MutexLock lock(&mutex_); 38881be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania return impl_->current_test_case(); 38891be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 38901be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 38911be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Returns the TestInfo object for the test that's currently running, 38921be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// or NULL if no test is running. 38931be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// L < mutex_ 38941be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniaconst TestInfo* UnitTest::current_test_info() const { 38951be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania internal::MutexLock lock(&mutex_); 38961be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania return impl_->current_test_info(); 38971be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 38981be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 389941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// Returns the random seed used at the start of the current test run. 390041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotint UnitTest::random_seed() const { return impl_->random_seed(); } 390141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 39021be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#if GTEST_HAS_PARAM_TEST 39031be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Returns ParameterizedTestCaseRegistry object used to keep track of 39041be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// value-parameterized tests and instantiate and register them. 39051be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// L < mutex_ 39061be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniainternal::ParameterizedTestCaseRegistry& 39071be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania UnitTest::parameterized_test_registry() { 39081be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania return impl_->parameterized_test_registry(); 39091be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 39101be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#endif // GTEST_HAS_PARAM_TEST 39111be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 39121be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Creates an empty UnitTest. 39131be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaUnitTest::UnitTest() { 39141be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania impl_ = new internal::UnitTestImpl(this); 39151be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 39161be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 39171be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Destructor of UnitTest. 39181be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaUnitTest::~UnitTest() { 39191be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania delete impl_; 39201be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 39211be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 39221be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Pushes a trace defined by SCOPED_TRACE() on to the per-thread 39231be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Google Test trace stack. 39241be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// L < mutex_ 39251be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniavoid UnitTest::PushGTestTrace(const internal::TraceInfo& trace) { 39261be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania internal::MutexLock lock(&mutex_); 392741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot impl_->gtest_trace_stack().push_back(trace); 39281be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 39291be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 39301be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Pops a trace from the per-thread Google Test trace stack. 39311be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// L < mutex_ 39321be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniavoid UnitTest::PopGTestTrace() { 39331be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania internal::MutexLock lock(&mutex_); 393441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot impl_->gtest_trace_stack().pop_back(); 39351be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 39361be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 39371be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catanianamespace internal { 39381be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 39391be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaUnitTestImpl::UnitTestImpl(UnitTest* parent) 39401be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania : parent_(parent), 39411be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#ifdef _MSC_VER 394241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot# pragma warning(push) // Saves the current warning state. 394341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot# pragma warning(disable:4355) // Temporarily disables warning 4355 39441be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // (using this in initializer). 39451be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania default_global_test_part_result_reporter_(this), 39461be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania default_per_thread_test_part_result_reporter_(this), 394741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot# pragma warning(pop) // Restores the warning state again. 39481be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#else 39491be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania default_global_test_part_result_reporter_(this), 39501be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania default_per_thread_test_part_result_reporter_(this), 39511be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#endif // _MSC_VER 39521be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania global_test_part_result_repoter_( 39531be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania &default_global_test_part_result_reporter_), 39541be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania per_thread_test_part_result_reporter_( 39551be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania &default_per_thread_test_part_result_reporter_), 39561be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#if GTEST_HAS_PARAM_TEST 39571be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania parameterized_test_registry_(), 39581be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania parameterized_tests_registered_(false), 39591be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#endif // GTEST_HAS_PARAM_TEST 396041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot last_death_test_case_(-1), 39611be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania current_test_case_(NULL), 39621be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania current_test_info_(NULL), 39631be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania ad_hoc_test_result_(), 39641be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania os_stack_trace_getter_(NULL), 396541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot post_flag_parse_init_performed_(false), 396641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot random_seed_(0), // Will be overridden by the flag before first use. 396741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot random_(0), // Will be reseeded before first use. 39681be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania elapsed_time_(0), 396941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot#if GTEST_HAS_DEATH_TEST 39701be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania internal_run_death_test_flag_(NULL), 397141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot death_test_factory_(new DefaultDeathTestFactory), 397241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot#endif 397341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // Will be overridden by the flag before first use. 397441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot catch_exceptions_(false) { 397541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot listeners()->SetDefaultResultPrinter(new PrettyUnitTestResultPrinter); 39761be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 39771be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 39781be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaUnitTestImpl::~UnitTestImpl() { 39791be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // Deletes every TestCase. 398041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot ForEach(test_cases_, internal::Delete<TestCase>); 39811be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 39821be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // Deletes every Environment. 398341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot ForEach(environments_, internal::Delete<Environment>); 39841be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 39851be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania delete os_stack_trace_getter_; 39861be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 39871be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 398841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot#if GTEST_HAS_DEATH_TEST 398941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// Disables event forwarding if the control is currently in a death test 399041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// subprocess. Must not be called before InitGoogleTest. 399141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotvoid UnitTestImpl::SuppressTestEventsIfInSubprocess() { 399241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot if (internal_run_death_test_flag_.get() != NULL) 399341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot listeners()->SuppressEventForwarding(); 399441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot} 399541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot#endif // GTEST_HAS_DEATH_TEST 399641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 399741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// Initializes event listeners performing XML output as specified by 399841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// UnitTestOptions. Must not be called before InitGoogleTest. 399941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotvoid UnitTestImpl::ConfigureXmlOutput() { 400041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot const String& output_format = UnitTestOptions::GetOutputFormat(); 400141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot if (output_format == "xml") { 400241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot listeners()->SetDefaultXmlGenerator(new XmlUnitTestResultPrinter( 400341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot UnitTestOptions::GetAbsolutePathToOutputFile().c_str())); 400441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot } else if (output_format != "") { 400541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot printf("WARNING: unrecognized output format \"%s\" ignored.\n", 400641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot output_format.c_str()); 400741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot fflush(stdout); 400841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot } 400941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot} 401041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 401141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot#if GTEST_CAN_STREAM_RESULTS_ 401241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// Initializes event listeners for streaming test results in String form. 401341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// Must not be called before InitGoogleTest. 401441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotvoid UnitTestImpl::ConfigureStreamingOutput() { 401541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot const string& target = GTEST_FLAG(stream_result_to); 401641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot if (!target.empty()) { 401741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot const size_t pos = target.find(':'); 401841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot if (pos != string::npos) { 401941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot listeners()->Append(new StreamingListener(target.substr(0, pos), 402041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot target.substr(pos+1))); 402141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot } else { 402241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot printf("WARNING: unrecognized streaming target \"%s\" ignored.\n", 402341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot target.c_str()); 402441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot fflush(stdout); 402541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot } 402641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot } 402741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot} 402841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot#endif // GTEST_CAN_STREAM_RESULTS_ 402941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 403041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// Performs initialization dependent upon flag values obtained in 403141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// ParseGoogleTestFlagsOnly. Is called from InitGoogleTest after the call to 403241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// ParseGoogleTestFlagsOnly. In case a user neglects to call InitGoogleTest 403341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// this function is also called from RunAllTests. Since this function can be 403441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// called more than once, it has to be idempotent. 403541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotvoid UnitTestImpl::PostFlagParsingInit() { 403641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // Ensures that this function does not execute more than once. 403741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot if (!post_flag_parse_init_performed_) { 403841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot post_flag_parse_init_performed_ = true; 403941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 404041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot#if GTEST_HAS_DEATH_TEST 404141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot InitDeathTestSubprocessControlInfo(); 404241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot SuppressTestEventsIfInSubprocess(); 404341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot#endif // GTEST_HAS_DEATH_TEST 404441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 404541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // Registers parameterized tests. This makes parameterized tests 404641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // available to the UnitTest reflection API without running 404741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // RUN_ALL_TESTS. 404841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot RegisterParameterizedTests(); 404941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 405041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // Configures listeners for XML output. This makes it possible for users 405141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // to shut down the default XML output before invoking RUN_ALL_TESTS. 405241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot ConfigureXmlOutput(); 405341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 405441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot#if GTEST_CAN_STREAM_RESULTS_ 405541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // Configures listeners for streaming test results to the specified server. 405641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot ConfigureStreamingOutput(); 405741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot#endif // GTEST_CAN_STREAM_RESULTS_ 405841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot } 405941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot} 406041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 40611be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// A predicate that checks the name of a TestCase against a known 40621be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// value. 40631be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// 40641be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// This is used for implementation of the UnitTest class only. We put 40651be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// it in the anonymous namespace to prevent polluting the outer 40661be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// namespace. 40671be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// 40681be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// TestCaseNameIs is copyable. 40691be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniaclass TestCaseNameIs { 40701be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania public: 40711be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // Constructor. 40721be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania explicit TestCaseNameIs(const String& name) 40731be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania : name_(name) {} 40741be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 40751be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // Returns true iff the name of test_case matches name_. 40761be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania bool operator()(const TestCase* test_case) const { 40771be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania return test_case != NULL && strcmp(test_case->name(), name_.c_str()) == 0; 40781be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania } 40791be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 40801be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania private: 40811be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania String name_; 40821be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}; 40831be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 40841be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Finds and returns a TestCase with the given name. If one doesn't 408541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// exist, creates one and returns it. It's the CALLER'S 408641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// RESPONSIBILITY to ensure that this function is only called WHEN THE 408741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// TESTS ARE NOT SHUFFLED. 40881be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// 40891be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Arguments: 40901be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// 40911be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// test_case_name: name of the test case 409241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// type_param: the name of the test case's type parameter, or NULL if 409341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// this is not a typed or a type-parameterized test case. 40941be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// set_up_tc: pointer to the function that sets up the test case 40951be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// tear_down_tc: pointer to the function that tears down the test case 40961be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaTestCase* UnitTestImpl::GetTestCase(const char* test_case_name, 409741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot const char* type_param, 40981be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania Test::SetUpTestCaseFunc set_up_tc, 40991be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania Test::TearDownTestCaseFunc tear_down_tc) { 41001be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // Can we find a TestCase with the given name? 410141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot const std::vector<TestCase*>::const_iterator test_case = 410241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot std::find_if(test_cases_.begin(), test_cases_.end(), 410341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot TestCaseNameIs(test_case_name)); 410441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 410541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot if (test_case != test_cases_.end()) 410641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot return *test_case; 410741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 410841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // No. Let's create one. 410941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot TestCase* const new_test_case = 411041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot new TestCase(test_case_name, type_param, set_up_tc, tear_down_tc); 411141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 411241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // Is this a death test case? 411341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot if (internal::UnitTestOptions::MatchesFilter(String(test_case_name), 411441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot kDeathTestCaseFilter)) { 411541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // Yes. Inserts the test case after the last death test case 411641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // defined so far. This only works when the test cases haven't 411741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // been shuffled. Otherwise we may end up running a death test 411841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // after a non-death test. 411941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot ++last_death_test_case_; 412041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot test_cases_.insert(test_cases_.begin() + last_death_test_case_, 412141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot new_test_case); 412241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot } else { 412341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // No. Appends to the end of the list. 412441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot test_cases_.push_back(new_test_case); 41251be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania } 41261be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 412741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot test_case_indices_.push_back(static_cast<int>(test_case_indices_.size())); 412841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot return new_test_case; 41291be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 41301be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 41311be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Helpers for setting up / tearing down the given environment. They 413241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// are for use in the ForEach() function. 41331be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniastatic void SetUpEnvironment(Environment* env) { env->SetUp(); } 41341be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniastatic void TearDownEnvironment(Environment* env) { env->TearDown(); } 41351be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 41361be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Runs all tests in this UnitTest object, prints the result, and 413741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// returns true if all tests are successful. If any exception is 413841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// thrown during a test, the test is considered to be failed, but the 413941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// rest of the tests will still be run. 414041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// 414141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// When parameterized tests are enabled, it expands and registers 41421be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// parameterized tests first in RegisterParameterizedTests(). 41431be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// All other functions called from RunAllTests() may safely assume that 41441be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// parameterized tests are ready to be counted and run. 414541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotbool UnitTestImpl::RunAllTests() { 41461be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // Makes sure InitGoogleTest() was called. 41471be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania if (!GTestIsInitialized()) { 41481be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania printf("%s", 41491be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania "\nThis test program did NOT call ::testing::InitGoogleTest " 41501be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania "before calling RUN_ALL_TESTS(). Please fix it.\n"); 415141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot return false; 41521be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania } 41531be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 41541be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // Do not run any test if the --help flag was specified. 41551be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania if (g_help_flag) 415641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot return true; 41571be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 415841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // Repeats the call to the post-flag parsing initialization in case the 415941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // user didn't call InitGoogleTest. 416041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot PostFlagParsingInit(); 41611be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 41621be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // Even if sharding is not on, test runners may want to use the 41631be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // GTEST_SHARD_STATUS_FILE to query whether the test supports the sharding 41641be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // protocol. 41651be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania internal::WriteToShardStatusFileIfNeeded(); 41661be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 41671be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // True iff we are in a subprocess for running a thread-safe-style 41681be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // death test. 41691be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania bool in_subprocess_for_death_test = false; 41701be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 41711be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#if GTEST_HAS_DEATH_TEST 41721be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania in_subprocess_for_death_test = (internal_run_death_test_flag_.get() != NULL); 41731be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#endif // GTEST_HAS_DEATH_TEST 41741be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 41751be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania const bool should_shard = ShouldShard(kTestTotalShards, kTestShardIndex, 41761be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania in_subprocess_for_death_test); 41771be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 41781be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // Compares the full test names with the filter to decide which 41791be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // tests to run. 41801be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania const bool has_tests_to_run = FilterTests(should_shard 41811be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania ? HONOR_SHARDING_PROTOCOL 41821be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania : IGNORE_SHARDING_PROTOCOL) > 0; 41831be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 418441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // Lists the tests and exits if the --gtest_list_tests flag was specified. 418541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot if (GTEST_FLAG(list_tests)) { 418641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // This must be called *after* FilterTests() has been called. 418741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot ListTestsMatchingFilter(); 418841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot return true; 418941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot } 419041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 419141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot random_seed_ = GTEST_FLAG(shuffle) ? 419241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot GetRandomSeedFromFlag(GTEST_FLAG(random_seed)) : 0; 419341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 41941be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // True iff at least one test has failed. 41951be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania bool failed = false; 41961be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 419741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot TestEventListener* repeater = listeners()->repeater(); 419841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 419941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot repeater->OnTestProgramStart(*parent_); 420041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 42011be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // How many times to repeat the tests? We don't want to repeat them 42021be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // when we are inside the subprocess of a death test. 42031be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania const int repeat = in_subprocess_for_death_test ? 1 : GTEST_FLAG(repeat); 42041be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // Repeats forever if the repeat count is negative. 42051be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania const bool forever = repeat < 0; 42061be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania for (int i = 0; forever || i != repeat; i++) { 420741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // We want to preserve failures generated by ad-hoc test 420841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // assertions executed before RUN_ALL_TESTS(). 420941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot ClearNonAdHocTestResult(); 42101be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 42111be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania const TimeInMillis start = GetTimeInMillis(); 42121be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 421341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // Shuffles test cases and tests if requested. 421441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot if (has_tests_to_run && GTEST_FLAG(shuffle)) { 421541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot random()->Reseed(random_seed_); 421641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // This should be done before calling OnTestIterationStart(), 421741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // such that a test event listener can see the actual test order 421841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // in the event. 421941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot ShuffleTests(); 422041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot } 422141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 422241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // Tells the unit test event listeners that the tests are about to start. 422341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot repeater->OnTestIterationStart(*parent_, i); 422441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 42251be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // Runs each test case if there is at least one test to run. 42261be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania if (has_tests_to_run) { 42271be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // Sets up all environments beforehand. 422841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot repeater->OnEnvironmentsSetUpStart(*parent_); 422941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot ForEach(environments_, SetUpEnvironment); 423041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot repeater->OnEnvironmentsSetUpEnd(*parent_); 42311be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 42321be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // Runs the tests only if there was no fatal failure during global 42331be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // set-up. 42341be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania if (!Test::HasFatalFailure()) { 423541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot for (int test_index = 0; test_index < total_test_case_count(); 423641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot test_index++) { 423741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot GetMutableTestCase(test_index)->Run(); 423841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot } 42391be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania } 42401be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 42411be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // Tears down all environments in reverse order afterwards. 424241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot repeater->OnEnvironmentsTearDownStart(*parent_); 424341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot std::for_each(environments_.rbegin(), environments_.rend(), 424441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot TearDownEnvironment); 424541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot repeater->OnEnvironmentsTearDownEnd(*parent_); 42461be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania } 42471be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 42481be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania elapsed_time_ = GetTimeInMillis() - start; 42491be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 425041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // Tells the unit test event listener that the tests have just finished. 425141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot repeater->OnTestIterationEnd(*parent_, i); 42521be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 42531be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // Gets the result and clears it. 42541be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania if (!Passed()) { 42551be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania failed = true; 42561be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania } 425741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 425841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // Restores the original test order after the iteration. This 425941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // allows the user to quickly repro a failure that happens in the 426041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // N-th iteration without repeating the first (N - 1) iterations. 426141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // This is not enclosed in "if (GTEST_FLAG(shuffle)) { ... }", in 426241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // case the user somehow changes the value of the flag somewhere 426341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // (it's always safe to unshuffle the tests). 426441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot UnshuffleTests(); 426541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 426641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot if (GTEST_FLAG(shuffle)) { 426741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // Picks a new random seed for each iteration. 426841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot random_seed_ = GetNextRandomSeed(random_seed_); 426941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot } 42701be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania } 42711be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 427241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot repeater->OnTestProgramEnd(*parent_); 427341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 427441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot return !failed; 42751be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 42761be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 42771be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Reads the GTEST_SHARD_STATUS_FILE environment variable, and creates the file 42781be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// if the variable is present. If a file already exists at this location, this 42791be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// function will write over it. If the variable is present, but the file cannot 42801be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// be created, prints an error and exits. 42811be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniavoid WriteToShardStatusFileIfNeeded() { 428241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot const char* const test_shard_file = posix::GetEnv(kTestShardStatusFile); 42831be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania if (test_shard_file != NULL) { 428441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot FILE* const file = posix::FOpen(test_shard_file, "w"); 42851be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania if (file == NULL) { 42861be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania ColoredPrintf(COLOR_RED, 42871be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania "Could not write to the test shard status file \"%s\" " 42881be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania "specified by the %s environment variable.\n", 42891be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania test_shard_file, kTestShardStatusFile); 42901be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania fflush(stdout); 42911be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania exit(EXIT_FAILURE); 42921be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania } 42931be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania fclose(file); 42941be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania } 42951be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 42961be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 42971be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Checks whether sharding is enabled by examining the relevant 42981be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// environment variable values. If the variables are present, 42991be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// but inconsistent (i.e., shard_index >= total_shards), prints 43001be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// an error and exits. If in_subprocess_for_death_test, sharding is 43011be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// disabled because it must only be applied to the original test 43021be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// process. Otherwise, we could filter out death tests we intended to execute. 43031be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniabool ShouldShard(const char* total_shards_env, 43041be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania const char* shard_index_env, 43051be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania bool in_subprocess_for_death_test) { 43061be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania if (in_subprocess_for_death_test) { 43071be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania return false; 43081be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania } 43091be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 43101be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania const Int32 total_shards = Int32FromEnvOrDie(total_shards_env, -1); 43111be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania const Int32 shard_index = Int32FromEnvOrDie(shard_index_env, -1); 43121be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 43131be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania if (total_shards == -1 && shard_index == -1) { 43141be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania return false; 43151be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania } else if (total_shards == -1 && shard_index != -1) { 43161be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania const Message msg = Message() 43171be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania << "Invalid environment variables: you have " 43181be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania << kTestShardIndex << " = " << shard_index 43191be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania << ", but have left " << kTestTotalShards << " unset.\n"; 43201be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania ColoredPrintf(COLOR_RED, msg.GetString().c_str()); 43211be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania fflush(stdout); 43221be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania exit(EXIT_FAILURE); 43231be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania } else if (total_shards != -1 && shard_index == -1) { 43241be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania const Message msg = Message() 43251be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania << "Invalid environment variables: you have " 43261be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania << kTestTotalShards << " = " << total_shards 43271be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania << ", but have left " << kTestShardIndex << " unset.\n"; 43281be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania ColoredPrintf(COLOR_RED, msg.GetString().c_str()); 43291be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania fflush(stdout); 43301be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania exit(EXIT_FAILURE); 43311be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania } else if (shard_index < 0 || shard_index >= total_shards) { 43321be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania const Message msg = Message() 43331be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania << "Invalid environment variables: we require 0 <= " 43341be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania << kTestShardIndex << " < " << kTestTotalShards 43351be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania << ", but you have " << kTestShardIndex << "=" << shard_index 43361be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania << ", " << kTestTotalShards << "=" << total_shards << ".\n"; 43371be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania ColoredPrintf(COLOR_RED, msg.GetString().c_str()); 43381be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania fflush(stdout); 43391be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania exit(EXIT_FAILURE); 43401be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania } 43411be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 43421be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania return total_shards > 1; 43431be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 43441be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 43451be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Parses the environment variable var as an Int32. If it is unset, 43461be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// returns default_val. If it is not an Int32, prints an error 43471be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// and aborts. 434841d0579e8de9ef4ff178fc4991043c61a19943f7Brett ChabotInt32 Int32FromEnvOrDie(const char* var, Int32 default_val) { 434941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot const char* str_val = posix::GetEnv(var); 43501be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania if (str_val == NULL) { 43511be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania return default_val; 43521be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania } 43531be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 43541be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania Int32 result; 43551be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania if (!ParseInt32(Message() << "The value of environment variable " << var, 43561be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania str_val, &result)) { 43571be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania exit(EXIT_FAILURE); 43581be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania } 43591be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania return result; 43601be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 43611be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 43621be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Given the total number of shards, the shard index, and the test id, 43631be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// returns true iff the test should be run on this shard. The test id is 43641be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// some arbitrary but unique non-negative integer assigned to each test 43651be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// method. Assumes that 0 <= shard_index < total_shards. 43661be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniabool ShouldRunTestOnShard(int total_shards, int shard_index, int test_id) { 43671be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania return (test_id % total_shards) == shard_index; 43681be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 43691be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 43701be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Compares the name of each test with the user-specified filter to 43711be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// decide whether the test should be run, then records the result in 43721be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// each TestCase and TestInfo object. 43731be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// If shard_tests == true, further filters tests based on sharding 43741be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// variables in the environment - see 43751be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// http://code.google.com/p/googletest/wiki/GoogleTestAdvancedGuide. 43761be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Returns the number of tests that should run. 43771be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniaint UnitTestImpl::FilterTests(ReactionToSharding shard_tests) { 43781be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania const Int32 total_shards = shard_tests == HONOR_SHARDING_PROTOCOL ? 43791be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania Int32FromEnvOrDie(kTestTotalShards, -1) : -1; 43801be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania const Int32 shard_index = shard_tests == HONOR_SHARDING_PROTOCOL ? 43811be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania Int32FromEnvOrDie(kTestShardIndex, -1) : -1; 43821be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 43831be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // num_runnable_tests are the number of tests that will 43841be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // run across all shards (i.e., match filter and are not disabled). 43851be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // num_selected_tests are the number of tests to be run on 43861be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // this shard. 43871be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania int num_runnable_tests = 0; 43881be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania int num_selected_tests = 0; 438941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot for (size_t i = 0; i < test_cases_.size(); i++) { 439041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot TestCase* const test_case = test_cases_[i]; 43911be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania const String &test_case_name = test_case->name(); 43921be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania test_case->set_should_run(false); 43931be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 439441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot for (size_t j = 0; j < test_case->test_info_list().size(); j++) { 439541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot TestInfo* const test_info = test_case->test_info_list()[j]; 43961be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania const String test_name(test_info->name()); 43971be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // A test is disabled if test case name or test name matches 43981be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // kDisableTestFilter. 43991be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania const bool is_disabled = 44001be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania internal::UnitTestOptions::MatchesFilter(test_case_name, 44011be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania kDisableTestFilter) || 44021be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania internal::UnitTestOptions::MatchesFilter(test_name, 44031be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania kDisableTestFilter); 440441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot test_info->is_disabled_ = is_disabled; 44051be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 440641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot const bool matches_filter = 44071be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania internal::UnitTestOptions::FilterMatchesTest(test_case_name, 44081be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania test_name); 440941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot test_info->matches_filter_ = matches_filter; 441041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 441141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot const bool is_runnable = 441241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot (GTEST_FLAG(also_run_disabled_tests) || !is_disabled) && 441341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot matches_filter; 44141be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 44151be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania const bool is_selected = is_runnable && 44161be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania (shard_tests == IGNORE_SHARDING_PROTOCOL || 44171be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania ShouldRunTestOnShard(total_shards, shard_index, 44181be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania num_runnable_tests)); 44191be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 44201be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania num_runnable_tests += is_runnable; 44211be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania num_selected_tests += is_selected; 44221be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 442341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot test_info->should_run_ = is_selected; 44241be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania test_case->set_should_run(test_case->should_run() || is_selected); 44251be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania } 44261be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania } 44271be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania return num_selected_tests; 44281be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 44291be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 443041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// Prints the names of the tests matching the user-specified filter flag. 443141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotvoid UnitTestImpl::ListTestsMatchingFilter() { 443241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot for (size_t i = 0; i < test_cases_.size(); i++) { 443341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot const TestCase* const test_case = test_cases_[i]; 443441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot bool printed_test_case_name = false; 44351be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 443641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot for (size_t j = 0; j < test_case->test_info_list().size(); j++) { 443741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot const TestInfo* const test_info = 443841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot test_case->test_info_list()[j]; 443941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot if (test_info->matches_filter_) { 444041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot if (!printed_test_case_name) { 444141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot printed_test_case_name = true; 444241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot printf("%s.\n", test_case->name()); 444341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot } 444441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot printf(" %s\n", test_info->name()); 444541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot } 44461be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania } 44471be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania } 44481be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania fflush(stdout); 44491be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 44501be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 44511be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Sets the OS stack trace getter. 44521be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// 44531be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Does nothing if the input and the current OS stack trace getter are 44541be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// the same; otherwise, deletes the old getter and makes the input the 44551be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// current getter. 44561be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniavoid UnitTestImpl::set_os_stack_trace_getter( 44571be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania OsStackTraceGetterInterface* getter) { 44581be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania if (os_stack_trace_getter_ != getter) { 44591be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania delete os_stack_trace_getter_; 44601be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania os_stack_trace_getter_ = getter; 44611be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania } 44621be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 44631be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 44641be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Returns the current OS stack trace getter if it is not NULL; 44651be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// otherwise, creates an OsStackTraceGetter, makes it the current 44661be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// getter, and returns it. 44671be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaOsStackTraceGetterInterface* UnitTestImpl::os_stack_trace_getter() { 44681be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania if (os_stack_trace_getter_ == NULL) { 44691be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania os_stack_trace_getter_ = new OsStackTraceGetter; 44701be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania } 44711be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 44721be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania return os_stack_trace_getter_; 44731be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 44741be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 44751be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Returns the TestResult for the test that's currently running, or 44761be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// the TestResult for the ad hoc test if no test is running. 447741d0579e8de9ef4ff178fc4991043c61a19943f7Brett ChabotTestResult* UnitTestImpl::current_test_result() { 44781be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania return current_test_info_ ? 447941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot &(current_test_info_->result_) : &ad_hoc_test_result_; 448041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot} 448141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 448241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// Shuffles all test cases, and the tests within each test case, 448341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// making sure that death tests are still run first. 448441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotvoid UnitTestImpl::ShuffleTests() { 448541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // Shuffles the death test cases. 448641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot ShuffleRange(random(), 0, last_death_test_case_ + 1, &test_case_indices_); 448741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 448841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // Shuffles the non-death test cases. 448941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot ShuffleRange(random(), last_death_test_case_ + 1, 449041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot static_cast<int>(test_cases_.size()), &test_case_indices_); 449141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 449241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // Shuffles the tests inside each test case. 449341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot for (size_t i = 0; i < test_cases_.size(); i++) { 449441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot test_cases_[i]->ShuffleTests(random()); 449541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot } 449641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot} 449741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 449841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// Restores the test cases and tests to their order before the first shuffle. 449941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotvoid UnitTestImpl::UnshuffleTests() { 450041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot for (size_t i = 0; i < test_cases_.size(); i++) { 450141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // Unshuffles the tests in each test case. 450241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot test_cases_[i]->UnshuffleTests(); 450341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // Resets the index of each test case. 450441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot test_case_indices_[i] = static_cast<int>(i); 450541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot } 45061be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 45071be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 45081be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Returns the current OS stack trace as a String. 45091be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// 45101be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// The maximum number of stack frames to be included is specified by 45111be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// the gtest_stack_trace_depth flag. The skip_count parameter 45121be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// specifies the number of top frames to be skipped, which doesn't 45131be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// count against the number of frames to be included. 45141be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// 45151be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// For example, if Foo() calls Bar(), which in turn calls 45161be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// GetCurrentOsStackTraceExceptTop(..., 1), Foo() will be included in 45171be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// the trace but Bar() and GetCurrentOsStackTraceExceptTop() won't. 451841d0579e8de9ef4ff178fc4991043c61a19943f7Brett ChabotString GetCurrentOsStackTraceExceptTop(UnitTest* /*unit_test*/, 451941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot int skip_count) { 45201be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // We pass skip_count + 1 to skip this wrapper function in addition 45211be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // to what the user really wants to skip. 452241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot return GetUnitTestImpl()->CurrentOsStackTraceExceptTop(skip_count + 1); 45231be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 45241be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 452541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// Used by the GTEST_SUPPRESS_UNREACHABLE_CODE_WARNING_BELOW_ macro to 452641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// suppress unreachable code warnings. 45271be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catanianamespace { 45281be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniaclass ClassUniqueToAlwaysTrue {}; 45291be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 45301be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 453141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotbool IsTrue(bool condition) { return condition; } 453241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 45331be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniabool AlwaysTrue() { 45341be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#if GTEST_HAS_EXCEPTIONS 45351be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // This condition is always false so AlwaysTrue() never actually throws, 45361be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // but it makes the compiler think that it may throw. 453741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot if (IsTrue(false)) 45381be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania throw ClassUniqueToAlwaysTrue(); 45391be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#endif // GTEST_HAS_EXCEPTIONS 45401be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania return true; 45411be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 45421be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 454341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// If *pstr starts with the given prefix, modifies *pstr to be right 454441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// past the prefix and returns true; otherwise leaves *pstr unchanged 454541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// and returns false. None of pstr, *pstr, and prefix can be NULL. 454641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotbool SkipPrefix(const char* prefix, const char** pstr) { 454741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot const size_t prefix_len = strlen(prefix); 454841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot if (strncmp(*pstr, prefix, prefix_len) == 0) { 454941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot *pstr += prefix_len; 455041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot return true; 455141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot } 455241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot return false; 455341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot} 455441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 45551be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Parses a string as a command line flag. The string should have 45561be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// the format "--flag=value". When def_optional is true, the "=value" 45571be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// part can be omitted. 45581be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// 45591be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Returns the value of the flag, or NULL if the parsing failed. 45601be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniaconst char* ParseFlagValue(const char* str, 45611be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania const char* flag, 45621be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania bool def_optional) { 45631be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // str and flag must not be NULL. 45641be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania if (str == NULL || flag == NULL) return NULL; 45651be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 45661be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // The flag must start with "--" followed by GTEST_FLAG_PREFIX_. 45671be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania const String flag_str = String::Format("--%s%s", GTEST_FLAG_PREFIX_, flag); 456841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot const size_t flag_len = flag_str.length(); 45691be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania if (strncmp(str, flag_str.c_str(), flag_len) != 0) return NULL; 45701be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 45711be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // Skips the flag name. 45721be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania const char* flag_end = str + flag_len; 45731be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 45741be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // When def_optional is true, it's OK to not have a "=value" part. 45751be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania if (def_optional && (flag_end[0] == '\0')) { 45761be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania return flag_end; 45771be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania } 45781be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 45791be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // If def_optional is true and there are more characters after the 45801be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // flag name, or if def_optional is false, there must be a '=' after 45811be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // the flag name. 45821be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania if (flag_end[0] != '=') return NULL; 45831be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 45841be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // Returns the string after "=". 45851be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania return flag_end + 1; 45861be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 45871be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 45881be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Parses a string for a bool flag, in the form of either 45891be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// "--flag=value" or "--flag". 45901be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// 45911be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// In the former case, the value is taken as true as long as it does 45921be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// not start with '0', 'f', or 'F'. 45931be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// 45941be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// In the latter case, the value is taken as true. 45951be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// 45961be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// On success, stores the value of the flag in *value, and returns 45971be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// true. On failure, returns false without changing *value. 45981be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniabool ParseBoolFlag(const char* str, const char* flag, bool* value) { 45991be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // Gets the value of the flag as a string. 46001be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania const char* const value_str = ParseFlagValue(str, flag, true); 46011be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 46021be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // Aborts if the parsing failed. 46031be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania if (value_str == NULL) return false; 46041be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 46051be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // Converts the string value to a bool. 46061be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania *value = !(*value_str == '0' || *value_str == 'f' || *value_str == 'F'); 46071be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania return true; 46081be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 46091be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 46101be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Parses a string for an Int32 flag, in the form of 46111be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// "--flag=value". 46121be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// 46131be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// On success, stores the value of the flag in *value, and returns 46141be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// true. On failure, returns false without changing *value. 46151be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniabool ParseInt32Flag(const char* str, const char* flag, Int32* value) { 46161be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // Gets the value of the flag as a string. 46171be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania const char* const value_str = ParseFlagValue(str, flag, false); 46181be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 46191be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // Aborts if the parsing failed. 46201be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania if (value_str == NULL) return false; 46211be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 46221be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // Sets *value to the value of the flag. 46231be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania return ParseInt32(Message() << "The value of flag --" << flag, 46241be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania value_str, value); 46251be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 46261be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 46271be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Parses a string for a string flag, in the form of 46281be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// "--flag=value". 46291be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// 46301be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// On success, stores the value of the flag in *value, and returns 46311be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// true. On failure, returns false without changing *value. 46321be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniabool ParseStringFlag(const char* str, const char* flag, String* value) { 46331be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // Gets the value of the flag as a string. 46341be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania const char* const value_str = ParseFlagValue(str, flag, false); 46351be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 46361be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // Aborts if the parsing failed. 46371be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania if (value_str == NULL) return false; 46381be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 46391be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // Sets *value to the value of the flag. 46401be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania *value = value_str; 46411be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania return true; 46421be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 46431be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 464441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// Determines whether a string has a prefix that Google Test uses for its 464541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// flags, i.e., starts with GTEST_FLAG_PREFIX_ or GTEST_FLAG_PREFIX_DASH_. 464641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// If Google Test detects that a command line flag has its prefix but is not 464741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// recognized, it will print its help message. Flags starting with 464841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// GTEST_INTERNAL_PREFIX_ followed by "internal_" are considered Google Test 464941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// internal flags and do not trigger the help message. 465041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotstatic bool HasGoogleTestFlagPrefix(const char* str) { 465141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot return (SkipPrefix("--", &str) || 465241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot SkipPrefix("-", &str) || 465341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot SkipPrefix("/", &str)) && 465441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot !SkipPrefix(GTEST_FLAG_PREFIX_ "internal_", &str) && 465541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot (SkipPrefix(GTEST_FLAG_PREFIX_, &str) || 465641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot SkipPrefix(GTEST_FLAG_PREFIX_DASH_, &str)); 465741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot} 465841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 46591be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Prints a string containing code-encoded text. The following escape 46601be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// sequences can be used in the string to control the text color: 46611be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// 46621be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// @@ prints a single '@' character. 46631be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// @R changes the color to red. 46641be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// @G changes the color to green. 46651be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// @Y changes the color to yellow. 46661be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// @D changes to the default terminal text color. 46671be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// 46681be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// TODO(wan@google.com): Write tests for this once we add stdout 46691be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// capturing to Google Test. 46701be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniastatic void PrintColorEncoded(const char* str) { 46711be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania GTestColor color = COLOR_DEFAULT; // The current color. 46721be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 46731be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // Conceptually, we split the string into segments divided by escape 46741be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // sequences. Then we print one segment at a time. At the end of 46751be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // each iteration, the str pointer advances to the beginning of the 46761be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // next segment. 46771be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania for (;;) { 46781be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania const char* p = strchr(str, '@'); 46791be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania if (p == NULL) { 46801be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania ColoredPrintf(color, "%s", str); 46811be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania return; 46821be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania } 46831be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 46841be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania ColoredPrintf(color, "%s", String(str, p - str).c_str()); 46851be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 46861be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania const char ch = p[1]; 46871be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania str = p + 2; 46881be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania if (ch == '@') { 46891be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania ColoredPrintf(color, "@"); 46901be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania } else if (ch == 'D') { 46911be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania color = COLOR_DEFAULT; 46921be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania } else if (ch == 'R') { 46931be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania color = COLOR_RED; 46941be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania } else if (ch == 'G') { 46951be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania color = COLOR_GREEN; 46961be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania } else if (ch == 'Y') { 46971be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania color = COLOR_YELLOW; 46981be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania } else { 46991be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania --str; 47001be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania } 47011be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania } 47021be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 47031be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 47041be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniastatic const char kColorEncodedHelpMessage[] = 47051be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania"This program contains tests written using " GTEST_NAME_ ". You can use the\n" 47061be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania"following command line flags to control its behavior:\n" 47071be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania"\n" 47081be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania"Test Selection:\n" 47091be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania" @G--" GTEST_FLAG_PREFIX_ "list_tests@D\n" 47101be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania" List the names of all tests instead of running them. The name of\n" 47111be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania" TEST(Foo, Bar) is \"Foo.Bar\".\n" 47121be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania" @G--" GTEST_FLAG_PREFIX_ "filter=@YPOSTIVE_PATTERNS" 47131be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania "[@G-@YNEGATIVE_PATTERNS]@D\n" 47141be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania" Run only the tests whose name matches one of the positive patterns but\n" 47151be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania" none of the negative patterns. '?' matches any single character; '*'\n" 47161be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania" matches any substring; ':' separates two patterns.\n" 47171be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania" @G--" GTEST_FLAG_PREFIX_ "also_run_disabled_tests@D\n" 47181be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania" Run all disabled tests too.\n" 471941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot"\n" 472041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot"Test Execution:\n" 47211be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania" @G--" GTEST_FLAG_PREFIX_ "repeat=@Y[COUNT]@D\n" 47221be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania" Run the tests repeatedly; use a negative count to repeat forever.\n" 472341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot" @G--" GTEST_FLAG_PREFIX_ "shuffle@D\n" 472441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot" Randomize tests' orders on every iteration.\n" 472541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot" @G--" GTEST_FLAG_PREFIX_ "random_seed=@Y[NUMBER]@D\n" 472641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot" Random number seed to use for shuffling test orders (between 1 and\n" 472741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot" 99999, or 0 to use a seed based on the current time).\n" 47281be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania"\n" 47291be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania"Test Output:\n" 47301be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania" @G--" GTEST_FLAG_PREFIX_ "color=@Y(@Gyes@Y|@Gno@Y|@Gauto@Y)@D\n" 47311be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania" Enable/disable colored output. The default is @Gauto@D.\n" 473241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot" -@G-" GTEST_FLAG_PREFIX_ "print_time=0@D\n" 473341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot" Don't print the elapsed time of each test.\n" 47341be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania" @G--" GTEST_FLAG_PREFIX_ "output=xml@Y[@G:@YDIRECTORY_PATH@G" 47351be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania GTEST_PATH_SEP_ "@Y|@G:@YFILE_PATH]@D\n" 47361be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania" Generate an XML report in the given directory or with the given file\n" 47371be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania" name. @YFILE_PATH@D defaults to @Gtest_details.xml@D.\n" 473841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot#if GTEST_CAN_STREAM_RESULTS_ 473941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot" @G--" GTEST_FLAG_PREFIX_ "stream_result_to=@YHOST@G:@YPORT@D\n" 474041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot" Stream test results to the given server.\n" 474141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot#endif // GTEST_CAN_STREAM_RESULTS_ 47421be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania"\n" 474341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot"Assertion Behavior:\n" 474441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot#if GTEST_HAS_DEATH_TEST && !GTEST_OS_WINDOWS 474541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot" @G--" GTEST_FLAG_PREFIX_ "death_test_style=@Y(@Gfast@Y|@Gthreadsafe@Y)@D\n" 474641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot" Set the default death test style.\n" 474741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot#endif // GTEST_HAS_DEATH_TEST && !GTEST_OS_WINDOWS 47481be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania" @G--" GTEST_FLAG_PREFIX_ "break_on_failure@D\n" 47491be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania" Turn assertion failures into debugger break-points.\n" 47501be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania" @G--" GTEST_FLAG_PREFIX_ "throw_on_failure@D\n" 47511be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania" Turn assertion failures into C++ exceptions.\n" 475241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot" @G--" GTEST_FLAG_PREFIX_ "catch_exceptions=0@D\n" 475341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot" Do not report exceptions as test failures. Instead, allow them\n" 475441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot" to crash the program or throw a pop-up (on Windows).\n" 47551be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania"\n" 47561be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania"Except for @G--" GTEST_FLAG_PREFIX_ "list_tests@D, you can alternatively set " 47571be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania "the corresponding\n" 47581be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania"environment variable of a flag (all letters in upper-case). For example, to\n" 475941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot"disable colored text output, you can either specify @G--" GTEST_FLAG_PREFIX_ 476041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot "color=no@D or set\n" 476141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot"the @G" GTEST_FLAG_PREFIX_UPPER_ "COLOR@D environment variable to @Gno@D.\n" 47621be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania"\n" 47631be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania"For more information, please read the " GTEST_NAME_ " documentation at\n" 47641be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania"@G" GTEST_PROJECT_URL_ "@D. If you find a bug in " GTEST_NAME_ "\n" 47651be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania"(not one in your own code or tests), please report it to\n" 47661be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania"@G<" GTEST_DEV_EMAIL_ ">@D.\n"; 47671be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 47681be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Parses the command line for Google Test flags, without initializing 47691be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// other parts of Google Test. The type parameter CharType can be 47701be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// instantiated to either char or wchar_t. 47711be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniatemplate <typename CharType> 47721be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniavoid ParseGoogleTestFlagsOnlyImpl(int* argc, CharType** argv) { 47731be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania for (int i = 1; i < *argc; i++) { 47741be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania const String arg_string = StreamableToString(argv[i]); 47751be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania const char* const arg = arg_string.c_str(); 47761be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 47771be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania using internal::ParseBoolFlag; 47781be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania using internal::ParseInt32Flag; 47791be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania using internal::ParseStringFlag; 47801be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 47811be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // Do we see a Google Test flag? 47821be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania if (ParseBoolFlag(arg, kAlsoRunDisabledTestsFlag, 47831be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania >EST_FLAG(also_run_disabled_tests)) || 47841be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania ParseBoolFlag(arg, kBreakOnFailureFlag, 47851be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania >EST_FLAG(break_on_failure)) || 47861be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania ParseBoolFlag(arg, kCatchExceptionsFlag, 47871be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania >EST_FLAG(catch_exceptions)) || 47881be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania ParseStringFlag(arg, kColorFlag, >EST_FLAG(color)) || 47891be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania ParseStringFlag(arg, kDeathTestStyleFlag, 47901be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania >EST_FLAG(death_test_style)) || 47911be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania ParseBoolFlag(arg, kDeathTestUseFork, 47921be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania >EST_FLAG(death_test_use_fork)) || 47931be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania ParseStringFlag(arg, kFilterFlag, >EST_FLAG(filter)) || 47941be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania ParseStringFlag(arg, kInternalRunDeathTestFlag, 47951be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania >EST_FLAG(internal_run_death_test)) || 47961be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania ParseBoolFlag(arg, kListTestsFlag, >EST_FLAG(list_tests)) || 47971be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania ParseStringFlag(arg, kOutputFlag, >EST_FLAG(output)) || 47981be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania ParseBoolFlag(arg, kPrintTimeFlag, >EST_FLAG(print_time)) || 479941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot ParseInt32Flag(arg, kRandomSeedFlag, >EST_FLAG(random_seed)) || 48001be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania ParseInt32Flag(arg, kRepeatFlag, >EST_FLAG(repeat)) || 480141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot ParseBoolFlag(arg, kShuffleFlag, >EST_FLAG(shuffle)) || 480241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot ParseInt32Flag(arg, kStackTraceDepthFlag, 480341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot >EST_FLAG(stack_trace_depth)) || 480441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot ParseStringFlag(arg, kStreamResultToFlag, 480541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot >EST_FLAG(stream_result_to)) || 480641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot ParseBoolFlag(arg, kThrowOnFailureFlag, 480741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot >EST_FLAG(throw_on_failure)) 48081be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania ) { 48091be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // Yes. Shift the remainder of the argv list left by one. Note 48101be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // that argv has (*argc + 1) elements, the last one always being 48111be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // NULL. The following loop moves the trailing NULL element as 48121be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // well. 48131be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania for (int j = i; j != *argc; j++) { 48141be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania argv[j] = argv[j + 1]; 48151be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania } 48161be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 48171be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // Decrements the argument count. 48181be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania (*argc)--; 48191be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 48201be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // We also need to decrement the iterator as we just removed 48211be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // an element. 48221be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania i--; 48231be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania } else if (arg_string == "--help" || arg_string == "-h" || 482441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot arg_string == "-?" || arg_string == "/?" || 482541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot HasGoogleTestFlagPrefix(arg)) { 482641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // Both help flag and unrecognized Google Test flags (excluding 482741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // internal ones) trigger help display. 48281be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania g_help_flag = true; 48291be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania } 48301be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania } 48311be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 48321be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania if (g_help_flag) { 48331be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // We print the help here instead of in RUN_ALL_TESTS(), as the 48341be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // latter may not be called at all if the user is using Google 48351be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // Test with another testing framework. 48361be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania PrintColorEncoded(kColorEncodedHelpMessage); 48371be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania } 48381be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 48391be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 48401be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Parses the command line for Google Test flags, without initializing 48411be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// other parts of Google Test. 48421be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniavoid ParseGoogleTestFlagsOnly(int* argc, char** argv) { 48431be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania ParseGoogleTestFlagsOnlyImpl(argc, argv); 48441be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 48451be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniavoid ParseGoogleTestFlagsOnly(int* argc, wchar_t** argv) { 48461be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania ParseGoogleTestFlagsOnlyImpl(argc, argv); 48471be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 48481be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 48491be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// The internal implementation of InitGoogleTest(). 48501be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// 48511be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// The type parameter CharType can be instantiated to either char or 48521be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// wchar_t. 48531be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniatemplate <typename CharType> 48541be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniavoid InitGoogleTestImpl(int* argc, CharType** argv) { 48551be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania g_init_gtest_count++; 48561be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 48571be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // We don't want to run the initialization code twice. 48581be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania if (g_init_gtest_count != 1) return; 48591be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 48601be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania if (*argc <= 0) return; 48611be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 48621be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania internal::g_executable_path = internal::StreamableToString(argv[0]); 48631be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 48641be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#if GTEST_HAS_DEATH_TEST 486541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 48661be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania g_argvs.clear(); 48671be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania for (int i = 0; i != *argc; i++) { 48681be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania g_argvs.push_back(StreamableToString(argv[i])); 48691be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania } 487041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 48711be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#endif // GTEST_HAS_DEATH_TEST 48721be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 48731be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania ParseGoogleTestFlagsOnly(argc, argv); 487441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot GetUnitTestImpl()->PostFlagParsingInit(); 48751be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 48761be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 48771be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} // namespace internal 48781be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 48791be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Initializes Google Test. This must be called before calling 48801be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// RUN_ALL_TESTS(). In particular, it parses a command line for the 48811be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// flags that Google Test recognizes. Whenever a Google Test flag is 48821be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// seen, it is removed from argv, and *argc is decremented. 48831be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// 48841be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// No value is returned. Instead, the Google Test flag variables are 48851be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// updated. 48861be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// 48871be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Calling the function for the second time has no user-visible effect. 48881be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniavoid InitGoogleTest(int* argc, char** argv) { 48891be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania internal::InitGoogleTestImpl(argc, argv); 48901be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 48911be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 48921be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// This overloaded version can be used in Windows programs compiled in 48931be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// UNICODE mode. 48941be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniavoid InitGoogleTest(int* argc, wchar_t** argv) { 48951be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania internal::InitGoogleTestImpl(argc, argv); 48961be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 48971be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 48981be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} // namespace testing 4899