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>
42fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes#include <time.h>
431be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#include <wchar.h>
441be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#include <wctype.h>
451be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
4641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot#include <algorithm>
47fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes#include <iomanip>
48fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes#include <limits>
4941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot#include <ostream>  // NOLINT
5041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot#include <sstream>
5141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot#include <vector>
5241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot
531be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#if GTEST_OS_LINUX
541be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
551be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// TODO(kenton@google.com): Use autoconf to detect availability of
561be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// gettimeofday().
5741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot# define GTEST_HAS_GETTIMEOFDAY_ 1
581be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
5941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot# include <fcntl.h>  // NOLINT
6041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot# include <limits.h>  // NOLINT
6141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot# include <sched.h>  // NOLINT
621be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Declares vsnprintf().  This header is not available on Windows.
6341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot# include <strings.h>  // NOLINT
6441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot# include <sys/mman.h>  // NOLINT
6541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot# include <sys/time.h>  // NOLINT
6641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot# include <unistd.h>  // NOLINT
6741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot# include <string>
681be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
691be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#elif GTEST_OS_SYMBIAN
7041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot# define GTEST_HAS_GETTIMEOFDAY_ 1
7141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot# include <sys/time.h>  // NOLINT
721be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
731be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#elif GTEST_OS_ZOS
7441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot# define GTEST_HAS_GETTIMEOFDAY_ 1
7541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot# include <sys/time.h>  // NOLINT
761be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
771be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// On z/OS we additionally need strings.h for strcasecmp.
7841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot# include <strings.h>  // NOLINT
791be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
8041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot#elif GTEST_OS_WINDOWS_MOBILE  // We are on Windows CE.
811be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
8241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot# include <windows.h>  // NOLINT
831be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
841be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#elif GTEST_OS_WINDOWS  // We are on Windows proper.
851be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
8641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot# include <io.h>  // NOLINT
8741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot# include <sys/timeb.h>  // NOLINT
8841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot# include <sys/types.h>  // NOLINT
8941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot# include <sys/stat.h>  // NOLINT
901be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
9141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot# if GTEST_OS_WINDOWS_MINGW
921be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// MinGW has gettimeofday() but not _ftime64().
931be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// TODO(kenton@google.com): Use autoconf to detect availability of
941be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//   gettimeofday().
951be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// TODO(kenton@google.com): There are other ways to get the time on
961be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//   Windows, like GetTickCount() or GetSystemTimeAsFileTime().  MinGW
971be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//   supports these.  consider using them instead.
9841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot#  define GTEST_HAS_GETTIMEOFDAY_ 1
9941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot#  include <sys/time.h>  // NOLINT
10041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot# endif  // GTEST_OS_WINDOWS_MINGW
1011be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
1021be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// cpplint thinks that the header is already included, so we want to
1031be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// silence it.
10441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot# include <windows.h>  // NOLINT
1051be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
1061be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#else
1071be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
1081be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Assume other platforms have gettimeofday().
1091be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// TODO(kenton@google.com): Use autoconf to detect availability of
1101be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//   gettimeofday().
11141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot# define GTEST_HAS_GETTIMEOFDAY_ 1
1121be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
1131be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// cpplint thinks that the header is already included, so we want to
1141be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// silence it.
11541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot# include <sys/time.h>  // NOLINT
11641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot# include <unistd.h>  // NOLINT
1171be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
1181be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#endif  // GTEST_OS_LINUX
1191be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
1201be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#if GTEST_HAS_EXCEPTIONS
12141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot# include <stdexcept>
12241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot#endif
12341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot
12441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot#if GTEST_CAN_STREAM_RESULTS_
12541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot# include <arpa/inet.h>  // NOLINT
12641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot# include <netdb.h>  // NOLINT
1271be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#endif
1281be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
1291be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Indicates that this translation unit is part of Google Test's
1301be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// implementation.  It must come before gtest-internal-inl.h is
1311be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// included, or there will be a compiler error.  This trick is to
1321be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// prevent a user from accidentally including gtest-internal-inl.h in
1331be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// his code.
1341be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#define GTEST_IMPLEMENTATION_ 1
1351be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#include "src/gtest-internal-inl.h"
1361be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#undef GTEST_IMPLEMENTATION_
1371be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
1381be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#if GTEST_OS_WINDOWS
13941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot# define vsnprintf _vsnprintf
1401be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#endif  // GTEST_OS_WINDOWS
1411be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
1421be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catanianamespace testing {
1431be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
14441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotusing internal::CountIf;
14541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotusing internal::ForEach;
14641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotusing internal::GetElementOr;
14741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotusing internal::Shuffle;
14841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot
1491be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Constants.
1501be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
1511be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// A test whose test case name or test name matches this filter is
1521be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// disabled and not run.
1531be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniastatic const char kDisableTestFilter[] = "DISABLED_*:*/DISABLED_*";
1541be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
1551be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// A test case whose name matches this filter is considered a death
1561be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// test case and will be run before test cases whose name doesn't
1571be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// match this filter.
1581be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniastatic const char kDeathTestCaseFilter[] = "*DeathTest:*DeathTest/*";
1591be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
1601be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// A test filter that matches everything.
1611be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniastatic const char kUniversalFilter[] = "*";
1621be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
1631be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// The default output file for XML output.
1641be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniastatic const char kDefaultOutputFile[] = "test_detail.xml";
1651be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
1661be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// The environment variable name for the test shard index.
1671be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniastatic const char kTestShardIndex[] = "GTEST_SHARD_INDEX";
1681be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// The environment variable name for the total number of test shards.
1691be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniastatic const char kTestTotalShards[] = "GTEST_TOTAL_SHARDS";
1701be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// The environment variable name for the test shard status file.
1711be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniastatic const char kTestShardStatusFile[] = "GTEST_SHARD_STATUS_FILE";
1721be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
1731be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catanianamespace internal {
1741be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
1751be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// The text used in failure messages to indicate the start of the
1761be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// stack trace.
1771be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniaconst char kStackTraceMarker[] = "\nStack trace:\n";
1781be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
17941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// g_help_flag is true iff the --help flag or an equivalent form is
18041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// specified on the command line.
18141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotbool g_help_flag = false;
18241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot
1831be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}  // namespace internal
1841be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
185fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughesstatic const char* GetDefaultFilter() {
186fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes  return kUniversalFilter;
187fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes}
188fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes
1891be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaGTEST_DEFINE_bool_(
1901be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    also_run_disabled_tests,
1911be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    internal::BoolFromGTestEnv("also_run_disabled_tests", false),
1921be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    "Run disabled tests too, in addition to the tests normally being run.");
1931be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
1941be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaGTEST_DEFINE_bool_(
1951be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    break_on_failure,
1961be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    internal::BoolFromGTestEnv("break_on_failure", false),
1971be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    "True iff a failed assertion should be a debugger break-point.");
1981be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
1991be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaGTEST_DEFINE_bool_(
2001be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    catch_exceptions,
20141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot    internal::BoolFromGTestEnv("catch_exceptions", true),
2021be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    "True iff " GTEST_NAME_
2031be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    " should catch exceptions and treat them as test failures.");
2041be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
2051be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaGTEST_DEFINE_string_(
2061be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    color,
2071be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    internal::StringFromGTestEnv("color", "auto"),
2081be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    "Whether to use colors in the output.  Valid values: yes, no, "
2091be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    "and auto.  'auto' means to use colors if the output is "
2101be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    "being sent to a terminal and the TERM environment variable "
211fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes    "is set to a terminal type that supports colors.");
2121be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
2131be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaGTEST_DEFINE_string_(
2141be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    filter,
215fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes    internal::StringFromGTestEnv("filter", GetDefaultFilter()),
2161be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    "A colon-separated list of glob (not regex) patterns "
2171be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    "for filtering the tests to run, optionally followed by a "
2181be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    "'-' and a : separated list of negative patterns (tests to "
2191be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    "exclude).  A test is run if it matches one of the positive "
2201be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    "patterns and does not match any of the negative patterns.");
2211be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
2221be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaGTEST_DEFINE_bool_(list_tests, false,
2231be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania                   "List all tests without running them.");
2241be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
2251be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaGTEST_DEFINE_string_(
2261be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    output,
2271be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    internal::StringFromGTestEnv("output", ""),
2281be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    "A format (currently must be \"xml\"), optionally followed "
2291be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    "by a colon and an output file name or directory. A directory "
2301be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    "is indicated by a trailing pathname separator. "
2311be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    "Examples: \"xml:filename.xml\", \"xml::directoryname/\". "
2321be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    "If a directory is specified, output files will be created "
2331be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    "within that directory, with file-names based on the test "
2341be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    "executable's name and, if necessary, made unique by adding "
2351be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    "digits.");
2361be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
2371be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaGTEST_DEFINE_bool_(
2381be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    print_time,
23941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot    internal::BoolFromGTestEnv("print_time", true),
2401be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    "True iff " GTEST_NAME_
2411be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    " should display elapsed time in text output.");
2421be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
2431be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaGTEST_DEFINE_int32_(
24441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot    random_seed,
24541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot    internal::Int32FromGTestEnv("random_seed", 0),
24641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot    "Random number seed to use when shuffling test orders.  Must be in range "
24741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot    "[1, 99999], or 0 to use a seed based on the current time.");
24841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot
24941d0579e8de9ef4ff178fc4991043c61a19943f7Brett ChabotGTEST_DEFINE_int32_(
2501be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    repeat,
2511be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    internal::Int32FromGTestEnv("repeat", 1),
2521be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    "How many times to repeat each test.  Specify a negative number "
2531be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    "for repeating forever.  Useful for shaking out flaky tests.");
2541be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
25541d0579e8de9ef4ff178fc4991043c61a19943f7Brett ChabotGTEST_DEFINE_bool_(
25641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot    show_internal_stack_frames, false,
25741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot    "True iff " GTEST_NAME_ " should include internal stack frames when "
25841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot    "printing test failure stack traces.");
25941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot
26041d0579e8de9ef4ff178fc4991043c61a19943f7Brett ChabotGTEST_DEFINE_bool_(
26141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot    shuffle,
26241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot    internal::BoolFromGTestEnv("shuffle", false),
26341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot    "True iff " GTEST_NAME_
26441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot    " should randomize tests' order on every run.");
26541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot
2661be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaGTEST_DEFINE_int32_(
2671be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    stack_trace_depth,
26841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot    internal::Int32FromGTestEnv("stack_trace_depth", kMaxStackTraceDepth),
2691be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    "The maximum number of stack frames to print when an "
2701be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    "assertion fails.  The valid range is 0 through 100, inclusive.");
2711be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
27241d0579e8de9ef4ff178fc4991043c61a19943f7Brett ChabotGTEST_DEFINE_string_(
27341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot    stream_result_to,
27441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot    internal::StringFromGTestEnv("stream_result_to", ""),
27541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot    "This flag specifies the host name and the port number on which to stream "
27641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot    "test results. Example: \"localhost:555\". The flag is effective only on "
27741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot    "Linux.");
2781be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
2791be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaGTEST_DEFINE_bool_(
2801be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    throw_on_failure,
2811be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    internal::BoolFromGTestEnv("throw_on_failure", false),
2821be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    "When this flag is specified, a failed assertion will throw an exception "
2831be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    "if exceptions are enabled or exit the program with a non-zero code "
2841be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    "otherwise.");
2851be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
2861be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catanianamespace internal {
2871be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
28841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// Generates a random number from [0, range), using a Linear
28941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// Congruential Generator (LCG).  Crashes if 'range' is 0 or greater
29041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// than kMaxRange.
29141d0579e8de9ef4ff178fc4991043c61a19943f7Brett ChabotUInt32 Random::Generate(UInt32 range) {
29241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  // These constants are the same as are used in glibc's rand(3).
29341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  state_ = (1103515245U*state_ + 12345U) % kMaxRange;
29441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot
29541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  GTEST_CHECK_(range > 0)
29641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot      << "Cannot generate a number in the range [0, 0).";
29741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  GTEST_CHECK_(range <= kMaxRange)
29841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot      << "Generation of a number in [0, " << range << ") was requested, "
29941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot      << "but this can only generate numbers in [0, " << kMaxRange << ").";
30041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot
30141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  // Converting via modulus introduces a bit of downward bias, but
30241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  // it's simple, and a linear congruential generator isn't too good
30341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  // to begin with.
30441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  return state_ % range;
30541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot}
3061be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
3071be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// GTestIsInitialized() returns true iff the user has initialized
3081be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Google Test.  Useful for catching the user mistake of not initializing
3091be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Google Test before calling RUN_ALL_TESTS().
3101be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//
3111be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// A user must call testing::InitGoogleTest() to initialize Google
3121be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Test.  g_init_gtest_count is set to the number of times
3131be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// InitGoogleTest() has been called.  We don't protect this variable
3141be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// under a mutex as it is only accessed in the main thread.
315fc2de66453b0669c09eaca643b07d34443858b6fElliott HughesGTEST_API_ int g_init_gtest_count = 0;
3161be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniastatic bool GTestIsInitialized() { return g_init_gtest_count != 0; }
3171be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
31841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// Iterates over a vector of TestCases, keeping a running sum of the
3191be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// results of calling a given int-returning method on each.
3201be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Returns the sum.
32141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotstatic int SumOverTestCaseList(const std::vector<TestCase*>& case_list,
3221be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania                               int (TestCase::*method)() const) {
3231be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  int sum = 0;
32441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  for (size_t i = 0; i < case_list.size(); i++) {
32541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot    sum += (case_list[i]->*method)();
3261be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  }
3271be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  return sum;
3281be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}
3291be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
3301be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Returns true iff the test case passed.
3311be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniastatic bool TestCasePassed(const TestCase* test_case) {
3321be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  return test_case->should_run() && test_case->Passed();
3331be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}
3341be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
3351be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Returns true iff the test case failed.
3361be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniastatic bool TestCaseFailed(const TestCase* test_case) {
3371be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  return test_case->should_run() && test_case->Failed();
3381be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}
3391be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
3401be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Returns true iff test_case contains at least one test that should
3411be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// run.
3421be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniastatic bool ShouldRunTestCase(const TestCase* test_case) {
3431be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  return test_case->should_run();
3441be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}
3451be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
3461be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// AssertHelper constructor.
34741d0579e8de9ef4ff178fc4991043c61a19943f7Brett ChabotAssertHelper::AssertHelper(TestPartResult::Type type,
34841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot                           const char* file,
34941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot                           int line,
35041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot                           const char* message)
35141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot    : data_(new AssertHelperData(type, file, line, message)) {
35241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot}
35341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot
35441d0579e8de9ef4ff178fc4991043c61a19943f7Brett ChabotAssertHelper::~AssertHelper() {
35541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  delete data_;
3561be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}
3571be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
3581be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Message assignment, for assertion streaming support.
3591be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniavoid AssertHelper::operator=(const Message& message) const {
3601be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  UnitTest::GetInstance()->
36141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot    AddTestPartResult(data_->type, data_->file, data_->line,
36241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot                      AppendUserMessage(data_->message, message),
3631be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania                      UnitTest::GetInstance()->impl()
3641be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania                      ->CurrentOsStackTraceExceptTop(1)
3651be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania                      // Skips the stack frame for this function itself.
3661be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania                      );  // NOLINT
3671be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}
3681be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
3691be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Mutex for linked pointers.
370fc2de66453b0669c09eaca643b07d34443858b6fElliott HughesGTEST_API_ GTEST_DEFINE_STATIC_MUTEX_(g_linked_ptr_mutex);
3711be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
3721be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Application pathname gotten in InitGoogleTest.
373fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughesstd::string g_executable_path;
3741be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
3751be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Returns the current application's name, removing directory path if that
3761be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// is present.
3771be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaFilePath GetCurrentExecutableName() {
3781be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  FilePath result;
3791be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
38041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot#if GTEST_OS_WINDOWS
3811be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  result.Set(FilePath(g_executable_path).RemoveExtension("exe"));
3821be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#else
3831be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  result.Set(FilePath(g_executable_path));
38441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot#endif  // GTEST_OS_WINDOWS
3851be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
3861be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  return result.RemoveDirectoryName();
3871be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}
3881be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
3891be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Functions for processing the gtest_output flag.
3901be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
3911be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Returns the output format, or "" for normal printed output.
392fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughesstd::string UnitTestOptions::GetOutputFormat() {
3931be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  const char* const gtest_output_flag = GTEST_FLAG(output).c_str();
394fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes  if (gtest_output_flag == NULL) return std::string("");
3951be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
3961be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  const char* const colon = strchr(gtest_output_flag, ':');
3971be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  return (colon == NULL) ?
398fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes      std::string(gtest_output_flag) :
399fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes      std::string(gtest_output_flag, colon - gtest_output_flag);
4001be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}
4011be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
4021be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Returns the name of the requested output file, or the default if none
4031be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// was explicitly specified.
404fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughesstd::string UnitTestOptions::GetAbsolutePathToOutputFile() {
4051be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  const char* const gtest_output_flag = GTEST_FLAG(output).c_str();
4061be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  if (gtest_output_flag == NULL)
407fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes    return "";
4081be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
4091be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  const char* const colon = strchr(gtest_output_flag, ':');
4101be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  if (colon == NULL)
411fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes    return internal::FilePath::ConcatPaths(
412fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes        internal::FilePath(
413fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes            UnitTest::GetInstance()->original_working_dir()),
414fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes        internal::FilePath(kDefaultOutputFile)).string();
4151be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
4161be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  internal::FilePath output_name(colon + 1);
4171be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  if (!output_name.IsAbsolutePath())
4181be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    // TODO(wan@google.com): on Windows \some\path is not an absolute
4191be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    // path (as its meaning depends on the current drive), yet the
4201be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    // following logic for turning it into an absolute path is wrong.
4211be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    // Fix it.
4221be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    output_name = internal::FilePath::ConcatPaths(
4231be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania        internal::FilePath(UnitTest::GetInstance()->original_working_dir()),
4241be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania        internal::FilePath(colon + 1));
4251be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
4261be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  if (!output_name.IsDirectory())
427fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes    return output_name.string();
4281be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
4291be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  internal::FilePath result(internal::FilePath::GenerateUniqueFileName(
4301be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania      output_name, internal::GetCurrentExecutableName(),
4311be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania      GetOutputFormat().c_str()));
432fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes  return result.string();
4331be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}
4341be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
4351be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Returns true iff the wildcard pattern matches the string.  The
4361be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// first ':' or '\0' character in pattern marks the end of it.
4371be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//
4381be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// This recursive algorithm isn't very efficient, but is clear and
4391be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// works well enough for matching test names, which are short.
4401be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniabool UnitTestOptions::PatternMatchesString(const char *pattern,
4411be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania                                           const char *str) {
4421be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  switch (*pattern) {
4431be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    case '\0':
4441be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    case ':':  // Either ':' or '\0' marks the end of the pattern.
4451be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania      return *str == '\0';
4461be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    case '?':  // Matches any single character.
4471be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania      return *str != '\0' && PatternMatchesString(pattern + 1, str + 1);
4481be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    case '*':  // Matches any string (possibly empty) of characters.
4491be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania      return (*str != '\0' && PatternMatchesString(pattern, str + 1)) ||
4501be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania          PatternMatchesString(pattern + 1, str);
4511be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    default:  // Non-special character.  Matches itself.
4521be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania      return *pattern == *str &&
4531be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania          PatternMatchesString(pattern + 1, str + 1);
4541be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  }
4551be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}
4561be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
457fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughesbool UnitTestOptions::MatchesFilter(
458fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes    const std::string& name, const char* filter) {
4591be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  const char *cur_pattern = filter;
4601be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  for (;;) {
4611be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    if (PatternMatchesString(cur_pattern, name.c_str())) {
4621be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania      return true;
4631be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    }
4641be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
4651be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    // Finds the next pattern in the filter.
4661be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    cur_pattern = strchr(cur_pattern, ':');
4671be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
4681be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    // Returns if no more pattern can be found.
4691be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    if (cur_pattern == NULL) {
4701be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania      return false;
4711be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    }
4721be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
4731be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    // Skips the pattern separater (the ':' character).
4741be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    cur_pattern++;
4751be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  }
4761be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}
4771be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
4781be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Returns true iff the user-specified filter matches the test case
4791be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// name and the test name.
480fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughesbool UnitTestOptions::FilterMatchesTest(const std::string &test_case_name,
481fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes                                        const std::string &test_name) {
482fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes  const std::string& full_name = test_case_name + "." + test_name.c_str();
4831be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
4841be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // Split --gtest_filter at '-', if there is one, to separate into
4851be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // positive filter and negative filter portions
4861be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  const char* const p = GTEST_FLAG(filter).c_str();
4871be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  const char* const dash = strchr(p, '-');
488fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes  std::string positive;
489fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes  std::string negative;
4901be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  if (dash == NULL) {
4911be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    positive = GTEST_FLAG(filter).c_str();  // Whole string is a positive filter
492fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes    negative = "";
4931be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  } else {
494fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes    positive = std::string(p, dash);   // Everything up to the dash
495fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes    negative = std::string(dash + 1);  // Everything after the dash
4961be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    if (positive.empty()) {
4971be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania      // Treat '-test1' as the same as '*-test1'
4981be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania      positive = kUniversalFilter;
4991be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    }
5001be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  }
5011be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
5021be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // A filter is a colon-separated list of patterns.  It matches a
5031be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // test if any pattern in it matches the test.
5041be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  return (MatchesFilter(full_name, positive.c_str()) &&
5051be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania          !MatchesFilter(full_name, negative.c_str()));
5061be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}
5071be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
50841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot#if GTEST_HAS_SEH
5091be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Returns EXCEPTION_EXECUTE_HANDLER if Google Test should handle the
5101be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// given SEH exception, or EXCEPTION_CONTINUE_SEARCH otherwise.
5111be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// This function is useful as an __except condition.
5121be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniaint UnitTestOptions::GTestShouldProcessSEH(DWORD exception_code) {
51341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  // Google Test should handle a SEH exception if:
5141be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  //   1. the user wants it to, AND
51541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  //   2. this is not a breakpoint exception, AND
51641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  //   3. this is not a C++ exception (VC++ implements them via SEH,
51741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  //      apparently).
51841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  //
51941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  // SEH exception code for C++ exceptions.
52041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  // (see http://support.microsoft.com/kb/185294 for more information).
52141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  const DWORD kCxxExceptionCode = 0xe06d7363;
5221be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
52341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  bool should_handle = true;
5241be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
52541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  if (!GTEST_FLAG(catch_exceptions))
52641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot    should_handle = false;
52741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  else if (exception_code == EXCEPTION_BREAKPOINT)
52841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot    should_handle = false;
52941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  else if (exception_code == kCxxExceptionCode)
53041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot    should_handle = false;
5311be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
53241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  return should_handle ? EXCEPTION_EXECUTE_HANDLER : EXCEPTION_CONTINUE_SEARCH;
53341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot}
53441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot#endif  // GTEST_HAS_SEH
5351be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
53641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot}  // namespace internal
5371be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
5381be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// The c'tor sets this object as the test part result reporter used by
5391be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Google Test.  The 'result' parameter specifies where to report the
5401be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// results. Intercepts only failures from the current thread.
5411be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaScopedFakeTestPartResultReporter::ScopedFakeTestPartResultReporter(
5421be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    TestPartResultArray* result)
5431be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    : intercept_mode_(INTERCEPT_ONLY_CURRENT_THREAD),
5441be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania      result_(result) {
5451be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  Init();
5461be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}
5471be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
5481be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// The c'tor sets this object as the test part result reporter used by
5491be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Google Test.  The 'result' parameter specifies where to report the
5501be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// results.
5511be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaScopedFakeTestPartResultReporter::ScopedFakeTestPartResultReporter(
5521be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    InterceptMode intercept_mode, TestPartResultArray* result)
5531be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    : intercept_mode_(intercept_mode),
5541be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania      result_(result) {
5551be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  Init();
5561be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}
5571be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
5581be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniavoid ScopedFakeTestPartResultReporter::Init() {
55941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  internal::UnitTestImpl* const impl = internal::GetUnitTestImpl();
5601be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  if (intercept_mode_ == INTERCEPT_ALL_THREADS) {
5611be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    old_reporter_ = impl->GetGlobalTestPartResultReporter();
5621be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    impl->SetGlobalTestPartResultReporter(this);
5631be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  } else {
5641be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    old_reporter_ = impl->GetTestPartResultReporterForCurrentThread();
5651be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    impl->SetTestPartResultReporterForCurrentThread(this);
5661be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  }
5671be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}
5681be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
5691be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// The d'tor restores the test part result reporter used by Google Test
5701be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// before.
5711be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaScopedFakeTestPartResultReporter::~ScopedFakeTestPartResultReporter() {
57241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  internal::UnitTestImpl* const impl = internal::GetUnitTestImpl();
5731be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  if (intercept_mode_ == INTERCEPT_ALL_THREADS) {
5741be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    impl->SetGlobalTestPartResultReporter(old_reporter_);
5751be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  } else {
5761be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    impl->SetTestPartResultReporterForCurrentThread(old_reporter_);
5771be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  }
5781be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}
5791be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
5801be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Increments the test part result count and remembers the result.
5811be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// This method is from the TestPartResultReporterInterface interface.
5821be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniavoid ScopedFakeTestPartResultReporter::ReportTestPartResult(
5831be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    const TestPartResult& result) {
5841be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  result_->Append(result);
5851be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}
5861be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
5871be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catanianamespace internal {
5881be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
5891be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Returns the type ID of ::testing::Test.  We should always call this
5901be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// instead of GetTypeId< ::testing::Test>() to get the type ID of
5911be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// testing::Test.  This is to work around a suspected linker bug when
5921be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// using Google Test as a framework on Mac OS X.  The bug causes
5931be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// GetTypeId< ::testing::Test>() to return different values depending
5941be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// on whether the call is from the Google Test framework itself or
5951be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// from user test code.  GetTestTypeId() is guaranteed to always
5961be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// return the same value, as it always calls GetTypeId<>() from the
5971be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// gtest.cc, which is within the Google Test framework.
5981be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaTypeId GetTestTypeId() {
5991be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  return GetTypeId<Test>();
6001be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}
6011be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
6021be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// The value of GetTestTypeId() as seen from within the Google Test
6031be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// library.  This is solely for testing GetTestTypeId().
6041be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniaextern const TypeId kTestTypeIdInGoogleTest = GetTestTypeId();
6051be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
6061be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// This predicate-formatter checks that 'results' contains a test part
6071be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// failure of the given type and that the failure message contains the
6081be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// given substring.
6091be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaAssertionResult HasOneFailure(const char* /* results_expr */,
6101be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania                              const char* /* type_expr */,
6111be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania                              const char* /* substr_expr */,
6121be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania                              const TestPartResultArray& results,
61341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot                              TestPartResult::Type type,
61441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot                              const string& substr) {
615fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes  const std::string expected(type == TestPartResult::kFatalFailure ?
61641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot                        "1 fatal failure" :
61741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot                        "1 non-fatal failure");
6181be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  Message msg;
6191be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  if (results.size() != 1) {
6201be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    msg << "Expected: " << expected << "\n"
6211be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania        << "  Actual: " << results.size() << " failures";
6221be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    for (int i = 0; i < results.size(); i++) {
6231be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania      msg << "\n" << results.GetTestPartResult(i);
6241be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    }
62541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot    return AssertionFailure() << msg;
6261be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  }
6271be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
6281be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  const TestPartResult& r = results.GetTestPartResult(0);
6291be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  if (r.type() != type) {
63041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot    return AssertionFailure() << "Expected: " << expected << "\n"
63141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot                              << "  Actual:\n"
63241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot                              << r;
6331be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  }
6341be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
63541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  if (strstr(r.message(), substr.c_str()) == NULL) {
63641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot    return AssertionFailure() << "Expected: " << expected << " containing \""
63741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot                              << substr << "\"\n"
63841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot                              << "  Actual:\n"
63941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot                              << r;
6401be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  }
6411be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
6421be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  return AssertionSuccess();
6431be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}
6441be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
6451be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// The constructor of SingleFailureChecker remembers where to look up
6461be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// test part results, what type of failure we expect, and what
6471be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// substring the failure message should contain.
6481be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaSingleFailureChecker:: SingleFailureChecker(
6491be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    const TestPartResultArray* results,
65041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot    TestPartResult::Type type,
65141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot    const string& substr)
6521be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    : results_(results),
6531be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania      type_(type),
6541be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania      substr_(substr) {}
6551be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
6561be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// The destructor of SingleFailureChecker verifies that the given
6571be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// TestPartResultArray contains exactly one failure that has the given
6581be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// type and contains the given substring.  If that's not the case, a
6591be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// non-fatal failure will be generated.
6601be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaSingleFailureChecker::~SingleFailureChecker() {
66141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  EXPECT_PRED_FORMAT3(HasOneFailure, *results_, type_, substr_);
6621be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}
6631be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
6641be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaDefaultGlobalTestPartResultReporter::DefaultGlobalTestPartResultReporter(
6651be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    UnitTestImpl* unit_test) : unit_test_(unit_test) {}
6661be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
6671be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniavoid DefaultGlobalTestPartResultReporter::ReportTestPartResult(
6681be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    const TestPartResult& result) {
6691be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  unit_test_->current_test_result()->AddTestPartResult(result);
67041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  unit_test_->listeners()->repeater()->OnTestPartResult(result);
6711be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}
6721be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
6731be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaDefaultPerThreadTestPartResultReporter::DefaultPerThreadTestPartResultReporter(
6741be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    UnitTestImpl* unit_test) : unit_test_(unit_test) {}
6751be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
6761be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniavoid DefaultPerThreadTestPartResultReporter::ReportTestPartResult(
6771be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    const TestPartResult& result) {
6781be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  unit_test_->GetGlobalTestPartResultReporter()->ReportTestPartResult(result);
6791be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}
6801be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
6811be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Returns the global test part result reporter.
6821be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaTestPartResultReporterInterface*
6831be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaUnitTestImpl::GetGlobalTestPartResultReporter() {
6841be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  internal::MutexLock lock(&global_test_part_result_reporter_mutex_);
6851be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  return global_test_part_result_repoter_;
6861be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}
6871be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
6881be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Sets the global test part result reporter.
6891be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniavoid UnitTestImpl::SetGlobalTestPartResultReporter(
6901be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    TestPartResultReporterInterface* reporter) {
6911be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  internal::MutexLock lock(&global_test_part_result_reporter_mutex_);
6921be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  global_test_part_result_repoter_ = reporter;
6931be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}
6941be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
6951be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Returns the test part result reporter for the current thread.
6961be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaTestPartResultReporterInterface*
6971be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaUnitTestImpl::GetTestPartResultReporterForCurrentThread() {
6981be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  return per_thread_test_part_result_reporter_.get();
6991be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}
7001be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
7011be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Sets the test part result reporter for the current thread.
7021be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniavoid UnitTestImpl::SetTestPartResultReporterForCurrentThread(
7031be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    TestPartResultReporterInterface* reporter) {
7041be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  per_thread_test_part_result_reporter_.set(reporter);
7051be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}
7061be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
7071be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Gets the number of successful test cases.
7081be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniaint UnitTestImpl::successful_test_case_count() const {
70941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  return CountIf(test_cases_, TestCasePassed);
7101be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}
7111be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
7121be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Gets the number of failed test cases.
7131be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniaint UnitTestImpl::failed_test_case_count() const {
71441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  return CountIf(test_cases_, TestCaseFailed);
7151be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}
7161be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
7171be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Gets the number of all test cases.
7181be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniaint UnitTestImpl::total_test_case_count() const {
71941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  return static_cast<int>(test_cases_.size());
7201be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}
7211be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
7221be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Gets the number of all test cases that contain at least one test
7231be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// that should run.
7241be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniaint UnitTestImpl::test_case_to_run_count() const {
72541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  return CountIf(test_cases_, ShouldRunTestCase);
7261be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}
7271be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
7281be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Gets the number of successful tests.
7291be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniaint UnitTestImpl::successful_test_count() const {
7301be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  return SumOverTestCaseList(test_cases_, &TestCase::successful_test_count);
7311be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}
7321be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
7331be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Gets the number of failed tests.
7341be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniaint UnitTestImpl::failed_test_count() const {
7351be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  return SumOverTestCaseList(test_cases_, &TestCase::failed_test_count);
7361be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}
7371be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
738fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes// Gets the number of disabled tests that will be reported in the XML report.
739fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughesint UnitTestImpl::reportable_disabled_test_count() const {
740fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes  return SumOverTestCaseList(test_cases_,
741fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes                             &TestCase::reportable_disabled_test_count);
742fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes}
743fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes
7441be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Gets the number of disabled tests.
7451be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniaint UnitTestImpl::disabled_test_count() const {
7461be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  return SumOverTestCaseList(test_cases_, &TestCase::disabled_test_count);
7471be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}
7481be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
749fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes// Gets the number of tests to be printed in the XML report.
750fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughesint UnitTestImpl::reportable_test_count() const {
751fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes  return SumOverTestCaseList(test_cases_, &TestCase::reportable_test_count);
752fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes}
753fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes
7541be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Gets the number of all tests.
7551be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniaint UnitTestImpl::total_test_count() const {
7561be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  return SumOverTestCaseList(test_cases_, &TestCase::total_test_count);
7571be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}
7581be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
7591be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Gets the number of tests that should run.
7601be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniaint UnitTestImpl::test_to_run_count() const {
7611be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  return SumOverTestCaseList(test_cases_, &TestCase::test_to_run_count);
7621be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}
7631be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
764fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes// Returns the current OS stack trace as an std::string.
7651be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//
7661be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// The maximum number of stack frames to be included is specified by
7671be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// the gtest_stack_trace_depth flag.  The skip_count parameter
7681be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// specifies the number of top frames to be skipped, which doesn't
7691be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// count against the number of frames to be included.
7701be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//
7711be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// For example, if Foo() calls Bar(), which in turn calls
7721be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// CurrentOsStackTraceExceptTop(1), Foo() will be included in the
7731be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// trace but Bar() and CurrentOsStackTraceExceptTop() won't.
774fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughesstd::string UnitTestImpl::CurrentOsStackTraceExceptTop(int skip_count) {
7751be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  (void)skip_count;
776fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes  return "";
7771be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}
7781be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
77941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// Returns the current time in milliseconds.
78041d0579e8de9ef4ff178fc4991043c61a19943f7Brett ChabotTimeInMillis GetTimeInMillis() {
78141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot#if GTEST_OS_WINDOWS_MOBILE || defined(__BORLANDC__)
78241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  // Difference between 1970-01-01 and 1601-01-01 in milliseconds.
7831be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // http://analogous.blogspot.com/2005/04/epoch.html
78441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  const TimeInMillis kJavaEpochToWinFileTimeDelta =
78541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot    static_cast<TimeInMillis>(116444736UL) * 100000UL;
7861be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  const DWORD kTenthMicrosInMilliSecond = 10000;
7871be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
7881be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  SYSTEMTIME now_systime;
7891be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  FILETIME now_filetime;
7901be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  ULARGE_INTEGER now_int64;
7911be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // TODO(kenton@google.com): Shouldn't this just use
7921be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  //   GetSystemTimeAsFileTime()?
7931be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  GetSystemTime(&now_systime);
7941be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  if (SystemTimeToFileTime(&now_systime, &now_filetime)) {
7951be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    now_int64.LowPart = now_filetime.dwLowDateTime;
7961be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    now_int64.HighPart = now_filetime.dwHighDateTime;
7971be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    now_int64.QuadPart = (now_int64.QuadPart / kTenthMicrosInMilliSecond) -
7981be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania      kJavaEpochToWinFileTimeDelta;
7991be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    return now_int64.QuadPart;
8001be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  }
8011be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  return 0;
8021be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#elif GTEST_OS_WINDOWS && !GTEST_HAS_GETTIMEOFDAY_
8031be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  __timeb64 now;
80441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot
80541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot# ifdef _MSC_VER
80641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot
8071be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // MSVC 8 deprecates _ftime64(), so we want to suppress warning 4996
8081be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // (deprecated function) there.
8091be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // TODO(kenton@google.com): Use GetTickCount()?  Or use
8101be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  //   SystemTimeToFileTime()
81141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot#  pragma warning(push)          // Saves the current warning state.
81241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot#  pragma warning(disable:4996)  // Temporarily disables warning 4996.
8131be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  _ftime64(&now);
81441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot#  pragma warning(pop)           // Restores the warning state.
81541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot# else
81641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot
8171be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  _ftime64(&now);
81841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot
81941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot# endif  // _MSC_VER
82041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot
8211be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  return static_cast<TimeInMillis>(now.time) * 1000 + now.millitm;
8221be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#elif GTEST_HAS_GETTIMEOFDAY_
8231be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  struct timeval now;
8241be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  gettimeofday(&now, NULL);
8251be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  return static_cast<TimeInMillis>(now.tv_sec) * 1000 + now.tv_usec / 1000;
8261be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#else
82741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot# error "Don't know how to get the current time on your system."
8281be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#endif
8291be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}
8301be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
8311be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Utilities
8321be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
833fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes// class String.
8341be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
83541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot#if GTEST_OS_WINDOWS_MOBILE
8361be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Creates a UTF-16 wide string from the given ANSI string, allocating
8371be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// memory using new. The caller is responsible for deleting the return
8381be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// value using delete[]. Returns the wide string, or NULL if the
8391be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// input is NULL.
8401be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaLPCWSTR String::AnsiToUtf16(const char* ansi) {
8411be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  if (!ansi) return NULL;
8421be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  const int length = strlen(ansi);
8431be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  const int unicode_length =
8441be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania      MultiByteToWideChar(CP_ACP, 0, ansi, length,
8451be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania                          NULL, 0);
8461be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  WCHAR* unicode = new WCHAR[unicode_length + 1];
8471be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  MultiByteToWideChar(CP_ACP, 0, ansi, length,
8481be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania                      unicode, unicode_length);
8491be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  unicode[unicode_length] = 0;
8501be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  return unicode;
8511be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}
8521be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
8531be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Creates an ANSI string from the given wide string, allocating
8541be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// memory using new. The caller is responsible for deleting the return
8551be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// value using delete[]. Returns the ANSI string, or NULL if the
8561be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// input is NULL.
8571be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniaconst char* String::Utf16ToAnsi(LPCWSTR utf16_str)  {
8581be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  if (!utf16_str) return NULL;
8591be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  const int ansi_length =
8601be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania      WideCharToMultiByte(CP_ACP, 0, utf16_str, -1,
8611be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania                          NULL, 0, NULL, NULL);
8621be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  char* ansi = new char[ansi_length + 1];
8631be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  WideCharToMultiByte(CP_ACP, 0, utf16_str, -1,
8641be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania                      ansi, ansi_length, NULL, NULL);
8651be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  ansi[ansi_length] = 0;
8661be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  return ansi;
8671be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}
8681be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
86941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot#endif  // GTEST_OS_WINDOWS_MOBILE
8701be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
8711be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Compares two C strings.  Returns true iff they have the same content.
8721be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//
8731be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Unlike strcmp(), this function can handle NULL argument(s).  A NULL
8741be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// C string is considered different to any non-NULL C string,
8751be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// including the empty string.
8761be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniabool String::CStringEquals(const char * lhs, const char * rhs) {
8771be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  if ( lhs == NULL ) return rhs == NULL;
8781be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
8791be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  if ( rhs == NULL ) return false;
8801be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
8811be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  return strcmp(lhs, rhs) == 0;
8821be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}
8831be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
8841be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#if GTEST_HAS_STD_WSTRING || GTEST_HAS_GLOBAL_WSTRING
8851be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
8861be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Converts an array of wide chars to a narrow string using the UTF-8
8871be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// encoding, and streams the result to the given Message object.
88841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotstatic void StreamWideCharsToMessage(const wchar_t* wstr, size_t length,
8891be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania                                     Message* msg) {
89041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  for (size_t i = 0; i != length; ) {  // NOLINT
8911be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    if (wstr[i] != L'\0') {
89241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot      *msg << WideStringToUtf8(wstr + i, static_cast<int>(length - i));
89341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot      while (i != length && wstr[i] != L'\0')
8941be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania        i++;
8951be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    } else {
8961be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania      *msg << '\0';
8971be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania      i++;
8981be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    }
8991be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  }
9001be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}
9011be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
9021be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#endif  // GTEST_HAS_STD_WSTRING || GTEST_HAS_GLOBAL_WSTRING
9031be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
9041be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}  // namespace internal
9051be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
906fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes// Constructs an empty Message.
907fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes// We allocate the stringstream separately because otherwise each use of
908fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes// ASSERT/EXPECT in a procedure adds over 200 bytes to the procedure's
909fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes// stack frame leading to huge stack frames in some cases; gcc does not reuse
910fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes// the stack space.
911fc2de66453b0669c09eaca643b07d34443858b6fElliott HughesMessage::Message() : ss_(new ::std::stringstream) {
912fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes  // By default, we want there to be enough precision when printing
913fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes  // a double to a Message.
914fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes  *ss_ << std::setprecision(std::numeric_limits<double>::digits10 + 2);
915fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes}
916fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes
917fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes// These two overloads allow streaming a wide C string to a Message
918fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes// using the UTF-8 encoding.
919fc2de66453b0669c09eaca643b07d34443858b6fElliott HughesMessage& Message::operator <<(const wchar_t* wide_c_str) {
920fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes  return *this << internal::String::ShowWideCString(wide_c_str);
921fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes}
922fc2de66453b0669c09eaca643b07d34443858b6fElliott HughesMessage& Message::operator <<(wchar_t* wide_c_str) {
923fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes  return *this << internal::String::ShowWideCString(wide_c_str);
924fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes}
925fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes
9261be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#if GTEST_HAS_STD_WSTRING
9271be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Converts the given wide string to a narrow string using the UTF-8
9281be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// encoding, and streams the result to this Message object.
9291be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaMessage& Message::operator <<(const ::std::wstring& wstr) {
9301be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  internal::StreamWideCharsToMessage(wstr.c_str(), wstr.length(), this);
9311be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  return *this;
9321be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}
9331be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#endif  // GTEST_HAS_STD_WSTRING
9341be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
9351be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#if GTEST_HAS_GLOBAL_WSTRING
9361be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Converts the given wide string to a narrow string using the UTF-8
9371be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// encoding, and streams the result to this Message object.
9381be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaMessage& Message::operator <<(const ::wstring& wstr) {
9391be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  internal::StreamWideCharsToMessage(wstr.c_str(), wstr.length(), this);
9401be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  return *this;
9411be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}
9421be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#endif  // GTEST_HAS_GLOBAL_WSTRING
9431be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
944fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes// Gets the text streamed to this object so far as an std::string.
945fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes// Each '\0' character in the buffer is replaced with "\\0".
946fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughesstd::string Message::GetString() const {
947fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes  return internal::StringStreamToString(ss_.get());
948fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes}
949fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes
95041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// AssertionResult constructors.
95141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// Used in EXPECT_TRUE/FALSE(assertion_result).
95241d0579e8de9ef4ff178fc4991043c61a19943f7Brett ChabotAssertionResult::AssertionResult(const AssertionResult& other)
95341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot    : success_(other.success_),
95441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot      message_(other.message_.get() != NULL ?
95541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot               new ::std::string(*other.message_) :
95641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot               static_cast< ::std::string*>(NULL)) {
9571be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}
9581be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
95941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// Returns the assertion's negation. Used with EXPECT/ASSERT_FALSE.
96041d0579e8de9ef4ff178fc4991043c61a19943f7Brett ChabotAssertionResult AssertionResult::operator!() const {
96141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  AssertionResult negation(!success_);
96241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  if (message_.get() != NULL)
96341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot    negation << *message_;
96441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  return negation;
9651be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}
9661be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
9671be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Makes a successful assertion result.
9681be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaAssertionResult AssertionSuccess() {
96941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  return AssertionResult(true);
9701be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}
9711be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
97241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// Makes a failed assertion result.
97341d0579e8de9ef4ff178fc4991043c61a19943f7Brett ChabotAssertionResult AssertionFailure() {
97441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  return AssertionResult(false);
97541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot}
9761be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
9771be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Makes a failed assertion result with the given failure message.
97841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// Deprecated; use AssertionFailure() << message.
9791be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaAssertionResult AssertionFailure(const Message& message) {
98041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  return AssertionFailure() << message;
9811be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}
9821be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
9831be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catanianamespace internal {
9841be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
9851be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Constructs and returns the message for an equality assertion
9861be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// (e.g. ASSERT_EQ, EXPECT_STREQ, etc) failure.
9871be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//
9881be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// The first four parameters are the expressions used in the assertion
9891be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// and their values, as strings.  For example, for ASSERT_EQ(foo, bar)
9901be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// where foo is 5 and bar is 6, we have:
9911be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//
9921be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//   expected_expression: "foo"
9931be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//   actual_expression:   "bar"
9941be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//   expected_value:      "5"
9951be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//   actual_value:        "6"
9961be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//
9971be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// The ignoring_case parameter is true iff the assertion is a
9981be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// *_STRCASEEQ*.  When it's true, the string " (ignoring case)" will
9991be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// be inserted into the message.
10001be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaAssertionResult EqFailure(const char* expected_expression,
10011be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania                          const char* actual_expression,
1002fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes                          const std::string& expected_value,
1003fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes                          const std::string& actual_value,
10041be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania                          bool ignoring_case) {
10051be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  Message msg;
10061be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  msg << "Value of: " << actual_expression;
10071be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  if (actual_value != actual_expression) {
10081be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    msg << "\n  Actual: " << actual_value;
10091be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  }
10101be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
10111be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  msg << "\nExpected: " << expected_expression;
10121be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  if (ignoring_case) {
10131be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    msg << " (ignoring case)";
10141be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  }
10151be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  if (expected_value != expected_expression) {
10161be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    msg << "\nWhich is: " << expected_value;
10171be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  }
10181be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
101941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  return AssertionFailure() << msg;
10201be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}
10211be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
102241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// Constructs a failure message for Boolean assertions such as EXPECT_TRUE.
1023fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughesstd::string GetBoolAssertionFailureMessage(
1024fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes    const AssertionResult& assertion_result,
1025fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes    const char* expression_text,
1026fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes    const char* actual_predicate_value,
1027fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes    const char* expected_predicate_value) {
102841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  const char* actual_message = assertion_result.message();
102941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  Message msg;
103041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  msg << "Value of: " << expression_text
103141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot      << "\n  Actual: " << actual_predicate_value;
103241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  if (actual_message[0] != '\0')
103341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot    msg << " (" << actual_message << ")";
103441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  msg << "\nExpected: " << expected_predicate_value;
103541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  return msg.GetString();
103641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot}
10371be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
10381be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Helper function for implementing ASSERT_NEAR.
10391be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaAssertionResult DoubleNearPredFormat(const char* expr1,
10401be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania                                     const char* expr2,
10411be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania                                     const char* abs_error_expr,
10421be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania                                     double val1,
10431be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania                                     double val2,
10441be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania                                     double abs_error) {
10451be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  const double diff = fabs(val1 - val2);
10461be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  if (diff <= abs_error) return AssertionSuccess();
10471be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
10481be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // TODO(wan): do not print the value of an expression if it's
10491be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // already a literal.
105041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  return AssertionFailure()
105141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot      << "The difference between " << expr1 << " and " << expr2
10521be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania      << " is " << diff << ", which exceeds " << abs_error_expr << ", where\n"
10531be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania      << expr1 << " evaluates to " << val1 << ",\n"
10541be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania      << expr2 << " evaluates to " << val2 << ", and\n"
10551be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania      << abs_error_expr << " evaluates to " << abs_error << ".";
10561be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}
10571be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
10581be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
10591be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Helper template for implementing FloatLE() and DoubleLE().
10601be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniatemplate <typename RawType>
10611be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaAssertionResult FloatingPointLE(const char* expr1,
10621be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania                                const char* expr2,
10631be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania                                RawType val1,
10641be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania                                RawType val2) {
10651be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // Returns success if val1 is less than val2,
10661be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  if (val1 < val2) {
10671be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    return AssertionSuccess();
10681be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  }
10691be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
10701be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // or if val1 is almost equal to val2.
10711be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  const FloatingPoint<RawType> lhs(val1), rhs(val2);
10721be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  if (lhs.AlmostEquals(rhs)) {
10731be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    return AssertionSuccess();
10741be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  }
10751be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
10761be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // Note that the above two checks will both fail if either val1 or
10771be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // val2 is NaN, as the IEEE floating-point standard requires that
10781be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // any predicate involving a NaN must return false.
10791be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
108041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  ::std::stringstream val1_ss;
10811be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  val1_ss << std::setprecision(std::numeric_limits<RawType>::digits10 + 2)
10821be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania          << val1;
10831be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
108441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  ::std::stringstream val2_ss;
10851be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  val2_ss << std::setprecision(std::numeric_limits<RawType>::digits10 + 2)
10861be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania          << val2;
10871be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
108841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  return AssertionFailure()
108941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot      << "Expected: (" << expr1 << ") <= (" << expr2 << ")\n"
109041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot      << "  Actual: " << StringStreamToString(&val1_ss) << " vs "
109141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot      << StringStreamToString(&val2_ss);
10921be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}
10931be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
10941be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}  // namespace internal
10951be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
10961be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Asserts that val1 is less than, or almost equal to, val2.  Fails
10971be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// otherwise.  In particular, it fails if either val1 or val2 is NaN.
10981be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaAssertionResult FloatLE(const char* expr1, const char* expr2,
10991be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania                        float val1, float val2) {
11001be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  return internal::FloatingPointLE<float>(expr1, expr2, val1, val2);
11011be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}
11021be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
11031be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Asserts that val1 is less than, or almost equal to, val2.  Fails
11041be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// otherwise.  In particular, it fails if either val1 or val2 is NaN.
11051be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaAssertionResult DoubleLE(const char* expr1, const char* expr2,
11061be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania                         double val1, double val2) {
11071be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  return internal::FloatingPointLE<double>(expr1, expr2, val1, val2);
11081be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}
11091be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
11101be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catanianamespace internal {
11111be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
11121be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// The helper function for {ASSERT|EXPECT}_EQ with int or enum
11131be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// arguments.
11141be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaAssertionResult CmpHelperEQ(const char* expected_expression,
11151be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania                            const char* actual_expression,
11161be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania                            BiggestInt expected,
11171be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania                            BiggestInt actual) {
11181be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  if (expected == actual) {
11191be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    return AssertionSuccess();
11201be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  }
11211be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
11221be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  return EqFailure(expected_expression,
11231be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania                   actual_expression,
11241be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania                   FormatForComparisonFailureMessage(expected, actual),
11251be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania                   FormatForComparisonFailureMessage(actual, expected),
11261be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania                   false);
11271be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}
11281be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
11291be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// A macro for implementing the helper functions needed to implement
11301be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// ASSERT_?? and EXPECT_?? with integer or enum arguments.  It is here
11311be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// just to avoid copy-and-paste of similar code.
11321be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#define GTEST_IMPL_CMP_HELPER_(op_name, op)\
11331be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaAssertionResult CmpHelper##op_name(const char* expr1, const char* expr2, \
11341be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania                                   BiggestInt val1, BiggestInt val2) {\
11351be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  if (val1 op val2) {\
11361be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    return AssertionSuccess();\
11371be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  } else {\
113841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot    return AssertionFailure() \
113941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot        << "Expected: (" << expr1 << ") " #op " (" << expr2\
11401be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania        << "), actual: " << FormatForComparisonFailureMessage(val1, val2)\
11411be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania        << " vs " << FormatForComparisonFailureMessage(val2, val1);\
11421be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  }\
11431be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}
11441be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
11451be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Implements the helper function for {ASSERT|EXPECT}_NE with int or
11461be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// enum arguments.
11471be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaGTEST_IMPL_CMP_HELPER_(NE, !=)
11481be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Implements the helper function for {ASSERT|EXPECT}_LE with int or
11491be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// enum arguments.
11501be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaGTEST_IMPL_CMP_HELPER_(LE, <=)
11511be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Implements the helper function for {ASSERT|EXPECT}_LT with int or
11521be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// enum arguments.
11531be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaGTEST_IMPL_CMP_HELPER_(LT, < )
11541be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Implements the helper function for {ASSERT|EXPECT}_GE with int or
11551be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// enum arguments.
11561be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaGTEST_IMPL_CMP_HELPER_(GE, >=)
11571be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Implements the helper function for {ASSERT|EXPECT}_GT with int or
11581be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// enum arguments.
11591be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaGTEST_IMPL_CMP_HELPER_(GT, > )
11601be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
11611be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#undef GTEST_IMPL_CMP_HELPER_
11621be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
11631be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// The helper function for {ASSERT|EXPECT}_STREQ.
11641be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaAssertionResult CmpHelperSTREQ(const char* expected_expression,
11651be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania                               const char* actual_expression,
11661be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania                               const char* expected,
11671be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania                               const char* actual) {
11681be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  if (String::CStringEquals(expected, actual)) {
11691be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    return AssertionSuccess();
11701be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  }
11711be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
11721be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  return EqFailure(expected_expression,
11731be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania                   actual_expression,
1174fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes                   PrintToString(expected),
1175fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes                   PrintToString(actual),
11761be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania                   false);
11771be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}
11781be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
11791be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// The helper function for {ASSERT|EXPECT}_STRCASEEQ.
11801be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaAssertionResult CmpHelperSTRCASEEQ(const char* expected_expression,
11811be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania                                   const char* actual_expression,
11821be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania                                   const char* expected,
11831be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania                                   const char* actual) {
11841be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  if (String::CaseInsensitiveCStringEquals(expected, actual)) {
11851be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    return AssertionSuccess();
11861be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  }
11871be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
11881be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  return EqFailure(expected_expression,
11891be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania                   actual_expression,
1190fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes                   PrintToString(expected),
1191fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes                   PrintToString(actual),
11921be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania                   true);
11931be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}
11941be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
11951be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// The helper function for {ASSERT|EXPECT}_STRNE.
11961be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaAssertionResult CmpHelperSTRNE(const char* s1_expression,
11971be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania                               const char* s2_expression,
11981be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania                               const char* s1,
11991be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania                               const char* s2) {
12001be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  if (!String::CStringEquals(s1, s2)) {
12011be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    return AssertionSuccess();
12021be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  } else {
120341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot    return AssertionFailure() << "Expected: (" << s1_expression << ") != ("
120441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot                              << s2_expression << "), actual: \""
120541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot                              << s1 << "\" vs \"" << s2 << "\"";
12061be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  }
12071be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}
12081be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
12091be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// The helper function for {ASSERT|EXPECT}_STRCASENE.
12101be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaAssertionResult CmpHelperSTRCASENE(const char* s1_expression,
12111be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania                                   const char* s2_expression,
12121be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania                                   const char* s1,
12131be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania                                   const char* s2) {
12141be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  if (!String::CaseInsensitiveCStringEquals(s1, s2)) {
12151be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    return AssertionSuccess();
12161be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  } else {
121741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot    return AssertionFailure()
121841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot        << "Expected: (" << s1_expression << ") != ("
12191be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania        << s2_expression << ") (ignoring case), actual: \""
12201be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania        << s1 << "\" vs \"" << s2 << "\"";
12211be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  }
12221be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}
12231be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
12241be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}  // namespace internal
12251be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
12261be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catanianamespace {
12271be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
12281be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Helper functions for implementing IsSubString() and IsNotSubstring().
12291be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
12301be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// This group of overloaded functions return true iff needle is a
12311be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// substring of haystack.  NULL is considered a substring of itself
12321be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// only.
12331be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
12341be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniabool IsSubstringPred(const char* needle, const char* haystack) {
12351be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  if (needle == NULL || haystack == NULL)
12361be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    return needle == haystack;
12371be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
12381be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  return strstr(haystack, needle) != NULL;
12391be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}
12401be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
12411be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniabool IsSubstringPred(const wchar_t* needle, const wchar_t* haystack) {
12421be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  if (needle == NULL || haystack == NULL)
12431be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    return needle == haystack;
12441be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
12451be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  return wcsstr(haystack, needle) != NULL;
12461be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}
12471be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
12481be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// StringType here can be either ::std::string or ::std::wstring.
12491be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniatemplate <typename StringType>
12501be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniabool IsSubstringPred(const StringType& needle,
12511be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania                     const StringType& haystack) {
12521be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  return haystack.find(needle) != StringType::npos;
12531be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}
12541be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
12551be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// This function implements either IsSubstring() or IsNotSubstring(),
12561be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// depending on the value of the expected_to_be_substring parameter.
12571be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// StringType here can be const char*, const wchar_t*, ::std::string,
12581be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// or ::std::wstring.
12591be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniatemplate <typename StringType>
12601be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaAssertionResult IsSubstringImpl(
12611be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    bool expected_to_be_substring,
12621be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    const char* needle_expr, const char* haystack_expr,
12631be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    const StringType& needle, const StringType& haystack) {
12641be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  if (IsSubstringPred(needle, haystack) == expected_to_be_substring)
12651be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    return AssertionSuccess();
12661be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
12671be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  const bool is_wide_string = sizeof(needle[0]) > 1;
12681be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  const char* const begin_string_quote = is_wide_string ? "L\"" : "\"";
126941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  return AssertionFailure()
12701be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania      << "Value of: " << needle_expr << "\n"
12711be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania      << "  Actual: " << begin_string_quote << needle << "\"\n"
12721be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania      << "Expected: " << (expected_to_be_substring ? "" : "not ")
12731be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania      << "a substring of " << haystack_expr << "\n"
127441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot      << "Which is: " << begin_string_quote << haystack << "\"";
12751be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}
12761be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
12771be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}  // namespace
12781be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
12791be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// IsSubstring() and IsNotSubstring() check whether needle is a
12801be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// substring of haystack (NULL is considered a substring of itself
12811be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// only), and return an appropriate error message when they fail.
12821be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
12831be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaAssertionResult IsSubstring(
12841be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    const char* needle_expr, const char* haystack_expr,
12851be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    const char* needle, const char* haystack) {
12861be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  return IsSubstringImpl(true, needle_expr, haystack_expr, needle, haystack);
12871be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}
12881be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
12891be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaAssertionResult IsSubstring(
12901be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    const char* needle_expr, const char* haystack_expr,
12911be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    const wchar_t* needle, const wchar_t* haystack) {
12921be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  return IsSubstringImpl(true, needle_expr, haystack_expr, needle, haystack);
12931be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}
12941be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
12951be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaAssertionResult IsNotSubstring(
12961be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    const char* needle_expr, const char* haystack_expr,
12971be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    const char* needle, const char* haystack) {
12981be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  return IsSubstringImpl(false, needle_expr, haystack_expr, needle, haystack);
12991be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}
13001be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
13011be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaAssertionResult IsNotSubstring(
13021be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    const char* needle_expr, const char* haystack_expr,
13031be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    const wchar_t* needle, const wchar_t* haystack) {
13041be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  return IsSubstringImpl(false, needle_expr, haystack_expr, needle, haystack);
13051be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}
13061be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
13071be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaAssertionResult IsSubstring(
13081be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    const char* needle_expr, const char* haystack_expr,
13091be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    const ::std::string& needle, const ::std::string& haystack) {
13101be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  return IsSubstringImpl(true, needle_expr, haystack_expr, needle, haystack);
13111be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}
13121be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
13131be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaAssertionResult IsNotSubstring(
13141be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    const char* needle_expr, const char* haystack_expr,
13151be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    const ::std::string& needle, const ::std::string& haystack) {
13161be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  return IsSubstringImpl(false, needle_expr, haystack_expr, needle, haystack);
13171be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}
13181be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
13191be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#if GTEST_HAS_STD_WSTRING
13201be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaAssertionResult IsSubstring(
13211be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    const char* needle_expr, const char* haystack_expr,
13221be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    const ::std::wstring& needle, const ::std::wstring& haystack) {
13231be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  return IsSubstringImpl(true, needle_expr, haystack_expr, needle, haystack);
13241be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}
13251be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
13261be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaAssertionResult IsNotSubstring(
13271be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    const char* needle_expr, const char* haystack_expr,
13281be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    const ::std::wstring& needle, const ::std::wstring& haystack) {
13291be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  return IsSubstringImpl(false, needle_expr, haystack_expr, needle, haystack);
13301be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}
13311be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#endif  // GTEST_HAS_STD_WSTRING
13321be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
13331be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catanianamespace internal {
13341be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
13351be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#if GTEST_OS_WINDOWS
13361be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
13371be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catanianamespace {
13381be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
13391be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Helper function for IsHRESULT{SuccessFailure} predicates
13401be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaAssertionResult HRESULTFailureHelper(const char* expr,
13411be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania                                     const char* expected,
13421be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania                                     long hr) {  // NOLINT
134341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot# if GTEST_OS_WINDOWS_MOBILE
134441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot
13451be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // Windows CE doesn't support FormatMessage.
13461be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  const char error_text[] = "";
134741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot
134841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot# else
134941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot
13501be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // Looks up the human-readable system message for the HRESULT code
13511be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // and since we're not passing any params to FormatMessage, we don't
13521be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // want inserts expanded.
13531be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  const DWORD kFlags = FORMAT_MESSAGE_FROM_SYSTEM |
13541be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania                       FORMAT_MESSAGE_IGNORE_INSERTS;
1355fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes  const DWORD kBufSize = 4096;
13561be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // Gets the system's human readable message string for this HRESULT.
13571be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  char error_text[kBufSize] = { '\0' };
13581be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  DWORD message_length = ::FormatMessageA(kFlags,
13591be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania                                          0,  // no source, we're asking system
13601be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania                                          hr,  // the error
13611be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania                                          0,  // no line width restrictions
13621be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania                                          error_text,  // output buffer
13631be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania                                          kBufSize,  // buf size
13641be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania                                          NULL);  // no arguments for inserts
1365fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes  // Trims tailing white space (FormatMessage leaves a trailing CR-LF)
136641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  for (; message_length && IsSpace(error_text[message_length - 1]);
13671be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania          --message_length) {
13681be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    error_text[message_length - 1] = '\0';
13691be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  }
137041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot
137141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot# endif  // GTEST_OS_WINDOWS_MOBILE
13721be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
1373fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes  const std::string error_hex("0x" + String::FormatHexInt(hr));
137441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  return ::testing::AssertionFailure()
137541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot      << "Expected: " << expr << " " << expected << ".\n"
1376fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes      << "  Actual: " << error_hex << " " << error_text << "\n";
13771be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}
13781be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
13791be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}  // namespace
13801be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
13811be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaAssertionResult IsHRESULTSuccess(const char* expr, long hr) {  // NOLINT
13821be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  if (SUCCEEDED(hr)) {
13831be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    return AssertionSuccess();
13841be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  }
13851be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  return HRESULTFailureHelper(expr, "succeeds", hr);
13861be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}
13871be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
13881be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaAssertionResult IsHRESULTFailure(const char* expr, long hr) {  // NOLINT
13891be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  if (FAILED(hr)) {
13901be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    return AssertionSuccess();
13911be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  }
13921be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  return HRESULTFailureHelper(expr, "fails", hr);
13931be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}
13941be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
13951be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#endif  // GTEST_OS_WINDOWS
13961be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
13971be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Utility functions for encoding Unicode text (wide strings) in
13981be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// UTF-8.
13991be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
14001be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// A Unicode code-point can have upto 21 bits, and is encoded in UTF-8
14011be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// like this:
14021be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//
14031be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Code-point length   Encoding
14041be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//   0 -  7 bits       0xxxxxxx
14051be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//   8 - 11 bits       110xxxxx 10xxxxxx
14061be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//  12 - 16 bits       1110xxxx 10xxxxxx 10xxxxxx
14071be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//  17 - 21 bits       11110xxx 10xxxxxx 10xxxxxx 10xxxxxx
14081be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
14091be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// The maximum code-point a one-byte UTF-8 sequence can represent.
14101be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniaconst UInt32 kMaxCodePoint1 = (static_cast<UInt32>(1) <<  7) - 1;
14111be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
14121be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// The maximum code-point a two-byte UTF-8 sequence can represent.
14131be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniaconst UInt32 kMaxCodePoint2 = (static_cast<UInt32>(1) << (5 + 6)) - 1;
14141be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
14151be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// The maximum code-point a three-byte UTF-8 sequence can represent.
14161be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniaconst UInt32 kMaxCodePoint3 = (static_cast<UInt32>(1) << (4 + 2*6)) - 1;
14171be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
14181be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// The maximum code-point a four-byte UTF-8 sequence can represent.
14191be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniaconst UInt32 kMaxCodePoint4 = (static_cast<UInt32>(1) << (3 + 3*6)) - 1;
14201be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
14211be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Chops off the n lowest bits from a bit pattern.  Returns the n
14221be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// lowest bits.  As a side effect, the original bit pattern will be
14231be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// shifted to the right by n bits.
14241be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniainline UInt32 ChopLowBits(UInt32* bits, int n) {
14251be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  const UInt32 low_bits = *bits & ((static_cast<UInt32>(1) << n) - 1);
14261be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  *bits >>= n;
14271be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  return low_bits;
14281be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}
14291be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
14301be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Converts a Unicode code point to a narrow string in UTF-8 encoding.
14311be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// code_point parameter is of type UInt32 because wchar_t may not be
14321be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// wide enough to contain a code point.
14331be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// If the code_point is not a valid Unicode code point
1434fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes// (i.e. outside of Unicode range U+0 to U+10FFFF) it will be converted
1435fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes// to "(Invalid Unicode 0xXXXXXXXX)".
1436fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughesstd::string CodePointToUtf8(UInt32 code_point) {
1437fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes  if (code_point > kMaxCodePoint4) {
1438fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes    return "(Invalid Unicode 0x" + String::FormatHexInt(code_point) + ")";
1439fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes  }
1440fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes
1441fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes  char str[5];  // Big enough for the largest valid code point.
14421be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  if (code_point <= kMaxCodePoint1) {
14431be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    str[1] = '\0';
14441be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    str[0] = static_cast<char>(code_point);                          // 0xxxxxxx
14451be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  } else if (code_point <= kMaxCodePoint2) {
14461be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    str[2] = '\0';
14471be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    str[1] = static_cast<char>(0x80 | ChopLowBits(&code_point, 6));  // 10xxxxxx
14481be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    str[0] = static_cast<char>(0xC0 | code_point);                   // 110xxxxx
14491be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  } else if (code_point <= kMaxCodePoint3) {
14501be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    str[3] = '\0';
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>(0xE0 | code_point);                   // 1110xxxx
1454fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes  } else {  // code_point <= kMaxCodePoint4
14551be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    str[4] = '\0';
14561be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    str[3] = static_cast<char>(0x80 | ChopLowBits(&code_point, 6));  // 10xxxxxx
14571be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    str[2] = static_cast<char>(0x80 | ChopLowBits(&code_point, 6));  // 10xxxxxx
14581be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    str[1] = static_cast<char>(0x80 | ChopLowBits(&code_point, 6));  // 10xxxxxx
14591be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    str[0] = static_cast<char>(0xF0 | code_point);                   // 11110xxx
14601be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  }
14611be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  return str;
14621be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}
14631be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
14641be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// The following two functions only make sense if the the system
14651be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// uses UTF-16 for wide string encoding. All supported systems
14661be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// with 16 bit wchar_t (Windows, Cygwin, Symbian OS) do use UTF-16.
14671be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
14681be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Determines if the arguments constitute UTF-16 surrogate pair
14691be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// and thus should be combined into a single Unicode code point
14701be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// using CreateCodePointFromUtf16SurrogatePair.
14711be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniainline bool IsUtf16SurrogatePair(wchar_t first, wchar_t second) {
14721be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  return sizeof(wchar_t) == 2 &&
14731be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania      (first & 0xFC00) == 0xD800 && (second & 0xFC00) == 0xDC00;
14741be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}
14751be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
14761be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Creates a Unicode code point from UTF16 surrogate pair.
14771be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniainline UInt32 CreateCodePointFromUtf16SurrogatePair(wchar_t first,
14781be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania                                                    wchar_t second) {
14791be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  const UInt32 mask = (1 << 10) - 1;
14801be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  return (sizeof(wchar_t) == 2) ?
14811be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania      (((first & mask) << 10) | (second & mask)) + 0x10000 :
14821be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania      // This function should not be called when the condition is
14831be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania      // false, but we provide a sensible default in case it is.
14841be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania      static_cast<UInt32>(first);
14851be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}
14861be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
14871be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Converts a wide string to a narrow string in UTF-8 encoding.
14881be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// The wide string is assumed to have the following encoding:
14891be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//   UTF-16 if sizeof(wchar_t) == 2 (on Windows, Cygwin, Symbian OS)
14901be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//   UTF-32 if sizeof(wchar_t) == 4 (on Linux)
14911be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Parameter str points to a null-terminated wide string.
14921be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Parameter num_chars may additionally limit the number
14931be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// of wchar_t characters processed. -1 is used when the entire string
14941be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// should be processed.
14951be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// If the string contains code points that are not valid Unicode code points
14961be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// (i.e. outside of Unicode range U+0 to U+10FFFF) they will be output
14971be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// as '(Invalid Unicode 0xXXXXXXXX)'. If the string is in UTF16 encoding
14981be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// and contains invalid UTF-16 surrogate pairs, values in those pairs
14991be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// will be encoded as individual Unicode characters from Basic Normal Plane.
1500fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughesstd::string WideStringToUtf8(const wchar_t* str, int num_chars) {
15011be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  if (num_chars == -1)
15021be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    num_chars = static_cast<int>(wcslen(str));
15031be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
150441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  ::std::stringstream stream;
15051be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  for (int i = 0; i < num_chars; ++i) {
15061be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    UInt32 unicode_code_point;
15071be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
15081be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    if (str[i] == L'\0') {
15091be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania      break;
15101be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    } else if (i + 1 < num_chars && IsUtf16SurrogatePair(str[i], str[i + 1])) {
15111be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania      unicode_code_point = CreateCodePointFromUtf16SurrogatePair(str[i],
15121be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania                                                                 str[i + 1]);
15131be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania      i++;
15141be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    } else {
15151be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania      unicode_code_point = static_cast<UInt32>(str[i]);
15161be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    }
15171be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
1518fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes    stream << CodePointToUtf8(unicode_code_point);
15191be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  }
152041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  return StringStreamToString(&stream);
15211be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}
15221be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
1523fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes// Converts a wide C string to an std::string using the UTF-8 encoding.
15241be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// NULL will be converted to "(null)".
1525fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughesstd::string String::ShowWideCString(const wchar_t * wide_c_str) {
1526fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes  if (wide_c_str == NULL)  return "(null)";
15271be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
1528fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes  return internal::WideStringToUtf8(wide_c_str, -1);
15291be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}
15301be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
15311be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Compares two wide C strings.  Returns true iff they have the same
15321be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// content.
15331be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//
15341be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Unlike wcscmp(), this function can handle NULL argument(s).  A NULL
15351be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// C string is considered different to any non-NULL C string,
15361be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// including the empty string.
15371be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniabool String::WideCStringEquals(const wchar_t * lhs, const wchar_t * rhs) {
15381be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  if (lhs == NULL) return rhs == NULL;
15391be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
15401be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  if (rhs == NULL) return false;
15411be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
15421be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  return wcscmp(lhs, rhs) == 0;
15431be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}
15441be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
15451be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Helper function for *_STREQ on wide strings.
15461be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaAssertionResult CmpHelperSTREQ(const char* expected_expression,
15471be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania                               const char* actual_expression,
15481be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania                               const wchar_t* expected,
15491be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania                               const wchar_t* actual) {
15501be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  if (String::WideCStringEquals(expected, actual)) {
15511be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    return AssertionSuccess();
15521be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  }
15531be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
15541be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  return EqFailure(expected_expression,
15551be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania                   actual_expression,
1556fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes                   PrintToString(expected),
1557fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes                   PrintToString(actual),
15581be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania                   false);
15591be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}
15601be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
15611be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Helper function for *_STRNE on wide strings.
15621be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaAssertionResult CmpHelperSTRNE(const char* s1_expression,
15631be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania                               const char* s2_expression,
15641be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania                               const wchar_t* s1,
15651be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania                               const wchar_t* s2) {
15661be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  if (!String::WideCStringEquals(s1, s2)) {
15671be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    return AssertionSuccess();
15681be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  }
15691be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
157041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  return AssertionFailure() << "Expected: (" << s1_expression << ") != ("
157141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot                            << s2_expression << "), actual: "
1572fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes                            << PrintToString(s1)
1573fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes                            << " vs " << PrintToString(s2);
15741be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}
15751be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
15761be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Compares two C strings, ignoring case.  Returns true iff they have
15771be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// the same content.
15781be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//
15791be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Unlike strcasecmp(), this function can handle NULL argument(s).  A
15801be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// NULL C string is considered different to any non-NULL C string,
15811be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// including the empty string.
15821be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniabool String::CaseInsensitiveCStringEquals(const char * lhs, const char * rhs) {
158341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  if (lhs == NULL)
158441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot    return rhs == NULL;
158541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  if (rhs == NULL)
158641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot    return false;
158741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  return posix::StrCaseCmp(lhs, rhs) == 0;
15881be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}
15891be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
15901be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // Compares two wide C strings, ignoring case.  Returns true iff they
15911be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // have the same content.
15921be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  //
15931be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // Unlike wcscasecmp(), this function can handle NULL argument(s).
15941be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // A NULL C string is considered different to any non-NULL wide C string,
15951be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // including the empty string.
15961be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // NB: The implementations on different platforms slightly differ.
15971be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // On windows, this method uses _wcsicmp which compares according to LC_CTYPE
15981be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // environment variable. On GNU platform this method uses wcscasecmp
15991be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // which compares according to LC_CTYPE category of the current locale.
16001be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // On MacOS X, it uses towlower, which also uses LC_CTYPE category of the
16011be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // current locale.
16021be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniabool String::CaseInsensitiveWideCStringEquals(const wchar_t* lhs,
16031be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania                                              const wchar_t* rhs) {
160441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  if (lhs == NULL) return rhs == NULL;
16051be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
160641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  if (rhs == NULL) return false;
16071be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
16081be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#if GTEST_OS_WINDOWS
16091be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  return _wcsicmp(lhs, rhs) == 0;
161041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot#elif GTEST_OS_LINUX && !GTEST_OS_LINUX_ANDROID
16111be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  return wcscasecmp(lhs, rhs) == 0;
16121be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#else
161341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  // Android, Mac OS X and Cygwin don't define wcscasecmp.
161441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  // Other unknown OSes may not define it either.
16151be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  wint_t left, right;
16161be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  do {
16171be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    left = towlower(*lhs++);
16181be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    right = towlower(*rhs++);
16191be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  } while (left && left == right);
16201be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  return left == right;
16211be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#endif  // OS selector
16221be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}
16231be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
1624fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes// Returns true iff str ends with the given suffix, ignoring case.
1625fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes// Any string is considered to end with an empty suffix.
1626fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughesbool String::EndsWithCaseInsensitive(
1627fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes    const std::string& str, const std::string& suffix) {
1628fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes  const size_t str_len = str.length();
1629fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes  const size_t suffix_len = suffix.length();
1630fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes  return (str_len >= suffix_len) &&
1631fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes         CaseInsensitiveCStringEquals(str.c_str() + str_len - suffix_len,
1632fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes                                      suffix.c_str());
16331be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}
16341be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
1635fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes// Formats an int value as "%02d".
1636fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughesstd::string String::FormatIntWidth2(int value) {
1637fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes  std::stringstream ss;
1638fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes  ss << std::setfill('0') << std::setw(2) << value;
1639fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes  return ss.str();
16401be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}
16411be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
1642fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes// Formats an int value as "%X".
1643fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughesstd::string String::FormatHexInt(int value) {
1644fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes  std::stringstream ss;
1645fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes  ss << std::hex << std::uppercase << value;
1646fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes  return ss.str();
16471be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}
16481be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
1649fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes// Formats a byte as "%02X".
1650fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughesstd::string String::FormatByte(unsigned char value) {
1651fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes  std::stringstream ss;
1652fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes  ss << std::setfill('0') << std::setw(2) << std::hex << std::uppercase
1653fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes     << static_cast<unsigned int>(value);
1654fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes  return ss.str();
16551be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}
16561be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
1657fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes// Converts the buffer in a stringstream to an std::string, converting NUL
16581be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// bytes to "\\0" along the way.
1659fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughesstd::string StringStreamToString(::std::stringstream* ss) {
16601be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  const ::std::string& str = ss->str();
16611be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  const char* const start = str.c_str();
16621be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  const char* const end = start + str.length();
16631be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
1664fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes  std::string result;
1665fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes  result.reserve(2 * (end - start));
16661be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  for (const char* ch = start; ch != end; ++ch) {
16671be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    if (*ch == '\0') {
1668fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes      result += "\\0";  // Replaces NUL with "\\0";
16691be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    } else {
1670fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes      result += *ch;
16711be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    }
16721be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  }
16731be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
1674fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes  return result;
16751be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}
16761be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
16771be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Appends the user-supplied message to the Google-Test-generated message.
1678fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughesstd::string AppendUserMessage(const std::string& gtest_msg,
1679fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes                              const Message& user_msg) {
16801be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // Appends the user message if it's non-empty.
1681fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes  const std::string user_msg_string = user_msg.GetString();
16821be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  if (user_msg_string.empty()) {
16831be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    return gtest_msg;
16841be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  }
16851be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
1686fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes  return gtest_msg + "\n" + user_msg_string;
16871be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}
16881be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
168941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot}  // namespace internal
169041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot
16911be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// class TestResult
16921be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
16931be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Creates an empty TestResult.
16941be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaTestResult::TestResult()
16951be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    : death_test_count_(0),
16961be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania      elapsed_time_(0) {
16971be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}
16981be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
16991be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// D'tor.
17001be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaTestResult::~TestResult() {
17011be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}
17021be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
170341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// Returns the i-th test part result among all the results. i can
170441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// range from 0 to total_part_count() - 1. If i is not in that range,
170541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// aborts the program.
170641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotconst TestPartResult& TestResult::GetTestPartResult(int i) const {
170741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  if (i < 0 || i >= total_part_count())
170841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot    internal::posix::Abort();
170941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  return test_part_results_.at(i);
171041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot}
171141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot
171241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// Returns the i-th test property. i can range from 0 to
171341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// test_property_count() - 1. If i is not in that range, aborts the
171441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// program.
171541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotconst TestProperty& TestResult::GetTestProperty(int i) const {
171641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  if (i < 0 || i >= test_property_count())
171741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot    internal::posix::Abort();
171841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  return test_properties_.at(i);
171941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot}
172041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot
172141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// Clears the test part results.
172241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotvoid TestResult::ClearTestPartResults() {
172341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  test_part_results_.clear();
172441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot}
172541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot
17261be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Adds a test part result to the list.
17271be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniavoid TestResult::AddTestPartResult(const TestPartResult& test_part_result) {
172841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  test_part_results_.push_back(test_part_result);
17291be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}
17301be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
17311be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Adds a test property to the list. If a property with the same key as the
17321be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// supplied property is already represented, the value of this test_property
17331be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// replaces the old value for that key.
1734fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughesvoid TestResult::RecordProperty(const std::string& xml_element,
1735fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes                                const TestProperty& test_property) {
1736fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes  if (!ValidateTestProperty(xml_element, test_property)) {
17371be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    return;
17381be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  }
173941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  internal::MutexLock lock(&test_properites_mutex_);
174041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  const std::vector<TestProperty>::iterator property_with_matching_key =
174141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot      std::find_if(test_properties_.begin(), test_properties_.end(),
174241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot                   internal::TestPropertyKeyIs(test_property.key()));
174341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  if (property_with_matching_key == test_properties_.end()) {
174441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot    test_properties_.push_back(test_property);
17451be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    return;
17461be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  }
174741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  property_with_matching_key->SetValue(test_property.value());
17481be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}
17491be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
1750fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes// The list of reserved attributes used in the <testsuites> element of XML
1751fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes// output.
1752fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughesstatic const char* const kReservedTestSuitesAttributes[] = {
1753fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes  "disabled",
1754fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes  "errors",
1755fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes  "failures",
1756fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes  "name",
1757fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes  "random_seed",
1758fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes  "tests",
1759fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes  "time",
1760fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes  "timestamp"
1761fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes};
1762fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes
1763fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes// The list of reserved attributes used in the <testsuite> element of XML
1764fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes// output.
1765fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughesstatic const char* const kReservedTestSuiteAttributes[] = {
1766fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes  "disabled",
1767fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes  "errors",
1768fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes  "failures",
1769fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes  "name",
1770fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes  "tests",
1771fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes  "time"
1772fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes};
1773fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes
1774fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes// The list of reserved attributes used in the <testcase> element of XML output.
1775fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughesstatic const char* const kReservedTestCaseAttributes[] = {
1776fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes  "classname",
1777fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes  "name",
1778fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes  "status",
1779fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes  "time",
1780fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes  "type_param",
1781fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes  "value_param"
1782fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes};
1783fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes
1784fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughestemplate <int kSize>
1785fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughesstd::vector<std::string> ArrayAsVector(const char* const (&array)[kSize]) {
1786fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes  return std::vector<std::string>(array, array + kSize);
1787fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes}
1788fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes
1789fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughesstatic std::vector<std::string> GetReservedAttributesForElement(
1790fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes    const std::string& xml_element) {
1791fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes  if (xml_element == "testsuites") {
1792fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes    return ArrayAsVector(kReservedTestSuitesAttributes);
1793fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes  } else if (xml_element == "testsuite") {
1794fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes    return ArrayAsVector(kReservedTestSuiteAttributes);
1795fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes  } else if (xml_element == "testcase") {
1796fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes    return ArrayAsVector(kReservedTestCaseAttributes);
1797fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes  } else {
1798fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes    GTEST_CHECK_(false) << "Unrecognized xml_element provided: " << xml_element;
1799fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes  }
1800fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes  // This code is unreachable but some compilers may not realizes that.
1801fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes  return std::vector<std::string>();
1802fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes}
1803fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes
1804fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughesstatic std::string FormatWordList(const std::vector<std::string>& words) {
1805fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes  Message word_list;
1806fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes  for (size_t i = 0; i < words.size(); ++i) {
1807fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes    if (i > 0 && words.size() > 2) {
1808fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes      word_list << ", ";
1809fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes    }
1810fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes    if (i == words.size() - 1) {
1811fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes      word_list << "and ";
1812fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes    }
1813fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes    word_list << "'" << words[i] << "'";
1814fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes  }
1815fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes  return word_list.GetString();
1816fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes}
1817fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes
1818fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughesbool ValidateTestPropertyName(const std::string& property_name,
1819fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes                              const std::vector<std::string>& reserved_names) {
1820fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes  if (std::find(reserved_names.begin(), reserved_names.end(), property_name) !=
1821fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes          reserved_names.end()) {
1822fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes    ADD_FAILURE() << "Reserved key used in RecordProperty(): " << property_name
1823fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes                  << " (" << FormatWordList(reserved_names)
1824fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes                  << " are reserved by " << GTEST_NAME_ << ")";
18251be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    return false;
18261be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  }
18271be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  return true;
18281be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}
18291be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
1830fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes// Adds a failure if the key is a reserved attribute of the element named
1831fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes// xml_element.  Returns true if the property is valid.
1832fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughesbool TestResult::ValidateTestProperty(const std::string& xml_element,
1833fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes                                      const TestProperty& test_property) {
1834fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes  return ValidateTestPropertyName(test_property.key(),
1835fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes                                  GetReservedAttributesForElement(xml_element));
1836fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes}
1837fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes
18381be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Clears the object.
18391be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniavoid TestResult::Clear() {
184041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  test_part_results_.clear();
184141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  test_properties_.clear();
18421be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  death_test_count_ = 0;
18431be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  elapsed_time_ = 0;
18441be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}
18451be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
184641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// Returns true iff the test failed.
184741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotbool TestResult::Failed() const {
184841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  for (int i = 0; i < total_part_count(); ++i) {
184941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot    if (GetTestPartResult(i).failed())
185041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot      return true;
185141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  }
185241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  return false;
18531be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}
18541be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
18551be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Returns true iff the test part fatally failed.
185641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotstatic bool TestPartFatallyFailed(const TestPartResult& result) {
18571be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  return result.fatally_failed();
18581be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}
18591be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
18601be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Returns true iff the test fatally failed.
18611be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniabool TestResult::HasFatalFailure() const {
186241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  return CountIf(test_part_results_, TestPartFatallyFailed) > 0;
186341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot}
186441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot
186541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// Returns true iff the test part non-fatally failed.
186641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotstatic bool TestPartNonfatallyFailed(const TestPartResult& result) {
186741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  return result.nonfatally_failed();
186841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot}
186941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot
187041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// Returns true iff the test has a non-fatal failure.
187141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotbool TestResult::HasNonfatalFailure() const {
187241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  return CountIf(test_part_results_, TestPartNonfatallyFailed) > 0;
18731be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}
18741be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
18751be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Gets the number of all test parts.  This is the sum of the number
18761be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// of successful test parts and the number of failed test parts.
18771be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniaint TestResult::total_part_count() const {
187841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  return static_cast<int>(test_part_results_.size());
18791be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}
18801be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
188141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// Returns the number of the test properties.
188241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotint TestResult::test_property_count() const {
188341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  return static_cast<int>(test_properties_.size());
188441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot}
18851be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
18861be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// class Test
18871be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
18881be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Creates a Test object.
18891be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
18901be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// The c'tor saves the values of all Google Test flags.
18911be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaTest::Test()
18921be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    : gtest_flag_saver_(new internal::GTestFlagSaver) {
18931be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}
18941be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
18951be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// The d'tor restores the values of all Google Test flags.
18961be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaTest::~Test() {
18971be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  delete gtest_flag_saver_;
18981be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}
18991be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
19001be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Sets up the test fixture.
19011be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//
19021be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// A sub-class may override this.
19031be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniavoid Test::SetUp() {
19041be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}
19051be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
19061be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Tears down the test fixture.
19071be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//
19081be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// A sub-class may override this.
19091be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniavoid Test::TearDown() {
19101be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}
19111be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
19121be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Allows user supplied key value pairs to be recorded for later output.
1913fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughesvoid Test::RecordProperty(const std::string& key, const std::string& value) {
1914fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes  UnitTest::GetInstance()->RecordProperty(key, value);
19151be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}
19161be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
19171be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Allows user supplied key value pairs to be recorded for later output.
1918fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughesvoid Test::RecordProperty(const std::string& key, int value) {
19191be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  Message value_message;
19201be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  value_message << value;
19211be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  RecordProperty(key, value_message.GetString().c_str());
19221be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}
19231be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
192441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotnamespace internal {
19251be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
192641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotvoid ReportFailureInUnknownLocation(TestPartResult::Type result_type,
1927fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes                                    const std::string& message) {
192841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  // This function is a friend of UnitTest and as such has access to
192941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  // AddTestPartResult.
193041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  UnitTest::GetInstance()->AddTestPartResult(
193141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot      result_type,
193241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot      NULL,  // No info about the source file where the exception occurred.
193341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot      -1,    // We have no info on which line caused the exception.
193441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot      message,
1935fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes      "");   // No stack trace, either.
19361be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}
19371be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
193841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot}  // namespace internal
19391be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
19401be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Google Test requires all tests in the same test case to use the same test
19411be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// fixture class.  This function checks if the current test has the
19421be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// same fixture class as the first test in the current test case.  If
19431be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// yes, it returns true; otherwise it generates a Google Test failure and
19441be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// returns false.
19451be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniabool Test::HasSameFixtureClass() {
19461be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  internal::UnitTestImpl* const impl = internal::GetUnitTestImpl();
19471be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  const TestCase* const test_case = impl->current_test_case();
19481be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
19491be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // Info about the first test in the current test case.
195041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  const TestInfo* const first_test_info = test_case->test_info_list()[0];
195141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  const internal::TypeId first_fixture_id = first_test_info->fixture_class_id_;
19521be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  const char* const first_test_name = first_test_info->name();
19531be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
19541be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // Info about the current test.
195541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  const TestInfo* const this_test_info = impl->current_test_info();
195641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  const internal::TypeId this_fixture_id = this_test_info->fixture_class_id_;
19571be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  const char* const this_test_name = this_test_info->name();
19581be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
19591be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  if (this_fixture_id != first_fixture_id) {
19601be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    // Is the first test defined using TEST?
19611be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    const bool first_is_TEST = first_fixture_id == internal::GetTestTypeId();
19621be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    // Is this test defined using TEST?
19631be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    const bool this_is_TEST = this_fixture_id == internal::GetTestTypeId();
19641be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
19651be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    if (first_is_TEST || this_is_TEST) {
19661be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania      // The user mixed TEST and TEST_F in this test case - we'll tell
19671be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania      // him/her how to fix it.
19681be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
19691be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania      // Gets the name of the TEST and the name of the TEST_F.  Note
19701be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania      // that first_is_TEST and this_is_TEST cannot both be true, as
19711be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania      // the fixture IDs are different for the two tests.
19721be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania      const char* const TEST_name =
19731be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania          first_is_TEST ? first_test_name : this_test_name;
19741be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania      const char* const TEST_F_name =
19751be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania          first_is_TEST ? this_test_name : first_test_name;
19761be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
19771be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania      ADD_FAILURE()
19781be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania          << "All tests in the same test case must use the same test fixture\n"
19791be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania          << "class, so mixing TEST_F and TEST in the same test case is\n"
19801be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania          << "illegal.  In test case " << this_test_info->test_case_name()
19811be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania          << ",\n"
19821be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania          << "test " << TEST_F_name << " is defined using TEST_F but\n"
19831be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania          << "test " << TEST_name << " is defined using TEST.  You probably\n"
19841be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania          << "want to change the TEST to TEST_F or move it to another test\n"
19851be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania          << "case.";
19861be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    } else {
19871be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania      // The user defined two fixture classes with the same name in
19881be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania      // two namespaces - we'll tell him/her how to fix it.
19891be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania      ADD_FAILURE()
19901be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania          << "All tests in the same test case must use the same test fixture\n"
19911be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania          << "class.  However, in test case "
19921be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania          << this_test_info->test_case_name() << ",\n"
19931be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania          << "you defined test " << first_test_name
19941be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania          << " and test " << this_test_name << "\n"
19951be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania          << "using two different test fixture classes.  This can happen if\n"
19961be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania          << "the two classes are from different namespaces or translation\n"
19971be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania          << "units and have the same name.  You should probably rename one\n"
19981be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania          << "of the classes to put the tests into different test cases.";
19991be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    }
20001be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    return false;
20011be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  }
20021be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
20031be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  return true;
20041be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}
20051be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
200641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot#if GTEST_HAS_SEH
20071be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
200841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// Adds an "exception thrown" fatal failure to the current test.  This
200941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// function returns its result via an output parameter pointer because VC++
201041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// prohibits creation of objects with destructors on stack in functions
201141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// using __try (see error C2712).
2012fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughesstatic std::string* FormatSehExceptionMessage(DWORD exception_code,
2013fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes                                              const char* location) {
201441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  Message message;
201541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  message << "SEH exception with code 0x" << std::setbase(16) <<
201641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot    exception_code << std::setbase(10) << " thrown in " << location << ".";
20171be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
2018fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes  return new std::string(message.GetString());
201941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot}
202041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot
202141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot#endif  // GTEST_HAS_SEH
202241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot
2023fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughesnamespace internal {
2024fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes
202541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot#if GTEST_HAS_EXCEPTIONS
202641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot
202741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// Adds an "exception thrown" fatal failure to the current test.
2028fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughesstatic std::string FormatCxxExceptionMessage(const char* description,
2029fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes                                             const char* location) {
203041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  Message message;
203141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  if (description != NULL) {
203241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot    message << "C++ exception with description \"" << description << "\"";
203341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  } else {
203441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot    message << "Unknown C++ exception";
20351be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  }
203641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  message << " thrown in " << location << ".";
20371be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
203841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  return message.GetString();
203941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot}
204041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot
2041fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughesstatic std::string PrintTestPartResultToString(
204241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot    const TestPartResult& test_part_result);
204341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot
2044fc2de66453b0669c09eaca643b07d34443858b6fElliott HughesGoogleTestFailureException::GoogleTestFailureException(
2045fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes    const TestPartResult& failure)
2046fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes    : ::std::runtime_error(PrintTestPartResultToString(failure).c_str()) {}
2047fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes
204841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot#endif  // GTEST_HAS_EXCEPTIONS
204941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot
205041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// We put these helper functions in the internal namespace as IBM's xlC
205141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// compiler rejects the code if they were declared static.
205241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot
205341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// Runs the given method and handles SEH exceptions it throws, when
205441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// SEH is supported; returns the 0-value for type Result in case of an
205541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// SEH exception.  (Microsoft compilers cannot handle SEH and C++
205641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// exceptions in the same function.  Therefore, we provide a separate
205741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// wrapper function for handling SEH exceptions.)
205841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabottemplate <class T, typename Result>
205941d0579e8de9ef4ff178fc4991043c61a19943f7Brett ChabotResult HandleSehExceptionsInMethodIfSupported(
206041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot    T* object, Result (T::*method)(), const char* location) {
206141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot#if GTEST_HAS_SEH
20621be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  __try {
206341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot    return (object->*method)();
206441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  } __except (internal::UnitTestOptions::GTestShouldProcessSEH(  // NOLINT
20651be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania      GetExceptionCode())) {
206641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot    // We create the exception message on the heap because VC++ prohibits
206741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot    // creation of objects with destructors on stack in functions using __try
206841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot    // (see error C2712).
2069fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes    std::string* exception_message = FormatSehExceptionMessage(
207041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot        GetExceptionCode(), location);
207141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot    internal::ReportFailureInUnknownLocation(TestPartResult::kFatalFailure,
207241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot                                             *exception_message);
207341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot    delete exception_message;
207441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot    return static_cast<Result>(0);
207541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  }
207641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot#else
207741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  (void)location;
207841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  return (object->*method)();
207941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot#endif  // GTEST_HAS_SEH
208041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot}
208141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot
208241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// Runs the given method and catches and reports C++ and/or SEH-style
208341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// exceptions, if they are supported; returns the 0-value for type
208441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// Result in case of an SEH exception.
208541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabottemplate <class T, typename Result>
208641d0579e8de9ef4ff178fc4991043c61a19943f7Brett ChabotResult HandleExceptionsInMethodIfSupported(
208741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot    T* object, Result (T::*method)(), const char* location) {
208841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  // NOTE: The user code can affect the way in which Google Test handles
208941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  // exceptions by setting GTEST_FLAG(catch_exceptions), but only before
209041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  // RUN_ALL_TESTS() starts. It is technically possible to check the flag
209141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  // after the exception is caught and either report or re-throw the
209241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  // exception based on the flag's value:
209341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  //
209441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  // try {
209541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  //   // Perform the test method.
209641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  // } catch (...) {
209741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  //   if (GTEST_FLAG(catch_exceptions))
209841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  //     // Report the exception as failure.
209941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  //   else
210041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  //     throw;  // Re-throws the original exception.
210141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  // }
210241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  //
210341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  // However, the purpose of this flag is to allow the program to drop into
210441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  // the debugger when the exception is thrown. On most platforms, once the
210541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  // control enters the catch block, the exception origin information is
210641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  // lost and the debugger will stop the program at the point of the
210741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  // re-throw in this function -- instead of at the point of the original
210841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  // throw statement in the code under test.  For this reason, we perform
210941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  // the check early, sacrificing the ability to affect Google Test's
211041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  // exception handling in the method where the exception is thrown.
211141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  if (internal::GetUnitTestImpl()->catch_exceptions()) {
211241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot#if GTEST_HAS_EXCEPTIONS
211341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot    try {
211441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot      return HandleSehExceptionsInMethodIfSupported(object, method, location);
2115fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes    } catch (const internal::GoogleTestFailureException&) {  // NOLINT
2116fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes      // This exception type can only be thrown by a failed Google
2117fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes      // Test assertion with the intention of letting another testing
2118fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes      // framework catch it.  Therefore we just re-throw it.
211941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot      throw;
212041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot    } catch (const std::exception& e) {  // NOLINT
212141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot      internal::ReportFailureInUnknownLocation(
212241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot          TestPartResult::kFatalFailure,
212341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot          FormatCxxExceptionMessage(e.what(), location));
212441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot    } catch (...) {  // NOLINT
212541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot      internal::ReportFailureInUnknownLocation(
212641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot          TestPartResult::kFatalFailure,
212741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot          FormatCxxExceptionMessage(NULL, location));
212841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot    }
212941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot    return static_cast<Result>(0);
213041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot#else
213141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot    return HandleSehExceptionsInMethodIfSupported(object, method, location);
213241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot#endif  // GTEST_HAS_EXCEPTIONS
213341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  } else {
213441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot    return (object->*method)();
21351be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  }
213641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot}
21371be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
213841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot}  // namespace internal
213941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot
214041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// Runs the test and updates the test result.
214141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotvoid Test::Run() {
214241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  if (!HasSameFixtureClass()) return;
21431be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
214441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  internal::UnitTestImpl* const impl = internal::GetUnitTestImpl();
214541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  impl->os_stack_trace_getter()->UponLeavingGTest();
214641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  internal::HandleExceptionsInMethodIfSupported(this, &Test::SetUp, "SetUp()");
21471be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // We will run the test only if SetUp() was successful.
21481be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  if (!HasFatalFailure()) {
21491be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    impl->os_stack_trace_getter()->UponLeavingGTest();
215041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot    internal::HandleExceptionsInMethodIfSupported(
215141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot        this, &Test::TestBody, "the test body");
21521be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  }
21531be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
21541be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // However, we want to clean up as much as possible.  Hence we will
21551be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // always call TearDown(), even if SetUp() or the test body has
21561be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // failed.
21571be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  impl->os_stack_trace_getter()->UponLeavingGTest();
215841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  internal::HandleExceptionsInMethodIfSupported(
215941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot      this, &Test::TearDown, "TearDown()");
21601be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}
21611be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
21621be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Returns true iff the current test has a fatal failure.
21631be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniabool Test::HasFatalFailure() {
21641be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  return internal::GetUnitTestImpl()->current_test_result()->HasFatalFailure();
21651be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}
21661be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
216741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// Returns true iff the current test has a non-fatal failure.
216841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotbool Test::HasNonfatalFailure() {
216941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  return internal::GetUnitTestImpl()->current_test_result()->
217041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot      HasNonfatalFailure();
217141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot}
217241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot
21731be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// class TestInfo
21741be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
21751be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Constructs a TestInfo object. It assumes ownership of the test factory
217641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// object.
2177fc2de66453b0669c09eaca643b07d34443858b6fElliott HughesTestInfo::TestInfo(const std::string& a_test_case_name,
2178fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes                   const std::string& a_name,
217941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot                   const char* a_type_param,
218041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot                   const char* a_value_param,
21811be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania                   internal::TypeId fixture_class_id,
218241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot                   internal::TestFactoryBase* factory)
218341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot    : test_case_name_(a_test_case_name),
218441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot      name_(a_name),
218541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot      type_param_(a_type_param ? new std::string(a_type_param) : NULL),
218641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot      value_param_(a_value_param ? new std::string(a_value_param) : NULL),
218741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot      fixture_class_id_(fixture_class_id),
218841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot      should_run_(false),
218941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot      is_disabled_(false),
219041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot      matches_filter_(false),
219141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot      factory_(factory),
219241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot      result_() {}
21931be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
21941be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Destructs a TestInfo object.
219541d0579e8de9ef4ff178fc4991043c61a19943f7Brett ChabotTestInfo::~TestInfo() { delete factory_; }
21961be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
21971be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catanianamespace internal {
21981be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
21991be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Creates a new TestInfo object and registers it with Google Test;
22001be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// returns the created object.
22011be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//
22021be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Arguments:
22031be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//
22041be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//   test_case_name:   name of the test case
22051be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//   name:             name of the test
220641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot//   type_param:       the name of the test's type parameter, or NULL if
220741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot//                     this is not a typed or a type-parameterized test.
220841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot//   value_param:      text representation of the test's value parameter,
220941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot//                     or NULL if this is not a value-parameterized test.
22101be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//   fixture_class_id: ID of the test fixture class
22111be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//   set_up_tc:        pointer to the function that sets up the test case
22121be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//   tear_down_tc:     pointer to the function that tears down the test case
22131be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//   factory:          pointer to the factory that creates a test object.
22141be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//                     The newly created TestInfo instance will assume
22151be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//                     ownership of the factory object.
22161be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaTestInfo* MakeAndRegisterTestInfo(
2217fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes    const char* test_case_name,
2218fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes    const char* name,
221941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot    const char* type_param,
222041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot    const char* value_param,
22211be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    TypeId fixture_class_id,
22221be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    SetUpTestCaseFunc set_up_tc,
22231be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    TearDownTestCaseFunc tear_down_tc,
22241be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    TestFactoryBase* factory) {
22251be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  TestInfo* const test_info =
222641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot      new TestInfo(test_case_name, name, type_param, value_param,
22271be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania                   fixture_class_id, factory);
22281be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  GetUnitTestImpl()->AddTestInfo(set_up_tc, tear_down_tc, test_info);
22291be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  return test_info;
22301be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}
22311be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
22321be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#if GTEST_HAS_PARAM_TEST
22331be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniavoid ReportInvalidTestCaseType(const char* test_case_name,
22341be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania                               const char* file, int line) {
22351be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  Message errors;
22361be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  errors
22371be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania      << "Attempted redefinition of test case " << test_case_name << ".\n"
22381be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania      << "All tests in the same test case must use the same test fixture\n"
22391be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania      << "class.  However, in test case " << test_case_name << ", you tried\n"
22401be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania      << "to define a test using a fixture class different from the one\n"
22411be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania      << "used earlier. This can happen if the two fixture classes are\n"
22421be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania      << "from different namespaces and have the same name. You should\n"
22431be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania      << "probably rename one of the classes to put the tests into different\n"
22441be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania      << "test cases.";
22451be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
22461be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  fprintf(stderr, "%s %s", FormatFileLocation(file, line).c_str(),
22471be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania          errors.GetString().c_str());
22481be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}
22491be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#endif  // GTEST_HAS_PARAM_TEST
22501be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
22511be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}  // namespace internal
22521be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
22531be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catanianamespace {
22541be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
22551be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// A predicate that checks the test name of a TestInfo against a known
22561be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// value.
22571be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//
22581be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// This is used for implementation of the TestCase class only.  We put
22591be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// it in the anonymous namespace to prevent polluting the outer
22601be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// namespace.
22611be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//
22621be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// TestNameIs is copyable.
22631be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniaclass TestNameIs {
22641be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania public:
22651be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // Constructor.
22661be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  //
22671be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // TestNameIs has NO default constructor.
22681be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  explicit TestNameIs(const char* name)
22691be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania      : name_(name) {}
22701be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
22711be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // Returns true iff the test name of test_info matches name_.
22721be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  bool operator()(const TestInfo * test_info) const {
2273fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes    return test_info && test_info->name() == name_;
22741be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  }
22751be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
22761be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania private:
2277fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes  std::string name_;
22781be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania};
22791be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
22801be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}  // namespace
22811be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
22821be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catanianamespace internal {
22831be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
22841be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// This method expands all parameterized tests registered with macros TEST_P
22851be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// and INSTANTIATE_TEST_CASE_P into regular tests and registers those.
22861be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// This will be done just once during the program runtime.
22871be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniavoid UnitTestImpl::RegisterParameterizedTests() {
22881be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#if GTEST_HAS_PARAM_TEST
22891be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  if (!parameterized_tests_registered_) {
22901be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    parameterized_test_registry_.RegisterTests();
22911be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    parameterized_tests_registered_ = true;
22921be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  }
22931be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#endif
22941be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}
22951be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
229641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot}  // namespace internal
229741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot
22981be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Creates the test object, runs it, records its result, and then
22991be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// deletes it.
230041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotvoid TestInfo::Run() {
23011be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  if (!should_run_) return;
23021be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
23031be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // Tells UnitTest where to store test result.
230441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  internal::UnitTestImpl* const impl = internal::GetUnitTestImpl();
230541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  impl->set_current_test_info(this);
23061be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
230741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  TestEventListener* repeater = UnitTest::GetInstance()->listeners().repeater();
23081be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
230941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  // Notifies the unit test event listeners that a test is about to start.
231041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  repeater->OnTestStart(*this);
23111be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
231241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  const TimeInMillis start = internal::GetTimeInMillis();
23131be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
231441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  impl->os_stack_trace_getter()->UponLeavingGTest();
23151be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
23161be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // Creates the test object.
231741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  Test* const test = internal::HandleExceptionsInMethodIfSupported(
231841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot      factory_, &internal::TestFactoryBase::CreateTest,
231941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot      "the test fixture's constructor");
232041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot
232141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  // Runs the test only if the test object was created and its
232241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  // constructor didn't generate a fatal failure.
232341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  if ((test != NULL) && !Test::HasFatalFailure()) {
232441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot    // This doesn't throw as all user code that can throw are wrapped into
232541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot    // exception handling code.
23261be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    test->Run();
23271be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  }
23281be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
23291be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // Deletes the test object.
23301be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  impl->os_stack_trace_getter()->UponLeavingGTest();
233141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  internal::HandleExceptionsInMethodIfSupported(
233241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot      test, &Test::DeleteSelf_, "the test fixture's destructor");
23331be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
233441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  result_.set_elapsed_time(internal::GetTimeInMillis() - start);
23351be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
23361be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // Notifies the unit test event listener that a test has just finished.
233741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  repeater->OnTestEnd(*this);
23381be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
23391be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // Tells UnitTest to stop associating assertion results to this
23401be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // test.
23411be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  impl->set_current_test_info(NULL);
23421be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}
23431be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
23441be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// class TestCase
23451be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
23461be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Gets the number of successful tests in this test case.
23471be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniaint TestCase::successful_test_count() const {
234841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  return CountIf(test_info_list_, TestPassed);
23491be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}
23501be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
23511be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Gets the number of failed tests in this test case.
23521be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniaint TestCase::failed_test_count() const {
235341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  return CountIf(test_info_list_, TestFailed);
23541be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}
23551be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
2356fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes// Gets the number of disabled tests that will be reported in the XML report.
2357fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughesint TestCase::reportable_disabled_test_count() const {
2358fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes  return CountIf(test_info_list_, TestReportableDisabled);
2359fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes}
2360fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes
2361fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes// Gets the number of disabled tests in this test case.
23621be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniaint TestCase::disabled_test_count() const {
236341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  return CountIf(test_info_list_, TestDisabled);
23641be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}
23651be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
2366fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes// Gets the number of tests to be printed in the XML report.
2367fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughesint TestCase::reportable_test_count() const {
2368fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes  return CountIf(test_info_list_, TestReportable);
2369fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes}
2370fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes
23711be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Get the number of tests in this test case that should run.
23721be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniaint TestCase::test_to_run_count() const {
237341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  return CountIf(test_info_list_, ShouldRunTest);
23741be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}
23751be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
23761be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Gets the number of all tests.
23771be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniaint TestCase::total_test_count() const {
237841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  return static_cast<int>(test_info_list_.size());
23791be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}
23801be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
23811be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Creates a TestCase with the given name.
23821be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//
23831be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Arguments:
23841be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//
23851be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//   name:         name of the test case
238641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot//   a_type_param: the name of the test case's type parameter, or NULL if
238741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot//                 this is not a typed or a type-parameterized test case.
23881be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//   set_up_tc:    pointer to the function that sets up the test case
23891be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//   tear_down_tc: pointer to the function that tears down the test case
239041d0579e8de9ef4ff178fc4991043c61a19943f7Brett ChabotTestCase::TestCase(const char* a_name, const char* a_type_param,
23911be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania                   Test::SetUpTestCaseFunc set_up_tc,
23921be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania                   Test::TearDownTestCaseFunc tear_down_tc)
239341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot    : name_(a_name),
239441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot      type_param_(a_type_param ? new std::string(a_type_param) : NULL),
23951be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania      set_up_tc_(set_up_tc),
23961be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania      tear_down_tc_(tear_down_tc),
23971be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania      should_run_(false),
23981be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania      elapsed_time_(0) {
23991be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}
24001be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
24011be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Destructor of TestCase.
24021be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaTestCase::~TestCase() {
24031be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // Deletes every Test in the collection.
240441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  ForEach(test_info_list_, internal::Delete<TestInfo>);
240541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot}
24061be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
240741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// Returns the i-th test among all the tests. i can range from 0 to
240841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// total_test_count() - 1. If i is not in that range, returns NULL.
240941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotconst TestInfo* TestCase::GetTestInfo(int i) const {
241041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  const int index = GetElementOr(test_indices_, i, -1);
241141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  return index < 0 ? NULL : test_info_list_[index];
241241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot}
241341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot
241441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// Returns the i-th test among all the tests. i can range from 0 to
241541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// total_test_count() - 1. If i is not in that range, returns NULL.
241641d0579e8de9ef4ff178fc4991043c61a19943f7Brett ChabotTestInfo* TestCase::GetMutableTestInfo(int i) {
241741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  const int index = GetElementOr(test_indices_, i, -1);
241841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  return index < 0 ? NULL : test_info_list_[index];
24191be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}
24201be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
24211be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Adds a test to this test case.  Will delete the test upon
24221be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// destruction of the TestCase object.
24231be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniavoid TestCase::AddTestInfo(TestInfo * test_info) {
242441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  test_info_list_.push_back(test_info);
242541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  test_indices_.push_back(static_cast<int>(test_indices_.size()));
24261be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}
24271be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
24281be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Runs every test in this TestCase.
24291be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniavoid TestCase::Run() {
24301be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  if (!should_run_) return;
24311be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
24321be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  internal::UnitTestImpl* const impl = internal::GetUnitTestImpl();
24331be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  impl->set_current_test_case(this);
24341be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
243541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  TestEventListener* repeater = UnitTest::GetInstance()->listeners().repeater();
24361be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
243741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  repeater->OnTestCaseStart(*this);
24381be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  impl->os_stack_trace_getter()->UponLeavingGTest();
243941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  internal::HandleExceptionsInMethodIfSupported(
244041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot      this, &TestCase::RunSetUpTestCase, "SetUpTestCase()");
24411be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
24421be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  const internal::TimeInMillis start = internal::GetTimeInMillis();
244341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  for (int i = 0; i < total_test_count(); i++) {
244441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot    GetMutableTestInfo(i)->Run();
244541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  }
24461be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  elapsed_time_ = internal::GetTimeInMillis() - start;
24471be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
24481be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  impl->os_stack_trace_getter()->UponLeavingGTest();
244941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  internal::HandleExceptionsInMethodIfSupported(
245041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot      this, &TestCase::RunTearDownTestCase, "TearDownTestCase()");
245141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot
245241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  repeater->OnTestCaseEnd(*this);
24531be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  impl->set_current_test_case(NULL);
24541be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}
24551be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
24561be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Clears the results of all tests in this test case.
24571be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniavoid TestCase::ClearResult() {
2458fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes  ad_hoc_test_result_.Clear();
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".
2479fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughesstatic std::string FormatCountableNoun(int count,
2480fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes                                       const char * singular_form,
2481fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes                                       const char * plural_form) {
2482fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes  return internal::StreamableToString(count) + " " +
2483fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes      (count == 1 ? singular_form : plural_form);
24841be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}
24851be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
24861be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Formats the count of tests.
2487fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughesstatic std::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.
2492fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughesstatic std::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
2517fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughesnamespace internal {
2518fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes
2519fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes// Prints a TestPartResult to an std::string.
2520fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughesstatic std::string PrintTestPartResultToString(
25211be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    const TestPartResult& test_part_result) {
25221be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  return (Message()
25231be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania          << internal::FormatFileLocation(test_part_result.file_name(),
25241be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania                                          test_part_result.line_number())
25251be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania          << " " << TestPartResultTypeToString(test_part_result.type())
25261be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania          << test_part_result.message()).GetString();
25271be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}
25281be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
25291be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Prints a TestPartResult.
253041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotstatic void PrintTestPartResult(const TestPartResult& test_part_result) {
2531fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes  const std::string& result =
253241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot      PrintTestPartResultToString(test_part_result);
253341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  printf("%s\n", result.c_str());
25341be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  fflush(stdout);
253541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  // If the test program runs in Visual Studio or a debugger, the
253641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  // following statements add the test part result message to the Output
253741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  // window such that the user can double-click on it to jump to the
253841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  // corresponding source code location; otherwise they do nothing.
253941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot#if GTEST_OS_WINDOWS && !GTEST_OS_WINDOWS_MOBILE
254041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  // We don't call OutputDebugString*() on Windows Mobile, as printing
254141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  // to stdout is done by OutputDebugString() there already - we don't
254241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  // want the same message printed twice.
254341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  ::OutputDebugStringA(result.c_str());
254441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  ::OutputDebugStringA("\n");
254541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot#endif
25461be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}
25471be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
25481be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// class PrettyUnitTestResultPrinter
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") ||
2601fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes        String::CStringEquals(term, "screen-256color") ||
260241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot        String::CStringEquals(term, "linux") ||
26031be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania        String::CStringEquals(term, "cygwin");
26041be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    return stdout_is_tty && term_supports_color;
26051be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#endif  // GTEST_OS_WINDOWS
26061be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  }
26071be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
26081be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  return String::CaseInsensitiveCStringEquals(gtest_color, "yes") ||
26091be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania      String::CaseInsensitiveCStringEquals(gtest_color, "true") ||
26101be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania      String::CaseInsensitiveCStringEquals(gtest_color, "t") ||
26111be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania      String::CStringEquals(gtest_color, "1");
26121be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // We take "yes", "true", "t", and "1" as meaning "yes".  If the
26131be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // value is neither one of these nor "auto", we treat it as "no" to
26141be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // be conservative.
26151be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}
26161be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
26171be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Helpers for printing colored strings to stdout. Note that on Windows, we
26181be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// cannot simply emit special characters and have the terminal change colors.
26191be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// This routine must actually emit the characters rather than return a string
26201be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// that would be colored when printed, as can be done on Linux.
26211be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniavoid ColoredPrintf(GTestColor color, const char* fmt, ...) {
26221be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  va_list args;
26231be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  va_start(args, fmt);
26241be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
2625fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes#if GTEST_OS_WINDOWS_MOBILE || GTEST_OS_SYMBIAN || GTEST_OS_ZOS || GTEST_OS_IOS
26261be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  const bool use_color = false;
26271be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#else
26281be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  static const bool in_color_mode =
262941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot      ShouldUseColor(posix::IsATTY(posix::FileNo(stdout)) != 0);
26301be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  const bool use_color = in_color_mode && (color != COLOR_DEFAULT);
263141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot#endif  // GTEST_OS_WINDOWS_MOBILE || GTEST_OS_SYMBIAN || GTEST_OS_ZOS
26321be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // The '!= 0' comparison is necessary to satisfy MSVC 7.1.
26331be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
26341be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  if (!use_color) {
26351be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    vprintf(fmt, args);
26361be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    va_end(args);
26371be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    return;
26381be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  }
26391be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
264041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot#if GTEST_OS_WINDOWS && !GTEST_OS_WINDOWS_MOBILE
26411be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  const HANDLE stdout_handle = GetStdHandle(STD_OUTPUT_HANDLE);
26421be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
26431be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // Gets the current text color.
26441be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  CONSOLE_SCREEN_BUFFER_INFO buffer_info;
26451be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  GetConsoleScreenBufferInfo(stdout_handle, &buffer_info);
26461be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  const WORD old_color_attrs = buffer_info.wAttributes;
26471be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
264841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  // We need to flush the stream buffers into the console before each
264941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  // SetConsoleTextAttribute call lest it affect the text that is already
265041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  // printed but has not yet reached the console.
265141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  fflush(stdout);
26521be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  SetConsoleTextAttribute(stdout_handle,
26531be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania                          GetColorAttribute(color) | FOREGROUND_INTENSITY);
26541be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  vprintf(fmt, args);
26551be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
265641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  fflush(stdout);
26571be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // Restores the text color.
26581be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  SetConsoleTextAttribute(stdout_handle, old_color_attrs);
26591be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#else
26601be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  printf("\033[0;3%sm", GetAnsiColorCode(color));
26611be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  vprintf(fmt, args);
26621be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  printf("\033[m");  // Resets the terminal to default.
266341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot#endif  // GTEST_OS_WINDOWS && !GTEST_OS_WINDOWS_MOBILE
26641be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  va_end(args);
26651be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}
26661be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
2667fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes// Text printed in Google Test's text output and --gunit_list_tests
2668fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes// output to label the type parameter and value parameter for a test.
2669fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughesstatic const char kTypeParamLabel[] = "TypeParam";
2670fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughesstatic const char kValueParamLabel[] = "GetParam()";
2671fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes
267241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotvoid PrintFullTestCommentIfPresent(const TestInfo& test_info) {
267341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  const char* const type_param = test_info.type_param();
267441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  const char* const value_param = test_info.value_param();
26751be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
267641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  if (type_param != NULL || value_param != NULL) {
267741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot    printf(", where ");
267841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot    if (type_param != NULL) {
2679fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes      printf("%s = %s", kTypeParamLabel, type_param);
268041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot      if (value_param != NULL)
268141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot        printf(" and ");
268241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot    }
268341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot    if (value_param != NULL) {
2684fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes      printf("%s = %s", kValueParamLabel, value_param);
268541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot    }
268641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  }
268741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot}
26881be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
268941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// This class implements the TestEventListener interface.
26901be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//
26911be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Class PrettyUnitTestResultPrinter is copyable.
269241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotclass PrettyUnitTestResultPrinter : public TestEventListener {
26931be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania public:
26941be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  PrettyUnitTestResultPrinter() {}
26951be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  static void PrintTestName(const char * test_case, const char * test) {
26961be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    printf("%s.%s", test_case, test);
26971be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  }
26981be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
269941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  // The following methods override what's in the TestEventListener class.
270041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  virtual void OnTestProgramStart(const UnitTest& /*unit_test*/) {}
270141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  virtual void OnTestIterationStart(const UnitTest& unit_test, int iteration);
270241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  virtual void OnEnvironmentsSetUpStart(const UnitTest& unit_test);
270341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  virtual void OnEnvironmentsSetUpEnd(const UnitTest& /*unit_test*/) {}
270441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  virtual void OnTestCaseStart(const TestCase& test_case);
270541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  virtual void OnTestStart(const TestInfo& test_info);
270641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  virtual void OnTestPartResult(const TestPartResult& result);
270741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  virtual void OnTestEnd(const TestInfo& test_info);
270841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  virtual void OnTestCaseEnd(const TestCase& test_case);
270941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  virtual void OnEnvironmentsTearDownStart(const UnitTest& unit_test);
271041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  virtual void OnEnvironmentsTearDownEnd(const UnitTest& /*unit_test*/) {}
271141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  virtual void OnTestIterationEnd(const UnitTest& unit_test, int iteration);
271241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  virtual void OnTestProgramEnd(const UnitTest& /*unit_test*/) {}
27131be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
27141be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania private:
271541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  static void PrintFailedTests(const UnitTest& unit_test);
27161be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania};
27171be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
271841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  // Fired before each iteration of tests starts.
271941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotvoid PrettyUnitTestResultPrinter::OnTestIterationStart(
272041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot    const UnitTest& unit_test, int iteration) {
272141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  if (GTEST_FLAG(repeat) != 1)
272241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot    printf("\nRepeating all tests (iteration %d) . . .\n\n", iteration + 1);
272341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot
272441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  const char* const filter = GTEST_FLAG(filter).c_str();
27251be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
27261be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // Prints the filter if it's not *.  This reminds the user that some
27271be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // tests may be skipped.
2728fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes  if (!String::CStringEquals(filter, kUniversalFilter)) {
27291be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    ColoredPrintf(COLOR_YELLOW,
27301be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania                  "Note: %s filter = %s\n", GTEST_NAME_, filter);
27311be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  }
27321be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
27331be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  if (internal::ShouldShard(kTestTotalShards, kTestShardIndex, false)) {
273441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot    const Int32 shard_index = Int32FromEnvOrDie(kTestShardIndex, -1);
273541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot    ColoredPrintf(COLOR_YELLOW,
273641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot                  "Note: This is test shard %d of %s.\n",
273741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot                  static_cast<int>(shard_index) + 1,
273841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot                  internal::posix::GetEnv(kTestTotalShards));
273941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  }
274041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot
274141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  if (GTEST_FLAG(shuffle)) {
27421be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    ColoredPrintf(COLOR_YELLOW,
274341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot                  "Note: Randomizing tests' orders with a seed of %d .\n",
274441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot                  unit_test.random_seed());
27451be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  }
27461be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
27471be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  ColoredPrintf(COLOR_GREEN,  "[==========] ");
27481be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  printf("Running %s from %s.\n",
274941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot         FormatTestCount(unit_test.test_to_run_count()).c_str(),
275041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot         FormatTestCaseCount(unit_test.test_case_to_run_count()).c_str());
27511be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  fflush(stdout);
27521be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}
27531be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
275441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotvoid PrettyUnitTestResultPrinter::OnEnvironmentsSetUpStart(
275541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot    const UnitTest& /*unit_test*/) {
27561be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  ColoredPrintf(COLOR_GREEN,  "[----------] ");
27571be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  printf("Global test environment set-up.\n");
27581be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  fflush(stdout);
27591be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}
27601be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
276141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotvoid PrettyUnitTestResultPrinter::OnTestCaseStart(const TestCase& test_case) {
2762fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes  const std::string counts =
276341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot      FormatCountableNoun(test_case.test_to_run_count(), "test", "tests");
27641be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  ColoredPrintf(COLOR_GREEN, "[----------] ");
2765fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes  printf("%s from %s", counts.c_str(), test_case.name());
276641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  if (test_case.type_param() == NULL) {
27671be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    printf("\n");
27681be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  } else {
2769fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes    printf(", where %s = %s\n", kTypeParamLabel, test_case.type_param());
27701be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  }
27711be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  fflush(stdout);
27721be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}
27731be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
277441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotvoid PrettyUnitTestResultPrinter::OnTestStart(const TestInfo& test_info) {
277541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  ColoredPrintf(COLOR_GREEN,  "[ RUN      ] ");
2776fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes  PrintTestName(test_info.test_case_name(), test_info.name());
277741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  printf("\n");
27781be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  fflush(stdout);
27791be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}
27801be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
278141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// Called after an assertion failure.
278241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotvoid PrettyUnitTestResultPrinter::OnTestPartResult(
278341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot    const TestPartResult& result) {
278441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  // If the test part succeeded, we don't need to do anything.
278541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  if (result.type() == TestPartResult::kSuccess)
278641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot    return;
278741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot
278841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  // Print failure message from the assertion (e.g. expected this and got that).
278941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  PrintTestPartResult(result);
27901be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  fflush(stdout);
27911be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}
27921be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
279341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotvoid PrettyUnitTestResultPrinter::OnTestEnd(const TestInfo& test_info) {
279441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  if (test_info.result()->Passed()) {
27951be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    ColoredPrintf(COLOR_GREEN, "[       OK ] ");
27961be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  } else {
27971be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    ColoredPrintf(COLOR_RED, "[  FAILED  ] ");
27981be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  }
2799fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes  PrintTestName(test_info.test_case_name(), test_info.name());
280041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  if (test_info.result()->Failed())
280141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot    PrintFullTestCommentIfPresent(test_info);
280241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot
28031be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  if (GTEST_FLAG(print_time)) {
28041be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    printf(" (%s ms)\n", internal::StreamableToString(
280541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot           test_info.result()->elapsed_time()).c_str());
28061be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  } else {
28071be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    printf("\n");
28081be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  }
28091be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  fflush(stdout);
28101be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}
28111be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
281241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotvoid PrettyUnitTestResultPrinter::OnTestCaseEnd(const TestCase& test_case) {
281341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  if (!GTEST_FLAG(print_time)) return;
28141be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
2815fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes  const std::string counts =
281641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot      FormatCountableNoun(test_case.test_to_run_count(), "test", "tests");
281741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  ColoredPrintf(COLOR_GREEN, "[----------] ");
281841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  printf("%s from %s (%s ms total)\n\n",
2819fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes         counts.c_str(), test_case.name(),
282041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot         internal::StreamableToString(test_case.elapsed_time()).c_str());
28211be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  fflush(stdout);
28221be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}
28231be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
282441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotvoid PrettyUnitTestResultPrinter::OnEnvironmentsTearDownStart(
282541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot    const UnitTest& /*unit_test*/) {
28261be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  ColoredPrintf(COLOR_GREEN,  "[----------] ");
28271be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  printf("Global test environment tear-down\n");
28281be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  fflush(stdout);
28291be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}
28301be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
28311be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Internal helper for printing the list of failed tests.
283241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotvoid PrettyUnitTestResultPrinter::PrintFailedTests(const UnitTest& unit_test) {
283341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  const int failed_test_count = unit_test.failed_test_count();
28341be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  if (failed_test_count == 0) {
28351be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    return;
28361be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  }
28371be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
283841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  for (int i = 0; i < unit_test.total_test_case_count(); ++i) {
283941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot    const TestCase& test_case = *unit_test.GetTestCase(i);
284041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot    if (!test_case.should_run() || (test_case.failed_test_count() == 0)) {
28411be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania      continue;
28421be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    }
284341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot    for (int j = 0; j < test_case.total_test_count(); ++j) {
284441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot      const TestInfo& test_info = *test_case.GetTestInfo(j);
284541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot      if (!test_info.should_run() || test_info.result()->Passed()) {
28461be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania        continue;
28471be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania      }
28481be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania      ColoredPrintf(COLOR_RED, "[  FAILED  ] ");
284941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot      printf("%s.%s", test_case.name(), test_info.name());
285041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot      PrintFullTestCommentIfPresent(test_info);
285141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot      printf("\n");
28521be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    }
28531be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  }
28541be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}
28551be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
285641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotvoid PrettyUnitTestResultPrinter::OnTestIterationEnd(const UnitTest& unit_test,
285741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot                                                     int /*iteration*/) {
28581be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  ColoredPrintf(COLOR_GREEN,  "[==========] ");
28591be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  printf("%s from %s ran.",
286041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot         FormatTestCount(unit_test.test_to_run_count()).c_str(),
286141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot         FormatTestCaseCount(unit_test.test_case_to_run_count()).c_str());
28621be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  if (GTEST_FLAG(print_time)) {
28631be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    printf(" (%s ms total)",
286441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot           internal::StreamableToString(unit_test.elapsed_time()).c_str());
28651be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  }
28661be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  printf("\n");
28671be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  ColoredPrintf(COLOR_GREEN,  "[  PASSED  ] ");
286841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  printf("%s.\n", FormatTestCount(unit_test.successful_test_count()).c_str());
28691be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
287041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  int num_failures = unit_test.failed_test_count();
287141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  if (!unit_test.Passed()) {
287241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot    const int failed_test_count = unit_test.failed_test_count();
28731be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    ColoredPrintf(COLOR_RED,  "[  FAILED  ] ");
28741be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    printf("%s, listed below:\n", FormatTestCount(failed_test_count).c_str());
287541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot    PrintFailedTests(unit_test);
28761be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    printf("\n%2d FAILED %s\n", num_failures,
28771be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania                        num_failures == 1 ? "TEST" : "TESTS");
28781be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  }
28791be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
2880fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes  int num_disabled = unit_test.reportable_disabled_test_count();
28811be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  if (num_disabled && !GTEST_FLAG(also_run_disabled_tests)) {
28821be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    if (!num_failures) {
28831be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania      printf("\n");  // Add a spacer if no FAILURE banner is displayed.
28841be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    }
28851be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    ColoredPrintf(COLOR_YELLOW,
28861be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania                  "  YOU HAVE %d DISABLED %s\n\n",
28871be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania                  num_disabled,
28881be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania                  num_disabled == 1 ? "TEST" : "TESTS");
28891be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  }
28901be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // Ensure that Google Test output is printed before, e.g., heapchecker output.
28911be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  fflush(stdout);
28921be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}
28931be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
28941be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// End PrettyUnitTestResultPrinter
28951be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
289641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// class TestEventRepeater
28971be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//
28981be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// This class forwards events to other event listeners.
289941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotclass TestEventRepeater : public TestEventListener {
29001be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania public:
290141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  TestEventRepeater() : forwarding_enabled_(true) {}
290241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  virtual ~TestEventRepeater();
290341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  void Append(TestEventListener *listener);
290441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  TestEventListener* Release(TestEventListener* listener);
290541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot
290641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  // Controls whether events will be forwarded to listeners_. Set to false
290741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  // in death test child processes.
290841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  bool forwarding_enabled() const { return forwarding_enabled_; }
290941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  void set_forwarding_enabled(bool enable) { forwarding_enabled_ = enable; }
291041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot
291141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  virtual void OnTestProgramStart(const UnitTest& unit_test);
291241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  virtual void OnTestIterationStart(const UnitTest& unit_test, int iteration);
291341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  virtual void OnEnvironmentsSetUpStart(const UnitTest& unit_test);
291441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  virtual void OnEnvironmentsSetUpEnd(const UnitTest& unit_test);
291541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  virtual void OnTestCaseStart(const TestCase& test_case);
291641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  virtual void OnTestStart(const TestInfo& test_info);
291741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  virtual void OnTestPartResult(const TestPartResult& result);
291841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  virtual void OnTestEnd(const TestInfo& test_info);
291941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  virtual void OnTestCaseEnd(const TestCase& test_case);
292041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  virtual void OnEnvironmentsTearDownStart(const UnitTest& unit_test);
292141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  virtual void OnEnvironmentsTearDownEnd(const UnitTest& unit_test);
292241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  virtual void OnTestIterationEnd(const UnitTest& unit_test, int iteration);
292341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  virtual void OnTestProgramEnd(const UnitTest& unit_test);
29241be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
29251be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania private:
292641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  // Controls whether events will be forwarded to listeners_. Set to false
292741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  // in death test child processes.
292841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  bool forwarding_enabled_;
292941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  // The list of listeners that receive events.
293041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  std::vector<TestEventListener*> listeners_;
29311be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
293241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  GTEST_DISALLOW_COPY_AND_ASSIGN_(TestEventRepeater);
29331be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania};
29341be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
293541d0579e8de9ef4ff178fc4991043c61a19943f7Brett ChabotTestEventRepeater::~TestEventRepeater() {
293641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  ForEach(listeners_, Delete<TestEventListener>);
29371be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}
29381be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
293941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotvoid TestEventRepeater::Append(TestEventListener *listener) {
294041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  listeners_.push_back(listener);
294141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot}
294241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot
294341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// TODO(vladl@google.com): Factor the search functionality into Vector::Find.
294441d0579e8de9ef4ff178fc4991043c61a19943f7Brett ChabotTestEventListener* TestEventRepeater::Release(TestEventListener *listener) {
294541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  for (size_t i = 0; i < listeners_.size(); ++i) {
294641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot    if (listeners_[i] == listener) {
294741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot      listeners_.erase(listeners_.begin() + i);
294841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot      return listener;
294941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot    }
295041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  }
295141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot
295241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  return NULL;
29531be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}
29541be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
295541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// Since most methods are very similar, use macros to reduce boilerplate.
295641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// This defines a member that forwards the call to all listeners.
29571be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#define GTEST_REPEATER_METHOD_(Name, Type) \
295841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotvoid TestEventRepeater::Name(const Type& parameter) { \
295941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  if (forwarding_enabled_) { \
296041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot    for (size_t i = 0; i < listeners_.size(); i++) { \
296141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot      listeners_[i]->Name(parameter); \
296241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot    } \
296341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  } \
296441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot}
296541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// This defines a member that forwards the call to all listeners in reverse
296641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// order.
296741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot#define GTEST_REVERSE_REPEATER_METHOD_(Name, Type) \
296841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotvoid TestEventRepeater::Name(const Type& parameter) { \
296941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  if (forwarding_enabled_) { \
297041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot    for (int i = static_cast<int>(listeners_.size()) - 1; i >= 0; i--) { \
297141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot      listeners_[i]->Name(parameter); \
297241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot    } \
29731be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  } \
29741be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}
29751be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
297641d0579e8de9ef4ff178fc4991043c61a19943f7Brett ChabotGTEST_REPEATER_METHOD_(OnTestProgramStart, UnitTest)
297741d0579e8de9ef4ff178fc4991043c61a19943f7Brett ChabotGTEST_REPEATER_METHOD_(OnEnvironmentsSetUpStart, UnitTest)
29781be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaGTEST_REPEATER_METHOD_(OnTestCaseStart, TestCase)
29791be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaGTEST_REPEATER_METHOD_(OnTestStart, TestInfo)
298041d0579e8de9ef4ff178fc4991043c61a19943f7Brett ChabotGTEST_REPEATER_METHOD_(OnTestPartResult, TestPartResult)
298141d0579e8de9ef4ff178fc4991043c61a19943f7Brett ChabotGTEST_REPEATER_METHOD_(OnEnvironmentsTearDownStart, UnitTest)
298241d0579e8de9ef4ff178fc4991043c61a19943f7Brett ChabotGTEST_REVERSE_REPEATER_METHOD_(OnEnvironmentsSetUpEnd, UnitTest)
298341d0579e8de9ef4ff178fc4991043c61a19943f7Brett ChabotGTEST_REVERSE_REPEATER_METHOD_(OnEnvironmentsTearDownEnd, UnitTest)
298441d0579e8de9ef4ff178fc4991043c61a19943f7Brett ChabotGTEST_REVERSE_REPEATER_METHOD_(OnTestEnd, TestInfo)
298541d0579e8de9ef4ff178fc4991043c61a19943f7Brett ChabotGTEST_REVERSE_REPEATER_METHOD_(OnTestCaseEnd, TestCase)
298641d0579e8de9ef4ff178fc4991043c61a19943f7Brett ChabotGTEST_REVERSE_REPEATER_METHOD_(OnTestProgramEnd, UnitTest)
29871be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
29881be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#undef GTEST_REPEATER_METHOD_
298941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot#undef GTEST_REVERSE_REPEATER_METHOD_
29901be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
299141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotvoid TestEventRepeater::OnTestIterationStart(const UnitTest& unit_test,
299241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot                                             int iteration) {
299341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  if (forwarding_enabled_) {
299441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot    for (size_t i = 0; i < listeners_.size(); i++) {
299541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot      listeners_[i]->OnTestIterationStart(unit_test, iteration);
299641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot    }
299741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  }
299841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot}
299941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot
300041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotvoid TestEventRepeater::OnTestIterationEnd(const UnitTest& unit_test,
300141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot                                           int iteration) {
300241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  if (forwarding_enabled_) {
300341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot    for (int i = static_cast<int>(listeners_.size()) - 1; i >= 0; i--) {
300441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot      listeners_[i]->OnTestIterationEnd(unit_test, iteration);
300541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot    }
300641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  }
300741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot}
300841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot
300941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// End TestEventRepeater
30101be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
30111be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// This class generates an XML output file.
301241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotclass XmlUnitTestResultPrinter : public EmptyTestEventListener {
30131be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania public:
30141be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  explicit XmlUnitTestResultPrinter(const char* output_file);
30151be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
301641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  virtual void OnTestIterationEnd(const UnitTest& unit_test, int iteration);
30171be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
30181be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania private:
30191be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // Is c a whitespace character that is normalized to a space character
30201be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // when it appears in an XML attribute value?
30211be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  static bool IsNormalizableWhitespace(char c) {
30221be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    return c == 0x9 || c == 0xA || c == 0xD;
30231be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  }
30241be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
30251be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // May c appear in a well-formed XML document?
30261be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  static bool IsValidXmlCharacter(char c) {
30271be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    return IsNormalizableWhitespace(c) || c >= 0x20;
30281be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  }
30291be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
30301be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // Returns an XML-escaped copy of the input string str.  If
30311be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // is_attribute is true, the text is meant to appear as an attribute
30321be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // value, and normalizable whitespace is preserved by replacing it
30331be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // with character references.
3034fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes  static std::string EscapeXml(const std::string& str, bool is_attribute);
303541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot
303641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  // Returns the given string with all characters invalid in XML removed.
3037fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes  static std::string RemoveInvalidXmlCharacters(const std::string& str);
30381be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
30391be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // Convenience wrapper around EscapeXml when str is an attribute value.
3040fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes  static std::string EscapeXmlAttribute(const std::string& str) {
30411be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    return EscapeXml(str, true);
30421be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  }
30431be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
30441be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // Convenience wrapper around EscapeXml when str is not an attribute value.
3045fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes  static std::string EscapeXmlText(const char* str) {
3046fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes    return EscapeXml(str, false);
3047fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes  }
3048fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes
3049fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes  // Verifies that the given attribute belongs to the given element and
3050fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes  // streams the attribute as XML.
3051fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes  static void OutputXmlAttribute(std::ostream* stream,
3052fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes                                 const std::string& element_name,
3053fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes                                 const std::string& name,
3054fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes                                 const std::string& value);
30551be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
305641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  // Streams an XML CDATA section, escaping invalid CDATA sequences as needed.
305741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  static void OutputXmlCDataSection(::std::ostream* stream, const char* data);
305841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot
305941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  // Streams an XML representation of a TestInfo object.
306041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  static void OutputXmlTestInfo(::std::ostream* stream,
306141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot                                const char* test_case_name,
306241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot                                const TestInfo& test_info);
30631be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
30641be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // Prints an XML representation of a TestCase object
3065fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes  static void PrintXmlTestCase(::std::ostream* stream,
3066fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes                               const TestCase& test_case);
30671be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
30681be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // Prints an XML summary of unit_test to output stream out.
3069fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes  static void PrintXmlUnitTest(::std::ostream* stream,
3070fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes                               const UnitTest& unit_test);
30711be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
30721be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // Produces a string representing the test properties in a result as space
30731be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // delimited XML attributes based on the property key="value" pairs.
3074fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes  // When the std::string is not empty, it includes a space at the beginning,
30751be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // to delimit this attribute from prior attributes.
3076fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes  static std::string TestPropertiesAsXmlAttributes(const TestResult& result);
30771be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
30781be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // The output file.
3079fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes  const std::string output_file_;
30801be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
30811be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  GTEST_DISALLOW_COPY_AND_ASSIGN_(XmlUnitTestResultPrinter);
30821be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania};
30831be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
30841be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Creates a new XmlUnitTestResultPrinter.
30851be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaXmlUnitTestResultPrinter::XmlUnitTestResultPrinter(const char* output_file)
30861be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    : output_file_(output_file) {
30871be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  if (output_file_.c_str() == NULL || output_file_.empty()) {
30881be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    fprintf(stderr, "XML output file may not be null\n");
30891be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    fflush(stderr);
30901be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    exit(EXIT_FAILURE);
30911be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  }
30921be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}
30931be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
30941be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Called after the unit test ends.
309541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotvoid XmlUnitTestResultPrinter::OnTestIterationEnd(const UnitTest& unit_test,
309641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot                                                  int /*iteration*/) {
30971be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  FILE* xmlout = NULL;
309841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  FilePath output_file(output_file_);
309941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  FilePath output_dir(output_file.RemoveFileName());
31001be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
31011be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  if (output_dir.CreateDirectoriesRecursively()) {
310241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot    xmlout = posix::FOpen(output_file_.c_str(), "w");
31031be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  }
31041be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  if (xmlout == NULL) {
31051be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    // TODO(wan): report the reason of the failure.
31061be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    //
31071be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    // We don't do it for now as:
31081be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    //
31091be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    //   1. There is no urgent need for it.
31101be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    //   2. It's a bit involved to make the errno variable thread-safe on
31111be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    //      all three operating systems (Linux, Windows, and Mac OS).
31121be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    //   3. To interpret the meaning of errno in a thread-safe way,
31131be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    //      we need the strerror_r() function, which is not available on
31141be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    //      Windows.
31151be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    fprintf(stderr,
31161be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania            "Unable to open file \"%s\"\n",
31171be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania            output_file_.c_str());
31181be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    fflush(stderr);
31191be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    exit(EXIT_FAILURE);
31201be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  }
3121fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes  std::stringstream stream;
3122fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes  PrintXmlUnitTest(&stream, unit_test);
3123fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes  fprintf(xmlout, "%s", StringStreamToString(&stream).c_str());
31241be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  fclose(xmlout);
31251be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}
31261be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
31271be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Returns an XML-escaped copy of the input string str.  If is_attribute
31281be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// is true, the text is meant to appear as an attribute value, and
31291be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// normalizable whitespace is preserved by replacing it with character
31301be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// references.
31311be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//
31321be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Invalid XML characters in str, if any, are stripped from the output.
31331be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// It is expected that most, if not all, of the text processed by this
31341be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// module will consist of ordinary English text.
31351be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// If this module is ever modified to produce version 1.1 XML output,
31361be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// most invalid characters can be retained using character references.
31371be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// TODO(wan): It might be nice to have a minimally invasive, human-readable
31381be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// escaping scheme for invalid characters, rather than dropping them.
3139fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughesstd::string XmlUnitTestResultPrinter::EscapeXml(
3140fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes    const std::string& str, bool is_attribute) {
31411be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  Message m;
31421be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
3143fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes  for (size_t i = 0; i < str.size(); ++i) {
3144fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes    const char ch = str[i];
3145fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes    switch (ch) {
3146fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes      case '<':
3147fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes        m << "&lt;";
3148fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes        break;
3149fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes      case '>':
3150fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes        m << "&gt;";
3151fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes        break;
3152fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes      case '&':
3153fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes        m << "&amp;";
3154fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes        break;
3155fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes      case '\'':
3156fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes        if (is_attribute)
3157fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes          m << "&apos;";
3158fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes        else
3159fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes          m << '\'';
3160fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes        break;
3161fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes      case '"':
3162fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes        if (is_attribute)
3163fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes          m << "&quot;";
3164fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes        else
3165fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes          m << '"';
3166fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes        break;
3167fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes      default:
3168fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes        if (IsValidXmlCharacter(ch)) {
3169fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes          if (is_attribute && IsNormalizableWhitespace(ch))
3170fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes            m << "&#x" << String::FormatByte(static_cast<unsigned char>(ch))
3171fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes              << ";";
31721be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania          else
3173fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes            m << ch;
3174fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes        }
3175fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes        break;
31761be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    }
31771be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  }
31781be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
31791be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  return m.GetString();
31801be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}
31811be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
318241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// Returns the given string with all characters invalid in XML removed.
318341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// Currently invalid characters are dropped from the string. An
318441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// alternative is to replace them with certain characters such as . or ?.
3185fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughesstd::string XmlUnitTestResultPrinter::RemoveInvalidXmlCharacters(
3186fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes    const std::string& str) {
3187fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes  std::string output;
318841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  output.reserve(str.size());
3189fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes  for (std::string::const_iterator it = str.begin(); it != str.end(); ++it)
319041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot    if (IsValidXmlCharacter(*it))
319141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot      output.push_back(*it);
319241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot
319341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  return output;
319441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot}
31951be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
31961be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// The following routines generate an XML representation of a UnitTest
31971be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// object.
31981be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//
31991be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// This is how Google Test concepts map to the DTD:
32001be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//
320141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// <testsuites name="AllTests">        <-- corresponds to a UnitTest object
32021be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//   <testsuite name="testcase-name">  <-- corresponds to a TestCase object
32031be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//     <testcase name="test-name">     <-- corresponds to a TestInfo object
32041be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//       <failure message="...">...</failure>
32051be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//       <failure message="...">...</failure>
32061be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//       <failure message="...">...</failure>
32071be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//                                     <-- individual assertion failures
32081be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//     </testcase>
32091be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//   </testsuite>
321041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// </testsuites>
32111be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
321241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// Formats the given time in milliseconds as seconds.
321341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotstd::string FormatTimeInMillisAsSeconds(TimeInMillis ms) {
321441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  ::std::stringstream ss;
321541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  ss << ms/1000.0;
321641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  return ss.str();
32171be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}
32181be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
3219fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes// Converts the given epoch time in milliseconds to a date string in the ISO
3220fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes// 8601 format, without the timezone information.
3221fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughesstd::string FormatEpochTimeInMillisAsIso8601(TimeInMillis ms) {
3222fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes  // Using non-reentrant version as localtime_r is not portable.
3223fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes  time_t seconds = static_cast<time_t>(ms / 1000);
3224fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes#ifdef _MSC_VER
3225fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes# pragma warning(push)          // Saves the current warning state.
3226fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes# pragma warning(disable:4996)  // Temporarily disables warning 4996
3227fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes                                // (function or variable may be unsafe).
3228fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes  const struct tm* const time_struct = localtime(&seconds);  // NOLINT
3229fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes# pragma warning(pop)           // Restores the warning state again.
3230fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes#else
3231fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes  const struct tm* const time_struct = localtime(&seconds);  // NOLINT
3232fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes#endif
3233fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes  if (time_struct == NULL)
3234fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes    return "";  // Invalid ms value
3235fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes
3236fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes  // YYYY-MM-DDThh:mm:ss
3237fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes  return StreamableToString(time_struct->tm_year + 1900) + "-" +
3238fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes      String::FormatIntWidth2(time_struct->tm_mon + 1) + "-" +
3239fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes      String::FormatIntWidth2(time_struct->tm_mday) + "T" +
3240fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes      String::FormatIntWidth2(time_struct->tm_hour) + ":" +
3241fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes      String::FormatIntWidth2(time_struct->tm_min) + ":" +
3242fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes      String::FormatIntWidth2(time_struct->tm_sec);
3243fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes}
3244fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes
324541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// Streams an XML CDATA section, escaping invalid CDATA sequences as needed.
324641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotvoid XmlUnitTestResultPrinter::OutputXmlCDataSection(::std::ostream* stream,
324741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot                                                     const char* data) {
324841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  const char* segment = data;
324941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  *stream << "<![CDATA[";
325041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  for (;;) {
325141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot    const char* const next_segment = strstr(segment, "]]>");
325241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot    if (next_segment != NULL) {
325341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot      stream->write(
325441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot          segment, static_cast<std::streamsize>(next_segment - segment));
325541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot      *stream << "]]>]]&gt;<![CDATA[";
325641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot      segment = next_segment + strlen("]]>");
325741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot    } else {
325841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot      *stream << segment;
325941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot      break;
326041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot    }
326141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  }
326241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  *stream << "]]>";
326341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot}
32641be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
3265fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughesvoid XmlUnitTestResultPrinter::OutputXmlAttribute(
3266fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes    std::ostream* stream,
3267fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes    const std::string& element_name,
3268fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes    const std::string& name,
3269fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes    const std::string& value) {
3270fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes  const std::vector<std::string>& allowed_names =
3271fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes      GetReservedAttributesForElement(element_name);
3272fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes
3273fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes  GTEST_CHECK_(std::find(allowed_names.begin(), allowed_names.end(), name) !=
3274fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes                   allowed_names.end())
3275fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes      << "Attribute " << name << " is not allowed for element <" << element_name
3276fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes      << ">.";
3277fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes
3278fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes  *stream << " " << name << "=\"" << EscapeXmlAttribute(value) << "\"";
3279fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes}
3280fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes
32811be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Prints an XML representation of a TestInfo object.
32821be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// TODO(wan): There is also value in printing properties with the plain printer.
328341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotvoid XmlUnitTestResultPrinter::OutputXmlTestInfo(::std::ostream* stream,
328441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot                                                 const char* test_case_name,
328541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot                                                 const TestInfo& test_info) {
328641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  const TestResult& result = *test_info.result();
3287fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes  const std::string kTestcase = "testcase";
3288fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes
3289fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes  *stream << "    <testcase";
3290fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes  OutputXmlAttribute(stream, kTestcase, "name", test_info.name());
329141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot
329241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  if (test_info.value_param() != NULL) {
3293fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes    OutputXmlAttribute(stream, kTestcase, "value_param",
3294fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes                       test_info.value_param());
329541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  }
329641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  if (test_info.type_param() != NULL) {
3297fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes    OutputXmlAttribute(stream, kTestcase, "type_param", test_info.type_param());
329841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  }
329941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot
3300fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes  OutputXmlAttribute(stream, kTestcase, "status",
3301fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes                     test_info.should_run() ? "run" : "notrun");
3302fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes  OutputXmlAttribute(stream, kTestcase, "time",
3303fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes                     FormatTimeInMillisAsSeconds(result.elapsed_time()));
3304fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes  OutputXmlAttribute(stream, kTestcase, "classname", test_case_name);
3305fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes  *stream << TestPropertiesAsXmlAttributes(result);
33061be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
33071be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  int failures = 0;
330841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  for (int i = 0; i < result.total_part_count(); ++i) {
330941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot    const TestPartResult& part = result.GetTestPartResult(i);
33101be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    if (part.failed()) {
3311fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes      if (++failures == 1) {
331241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot        *stream << ">\n";
3313fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes      }
331441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot      const string location = internal::FormatCompilerIndependentFileLocation(
331541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot          part.file_name(), part.line_number());
3316fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes      const string summary = location + "\n" + part.summary();
3317fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes      *stream << "      <failure message=\""
3318fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes              << EscapeXmlAttribute(summary.c_str())
3319fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes              << "\" type=\"\">";
3320fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes      const string detail = location + "\n" + part.message();
3321fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes      OutputXmlCDataSection(stream, RemoveInvalidXmlCharacters(detail).c_str());
332241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot      *stream << "</failure>\n";
33231be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    }
33241be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  }
33251be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
33261be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  if (failures == 0)
332741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot    *stream << " />\n";
33281be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  else
332941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot    *stream << "    </testcase>\n";
33301be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}
33311be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
33321be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Prints an XML representation of a TestCase object
3333fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughesvoid XmlUnitTestResultPrinter::PrintXmlTestCase(std::ostream* stream,
333441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot                                                const TestCase& test_case) {
3335fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes  const std::string kTestsuite = "testsuite";
3336fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes  *stream << "  <" << kTestsuite;
3337fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes  OutputXmlAttribute(stream, kTestsuite, "name", test_case.name());
3338fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes  OutputXmlAttribute(stream, kTestsuite, "tests",
3339fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes                     StreamableToString(test_case.reportable_test_count()));
3340fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes  OutputXmlAttribute(stream, kTestsuite, "failures",
3341fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes                     StreamableToString(test_case.failed_test_count()));
3342fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes  OutputXmlAttribute(
3343fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes      stream, kTestsuite, "disabled",
3344fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes      StreamableToString(test_case.reportable_disabled_test_count()));
3345fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes  OutputXmlAttribute(stream, kTestsuite, "errors", "0");
3346fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes  OutputXmlAttribute(stream, kTestsuite, "time",
3347fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes                     FormatTimeInMillisAsSeconds(test_case.elapsed_time()));
3348fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes  *stream << TestPropertiesAsXmlAttributes(test_case.ad_hoc_test_result())
3349fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes          << ">\n";
3350fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes
335141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  for (int i = 0; i < test_case.total_test_count(); ++i) {
3352fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes    if (test_case.GetTestInfo(i)->is_reportable())
3353fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes      OutputXmlTestInfo(stream, test_case.name(), *test_case.GetTestInfo(i));
33541be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  }
3355fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes  *stream << "  </" << kTestsuite << ">\n";
33561be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}
33571be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
33581be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Prints an XML summary of unit_test to output stream out.
3359fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughesvoid XmlUnitTestResultPrinter::PrintXmlUnitTest(std::ostream* stream,
336041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot                                                const UnitTest& unit_test) {
3361fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes  const std::string kTestsuites = "testsuites";
3362fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes
3363fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes  *stream << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
3364fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes  *stream << "<" << kTestsuites;
3365fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes
3366fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes  OutputXmlAttribute(stream, kTestsuites, "tests",
3367fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes                     StreamableToString(unit_test.reportable_test_count()));
3368fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes  OutputXmlAttribute(stream, kTestsuites, "failures",
3369fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes                     StreamableToString(unit_test.failed_test_count()));
3370fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes  OutputXmlAttribute(
3371fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes      stream, kTestsuites, "disabled",
3372fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes      StreamableToString(unit_test.reportable_disabled_test_count()));
3373fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes  OutputXmlAttribute(stream, kTestsuites, "errors", "0");
3374fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes  OutputXmlAttribute(
3375fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes      stream, kTestsuites, "timestamp",
3376fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes      FormatEpochTimeInMillisAsIso8601(unit_test.start_timestamp()));
3377fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes  OutputXmlAttribute(stream, kTestsuites, "time",
3378fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes                     FormatTimeInMillisAsSeconds(unit_test.elapsed_time()));
3379fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes
338041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  if (GTEST_FLAG(shuffle)) {
3381fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes    OutputXmlAttribute(stream, kTestsuites, "random_seed",
3382fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes                       StreamableToString(unit_test.random_seed()));
3383fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes  }
3384fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes
3385fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes  *stream << TestPropertiesAsXmlAttributes(unit_test.ad_hoc_test_result());
3386fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes
3387fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes  OutputXmlAttribute(stream, kTestsuites, "name", "AllTests");
3388fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes  *stream << ">\n";
3389fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes
3390fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes  for (int i = 0; i < unit_test.total_test_case_count(); ++i) {
3391fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes    if (unit_test.GetTestCase(i)->reportable_test_count() > 0)
3392fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes      PrintXmlTestCase(stream, *unit_test.GetTestCase(i));
33931be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  }
3394fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes  *stream << "</" << kTestsuites << ">\n";
33951be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}
33961be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
33971be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Produces a string representing the test properties in a result as space
33981be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// delimited XML attributes based on the property key="value" pairs.
3399fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughesstd::string XmlUnitTestResultPrinter::TestPropertiesAsXmlAttributes(
340041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot    const TestResult& result) {
34011be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  Message attributes;
340241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  for (int i = 0; i < result.test_property_count(); ++i) {
340341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot    const TestProperty& property = result.GetTestProperty(i);
34041be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    attributes << " " << property.key() << "="
34051be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania        << "\"" << EscapeXmlAttribute(property.value()) << "\"";
34061be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  }
34071be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  return attributes.GetString();
34081be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}
34091be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
34101be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// End XmlUnitTestResultPrinter
34111be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
341241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot#if GTEST_CAN_STREAM_RESULTS_
341341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot
341441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// Checks if str contains '=', '&', '%' or '\n' characters. If yes,
341541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// replaces them by "%xx" where xx is their hexadecimal value. For
341641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// example, replaces "=" with "%3D".  This algorithm is O(strlen(str))
341741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// in both time and space -- important as the input str may contain an
341841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// arbitrarily long test failure message and stack trace.
341941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotstring StreamingListener::UrlEncode(const char* str) {
342041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  string result;
342141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  result.reserve(strlen(str) + 1);
342241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  for (char ch = *str; ch != '\0'; ch = *++str) {
342341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot    switch (ch) {
342441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot      case '%':
342541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot      case '=':
342641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot      case '&':
342741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot      case '\n':
3428fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes        result.append("%" + String::FormatByte(static_cast<unsigned char>(ch)));
342941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot        break;
343041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot      default:
343141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot        result.push_back(ch);
343241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot        break;
343341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot    }
343441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  }
343541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  return result;
343641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot}
343741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot
3438fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughesvoid StreamingListener::SocketWriter::MakeConnection() {
343941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  GTEST_CHECK_(sockfd_ == -1)
344041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot      << "MakeConnection() can't be called when there is already a connection.";
344141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot
344241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  addrinfo hints;
344341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  memset(&hints, 0, sizeof(hints));
344441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  hints.ai_family = AF_UNSPEC;    // To allow both IPv4 and IPv6 addresses.
344541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  hints.ai_socktype = SOCK_STREAM;
344641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  addrinfo* servinfo = NULL;
344741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot
344841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  // Use the getaddrinfo() to get a linked list of IP addresses for
344941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  // the given host name.
345041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  const int error_num = getaddrinfo(
345141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot      host_name_.c_str(), port_num_.c_str(), &hints, &servinfo);
345241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  if (error_num != 0) {
345341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot    GTEST_LOG_(WARNING) << "stream_result_to: getaddrinfo() failed: "
345441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot                        << gai_strerror(error_num);
345541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  }
345641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot
345741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  // Loop through all the results and connect to the first we can.
345841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  for (addrinfo* cur_addr = servinfo; sockfd_ == -1 && cur_addr != NULL;
345941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot       cur_addr = cur_addr->ai_next) {
346041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot    sockfd_ = socket(
346141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot        cur_addr->ai_family, cur_addr->ai_socktype, cur_addr->ai_protocol);
346241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot    if (sockfd_ != -1) {
346341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot      // Connect the client socket to the server socket.
346441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot      if (connect(sockfd_, cur_addr->ai_addr, cur_addr->ai_addrlen) == -1) {
346541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot        close(sockfd_);
346641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot        sockfd_ = -1;
346741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot      }
346841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot    }
346941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  }
347041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot
347141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  freeaddrinfo(servinfo);  // all done with this structure
347241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot
347341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  if (sockfd_ == -1) {
347441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot    GTEST_LOG_(WARNING) << "stream_result_to: failed to connect to "
347541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot                        << host_name_ << ":" << port_num_;
347641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  }
347741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot}
347841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot
347941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// End of class Streaming Listener
348041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot#endif  // GTEST_CAN_STREAM_RESULTS__
34811be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
34821be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Class ScopedTrace
34831be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
34841be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Pushes the given source file location and message onto a per-thread
34851be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// trace stack maintained by Google Test.
3486fc2de66453b0669c09eaca643b07d34443858b6fElliott HughesScopedTrace::ScopedTrace(const char* file, int line, const Message& message)
3487fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes    GTEST_LOCK_EXCLUDED_(&UnitTest::mutex_) {
34881be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  TraceInfo trace;
34891be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  trace.file = file;
34901be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  trace.line = line;
34911be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  trace.message = message.GetString();
34921be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
34931be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  UnitTest::GetInstance()->PushGTestTrace(trace);
34941be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}
34951be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
34961be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Pops the info pushed by the c'tor.
3497fc2de66453b0669c09eaca643b07d34443858b6fElliott HughesScopedTrace::~ScopedTrace()
3498fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes    GTEST_LOCK_EXCLUDED_(&UnitTest::mutex_) {
34991be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  UnitTest::GetInstance()->PopGTestTrace();
35001be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}
35011be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
35021be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
35031be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// class OsStackTraceGetter
35041be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
3505fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes// Returns the current OS stack trace as an std::string.  Parameters:
35061be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//
35071be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//   max_depth  - the maximum number of stack frames to be included
35081be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//                in the trace.
35091be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//   skip_count - the number of top frames to be skipped; doesn't count
35101be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//                against max_depth.
35111be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//
3512fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughesstring OsStackTraceGetter::CurrentStackTrace(int /* max_depth */,
3513fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes                                             int /* skip_count */)
3514fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes    GTEST_LOCK_EXCLUDED_(mutex_) {
3515fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes  return "";
35161be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}
35171be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
3518fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughesvoid OsStackTraceGetter::UponLeavingGTest()
3519fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes    GTEST_LOCK_EXCLUDED_(mutex_) {
35201be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}
35211be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
35221be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniaconst char* const
35231be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaOsStackTraceGetter::kElidedFramesMarker =
35241be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    "... " GTEST_NAME_ " internal frames ...";
35251be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
3526fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes// A helper class that creates the premature-exit file in its
3527fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes// constructor and deletes the file in its destructor.
3528fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughesclass ScopedPrematureExitFile {
3529fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes public:
3530fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes  explicit ScopedPrematureExitFile(const char* premature_exit_filepath)
3531fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes      : premature_exit_filepath_(premature_exit_filepath) {
3532fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes    // If a path to the premature-exit file is specified...
3533fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes    if (premature_exit_filepath != NULL && *premature_exit_filepath != '\0') {
3534fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes      // create the file with a single "0" character in it.  I/O
3535fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes      // errors are ignored as there's nothing better we can do and we
3536fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes      // don't want to fail the test because of this.
3537fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes      FILE* pfile = posix::FOpen(premature_exit_filepath, "w");
3538fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes      fwrite("0", 1, 1, pfile);
3539fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes      fclose(pfile);
3540fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes    }
3541fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes  }
3542fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes
3543fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes  ~ScopedPrematureExitFile() {
3544fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes    if (premature_exit_filepath_ != NULL && *premature_exit_filepath_ != '\0') {
3545fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes      remove(premature_exit_filepath_);
3546fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes    }
3547fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes  }
3548fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes
3549fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes private:
3550fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes  const char* const premature_exit_filepath_;
3551fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes
3552fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes  GTEST_DISALLOW_COPY_AND_ASSIGN_(ScopedPrematureExitFile);
3553fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes};
3554fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes
35551be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}  // namespace internal
35561be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
355741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// class TestEventListeners
355841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot
355941d0579e8de9ef4ff178fc4991043c61a19943f7Brett ChabotTestEventListeners::TestEventListeners()
356041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot    : repeater_(new internal::TestEventRepeater()),
356141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot      default_result_printer_(NULL),
356241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot      default_xml_generator_(NULL) {
356341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot}
356441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot
356541d0579e8de9ef4ff178fc4991043c61a19943f7Brett ChabotTestEventListeners::~TestEventListeners() { delete repeater_; }
356641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot
356741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// Returns the standard listener responsible for the default console
356841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// output.  Can be removed from the listeners list to shut down default
356941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// console output.  Note that removing this object from the listener list
357041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// with Release transfers its ownership to the user.
357141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotvoid TestEventListeners::Append(TestEventListener* listener) {
357241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  repeater_->Append(listener);
357341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot}
357441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot
357541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// Removes the given event listener from the list and returns it.  It then
357641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// becomes the caller's responsibility to delete the listener. Returns
357741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// NULL if the listener is not found in the list.
357841d0579e8de9ef4ff178fc4991043c61a19943f7Brett ChabotTestEventListener* TestEventListeners::Release(TestEventListener* listener) {
357941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  if (listener == default_result_printer_)
358041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot    default_result_printer_ = NULL;
358141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  else if (listener == default_xml_generator_)
358241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot    default_xml_generator_ = NULL;
358341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  return repeater_->Release(listener);
358441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot}
358541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot
358641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// Returns repeater that broadcasts the TestEventListener events to all
358741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// subscribers.
358841d0579e8de9ef4ff178fc4991043c61a19943f7Brett ChabotTestEventListener* TestEventListeners::repeater() { return repeater_; }
358941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot
359041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// Sets the default_result_printer attribute to the provided listener.
359141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// The listener is also added to the listener list and previous
359241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// default_result_printer is removed from it and deleted. The listener can
359341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// also be NULL in which case it will not be added to the list. Does
359441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// nothing if the previous and the current listener objects are the same.
359541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotvoid TestEventListeners::SetDefaultResultPrinter(TestEventListener* listener) {
359641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  if (default_result_printer_ != listener) {
359741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot    // It is an error to pass this method a listener that is already in the
359841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot    // list.
359941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot    delete Release(default_result_printer_);
360041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot    default_result_printer_ = listener;
360141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot    if (listener != NULL)
360241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot      Append(listener);
360341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  }
360441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot}
360541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot
360641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// Sets the default_xml_generator attribute to the provided listener.  The
360741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// listener is also added to the listener list and previous
360841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// default_xml_generator is removed from it and deleted. The listener can
360941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// also be NULL in which case it will not be added to the list. Does
361041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// nothing if the previous and the current listener objects are the same.
361141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotvoid TestEventListeners::SetDefaultXmlGenerator(TestEventListener* listener) {
361241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  if (default_xml_generator_ != listener) {
361341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot    // It is an error to pass this method a listener that is already in the
361441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot    // list.
361541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot    delete Release(default_xml_generator_);
361641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot    default_xml_generator_ = listener;
361741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot    if (listener != NULL)
361841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot      Append(listener);
361941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  }
362041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot}
362141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot
362241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// Controls whether events will be forwarded by the repeater to the
362341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// listeners in the list.
362441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotbool TestEventListeners::EventForwardingEnabled() const {
362541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  return repeater_->forwarding_enabled();
362641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot}
362741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot
362841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotvoid TestEventListeners::SuppressEventForwarding() {
362941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  repeater_->set_forwarding_enabled(false);
363041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot}
363141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot
36321be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// class UnitTest
36331be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
36341be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Gets the singleton UnitTest object.  The first time this method is
36351be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// called, a UnitTest object is constructed and returned.  Consecutive
36361be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// calls will return the same object.
36371be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//
36381be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// We don't protect this under mutex_ as a user is not supposed to
36391be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// call this before main() starts, from which point on the return
36401be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// value will never change.
3641fc2de66453b0669c09eaca643b07d34443858b6fElliott HughesUnitTest* UnitTest::GetInstance() {
36421be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // When compiled with MSVC 7.1 in optimized mode, destroying the
36431be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // UnitTest object upon exiting the program messes up the exit code,
36441be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // causing successful tests to appear failed.  We have to use a
36451be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // different implementation in this case to bypass the compiler bug.
36461be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // This implementation makes the compiler happy, at the cost of
36471be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // leaking the UnitTest object.
364841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot
364941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  // CodeGear C++Builder insists on a public destructor for the
365041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  // default implementation.  Use this implementation to keep good OO
365141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  // design with private destructor.
365241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot
365341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot#if (_MSC_VER == 1310 && !defined(_DEBUG)) || defined(__BORLANDC__)
36541be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  static UnitTest* const instance = new UnitTest;
36551be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  return instance;
36561be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#else
36571be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  static UnitTest instance;
36581be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  return &instance;
365941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot#endif  // (_MSC_VER == 1310 && !defined(_DEBUG)) || defined(__BORLANDC__)
366041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot}
366141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot
366241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// Gets the number of successful test cases.
366341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotint UnitTest::successful_test_case_count() const {
366441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  return impl()->successful_test_case_count();
366541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot}
366641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot
366741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// Gets the number of failed test cases.
366841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotint UnitTest::failed_test_case_count() const {
366941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  return impl()->failed_test_case_count();
367041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot}
367141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot
367241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// Gets the number of all test cases.
367341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotint UnitTest::total_test_case_count() const {
367441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  return impl()->total_test_case_count();
367541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot}
367641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot
367741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// Gets the number of all test cases that contain at least one test
367841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// that should run.
367941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotint UnitTest::test_case_to_run_count() const {
368041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  return impl()->test_case_to_run_count();
368141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot}
368241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot
368341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// Gets the number of successful tests.
368441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotint UnitTest::successful_test_count() const {
368541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  return impl()->successful_test_count();
368641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot}
368741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot
368841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// Gets the number of failed tests.
368941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotint UnitTest::failed_test_count() const { return impl()->failed_test_count(); }
369041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot
3691fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes// Gets the number of disabled tests that will be reported in the XML report.
3692fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughesint UnitTest::reportable_disabled_test_count() const {
3693fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes  return impl()->reportable_disabled_test_count();
3694fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes}
3695fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes
369641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// Gets the number of disabled tests.
369741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotint UnitTest::disabled_test_count() const {
369841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  return impl()->disabled_test_count();
369941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot}
370041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot
3701fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes// Gets the number of tests to be printed in the XML report.
3702fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughesint UnitTest::reportable_test_count() const {
3703fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes  return impl()->reportable_test_count();
3704fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes}
3705fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes
370641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// Gets the number of all tests.
370741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotint UnitTest::total_test_count() const { return impl()->total_test_count(); }
370841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot
370941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// Gets the number of tests that should run.
371041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotint UnitTest::test_to_run_count() const { return impl()->test_to_run_count(); }
371141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot
3712fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes// Gets the time of the test program start, in ms from the start of the
3713fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes// UNIX epoch.
3714fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughesinternal::TimeInMillis UnitTest::start_timestamp() const {
3715fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes    return impl()->start_timestamp();
3716fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes}
3717fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes
371841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// Gets the elapsed time, in milliseconds.
371941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotinternal::TimeInMillis UnitTest::elapsed_time() const {
372041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  return impl()->elapsed_time();
372141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot}
372241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot
372341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// Returns true iff the unit test passed (i.e. all test cases passed).
372441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotbool UnitTest::Passed() const { return impl()->Passed(); }
372541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot
372641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// Returns true iff the unit test failed (i.e. some test case failed
372741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// or something outside of all tests failed).
372841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotbool UnitTest::Failed() const { return impl()->Failed(); }
372941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot
373041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// Gets the i-th test case among all the test cases. i can range from 0 to
373141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// total_test_case_count() - 1. If i is not in that range, returns NULL.
373241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotconst TestCase* UnitTest::GetTestCase(int i) const {
373341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  return impl()->GetTestCase(i);
373441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot}
373541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot
3736fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes// Returns the TestResult containing information on test failures and
3737fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes// properties logged outside of individual test cases.
3738fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughesconst TestResult& UnitTest::ad_hoc_test_result() const {
3739fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes  return *impl()->ad_hoc_test_result();
3740fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes}
3741fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes
374241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// Gets the i-th test case among all the test cases. i can range from 0 to
374341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// total_test_case_count() - 1. If i is not in that range, returns NULL.
374441d0579e8de9ef4ff178fc4991043c61a19943f7Brett ChabotTestCase* UnitTest::GetMutableTestCase(int i) {
374541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  return impl()->GetMutableTestCase(i);
374641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot}
374741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot
374841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// Returns the list of event listeners that can be used to track events
374941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// inside Google Test.
375041d0579e8de9ef4ff178fc4991043c61a19943f7Brett ChabotTestEventListeners& UnitTest::listeners() {
375141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  return *impl()->listeners();
37521be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}
37531be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
37541be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Registers and returns a global test environment.  When a test
37551be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// program is run, all global test environments will be set-up in the
37561be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// order they were registered.  After all tests in the program have
37571be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// finished, all global test environments will be torn-down in the
37581be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// *reverse* order they were registered.
37591be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//
37601be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// The UnitTest object takes ownership of the given environment.
37611be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//
37621be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// We don't protect this under mutex_, as we only support calling it
37631be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// from the main thread.
37641be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaEnvironment* UnitTest::AddEnvironment(Environment* env) {
37651be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  if (env == NULL) {
37661be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    return NULL;
37671be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  }
37681be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
376941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  impl_->environments().push_back(env);
37701be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  return env;
37711be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}
37721be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
37731be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Adds a TestPartResult to the current TestResult object.  All Google Test
37741be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// assertion macros (e.g. ASSERT_TRUE, EXPECT_EQ, etc) eventually call
37751be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// this to report their results.  The user code should use the
37761be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// assertion macros instead of calling this directly.
3777fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughesvoid UnitTest::AddTestPartResult(
3778fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes    TestPartResult::Type result_type,
3779fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes    const char* file_name,
3780fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes    int line_number,
3781fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes    const std::string& message,
3782fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes    const std::string& os_stack_trace) GTEST_LOCK_EXCLUDED_(mutex_) {
37831be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  Message msg;
37841be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  msg << message;
37851be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
37861be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  internal::MutexLock lock(&mutex_);
378741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  if (impl_->gtest_trace_stack().size() > 0) {
37881be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    msg << "\n" << GTEST_NAME_ << " trace:";
37891be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
379041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot    for (int i = static_cast<int>(impl_->gtest_trace_stack().size());
379141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot         i > 0; --i) {
379241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot      const internal::TraceInfo& trace = impl_->gtest_trace_stack()[i - 1];
379341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot      msg << "\n" << internal::FormatFileLocation(trace.file, trace.line)
379441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot          << " " << trace.message;
37951be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    }
37961be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  }
37971be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
37981be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  if (os_stack_trace.c_str() != NULL && !os_stack_trace.empty()) {
37991be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    msg << internal::kStackTraceMarker << os_stack_trace;
38001be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  }
38011be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
38021be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  const TestPartResult result =
38031be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    TestPartResult(result_type, file_name, line_number,
38041be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania                   msg.GetString().c_str());
38051be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  impl_->GetTestPartResultReporterForCurrentThread()->
38061be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania      ReportTestPartResult(result);
38071be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
380841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  if (result_type != TestPartResult::kSuccess) {
380941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot    // gtest_break_on_failure takes precedence over
381041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot    // gtest_throw_on_failure.  This allows a user to set the latter
38111be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    // in the code (perhaps in order to use Google Test assertions
38121be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    // with another testing framework) and specify the former on the
38131be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    // command line for debugging.
38141be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    if (GTEST_FLAG(break_on_failure)) {
381541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot#if GTEST_OS_WINDOWS
381641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot      // Using DebugBreak on Windows allows gtest to still break into a debugger
381741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot      // when a failure happens and both the --gtest_break_on_failure and
381841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot      // the --gtest_catch_exceptions flags are specified.
381941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot      DebugBreak();
382041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot#else
382141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot      // Dereference NULL through a volatile pointer to prevent the compiler
382241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot      // from removing. We use this rather than abort() or __builtin_trap() for
382341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot      // portability: Symbian doesn't implement abort() well, and some debuggers
382441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot      // don't correctly trap abort().
382541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot      *static_cast<volatile int*>(NULL) = 1;
382641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot#endif  // GTEST_OS_WINDOWS
38271be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    } else if (GTEST_FLAG(throw_on_failure)) {
38281be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#if GTEST_HAS_EXCEPTIONS
3829fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes      throw internal::GoogleTestFailureException(result);
38301be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#else
38311be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania      // We cannot call abort() as it generates a pop-up in debug mode
38321be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania      // that cannot be suppressed in VC 7.1 or below.
38331be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania      exit(1);
38341be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#endif
38351be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    }
38361be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  }
38371be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}
38381be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
3839fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes// Adds a TestProperty to the current TestResult object when invoked from
3840fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes// inside a test, to current TestCase's ad_hoc_test_result_ when invoked
3841fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes// from SetUpTestCase or TearDownTestCase, or to the global property set
3842fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes// when invoked elsewhere.  If the result already contains a property with
3843fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes// the same key, the value will be updated.
3844fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughesvoid UnitTest::RecordProperty(const std::string& key,
3845fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes                              const std::string& value) {
3846fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes  impl_->RecordProperty(TestProperty(key, value));
38471be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}
38481be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
38491be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Runs all tests in this UnitTest object and prints the result.
38501be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Returns 0 if successful, or 1 otherwise.
38511be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//
38521be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// We don't protect this under mutex_, as we only support calling it
38531be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// from the main thread.
38541be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniaint UnitTest::Run() {
3855fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes  const bool in_death_test_child_process =
3856fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes      internal::GTEST_FLAG(internal_run_death_test).length() > 0;
3857fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes
3858fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes  // Google Test implements this protocol for catching that a test
3859fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes  // program exits before returning control to Google Test:
3860fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes  //
3861fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes  //   1. Upon start, Google Test creates a file whose absolute path
3862fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes  //      is specified by the environment variable
3863fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes  //      TEST_PREMATURE_EXIT_FILE.
3864fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes  //   2. When Google Test has finished its work, it deletes the file.
3865fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes  //
3866fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes  // This allows a test runner to set TEST_PREMATURE_EXIT_FILE before
3867fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes  // running a Google-Test-based test program and check the existence
3868fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes  // of the file at the end of the test execution to see if it has
3869fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes  // exited prematurely.
3870fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes
3871fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes  // If we are in the child process of a death test, don't
3872fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes  // create/delete the premature exit file, as doing so is unnecessary
3873fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes  // and will confuse the parent process.  Otherwise, create/delete
3874fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes  // the file upon entering/leaving this function.  If the program
3875fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes  // somehow exits before this function has a chance to return, the
3876fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes  // premature-exit file will be left undeleted, causing a test runner
3877fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes  // that understands the premature-exit-file protocol to report the
3878fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes  // test as having failed.
3879fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes  const internal::ScopedPrematureExitFile premature_exit_file(
3880fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes      in_death_test_child_process ?
3881fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes      NULL : internal::posix::GetEnv("TEST_PREMATURE_EXIT_FILE"));
3882fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes
388341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  // Captures the value of GTEST_FLAG(catch_exceptions).  This value will be
388441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  // used for the duration of the program.
388541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  impl()->set_catch_exceptions(GTEST_FLAG(catch_exceptions));
38861be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
388741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot#if GTEST_HAS_SEH
38881be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // Either the user wants Google Test to catch exceptions thrown by the
38891be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // tests or this is executing in the context of death test child
38901be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // process. In either case the user does not want to see pop-up dialogs
389141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  // about crashes - they are expected.
389241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  if (impl()->catch_exceptions() || in_death_test_child_process) {
389341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot# if !GTEST_OS_WINDOWS_MOBILE
38941be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    // SetErrorMode doesn't exist on CE.
38951be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    SetErrorMode(SEM_FAILCRITICALERRORS | SEM_NOALIGNMENTFAULTEXCEPT |
38961be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania                 SEM_NOGPFAULTERRORBOX | SEM_NOOPENFILEERRORBOX);
389741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot# endif  // !GTEST_OS_WINDOWS_MOBILE
38981be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
389941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot# if (defined(_MSC_VER) || GTEST_OS_WINDOWS_MINGW) && !GTEST_OS_WINDOWS_MOBILE
39001be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    // Death test children can be terminated with _abort().  On Windows,
39011be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    // _abort() can show a dialog with a warning message.  This forces the
39021be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    // abort message to go to stderr instead.
39031be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    _set_error_mode(_OUT_TO_STDERR);
390441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot# endif
39051be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
390641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot# if _MSC_VER >= 1400 && !GTEST_OS_WINDOWS_MOBILE
39071be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    // In the debug version, Visual Studio pops up a separate dialog
39081be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    // offering a choice to debug the aborted program. We need to suppress
39091be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    // this dialog or it will pop up for every EXPECT/ASSERT_DEATH statement
39101be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    // executed. Google Test will notify the user of any unexpected
39111be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    // failure via stderr.
391241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot    //
39131be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    // VC++ doesn't define _set_abort_behavior() prior to the version 8.0.
39141be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    // Users of prior VC versions shall suffer the agony and pain of
39151be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    // clicking through the countless debug dialogs.
39161be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    // TODO(vladl@google.com): find a way to suppress the abort dialog() in the
39171be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    // debug mode when compiled with VC 7.1 or lower.
39181be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    if (!GTEST_FLAG(break_on_failure))
39191be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania      _set_abort_behavior(
39201be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania          0x0,                                    // Clear the following flags:
39211be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania          _WRITE_ABORT_MSG | _CALL_REPORTFAULT);  // pop-up window, core dump.
392241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot# endif
39231be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  }
392441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot#endif  // GTEST_HAS_SEH
39251be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
392641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  return internal::HandleExceptionsInMethodIfSupported(
392741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot      impl(),
392841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot      &internal::UnitTestImpl::RunAllTests,
392941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot      "auxiliary test code (environments or event listeners)") ? 0 : 1;
39301be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}
39311be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
39321be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Returns the working directory when the first TEST() or TEST_F() was
39331be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// executed.
39341be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniaconst char* UnitTest::original_working_dir() const {
39351be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  return impl_->original_working_dir_.c_str();
39361be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}
39371be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
39381be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Returns the TestCase object for the test that's currently running,
39391be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// or NULL if no test is running.
3940fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughesconst TestCase* UnitTest::current_test_case() const
3941fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes    GTEST_LOCK_EXCLUDED_(mutex_) {
39421be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  internal::MutexLock lock(&mutex_);
39431be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  return impl_->current_test_case();
39441be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}
39451be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
39461be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Returns the TestInfo object for the test that's currently running,
39471be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// or NULL if no test is running.
3948fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughesconst TestInfo* UnitTest::current_test_info() const
3949fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes    GTEST_LOCK_EXCLUDED_(mutex_) {
39501be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  internal::MutexLock lock(&mutex_);
39511be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  return impl_->current_test_info();
39521be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}
39531be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
395441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// Returns the random seed used at the start of the current test run.
395541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotint UnitTest::random_seed() const { return impl_->random_seed(); }
395641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot
39571be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#if GTEST_HAS_PARAM_TEST
39581be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Returns ParameterizedTestCaseRegistry object used to keep track of
39591be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// value-parameterized tests and instantiate and register them.
39601be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniainternal::ParameterizedTestCaseRegistry&
3961fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes    UnitTest::parameterized_test_registry()
3962fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes        GTEST_LOCK_EXCLUDED_(mutex_) {
39631be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  return impl_->parameterized_test_registry();
39641be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}
39651be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#endif  // GTEST_HAS_PARAM_TEST
39661be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
39671be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Creates an empty UnitTest.
39681be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaUnitTest::UnitTest() {
39691be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  impl_ = new internal::UnitTestImpl(this);
39701be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}
39711be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
39721be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Destructor of UnitTest.
39731be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaUnitTest::~UnitTest() {
39741be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  delete impl_;
39751be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}
39761be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
39771be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Pushes a trace defined by SCOPED_TRACE() on to the per-thread
39781be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Google Test trace stack.
3979fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughesvoid UnitTest::PushGTestTrace(const internal::TraceInfo& trace)
3980fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes    GTEST_LOCK_EXCLUDED_(mutex_) {
39811be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  internal::MutexLock lock(&mutex_);
398241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  impl_->gtest_trace_stack().push_back(trace);
39831be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}
39841be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
39851be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Pops a trace from the per-thread Google Test trace stack.
3986fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughesvoid UnitTest::PopGTestTrace()
3987fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes    GTEST_LOCK_EXCLUDED_(mutex_) {
39881be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  internal::MutexLock lock(&mutex_);
398941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  impl_->gtest_trace_stack().pop_back();
39901be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}
39911be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
39921be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catanianamespace internal {
39931be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
39941be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaUnitTestImpl::UnitTestImpl(UnitTest* parent)
39951be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    : parent_(parent),
39961be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#ifdef _MSC_VER
399741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot# pragma warning(push)                    // Saves the current warning state.
399841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot# pragma warning(disable:4355)            // Temporarily disables warning 4355
39991be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania                                         // (using this in initializer).
40001be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania      default_global_test_part_result_reporter_(this),
40011be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania      default_per_thread_test_part_result_reporter_(this),
400241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot# pragma warning(pop)                     // Restores the warning state again.
40031be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#else
40041be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania      default_global_test_part_result_reporter_(this),
40051be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania      default_per_thread_test_part_result_reporter_(this),
40061be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#endif  // _MSC_VER
40071be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania      global_test_part_result_repoter_(
40081be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania          &default_global_test_part_result_reporter_),
40091be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania      per_thread_test_part_result_reporter_(
40101be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania          &default_per_thread_test_part_result_reporter_),
40111be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#if GTEST_HAS_PARAM_TEST
40121be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania      parameterized_test_registry_(),
40131be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania      parameterized_tests_registered_(false),
40141be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#endif  // GTEST_HAS_PARAM_TEST
401541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot      last_death_test_case_(-1),
40161be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania      current_test_case_(NULL),
40171be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania      current_test_info_(NULL),
40181be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania      ad_hoc_test_result_(),
40191be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania      os_stack_trace_getter_(NULL),
402041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot      post_flag_parse_init_performed_(false),
402141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot      random_seed_(0),  // Will be overridden by the flag before first use.
402241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot      random_(0),  // Will be reseeded before first use.
4023fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes      start_timestamp_(0),
40241be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania      elapsed_time_(0),
402541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot#if GTEST_HAS_DEATH_TEST
402641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot      death_test_factory_(new DefaultDeathTestFactory),
402741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot#endif
402841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot      // Will be overridden by the flag before first use.
402941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot      catch_exceptions_(false) {
403041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  listeners()->SetDefaultResultPrinter(new PrettyUnitTestResultPrinter);
40311be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}
40321be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
40331be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaUnitTestImpl::~UnitTestImpl() {
40341be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // Deletes every TestCase.
403541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  ForEach(test_cases_, internal::Delete<TestCase>);
40361be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
40371be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // Deletes every Environment.
403841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  ForEach(environments_, internal::Delete<Environment>);
40391be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
40401be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  delete os_stack_trace_getter_;
40411be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}
40421be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
4043fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes// Adds a TestProperty to the current TestResult object when invoked in a
4044fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes// context of a test, to current test case's ad_hoc_test_result when invoke
4045fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes// from SetUpTestCase/TearDownTestCase, or to the global property set
4046fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes// otherwise.  If the result already contains a property with the same key,
4047fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes// the value will be updated.
4048fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughesvoid UnitTestImpl::RecordProperty(const TestProperty& test_property) {
4049fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes  std::string xml_element;
4050fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes  TestResult* test_result;  // TestResult appropriate for property recording.
4051fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes
4052fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes  if (current_test_info_ != NULL) {
4053fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes    xml_element = "testcase";
4054fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes    test_result = &(current_test_info_->result_);
4055fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes  } else if (current_test_case_ != NULL) {
4056fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes    xml_element = "testsuite";
4057fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes    test_result = &(current_test_case_->ad_hoc_test_result_);
4058fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes  } else {
4059fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes    xml_element = "testsuites";
4060fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes    test_result = &ad_hoc_test_result_;
4061fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes  }
4062fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes  test_result->RecordProperty(xml_element, test_property);
4063fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes}
4064fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes
406541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot#if GTEST_HAS_DEATH_TEST
406641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// Disables event forwarding if the control is currently in a death test
406741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// subprocess. Must not be called before InitGoogleTest.
406841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotvoid UnitTestImpl::SuppressTestEventsIfInSubprocess() {
406941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  if (internal_run_death_test_flag_.get() != NULL)
407041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot    listeners()->SuppressEventForwarding();
407141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot}
407241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot#endif  // GTEST_HAS_DEATH_TEST
407341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot
407441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// Initializes event listeners performing XML output as specified by
407541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// UnitTestOptions. Must not be called before InitGoogleTest.
407641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotvoid UnitTestImpl::ConfigureXmlOutput() {
4077fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes  const std::string& output_format = UnitTestOptions::GetOutputFormat();
407841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  if (output_format == "xml") {
407941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot    listeners()->SetDefaultXmlGenerator(new XmlUnitTestResultPrinter(
408041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot        UnitTestOptions::GetAbsolutePathToOutputFile().c_str()));
408141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  } else if (output_format != "") {
408241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot    printf("WARNING: unrecognized output format \"%s\" ignored.\n",
408341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot           output_format.c_str());
408441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot    fflush(stdout);
408541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  }
408641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot}
408741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot
408841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot#if GTEST_CAN_STREAM_RESULTS_
4089fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes// Initializes event listeners for streaming test results in string form.
409041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// Must not be called before InitGoogleTest.
409141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotvoid UnitTestImpl::ConfigureStreamingOutput() {
4092fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes  const std::string& target = GTEST_FLAG(stream_result_to);
409341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  if (!target.empty()) {
409441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot    const size_t pos = target.find(':');
4095fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes    if (pos != std::string::npos) {
409641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot      listeners()->Append(new StreamingListener(target.substr(0, pos),
409741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot                                                target.substr(pos+1)));
409841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot    } else {
409941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot      printf("WARNING: unrecognized streaming target \"%s\" ignored.\n",
410041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot             target.c_str());
410141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot      fflush(stdout);
410241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot    }
410341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  }
410441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot}
410541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot#endif  // GTEST_CAN_STREAM_RESULTS_
410641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot
410741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// Performs initialization dependent upon flag values obtained in
410841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// ParseGoogleTestFlagsOnly.  Is called from InitGoogleTest after the call to
410941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// ParseGoogleTestFlagsOnly.  In case a user neglects to call InitGoogleTest
411041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// this function is also called from RunAllTests.  Since this function can be
411141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// called more than once, it has to be idempotent.
411241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotvoid UnitTestImpl::PostFlagParsingInit() {
411341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  // Ensures that this function does not execute more than once.
411441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  if (!post_flag_parse_init_performed_) {
411541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot    post_flag_parse_init_performed_ = true;
411641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot
411741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot#if GTEST_HAS_DEATH_TEST
411841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot    InitDeathTestSubprocessControlInfo();
411941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot    SuppressTestEventsIfInSubprocess();
412041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot#endif  // GTEST_HAS_DEATH_TEST
412141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot
412241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot    // Registers parameterized tests. This makes parameterized tests
412341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot    // available to the UnitTest reflection API without running
412441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot    // RUN_ALL_TESTS.
412541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot    RegisterParameterizedTests();
412641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot
412741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot    // Configures listeners for XML output. This makes it possible for users
412841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot    // to shut down the default XML output before invoking RUN_ALL_TESTS.
412941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot    ConfigureXmlOutput();
413041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot
413141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot#if GTEST_CAN_STREAM_RESULTS_
413241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot    // Configures listeners for streaming test results to the specified server.
413341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot    ConfigureStreamingOutput();
413441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot#endif  // GTEST_CAN_STREAM_RESULTS_
413541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  }
413641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot}
413741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot
41381be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// A predicate that checks the name of a TestCase against a known
41391be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// value.
41401be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//
41411be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// This is used for implementation of the UnitTest class only.  We put
41421be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// it in the anonymous namespace to prevent polluting the outer
41431be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// namespace.
41441be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//
41451be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// TestCaseNameIs is copyable.
41461be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniaclass TestCaseNameIs {
41471be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania public:
41481be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // Constructor.
4149fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes  explicit TestCaseNameIs(const std::string& name)
41501be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania      : name_(name) {}
41511be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
41521be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // Returns true iff the name of test_case matches name_.
41531be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  bool operator()(const TestCase* test_case) const {
41541be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    return test_case != NULL && strcmp(test_case->name(), name_.c_str()) == 0;
41551be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  }
41561be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
41571be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania private:
4158fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes  std::string name_;
41591be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania};
41601be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
41611be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Finds and returns a TestCase with the given name.  If one doesn't
416241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// exist, creates one and returns it.  It's the CALLER'S
416341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// RESPONSIBILITY to ensure that this function is only called WHEN THE
416441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// TESTS ARE NOT SHUFFLED.
41651be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//
41661be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Arguments:
41671be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//
41681be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//   test_case_name: name of the test case
416941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot//   type_param:     the name of the test case's type parameter, or NULL if
417041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot//                   this is not a typed or a type-parameterized test case.
41711be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//   set_up_tc:      pointer to the function that sets up the test case
41721be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//   tear_down_tc:   pointer to the function that tears down the test case
41731be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaTestCase* UnitTestImpl::GetTestCase(const char* test_case_name,
417441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot                                    const char* type_param,
41751be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania                                    Test::SetUpTestCaseFunc set_up_tc,
41761be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania                                    Test::TearDownTestCaseFunc tear_down_tc) {
41771be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // Can we find a TestCase with the given name?
417841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  const std::vector<TestCase*>::const_iterator test_case =
417941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot      std::find_if(test_cases_.begin(), test_cases_.end(),
418041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot                   TestCaseNameIs(test_case_name));
418141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot
418241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  if (test_case != test_cases_.end())
418341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot    return *test_case;
418441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot
418541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  // No.  Let's create one.
418641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  TestCase* const new_test_case =
418741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot      new TestCase(test_case_name, type_param, set_up_tc, tear_down_tc);
418841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot
418941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  // Is this a death test case?
4190fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes  if (internal::UnitTestOptions::MatchesFilter(test_case_name,
419141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot                                               kDeathTestCaseFilter)) {
419241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot    // Yes.  Inserts the test case after the last death test case
419341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot    // defined so far.  This only works when the test cases haven't
419441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot    // been shuffled.  Otherwise we may end up running a death test
419541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot    // after a non-death test.
419641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot    ++last_death_test_case_;
419741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot    test_cases_.insert(test_cases_.begin() + last_death_test_case_,
419841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot                       new_test_case);
419941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  } else {
420041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot    // No.  Appends to the end of the list.
420141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot    test_cases_.push_back(new_test_case);
42021be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  }
42031be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
420441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  test_case_indices_.push_back(static_cast<int>(test_case_indices_.size()));
420541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  return new_test_case;
42061be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}
42071be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
42081be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Helpers for setting up / tearing down the given environment.  They
420941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// are for use in the ForEach() function.
42101be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniastatic void SetUpEnvironment(Environment* env) { env->SetUp(); }
42111be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniastatic void TearDownEnvironment(Environment* env) { env->TearDown(); }
42121be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
42131be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Runs all tests in this UnitTest object, prints the result, and
421441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// returns true if all tests are successful.  If any exception is
421541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// thrown during a test, the test is considered to be failed, but the
421641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// rest of the tests will still be run.
421741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot//
421841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// When parameterized tests are enabled, it expands and registers
42191be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// parameterized tests first in RegisterParameterizedTests().
42201be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// All other functions called from RunAllTests() may safely assume that
42211be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// parameterized tests are ready to be counted and run.
422241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotbool UnitTestImpl::RunAllTests() {
42231be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // Makes sure InitGoogleTest() was called.
42241be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  if (!GTestIsInitialized()) {
42251be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    printf("%s",
42261be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania           "\nThis test program did NOT call ::testing::InitGoogleTest "
42271be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania           "before calling RUN_ALL_TESTS().  Please fix it.\n");
422841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot    return false;
42291be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  }
42301be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
42311be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // Do not run any test if the --help flag was specified.
42321be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  if (g_help_flag)
423341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot    return true;
42341be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
423541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  // Repeats the call to the post-flag parsing initialization in case the
423641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  // user didn't call InitGoogleTest.
423741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  PostFlagParsingInit();
42381be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
42391be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // Even if sharding is not on, test runners may want to use the
42401be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // GTEST_SHARD_STATUS_FILE to query whether the test supports the sharding
42411be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // protocol.
42421be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  internal::WriteToShardStatusFileIfNeeded();
42431be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
42441be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // True iff we are in a subprocess for running a thread-safe-style
42451be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // death test.
42461be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  bool in_subprocess_for_death_test = false;
42471be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
42481be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#if GTEST_HAS_DEATH_TEST
42491be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  in_subprocess_for_death_test = (internal_run_death_test_flag_.get() != NULL);
42501be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#endif  // GTEST_HAS_DEATH_TEST
42511be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
42521be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  const bool should_shard = ShouldShard(kTestTotalShards, kTestShardIndex,
42531be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania                                        in_subprocess_for_death_test);
42541be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
42551be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // Compares the full test names with the filter to decide which
42561be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // tests to run.
42571be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  const bool has_tests_to_run = FilterTests(should_shard
42581be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania                                              ? HONOR_SHARDING_PROTOCOL
42591be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania                                              : IGNORE_SHARDING_PROTOCOL) > 0;
42601be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
426141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  // Lists the tests and exits if the --gtest_list_tests flag was specified.
426241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  if (GTEST_FLAG(list_tests)) {
426341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot    // This must be called *after* FilterTests() has been called.
426441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot    ListTestsMatchingFilter();
426541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot    return true;
426641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  }
426741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot
426841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  random_seed_ = GTEST_FLAG(shuffle) ?
426941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot      GetRandomSeedFromFlag(GTEST_FLAG(random_seed)) : 0;
427041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot
42711be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // True iff at least one test has failed.
42721be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  bool failed = false;
42731be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
427441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  TestEventListener* repeater = listeners()->repeater();
427541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot
4276fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes  start_timestamp_ = GetTimeInMillis();
427741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  repeater->OnTestProgramStart(*parent_);
427841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot
42791be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // How many times to repeat the tests?  We don't want to repeat them
42801be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // when we are inside the subprocess of a death test.
42811be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  const int repeat = in_subprocess_for_death_test ? 1 : GTEST_FLAG(repeat);
42821be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // Repeats forever if the repeat count is negative.
42831be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  const bool forever = repeat < 0;
42841be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  for (int i = 0; forever || i != repeat; i++) {
428541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot    // We want to preserve failures generated by ad-hoc test
428641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot    // assertions executed before RUN_ALL_TESTS().
428741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot    ClearNonAdHocTestResult();
42881be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
42891be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    const TimeInMillis start = GetTimeInMillis();
42901be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
429141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot    // Shuffles test cases and tests if requested.
429241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot    if (has_tests_to_run && GTEST_FLAG(shuffle)) {
429341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot      random()->Reseed(random_seed_);
429441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot      // This should be done before calling OnTestIterationStart(),
429541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot      // such that a test event listener can see the actual test order
429641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot      // in the event.
429741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot      ShuffleTests();
429841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot    }
429941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot
430041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot    // Tells the unit test event listeners that the tests are about to start.
430141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot    repeater->OnTestIterationStart(*parent_, i);
430241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot
43031be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    // Runs each test case if there is at least one test to run.
43041be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    if (has_tests_to_run) {
43051be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania      // Sets up all environments beforehand.
430641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot      repeater->OnEnvironmentsSetUpStart(*parent_);
430741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot      ForEach(environments_, SetUpEnvironment);
430841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot      repeater->OnEnvironmentsSetUpEnd(*parent_);
43091be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
43101be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania      // Runs the tests only if there was no fatal failure during global
43111be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania      // set-up.
43121be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania      if (!Test::HasFatalFailure()) {
431341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot        for (int test_index = 0; test_index < total_test_case_count();
431441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot             test_index++) {
431541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot          GetMutableTestCase(test_index)->Run();
431641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot        }
43171be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania      }
43181be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
43191be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania      // Tears down all environments in reverse order afterwards.
432041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot      repeater->OnEnvironmentsTearDownStart(*parent_);
432141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot      std::for_each(environments_.rbegin(), environments_.rend(),
432241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot                    TearDownEnvironment);
432341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot      repeater->OnEnvironmentsTearDownEnd(*parent_);
43241be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    }
43251be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
43261be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    elapsed_time_ = GetTimeInMillis() - start;
43271be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
432841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot    // Tells the unit test event listener that the tests have just finished.
432941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot    repeater->OnTestIterationEnd(*parent_, i);
43301be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
43311be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    // Gets the result and clears it.
43321be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    if (!Passed()) {
43331be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania      failed = true;
43341be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    }
433541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot
433641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot    // Restores the original test order after the iteration.  This
433741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot    // allows the user to quickly repro a failure that happens in the
433841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot    // N-th iteration without repeating the first (N - 1) iterations.
433941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot    // This is not enclosed in "if (GTEST_FLAG(shuffle)) { ... }", in
434041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot    // case the user somehow changes the value of the flag somewhere
434141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot    // (it's always safe to unshuffle the tests).
434241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot    UnshuffleTests();
434341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot
434441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot    if (GTEST_FLAG(shuffle)) {
434541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot      // Picks a new random seed for each iteration.
434641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot      random_seed_ = GetNextRandomSeed(random_seed_);
434741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot    }
43481be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  }
43491be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
435041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  repeater->OnTestProgramEnd(*parent_);
435141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot
435241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  return !failed;
43531be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}
43541be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
43551be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Reads the GTEST_SHARD_STATUS_FILE environment variable, and creates the file
43561be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// if the variable is present. If a file already exists at this location, this
43571be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// function will write over it. If the variable is present, but the file cannot
43581be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// be created, prints an error and exits.
43591be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniavoid WriteToShardStatusFileIfNeeded() {
436041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  const char* const test_shard_file = posix::GetEnv(kTestShardStatusFile);
43611be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  if (test_shard_file != NULL) {
436241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot    FILE* const file = posix::FOpen(test_shard_file, "w");
43631be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    if (file == NULL) {
43641be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania      ColoredPrintf(COLOR_RED,
43651be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania                    "Could not write to the test shard status file \"%s\" "
43661be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania                    "specified by the %s environment variable.\n",
43671be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania                    test_shard_file, kTestShardStatusFile);
43681be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania      fflush(stdout);
43691be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania      exit(EXIT_FAILURE);
43701be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    }
43711be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    fclose(file);
43721be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  }
43731be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}
43741be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
43751be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Checks whether sharding is enabled by examining the relevant
43761be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// environment variable values. If the variables are present,
43771be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// but inconsistent (i.e., shard_index >= total_shards), prints
43781be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// an error and exits. If in_subprocess_for_death_test, sharding is
43791be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// disabled because it must only be applied to the original test
43801be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// process. Otherwise, we could filter out death tests we intended to execute.
43811be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniabool ShouldShard(const char* total_shards_env,
43821be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania                 const char* shard_index_env,
43831be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania                 bool in_subprocess_for_death_test) {
43841be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  if (in_subprocess_for_death_test) {
43851be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    return false;
43861be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  }
43871be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
43881be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  const Int32 total_shards = Int32FromEnvOrDie(total_shards_env, -1);
43891be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  const Int32 shard_index = Int32FromEnvOrDie(shard_index_env, -1);
43901be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
43911be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  if (total_shards == -1 && shard_index == -1) {
43921be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    return false;
43931be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  } else if (total_shards == -1 && shard_index != -1) {
43941be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    const Message msg = Message()
43951be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania      << "Invalid environment variables: you have "
43961be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania      << kTestShardIndex << " = " << shard_index
43971be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania      << ", but have left " << kTestTotalShards << " unset.\n";
43981be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    ColoredPrintf(COLOR_RED, msg.GetString().c_str());
43991be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    fflush(stdout);
44001be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    exit(EXIT_FAILURE);
44011be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  } else if (total_shards != -1 && shard_index == -1) {
44021be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    const Message msg = Message()
44031be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania      << "Invalid environment variables: you have "
44041be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania      << kTestTotalShards << " = " << total_shards
44051be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania      << ", but have left " << kTestShardIndex << " unset.\n";
44061be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    ColoredPrintf(COLOR_RED, msg.GetString().c_str());
44071be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    fflush(stdout);
44081be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    exit(EXIT_FAILURE);
44091be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  } else if (shard_index < 0 || shard_index >= total_shards) {
44101be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    const Message msg = Message()
44111be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania      << "Invalid environment variables: we require 0 <= "
44121be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania      << kTestShardIndex << " < " << kTestTotalShards
44131be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania      << ", but you have " << kTestShardIndex << "=" << shard_index
44141be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania      << ", " << kTestTotalShards << "=" << total_shards << ".\n";
44151be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    ColoredPrintf(COLOR_RED, msg.GetString().c_str());
44161be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    fflush(stdout);
44171be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    exit(EXIT_FAILURE);
44181be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  }
44191be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
44201be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  return total_shards > 1;
44211be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}
44221be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
44231be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Parses the environment variable var as an Int32. If it is unset,
44241be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// returns default_val. If it is not an Int32, prints an error
44251be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// and aborts.
442641d0579e8de9ef4ff178fc4991043c61a19943f7Brett ChabotInt32 Int32FromEnvOrDie(const char* var, Int32 default_val) {
442741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  const char* str_val = posix::GetEnv(var);
44281be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  if (str_val == NULL) {
44291be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    return default_val;
44301be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  }
44311be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
44321be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  Int32 result;
44331be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  if (!ParseInt32(Message() << "The value of environment variable " << var,
44341be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania                  str_val, &result)) {
44351be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    exit(EXIT_FAILURE);
44361be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  }
44371be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  return result;
44381be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}
44391be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
44401be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Given the total number of shards, the shard index, and the test id,
44411be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// returns true iff the test should be run on this shard. The test id is
44421be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// some arbitrary but unique non-negative integer assigned to each test
44431be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// method. Assumes that 0 <= shard_index < total_shards.
44441be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniabool ShouldRunTestOnShard(int total_shards, int shard_index, int test_id) {
44451be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  return (test_id % total_shards) == shard_index;
44461be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}
44471be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
44481be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Compares the name of each test with the user-specified filter to
44491be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// decide whether the test should be run, then records the result in
44501be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// each TestCase and TestInfo object.
44511be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// If shard_tests == true, further filters tests based on sharding
44521be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// variables in the environment - see
44531be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// http://code.google.com/p/googletest/wiki/GoogleTestAdvancedGuide.
44541be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Returns the number of tests that should run.
44551be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniaint UnitTestImpl::FilterTests(ReactionToSharding shard_tests) {
44561be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  const Int32 total_shards = shard_tests == HONOR_SHARDING_PROTOCOL ?
44571be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania      Int32FromEnvOrDie(kTestTotalShards, -1) : -1;
44581be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  const Int32 shard_index = shard_tests == HONOR_SHARDING_PROTOCOL ?
44591be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania      Int32FromEnvOrDie(kTestShardIndex, -1) : -1;
44601be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
44611be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // num_runnable_tests are the number of tests that will
44621be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // run across all shards (i.e., match filter and are not disabled).
44631be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // num_selected_tests are the number of tests to be run on
44641be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // this shard.
44651be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  int num_runnable_tests = 0;
44661be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  int num_selected_tests = 0;
446741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  for (size_t i = 0; i < test_cases_.size(); i++) {
446841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot    TestCase* const test_case = test_cases_[i];
4469fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes    const std::string &test_case_name = test_case->name();
44701be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    test_case->set_should_run(false);
44711be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
447241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot    for (size_t j = 0; j < test_case->test_info_list().size(); j++) {
447341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot      TestInfo* const test_info = test_case->test_info_list()[j];
4474fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes      const std::string test_name(test_info->name());
44751be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania      // A test is disabled if test case name or test name matches
44761be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania      // kDisableTestFilter.
44771be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania      const bool is_disabled =
44781be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania          internal::UnitTestOptions::MatchesFilter(test_case_name,
44791be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania                                                   kDisableTestFilter) ||
44801be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania          internal::UnitTestOptions::MatchesFilter(test_name,
44811be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania                                                   kDisableTestFilter);
448241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot      test_info->is_disabled_ = is_disabled;
44831be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
448441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot      const bool matches_filter =
44851be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania          internal::UnitTestOptions::FilterMatchesTest(test_case_name,
44861be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania                                                       test_name);
448741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot      test_info->matches_filter_ = matches_filter;
448841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot
448941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot      const bool is_runnable =
449041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot          (GTEST_FLAG(also_run_disabled_tests) || !is_disabled) &&
449141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot          matches_filter;
44921be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
44931be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania      const bool is_selected = is_runnable &&
44941be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania          (shard_tests == IGNORE_SHARDING_PROTOCOL ||
44951be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania           ShouldRunTestOnShard(total_shards, shard_index,
44961be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania                                num_runnable_tests));
44971be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
44981be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania      num_runnable_tests += is_runnable;
44991be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania      num_selected_tests += is_selected;
45001be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
450141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot      test_info->should_run_ = is_selected;
45021be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania      test_case->set_should_run(test_case->should_run() || is_selected);
45031be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    }
45041be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  }
45051be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  return num_selected_tests;
45061be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}
45071be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
4508fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes// Prints the given C-string on a single line by replacing all '\n'
4509fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes// characters with string "\\n".  If the output takes more than
4510fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes// max_length characters, only prints the first max_length characters
4511fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes// and "...".
4512fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughesstatic void PrintOnOneLine(const char* str, int max_length) {
4513fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes  if (str != NULL) {
4514fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes    for (int i = 0; *str != '\0'; ++str) {
4515fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes      if (i >= max_length) {
4516fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes        printf("...");
4517fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes        break;
4518fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes      }
4519fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes      if (*str == '\n') {
4520fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes        printf("\\n");
4521fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes        i += 2;
4522fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes      } else {
4523fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes        printf("%c", *str);
4524fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes        ++i;
4525fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes      }
4526fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes    }
4527fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes  }
4528fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes}
4529fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes
453041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// Prints the names of the tests matching the user-specified filter flag.
453141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotvoid UnitTestImpl::ListTestsMatchingFilter() {
4532fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes  // Print at most this many characters for each type/value parameter.
4533fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes  const int kMaxParamLength = 250;
4534fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes
453541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  for (size_t i = 0; i < test_cases_.size(); i++) {
453641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot    const TestCase* const test_case = test_cases_[i];
453741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot    bool printed_test_case_name = false;
45381be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
453941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot    for (size_t j = 0; j < test_case->test_info_list().size(); j++) {
454041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot      const TestInfo* const test_info =
454141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot          test_case->test_info_list()[j];
454241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot      if (test_info->matches_filter_) {
454341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot        if (!printed_test_case_name) {
454441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot          printed_test_case_name = true;
4545fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes          printf("%s.", test_case->name());
4546fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes          if (test_case->type_param() != NULL) {
4547fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes            printf("  # %s = ", kTypeParamLabel);
4548fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes            // We print the type parameter on a single line to make
4549fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes            // the output easy to parse by a program.
4550fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes            PrintOnOneLine(test_case->type_param(), kMaxParamLength);
4551fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes          }
4552fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes          printf("\n");
4553fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes        }
4554fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes        printf("  %s", test_info->name());
4555fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes        if (test_info->value_param() != NULL) {
4556fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes          printf("  # %s = ", kValueParamLabel);
4557fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes          // We print the value parameter on a single line to make the
4558fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes          // output easy to parse by a program.
4559fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes          PrintOnOneLine(test_info->value_param(), kMaxParamLength);
456041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot        }
4561fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes        printf("\n");
456241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot      }
45631be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    }
45641be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  }
45651be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  fflush(stdout);
45661be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}
45671be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
45681be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Sets the OS stack trace getter.
45691be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//
45701be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Does nothing if the input and the current OS stack trace getter are
45711be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// the same; otherwise, deletes the old getter and makes the input the
45721be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// current getter.
45731be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniavoid UnitTestImpl::set_os_stack_trace_getter(
45741be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    OsStackTraceGetterInterface* getter) {
45751be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  if (os_stack_trace_getter_ != getter) {
45761be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    delete os_stack_trace_getter_;
45771be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    os_stack_trace_getter_ = getter;
45781be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  }
45791be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}
45801be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
45811be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Returns the current OS stack trace getter if it is not NULL;
45821be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// otherwise, creates an OsStackTraceGetter, makes it the current
45831be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// getter, and returns it.
45841be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaOsStackTraceGetterInterface* UnitTestImpl::os_stack_trace_getter() {
45851be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  if (os_stack_trace_getter_ == NULL) {
45861be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    os_stack_trace_getter_ = new OsStackTraceGetter;
45871be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  }
45881be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
45891be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  return os_stack_trace_getter_;
45901be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}
45911be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
45921be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Returns the TestResult for the test that's currently running, or
45931be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// the TestResult for the ad hoc test if no test is running.
459441d0579e8de9ef4ff178fc4991043c61a19943f7Brett ChabotTestResult* UnitTestImpl::current_test_result() {
45951be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  return current_test_info_ ?
459641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot      &(current_test_info_->result_) : &ad_hoc_test_result_;
459741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot}
459841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot
459941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// Shuffles all test cases, and the tests within each test case,
460041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// making sure that death tests are still run first.
460141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotvoid UnitTestImpl::ShuffleTests() {
460241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  // Shuffles the death test cases.
460341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  ShuffleRange(random(), 0, last_death_test_case_ + 1, &test_case_indices_);
460441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot
460541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  // Shuffles the non-death test cases.
460641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  ShuffleRange(random(), last_death_test_case_ + 1,
460741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot               static_cast<int>(test_cases_.size()), &test_case_indices_);
460841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot
460941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  // Shuffles the tests inside each test case.
461041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  for (size_t i = 0; i < test_cases_.size(); i++) {
461141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot    test_cases_[i]->ShuffleTests(random());
461241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  }
461341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot}
461441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot
461541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// Restores the test cases and tests to their order before the first shuffle.
461641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotvoid UnitTestImpl::UnshuffleTests() {
461741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  for (size_t i = 0; i < test_cases_.size(); i++) {
461841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot    // Unshuffles the tests in each test case.
461941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot    test_cases_[i]->UnshuffleTests();
462041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot    // Resets the index of each test case.
462141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot    test_case_indices_[i] = static_cast<int>(i);
462241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  }
46231be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}
46241be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
4625fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes// Returns the current OS stack trace as an std::string.
46261be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//
46271be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// The maximum number of stack frames to be included is specified by
46281be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// the gtest_stack_trace_depth flag.  The skip_count parameter
46291be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// specifies the number of top frames to be skipped, which doesn't
46301be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// count against the number of frames to be included.
46311be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//
46321be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// For example, if Foo() calls Bar(), which in turn calls
46331be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// GetCurrentOsStackTraceExceptTop(..., 1), Foo() will be included in
46341be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// the trace but Bar() and GetCurrentOsStackTraceExceptTop() won't.
4635fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughesstd::string GetCurrentOsStackTraceExceptTop(UnitTest* /*unit_test*/,
4636fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes                                            int skip_count) {
46371be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // We pass skip_count + 1 to skip this wrapper function in addition
46381be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // to what the user really wants to skip.
463941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  return GetUnitTestImpl()->CurrentOsStackTraceExceptTop(skip_count + 1);
46401be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}
46411be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
464241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// Used by the GTEST_SUPPRESS_UNREACHABLE_CODE_WARNING_BELOW_ macro to
464341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// suppress unreachable code warnings.
46441be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catanianamespace {
46451be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniaclass ClassUniqueToAlwaysTrue {};
46461be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}
46471be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
464841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotbool IsTrue(bool condition) { return condition; }
464941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot
46501be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniabool AlwaysTrue() {
46511be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#if GTEST_HAS_EXCEPTIONS
46521be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // This condition is always false so AlwaysTrue() never actually throws,
46531be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // but it makes the compiler think that it may throw.
465441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  if (IsTrue(false))
46551be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    throw ClassUniqueToAlwaysTrue();
46561be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#endif  // GTEST_HAS_EXCEPTIONS
46571be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  return true;
46581be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}
46591be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
466041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// If *pstr starts with the given prefix, modifies *pstr to be right
466141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// past the prefix and returns true; otherwise leaves *pstr unchanged
466241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// and returns false.  None of pstr, *pstr, and prefix can be NULL.
466341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotbool SkipPrefix(const char* prefix, const char** pstr) {
466441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  const size_t prefix_len = strlen(prefix);
466541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  if (strncmp(*pstr, prefix, prefix_len) == 0) {
466641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot    *pstr += prefix_len;
466741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot    return true;
466841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  }
466941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  return false;
467041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot}
467141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot
46721be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Parses a string as a command line flag.  The string should have
46731be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// the format "--flag=value".  When def_optional is true, the "=value"
46741be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// part can be omitted.
46751be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//
46761be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Returns the value of the flag, or NULL if the parsing failed.
46771be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniaconst char* ParseFlagValue(const char* str,
46781be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania                           const char* flag,
46791be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania                           bool def_optional) {
46801be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // str and flag must not be NULL.
46811be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  if (str == NULL || flag == NULL) return NULL;
46821be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
46831be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // The flag must start with "--" followed by GTEST_FLAG_PREFIX_.
4684fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes  const std::string flag_str = std::string("--") + GTEST_FLAG_PREFIX_ + flag;
468541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  const size_t flag_len = flag_str.length();
46861be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  if (strncmp(str, flag_str.c_str(), flag_len) != 0) return NULL;
46871be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
46881be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // Skips the flag name.
46891be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  const char* flag_end = str + flag_len;
46901be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
46911be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // When def_optional is true, it's OK to not have a "=value" part.
46921be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  if (def_optional && (flag_end[0] == '\0')) {
46931be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    return flag_end;
46941be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  }
46951be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
46961be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // If def_optional is true and there are more characters after the
46971be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // flag name, or if def_optional is false, there must be a '=' after
46981be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // the flag name.
46991be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  if (flag_end[0] != '=') return NULL;
47001be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
47011be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // Returns the string after "=".
47021be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  return flag_end + 1;
47031be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}
47041be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
47051be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Parses a string for a bool flag, in the form of either
47061be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// "--flag=value" or "--flag".
47071be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//
47081be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// In the former case, the value is taken as true as long as it does
47091be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// not start with '0', 'f', or 'F'.
47101be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//
47111be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// In the latter case, the value is taken as true.
47121be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//
47131be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// On success, stores the value of the flag in *value, and returns
47141be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// true.  On failure, returns false without changing *value.
47151be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniabool ParseBoolFlag(const char* str, const char* flag, bool* value) {
47161be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // Gets the value of the flag as a string.
47171be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  const char* const value_str = ParseFlagValue(str, flag, true);
47181be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
47191be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // Aborts if the parsing failed.
47201be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  if (value_str == NULL) return false;
47211be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
47221be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // Converts the string value to a bool.
47231be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  *value = !(*value_str == '0' || *value_str == 'f' || *value_str == 'F');
47241be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  return true;
47251be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}
47261be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
47271be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Parses a string for an Int32 flag, in the form of
47281be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// "--flag=value".
47291be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//
47301be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// On success, stores the value of the flag in *value, and returns
47311be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// true.  On failure, returns false without changing *value.
47321be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniabool ParseInt32Flag(const char* str, const char* flag, Int32* value) {
47331be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // Gets the value of the flag as a string.
47341be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  const char* const value_str = ParseFlagValue(str, flag, false);
47351be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
47361be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // Aborts if the parsing failed.
47371be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  if (value_str == NULL) return false;
47381be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
47391be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // Sets *value to the value of the flag.
47401be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  return ParseInt32(Message() << "The value of flag --" << flag,
47411be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania                    value_str, value);
47421be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}
47431be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
47441be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Parses a string for a string flag, in the form of
47451be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// "--flag=value".
47461be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//
47471be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// On success, stores the value of the flag in *value, and returns
47481be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// true.  On failure, returns false without changing *value.
4749fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughesbool ParseStringFlag(const char* str, const char* flag, std::string* value) {
47501be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // Gets the value of the flag as a string.
47511be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  const char* const value_str = ParseFlagValue(str, flag, false);
47521be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
47531be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // Aborts if the parsing failed.
47541be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  if (value_str == NULL) return false;
47551be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
47561be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // Sets *value to the value of the flag.
47571be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  *value = value_str;
47581be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  return true;
47591be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}
47601be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
476141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// Determines whether a string has a prefix that Google Test uses for its
476241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// flags, i.e., starts with GTEST_FLAG_PREFIX_ or GTEST_FLAG_PREFIX_DASH_.
476341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// If Google Test detects that a command line flag has its prefix but is not
476441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// recognized, it will print its help message. Flags starting with
476541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// GTEST_INTERNAL_PREFIX_ followed by "internal_" are considered Google Test
476641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// internal flags and do not trigger the help message.
476741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotstatic bool HasGoogleTestFlagPrefix(const char* str) {
476841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  return (SkipPrefix("--", &str) ||
476941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot          SkipPrefix("-", &str) ||
477041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot          SkipPrefix("/", &str)) &&
477141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot         !SkipPrefix(GTEST_FLAG_PREFIX_ "internal_", &str) &&
477241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot         (SkipPrefix(GTEST_FLAG_PREFIX_, &str) ||
477341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot          SkipPrefix(GTEST_FLAG_PREFIX_DASH_, &str));
477441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot}
477541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot
47761be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Prints a string containing code-encoded text.  The following escape
47771be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// sequences can be used in the string to control the text color:
47781be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//
47791be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//   @@    prints a single '@' character.
47801be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//   @R    changes the color to red.
47811be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//   @G    changes the color to green.
47821be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//   @Y    changes the color to yellow.
47831be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//   @D    changes to the default terminal text color.
47841be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//
47851be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// TODO(wan@google.com): Write tests for this once we add stdout
47861be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// capturing to Google Test.
47871be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniastatic void PrintColorEncoded(const char* str) {
47881be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  GTestColor color = COLOR_DEFAULT;  // The current color.
47891be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
47901be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // Conceptually, we split the string into segments divided by escape
47911be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // sequences.  Then we print one segment at a time.  At the end of
47921be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // each iteration, the str pointer advances to the beginning of the
47931be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // next segment.
47941be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  for (;;) {
47951be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    const char* p = strchr(str, '@');
47961be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    if (p == NULL) {
47971be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania      ColoredPrintf(color, "%s", str);
47981be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania      return;
47991be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    }
48001be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
4801fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes    ColoredPrintf(color, "%s", std::string(str, p).c_str());
48021be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
48031be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    const char ch = p[1];
48041be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    str = p + 2;
48051be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    if (ch == '@') {
48061be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania      ColoredPrintf(color, "@");
48071be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    } else if (ch == 'D') {
48081be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania      color = COLOR_DEFAULT;
48091be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    } else if (ch == 'R') {
48101be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania      color = COLOR_RED;
48111be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    } else if (ch == 'G') {
48121be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania      color = COLOR_GREEN;
48131be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    } else if (ch == 'Y') {
48141be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania      color = COLOR_YELLOW;
48151be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    } else {
48161be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania      --str;
48171be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    }
48181be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  }
48191be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}
48201be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
48211be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniastatic const char kColorEncodedHelpMessage[] =
48221be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania"This program contains tests written using " GTEST_NAME_ ". You can use the\n"
48231be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania"following command line flags to control its behavior:\n"
48241be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania"\n"
48251be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania"Test Selection:\n"
48261be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania"  @G--" GTEST_FLAG_PREFIX_ "list_tests@D\n"
48271be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania"      List the names of all tests instead of running them. The name of\n"
48281be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania"      TEST(Foo, Bar) is \"Foo.Bar\".\n"
48291be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania"  @G--" GTEST_FLAG_PREFIX_ "filter=@YPOSTIVE_PATTERNS"
48301be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    "[@G-@YNEGATIVE_PATTERNS]@D\n"
48311be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania"      Run only the tests whose name matches one of the positive patterns but\n"
48321be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania"      none of the negative patterns. '?' matches any single character; '*'\n"
48331be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania"      matches any substring; ':' separates two patterns.\n"
48341be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania"  @G--" GTEST_FLAG_PREFIX_ "also_run_disabled_tests@D\n"
48351be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania"      Run all disabled tests too.\n"
483641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot"\n"
483741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot"Test Execution:\n"
48381be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania"  @G--" GTEST_FLAG_PREFIX_ "repeat=@Y[COUNT]@D\n"
48391be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania"      Run the tests repeatedly; use a negative count to repeat forever.\n"
484041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot"  @G--" GTEST_FLAG_PREFIX_ "shuffle@D\n"
484141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot"      Randomize tests' orders on every iteration.\n"
484241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot"  @G--" GTEST_FLAG_PREFIX_ "random_seed=@Y[NUMBER]@D\n"
484341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot"      Random number seed to use for shuffling test orders (between 1 and\n"
484441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot"      99999, or 0 to use a seed based on the current time).\n"
48451be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania"\n"
48461be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania"Test Output:\n"
48471be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania"  @G--" GTEST_FLAG_PREFIX_ "color=@Y(@Gyes@Y|@Gno@Y|@Gauto@Y)@D\n"
48481be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania"      Enable/disable colored output. The default is @Gauto@D.\n"
484941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot"  -@G-" GTEST_FLAG_PREFIX_ "print_time=0@D\n"
485041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot"      Don't print the elapsed time of each test.\n"
48511be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania"  @G--" GTEST_FLAG_PREFIX_ "output=xml@Y[@G:@YDIRECTORY_PATH@G"
48521be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    GTEST_PATH_SEP_ "@Y|@G:@YFILE_PATH]@D\n"
48531be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania"      Generate an XML report in the given directory or with the given file\n"
48541be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania"      name. @YFILE_PATH@D defaults to @Gtest_details.xml@D.\n"
485541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot#if GTEST_CAN_STREAM_RESULTS_
485641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot"  @G--" GTEST_FLAG_PREFIX_ "stream_result_to=@YHOST@G:@YPORT@D\n"
485741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot"      Stream test results to the given server.\n"
485841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot#endif  // GTEST_CAN_STREAM_RESULTS_
48591be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania"\n"
486041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot"Assertion Behavior:\n"
486141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot#if GTEST_HAS_DEATH_TEST && !GTEST_OS_WINDOWS
486241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot"  @G--" GTEST_FLAG_PREFIX_ "death_test_style=@Y(@Gfast@Y|@Gthreadsafe@Y)@D\n"
486341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot"      Set the default death test style.\n"
486441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot#endif  // GTEST_HAS_DEATH_TEST && !GTEST_OS_WINDOWS
48651be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania"  @G--" GTEST_FLAG_PREFIX_ "break_on_failure@D\n"
48661be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania"      Turn assertion failures into debugger break-points.\n"
48671be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania"  @G--" GTEST_FLAG_PREFIX_ "throw_on_failure@D\n"
48681be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania"      Turn assertion failures into C++ exceptions.\n"
486941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot"  @G--" GTEST_FLAG_PREFIX_ "catch_exceptions=0@D\n"
487041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot"      Do not report exceptions as test failures. Instead, allow them\n"
487141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot"      to crash the program or throw a pop-up (on Windows).\n"
48721be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania"\n"
48731be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania"Except for @G--" GTEST_FLAG_PREFIX_ "list_tests@D, you can alternatively set "
48741be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    "the corresponding\n"
48751be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania"environment variable of a flag (all letters in upper-case). For example, to\n"
487641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot"disable colored text output, you can either specify @G--" GTEST_FLAG_PREFIX_
487741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot    "color=no@D or set\n"
487841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot"the @G" GTEST_FLAG_PREFIX_UPPER_ "COLOR@D environment variable to @Gno@D.\n"
48791be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania"\n"
48801be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania"For more information, please read the " GTEST_NAME_ " documentation at\n"
48811be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania"@G" GTEST_PROJECT_URL_ "@D. If you find a bug in " GTEST_NAME_ "\n"
48821be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania"(not one in your own code or tests), please report it to\n"
48831be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania"@G<" GTEST_DEV_EMAIL_ ">@D.\n";
48841be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
48851be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Parses the command line for Google Test flags, without initializing
48861be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// other parts of Google Test.  The type parameter CharType can be
48871be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// instantiated to either char or wchar_t.
48881be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniatemplate <typename CharType>
48891be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniavoid ParseGoogleTestFlagsOnlyImpl(int* argc, CharType** argv) {
48901be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  for (int i = 1; i < *argc; i++) {
4891fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes    const std::string arg_string = StreamableToString(argv[i]);
48921be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    const char* const arg = arg_string.c_str();
48931be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
48941be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    using internal::ParseBoolFlag;
48951be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    using internal::ParseInt32Flag;
48961be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    using internal::ParseStringFlag;
48971be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
48981be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    // Do we see a Google Test flag?
48991be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    if (ParseBoolFlag(arg, kAlsoRunDisabledTestsFlag,
49001be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania                      &GTEST_FLAG(also_run_disabled_tests)) ||
49011be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania        ParseBoolFlag(arg, kBreakOnFailureFlag,
49021be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania                      &GTEST_FLAG(break_on_failure)) ||
49031be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania        ParseBoolFlag(arg, kCatchExceptionsFlag,
49041be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania                      &GTEST_FLAG(catch_exceptions)) ||
49051be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania        ParseStringFlag(arg, kColorFlag, &GTEST_FLAG(color)) ||
49061be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania        ParseStringFlag(arg, kDeathTestStyleFlag,
49071be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania                        &GTEST_FLAG(death_test_style)) ||
49081be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania        ParseBoolFlag(arg, kDeathTestUseFork,
49091be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania                      &GTEST_FLAG(death_test_use_fork)) ||
49101be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania        ParseStringFlag(arg, kFilterFlag, &GTEST_FLAG(filter)) ||
49111be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania        ParseStringFlag(arg, kInternalRunDeathTestFlag,
49121be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania                        &GTEST_FLAG(internal_run_death_test)) ||
49131be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania        ParseBoolFlag(arg, kListTestsFlag, &GTEST_FLAG(list_tests)) ||
49141be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania        ParseStringFlag(arg, kOutputFlag, &GTEST_FLAG(output)) ||
49151be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania        ParseBoolFlag(arg, kPrintTimeFlag, &GTEST_FLAG(print_time)) ||
491641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot        ParseInt32Flag(arg, kRandomSeedFlag, &GTEST_FLAG(random_seed)) ||
49171be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania        ParseInt32Flag(arg, kRepeatFlag, &GTEST_FLAG(repeat)) ||
491841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot        ParseBoolFlag(arg, kShuffleFlag, &GTEST_FLAG(shuffle)) ||
491941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot        ParseInt32Flag(arg, kStackTraceDepthFlag,
492041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot                       &GTEST_FLAG(stack_trace_depth)) ||
492141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot        ParseStringFlag(arg, kStreamResultToFlag,
492241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot                        &GTEST_FLAG(stream_result_to)) ||
492341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot        ParseBoolFlag(arg, kThrowOnFailureFlag,
492441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot                      &GTEST_FLAG(throw_on_failure))
49251be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania        ) {
49261be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania      // Yes.  Shift the remainder of the argv list left by one.  Note
49271be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania      // that argv has (*argc + 1) elements, the last one always being
49281be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania      // NULL.  The following loop moves the trailing NULL element as
49291be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania      // well.
49301be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania      for (int j = i; j != *argc; j++) {
49311be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania        argv[j] = argv[j + 1];
49321be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania      }
49331be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
49341be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania      // Decrements the argument count.
49351be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania      (*argc)--;
49361be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
49371be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania      // We also need to decrement the iterator as we just removed
49381be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania      // an element.
49391be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania      i--;
49401be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    } else if (arg_string == "--help" || arg_string == "-h" ||
494141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot               arg_string == "-?" || arg_string == "/?" ||
494241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot               HasGoogleTestFlagPrefix(arg)) {
494341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot      // Both help flag and unrecognized Google Test flags (excluding
494441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot      // internal ones) trigger help display.
49451be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania      g_help_flag = true;
49461be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    }
49471be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  }
49481be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
49491be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  if (g_help_flag) {
49501be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    // We print the help here instead of in RUN_ALL_TESTS(), as the
49511be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    // latter may not be called at all if the user is using Google
49521be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    // Test with another testing framework.
49531be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    PrintColorEncoded(kColorEncodedHelpMessage);
49541be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  }
49551be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}
49561be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
49571be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Parses the command line for Google Test flags, without initializing
49581be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// other parts of Google Test.
49591be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniavoid ParseGoogleTestFlagsOnly(int* argc, char** argv) {
49601be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  ParseGoogleTestFlagsOnlyImpl(argc, argv);
49611be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}
49621be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniavoid ParseGoogleTestFlagsOnly(int* argc, wchar_t** argv) {
49631be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  ParseGoogleTestFlagsOnlyImpl(argc, argv);
49641be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}
49651be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
49661be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// The internal implementation of InitGoogleTest().
49671be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//
49681be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// The type parameter CharType can be instantiated to either char or
49691be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// wchar_t.
49701be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniatemplate <typename CharType>
49711be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniavoid InitGoogleTestImpl(int* argc, CharType** argv) {
49721be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  g_init_gtest_count++;
49731be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
49741be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // We don't want to run the initialization code twice.
49751be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  if (g_init_gtest_count != 1) return;
49761be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
49771be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  if (*argc <= 0) return;
49781be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
49791be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  internal::g_executable_path = internal::StreamableToString(argv[0]);
49801be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
49811be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#if GTEST_HAS_DEATH_TEST
498241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot
49831be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  g_argvs.clear();
49841be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  for (int i = 0; i != *argc; i++) {
49851be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    g_argvs.push_back(StreamableToString(argv[i]));
49861be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  }
498741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot
49881be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#endif  // GTEST_HAS_DEATH_TEST
49891be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
49901be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  ParseGoogleTestFlagsOnly(argc, argv);
499141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  GetUnitTestImpl()->PostFlagParsingInit();
49921be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}
49931be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
49941be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}  // namespace internal
49951be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
49961be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Initializes Google Test.  This must be called before calling
49971be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// RUN_ALL_TESTS().  In particular, it parses a command line for the
49981be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// flags that Google Test recognizes.  Whenever a Google Test flag is
49991be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// seen, it is removed from argv, and *argc is decremented.
50001be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//
50011be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// No value is returned.  Instead, the Google Test flag variables are
50021be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// updated.
50031be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//
50041be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Calling the function for the second time has no user-visible effect.
50051be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniavoid InitGoogleTest(int* argc, char** argv) {
50061be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  internal::InitGoogleTestImpl(argc, argv);
50071be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}
50081be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
50091be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// This overloaded version can be used in Windows programs compiled in
50101be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// UNICODE mode.
50111be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniavoid InitGoogleTest(int* argc, wchar_t** argv) {
50121be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  internal::InitGoogleTestImpl(argc, argv);
50131be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}
50141be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
50151be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}  // namespace testing
5016