1f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// Copyright 2005, Google Inc. 2f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// All rights reserved. 3f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// 4f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// Redistribution and use in source and binary forms, with or without 5f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// modification, are permitted provided that the following conditions are 6f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// met: 7f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// 8f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// * Redistributions of source code must retain the above copyright 9f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// notice, this list of conditions and the following disclaimer. 10f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// * Redistributions in binary form must reproduce the above 11f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// copyright notice, this list of conditions and the following disclaimer 12f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// in the documentation and/or other materials provided with the 13f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// distribution. 14f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// * Neither the name of Google Inc. nor the names of its 15f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// contributors may be used to endorse or promote products derived from 16f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// this software without specific prior written permission. 17f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// 18f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 19f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 20f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 21f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 22f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 23f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 24f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 25f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 26f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 28f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// 30f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// Author: wan@google.com (Zhanyong Wan) 31f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// 32f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// The Google C++ Testing Framework (Google Test) 33f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 34f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#include "gtest/gtest.h" 35f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#include "gtest/gtest-spi.h" 36f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 37f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#include <ctype.h> 38f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#include <math.h> 39f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#include <stdarg.h> 40f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#include <stdio.h> 41f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#include <stdlib.h> 42f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#include <wchar.h> 43f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#include <wctype.h> 44f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 45f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#include <algorithm> 46f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#include <ostream> // NOLINT 47f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#include <sstream> 48f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#include <vector> 49f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 50f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#if GTEST_OS_LINUX 51f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 52f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// TODO(kenton@google.com): Use autoconf to detect availability of 53f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// gettimeofday(). 54f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org# define GTEST_HAS_GETTIMEOFDAY_ 1 55f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 56f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org# include <fcntl.h> // NOLINT 57f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org# include <limits.h> // NOLINT 58f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org# include <sched.h> // NOLINT 59f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// Declares vsnprintf(). This header is not available on Windows. 60f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org# include <strings.h> // NOLINT 61f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org# include <sys/mman.h> // NOLINT 62f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org# include <sys/time.h> // NOLINT 63f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org# include <unistd.h> // NOLINT 64f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org# include <string> 65f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 66f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#elif GTEST_OS_SYMBIAN 67f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org# define GTEST_HAS_GETTIMEOFDAY_ 1 68f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org# include <sys/time.h> // NOLINT 69f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 70f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#elif GTEST_OS_ZOS 71f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org# define GTEST_HAS_GETTIMEOFDAY_ 1 72f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org# include <sys/time.h> // NOLINT 73f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 74f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// On z/OS we additionally need strings.h for strcasecmp. 75f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org# include <strings.h> // NOLINT 76f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 77f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#elif GTEST_OS_WINDOWS_MOBILE // We are on Windows CE. 78f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 79f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org# include <windows.h> // NOLINT 80f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 81f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#elif GTEST_OS_WINDOWS // We are on Windows proper. 82f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 83f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org# include <io.h> // NOLINT 84f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org# include <sys/timeb.h> // NOLINT 85f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org# include <sys/types.h> // NOLINT 86f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org# include <sys/stat.h> // NOLINT 87f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 88f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org# if GTEST_OS_WINDOWS_MINGW 89f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// MinGW has gettimeofday() but not _ftime64(). 90f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// TODO(kenton@google.com): Use autoconf to detect availability of 91f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// gettimeofday(). 92f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// TODO(kenton@google.com): There are other ways to get the time on 93f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// Windows, like GetTickCount() or GetSystemTimeAsFileTime(). MinGW 94f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// supports these. consider using them instead. 95f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org# define GTEST_HAS_GETTIMEOFDAY_ 1 96f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org# include <sys/time.h> // NOLINT 97f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org# endif // GTEST_OS_WINDOWS_MINGW 98f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 99f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// cpplint thinks that the header is already included, so we want to 100f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// silence it. 101f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org# include <windows.h> // NOLINT 102f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 103f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#else 104f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 105f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// Assume other platforms have gettimeofday(). 106f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// TODO(kenton@google.com): Use autoconf to detect availability of 107f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// gettimeofday(). 108f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org# define GTEST_HAS_GETTIMEOFDAY_ 1 109f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 110f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// cpplint thinks that the header is already included, so we want to 111f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// silence it. 112f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org# include <sys/time.h> // NOLINT 113f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org# include <unistd.h> // NOLINT 114f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 115f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#endif // GTEST_OS_LINUX 116f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 117f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#if GTEST_HAS_EXCEPTIONS 118f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org# include <stdexcept> 119f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#endif 120f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 121f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#if GTEST_CAN_STREAM_RESULTS_ 122f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org# include <arpa/inet.h> // NOLINT 123f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org# include <netdb.h> // NOLINT 124f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#endif 125f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 126f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// Indicates that this translation unit is part of Google Test's 127f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// implementation. It must come before gtest-internal-inl.h is 128f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// included, or there will be a compiler error. This trick is to 129f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// prevent a user from accidentally including gtest-internal-inl.h in 130f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// his code. 131f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#define GTEST_IMPLEMENTATION_ 1 132f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#include "src/gtest-internal-inl.h" 133f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#undef GTEST_IMPLEMENTATION_ 134f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 135f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#if GTEST_OS_WINDOWS 136f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org# define vsnprintf _vsnprintf 137f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#endif // GTEST_OS_WINDOWS 138f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 139f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgnamespace testing { 140f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 141f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgusing internal::CountIf; 142f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgusing internal::ForEach; 143f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgusing internal::GetElementOr; 144f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgusing internal::Shuffle; 145f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 146f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// Constants. 147f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 148f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// A test whose test case name or test name matches this filter is 149f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// disabled and not run. 150f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgstatic const char kDisableTestFilter[] = "DISABLED_*:*/DISABLED_*"; 151f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 152f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// A test case whose name matches this filter is considered a death 153f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// test case and will be run before test cases whose name doesn't 154f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// match this filter. 155f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgstatic const char kDeathTestCaseFilter[] = "*DeathTest:*DeathTest/*"; 156f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 157f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// A test filter that matches everything. 158f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgstatic const char kUniversalFilter[] = "*"; 159f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 160f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// The default output file for XML output. 161f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgstatic const char kDefaultOutputFile[] = "test_detail.xml"; 162f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 163f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// The environment variable name for the test shard index. 164f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgstatic const char kTestShardIndex[] = "GTEST_SHARD_INDEX"; 165f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// The environment variable name for the total number of test shards. 166f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgstatic const char kTestTotalShards[] = "GTEST_TOTAL_SHARDS"; 167f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// The environment variable name for the test shard status file. 168f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgstatic const char kTestShardStatusFile[] = "GTEST_SHARD_STATUS_FILE"; 169f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 170f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgnamespace internal { 171f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 172f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// The text used in failure messages to indicate the start of the 173f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// stack trace. 174f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgconst char kStackTraceMarker[] = "\nStack trace:\n"; 175f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 176f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// g_help_flag is true iff the --help flag or an equivalent form is 177f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// specified on the command line. 178f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgbool g_help_flag = false; 179f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 180f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org} // namespace internal 181f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 182f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgGTEST_DEFINE_bool_( 183f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org also_run_disabled_tests, 184f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org internal::BoolFromGTestEnv("also_run_disabled_tests", false), 185f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org "Run disabled tests too, in addition to the tests normally being run."); 186f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 187f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgGTEST_DEFINE_bool_( 188f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org break_on_failure, 189f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org internal::BoolFromGTestEnv("break_on_failure", false), 190f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org "True iff a failed assertion should be a debugger break-point."); 191f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 192f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgGTEST_DEFINE_bool_( 193f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org catch_exceptions, 194f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org internal::BoolFromGTestEnv("catch_exceptions", true), 195f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org "True iff " GTEST_NAME_ 196f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org " should catch exceptions and treat them as test failures."); 197f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 198f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgGTEST_DEFINE_string_( 199f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org color, 200f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org internal::StringFromGTestEnv("color", "auto"), 201f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org "Whether to use colors in the output. Valid values: yes, no, " 202f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org "and auto. 'auto' means to use colors if the output is " 203f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org "being sent to a terminal and the TERM environment variable " 204f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org "is set to xterm, xterm-color, xterm-256color, linux or cygwin."); 205f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 206f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgGTEST_DEFINE_string_( 207f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org filter, 208f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org internal::StringFromGTestEnv("filter", kUniversalFilter), 209f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org "A colon-separated list of glob (not regex) patterns " 210f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org "for filtering the tests to run, optionally followed by a " 211f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org "'-' and a : separated list of negative patterns (tests to " 212f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org "exclude). A test is run if it matches one of the positive " 213f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org "patterns and does not match any of the negative patterns."); 214f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 215f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgGTEST_DEFINE_bool_(list_tests, false, 216f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org "List all tests without running them."); 217f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 218f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgGTEST_DEFINE_string_( 219f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org output, 220f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org internal::StringFromGTestEnv("output", ""), 221f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org "A format (currently must be \"xml\"), optionally followed " 222f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org "by a colon and an output file name or directory. A directory " 223f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org "is indicated by a trailing pathname separator. " 224f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org "Examples: \"xml:filename.xml\", \"xml::directoryname/\". " 225f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org "If a directory is specified, output files will be created " 226f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org "within that directory, with file-names based on the test " 227f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org "executable's name and, if necessary, made unique by adding " 228f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org "digits."); 229f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 230f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgGTEST_DEFINE_bool_( 231f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org print_time, 232f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org internal::BoolFromGTestEnv("print_time", true), 233f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org "True iff " GTEST_NAME_ 234f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org " should display elapsed time in text output."); 235f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 236f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgGTEST_DEFINE_int32_( 237f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org random_seed, 238f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org internal::Int32FromGTestEnv("random_seed", 0), 239f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org "Random number seed to use when shuffling test orders. Must be in range " 240f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org "[1, 99999], or 0 to use a seed based on the current time."); 241f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 242f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgGTEST_DEFINE_int32_( 243f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org repeat, 244f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org internal::Int32FromGTestEnv("repeat", 1), 245f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org "How many times to repeat each test. Specify a negative number " 246f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org "for repeating forever. Useful for shaking out flaky tests."); 247f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 248f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgGTEST_DEFINE_bool_( 249f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org show_internal_stack_frames, false, 250f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org "True iff " GTEST_NAME_ " should include internal stack frames when " 251f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org "printing test failure stack traces."); 252f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 253f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgGTEST_DEFINE_bool_( 254f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org shuffle, 255f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org internal::BoolFromGTestEnv("shuffle", false), 256f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org "True iff " GTEST_NAME_ 257f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org " should randomize tests' order on every run."); 258f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 259f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgGTEST_DEFINE_int32_( 260f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org stack_trace_depth, 261f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org internal::Int32FromGTestEnv("stack_trace_depth", kMaxStackTraceDepth), 262f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org "The maximum number of stack frames to print when an " 263f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org "assertion fails. The valid range is 0 through 100, inclusive."); 264f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 265f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgGTEST_DEFINE_string_( 266f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org stream_result_to, 267f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org internal::StringFromGTestEnv("stream_result_to", ""), 268f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org "This flag specifies the host name and the port number on which to stream " 269f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org "test results. Example: \"localhost:555\". The flag is effective only on " 270f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org "Linux."); 271f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 272f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgGTEST_DEFINE_bool_( 273f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org throw_on_failure, 274f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org internal::BoolFromGTestEnv("throw_on_failure", false), 275f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org "When this flag is specified, a failed assertion will throw an exception " 276f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org "if exceptions are enabled or exit the program with a non-zero code " 277f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org "otherwise."); 278f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 279f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgnamespace internal { 280f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 281f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// Generates a random number from [0, range), using a Linear 282f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// Congruential Generator (LCG). Crashes if 'range' is 0 or greater 283f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// than kMaxRange. 284f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgUInt32 Random::Generate(UInt32 range) { 285f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // These constants are the same as are used in glibc's rand(3). 286f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org state_ = (1103515245U*state_ + 12345U) % kMaxRange; 287f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 288f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org GTEST_CHECK_(range > 0) 289f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org << "Cannot generate a number in the range [0, 0)."; 290f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org GTEST_CHECK_(range <= kMaxRange) 291f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org << "Generation of a number in [0, " << range << ") was requested, " 292f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org << "but this can only generate numbers in [0, " << kMaxRange << ")."; 293f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 294f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // Converting via modulus introduces a bit of downward bias, but 295f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // it's simple, and a linear congruential generator isn't too good 296f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // to begin with. 297f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org return state_ % range; 298f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org} 299f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 300f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// GTestIsInitialized() returns true iff the user has initialized 301f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// Google Test. Useful for catching the user mistake of not initializing 302f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// Google Test before calling RUN_ALL_TESTS(). 303f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// 304f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// A user must call testing::InitGoogleTest() to initialize Google 305f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// Test. g_init_gtest_count is set to the number of times 306f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// InitGoogleTest() has been called. We don't protect this variable 307f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// under a mutex as it is only accessed in the main thread. 308f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgint g_init_gtest_count = 0; 309f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgstatic bool GTestIsInitialized() { return g_init_gtest_count != 0; } 310f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 311f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// Iterates over a vector of TestCases, keeping a running sum of the 312f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// results of calling a given int-returning method on each. 313f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// Returns the sum. 314f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgstatic int SumOverTestCaseList(const std::vector<TestCase*>& case_list, 315f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org int (TestCase::*method)() const) { 316f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org int sum = 0; 317f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org for (size_t i = 0; i < case_list.size(); i++) { 318f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org sum += (case_list[i]->*method)(); 319f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org } 320f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org return sum; 321f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org} 322f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 323f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// Returns true iff the test case passed. 324f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgstatic bool TestCasePassed(const TestCase* test_case) { 325f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org return test_case->should_run() && test_case->Passed(); 326f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org} 327f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 328f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// Returns true iff the test case failed. 329f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgstatic bool TestCaseFailed(const TestCase* test_case) { 330f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org return test_case->should_run() && test_case->Failed(); 331f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org} 332f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 333f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// Returns true iff test_case contains at least one test that should 334f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// run. 335f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgstatic bool ShouldRunTestCase(const TestCase* test_case) { 336f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org return test_case->should_run(); 337f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org} 338f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 339f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// AssertHelper constructor. 340f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgAssertHelper::AssertHelper(TestPartResult::Type type, 341f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org const char* file, 342f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org int line, 343f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org const char* message) 344f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org : data_(new AssertHelperData(type, file, line, message)) { 345f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org} 346f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 347f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgAssertHelper::~AssertHelper() { 348f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org delete data_; 349f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org} 350f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 351f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// Message assignment, for assertion streaming support. 352f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgvoid AssertHelper::operator=(const Message& message) const { 353f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org UnitTest::GetInstance()-> 354f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org AddTestPartResult(data_->type, data_->file, data_->line, 355f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org AppendUserMessage(data_->message, message), 356f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org UnitTest::GetInstance()->impl() 357f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org ->CurrentOsStackTraceExceptTop(1) 358f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // Skips the stack frame for this function itself. 359f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org ); // NOLINT 360f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org} 361f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 362f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// Mutex for linked pointers. 363f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgGTEST_DEFINE_STATIC_MUTEX_(g_linked_ptr_mutex); 364f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 365f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// Application pathname gotten in InitGoogleTest. 366f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgString g_executable_path; 367f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 368f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// Returns the current application's name, removing directory path if that 369f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// is present. 370f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgFilePath GetCurrentExecutableName() { 371f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org FilePath result; 372f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 373f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#if GTEST_OS_WINDOWS 374f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org result.Set(FilePath(g_executable_path).RemoveExtension("exe")); 375f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#else 376f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org result.Set(FilePath(g_executable_path)); 377f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#endif // GTEST_OS_WINDOWS 378f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 379f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org return result.RemoveDirectoryName(); 380f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org} 381f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 382f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// Functions for processing the gtest_output flag. 383f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 384f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// Returns the output format, or "" for normal printed output. 385f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgString UnitTestOptions::GetOutputFormat() { 386f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org const char* const gtest_output_flag = GTEST_FLAG(output).c_str(); 387f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org if (gtest_output_flag == NULL) return String(""); 388f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 389f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org const char* const colon = strchr(gtest_output_flag, ':'); 390f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org return (colon == NULL) ? 391f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org String(gtest_output_flag) : 392f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org String(gtest_output_flag, colon - gtest_output_flag); 393f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org} 394f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 395f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// Returns the name of the requested output file, or the default if none 396f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// was explicitly specified. 397f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgString UnitTestOptions::GetAbsolutePathToOutputFile() { 398f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org const char* const gtest_output_flag = GTEST_FLAG(output).c_str(); 399f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org if (gtest_output_flag == NULL) 400f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org return String(""); 401f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 402f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org const char* const colon = strchr(gtest_output_flag, ':'); 403f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org if (colon == NULL) 404f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org return String(internal::FilePath::ConcatPaths( 405f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org internal::FilePath( 406f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org UnitTest::GetInstance()->original_working_dir()), 407f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org internal::FilePath(kDefaultOutputFile)).ToString() ); 408f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 409f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org internal::FilePath output_name(colon + 1); 410f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org if (!output_name.IsAbsolutePath()) 411f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // TODO(wan@google.com): on Windows \some\path is not an absolute 412f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // path (as its meaning depends on the current drive), yet the 413f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // following logic for turning it into an absolute path is wrong. 414f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // Fix it. 415f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org output_name = internal::FilePath::ConcatPaths( 416f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org internal::FilePath(UnitTest::GetInstance()->original_working_dir()), 417f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org internal::FilePath(colon + 1)); 418f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 419f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org if (!output_name.IsDirectory()) 420f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org return output_name.ToString(); 421f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 422f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org internal::FilePath result(internal::FilePath::GenerateUniqueFileName( 423f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org output_name, internal::GetCurrentExecutableName(), 424f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org GetOutputFormat().c_str())); 425f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org return result.ToString(); 426f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org} 427f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 428f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// Returns true iff the wildcard pattern matches the string. The 429f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// first ':' or '\0' character in pattern marks the end of it. 430f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// 431f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// This recursive algorithm isn't very efficient, but is clear and 432f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// works well enough for matching test names, which are short. 433f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgbool UnitTestOptions::PatternMatchesString(const char *pattern, 434f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org const char *str) { 435f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org switch (*pattern) { 436f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org case '\0': 437f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org case ':': // Either ':' or '\0' marks the end of the pattern. 438f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org return *str == '\0'; 439f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org case '?': // Matches any single character. 440f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org return *str != '\0' && PatternMatchesString(pattern + 1, str + 1); 441f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org case '*': // Matches any string (possibly empty) of characters. 442f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org return (*str != '\0' && PatternMatchesString(pattern, str + 1)) || 443f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org PatternMatchesString(pattern + 1, str); 444f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org default: // Non-special character. Matches itself. 445f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org return *pattern == *str && 446f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org PatternMatchesString(pattern + 1, str + 1); 447f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org } 448f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org} 449f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 450f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgbool UnitTestOptions::MatchesFilter(const String& name, const char* filter) { 451f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org const char *cur_pattern = filter; 452f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org for (;;) { 453f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org if (PatternMatchesString(cur_pattern, name.c_str())) { 454f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org return true; 455f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org } 456f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 457f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // Finds the next pattern in the filter. 458f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org cur_pattern = strchr(cur_pattern, ':'); 459f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 460f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // Returns if no more pattern can be found. 461f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org if (cur_pattern == NULL) { 462f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org return false; 463f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org } 464f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 465f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // Skips the pattern separater (the ':' character). 466f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org cur_pattern++; 467f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org } 468f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org} 469f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 470f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// TODO(keithray): move String function implementations to gtest-string.cc. 471f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 472f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// Returns true iff the user-specified filter matches the test case 473f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// name and the test name. 474f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgbool UnitTestOptions::FilterMatchesTest(const String &test_case_name, 475f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org const String &test_name) { 476f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org const String& full_name = String::Format("%s.%s", 477f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org test_case_name.c_str(), 478f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org test_name.c_str()); 479f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 480f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // Split --gtest_filter at '-', if there is one, to separate into 481f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // positive filter and negative filter portions 482f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org const char* const p = GTEST_FLAG(filter).c_str(); 483f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org const char* const dash = strchr(p, '-'); 484f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org String positive; 485f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org String negative; 486f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org if (dash == NULL) { 487f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org positive = GTEST_FLAG(filter).c_str(); // Whole string is a positive filter 488f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org negative = String(""); 489f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org } else { 490f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org positive = String(p, dash - p); // Everything up to the dash 491f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org negative = String(dash+1); // Everything after the dash 492f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org if (positive.empty()) { 493f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // Treat '-test1' as the same as '*-test1' 494f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org positive = kUniversalFilter; 495f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org } 496f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org } 497f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 498f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // A filter is a colon-separated list of patterns. It matches a 499f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // test if any pattern in it matches the test. 500f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org return (MatchesFilter(full_name, positive.c_str()) && 501f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org !MatchesFilter(full_name, negative.c_str())); 502f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org} 503f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 504f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#if GTEST_HAS_SEH 505f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// Returns EXCEPTION_EXECUTE_HANDLER if Google Test should handle the 506f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// given SEH exception, or EXCEPTION_CONTINUE_SEARCH otherwise. 507f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// This function is useful as an __except condition. 508f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgint UnitTestOptions::GTestShouldProcessSEH(DWORD exception_code) { 509f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // Google Test should handle a SEH exception if: 510f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // 1. the user wants it to, AND 511f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // 2. this is not a breakpoint exception, AND 512f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // 3. this is not a C++ exception (VC++ implements them via SEH, 513f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // apparently). 514f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // 515f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // SEH exception code for C++ exceptions. 516f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // (see http://support.microsoft.com/kb/185294 for more information). 517f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org const DWORD kCxxExceptionCode = 0xe06d7363; 518f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 519f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org bool should_handle = true; 520f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 521f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org if (!GTEST_FLAG(catch_exceptions)) 522f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org should_handle = false; 523f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org else if (exception_code == EXCEPTION_BREAKPOINT) 524f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org should_handle = false; 525f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org else if (exception_code == kCxxExceptionCode) 526f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org should_handle = false; 527f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 528f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org return should_handle ? EXCEPTION_EXECUTE_HANDLER : EXCEPTION_CONTINUE_SEARCH; 529f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org} 530f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#endif // GTEST_HAS_SEH 531f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 532f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org} // namespace internal 533f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 534f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// The c'tor sets this object as the test part result reporter used by 535f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// Google Test. The 'result' parameter specifies where to report the 536f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// results. Intercepts only failures from the current thread. 537f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgScopedFakeTestPartResultReporter::ScopedFakeTestPartResultReporter( 538f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org TestPartResultArray* result) 539f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org : intercept_mode_(INTERCEPT_ONLY_CURRENT_THREAD), 540f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org result_(result) { 541f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org Init(); 542f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org} 543f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 544f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// The c'tor sets this object as the test part result reporter used by 545f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// Google Test. The 'result' parameter specifies where to report the 546f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// results. 547f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgScopedFakeTestPartResultReporter::ScopedFakeTestPartResultReporter( 548f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org InterceptMode intercept_mode, TestPartResultArray* result) 549f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org : intercept_mode_(intercept_mode), 550f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org result_(result) { 551f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org Init(); 552f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org} 553f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 554f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgvoid ScopedFakeTestPartResultReporter::Init() { 555f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org internal::UnitTestImpl* const impl = internal::GetUnitTestImpl(); 556f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org if (intercept_mode_ == INTERCEPT_ALL_THREADS) { 557f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org old_reporter_ = impl->GetGlobalTestPartResultReporter(); 558f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org impl->SetGlobalTestPartResultReporter(this); 559f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org } else { 560f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org old_reporter_ = impl->GetTestPartResultReporterForCurrentThread(); 561f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org impl->SetTestPartResultReporterForCurrentThread(this); 562f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org } 563f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org} 564f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 565f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// The d'tor restores the test part result reporter used by Google Test 566f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// before. 567f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgScopedFakeTestPartResultReporter::~ScopedFakeTestPartResultReporter() { 568f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org internal::UnitTestImpl* const impl = internal::GetUnitTestImpl(); 569f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org if (intercept_mode_ == INTERCEPT_ALL_THREADS) { 570f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org impl->SetGlobalTestPartResultReporter(old_reporter_); 571f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org } else { 572f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org impl->SetTestPartResultReporterForCurrentThread(old_reporter_); 573f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org } 574f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org} 575f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 576f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// Increments the test part result count and remembers the result. 577f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// This method is from the TestPartResultReporterInterface interface. 578f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgvoid ScopedFakeTestPartResultReporter::ReportTestPartResult( 579f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org const TestPartResult& result) { 580f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org result_->Append(result); 581f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org} 582f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 583f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgnamespace internal { 584f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 585f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// Returns the type ID of ::testing::Test. We should always call this 586f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// instead of GetTypeId< ::testing::Test>() to get the type ID of 587f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// testing::Test. This is to work around a suspected linker bug when 588f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// using Google Test as a framework on Mac OS X. The bug causes 589f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// GetTypeId< ::testing::Test>() to return different values depending 590f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// on whether the call is from the Google Test framework itself or 591f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// from user test code. GetTestTypeId() is guaranteed to always 592f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// return the same value, as it always calls GetTypeId<>() from the 593f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// gtest.cc, which is within the Google Test framework. 594f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgTypeId GetTestTypeId() { 595f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org return GetTypeId<Test>(); 596f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org} 597f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 598f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// The value of GetTestTypeId() as seen from within the Google Test 599f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// library. This is solely for testing GetTestTypeId(). 600f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgextern const TypeId kTestTypeIdInGoogleTest = GetTestTypeId(); 601f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 602f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// This predicate-formatter checks that 'results' contains a test part 603f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// failure of the given type and that the failure message contains the 604f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// given substring. 605f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgAssertionResult HasOneFailure(const char* /* results_expr */, 606f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org const char* /* type_expr */, 607f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org const char* /* substr_expr */, 608f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org const TestPartResultArray& results, 609f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org TestPartResult::Type type, 610f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org const string& substr) { 611f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org const String expected(type == TestPartResult::kFatalFailure ? 612f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org "1 fatal failure" : 613f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org "1 non-fatal failure"); 614f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org Message msg; 615f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org if (results.size() != 1) { 616f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org msg << "Expected: " << expected << "\n" 617f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org << " Actual: " << results.size() << " failures"; 618f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org for (int i = 0; i < results.size(); i++) { 619f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org msg << "\n" << results.GetTestPartResult(i); 620f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org } 621f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org return AssertionFailure() << msg; 622f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org } 623f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 624f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org const TestPartResult& r = results.GetTestPartResult(0); 625f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org if (r.type() != type) { 626f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org return AssertionFailure() << "Expected: " << expected << "\n" 627f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org << " Actual:\n" 628f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org << r; 629f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org } 630f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 631f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org if (strstr(r.message(), substr.c_str()) == NULL) { 632f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org return AssertionFailure() << "Expected: " << expected << " containing \"" 633f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org << substr << "\"\n" 634f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org << " Actual:\n" 635f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org << r; 636f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org } 637f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 638f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org return AssertionSuccess(); 639f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org} 640f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 641f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// The constructor of SingleFailureChecker remembers where to look up 642f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// test part results, what type of failure we expect, and what 643f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// substring the failure message should contain. 644f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgSingleFailureChecker:: SingleFailureChecker( 645f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org const TestPartResultArray* results, 646f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org TestPartResult::Type type, 647f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org const string& substr) 648f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org : results_(results), 649f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org type_(type), 650f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org substr_(substr) {} 651f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 652f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// The destructor of SingleFailureChecker verifies that the given 653f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// TestPartResultArray contains exactly one failure that has the given 654f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// type and contains the given substring. If that's not the case, a 655f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// non-fatal failure will be generated. 656f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgSingleFailureChecker::~SingleFailureChecker() { 657f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org EXPECT_PRED_FORMAT3(HasOneFailure, *results_, type_, substr_); 658f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org} 659f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 660f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgDefaultGlobalTestPartResultReporter::DefaultGlobalTestPartResultReporter( 661f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org UnitTestImpl* unit_test) : unit_test_(unit_test) {} 662f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 663f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgvoid DefaultGlobalTestPartResultReporter::ReportTestPartResult( 664f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org const TestPartResult& result) { 665f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org unit_test_->current_test_result()->AddTestPartResult(result); 666f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org unit_test_->listeners()->repeater()->OnTestPartResult(result); 667f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org} 668f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 669f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgDefaultPerThreadTestPartResultReporter::DefaultPerThreadTestPartResultReporter( 670f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org UnitTestImpl* unit_test) : unit_test_(unit_test) {} 671f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 672f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgvoid DefaultPerThreadTestPartResultReporter::ReportTestPartResult( 673f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org const TestPartResult& result) { 674f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org unit_test_->GetGlobalTestPartResultReporter()->ReportTestPartResult(result); 675f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org} 676f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 677f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// Returns the global test part result reporter. 678f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgTestPartResultReporterInterface* 679f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgUnitTestImpl::GetGlobalTestPartResultReporter() { 680f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org internal::MutexLock lock(&global_test_part_result_reporter_mutex_); 681f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org return global_test_part_result_repoter_; 682f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org} 683f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 684f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// Sets the global test part result reporter. 685f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgvoid UnitTestImpl::SetGlobalTestPartResultReporter( 686f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org TestPartResultReporterInterface* reporter) { 687f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org internal::MutexLock lock(&global_test_part_result_reporter_mutex_); 688f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org global_test_part_result_repoter_ = reporter; 689f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org} 690f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 691f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// Returns the test part result reporter for the current thread. 692f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgTestPartResultReporterInterface* 693f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgUnitTestImpl::GetTestPartResultReporterForCurrentThread() { 694f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org return per_thread_test_part_result_reporter_.get(); 695f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org} 696f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 697f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// Sets the test part result reporter for the current thread. 698f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgvoid UnitTestImpl::SetTestPartResultReporterForCurrentThread( 699f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org TestPartResultReporterInterface* reporter) { 700f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org per_thread_test_part_result_reporter_.set(reporter); 701f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org} 702f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 703f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// Gets the number of successful test cases. 704f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgint UnitTestImpl::successful_test_case_count() const { 705f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org return CountIf(test_cases_, TestCasePassed); 706f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org} 707f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 708f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// Gets the number of failed test cases. 709f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgint UnitTestImpl::failed_test_case_count() const { 710f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org return CountIf(test_cases_, TestCaseFailed); 711f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org} 712f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 713f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// Gets the number of all test cases. 714f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgint UnitTestImpl::total_test_case_count() const { 715f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org return static_cast<int>(test_cases_.size()); 716f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org} 717f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 718f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// Gets the number of all test cases that contain at least one test 719f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// that should run. 720f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgint UnitTestImpl::test_case_to_run_count() const { 721f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org return CountIf(test_cases_, ShouldRunTestCase); 722f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org} 723f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 724f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// Gets the number of successful tests. 725f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgint UnitTestImpl::successful_test_count() const { 726f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org return SumOverTestCaseList(test_cases_, &TestCase::successful_test_count); 727f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org} 728f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 729f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// Gets the number of failed tests. 730f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgint UnitTestImpl::failed_test_count() const { 731f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org return SumOverTestCaseList(test_cases_, &TestCase::failed_test_count); 732f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org} 733f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 734f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// Gets the number of disabled tests. 735f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgint UnitTestImpl::disabled_test_count() const { 736f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org return SumOverTestCaseList(test_cases_, &TestCase::disabled_test_count); 737f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org} 738f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 739f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// Gets the number of all tests. 740f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgint UnitTestImpl::total_test_count() const { 741f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org return SumOverTestCaseList(test_cases_, &TestCase::total_test_count); 742f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org} 743f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 744f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// Gets the number of tests that should run. 745f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgint UnitTestImpl::test_to_run_count() const { 746f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org return SumOverTestCaseList(test_cases_, &TestCase::test_to_run_count); 747f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org} 748f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 749f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// Returns the current OS stack trace as a String. 750f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// 751f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// The maximum number of stack frames to be included is specified by 752f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// the gtest_stack_trace_depth flag. The skip_count parameter 753f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// specifies the number of top frames to be skipped, which doesn't 754f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// count against the number of frames to be included. 755f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// 756f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// For example, if Foo() calls Bar(), which in turn calls 757f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// CurrentOsStackTraceExceptTop(1), Foo() will be included in the 758f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// trace but Bar() and CurrentOsStackTraceExceptTop() won't. 759f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgString UnitTestImpl::CurrentOsStackTraceExceptTop(int skip_count) { 760f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org (void)skip_count; 761f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org return String(""); 762f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org} 763f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 764f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// Returns the current time in milliseconds. 765f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgTimeInMillis GetTimeInMillis() { 766f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#if GTEST_OS_WINDOWS_MOBILE || defined(__BORLANDC__) 767f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // Difference between 1970-01-01 and 1601-01-01 in milliseconds. 768f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // http://analogous.blogspot.com/2005/04/epoch.html 769f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org const TimeInMillis kJavaEpochToWinFileTimeDelta = 770f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org static_cast<TimeInMillis>(116444736UL) * 100000UL; 771f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org const DWORD kTenthMicrosInMilliSecond = 10000; 772f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 773f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org SYSTEMTIME now_systime; 774f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org FILETIME now_filetime; 775f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org ULARGE_INTEGER now_int64; 776f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // TODO(kenton@google.com): Shouldn't this just use 777f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // GetSystemTimeAsFileTime()? 778f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org GetSystemTime(&now_systime); 779f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org if (SystemTimeToFileTime(&now_systime, &now_filetime)) { 780f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org now_int64.LowPart = now_filetime.dwLowDateTime; 781f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org now_int64.HighPart = now_filetime.dwHighDateTime; 782f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org now_int64.QuadPart = (now_int64.QuadPart / kTenthMicrosInMilliSecond) - 783f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org kJavaEpochToWinFileTimeDelta; 784f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org return now_int64.QuadPart; 785f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org } 786f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org return 0; 787f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#elif GTEST_OS_WINDOWS && !GTEST_HAS_GETTIMEOFDAY_ 788f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org __timeb64 now; 789f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 790f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org# ifdef _MSC_VER 791f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 792f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // MSVC 8 deprecates _ftime64(), so we want to suppress warning 4996 793f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // (deprecated function) there. 794f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // TODO(kenton@google.com): Use GetTickCount()? Or use 795f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // SystemTimeToFileTime() 796f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org# pragma warning(push) // Saves the current warning state. 797f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org# pragma warning(disable:4996) // Temporarily disables warning 4996. 798f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org _ftime64(&now); 799f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org# pragma warning(pop) // Restores the warning state. 800f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org# else 801f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 802f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org _ftime64(&now); 803f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 804f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org# endif // _MSC_VER 805f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 806f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org return static_cast<TimeInMillis>(now.time) * 1000 + now.millitm; 807f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#elif GTEST_HAS_GETTIMEOFDAY_ 808f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org struct timeval now; 809f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org gettimeofday(&now, NULL); 810f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org return static_cast<TimeInMillis>(now.tv_sec) * 1000 + now.tv_usec / 1000; 811f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#else 812f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org# error "Don't know how to get the current time on your system." 813f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#endif 814f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org} 815f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 816f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// Utilities 817f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 818f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// class String 819f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 820f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// Returns the input enclosed in double quotes if it's not NULL; 821f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// otherwise returns "(null)". For example, "\"Hello\"" is returned 822f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// for input "Hello". 823f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// 824f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// This is useful for printing a C string in the syntax of a literal. 825f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// 826f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// Known issue: escape sequences are not handled yet. 827f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgString String::ShowCStringQuoted(const char* c_str) { 828f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org return c_str ? String::Format("\"%s\"", c_str) : String("(null)"); 829f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org} 830f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 831f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// Copies at most length characters from str into a newly-allocated 832f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// piece of memory of size length+1. The memory is allocated with new[]. 833f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// A terminating null byte is written to the memory, and a pointer to it 834f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// is returned. If str is NULL, NULL is returned. 835f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgstatic char* CloneString(const char* str, size_t length) { 836f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org if (str == NULL) { 837f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org return NULL; 838f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org } else { 839f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org char* const clone = new char[length + 1]; 840f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org posix::StrNCpy(clone, str, length); 841f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org clone[length] = '\0'; 842f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org return clone; 843f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org } 844f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org} 845f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 846f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// Clones a 0-terminated C string, allocating memory using new. The 847f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// caller is responsible for deleting[] the return value. Returns the 848f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// cloned string, or NULL if the input is NULL. 849f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgconst char * String::CloneCString(const char* c_str) { 850f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org return (c_str == NULL) ? 851f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org NULL : CloneString(c_str, strlen(c_str)); 852f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org} 853f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 854f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#if GTEST_OS_WINDOWS_MOBILE 855f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// Creates a UTF-16 wide string from the given ANSI string, allocating 856f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// memory using new. The caller is responsible for deleting the return 857f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// value using delete[]. Returns the wide string, or NULL if the 858f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// input is NULL. 859f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgLPCWSTR String::AnsiToUtf16(const char* ansi) { 860f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org if (!ansi) return NULL; 861f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org const int length = strlen(ansi); 862f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org const int unicode_length = 863f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org MultiByteToWideChar(CP_ACP, 0, ansi, length, 864f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org NULL, 0); 865f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org WCHAR* unicode = new WCHAR[unicode_length + 1]; 866f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org MultiByteToWideChar(CP_ACP, 0, ansi, length, 867f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org unicode, unicode_length); 868f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org unicode[unicode_length] = 0; 869f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org return unicode; 870f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org} 871f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 872f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// Creates an ANSI string from the given wide string, allocating 873f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// memory using new. The caller is responsible for deleting the return 874f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// value using delete[]. Returns the ANSI string, or NULL if the 875f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// input is NULL. 876f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgconst char* String::Utf16ToAnsi(LPCWSTR utf16_str) { 877f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org if (!utf16_str) return NULL; 878f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org const int ansi_length = 879f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org WideCharToMultiByte(CP_ACP, 0, utf16_str, -1, 880f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org NULL, 0, NULL, NULL); 881f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org char* ansi = new char[ansi_length + 1]; 882f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org WideCharToMultiByte(CP_ACP, 0, utf16_str, -1, 883f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org ansi, ansi_length, NULL, NULL); 884f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org ansi[ansi_length] = 0; 885f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org return ansi; 886f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org} 887f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 888f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#endif // GTEST_OS_WINDOWS_MOBILE 889f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 890f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// Compares two C strings. Returns true iff they have the same content. 891f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// 892f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// Unlike strcmp(), this function can handle NULL argument(s). A NULL 893f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// C string is considered different to any non-NULL C string, 894f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// including the empty string. 895f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgbool String::CStringEquals(const char * lhs, const char * rhs) { 896f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org if ( lhs == NULL ) return rhs == NULL; 897f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 898f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org if ( rhs == NULL ) return false; 899f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 900f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org return strcmp(lhs, rhs) == 0; 901f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org} 902f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 903f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#if GTEST_HAS_STD_WSTRING || GTEST_HAS_GLOBAL_WSTRING 904f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 905f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// Converts an array of wide chars to a narrow string using the UTF-8 906f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// encoding, and streams the result to the given Message object. 907f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgstatic void StreamWideCharsToMessage(const wchar_t* wstr, size_t length, 908f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org Message* msg) { 909f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // TODO(wan): consider allowing a testing::String object to 910f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // contain '\0'. This will make it behave more like std::string, 911f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // and will allow ToUtf8String() to return the correct encoding 912f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // for '\0' s.t. we can get rid of the conditional here (and in 913f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // several other places). 914f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org for (size_t i = 0; i != length; ) { // NOLINT 915f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org if (wstr[i] != L'\0') { 916f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org *msg << WideStringToUtf8(wstr + i, static_cast<int>(length - i)); 917f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org while (i != length && wstr[i] != L'\0') 918f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org i++; 919f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org } else { 920f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org *msg << '\0'; 921f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org i++; 922f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org } 923f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org } 924f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org} 925f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 926f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#endif // GTEST_HAS_STD_WSTRING || GTEST_HAS_GLOBAL_WSTRING 927f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 928f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org} // namespace internal 929f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 930f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#if GTEST_HAS_STD_WSTRING 931f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// Converts the given wide string to a narrow string using the UTF-8 932f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// encoding, and streams the result to this Message object. 933f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgMessage& Message::operator <<(const ::std::wstring& wstr) { 934f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org internal::StreamWideCharsToMessage(wstr.c_str(), wstr.length(), this); 935f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org return *this; 936f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org} 937f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#endif // GTEST_HAS_STD_WSTRING 938f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 939f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#if GTEST_HAS_GLOBAL_WSTRING 940f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// Converts the given wide string to a narrow string using the UTF-8 941f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// encoding, and streams the result to this Message object. 942f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgMessage& Message::operator <<(const ::wstring& wstr) { 943f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org internal::StreamWideCharsToMessage(wstr.c_str(), wstr.length(), this); 944f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org return *this; 945f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org} 946f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#endif // GTEST_HAS_GLOBAL_WSTRING 947f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 948f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// AssertionResult constructors. 949f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// Used in EXPECT_TRUE/FALSE(assertion_result). 950f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgAssertionResult::AssertionResult(const AssertionResult& other) 951f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org : success_(other.success_), 952f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org message_(other.message_.get() != NULL ? 953f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org new ::std::string(*other.message_) : 954f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org static_cast< ::std::string*>(NULL)) { 955f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org} 956f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 957f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// Returns the assertion's negation. Used with EXPECT/ASSERT_FALSE. 958f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgAssertionResult AssertionResult::operator!() const { 959f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org AssertionResult negation(!success_); 960f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org if (message_.get() != NULL) 961f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org negation << *message_; 962f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org return negation; 963f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org} 964f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 965f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// Makes a successful assertion result. 966f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgAssertionResult AssertionSuccess() { 967f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org return AssertionResult(true); 968f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org} 969f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 970f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// Makes a failed assertion result. 971f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgAssertionResult AssertionFailure() { 972f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org return AssertionResult(false); 973f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org} 974f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 975f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// Makes a failed assertion result with the given failure message. 976f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// Deprecated; use AssertionFailure() << message. 977f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgAssertionResult AssertionFailure(const Message& message) { 978f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org return AssertionFailure() << message; 979f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org} 980f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 981f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgnamespace internal { 982f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 983f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// Constructs and returns the message for an equality assertion 984f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// (e.g. ASSERT_EQ, EXPECT_STREQ, etc) failure. 985f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// 986f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// The first four parameters are the expressions used in the assertion 987f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// and their values, as strings. For example, for ASSERT_EQ(foo, bar) 988f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// where foo is 5 and bar is 6, we have: 989f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// 990f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// expected_expression: "foo" 991f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// actual_expression: "bar" 992f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// expected_value: "5" 993f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// actual_value: "6" 994f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// 995f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// The ignoring_case parameter is true iff the assertion is a 996f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// *_STRCASEEQ*. When it's true, the string " (ignoring case)" will 997f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// be inserted into the message. 998f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgAssertionResult EqFailure(const char* expected_expression, 999f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org const char* actual_expression, 1000f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org const String& expected_value, 1001f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org const String& actual_value, 1002f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org bool ignoring_case) { 1003f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org Message msg; 1004f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org msg << "Value of: " << actual_expression; 1005f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org if (actual_value != actual_expression) { 1006f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org msg << "\n Actual: " << actual_value; 1007f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org } 1008f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 1009f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org msg << "\nExpected: " << expected_expression; 1010f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org if (ignoring_case) { 1011f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org msg << " (ignoring case)"; 1012f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org } 1013f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org if (expected_value != expected_expression) { 1014f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org msg << "\nWhich is: " << expected_value; 1015f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org } 1016f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 1017f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org return AssertionFailure() << msg; 1018f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org} 1019f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 1020f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// Constructs a failure message for Boolean assertions such as EXPECT_TRUE. 1021f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgString GetBoolAssertionFailureMessage(const AssertionResult& assertion_result, 1022f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org const char* expression_text, 1023f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org const char* actual_predicate_value, 1024f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org const char* expected_predicate_value) { 1025f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org const char* actual_message = assertion_result.message(); 1026f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org Message msg; 1027f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org msg << "Value of: " << expression_text 1028f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org << "\n Actual: " << actual_predicate_value; 1029f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org if (actual_message[0] != '\0') 1030f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org msg << " (" << actual_message << ")"; 1031f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org msg << "\nExpected: " << expected_predicate_value; 1032f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org return msg.GetString(); 1033f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org} 1034f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 1035f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// Helper function for implementing ASSERT_NEAR. 1036f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgAssertionResult DoubleNearPredFormat(const char* expr1, 1037f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org const char* expr2, 1038f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org const char* abs_error_expr, 1039f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org double val1, 1040f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org double val2, 1041f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org double abs_error) { 1042f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org const double diff = fabs(val1 - val2); 1043f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org if (diff <= abs_error) return AssertionSuccess(); 1044f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 1045f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // TODO(wan): do not print the value of an expression if it's 1046f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // already a literal. 1047f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org return AssertionFailure() 1048f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org << "The difference between " << expr1 << " and " << expr2 1049f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org << " is " << diff << ", which exceeds " << abs_error_expr << ", where\n" 1050f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org << expr1 << " evaluates to " << val1 << ",\n" 1051f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org << expr2 << " evaluates to " << val2 << ", and\n" 1052f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org << abs_error_expr << " evaluates to " << abs_error << "."; 1053f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org} 1054f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 1055f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 1056f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// Helper template for implementing FloatLE() and DoubleLE(). 1057f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgtemplate <typename RawType> 1058f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgAssertionResult FloatingPointLE(const char* expr1, 1059f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org const char* expr2, 1060f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org RawType val1, 1061f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org RawType val2) { 1062f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // Returns success if val1 is less than val2, 1063f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org if (val1 < val2) { 1064f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org return AssertionSuccess(); 1065f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org } 1066f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 1067f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // or if val1 is almost equal to val2. 1068f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org const FloatingPoint<RawType> lhs(val1), rhs(val2); 1069f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org if (lhs.AlmostEquals(rhs)) { 1070f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org return AssertionSuccess(); 1071f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org } 1072f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 1073f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // Note that the above two checks will both fail if either val1 or 1074f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // val2 is NaN, as the IEEE floating-point standard requires that 1075f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // any predicate involving a NaN must return false. 1076f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 1077f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org ::std::stringstream val1_ss; 1078f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org val1_ss << std::setprecision(std::numeric_limits<RawType>::digits10 + 2) 1079f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org << val1; 1080f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 1081f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org ::std::stringstream val2_ss; 1082f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org val2_ss << std::setprecision(std::numeric_limits<RawType>::digits10 + 2) 1083f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org << val2; 1084f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 1085f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org return AssertionFailure() 1086f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org << "Expected: (" << expr1 << ") <= (" << expr2 << ")\n" 1087f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org << " Actual: " << StringStreamToString(&val1_ss) << " vs " 1088f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org << StringStreamToString(&val2_ss); 1089f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org} 1090f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 1091f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org} // namespace internal 1092f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 1093f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// Asserts that val1 is less than, or almost equal to, val2. Fails 1094f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// otherwise. In particular, it fails if either val1 or val2 is NaN. 1095f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgAssertionResult FloatLE(const char* expr1, const char* expr2, 1096f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org float val1, float val2) { 1097f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org return internal::FloatingPointLE<float>(expr1, expr2, val1, val2); 1098f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org} 1099f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 1100f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// Asserts that val1 is less than, or almost equal to, val2. Fails 1101f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// otherwise. In particular, it fails if either val1 or val2 is NaN. 1102f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgAssertionResult DoubleLE(const char* expr1, const char* expr2, 1103f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org double val1, double val2) { 1104f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org return internal::FloatingPointLE<double>(expr1, expr2, val1, val2); 1105f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org} 1106f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 1107f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgnamespace internal { 1108f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 1109f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// The helper function for {ASSERT|EXPECT}_EQ with int or enum 1110f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// arguments. 1111f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgAssertionResult CmpHelperEQ(const char* expected_expression, 1112f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org const char* actual_expression, 1113f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org BiggestInt expected, 1114f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org BiggestInt actual) { 1115f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org if (expected == actual) { 1116f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org return AssertionSuccess(); 1117f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org } 1118f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 1119f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org return EqFailure(expected_expression, 1120f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org actual_expression, 1121f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org FormatForComparisonFailureMessage(expected, actual), 1122f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org FormatForComparisonFailureMessage(actual, expected), 1123f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org false); 1124f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org} 1125f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 1126f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// A macro for implementing the helper functions needed to implement 1127f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// ASSERT_?? and EXPECT_?? with integer or enum arguments. It is here 1128f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// just to avoid copy-and-paste of similar code. 1129f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#define GTEST_IMPL_CMP_HELPER_(op_name, op)\ 1130f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgAssertionResult CmpHelper##op_name(const char* expr1, const char* expr2, \ 1131f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org BiggestInt val1, BiggestInt val2) {\ 1132f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org if (val1 op val2) {\ 1133f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org return AssertionSuccess();\ 1134f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org } else {\ 1135f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org return AssertionFailure() \ 1136f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org << "Expected: (" << expr1 << ") " #op " (" << expr2\ 1137f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org << "), actual: " << FormatForComparisonFailureMessage(val1, val2)\ 1138f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org << " vs " << FormatForComparisonFailureMessage(val2, val1);\ 1139f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org }\ 1140f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org} 1141f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 1142f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// Implements the helper function for {ASSERT|EXPECT}_NE with int or 1143f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// enum arguments. 1144f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgGTEST_IMPL_CMP_HELPER_(NE, !=) 1145f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// Implements the helper function for {ASSERT|EXPECT}_LE with int or 1146f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// enum arguments. 1147f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgGTEST_IMPL_CMP_HELPER_(LE, <=) 1148f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// Implements the helper function for {ASSERT|EXPECT}_LT with int or 1149f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// enum arguments. 1150f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgGTEST_IMPL_CMP_HELPER_(LT, < ) 1151f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// Implements the helper function for {ASSERT|EXPECT}_GE with int or 1152f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// enum arguments. 1153f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgGTEST_IMPL_CMP_HELPER_(GE, >=) 1154f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// Implements the helper function for {ASSERT|EXPECT}_GT with int or 1155f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// enum arguments. 1156f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgGTEST_IMPL_CMP_HELPER_(GT, > ) 1157f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 1158f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#undef GTEST_IMPL_CMP_HELPER_ 1159f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 1160f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// The helper function for {ASSERT|EXPECT}_STREQ. 1161f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgAssertionResult CmpHelperSTREQ(const char* expected_expression, 1162f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org const char* actual_expression, 1163f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org const char* expected, 1164f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org const char* actual) { 1165f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org if (String::CStringEquals(expected, actual)) { 1166f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org return AssertionSuccess(); 1167f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org } 1168f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 1169f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org return EqFailure(expected_expression, 1170f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org actual_expression, 1171f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org String::ShowCStringQuoted(expected), 1172f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org String::ShowCStringQuoted(actual), 1173f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org false); 1174f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org} 1175f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 1176f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// The helper function for {ASSERT|EXPECT}_STRCASEEQ. 1177f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgAssertionResult CmpHelperSTRCASEEQ(const char* expected_expression, 1178f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org const char* actual_expression, 1179f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org const char* expected, 1180f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org const char* actual) { 1181f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org if (String::CaseInsensitiveCStringEquals(expected, actual)) { 1182f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org return AssertionSuccess(); 1183f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org } 1184f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 1185f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org return EqFailure(expected_expression, 1186f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org actual_expression, 1187f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org String::ShowCStringQuoted(expected), 1188f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org String::ShowCStringQuoted(actual), 1189f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org true); 1190f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org} 1191f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 1192f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// The helper function for {ASSERT|EXPECT}_STRNE. 1193f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgAssertionResult CmpHelperSTRNE(const char* s1_expression, 1194f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org const char* s2_expression, 1195f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org const char* s1, 1196f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org const char* s2) { 1197f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org if (!String::CStringEquals(s1, s2)) { 1198f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org return AssertionSuccess(); 1199f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org } else { 1200f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org return AssertionFailure() << "Expected: (" << s1_expression << ") != (" 1201f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org << s2_expression << "), actual: \"" 1202f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org << s1 << "\" vs \"" << s2 << "\""; 1203f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org } 1204f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org} 1205f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 1206f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// The helper function for {ASSERT|EXPECT}_STRCASENE. 1207f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgAssertionResult CmpHelperSTRCASENE(const char* s1_expression, 1208f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org const char* s2_expression, 1209f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org const char* s1, 1210f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org const char* s2) { 1211f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org if (!String::CaseInsensitiveCStringEquals(s1, s2)) { 1212f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org return AssertionSuccess(); 1213f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org } else { 1214f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org return AssertionFailure() 1215f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org << "Expected: (" << s1_expression << ") != (" 1216f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org << s2_expression << ") (ignoring case), actual: \"" 1217f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org << s1 << "\" vs \"" << s2 << "\""; 1218f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org } 1219f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org} 1220f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 1221f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org} // namespace internal 1222f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 1223f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgnamespace { 1224f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 1225f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// Helper functions for implementing IsSubString() and IsNotSubstring(). 1226f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 1227f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// This group of overloaded functions return true iff needle is a 1228f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// substring of haystack. NULL is considered a substring of itself 1229f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// only. 1230f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 1231f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgbool IsSubstringPred(const char* needle, const char* haystack) { 1232f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org if (needle == NULL || haystack == NULL) 1233f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org return needle == haystack; 1234f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 1235f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org return strstr(haystack, needle) != NULL; 1236f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org} 1237f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 1238f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgbool IsSubstringPred(const wchar_t* needle, const wchar_t* haystack) { 1239f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org if (needle == NULL || haystack == NULL) 1240f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org return needle == haystack; 1241f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 1242f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org return wcsstr(haystack, needle) != NULL; 1243f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org} 1244f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 1245f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// StringType here can be either ::std::string or ::std::wstring. 1246f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgtemplate <typename StringType> 1247f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgbool IsSubstringPred(const StringType& needle, 1248f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org const StringType& haystack) { 1249f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org return haystack.find(needle) != StringType::npos; 1250f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org} 1251f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 1252f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// This function implements either IsSubstring() or IsNotSubstring(), 1253f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// depending on the value of the expected_to_be_substring parameter. 1254f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// StringType here can be const char*, const wchar_t*, ::std::string, 1255f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// or ::std::wstring. 1256f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgtemplate <typename StringType> 1257f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgAssertionResult IsSubstringImpl( 1258f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org bool expected_to_be_substring, 1259f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org const char* needle_expr, const char* haystack_expr, 1260f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org const StringType& needle, const StringType& haystack) { 1261f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org if (IsSubstringPred(needle, haystack) == expected_to_be_substring) 1262f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org return AssertionSuccess(); 1263f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 1264f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org const bool is_wide_string = sizeof(needle[0]) > 1; 1265f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org const char* const begin_string_quote = is_wide_string ? "L\"" : "\""; 1266f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org return AssertionFailure() 1267f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org << "Value of: " << needle_expr << "\n" 1268f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org << " Actual: " << begin_string_quote << needle << "\"\n" 1269f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org << "Expected: " << (expected_to_be_substring ? "" : "not ") 1270f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org << "a substring of " << haystack_expr << "\n" 1271f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org << "Which is: " << begin_string_quote << haystack << "\""; 1272f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org} 1273f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 1274f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org} // namespace 1275f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 1276f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// IsSubstring() and IsNotSubstring() check whether needle is a 1277f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// substring of haystack (NULL is considered a substring of itself 1278f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// only), and return an appropriate error message when they fail. 1279f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 1280f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgAssertionResult IsSubstring( 1281f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org const char* needle_expr, const char* haystack_expr, 1282f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org const char* needle, const char* haystack) { 1283f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org return IsSubstringImpl(true, needle_expr, haystack_expr, needle, haystack); 1284f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org} 1285f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 1286f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgAssertionResult IsSubstring( 1287f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org const char* needle_expr, const char* haystack_expr, 1288f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org const wchar_t* needle, const wchar_t* haystack) { 1289f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org return IsSubstringImpl(true, needle_expr, haystack_expr, needle, haystack); 1290f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org} 1291f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 1292f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgAssertionResult IsNotSubstring( 1293f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org const char* needle_expr, const char* haystack_expr, 1294f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org const char* needle, const char* haystack) { 1295f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org return IsSubstringImpl(false, needle_expr, haystack_expr, needle, haystack); 1296f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org} 1297f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 1298f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgAssertionResult IsNotSubstring( 1299f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org const char* needle_expr, const char* haystack_expr, 1300f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org const wchar_t* needle, const wchar_t* haystack) { 1301f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org return IsSubstringImpl(false, needle_expr, haystack_expr, needle, haystack); 1302f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org} 1303f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 1304f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgAssertionResult IsSubstring( 1305f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org const char* needle_expr, const char* haystack_expr, 1306f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org const ::std::string& needle, const ::std::string& haystack) { 1307f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org return IsSubstringImpl(true, needle_expr, haystack_expr, needle, haystack); 1308f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org} 1309f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 1310f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgAssertionResult IsNotSubstring( 1311f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org const char* needle_expr, const char* haystack_expr, 1312f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org const ::std::string& needle, const ::std::string& haystack) { 1313f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org return IsSubstringImpl(false, needle_expr, haystack_expr, needle, haystack); 1314f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org} 1315f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 1316f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#if GTEST_HAS_STD_WSTRING 1317f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgAssertionResult IsSubstring( 1318f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org const char* needle_expr, const char* haystack_expr, 1319f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org const ::std::wstring& needle, const ::std::wstring& haystack) { 1320f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org return IsSubstringImpl(true, needle_expr, haystack_expr, needle, haystack); 1321f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org} 1322f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 1323f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgAssertionResult IsNotSubstring( 1324f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org const char* needle_expr, const char* haystack_expr, 1325f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org const ::std::wstring& needle, const ::std::wstring& haystack) { 1326f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org return IsSubstringImpl(false, needle_expr, haystack_expr, needle, haystack); 1327f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org} 1328f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#endif // GTEST_HAS_STD_WSTRING 1329f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 1330f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgnamespace internal { 1331f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 1332f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#if GTEST_OS_WINDOWS 1333f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 1334f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgnamespace { 1335f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 1336f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// Helper function for IsHRESULT{SuccessFailure} predicates 1337f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgAssertionResult HRESULTFailureHelper(const char* expr, 1338f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org const char* expected, 1339f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org long hr) { // NOLINT 1340f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org# if GTEST_OS_WINDOWS_MOBILE 1341f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 1342f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // Windows CE doesn't support FormatMessage. 1343f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org const char error_text[] = ""; 1344f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 1345f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org# else 1346f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 1347f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // Looks up the human-readable system message for the HRESULT code 1348f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // and since we're not passing any params to FormatMessage, we don't 1349f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // want inserts expanded. 1350f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org const DWORD kFlags = FORMAT_MESSAGE_FROM_SYSTEM | 1351f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org FORMAT_MESSAGE_IGNORE_INSERTS; 1352f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org const DWORD kBufSize = 4096; // String::Format can't exceed this length. 1353f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // Gets the system's human readable message string for this HRESULT. 1354f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org char error_text[kBufSize] = { '\0' }; 1355f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org DWORD message_length = ::FormatMessageA(kFlags, 1356f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 0, // no source, we're asking system 1357f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org hr, // the error 1358f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 0, // no line width restrictions 1359f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org error_text, // output buffer 1360f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org kBufSize, // buf size 1361f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org NULL); // no arguments for inserts 1362f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // Trims tailing white space (FormatMessage leaves a trailing cr-lf) 1363f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org for (; message_length && IsSpace(error_text[message_length - 1]); 1364f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org --message_length) { 1365f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org error_text[message_length - 1] = '\0'; 1366f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org } 1367f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 1368f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org# endif // GTEST_OS_WINDOWS_MOBILE 1369f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 1370f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org const String error_hex(String::Format("0x%08X ", hr)); 1371f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org return ::testing::AssertionFailure() 1372f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org << "Expected: " << expr << " " << expected << ".\n" 1373f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org << " Actual: " << error_hex << error_text << "\n"; 1374f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org} 1375f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 1376f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org} // namespace 1377f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 1378f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgAssertionResult IsHRESULTSuccess(const char* expr, long hr) { // NOLINT 1379f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org if (SUCCEEDED(hr)) { 1380f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org return AssertionSuccess(); 1381f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org } 1382f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org return HRESULTFailureHelper(expr, "succeeds", hr); 1383f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org} 1384f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 1385f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgAssertionResult IsHRESULTFailure(const char* expr, long hr) { // NOLINT 1386f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org if (FAILED(hr)) { 1387f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org return AssertionSuccess(); 1388f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org } 1389f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org return HRESULTFailureHelper(expr, "fails", hr); 1390f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org} 1391f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 1392f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#endif // GTEST_OS_WINDOWS 1393f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 1394f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// Utility functions for encoding Unicode text (wide strings) in 1395f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// UTF-8. 1396f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 1397f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// A Unicode code-point can have upto 21 bits, and is encoded in UTF-8 1398f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// like this: 1399f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// 1400f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// Code-point length Encoding 1401f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// 0 - 7 bits 0xxxxxxx 1402f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// 8 - 11 bits 110xxxxx 10xxxxxx 1403f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// 12 - 16 bits 1110xxxx 10xxxxxx 10xxxxxx 1404f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// 17 - 21 bits 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx 1405f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 1406f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// The maximum code-point a one-byte UTF-8 sequence can represent. 1407f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgconst UInt32 kMaxCodePoint1 = (static_cast<UInt32>(1) << 7) - 1; 1408f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 1409f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// The maximum code-point a two-byte UTF-8 sequence can represent. 1410f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgconst UInt32 kMaxCodePoint2 = (static_cast<UInt32>(1) << (5 + 6)) - 1; 1411f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 1412f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// The maximum code-point a three-byte UTF-8 sequence can represent. 1413f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgconst UInt32 kMaxCodePoint3 = (static_cast<UInt32>(1) << (4 + 2*6)) - 1; 1414f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 1415f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// The maximum code-point a four-byte UTF-8 sequence can represent. 1416f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgconst UInt32 kMaxCodePoint4 = (static_cast<UInt32>(1) << (3 + 3*6)) - 1; 1417f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 1418f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// Chops off the n lowest bits from a bit pattern. Returns the n 1419f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// lowest bits. As a side effect, the original bit pattern will be 1420f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// shifted to the right by n bits. 1421f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orginline UInt32 ChopLowBits(UInt32* bits, int n) { 1422f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org const UInt32 low_bits = *bits & ((static_cast<UInt32>(1) << n) - 1); 1423f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org *bits >>= n; 1424f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org return low_bits; 1425f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org} 1426f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 1427f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// Converts a Unicode code point to a narrow string in UTF-8 encoding. 1428f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// code_point parameter is of type UInt32 because wchar_t may not be 1429f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// wide enough to contain a code point. 1430f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// The output buffer str must containt at least 32 characters. 1431f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// The function returns the address of the output buffer. 1432f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// If the code_point is not a valid Unicode code point 1433f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// (i.e. outside of Unicode range U+0 to U+10FFFF) it will be output 1434f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// as '(Invalid Unicode 0xXXXXXXXX)'. 1435f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgchar* CodePointToUtf8(UInt32 code_point, char* str) { 1436f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org if (code_point <= kMaxCodePoint1) { 1437f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org str[1] = '\0'; 1438f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org str[0] = static_cast<char>(code_point); // 0xxxxxxx 1439f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org } else if (code_point <= kMaxCodePoint2) { 1440f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org str[2] = '\0'; 1441f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org str[1] = static_cast<char>(0x80 | ChopLowBits(&code_point, 6)); // 10xxxxxx 1442f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org str[0] = static_cast<char>(0xC0 | code_point); // 110xxxxx 1443f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org } else if (code_point <= kMaxCodePoint3) { 1444f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org str[3] = '\0'; 1445f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org str[2] = static_cast<char>(0x80 | ChopLowBits(&code_point, 6)); // 10xxxxxx 1446f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org str[1] = static_cast<char>(0x80 | ChopLowBits(&code_point, 6)); // 10xxxxxx 1447f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org str[0] = static_cast<char>(0xE0 | code_point); // 1110xxxx 1448f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org } else if (code_point <= kMaxCodePoint4) { 1449f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org str[4] = '\0'; 1450f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org str[3] = static_cast<char>(0x80 | ChopLowBits(&code_point, 6)); // 10xxxxxx 1451f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org str[2] = static_cast<char>(0x80 | ChopLowBits(&code_point, 6)); // 10xxxxxx 1452f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org str[1] = static_cast<char>(0x80 | ChopLowBits(&code_point, 6)); // 10xxxxxx 1453f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org str[0] = static_cast<char>(0xF0 | code_point); // 11110xxx 1454f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org } else { 1455f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // The longest string String::Format can produce when invoked 1456f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // with these parameters is 28 character long (not including 1457f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // the terminating nul character). We are asking for 32 character 1458f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // buffer just in case. This is also enough for strncpy to 1459f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // null-terminate the destination string. 1460f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org posix::StrNCpy( 1461f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org str, String::Format("(Invalid Unicode 0x%X)", code_point).c_str(), 32); 1462f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org str[31] = '\0'; // Makes sure no change in the format to strncpy leaves 1463f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // the result unterminated. 1464f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org } 1465f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org return str; 1466f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org} 1467f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 1468f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// The following two functions only make sense if the the system 1469f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// uses UTF-16 for wide string encoding. All supported systems 1470f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// with 16 bit wchar_t (Windows, Cygwin, Symbian OS) do use UTF-16. 1471f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 1472f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// Determines if the arguments constitute UTF-16 surrogate pair 1473f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// and thus should be combined into a single Unicode code point 1474f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// using CreateCodePointFromUtf16SurrogatePair. 1475f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orginline bool IsUtf16SurrogatePair(wchar_t first, wchar_t second) { 1476f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org return sizeof(wchar_t) == 2 && 1477f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org (first & 0xFC00) == 0xD800 && (second & 0xFC00) == 0xDC00; 1478f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org} 1479f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 1480f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// Creates a Unicode code point from UTF16 surrogate pair. 1481f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orginline UInt32 CreateCodePointFromUtf16SurrogatePair(wchar_t first, 1482f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org wchar_t second) { 1483f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org const UInt32 mask = (1 << 10) - 1; 1484f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org return (sizeof(wchar_t) == 2) ? 1485f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org (((first & mask) << 10) | (second & mask)) + 0x10000 : 1486f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // This function should not be called when the condition is 1487f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // false, but we provide a sensible default in case it is. 1488f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org static_cast<UInt32>(first); 1489f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org} 1490f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 1491f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// Converts a wide string to a narrow string in UTF-8 encoding. 1492f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// The wide string is assumed to have the following encoding: 1493f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// UTF-16 if sizeof(wchar_t) == 2 (on Windows, Cygwin, Symbian OS) 1494f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// UTF-32 if sizeof(wchar_t) == 4 (on Linux) 1495f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// Parameter str points to a null-terminated wide string. 1496f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// Parameter num_chars may additionally limit the number 1497f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// of wchar_t characters processed. -1 is used when the entire string 1498f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// should be processed. 1499f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// If the string contains code points that are not valid Unicode code points 1500f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// (i.e. outside of Unicode range U+0 to U+10FFFF) they will be output 1501f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// as '(Invalid Unicode 0xXXXXXXXX)'. If the string is in UTF16 encoding 1502f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// and contains invalid UTF-16 surrogate pairs, values in those pairs 1503f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// will be encoded as individual Unicode characters from Basic Normal Plane. 1504f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgString WideStringToUtf8(const wchar_t* str, int num_chars) { 1505f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org if (num_chars == -1) 1506f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org num_chars = static_cast<int>(wcslen(str)); 1507f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 1508f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org ::std::stringstream stream; 1509f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org for (int i = 0; i < num_chars; ++i) { 1510f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org UInt32 unicode_code_point; 1511f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 1512f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org if (str[i] == L'\0') { 1513f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org break; 1514f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org } else if (i + 1 < num_chars && IsUtf16SurrogatePair(str[i], str[i + 1])) { 1515f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org unicode_code_point = CreateCodePointFromUtf16SurrogatePair(str[i], 1516f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org str[i + 1]); 1517f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org i++; 1518f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org } else { 1519f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org unicode_code_point = static_cast<UInt32>(str[i]); 1520f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org } 1521f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 1522f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org char buffer[32]; // CodePointToUtf8 requires a buffer this big. 1523f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org stream << CodePointToUtf8(unicode_code_point, buffer); 1524f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org } 1525f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org return StringStreamToString(&stream); 1526f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org} 1527f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 1528f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// Converts a wide C string to a String using the UTF-8 encoding. 1529f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// NULL will be converted to "(null)". 1530f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgString String::ShowWideCString(const wchar_t * wide_c_str) { 1531f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org if (wide_c_str == NULL) return String("(null)"); 1532f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 1533f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org return String(internal::WideStringToUtf8(wide_c_str, -1).c_str()); 1534f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org} 1535f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 1536f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// Similar to ShowWideCString(), except that this function encloses 1537f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// the converted string in double quotes. 1538f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgString String::ShowWideCStringQuoted(const wchar_t* wide_c_str) { 1539f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org if (wide_c_str == NULL) return String("(null)"); 1540f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 1541f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org return String::Format("L\"%s\"", 1542f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org String::ShowWideCString(wide_c_str).c_str()); 1543f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org} 1544f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 1545f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// Compares two wide C strings. Returns true iff they have the same 1546f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// content. 1547f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// 1548f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// Unlike wcscmp(), this function can handle NULL argument(s). A NULL 1549f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// C string is considered different to any non-NULL C string, 1550f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// including the empty string. 1551f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgbool String::WideCStringEquals(const wchar_t * lhs, const wchar_t * rhs) { 1552f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org if (lhs == NULL) return rhs == NULL; 1553f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 1554f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org if (rhs == NULL) return false; 1555f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 1556f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org return wcscmp(lhs, rhs) == 0; 1557f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org} 1558f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 1559f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// Helper function for *_STREQ on wide strings. 1560f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgAssertionResult CmpHelperSTREQ(const char* expected_expression, 1561f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org const char* actual_expression, 1562f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org const wchar_t* expected, 1563f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org const wchar_t* actual) { 1564f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org if (String::WideCStringEquals(expected, actual)) { 1565f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org return AssertionSuccess(); 1566f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org } 1567f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 1568f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org return EqFailure(expected_expression, 1569f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org actual_expression, 1570f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org String::ShowWideCStringQuoted(expected), 1571f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org String::ShowWideCStringQuoted(actual), 1572f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org false); 1573f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org} 1574f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 1575f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// Helper function for *_STRNE on wide strings. 1576f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgAssertionResult CmpHelperSTRNE(const char* s1_expression, 1577f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org const char* s2_expression, 1578f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org const wchar_t* s1, 1579f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org const wchar_t* s2) { 1580f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org if (!String::WideCStringEquals(s1, s2)) { 1581f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org return AssertionSuccess(); 1582f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org } 1583f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 1584f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org return AssertionFailure() << "Expected: (" << s1_expression << ") != (" 1585f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org << s2_expression << "), actual: " 1586f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org << String::ShowWideCStringQuoted(s1) 1587f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org << " vs " << String::ShowWideCStringQuoted(s2); 1588f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org} 1589f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 1590f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// Compares two C strings, ignoring case. Returns true iff they have 1591f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// the same content. 1592f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// 1593f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// Unlike strcasecmp(), this function can handle NULL argument(s). A 1594f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// NULL C string is considered different to any non-NULL C string, 1595f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// including the empty string. 1596f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgbool String::CaseInsensitiveCStringEquals(const char * lhs, const char * rhs) { 1597f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org if (lhs == NULL) 1598f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org return rhs == NULL; 1599f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org if (rhs == NULL) 1600f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org return false; 1601f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org return posix::StrCaseCmp(lhs, rhs) == 0; 1602f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org} 1603f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 1604f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // Compares two wide C strings, ignoring case. Returns true iff they 1605f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // have the same content. 1606f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // 1607f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // Unlike wcscasecmp(), this function can handle NULL argument(s). 1608f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // A NULL C string is considered different to any non-NULL wide C string, 1609f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // including the empty string. 1610f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // NB: The implementations on different platforms slightly differ. 1611f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // On windows, this method uses _wcsicmp which compares according to LC_CTYPE 1612f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // environment variable. On GNU platform this method uses wcscasecmp 1613f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // which compares according to LC_CTYPE category of the current locale. 1614f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // On MacOS X, it uses towlower, which also uses LC_CTYPE category of the 1615f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // current locale. 1616f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgbool String::CaseInsensitiveWideCStringEquals(const wchar_t* lhs, 1617f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org const wchar_t* rhs) { 1618f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org if (lhs == NULL) return rhs == NULL; 1619f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 1620f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org if (rhs == NULL) return false; 1621f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 1622f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#if GTEST_OS_WINDOWS 1623f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org return _wcsicmp(lhs, rhs) == 0; 1624f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#elif GTEST_OS_LINUX && !GTEST_OS_LINUX_ANDROID 1625f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org return wcscasecmp(lhs, rhs) == 0; 1626f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#else 1627f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // Android, Mac OS X and Cygwin don't define wcscasecmp. 1628f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // Other unknown OSes may not define it either. 1629f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org wint_t left, right; 1630f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org do { 1631f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org left = towlower(*lhs++); 1632f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org right = towlower(*rhs++); 1633f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org } while (left && left == right); 1634f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org return left == right; 1635f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#endif // OS selector 1636f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org} 1637f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 1638f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// Compares this with another String. 1639f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// Returns < 0 if this is less than rhs, 0 if this is equal to rhs, or > 0 1640f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// if this is greater than rhs. 1641f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgint String::Compare(const String & rhs) const { 1642f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org const char* const lhs_c_str = c_str(); 1643f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org const char* const rhs_c_str = rhs.c_str(); 1644f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 1645f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org if (lhs_c_str == NULL) { 1646f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org return rhs_c_str == NULL ? 0 : -1; // NULL < anything except NULL 1647f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org } else if (rhs_c_str == NULL) { 1648f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org return 1; 1649f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org } 1650f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 1651f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org const size_t shorter_str_len = 1652f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org length() <= rhs.length() ? length() : rhs.length(); 1653f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org for (size_t i = 0; i != shorter_str_len; i++) { 1654f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org if (lhs_c_str[i] < rhs_c_str[i]) { 1655f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org return -1; 1656f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org } else if (lhs_c_str[i] > rhs_c_str[i]) { 1657f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org return 1; 1658f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org } 1659f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org } 1660f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org return (length() < rhs.length()) ? -1 : 1661f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org (length() > rhs.length()) ? 1 : 0; 1662f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org} 1663f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 1664f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// Returns true iff this String ends with the given suffix. *Any* 1665f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// String is considered to end with a NULL or empty suffix. 1666f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgbool String::EndsWith(const char* suffix) const { 1667f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org if (suffix == NULL || CStringEquals(suffix, "")) return true; 1668f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 1669f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org if (c_str() == NULL) return false; 1670f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 1671f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org const size_t this_len = strlen(c_str()); 1672f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org const size_t suffix_len = strlen(suffix); 1673f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org return (this_len >= suffix_len) && 1674f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org CStringEquals(c_str() + this_len - suffix_len, suffix); 1675f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org} 1676f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 1677f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// Returns true iff this String ends with the given suffix, ignoring case. 1678f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// Any String is considered to end with a NULL or empty suffix. 1679f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgbool String::EndsWithCaseInsensitive(const char* suffix) const { 1680f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org if (suffix == NULL || CStringEquals(suffix, "")) return true; 1681f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 1682f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org if (c_str() == NULL) return false; 1683f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 1684f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org const size_t this_len = strlen(c_str()); 1685f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org const size_t suffix_len = strlen(suffix); 1686f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org return (this_len >= suffix_len) && 1687f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org CaseInsensitiveCStringEquals(c_str() + this_len - suffix_len, suffix); 1688f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org} 1689f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 1690f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// Formats a list of arguments to a String, using the same format 1691f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// spec string as for printf. 1692f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// 1693f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// We do not use the StringPrintf class as it is not universally 1694f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// available. 1695f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// 1696f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// The result is limited to 4096 characters (including the tailing 0). 1697f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// If 4096 characters are not enough to format the input, or if 1698f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// there's an error, "<formatting error or buffer exceeded>" is 1699f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// returned. 1700f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgString String::Format(const char * format, ...) { 1701f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org va_list args; 1702f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org va_start(args, format); 1703f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 1704f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org char buffer[4096]; 1705f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org const int kBufferSize = sizeof(buffer)/sizeof(buffer[0]); 1706f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 1707f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // MSVC 8 deprecates vsnprintf(), so we want to suppress warning 1708f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // 4996 (deprecated function) there. 1709f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#ifdef _MSC_VER // We are using MSVC. 1710f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org# pragma warning(push) // Saves the current warning state. 1711f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org# pragma warning(disable:4996) // Temporarily disables warning 4996. 1712f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 1713f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org const int size = vsnprintf(buffer, kBufferSize, format, args); 1714f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 1715f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org# pragma warning(pop) // Restores the warning state. 1716f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#else // We are not using MSVC. 1717f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org const int size = vsnprintf(buffer, kBufferSize, format, args); 1718f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#endif // _MSC_VER 1719f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org va_end(args); 1720f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 1721f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // vsnprintf()'s behavior is not portable. When the buffer is not 1722f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // big enough, it returns a negative value in MSVC, and returns the 1723f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // needed buffer size on Linux. When there is an output error, it 1724f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // always returns a negative value. For simplicity, we lump the two 1725f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // error cases together. 1726f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org if (size < 0 || size >= kBufferSize) { 1727f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org return String("<formatting error or buffer exceeded>"); 1728f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org } else { 1729f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org return String(buffer, size); 1730f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org } 1731f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org} 1732f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 1733f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// Converts the buffer in a stringstream to a String, converting NUL 1734f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// bytes to "\\0" along the way. 1735f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgString StringStreamToString(::std::stringstream* ss) { 1736f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org const ::std::string& str = ss->str(); 1737f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org const char* const start = str.c_str(); 1738f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org const char* const end = start + str.length(); 1739f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 1740f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // We need to use a helper stringstream to do this transformation 1741f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // because String doesn't support push_back(). 1742f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org ::std::stringstream helper; 1743f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org for (const char* ch = start; ch != end; ++ch) { 1744f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org if (*ch == '\0') { 1745f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org helper << "\\0"; // Replaces NUL with "\\0"; 1746f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org } else { 1747f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org helper.put(*ch); 1748f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org } 1749f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org } 1750f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 1751f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org return String(helper.str().c_str()); 1752f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org} 1753f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 1754f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// Appends the user-supplied message to the Google-Test-generated message. 1755f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgString AppendUserMessage(const String& gtest_msg, 1756f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org const Message& user_msg) { 1757f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // Appends the user message if it's non-empty. 1758f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org const String user_msg_string = user_msg.GetString(); 1759f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org if (user_msg_string.empty()) { 1760f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org return gtest_msg; 1761f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org } 1762f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 1763f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org Message msg; 1764f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org msg << gtest_msg << "\n" << user_msg_string; 1765f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 1766f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org return msg.GetString(); 1767f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org} 1768f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 1769f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org} // namespace internal 1770f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 1771f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// class TestResult 1772f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 1773f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// Creates an empty TestResult. 1774f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgTestResult::TestResult() 1775f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org : death_test_count_(0), 1776f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org elapsed_time_(0) { 1777f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org} 1778f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 1779f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// D'tor. 1780f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgTestResult::~TestResult() { 1781f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org} 1782f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 1783f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// Returns the i-th test part result among all the results. i can 1784f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// range from 0 to total_part_count() - 1. If i is not in that range, 1785f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// aborts the program. 1786f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgconst TestPartResult& TestResult::GetTestPartResult(int i) const { 1787f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org if (i < 0 || i >= total_part_count()) 1788f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org internal::posix::Abort(); 1789f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org return test_part_results_.at(i); 1790f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org} 1791f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 1792f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// Returns the i-th test property. i can range from 0 to 1793f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// test_property_count() - 1. If i is not in that range, aborts the 1794f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// program. 1795f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgconst TestProperty& TestResult::GetTestProperty(int i) const { 1796f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org if (i < 0 || i >= test_property_count()) 1797f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org internal::posix::Abort(); 1798f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org return test_properties_.at(i); 1799f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org} 1800f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 1801f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// Clears the test part results. 1802f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgvoid TestResult::ClearTestPartResults() { 1803f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org test_part_results_.clear(); 1804f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org} 1805f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 1806f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// Adds a test part result to the list. 1807f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgvoid TestResult::AddTestPartResult(const TestPartResult& test_part_result) { 1808f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org test_part_results_.push_back(test_part_result); 1809f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org} 1810f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 1811f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// Adds a test property to the list. If a property with the same key as the 1812f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// supplied property is already represented, the value of this test_property 1813f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// replaces the old value for that key. 1814f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgvoid TestResult::RecordProperty(const TestProperty& test_property) { 1815f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org if (!ValidateTestProperty(test_property)) { 1816f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org return; 1817f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org } 1818f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org internal::MutexLock lock(&test_properites_mutex_); 1819f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org const std::vector<TestProperty>::iterator property_with_matching_key = 1820f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org std::find_if(test_properties_.begin(), test_properties_.end(), 1821f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org internal::TestPropertyKeyIs(test_property.key())); 1822f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org if (property_with_matching_key == test_properties_.end()) { 1823f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org test_properties_.push_back(test_property); 1824f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org return; 1825f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org } 1826f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org property_with_matching_key->SetValue(test_property.value()); 1827f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org} 1828f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 1829f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// Adds a failure if the key is a reserved attribute of Google Test 1830f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// testcase tags. Returns true if the property is valid. 1831f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgbool TestResult::ValidateTestProperty(const TestProperty& test_property) { 1832f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org internal::String key(test_property.key()); 1833f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org if (key == "name" || key == "status" || key == "time" || key == "classname") { 1834f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org ADD_FAILURE() 1835f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org << "Reserved key used in RecordProperty(): " 1836f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org << key 1837f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org << " ('name', 'status', 'time', and 'classname' are reserved by " 1838f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org << GTEST_NAME_ << ")"; 1839f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org return false; 1840f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org } 1841f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org return true; 1842f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org} 1843f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 1844f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// Clears the object. 1845f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgvoid TestResult::Clear() { 1846f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org test_part_results_.clear(); 1847f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org test_properties_.clear(); 1848f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org death_test_count_ = 0; 1849f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org elapsed_time_ = 0; 1850f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org} 1851f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 1852f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// Returns true iff the test failed. 1853f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgbool TestResult::Failed() const { 1854f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org for (int i = 0; i < total_part_count(); ++i) { 1855f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org if (GetTestPartResult(i).failed()) 1856f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org return true; 1857f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org } 1858f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org return false; 1859f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org} 1860f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 1861f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// Returns true iff the test part fatally failed. 1862f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgstatic bool TestPartFatallyFailed(const TestPartResult& result) { 1863f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org return result.fatally_failed(); 1864f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org} 1865f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 1866f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// Returns true iff the test fatally failed. 1867f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgbool TestResult::HasFatalFailure() const { 1868f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org return CountIf(test_part_results_, TestPartFatallyFailed) > 0; 1869f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org} 1870f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 1871f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// Returns true iff the test part non-fatally failed. 1872f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgstatic bool TestPartNonfatallyFailed(const TestPartResult& result) { 1873f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org return result.nonfatally_failed(); 1874f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org} 1875f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 1876f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// Returns true iff the test has a non-fatal failure. 1877f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgbool TestResult::HasNonfatalFailure() const { 1878f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org return CountIf(test_part_results_, TestPartNonfatallyFailed) > 0; 1879f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org} 1880f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 1881f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// Gets the number of all test parts. This is the sum of the number 1882f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// of successful test parts and the number of failed test parts. 1883f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgint TestResult::total_part_count() const { 1884f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org return static_cast<int>(test_part_results_.size()); 1885f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org} 1886f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 1887f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// Returns the number of the test properties. 1888f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgint TestResult::test_property_count() const { 1889f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org return static_cast<int>(test_properties_.size()); 1890f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org} 1891f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 1892f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// class Test 1893f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 1894f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// Creates a Test object. 1895f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 1896f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// The c'tor saves the values of all Google Test flags. 1897f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgTest::Test() 1898f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org : gtest_flag_saver_(new internal::GTestFlagSaver) { 1899f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org} 1900f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 1901f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// The d'tor restores the values of all Google Test flags. 1902f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgTest::~Test() { 1903f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org delete gtest_flag_saver_; 1904f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org} 1905f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 1906f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// Sets up the test fixture. 1907f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// 1908f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// A sub-class may override this. 1909f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgvoid Test::SetUp() { 1910f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org} 1911f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 1912f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// Tears down the test fixture. 1913f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// 1914f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// A sub-class may override this. 1915f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgvoid Test::TearDown() { 1916f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org} 1917f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 1918f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// Allows user supplied key value pairs to be recorded for later output. 1919f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgvoid Test::RecordProperty(const char* key, const char* value) { 1920f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org UnitTest::GetInstance()->RecordPropertyForCurrentTest(key, value); 1921f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org} 1922f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 1923f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// Allows user supplied key value pairs to be recorded for later output. 1924f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgvoid Test::RecordProperty(const char* key, int value) { 1925f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org Message value_message; 1926f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org value_message << value; 1927f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org RecordProperty(key, value_message.GetString().c_str()); 1928f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org} 1929f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 1930f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgnamespace internal { 1931f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 1932f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgvoid ReportFailureInUnknownLocation(TestPartResult::Type result_type, 1933f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org const String& message) { 1934f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // This function is a friend of UnitTest and as such has access to 1935f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // AddTestPartResult. 1936f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org UnitTest::GetInstance()->AddTestPartResult( 1937f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org result_type, 1938f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org NULL, // No info about the source file where the exception occurred. 1939f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org -1, // We have no info on which line caused the exception. 1940f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org message, 1941f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org String()); // No stack trace, either. 1942f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org} 1943f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 1944f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org} // namespace internal 1945f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 1946f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// Google Test requires all tests in the same test case to use the same test 1947f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// fixture class. This function checks if the current test has the 1948f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// same fixture class as the first test in the current test case. If 1949f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// yes, it returns true; otherwise it generates a Google Test failure and 1950f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// returns false. 1951f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgbool Test::HasSameFixtureClass() { 1952f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org internal::UnitTestImpl* const impl = internal::GetUnitTestImpl(); 1953f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org const TestCase* const test_case = impl->current_test_case(); 1954f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 1955f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // Info about the first test in the current test case. 1956f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org const TestInfo* const first_test_info = test_case->test_info_list()[0]; 1957f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org const internal::TypeId first_fixture_id = first_test_info->fixture_class_id_; 1958f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org const char* const first_test_name = first_test_info->name(); 1959f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 1960f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // Info about the current test. 1961f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org const TestInfo* const this_test_info = impl->current_test_info(); 1962f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org const internal::TypeId this_fixture_id = this_test_info->fixture_class_id_; 1963f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org const char* const this_test_name = this_test_info->name(); 1964f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 1965f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org if (this_fixture_id != first_fixture_id) { 1966f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // Is the first test defined using TEST? 1967f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org const bool first_is_TEST = first_fixture_id == internal::GetTestTypeId(); 1968f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // Is this test defined using TEST? 1969f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org const bool this_is_TEST = this_fixture_id == internal::GetTestTypeId(); 1970f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 1971f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org if (first_is_TEST || this_is_TEST) { 1972f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // The user mixed TEST and TEST_F in this test case - we'll tell 1973f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // him/her how to fix it. 1974f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 1975f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // Gets the name of the TEST and the name of the TEST_F. Note 1976f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // that first_is_TEST and this_is_TEST cannot both be true, as 1977f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // the fixture IDs are different for the two tests. 1978f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org const char* const TEST_name = 1979f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org first_is_TEST ? first_test_name : this_test_name; 1980f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org const char* const TEST_F_name = 1981f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org first_is_TEST ? this_test_name : first_test_name; 1982f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 1983f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org ADD_FAILURE() 1984f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org << "All tests in the same test case must use the same test fixture\n" 1985f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org << "class, so mixing TEST_F and TEST in the same test case is\n" 1986f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org << "illegal. In test case " << this_test_info->test_case_name() 1987f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org << ",\n" 1988f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org << "test " << TEST_F_name << " is defined using TEST_F but\n" 1989f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org << "test " << TEST_name << " is defined using TEST. You probably\n" 1990f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org << "want to change the TEST to TEST_F or move it to another test\n" 1991f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org << "case."; 1992f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org } else { 1993f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // The user defined two fixture classes with the same name in 1994f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // two namespaces - we'll tell him/her how to fix it. 1995f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org ADD_FAILURE() 1996f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org << "All tests in the same test case must use the same test fixture\n" 1997f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org << "class. However, in test case " 1998f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org << this_test_info->test_case_name() << ",\n" 1999f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org << "you defined test " << first_test_name 2000f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org << " and test " << this_test_name << "\n" 2001f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org << "using two different test fixture classes. This can happen if\n" 2002f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org << "the two classes are from different namespaces or translation\n" 2003f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org << "units and have the same name. You should probably rename one\n" 2004f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org << "of the classes to put the tests into different test cases."; 2005f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org } 2006f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org return false; 2007f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org } 2008f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 2009f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org return true; 2010f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org} 2011f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 2012f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#if GTEST_HAS_SEH 2013f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 2014f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// Adds an "exception thrown" fatal failure to the current test. This 2015f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// function returns its result via an output parameter pointer because VC++ 2016f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// prohibits creation of objects with destructors on stack in functions 2017f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// using __try (see error C2712). 2018f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgstatic internal::String* FormatSehExceptionMessage(DWORD exception_code, 2019f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org const char* location) { 2020f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org Message message; 2021f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org message << "SEH exception with code 0x" << std::setbase(16) << 2022f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org exception_code << std::setbase(10) << " thrown in " << location << "."; 2023f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 2024f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org return new internal::String(message.GetString()); 2025f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org} 2026f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 2027f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#endif // GTEST_HAS_SEH 2028f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 2029f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#if GTEST_HAS_EXCEPTIONS 2030f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 2031f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// Adds an "exception thrown" fatal failure to the current test. 2032f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgstatic internal::String FormatCxxExceptionMessage(const char* description, 2033f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org const char* location) { 2034f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org Message message; 2035f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org if (description != NULL) { 2036f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org message << "C++ exception with description \"" << description << "\""; 2037f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org } else { 2038f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org message << "Unknown C++ exception"; 2039f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org } 2040f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org message << " thrown in " << location << "."; 2041f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 2042f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org return message.GetString(); 2043f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org} 2044f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 2045f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgstatic internal::String PrintTestPartResultToString( 2046f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org const TestPartResult& test_part_result); 2047f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 2048f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// A failed Google Test assertion will throw an exception of this type when 2049f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// GTEST_FLAG(throw_on_failure) is true (if exceptions are enabled). We 2050f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// derive it from std::runtime_error, which is for errors presumably 2051f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// detectable only at run time. Since std::runtime_error inherits from 2052f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// std::exception, many testing frameworks know how to extract and print the 2053f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// message inside it. 2054f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgclass GoogleTestFailureException : public ::std::runtime_error { 2055f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org public: 2056f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org explicit GoogleTestFailureException(const TestPartResult& failure) 2057f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org : ::std::runtime_error(PrintTestPartResultToString(failure).c_str()) {} 2058f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org}; 2059f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#endif // GTEST_HAS_EXCEPTIONS 2060f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 2061f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgnamespace internal { 2062f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// We put these helper functions in the internal namespace as IBM's xlC 2063f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// compiler rejects the code if they were declared static. 2064f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 2065f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// Runs the given method and handles SEH exceptions it throws, when 2066f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// SEH is supported; returns the 0-value for type Result in case of an 2067f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// SEH exception. (Microsoft compilers cannot handle SEH and C++ 2068f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// exceptions in the same function. Therefore, we provide a separate 2069f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// wrapper function for handling SEH exceptions.) 2070f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgtemplate <class T, typename Result> 2071f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgResult HandleSehExceptionsInMethodIfSupported( 2072f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org T* object, Result (T::*method)(), const char* location) { 2073f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#if GTEST_HAS_SEH 2074f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org __try { 2075f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org return (object->*method)(); 2076f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org } __except (internal::UnitTestOptions::GTestShouldProcessSEH( // NOLINT 2077f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org GetExceptionCode())) { 2078f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // We create the exception message on the heap because VC++ prohibits 2079f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // creation of objects with destructors on stack in functions using __try 2080f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // (see error C2712). 2081f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org internal::String* exception_message = FormatSehExceptionMessage( 2082f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org GetExceptionCode(), location); 2083f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org internal::ReportFailureInUnknownLocation(TestPartResult::kFatalFailure, 2084f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org *exception_message); 2085f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org delete exception_message; 2086f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org return static_cast<Result>(0); 2087f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org } 2088f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#else 2089f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org (void)location; 2090f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org return (object->*method)(); 2091f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#endif // GTEST_HAS_SEH 2092f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org} 2093f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 2094f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// Runs the given method and catches and reports C++ and/or SEH-style 2095f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// exceptions, if they are supported; returns the 0-value for type 2096f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// Result in case of an SEH exception. 2097f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgtemplate <class T, typename Result> 2098f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgResult HandleExceptionsInMethodIfSupported( 2099f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org T* object, Result (T::*method)(), const char* location) { 2100f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // NOTE: The user code can affect the way in which Google Test handles 2101f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // exceptions by setting GTEST_FLAG(catch_exceptions), but only before 2102f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // RUN_ALL_TESTS() starts. It is technically possible to check the flag 2103f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // after the exception is caught and either report or re-throw the 2104f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // exception based on the flag's value: 2105f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // 2106f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // try { 2107f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // // Perform the test method. 2108f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // } catch (...) { 2109f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // if (GTEST_FLAG(catch_exceptions)) 2110f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // // Report the exception as failure. 2111f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // else 2112f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // throw; // Re-throws the original exception. 2113f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // } 2114f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // 2115f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // However, the purpose of this flag is to allow the program to drop into 2116f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // the debugger when the exception is thrown. On most platforms, once the 2117f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // control enters the catch block, the exception origin information is 2118f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // lost and the debugger will stop the program at the point of the 2119f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // re-throw in this function -- instead of at the point of the original 2120f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // throw statement in the code under test. For this reason, we perform 2121f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // the check early, sacrificing the ability to affect Google Test's 2122f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // exception handling in the method where the exception is thrown. 2123f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org if (internal::GetUnitTestImpl()->catch_exceptions()) { 2124f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#if GTEST_HAS_EXCEPTIONS 2125f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org try { 2126f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org return HandleSehExceptionsInMethodIfSupported(object, method, location); 2127f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org } catch (const GoogleTestFailureException&) { // NOLINT 2128f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // This exception doesn't originate in code under test. It makes no 2129f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // sense to report it as a test failure. 2130f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org throw; 2131f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org } catch (const std::exception& e) { // NOLINT 2132f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org internal::ReportFailureInUnknownLocation( 2133f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org TestPartResult::kFatalFailure, 2134f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org FormatCxxExceptionMessage(e.what(), location)); 2135f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org } catch (...) { // NOLINT 2136f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org internal::ReportFailureInUnknownLocation( 2137f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org TestPartResult::kFatalFailure, 2138f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org FormatCxxExceptionMessage(NULL, location)); 2139f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org } 2140f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org return static_cast<Result>(0); 2141f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#else 2142f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org return HandleSehExceptionsInMethodIfSupported(object, method, location); 2143f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#endif // GTEST_HAS_EXCEPTIONS 2144f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org } else { 2145f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org return (object->*method)(); 2146f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org } 2147f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org} 2148f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 2149f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org} // namespace internal 2150f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 2151f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// Runs the test and updates the test result. 2152f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgvoid Test::Run() { 2153f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org if (!HasSameFixtureClass()) return; 2154f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 2155f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org internal::UnitTestImpl* const impl = internal::GetUnitTestImpl(); 2156f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org impl->os_stack_trace_getter()->UponLeavingGTest(); 2157f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org internal::HandleExceptionsInMethodIfSupported(this, &Test::SetUp, "SetUp()"); 2158f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // We will run the test only if SetUp() was successful. 2159f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org if (!HasFatalFailure()) { 2160f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org impl->os_stack_trace_getter()->UponLeavingGTest(); 2161f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org internal::HandleExceptionsInMethodIfSupported( 2162f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org this, &Test::TestBody, "the test body"); 2163f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org } 2164f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 2165f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // However, we want to clean up as much as possible. Hence we will 2166f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // always call TearDown(), even if SetUp() or the test body has 2167f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // failed. 2168f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org impl->os_stack_trace_getter()->UponLeavingGTest(); 2169f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org internal::HandleExceptionsInMethodIfSupported( 2170f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org this, &Test::TearDown, "TearDown()"); 2171f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org} 2172f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 2173f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// Returns true iff the current test has a fatal failure. 2174f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgbool Test::HasFatalFailure() { 2175f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org return internal::GetUnitTestImpl()->current_test_result()->HasFatalFailure(); 2176f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org} 2177f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 2178f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// Returns true iff the current test has a non-fatal failure. 2179f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgbool Test::HasNonfatalFailure() { 2180f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org return internal::GetUnitTestImpl()->current_test_result()-> 2181f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org HasNonfatalFailure(); 2182f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org} 2183f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 2184f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// class TestInfo 2185f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 2186f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// Constructs a TestInfo object. It assumes ownership of the test factory 2187f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// object. 2188f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// TODO(vladl@google.com): Make a_test_case_name and a_name const string&'s 2189f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// to signify they cannot be NULLs. 2190f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgTestInfo::TestInfo(const char* a_test_case_name, 2191f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org const char* a_name, 2192f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org const char* a_type_param, 2193f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org const char* a_value_param, 2194f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org internal::TypeId fixture_class_id, 2195f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org internal::TestFactoryBase* factory) 2196f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org : test_case_name_(a_test_case_name), 2197f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org name_(a_name), 2198f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org type_param_(a_type_param ? new std::string(a_type_param) : NULL), 2199f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org value_param_(a_value_param ? new std::string(a_value_param) : NULL), 2200f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org fixture_class_id_(fixture_class_id), 2201f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org should_run_(false), 2202f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org is_disabled_(false), 2203f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org matches_filter_(false), 2204f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org factory_(factory), 2205f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org result_() {} 2206f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 2207f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// Destructs a TestInfo object. 2208f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgTestInfo::~TestInfo() { delete factory_; } 2209f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 2210f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgnamespace internal { 2211f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 2212f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// Creates a new TestInfo object and registers it with Google Test; 2213f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// returns the created object. 2214f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// 2215f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// Arguments: 2216f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// 2217f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// test_case_name: name of the test case 2218f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// name: name of the test 2219f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// type_param: the name of the test's type parameter, or NULL if 2220f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// this is not a typed or a type-parameterized test. 2221f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// value_param: text representation of the test's value parameter, 2222f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// or NULL if this is not a value-parameterized test. 2223f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// fixture_class_id: ID of the test fixture class 2224f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// set_up_tc: pointer to the function that sets up the test case 2225f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// tear_down_tc: pointer to the function that tears down the test case 2226f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// factory: pointer to the factory that creates a test object. 2227f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// The newly created TestInfo instance will assume 2228f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// ownership of the factory object. 2229f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgTestInfo* MakeAndRegisterTestInfo( 2230f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org const char* test_case_name, const char* name, 2231f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org const char* type_param, 2232f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org const char* value_param, 2233f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org TypeId fixture_class_id, 2234f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org SetUpTestCaseFunc set_up_tc, 2235f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org TearDownTestCaseFunc tear_down_tc, 2236f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org TestFactoryBase* factory) { 2237f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org TestInfo* const test_info = 2238f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org new TestInfo(test_case_name, name, type_param, value_param, 2239f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org fixture_class_id, factory); 2240f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org GetUnitTestImpl()->AddTestInfo(set_up_tc, tear_down_tc, test_info); 2241f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org return test_info; 2242f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org} 2243f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 2244f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#if GTEST_HAS_PARAM_TEST 2245f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgvoid ReportInvalidTestCaseType(const char* test_case_name, 2246f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org const char* file, int line) { 2247f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org Message errors; 2248f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org errors 2249f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org << "Attempted redefinition of test case " << test_case_name << ".\n" 2250f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org << "All tests in the same test case must use the same test fixture\n" 2251f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org << "class. However, in test case " << test_case_name << ", you tried\n" 2252f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org << "to define a test using a fixture class different from the one\n" 2253f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org << "used earlier. This can happen if the two fixture classes are\n" 2254f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org << "from different namespaces and have the same name. You should\n" 2255f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org << "probably rename one of the classes to put the tests into different\n" 2256f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org << "test cases."; 2257f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 2258f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org fprintf(stderr, "%s %s", FormatFileLocation(file, line).c_str(), 2259f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org errors.GetString().c_str()); 2260f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org} 2261f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#endif // GTEST_HAS_PARAM_TEST 2262f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 2263f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org} // namespace internal 2264f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 2265f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgnamespace { 2266f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 2267f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// A predicate that checks the test name of a TestInfo against a known 2268f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// value. 2269f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// 2270f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// This is used for implementation of the TestCase class only. We put 2271f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// it in the anonymous namespace to prevent polluting the outer 2272f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// namespace. 2273f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// 2274f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// TestNameIs is copyable. 2275f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgclass TestNameIs { 2276f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org public: 2277f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // Constructor. 2278f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // 2279f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // TestNameIs has NO default constructor. 2280f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org explicit TestNameIs(const char* name) 2281f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org : name_(name) {} 2282f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 2283f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // Returns true iff the test name of test_info matches name_. 2284f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org bool operator()(const TestInfo * test_info) const { 2285f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org return test_info && internal::String(test_info->name()).Compare(name_) == 0; 2286f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org } 2287f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 2288f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org private: 2289f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org internal::String name_; 2290f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org}; 2291f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 2292f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org} // namespace 2293f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 2294f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgnamespace internal { 2295f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 2296f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// This method expands all parameterized tests registered with macros TEST_P 2297f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// and INSTANTIATE_TEST_CASE_P into regular tests and registers those. 2298f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// This will be done just once during the program runtime. 2299f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgvoid UnitTestImpl::RegisterParameterizedTests() { 2300f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#if GTEST_HAS_PARAM_TEST 2301f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org if (!parameterized_tests_registered_) { 2302f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org parameterized_test_registry_.RegisterTests(); 2303f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org parameterized_tests_registered_ = true; 2304f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org } 2305f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#endif 2306f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org} 2307f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 2308f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org} // namespace internal 2309f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 2310f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// Creates the test object, runs it, records its result, and then 2311f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// deletes it. 2312f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgvoid TestInfo::Run() { 2313f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org if (!should_run_) return; 2314f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 2315f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // Tells UnitTest where to store test result. 2316f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org internal::UnitTestImpl* const impl = internal::GetUnitTestImpl(); 2317f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org impl->set_current_test_info(this); 2318f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 2319f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org TestEventListener* repeater = UnitTest::GetInstance()->listeners().repeater(); 2320f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 2321f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // Notifies the unit test event listeners that a test is about to start. 2322f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org repeater->OnTestStart(*this); 2323f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 2324f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org const TimeInMillis start = internal::GetTimeInMillis(); 2325f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 2326f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org impl->os_stack_trace_getter()->UponLeavingGTest(); 2327f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 2328f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // Creates the test object. 2329f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org Test* const test = internal::HandleExceptionsInMethodIfSupported( 2330f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org factory_, &internal::TestFactoryBase::CreateTest, 2331f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org "the test fixture's constructor"); 2332f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 2333f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // Runs the test only if the test object was created and its 2334f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // constructor didn't generate a fatal failure. 2335f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org if ((test != NULL) && !Test::HasFatalFailure()) { 2336f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // This doesn't throw as all user code that can throw are wrapped into 2337f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // exception handling code. 2338f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org test->Run(); 2339f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org } 2340f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 2341f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // Deletes the test object. 2342f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org impl->os_stack_trace_getter()->UponLeavingGTest(); 2343f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org internal::HandleExceptionsInMethodIfSupported( 2344f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org test, &Test::DeleteSelf_, "the test fixture's destructor"); 2345f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 2346f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org result_.set_elapsed_time(internal::GetTimeInMillis() - start); 2347f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 2348f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // Notifies the unit test event listener that a test has just finished. 2349f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org repeater->OnTestEnd(*this); 2350f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 2351f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // Tells UnitTest to stop associating assertion results to this 2352f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // test. 2353f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org impl->set_current_test_info(NULL); 2354f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org} 2355f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 2356f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// class TestCase 2357f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 2358f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// Gets the number of successful tests in this test case. 2359f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgint TestCase::successful_test_count() const { 2360f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org return CountIf(test_info_list_, TestPassed); 2361f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org} 2362f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 2363f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// Gets the number of failed tests in this test case. 2364f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgint TestCase::failed_test_count() const { 2365f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org return CountIf(test_info_list_, TestFailed); 2366f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org} 2367f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 2368f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgint TestCase::disabled_test_count() const { 2369f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org return CountIf(test_info_list_, TestDisabled); 2370f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org} 2371f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 2372f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// Get the number of tests in this test case that should run. 2373f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgint TestCase::test_to_run_count() const { 2374f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org return CountIf(test_info_list_, ShouldRunTest); 2375f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org} 2376f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 2377f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// Gets the number of all tests. 2378f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgint TestCase::total_test_count() const { 2379f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org return static_cast<int>(test_info_list_.size()); 2380f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org} 2381f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 2382f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// Creates a TestCase with the given name. 2383f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// 2384f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// Arguments: 2385f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// 2386f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// name: name of the test case 2387f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// a_type_param: the name of the test case's type parameter, or NULL if 2388f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// this is not a typed or a type-parameterized test case. 2389f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// set_up_tc: pointer to the function that sets up the test case 2390f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// tear_down_tc: pointer to the function that tears down the test case 2391f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgTestCase::TestCase(const char* a_name, const char* a_type_param, 2392f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org Test::SetUpTestCaseFunc set_up_tc, 2393f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org Test::TearDownTestCaseFunc tear_down_tc) 2394f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org : name_(a_name), 2395f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org type_param_(a_type_param ? new std::string(a_type_param) : NULL), 2396f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org set_up_tc_(set_up_tc), 2397f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org tear_down_tc_(tear_down_tc), 2398f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org should_run_(false), 2399f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org elapsed_time_(0) { 2400f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org} 2401f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 2402f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// Destructor of TestCase. 2403f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgTestCase::~TestCase() { 2404f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // Deletes every Test in the collection. 2405f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org ForEach(test_info_list_, internal::Delete<TestInfo>); 2406f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org} 2407f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 2408f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// Returns the i-th test among all the tests. i can range from 0 to 2409f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// total_test_count() - 1. If i is not in that range, returns NULL. 2410f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgconst TestInfo* TestCase::GetTestInfo(int i) const { 2411f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org const int index = GetElementOr(test_indices_, i, -1); 2412f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org return index < 0 ? NULL : test_info_list_[index]; 2413f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org} 2414f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 2415f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// Returns the i-th test among all the tests. i can range from 0 to 2416f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// total_test_count() - 1. If i is not in that range, returns NULL. 2417f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgTestInfo* TestCase::GetMutableTestInfo(int i) { 2418f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org const int index = GetElementOr(test_indices_, i, -1); 2419f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org return index < 0 ? NULL : test_info_list_[index]; 2420f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org} 2421f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 2422f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// Adds a test to this test case. Will delete the test upon 2423f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// destruction of the TestCase object. 2424f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgvoid TestCase::AddTestInfo(TestInfo * test_info) { 2425f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org test_info_list_.push_back(test_info); 2426f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org test_indices_.push_back(static_cast<int>(test_indices_.size())); 2427f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org} 2428f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 2429f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// Runs every test in this TestCase. 2430f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgvoid TestCase::Run() { 2431f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org if (!should_run_) return; 2432f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 2433f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org internal::UnitTestImpl* const impl = internal::GetUnitTestImpl(); 2434f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org impl->set_current_test_case(this); 2435f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 2436f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org TestEventListener* repeater = UnitTest::GetInstance()->listeners().repeater(); 2437f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 2438f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org repeater->OnTestCaseStart(*this); 2439f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org impl->os_stack_trace_getter()->UponLeavingGTest(); 2440f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org internal::HandleExceptionsInMethodIfSupported( 2441f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org this, &TestCase::RunSetUpTestCase, "SetUpTestCase()"); 2442f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 2443f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org const internal::TimeInMillis start = internal::GetTimeInMillis(); 2444f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org for (int i = 0; i < total_test_count(); i++) { 2445f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org GetMutableTestInfo(i)->Run(); 2446f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org } 2447f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org elapsed_time_ = internal::GetTimeInMillis() - start; 2448f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 2449f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org impl->os_stack_trace_getter()->UponLeavingGTest(); 2450f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org internal::HandleExceptionsInMethodIfSupported( 2451f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org this, &TestCase::RunTearDownTestCase, "TearDownTestCase()"); 2452f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 2453f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org repeater->OnTestCaseEnd(*this); 2454f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org impl->set_current_test_case(NULL); 2455f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org} 2456f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 2457f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// Clears the results of all tests in this test case. 2458f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgvoid TestCase::ClearResult() { 2459f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org ForEach(test_info_list_, TestInfo::ClearTestResult); 2460f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org} 2461f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 2462f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// Shuffles the tests in this test case. 2463f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgvoid TestCase::ShuffleTests(internal::Random* random) { 2464f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org Shuffle(random, &test_indices_); 2465f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org} 2466f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 2467f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// Restores the test order to before the first shuffle. 2468f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgvoid TestCase::UnshuffleTests() { 2469f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org for (size_t i = 0; i < test_indices_.size(); i++) { 2470f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org test_indices_[i] = static_cast<int>(i); 2471f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org } 2472f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org} 2473f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 2474f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// Formats a countable noun. Depending on its quantity, either the 2475f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// singular form or the plural form is used. e.g. 2476f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// 2477f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// FormatCountableNoun(1, "formula", "formuli") returns "1 formula". 2478f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// FormatCountableNoun(5, "book", "books") returns "5 books". 2479f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgstatic internal::String FormatCountableNoun(int count, 2480f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org const char * singular_form, 2481f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org const char * plural_form) { 2482f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org return internal::String::Format("%d %s", count, 2483f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org count == 1 ? singular_form : plural_form); 2484f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org} 2485f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 2486f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// Formats the count of tests. 2487f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgstatic internal::String FormatTestCount(int test_count) { 2488f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org return FormatCountableNoun(test_count, "test", "tests"); 2489f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org} 2490f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 2491f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// Formats the count of test cases. 2492f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgstatic internal::String FormatTestCaseCount(int test_case_count) { 2493f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org return FormatCountableNoun(test_case_count, "test case", "test cases"); 2494f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org} 2495f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 2496f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// Converts a TestPartResult::Type enum to human-friendly string 2497f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// representation. Both kNonFatalFailure and kFatalFailure are translated 2498f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// to "Failure", as the user usually doesn't care about the difference 2499f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// between the two when viewing the test result. 2500f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgstatic const char * TestPartResultTypeToString(TestPartResult::Type type) { 2501f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org switch (type) { 2502f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org case TestPartResult::kSuccess: 2503f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org return "Success"; 2504f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 2505f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org case TestPartResult::kNonFatalFailure: 2506f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org case TestPartResult::kFatalFailure: 2507f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#ifdef _MSC_VER 2508f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org return "error: "; 2509f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#else 2510f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org return "Failure\n"; 2511f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#endif 2512f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org default: 2513f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org return "Unknown result type"; 2514f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org } 2515f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org} 2516f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 2517f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// Prints a TestPartResult to a String. 2518f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgstatic internal::String PrintTestPartResultToString( 2519f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org const TestPartResult& test_part_result) { 2520f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org return (Message() 2521f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org << internal::FormatFileLocation(test_part_result.file_name(), 2522f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org test_part_result.line_number()) 2523f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org << " " << TestPartResultTypeToString(test_part_result.type()) 2524f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org << test_part_result.message()).GetString(); 2525f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org} 2526f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 2527f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// Prints a TestPartResult. 2528f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgstatic void PrintTestPartResult(const TestPartResult& test_part_result) { 2529f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org const internal::String& result = 2530f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org PrintTestPartResultToString(test_part_result); 2531f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org printf("%s\n", result.c_str()); 2532f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org fflush(stdout); 2533f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // If the test program runs in Visual Studio or a debugger, the 2534f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // following statements add the test part result message to the Output 2535f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // window such that the user can double-click on it to jump to the 2536f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // corresponding source code location; otherwise they do nothing. 2537f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#if GTEST_OS_WINDOWS && !GTEST_OS_WINDOWS_MOBILE 2538f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // We don't call OutputDebugString*() on Windows Mobile, as printing 2539f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // to stdout is done by OutputDebugString() there already - we don't 2540f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // want the same message printed twice. 2541f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org ::OutputDebugStringA(result.c_str()); 2542f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org ::OutputDebugStringA("\n"); 2543f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#endif 2544f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org} 2545f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 2546f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// class PrettyUnitTestResultPrinter 2547f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 2548f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgnamespace internal { 2549f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 2550f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgenum GTestColor { 2551f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org COLOR_DEFAULT, 2552f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org COLOR_RED, 2553f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org COLOR_GREEN, 2554f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org COLOR_YELLOW 2555f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org}; 2556f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 2557f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#if GTEST_OS_WINDOWS && !GTEST_OS_WINDOWS_MOBILE 2558f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 2559f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// Returns the character attribute for the given color. 2560f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgWORD GetColorAttribute(GTestColor color) { 2561f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org switch (color) { 2562f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org case COLOR_RED: return FOREGROUND_RED; 2563f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org case COLOR_GREEN: return FOREGROUND_GREEN; 2564f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org case COLOR_YELLOW: return FOREGROUND_RED | FOREGROUND_GREEN; 2565f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org default: return 0; 2566f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org } 2567f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org} 2568f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 2569f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#else 2570f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 2571f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// Returns the ANSI color code for the given color. COLOR_DEFAULT is 2572f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// an invalid input. 2573f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgconst char* GetAnsiColorCode(GTestColor color) { 2574f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org switch (color) { 2575f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org case COLOR_RED: return "1"; 2576f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org case COLOR_GREEN: return "2"; 2577f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org case COLOR_YELLOW: return "3"; 2578f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org default: return NULL; 2579f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org }; 2580f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org} 2581f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 2582f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#endif // GTEST_OS_WINDOWS && !GTEST_OS_WINDOWS_MOBILE 2583f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 2584f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// Returns true iff Google Test should use colors in the output. 2585f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgbool ShouldUseColor(bool stdout_is_tty) { 2586f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org const char* const gtest_color = GTEST_FLAG(color).c_str(); 2587f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 2588f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org if (String::CaseInsensitiveCStringEquals(gtest_color, "auto")) { 2589f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#if GTEST_OS_WINDOWS 2590f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // On Windows the TERM variable is usually not set, but the 2591f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // console there does support colors. 2592f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org return stdout_is_tty; 2593f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#else 2594f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // On non-Windows platforms, we rely on the TERM variable. 2595f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org const char* const term = posix::GetEnv("TERM"); 2596f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org const bool term_supports_color = 2597f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org String::CStringEquals(term, "xterm") || 2598f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org String::CStringEquals(term, "xterm-color") || 2599f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org String::CStringEquals(term, "xterm-256color") || 2600f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org String::CStringEquals(term, "screen") || 2601f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org String::CStringEquals(term, "linux") || 2602f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org String::CStringEquals(term, "cygwin"); 2603f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org return stdout_is_tty && term_supports_color; 2604f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#endif // GTEST_OS_WINDOWS 2605f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org } 2606f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 2607f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org return String::CaseInsensitiveCStringEquals(gtest_color, "yes") || 2608f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org String::CaseInsensitiveCStringEquals(gtest_color, "true") || 2609f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org String::CaseInsensitiveCStringEquals(gtest_color, "t") || 2610f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org String::CStringEquals(gtest_color, "1"); 2611f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // We take "yes", "true", "t", and "1" as meaning "yes". If the 2612f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // value is neither one of these nor "auto", we treat it as "no" to 2613f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // be conservative. 2614f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org} 2615f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 2616f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// Helpers for printing colored strings to stdout. Note that on Windows, we 2617f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// cannot simply emit special characters and have the terminal change colors. 2618f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// This routine must actually emit the characters rather than return a string 2619f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// that would be colored when printed, as can be done on Linux. 2620f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgvoid ColoredPrintf(GTestColor color, const char* fmt, ...) { 2621f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org va_list args; 2622f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org va_start(args, fmt); 2623f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 2624f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#if GTEST_OS_WINDOWS_MOBILE || GTEST_OS_SYMBIAN || GTEST_OS_ZOS 2625f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org const bool use_color = false; 2626f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#else 2627f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org static const bool in_color_mode = 2628f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org ShouldUseColor(posix::IsATTY(posix::FileNo(stdout)) != 0); 2629f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org const bool use_color = in_color_mode && (color != COLOR_DEFAULT); 2630f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#endif // GTEST_OS_WINDOWS_MOBILE || GTEST_OS_SYMBIAN || GTEST_OS_ZOS 2631f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // The '!= 0' comparison is necessary to satisfy MSVC 7.1. 2632f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 2633f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org if (!use_color) { 2634f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org vprintf(fmt, args); 2635f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org va_end(args); 2636f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org return; 2637f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org } 2638f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 2639f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#if GTEST_OS_WINDOWS && !GTEST_OS_WINDOWS_MOBILE 2640f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org const HANDLE stdout_handle = GetStdHandle(STD_OUTPUT_HANDLE); 2641f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 2642f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // Gets the current text color. 2643f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org CONSOLE_SCREEN_BUFFER_INFO buffer_info; 2644f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org GetConsoleScreenBufferInfo(stdout_handle, &buffer_info); 2645f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org const WORD old_color_attrs = buffer_info.wAttributes; 2646f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 2647f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // We need to flush the stream buffers into the console before each 2648f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // SetConsoleTextAttribute call lest it affect the text that is already 2649f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // printed but has not yet reached the console. 2650f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org fflush(stdout); 2651f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org SetConsoleTextAttribute(stdout_handle, 2652f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org GetColorAttribute(color) | FOREGROUND_INTENSITY); 2653f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org vprintf(fmt, args); 2654f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 2655f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org fflush(stdout); 2656f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // Restores the text color. 2657f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org SetConsoleTextAttribute(stdout_handle, old_color_attrs); 2658f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#else 2659f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org printf("\033[0;3%sm", GetAnsiColorCode(color)); 2660f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org vprintf(fmt, args); 2661f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org printf("\033[m"); // Resets the terminal to default. 2662f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#endif // GTEST_OS_WINDOWS && !GTEST_OS_WINDOWS_MOBILE 2663f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org va_end(args); 2664f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org} 2665f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 2666f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgvoid PrintFullTestCommentIfPresent(const TestInfo& test_info) { 2667f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org const char* const type_param = test_info.type_param(); 2668f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org const char* const value_param = test_info.value_param(); 2669f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 2670f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org if (type_param != NULL || value_param != NULL) { 2671f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org printf(", where "); 2672f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org if (type_param != NULL) { 2673f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org printf("TypeParam = %s", type_param); 2674f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org if (value_param != NULL) 2675f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org printf(" and "); 2676f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org } 2677f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org if (value_param != NULL) { 2678f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org printf("GetParam() = %s", value_param); 2679f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org } 2680f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org } 2681f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org} 2682f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 2683f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// This class implements the TestEventListener interface. 2684f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// 2685f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// Class PrettyUnitTestResultPrinter is copyable. 2686f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgclass PrettyUnitTestResultPrinter : public TestEventListener { 2687f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org public: 2688f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org PrettyUnitTestResultPrinter() {} 2689f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org static void PrintTestName(const char * test_case, const char * test) { 2690f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org printf("%s.%s", test_case, test); 2691f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org } 2692f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 2693f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // The following methods override what's in the TestEventListener class. 2694f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org virtual void OnTestProgramStart(const UnitTest& /*unit_test*/) {} 2695f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org virtual void OnTestIterationStart(const UnitTest& unit_test, int iteration); 2696f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org virtual void OnEnvironmentsSetUpStart(const UnitTest& unit_test); 2697f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org virtual void OnEnvironmentsSetUpEnd(const UnitTest& /*unit_test*/) {} 2698f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org virtual void OnTestCaseStart(const TestCase& test_case); 2699f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org virtual void OnTestStart(const TestInfo& test_info); 2700f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org virtual void OnTestPartResult(const TestPartResult& result); 2701f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org virtual void OnTestEnd(const TestInfo& test_info); 2702f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org virtual void OnTestCaseEnd(const TestCase& test_case); 2703f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org virtual void OnEnvironmentsTearDownStart(const UnitTest& unit_test); 2704f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org virtual void OnEnvironmentsTearDownEnd(const UnitTest& /*unit_test*/) {} 2705f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org virtual void OnTestIterationEnd(const UnitTest& unit_test, int iteration); 2706f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org virtual void OnTestProgramEnd(const UnitTest& /*unit_test*/) {} 2707f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 2708f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org private: 2709f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org static void PrintFailedTests(const UnitTest& unit_test); 2710f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 2711f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org internal::String test_case_name_; 2712f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org}; 2713f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 2714f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // Fired before each iteration of tests starts. 2715f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgvoid PrettyUnitTestResultPrinter::OnTestIterationStart( 2716f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org const UnitTest& unit_test, int iteration) { 2717f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org if (GTEST_FLAG(repeat) != 1) 2718f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org printf("\nRepeating all tests (iteration %d) . . .\n\n", iteration + 1); 2719f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 2720f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org const char* const filter = GTEST_FLAG(filter).c_str(); 2721f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 2722f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // Prints the filter if it's not *. This reminds the user that some 2723f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // tests may be skipped. 2724f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org if (!internal::String::CStringEquals(filter, kUniversalFilter)) { 2725f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org ColoredPrintf(COLOR_YELLOW, 2726f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org "Note: %s filter = %s\n", GTEST_NAME_, filter); 2727f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org } 2728f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 2729f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org if (internal::ShouldShard(kTestTotalShards, kTestShardIndex, false)) { 2730f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org const Int32 shard_index = Int32FromEnvOrDie(kTestShardIndex, -1); 2731f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org ColoredPrintf(COLOR_YELLOW, 2732f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org "Note: This is test shard %d of %s.\n", 2733f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org static_cast<int>(shard_index) + 1, 2734f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org internal::posix::GetEnv(kTestTotalShards)); 2735f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org } 2736f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 2737f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org if (GTEST_FLAG(shuffle)) { 2738f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org ColoredPrintf(COLOR_YELLOW, 2739f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org "Note: Randomizing tests' orders with a seed of %d .\n", 2740f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org unit_test.random_seed()); 2741f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org } 2742f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 2743f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org ColoredPrintf(COLOR_GREEN, "[==========] "); 2744f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org printf("Running %s from %s.\n", 2745f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org FormatTestCount(unit_test.test_to_run_count()).c_str(), 2746f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org FormatTestCaseCount(unit_test.test_case_to_run_count()).c_str()); 2747f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org fflush(stdout); 2748f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org} 2749f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 2750f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgvoid PrettyUnitTestResultPrinter::OnEnvironmentsSetUpStart( 2751f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org const UnitTest& /*unit_test*/) { 2752f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org ColoredPrintf(COLOR_GREEN, "[----------] "); 2753f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org printf("Global test environment set-up.\n"); 2754f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org fflush(stdout); 2755f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org} 2756f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 2757f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgvoid PrettyUnitTestResultPrinter::OnTestCaseStart(const TestCase& test_case) { 2758f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org test_case_name_ = test_case.name(); 2759f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org const internal::String counts = 2760f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org FormatCountableNoun(test_case.test_to_run_count(), "test", "tests"); 2761f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org ColoredPrintf(COLOR_GREEN, "[----------] "); 2762f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org printf("%s from %s", counts.c_str(), test_case_name_.c_str()); 2763f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org if (test_case.type_param() == NULL) { 2764f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org printf("\n"); 2765f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org } else { 2766f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org printf(", where TypeParam = %s\n", test_case.type_param()); 2767f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org } 2768f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org fflush(stdout); 2769f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org} 2770f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 2771f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgvoid PrettyUnitTestResultPrinter::OnTestStart(const TestInfo& test_info) { 2772f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org ColoredPrintf(COLOR_GREEN, "[ RUN ] "); 2773f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org PrintTestName(test_case_name_.c_str(), test_info.name()); 2774f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org printf("\n"); 2775f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org fflush(stdout); 2776f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org} 2777f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 2778f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// Called after an assertion failure. 2779f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgvoid PrettyUnitTestResultPrinter::OnTestPartResult( 2780f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org const TestPartResult& result) { 2781f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // If the test part succeeded, we don't need to do anything. 2782f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org if (result.type() == TestPartResult::kSuccess) 2783f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org return; 2784f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 2785f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // Print failure message from the assertion (e.g. expected this and got that). 2786f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org PrintTestPartResult(result); 2787f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org fflush(stdout); 2788f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org} 2789f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 2790f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgvoid PrettyUnitTestResultPrinter::OnTestEnd(const TestInfo& test_info) { 2791f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org if (test_info.result()->Passed()) { 2792f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org ColoredPrintf(COLOR_GREEN, "[ OK ] "); 2793f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org } else { 2794f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org ColoredPrintf(COLOR_RED, "[ FAILED ] "); 2795f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org } 2796f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org PrintTestName(test_case_name_.c_str(), test_info.name()); 2797f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org if (test_info.result()->Failed()) 2798f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org PrintFullTestCommentIfPresent(test_info); 2799f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 2800f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org if (GTEST_FLAG(print_time)) { 2801f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org printf(" (%s ms)\n", internal::StreamableToString( 2802f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org test_info.result()->elapsed_time()).c_str()); 2803f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org } else { 2804f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org printf("\n"); 2805f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org } 2806f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org fflush(stdout); 2807f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org} 2808f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 2809f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgvoid PrettyUnitTestResultPrinter::OnTestCaseEnd(const TestCase& test_case) { 2810f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org if (!GTEST_FLAG(print_time)) return; 2811f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 2812f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org test_case_name_ = test_case.name(); 2813f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org const internal::String counts = 2814f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org FormatCountableNoun(test_case.test_to_run_count(), "test", "tests"); 2815f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org ColoredPrintf(COLOR_GREEN, "[----------] "); 2816f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org printf("%s from %s (%s ms total)\n\n", 2817f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org counts.c_str(), test_case_name_.c_str(), 2818f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org internal::StreamableToString(test_case.elapsed_time()).c_str()); 2819f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org fflush(stdout); 2820f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org} 2821f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 2822f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgvoid PrettyUnitTestResultPrinter::OnEnvironmentsTearDownStart( 2823f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org const UnitTest& /*unit_test*/) { 2824f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org ColoredPrintf(COLOR_GREEN, "[----------] "); 2825f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org printf("Global test environment tear-down\n"); 2826f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org fflush(stdout); 2827f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org} 2828f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 2829f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// Internal helper for printing the list of failed tests. 2830f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgvoid PrettyUnitTestResultPrinter::PrintFailedTests(const UnitTest& unit_test) { 2831f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org const int failed_test_count = unit_test.failed_test_count(); 2832f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org if (failed_test_count == 0) { 2833f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org return; 2834f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org } 2835f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 2836f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org for (int i = 0; i < unit_test.total_test_case_count(); ++i) { 2837f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org const TestCase& test_case = *unit_test.GetTestCase(i); 2838f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org if (!test_case.should_run() || (test_case.failed_test_count() == 0)) { 2839f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org continue; 2840f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org } 2841f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org for (int j = 0; j < test_case.total_test_count(); ++j) { 2842f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org const TestInfo& test_info = *test_case.GetTestInfo(j); 2843f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org if (!test_info.should_run() || test_info.result()->Passed()) { 2844f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org continue; 2845f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org } 2846f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org ColoredPrintf(COLOR_RED, "[ FAILED ] "); 2847f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org printf("%s.%s", test_case.name(), test_info.name()); 2848f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org PrintFullTestCommentIfPresent(test_info); 2849f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org printf("\n"); 2850f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org } 2851f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org } 2852f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org} 2853f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 2854f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgvoid PrettyUnitTestResultPrinter::OnTestIterationEnd(const UnitTest& unit_test, 2855f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org int /*iteration*/) { 2856f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org ColoredPrintf(COLOR_GREEN, "[==========] "); 2857f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org printf("%s from %s ran.", 2858f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org FormatTestCount(unit_test.test_to_run_count()).c_str(), 2859f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org FormatTestCaseCount(unit_test.test_case_to_run_count()).c_str()); 2860f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org if (GTEST_FLAG(print_time)) { 2861f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org printf(" (%s ms total)", 2862f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org internal::StreamableToString(unit_test.elapsed_time()).c_str()); 2863f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org } 2864f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org printf("\n"); 2865f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org ColoredPrintf(COLOR_GREEN, "[ PASSED ] "); 2866f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org printf("%s.\n", FormatTestCount(unit_test.successful_test_count()).c_str()); 2867f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 2868f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org int num_failures = unit_test.failed_test_count(); 2869f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org if (!unit_test.Passed()) { 2870f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org const int failed_test_count = unit_test.failed_test_count(); 2871f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org ColoredPrintf(COLOR_RED, "[ FAILED ] "); 2872f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org printf("%s, listed below:\n", FormatTestCount(failed_test_count).c_str()); 2873f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org PrintFailedTests(unit_test); 2874f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org printf("\n%2d FAILED %s\n", num_failures, 2875f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org num_failures == 1 ? "TEST" : "TESTS"); 2876f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org } 2877f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 2878f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org int num_disabled = unit_test.disabled_test_count(); 2879f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org if (num_disabled && !GTEST_FLAG(also_run_disabled_tests)) { 2880f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org if (!num_failures) { 2881f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org printf("\n"); // Add a spacer if no FAILURE banner is displayed. 2882f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org } 2883f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org ColoredPrintf(COLOR_YELLOW, 2884f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org " YOU HAVE %d DISABLED %s\n\n", 2885f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org num_disabled, 2886f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org num_disabled == 1 ? "TEST" : "TESTS"); 2887f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org } 2888f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // Ensure that Google Test output is printed before, e.g., heapchecker output. 2889f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org fflush(stdout); 2890f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org} 2891f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 2892f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// End PrettyUnitTestResultPrinter 2893f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 2894f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// class TestEventRepeater 2895f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// 2896f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// This class forwards events to other event listeners. 2897f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgclass TestEventRepeater : public TestEventListener { 2898f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org public: 2899f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org TestEventRepeater() : forwarding_enabled_(true) {} 2900f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org virtual ~TestEventRepeater(); 2901f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org void Append(TestEventListener *listener); 2902f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org TestEventListener* Release(TestEventListener* listener); 2903f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 2904f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // Controls whether events will be forwarded to listeners_. Set to false 2905f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // in death test child processes. 2906f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org bool forwarding_enabled() const { return forwarding_enabled_; } 2907f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org void set_forwarding_enabled(bool enable) { forwarding_enabled_ = enable; } 2908f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 2909f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org virtual void OnTestProgramStart(const UnitTest& unit_test); 2910f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org virtual void OnTestIterationStart(const UnitTest& unit_test, int iteration); 2911f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org virtual void OnEnvironmentsSetUpStart(const UnitTest& unit_test); 2912f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org virtual void OnEnvironmentsSetUpEnd(const UnitTest& unit_test); 2913f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org virtual void OnTestCaseStart(const TestCase& test_case); 2914f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org virtual void OnTestStart(const TestInfo& test_info); 2915f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org virtual void OnTestPartResult(const TestPartResult& result); 2916f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org virtual void OnTestEnd(const TestInfo& test_info); 2917f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org virtual void OnTestCaseEnd(const TestCase& test_case); 2918f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org virtual void OnEnvironmentsTearDownStart(const UnitTest& unit_test); 2919f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org virtual void OnEnvironmentsTearDownEnd(const UnitTest& unit_test); 2920f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org virtual void OnTestIterationEnd(const UnitTest& unit_test, int iteration); 2921f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org virtual void OnTestProgramEnd(const UnitTest& unit_test); 2922f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 2923f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org private: 2924f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // Controls whether events will be forwarded to listeners_. Set to false 2925f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // in death test child processes. 2926f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org bool forwarding_enabled_; 2927f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // The list of listeners that receive events. 2928f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org std::vector<TestEventListener*> listeners_; 2929f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 2930f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org GTEST_DISALLOW_COPY_AND_ASSIGN_(TestEventRepeater); 2931f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org}; 2932f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 2933f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgTestEventRepeater::~TestEventRepeater() { 2934f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org ForEach(listeners_, Delete<TestEventListener>); 2935f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org} 2936f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 2937f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgvoid TestEventRepeater::Append(TestEventListener *listener) { 2938f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org listeners_.push_back(listener); 2939f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org} 2940f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 2941f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// TODO(vladl@google.com): Factor the search functionality into Vector::Find. 2942f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgTestEventListener* TestEventRepeater::Release(TestEventListener *listener) { 2943f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org for (size_t i = 0; i < listeners_.size(); ++i) { 2944f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org if (listeners_[i] == listener) { 2945f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org listeners_.erase(listeners_.begin() + i); 2946f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org return listener; 2947f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org } 2948f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org } 2949f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 2950f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org return NULL; 2951f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org} 2952f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 2953f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// Since most methods are very similar, use macros to reduce boilerplate. 2954f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// This defines a member that forwards the call to all listeners. 2955f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#define GTEST_REPEATER_METHOD_(Name, Type) \ 2956f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgvoid TestEventRepeater::Name(const Type& parameter) { \ 2957f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org if (forwarding_enabled_) { \ 2958f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org for (size_t i = 0; i < listeners_.size(); i++) { \ 2959f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org listeners_[i]->Name(parameter); \ 2960f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org } \ 2961f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org } \ 2962f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org} 2963f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// This defines a member that forwards the call to all listeners in reverse 2964f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// order. 2965f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#define GTEST_REVERSE_REPEATER_METHOD_(Name, Type) \ 2966f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgvoid TestEventRepeater::Name(const Type& parameter) { \ 2967f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org if (forwarding_enabled_) { \ 2968f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org for (int i = static_cast<int>(listeners_.size()) - 1; i >= 0; i--) { \ 2969f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org listeners_[i]->Name(parameter); \ 2970f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org } \ 2971f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org } \ 2972f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org} 2973f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 2974f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgGTEST_REPEATER_METHOD_(OnTestProgramStart, UnitTest) 2975f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgGTEST_REPEATER_METHOD_(OnEnvironmentsSetUpStart, UnitTest) 2976f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgGTEST_REPEATER_METHOD_(OnTestCaseStart, TestCase) 2977f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgGTEST_REPEATER_METHOD_(OnTestStart, TestInfo) 2978f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgGTEST_REPEATER_METHOD_(OnTestPartResult, TestPartResult) 2979f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgGTEST_REPEATER_METHOD_(OnEnvironmentsTearDownStart, UnitTest) 2980f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgGTEST_REVERSE_REPEATER_METHOD_(OnEnvironmentsSetUpEnd, UnitTest) 2981f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgGTEST_REVERSE_REPEATER_METHOD_(OnEnvironmentsTearDownEnd, UnitTest) 2982f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgGTEST_REVERSE_REPEATER_METHOD_(OnTestEnd, TestInfo) 2983f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgGTEST_REVERSE_REPEATER_METHOD_(OnTestCaseEnd, TestCase) 2984f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgGTEST_REVERSE_REPEATER_METHOD_(OnTestProgramEnd, UnitTest) 2985f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 2986f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#undef GTEST_REPEATER_METHOD_ 2987f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#undef GTEST_REVERSE_REPEATER_METHOD_ 2988f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 2989f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgvoid TestEventRepeater::OnTestIterationStart(const UnitTest& unit_test, 2990f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org int iteration) { 2991f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org if (forwarding_enabled_) { 2992f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org for (size_t i = 0; i < listeners_.size(); i++) { 2993f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org listeners_[i]->OnTestIterationStart(unit_test, iteration); 2994f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org } 2995f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org } 2996f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org} 2997f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 2998f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgvoid TestEventRepeater::OnTestIterationEnd(const UnitTest& unit_test, 2999f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org int iteration) { 3000f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org if (forwarding_enabled_) { 3001f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org for (int i = static_cast<int>(listeners_.size()) - 1; i >= 0; i--) { 3002f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org listeners_[i]->OnTestIterationEnd(unit_test, iteration); 3003f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org } 3004f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org } 3005f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org} 3006f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 3007f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// End TestEventRepeater 3008f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 3009f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// This class generates an XML output file. 3010f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgclass XmlUnitTestResultPrinter : public EmptyTestEventListener { 3011f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org public: 3012f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org explicit XmlUnitTestResultPrinter(const char* output_file); 3013f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 3014f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org virtual void OnTestIterationEnd(const UnitTest& unit_test, int iteration); 3015f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 3016f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org private: 3017f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // Is c a whitespace character that is normalized to a space character 3018f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // when it appears in an XML attribute value? 3019f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org static bool IsNormalizableWhitespace(char c) { 3020f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org return c == 0x9 || c == 0xA || c == 0xD; 3021f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org } 3022f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 3023f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // May c appear in a well-formed XML document? 3024f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org static bool IsValidXmlCharacter(char c) { 3025f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org return IsNormalizableWhitespace(c) || c >= 0x20; 3026f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org } 3027f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 3028f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // Returns an XML-escaped copy of the input string str. If 3029f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // is_attribute is true, the text is meant to appear as an attribute 3030f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // value, and normalizable whitespace is preserved by replacing it 3031f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // with character references. 3032f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org static String EscapeXml(const char* str, bool is_attribute); 3033f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 3034f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // Returns the given string with all characters invalid in XML removed. 3035f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org static string RemoveInvalidXmlCharacters(const string& str); 3036f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 3037f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // Convenience wrapper around EscapeXml when str is an attribute value. 3038f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org static String EscapeXmlAttribute(const char* str) { 3039f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org return EscapeXml(str, true); 3040f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org } 3041f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 3042f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // Convenience wrapper around EscapeXml when str is not an attribute value. 3043f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org static String EscapeXmlText(const char* str) { return EscapeXml(str, false); } 3044f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 3045f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // Streams an XML CDATA section, escaping invalid CDATA sequences as needed. 3046f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org static void OutputXmlCDataSection(::std::ostream* stream, const char* data); 3047f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 3048f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // Streams an XML representation of a TestInfo object. 3049f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org static void OutputXmlTestInfo(::std::ostream* stream, 3050f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org const char* test_case_name, 3051f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org const TestInfo& test_info); 3052f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 3053f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // Prints an XML representation of a TestCase object 3054f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org static void PrintXmlTestCase(FILE* out, const TestCase& test_case); 3055f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 3056f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // Prints an XML summary of unit_test to output stream out. 3057f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org static void PrintXmlUnitTest(FILE* out, const UnitTest& unit_test); 3058f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 3059f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // Produces a string representing the test properties in a result as space 3060f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // delimited XML attributes based on the property key="value" pairs. 3061f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // When the String is not empty, it includes a space at the beginning, 3062f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // to delimit this attribute from prior attributes. 3063f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org static String TestPropertiesAsXmlAttributes(const TestResult& result); 3064f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 3065f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // The output file. 3066f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org const String output_file_; 3067f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 3068f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org GTEST_DISALLOW_COPY_AND_ASSIGN_(XmlUnitTestResultPrinter); 3069f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org}; 3070f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 3071f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// Creates a new XmlUnitTestResultPrinter. 3072f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgXmlUnitTestResultPrinter::XmlUnitTestResultPrinter(const char* output_file) 3073f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org : output_file_(output_file) { 3074f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org if (output_file_.c_str() == NULL || output_file_.empty()) { 3075f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org fprintf(stderr, "XML output file may not be null\n"); 3076f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org fflush(stderr); 3077f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org exit(EXIT_FAILURE); 3078f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org } 3079f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org} 3080f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 3081f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// Called after the unit test ends. 3082f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgvoid XmlUnitTestResultPrinter::OnTestIterationEnd(const UnitTest& unit_test, 3083f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org int /*iteration*/) { 3084f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org FILE* xmlout = NULL; 3085f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org FilePath output_file(output_file_); 3086f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org FilePath output_dir(output_file.RemoveFileName()); 3087f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 3088f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org if (output_dir.CreateDirectoriesRecursively()) { 3089f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org xmlout = posix::FOpen(output_file_.c_str(), "w"); 3090f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org } 3091f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org if (xmlout == NULL) { 3092f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // TODO(wan): report the reason of the failure. 3093f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // 3094f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // We don't do it for now as: 3095f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // 3096f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // 1. There is no urgent need for it. 3097f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // 2. It's a bit involved to make the errno variable thread-safe on 3098f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // all three operating systems (Linux, Windows, and Mac OS). 3099f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // 3. To interpret the meaning of errno in a thread-safe way, 3100f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // we need the strerror_r() function, which is not available on 3101f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // Windows. 3102f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org fprintf(stderr, 3103f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org "Unable to open file \"%s\"\n", 3104f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org output_file_.c_str()); 3105f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org fflush(stderr); 3106f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org exit(EXIT_FAILURE); 3107f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org } 3108f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org PrintXmlUnitTest(xmlout, unit_test); 3109f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org fclose(xmlout); 3110f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org} 3111f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 3112f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// Returns an XML-escaped copy of the input string str. If is_attribute 3113f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// is true, the text is meant to appear as an attribute value, and 3114f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// normalizable whitespace is preserved by replacing it with character 3115f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// references. 3116f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// 3117f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// Invalid XML characters in str, if any, are stripped from the output. 3118f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// It is expected that most, if not all, of the text processed by this 3119f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// module will consist of ordinary English text. 3120f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// If this module is ever modified to produce version 1.1 XML output, 3121f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// most invalid characters can be retained using character references. 3122f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// TODO(wan): It might be nice to have a minimally invasive, human-readable 3123f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// escaping scheme for invalid characters, rather than dropping them. 3124f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgString XmlUnitTestResultPrinter::EscapeXml(const char* str, bool is_attribute) { 3125f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org Message m; 3126f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 3127f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org if (str != NULL) { 3128f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org for (const char* src = str; *src; ++src) { 3129f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org switch (*src) { 3130f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org case '<': 3131f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org m << "<"; 3132f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org break; 3133f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org case '>': 3134f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org m << ">"; 3135f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org break; 3136f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org case '&': 3137f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org m << "&"; 3138f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org break; 3139f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org case '\'': 3140f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org if (is_attribute) 3141f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org m << "'"; 3142f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org else 3143f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org m << '\''; 3144f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org break; 3145f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org case '"': 3146f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org if (is_attribute) 3147f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org m << """; 3148f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org else 3149f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org m << '"'; 3150f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org break; 3151f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org default: 3152f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org if (IsValidXmlCharacter(*src)) { 3153f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org if (is_attribute && IsNormalizableWhitespace(*src)) 3154f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org m << String::Format("&#x%02X;", unsigned(*src)); 3155f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org else 3156f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org m << *src; 3157f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org } 3158f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org break; 3159f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org } 3160f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org } 3161f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org } 3162f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 3163f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org return m.GetString(); 3164f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org} 3165f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 3166f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// Returns the given string with all characters invalid in XML removed. 3167f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// Currently invalid characters are dropped from the string. An 3168f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// alternative is to replace them with certain characters such as . or ?. 3169f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgstring XmlUnitTestResultPrinter::RemoveInvalidXmlCharacters(const string& str) { 3170f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org string output; 3171f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org output.reserve(str.size()); 3172f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org for (string::const_iterator it = str.begin(); it != str.end(); ++it) 3173f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org if (IsValidXmlCharacter(*it)) 3174f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org output.push_back(*it); 3175f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 3176f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org return output; 3177f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org} 3178f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 3179f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// The following routines generate an XML representation of a UnitTest 3180f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// object. 3181f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// 3182f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// This is how Google Test concepts map to the DTD: 3183f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// 3184f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// <testsuites name="AllTests"> <-- corresponds to a UnitTest object 3185f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// <testsuite name="testcase-name"> <-- corresponds to a TestCase object 3186f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// <testcase name="test-name"> <-- corresponds to a TestInfo object 3187f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// <failure message="...">...</failure> 3188f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// <failure message="...">...</failure> 3189f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// <failure message="...">...</failure> 3190f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// <-- individual assertion failures 3191f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// </testcase> 3192f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// </testsuite> 3193f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// </testsuites> 3194f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 3195f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// Formats the given time in milliseconds as seconds. 3196f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgstd::string FormatTimeInMillisAsSeconds(TimeInMillis ms) { 3197f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org ::std::stringstream ss; 3198f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org ss << ms/1000.0; 3199f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org return ss.str(); 3200f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org} 3201f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 3202f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// Streams an XML CDATA section, escaping invalid CDATA sequences as needed. 3203f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgvoid XmlUnitTestResultPrinter::OutputXmlCDataSection(::std::ostream* stream, 3204f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org const char* data) { 3205f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org const char* segment = data; 3206f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org *stream << "<![CDATA["; 3207f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org for (;;) { 3208f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org const char* const next_segment = strstr(segment, "]]>"); 3209f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org if (next_segment != NULL) { 3210f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org stream->write( 3211f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org segment, static_cast<std::streamsize>(next_segment - segment)); 3212f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org *stream << "]]>]]><![CDATA["; 3213f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org segment = next_segment + strlen("]]>"); 3214f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org } else { 3215f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org *stream << segment; 3216f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org break; 3217f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org } 3218f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org } 3219f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org *stream << "]]>"; 3220f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org} 3221f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 3222f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// Prints an XML representation of a TestInfo object. 3223f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// TODO(wan): There is also value in printing properties with the plain printer. 3224f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgvoid XmlUnitTestResultPrinter::OutputXmlTestInfo(::std::ostream* stream, 3225f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org const char* test_case_name, 3226f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org const TestInfo& test_info) { 3227f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org const TestResult& result = *test_info.result(); 3228f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org *stream << " <testcase name=\"" 3229f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org << EscapeXmlAttribute(test_info.name()).c_str() << "\""; 3230f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 3231f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org if (test_info.value_param() != NULL) { 3232f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org *stream << " value_param=\"" << EscapeXmlAttribute(test_info.value_param()) 3233f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org << "\""; 3234f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org } 3235f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org if (test_info.type_param() != NULL) { 3236f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org *stream << " type_param=\"" << EscapeXmlAttribute(test_info.type_param()) 3237f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org << "\""; 3238f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org } 3239f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 3240f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org *stream << " status=\"" 3241f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org << (test_info.should_run() ? "run" : "notrun") 3242f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org << "\" time=\"" 3243f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org << FormatTimeInMillisAsSeconds(result.elapsed_time()) 3244f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org << "\" classname=\"" << EscapeXmlAttribute(test_case_name).c_str() 3245f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org << "\"" << TestPropertiesAsXmlAttributes(result).c_str(); 3246f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 3247f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org int failures = 0; 3248f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org for (int i = 0; i < result.total_part_count(); ++i) { 3249f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org const TestPartResult& part = result.GetTestPartResult(i); 3250f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org if (part.failed()) { 3251f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org if (++failures == 1) 3252f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org *stream << ">\n"; 3253f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org *stream << " <failure message=\"" 3254f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org << EscapeXmlAttribute(part.summary()).c_str() 3255f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org << "\" type=\"\">"; 3256f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org const string location = internal::FormatCompilerIndependentFileLocation( 3257f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org part.file_name(), part.line_number()); 3258f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org const string message = location + "\n" + part.message(); 3259f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org OutputXmlCDataSection(stream, 3260f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org RemoveInvalidXmlCharacters(message).c_str()); 3261f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org *stream << "</failure>\n"; 3262f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org } 3263f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org } 3264f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 3265f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org if (failures == 0) 3266f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org *stream << " />\n"; 3267f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org else 3268f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org *stream << " </testcase>\n"; 3269f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org} 3270f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 3271f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// Prints an XML representation of a TestCase object 3272f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgvoid XmlUnitTestResultPrinter::PrintXmlTestCase(FILE* out, 3273f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org const TestCase& test_case) { 3274f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org fprintf(out, 3275f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org " <testsuite name=\"%s\" tests=\"%d\" failures=\"%d\" " 3276f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org "disabled=\"%d\" ", 3277f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org EscapeXmlAttribute(test_case.name()).c_str(), 3278f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org test_case.total_test_count(), 3279f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org test_case.failed_test_count(), 3280f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org test_case.disabled_test_count()); 3281f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org fprintf(out, 3282f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org "errors=\"0\" time=\"%s\">\n", 3283f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org FormatTimeInMillisAsSeconds(test_case.elapsed_time()).c_str()); 3284f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org for (int i = 0; i < test_case.total_test_count(); ++i) { 3285f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org ::std::stringstream stream; 3286f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org OutputXmlTestInfo(&stream, test_case.name(), *test_case.GetTestInfo(i)); 3287f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org fprintf(out, "%s", StringStreamToString(&stream).c_str()); 3288f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org } 3289f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org fprintf(out, " </testsuite>\n"); 3290f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org} 3291f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 3292f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// Prints an XML summary of unit_test to output stream out. 3293f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgvoid XmlUnitTestResultPrinter::PrintXmlUnitTest(FILE* out, 3294f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org const UnitTest& unit_test) { 3295f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org fprintf(out, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"); 3296f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org fprintf(out, 3297f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org "<testsuites tests=\"%d\" failures=\"%d\" disabled=\"%d\" " 3298f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org "errors=\"0\" time=\"%s\" ", 3299f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org unit_test.total_test_count(), 3300f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org unit_test.failed_test_count(), 3301f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org unit_test.disabled_test_count(), 3302f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org FormatTimeInMillisAsSeconds(unit_test.elapsed_time()).c_str()); 3303f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org if (GTEST_FLAG(shuffle)) { 3304f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org fprintf(out, "random_seed=\"%d\" ", unit_test.random_seed()); 3305f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org } 3306f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org fprintf(out, "name=\"AllTests\">\n"); 3307f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org for (int i = 0; i < unit_test.total_test_case_count(); ++i) 3308f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org PrintXmlTestCase(out, *unit_test.GetTestCase(i)); 3309f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org fprintf(out, "</testsuites>\n"); 3310f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org} 3311f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 3312f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// Produces a string representing the test properties in a result as space 3313f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// delimited XML attributes based on the property key="value" pairs. 3314f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgString XmlUnitTestResultPrinter::TestPropertiesAsXmlAttributes( 3315f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org const TestResult& result) { 3316f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org Message attributes; 3317f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org for (int i = 0; i < result.test_property_count(); ++i) { 3318f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org const TestProperty& property = result.GetTestProperty(i); 3319f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org attributes << " " << property.key() << "=" 3320f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org << "\"" << EscapeXmlAttribute(property.value()) << "\""; 3321f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org } 3322f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org return attributes.GetString(); 3323f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org} 3324f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 3325f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// End XmlUnitTestResultPrinter 3326f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 3327f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#if GTEST_CAN_STREAM_RESULTS_ 3328f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 3329f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// Streams test results to the given port on the given host machine. 3330f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgclass StreamingListener : public EmptyTestEventListener { 3331f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org public: 3332f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // Escapes '=', '&', '%', and '\n' characters in str as "%xx". 3333f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org static string UrlEncode(const char* str); 3334f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 3335f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org StreamingListener(const string& host, const string& port) 3336f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org : sockfd_(-1), host_name_(host), port_num_(port) { 3337f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org MakeConnection(); 3338f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org Send("gtest_streaming_protocol_version=1.0\n"); 3339f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org } 3340f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 3341f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org virtual ~StreamingListener() { 3342f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org if (sockfd_ != -1) 3343f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org CloseConnection(); 3344f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org } 3345f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 3346f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org void OnTestProgramStart(const UnitTest& /* unit_test */) { 3347f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org Send("event=TestProgramStart\n"); 3348f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org } 3349f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 3350f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org void OnTestProgramEnd(const UnitTest& unit_test) { 3351f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // Note that Google Test current only report elapsed time for each 3352f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // test iteration, not for the entire test program. 3353f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org Send(String::Format("event=TestProgramEnd&passed=%d\n", 3354f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org unit_test.Passed())); 3355f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 3356f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // Notify the streaming server to stop. 3357f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org CloseConnection(); 3358f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org } 3359f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 3360f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org void OnTestIterationStart(const UnitTest& /* unit_test */, int iteration) { 3361f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org Send(String::Format("event=TestIterationStart&iteration=%d\n", 3362f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org iteration)); 3363f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org } 3364f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 3365f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org void OnTestIterationEnd(const UnitTest& unit_test, int /* iteration */) { 3366f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org Send(String::Format("event=TestIterationEnd&passed=%d&elapsed_time=%sms\n", 3367f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org unit_test.Passed(), 3368f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org StreamableToString(unit_test.elapsed_time()).c_str())); 3369f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org } 3370f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 3371f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org void OnTestCaseStart(const TestCase& test_case) { 3372f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org Send(String::Format("event=TestCaseStart&name=%s\n", test_case.name())); 3373f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org } 3374f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 3375f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org void OnTestCaseEnd(const TestCase& test_case) { 3376f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org Send(String::Format("event=TestCaseEnd&passed=%d&elapsed_time=%sms\n", 3377f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org test_case.Passed(), 3378f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org StreamableToString(test_case.elapsed_time()).c_str())); 3379f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org } 3380f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 3381f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org void OnTestStart(const TestInfo& test_info) { 3382f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org Send(String::Format("event=TestStart&name=%s\n", test_info.name())); 3383f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org } 3384f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 3385f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org void OnTestEnd(const TestInfo& test_info) { 3386f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org Send(String::Format( 3387f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org "event=TestEnd&passed=%d&elapsed_time=%sms\n", 3388f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org (test_info.result())->Passed(), 3389f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org StreamableToString((test_info.result())->elapsed_time()).c_str())); 3390f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org } 3391f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 3392f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org void OnTestPartResult(const TestPartResult& test_part_result) { 3393f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org const char* file_name = test_part_result.file_name(); 3394f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org if (file_name == NULL) 3395f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org file_name = ""; 3396f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org Send(String::Format("event=TestPartResult&file=%s&line=%d&message=", 3397f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org UrlEncode(file_name).c_str(), 3398f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org test_part_result.line_number())); 3399f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org Send(UrlEncode(test_part_result.message()) + "\n"); 3400f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org } 3401f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 3402f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org private: 3403f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // Creates a client socket and connects to the server. 3404f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org void MakeConnection(); 3405f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 3406f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // Closes the socket. 3407f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org void CloseConnection() { 3408f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org GTEST_CHECK_(sockfd_ != -1) 3409f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org << "CloseConnection() can be called only when there is a connection."; 3410f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 3411f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org close(sockfd_); 3412f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org sockfd_ = -1; 3413f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org } 3414f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 3415f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // Sends a string to the socket. 3416f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org void Send(const string& message) { 3417f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org GTEST_CHECK_(sockfd_ != -1) 3418f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org << "Send() can be called only when there is a connection."; 3419f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 3420f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org const int len = static_cast<int>(message.length()); 3421f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org if (write(sockfd_, message.c_str(), len) != len) { 3422f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org GTEST_LOG_(WARNING) 3423f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org << "stream_result_to: failed to stream to " 3424f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org << host_name_ << ":" << port_num_; 3425f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org } 3426f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org } 3427f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 3428f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org int sockfd_; // socket file descriptor 3429f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org const string host_name_; 3430f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org const string port_num_; 3431f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 3432f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org GTEST_DISALLOW_COPY_AND_ASSIGN_(StreamingListener); 3433f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org}; // class StreamingListener 3434f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 3435f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// Checks if str contains '=', '&', '%' or '\n' characters. If yes, 3436f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// replaces them by "%xx" where xx is their hexadecimal value. For 3437f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// example, replaces "=" with "%3D". This algorithm is O(strlen(str)) 3438f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// in both time and space -- important as the input str may contain an 3439f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// arbitrarily long test failure message and stack trace. 3440f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgstring StreamingListener::UrlEncode(const char* str) { 3441f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org string result; 3442f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org result.reserve(strlen(str) + 1); 3443f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org for (char ch = *str; ch != '\0'; ch = *++str) { 3444f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org switch (ch) { 3445f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org case '%': 3446f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org case '=': 3447f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org case '&': 3448f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org case '\n': 3449f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org result.append(String::Format("%%%02x", static_cast<unsigned char>(ch))); 3450f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org break; 3451f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org default: 3452f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org result.push_back(ch); 3453f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org break; 3454f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org } 3455f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org } 3456f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org return result; 3457f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org} 3458f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 3459f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgvoid StreamingListener::MakeConnection() { 3460f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org GTEST_CHECK_(sockfd_ == -1) 3461f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org << "MakeConnection() can't be called when there is already a connection."; 3462f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 3463f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org addrinfo hints; 3464f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org memset(&hints, 0, sizeof(hints)); 3465f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org hints.ai_family = AF_UNSPEC; // To allow both IPv4 and IPv6 addresses. 3466f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org hints.ai_socktype = SOCK_STREAM; 3467f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org addrinfo* servinfo = NULL; 3468f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 3469f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // Use the getaddrinfo() to get a linked list of IP addresses for 3470f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // the given host name. 3471f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org const int error_num = getaddrinfo( 3472f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org host_name_.c_str(), port_num_.c_str(), &hints, &servinfo); 3473f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org if (error_num != 0) { 3474f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org GTEST_LOG_(WARNING) << "stream_result_to: getaddrinfo() failed: " 3475f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org << gai_strerror(error_num); 3476f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org } 3477f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 3478f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // Loop through all the results and connect to the first we can. 3479f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org for (addrinfo* cur_addr = servinfo; sockfd_ == -1 && cur_addr != NULL; 3480f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org cur_addr = cur_addr->ai_next) { 3481f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org sockfd_ = socket( 3482f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org cur_addr->ai_family, cur_addr->ai_socktype, cur_addr->ai_protocol); 3483f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org if (sockfd_ != -1) { 3484f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // Connect the client socket to the server socket. 3485f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org if (connect(sockfd_, cur_addr->ai_addr, cur_addr->ai_addrlen) == -1) { 3486f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org close(sockfd_); 3487f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org sockfd_ = -1; 3488f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org } 3489f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org } 3490f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org } 3491f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 3492f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org freeaddrinfo(servinfo); // all done with this structure 3493f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 3494f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org if (sockfd_ == -1) { 3495f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org GTEST_LOG_(WARNING) << "stream_result_to: failed to connect to " 3496f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org << host_name_ << ":" << port_num_; 3497f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org } 3498f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org} 3499f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 3500f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// End of class Streaming Listener 3501f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#endif // GTEST_CAN_STREAM_RESULTS__ 3502f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 3503f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// Class ScopedTrace 3504f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 3505f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// Pushes the given source file location and message onto a per-thread 3506f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// trace stack maintained by Google Test. 3507f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// L < UnitTest::mutex_ 3508f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgScopedTrace::ScopedTrace(const char* file, int line, const Message& message) { 3509f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org TraceInfo trace; 3510f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org trace.file = file; 3511f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org trace.line = line; 3512f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org trace.message = message.GetString(); 3513f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 3514f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org UnitTest::GetInstance()->PushGTestTrace(trace); 3515f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org} 3516f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 3517f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// Pops the info pushed by the c'tor. 3518f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// L < UnitTest::mutex_ 3519f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgScopedTrace::~ScopedTrace() { 3520f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org UnitTest::GetInstance()->PopGTestTrace(); 3521f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org} 3522f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 3523f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 3524f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// class OsStackTraceGetter 3525f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 3526f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// Returns the current OS stack trace as a String. Parameters: 3527f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// 3528f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// max_depth - the maximum number of stack frames to be included 3529f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// in the trace. 3530f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// skip_count - the number of top frames to be skipped; doesn't count 3531f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// against max_depth. 3532f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// 3533f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// L < mutex_ 3534f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// We use "L < mutex_" to denote that the function may acquire mutex_. 3535f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgString OsStackTraceGetter::CurrentStackTrace(int, int) { 3536f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org return String(""); 3537f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org} 3538f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 3539f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// L < mutex_ 3540f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgvoid OsStackTraceGetter::UponLeavingGTest() { 3541f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org} 3542f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 3543f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgconst char* const 3544f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgOsStackTraceGetter::kElidedFramesMarker = 3545f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org "... " GTEST_NAME_ " internal frames ..."; 3546f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 3547f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org} // namespace internal 3548f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 3549f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// class TestEventListeners 3550f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 3551f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgTestEventListeners::TestEventListeners() 3552f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org : repeater_(new internal::TestEventRepeater()), 3553f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org default_result_printer_(NULL), 3554f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org default_xml_generator_(NULL) { 3555f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org} 3556f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 3557f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgTestEventListeners::~TestEventListeners() { delete repeater_; } 3558f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 3559f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// Returns the standard listener responsible for the default console 3560f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// output. Can be removed from the listeners list to shut down default 3561f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// console output. Note that removing this object from the listener list 3562f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// with Release transfers its ownership to the user. 3563f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgvoid TestEventListeners::Append(TestEventListener* listener) { 3564f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org repeater_->Append(listener); 3565f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org} 3566f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 3567f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// Removes the given event listener from the list and returns it. It then 3568f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// becomes the caller's responsibility to delete the listener. Returns 3569f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// NULL if the listener is not found in the list. 3570f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgTestEventListener* TestEventListeners::Release(TestEventListener* listener) { 3571f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org if (listener == default_result_printer_) 3572f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org default_result_printer_ = NULL; 3573f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org else if (listener == default_xml_generator_) 3574f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org default_xml_generator_ = NULL; 3575f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org return repeater_->Release(listener); 3576f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org} 3577f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 3578f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// Returns repeater that broadcasts the TestEventListener events to all 3579f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// subscribers. 3580f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgTestEventListener* TestEventListeners::repeater() { return repeater_; } 3581f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 3582f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// Sets the default_result_printer attribute to the provided listener. 3583f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// The listener is also added to the listener list and previous 3584f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// default_result_printer is removed from it and deleted. The listener can 3585f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// also be NULL in which case it will not be added to the list. Does 3586f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// nothing if the previous and the current listener objects are the same. 3587f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgvoid TestEventListeners::SetDefaultResultPrinter(TestEventListener* listener) { 3588f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org if (default_result_printer_ != listener) { 3589f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // It is an error to pass this method a listener that is already in the 3590f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // list. 3591f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org delete Release(default_result_printer_); 3592f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org default_result_printer_ = listener; 3593f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org if (listener != NULL) 3594f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org Append(listener); 3595f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org } 3596f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org} 3597f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 3598f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// Sets the default_xml_generator attribute to the provided listener. The 3599f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// listener is also added to the listener list and previous 3600f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// default_xml_generator is removed from it and deleted. The listener can 3601f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// also be NULL in which case it will not be added to the list. Does 3602f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// nothing if the previous and the current listener objects are the same. 3603f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgvoid TestEventListeners::SetDefaultXmlGenerator(TestEventListener* listener) { 3604f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org if (default_xml_generator_ != listener) { 3605f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // It is an error to pass this method a listener that is already in the 3606f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // list. 3607f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org delete Release(default_xml_generator_); 3608f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org default_xml_generator_ = listener; 3609f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org if (listener != NULL) 3610f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org Append(listener); 3611f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org } 3612f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org} 3613f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 3614f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// Controls whether events will be forwarded by the repeater to the 3615f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// listeners in the list. 3616f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgbool TestEventListeners::EventForwardingEnabled() const { 3617f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org return repeater_->forwarding_enabled(); 3618f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org} 3619f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 3620f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgvoid TestEventListeners::SuppressEventForwarding() { 3621f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org repeater_->set_forwarding_enabled(false); 3622f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org} 3623f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 3624f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// class UnitTest 3625f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 3626f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// Gets the singleton UnitTest object. The first time this method is 3627f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// called, a UnitTest object is constructed and returned. Consecutive 3628f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// calls will return the same object. 3629f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// 3630f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// We don't protect this under mutex_ as a user is not supposed to 3631f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// call this before main() starts, from which point on the return 3632f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// value will never change. 3633f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgUnitTest * UnitTest::GetInstance() { 3634f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // When compiled with MSVC 7.1 in optimized mode, destroying the 3635f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // UnitTest object upon exiting the program messes up the exit code, 3636f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // causing successful tests to appear failed. We have to use a 3637f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // different implementation in this case to bypass the compiler bug. 3638f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // This implementation makes the compiler happy, at the cost of 3639f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // leaking the UnitTest object. 3640f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 3641f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // CodeGear C++Builder insists on a public destructor for the 3642f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // default implementation. Use this implementation to keep good OO 3643f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // design with private destructor. 3644f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 3645f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#if (_MSC_VER == 1310 && !defined(_DEBUG)) || defined(__BORLANDC__) 3646f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org static UnitTest* const instance = new UnitTest; 3647f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org return instance; 3648f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#else 3649f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org static UnitTest instance; 3650f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org return &instance; 3651f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#endif // (_MSC_VER == 1310 && !defined(_DEBUG)) || defined(__BORLANDC__) 3652f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org} 3653f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 3654f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// Gets the number of successful test cases. 3655f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgint UnitTest::successful_test_case_count() const { 3656f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org return impl()->successful_test_case_count(); 3657f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org} 3658f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 3659f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// Gets the number of failed test cases. 3660f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgint UnitTest::failed_test_case_count() const { 3661f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org return impl()->failed_test_case_count(); 3662f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org} 3663f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 3664f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// Gets the number of all test cases. 3665f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgint UnitTest::total_test_case_count() const { 3666f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org return impl()->total_test_case_count(); 3667f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org} 3668f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 3669f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// Gets the number of all test cases that contain at least one test 3670f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// that should run. 3671f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgint UnitTest::test_case_to_run_count() const { 3672f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org return impl()->test_case_to_run_count(); 3673f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org} 3674f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 3675f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// Gets the number of successful tests. 3676f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgint UnitTest::successful_test_count() const { 3677f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org return impl()->successful_test_count(); 3678f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org} 3679f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 3680f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// Gets the number of failed tests. 3681f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgint UnitTest::failed_test_count() const { return impl()->failed_test_count(); } 3682f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 3683f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// Gets the number of disabled tests. 3684f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgint UnitTest::disabled_test_count() const { 3685f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org return impl()->disabled_test_count(); 3686f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org} 3687f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 3688f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// Gets the number of all tests. 3689f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgint UnitTest::total_test_count() const { return impl()->total_test_count(); } 3690f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 3691f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// Gets the number of tests that should run. 3692f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgint UnitTest::test_to_run_count() const { return impl()->test_to_run_count(); } 3693f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 3694f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// Gets the elapsed time, in milliseconds. 3695f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orginternal::TimeInMillis UnitTest::elapsed_time() const { 3696f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org return impl()->elapsed_time(); 3697f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org} 3698f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 3699f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// Returns true iff the unit test passed (i.e. all test cases passed). 3700f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgbool UnitTest::Passed() const { return impl()->Passed(); } 3701f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 3702f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// Returns true iff the unit test failed (i.e. some test case failed 3703f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// or something outside of all tests failed). 3704f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgbool UnitTest::Failed() const { return impl()->Failed(); } 3705f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 3706f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// Gets the i-th test case among all the test cases. i can range from 0 to 3707f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// total_test_case_count() - 1. If i is not in that range, returns NULL. 3708f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgconst TestCase* UnitTest::GetTestCase(int i) const { 3709f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org return impl()->GetTestCase(i); 3710f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org} 3711f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 3712f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// Gets the i-th test case among all the test cases. i can range from 0 to 3713f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// total_test_case_count() - 1. If i is not in that range, returns NULL. 3714f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgTestCase* UnitTest::GetMutableTestCase(int i) { 3715f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org return impl()->GetMutableTestCase(i); 3716f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org} 3717f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 3718f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// Returns the list of event listeners that can be used to track events 3719f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// inside Google Test. 3720f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgTestEventListeners& UnitTest::listeners() { 3721f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org return *impl()->listeners(); 3722f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org} 3723f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 3724f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// Registers and returns a global test environment. When a test 3725f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// program is run, all global test environments will be set-up in the 3726f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// order they were registered. After all tests in the program have 3727f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// finished, all global test environments will be torn-down in the 3728f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// *reverse* order they were registered. 3729f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// 3730f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// The UnitTest object takes ownership of the given environment. 3731f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// 3732f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// We don't protect this under mutex_, as we only support calling it 3733f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// from the main thread. 3734f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgEnvironment* UnitTest::AddEnvironment(Environment* env) { 3735f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org if (env == NULL) { 3736f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org return NULL; 3737f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org } 3738f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 3739f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org impl_->environments().push_back(env); 3740f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org return env; 3741f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org} 3742f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 3743f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// Adds a TestPartResult to the current TestResult object. All Google Test 3744f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// assertion macros (e.g. ASSERT_TRUE, EXPECT_EQ, etc) eventually call 3745f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// this to report their results. The user code should use the 3746f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// assertion macros instead of calling this directly. 3747f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// L < mutex_ 3748f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgvoid UnitTest::AddTestPartResult(TestPartResult::Type result_type, 3749f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org const char* file_name, 3750f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org int line_number, 3751f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org const internal::String& message, 3752f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org const internal::String& os_stack_trace) { 3753f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org Message msg; 3754f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org msg << message; 3755f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 3756f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org internal::MutexLock lock(&mutex_); 3757f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org if (impl_->gtest_trace_stack().size() > 0) { 3758f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org msg << "\n" << GTEST_NAME_ << " trace:"; 3759f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 3760f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org for (int i = static_cast<int>(impl_->gtest_trace_stack().size()); 3761f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org i > 0; --i) { 3762f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org const internal::TraceInfo& trace = impl_->gtest_trace_stack()[i - 1]; 3763f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org msg << "\n" << internal::FormatFileLocation(trace.file, trace.line) 3764f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org << " " << trace.message; 3765f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org } 3766f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org } 3767f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 3768f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org if (os_stack_trace.c_str() != NULL && !os_stack_trace.empty()) { 3769f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org msg << internal::kStackTraceMarker << os_stack_trace; 3770f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org } 3771f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 3772f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org const TestPartResult result = 3773f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org TestPartResult(result_type, file_name, line_number, 3774f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org msg.GetString().c_str()); 3775f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org impl_->GetTestPartResultReporterForCurrentThread()-> 3776f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org ReportTestPartResult(result); 3777f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 3778f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org if (result_type != TestPartResult::kSuccess) { 3779f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // gtest_break_on_failure takes precedence over 3780f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // gtest_throw_on_failure. This allows a user to set the latter 3781f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // in the code (perhaps in order to use Google Test assertions 3782f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // with another testing framework) and specify the former on the 3783f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // command line for debugging. 3784f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org if (GTEST_FLAG(break_on_failure)) { 3785f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#if GTEST_OS_WINDOWS 3786f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // Using DebugBreak on Windows allows gtest to still break into a debugger 3787f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // when a failure happens and both the --gtest_break_on_failure and 3788f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // the --gtest_catch_exceptions flags are specified. 3789f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org DebugBreak(); 3790f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#else 3791f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // Dereference NULL through a volatile pointer to prevent the compiler 3792f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // from removing. We use this rather than abort() or __builtin_trap() for 3793f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // portability: Symbian doesn't implement abort() well, and some debuggers 3794f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // don't correctly trap abort(). 3795f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org *static_cast<volatile int*>(NULL) = 1; 3796f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#endif // GTEST_OS_WINDOWS 3797f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org } else if (GTEST_FLAG(throw_on_failure)) { 3798f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#if GTEST_HAS_EXCEPTIONS 3799f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org throw GoogleTestFailureException(result); 3800f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#else 3801f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // We cannot call abort() as it generates a pop-up in debug mode 3802f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // that cannot be suppressed in VC 7.1 or below. 3803f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org exit(1); 3804f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#endif 3805f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org } 3806f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org } 3807f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org} 3808f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 3809f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// Creates and adds a property to the current TestResult. If a property matching 3810f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// the supplied value already exists, updates its value instead. 3811f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgvoid UnitTest::RecordPropertyForCurrentTest(const char* key, 3812f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org const char* value) { 3813f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org const TestProperty test_property(key, value); 3814f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org impl_->current_test_result()->RecordProperty(test_property); 3815f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org} 3816f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 3817f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// Runs all tests in this UnitTest object and prints the result. 3818f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// Returns 0 if successful, or 1 otherwise. 3819f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// 3820f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// We don't protect this under mutex_, as we only support calling it 3821f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// from the main thread. 3822f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgint UnitTest::Run() { 3823f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // Captures the value of GTEST_FLAG(catch_exceptions). This value will be 3824f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // used for the duration of the program. 3825f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org impl()->set_catch_exceptions(GTEST_FLAG(catch_exceptions)); 3826f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 3827f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#if GTEST_HAS_SEH 3828f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org const bool in_death_test_child_process = 3829f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org internal::GTEST_FLAG(internal_run_death_test).length() > 0; 3830f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 3831f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // Either the user wants Google Test to catch exceptions thrown by the 3832f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // tests or this is executing in the context of death test child 3833f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // process. In either case the user does not want to see pop-up dialogs 3834f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // about crashes - they are expected. 3835f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org if (impl()->catch_exceptions() || in_death_test_child_process) { 3836f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 3837f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org# if !GTEST_OS_WINDOWS_MOBILE 3838f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // SetErrorMode doesn't exist on CE. 3839f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org SetErrorMode(SEM_FAILCRITICALERRORS | SEM_NOALIGNMENTFAULTEXCEPT | 3840f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org SEM_NOGPFAULTERRORBOX | SEM_NOOPENFILEERRORBOX); 3841f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org# endif // !GTEST_OS_WINDOWS_MOBILE 3842f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 3843f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org# if (defined(_MSC_VER) || GTEST_OS_WINDOWS_MINGW) && !GTEST_OS_WINDOWS_MOBILE 3844f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // Death test children can be terminated with _abort(). On Windows, 3845f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // _abort() can show a dialog with a warning message. This forces the 3846f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // abort message to go to stderr instead. 3847f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org _set_error_mode(_OUT_TO_STDERR); 3848f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org# endif 3849f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 3850f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org# if _MSC_VER >= 1400 && !GTEST_OS_WINDOWS_MOBILE 3851f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // In the debug version, Visual Studio pops up a separate dialog 3852f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // offering a choice to debug the aborted program. We need to suppress 3853f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // this dialog or it will pop up for every EXPECT/ASSERT_DEATH statement 3854f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // executed. Google Test will notify the user of any unexpected 3855f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // failure via stderr. 3856f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // 3857f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // VC++ doesn't define _set_abort_behavior() prior to the version 8.0. 3858f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // Users of prior VC versions shall suffer the agony and pain of 3859f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // clicking through the countless debug dialogs. 3860f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // TODO(vladl@google.com): find a way to suppress the abort dialog() in the 3861f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // debug mode when compiled with VC 7.1 or lower. 3862f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org if (!GTEST_FLAG(break_on_failure)) 3863f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org _set_abort_behavior( 3864f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 0x0, // Clear the following flags: 3865f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org _WRITE_ABORT_MSG | _CALL_REPORTFAULT); // pop-up window, core dump. 3866f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org# endif 3867f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 3868f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org } 3869f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#endif // GTEST_HAS_SEH 3870f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 3871f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org return internal::HandleExceptionsInMethodIfSupported( 3872f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org impl(), 3873f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org &internal::UnitTestImpl::RunAllTests, 3874f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org "auxiliary test code (environments or event listeners)") ? 0 : 1; 3875f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org} 3876f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 3877f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// Returns the working directory when the first TEST() or TEST_F() was 3878f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// executed. 3879f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgconst char* UnitTest::original_working_dir() const { 3880f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org return impl_->original_working_dir_.c_str(); 3881f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org} 3882f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 3883f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// Returns the TestCase object for the test that's currently running, 3884f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// or NULL if no test is running. 3885f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// L < mutex_ 3886f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgconst TestCase* UnitTest::current_test_case() const { 3887f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org internal::MutexLock lock(&mutex_); 3888f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org return impl_->current_test_case(); 3889f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org} 3890f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 3891f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// Returns the TestInfo object for the test that's currently running, 3892f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// or NULL if no test is running. 3893f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// L < mutex_ 3894f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgconst TestInfo* UnitTest::current_test_info() const { 3895f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org internal::MutexLock lock(&mutex_); 3896f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org return impl_->current_test_info(); 3897f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org} 3898f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 3899f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// Returns the random seed used at the start of the current test run. 3900f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgint UnitTest::random_seed() const { return impl_->random_seed(); } 3901f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 3902f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#if GTEST_HAS_PARAM_TEST 3903f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// Returns ParameterizedTestCaseRegistry object used to keep track of 3904f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// value-parameterized tests and instantiate and register them. 3905f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// L < mutex_ 3906f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orginternal::ParameterizedTestCaseRegistry& 3907f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org UnitTest::parameterized_test_registry() { 3908f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org return impl_->parameterized_test_registry(); 3909f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org} 3910f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#endif // GTEST_HAS_PARAM_TEST 3911f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 3912f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// Creates an empty UnitTest. 3913f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgUnitTest::UnitTest() { 3914f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org impl_ = new internal::UnitTestImpl(this); 3915f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org} 3916f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 3917f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// Destructor of UnitTest. 3918f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgUnitTest::~UnitTest() { 3919f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org delete impl_; 3920f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org} 3921f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 3922f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// Pushes a trace defined by SCOPED_TRACE() on to the per-thread 3923f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// Google Test trace stack. 3924f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// L < mutex_ 3925f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgvoid UnitTest::PushGTestTrace(const internal::TraceInfo& trace) { 3926f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org internal::MutexLock lock(&mutex_); 3927f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org impl_->gtest_trace_stack().push_back(trace); 3928f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org} 3929f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 3930f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// Pops a trace from the per-thread Google Test trace stack. 3931f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// L < mutex_ 3932f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgvoid UnitTest::PopGTestTrace() { 3933f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org internal::MutexLock lock(&mutex_); 3934f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org impl_->gtest_trace_stack().pop_back(); 3935f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org} 3936f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 3937f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgnamespace internal { 3938f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 3939f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgUnitTestImpl::UnitTestImpl(UnitTest* parent) 3940f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org : parent_(parent), 3941f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#ifdef _MSC_VER 3942f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org# pragma warning(push) // Saves the current warning state. 3943f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org# pragma warning(disable:4355) // Temporarily disables warning 4355 3944f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // (using this in initializer). 3945f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org default_global_test_part_result_reporter_(this), 3946f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org default_per_thread_test_part_result_reporter_(this), 3947f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org# pragma warning(pop) // Restores the warning state again. 3948f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#else 3949f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org default_global_test_part_result_reporter_(this), 3950f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org default_per_thread_test_part_result_reporter_(this), 3951f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#endif // _MSC_VER 3952f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org global_test_part_result_repoter_( 3953f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org &default_global_test_part_result_reporter_), 3954f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org per_thread_test_part_result_reporter_( 3955f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org &default_per_thread_test_part_result_reporter_), 3956f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#if GTEST_HAS_PARAM_TEST 3957f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org parameterized_test_registry_(), 3958f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org parameterized_tests_registered_(false), 3959f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#endif // GTEST_HAS_PARAM_TEST 3960f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org last_death_test_case_(-1), 3961f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org current_test_case_(NULL), 3962f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org current_test_info_(NULL), 3963f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org ad_hoc_test_result_(), 3964f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org os_stack_trace_getter_(NULL), 3965f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org post_flag_parse_init_performed_(false), 3966f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org random_seed_(0), // Will be overridden by the flag before first use. 3967f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org random_(0), // Will be reseeded before first use. 3968f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org elapsed_time_(0), 3969f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#if GTEST_HAS_DEATH_TEST 3970f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org internal_run_death_test_flag_(NULL), 3971f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org death_test_factory_(new DefaultDeathTestFactory), 3972f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#endif 3973f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // Will be overridden by the flag before first use. 3974f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org catch_exceptions_(false) { 3975f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org listeners()->SetDefaultResultPrinter(new PrettyUnitTestResultPrinter); 3976f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org} 3977f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 3978f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgUnitTestImpl::~UnitTestImpl() { 3979f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // Deletes every TestCase. 3980f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org ForEach(test_cases_, internal::Delete<TestCase>); 3981f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 3982f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // Deletes every Environment. 3983f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org ForEach(environments_, internal::Delete<Environment>); 3984f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 3985f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org delete os_stack_trace_getter_; 3986f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org} 3987f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 3988f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#if GTEST_HAS_DEATH_TEST 3989f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// Disables event forwarding if the control is currently in a death test 3990f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// subprocess. Must not be called before InitGoogleTest. 3991f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgvoid UnitTestImpl::SuppressTestEventsIfInSubprocess() { 3992f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org if (internal_run_death_test_flag_.get() != NULL) 3993f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org listeners()->SuppressEventForwarding(); 3994f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org} 3995f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#endif // GTEST_HAS_DEATH_TEST 3996f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 3997f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// Initializes event listeners performing XML output as specified by 3998f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// UnitTestOptions. Must not be called before InitGoogleTest. 3999f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgvoid UnitTestImpl::ConfigureXmlOutput() { 4000f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org const String& output_format = UnitTestOptions::GetOutputFormat(); 4001f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org if (output_format == "xml") { 4002f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org listeners()->SetDefaultXmlGenerator(new XmlUnitTestResultPrinter( 4003f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org UnitTestOptions::GetAbsolutePathToOutputFile().c_str())); 4004f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org } else if (output_format != "") { 4005f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org printf("WARNING: unrecognized output format \"%s\" ignored.\n", 4006f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org output_format.c_str()); 4007f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org fflush(stdout); 4008f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org } 4009f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org} 4010f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 4011f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#if GTEST_CAN_STREAM_RESULTS_ 4012f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// Initializes event listeners for streaming test results in String form. 4013f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// Must not be called before InitGoogleTest. 4014f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgvoid UnitTestImpl::ConfigureStreamingOutput() { 4015f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org const string& target = GTEST_FLAG(stream_result_to); 4016f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org if (!target.empty()) { 4017f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org const size_t pos = target.find(':'); 4018f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org if (pos != string::npos) { 4019f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org listeners()->Append(new StreamingListener(target.substr(0, pos), 4020f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org target.substr(pos+1))); 4021f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org } else { 4022f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org printf("WARNING: unrecognized streaming target \"%s\" ignored.\n", 4023f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org target.c_str()); 4024f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org fflush(stdout); 4025f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org } 4026f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org } 4027f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org} 4028f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#endif // GTEST_CAN_STREAM_RESULTS_ 4029f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 4030f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// Performs initialization dependent upon flag values obtained in 4031f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// ParseGoogleTestFlagsOnly. Is called from InitGoogleTest after the call to 4032f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// ParseGoogleTestFlagsOnly. In case a user neglects to call InitGoogleTest 4033f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// this function is also called from RunAllTests. Since this function can be 4034f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// called more than once, it has to be idempotent. 4035f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgvoid UnitTestImpl::PostFlagParsingInit() { 4036f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // Ensures that this function does not execute more than once. 4037f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org if (!post_flag_parse_init_performed_) { 4038f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org post_flag_parse_init_performed_ = true; 4039f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 4040f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#if GTEST_HAS_DEATH_TEST 4041f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org InitDeathTestSubprocessControlInfo(); 4042f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org SuppressTestEventsIfInSubprocess(); 4043f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#endif // GTEST_HAS_DEATH_TEST 4044f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 4045f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // Registers parameterized tests. This makes parameterized tests 4046f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // available to the UnitTest reflection API without running 4047f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // RUN_ALL_TESTS. 4048f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org RegisterParameterizedTests(); 4049f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 4050f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // Configures listeners for XML output. This makes it possible for users 4051f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // to shut down the default XML output before invoking RUN_ALL_TESTS. 4052f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org ConfigureXmlOutput(); 4053f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 4054f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#if GTEST_CAN_STREAM_RESULTS_ 4055f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // Configures listeners for streaming test results to the specified server. 4056f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org ConfigureStreamingOutput(); 4057f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#endif // GTEST_CAN_STREAM_RESULTS_ 4058f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org } 4059f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org} 4060f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 4061f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// A predicate that checks the name of a TestCase against a known 4062f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// value. 4063f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// 4064f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// This is used for implementation of the UnitTest class only. We put 4065f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// it in the anonymous namespace to prevent polluting the outer 4066f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// namespace. 4067f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// 4068f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// TestCaseNameIs is copyable. 4069f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgclass TestCaseNameIs { 4070f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org public: 4071f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // Constructor. 4072f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org explicit TestCaseNameIs(const String& name) 4073f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org : name_(name) {} 4074f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 4075f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // Returns true iff the name of test_case matches name_. 4076f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org bool operator()(const TestCase* test_case) const { 4077f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org return test_case != NULL && strcmp(test_case->name(), name_.c_str()) == 0; 4078f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org } 4079f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 4080f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org private: 4081f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org String name_; 4082f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org}; 4083f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 4084f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// Finds and returns a TestCase with the given name. If one doesn't 4085f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// exist, creates one and returns it. It's the CALLER'S 4086f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// RESPONSIBILITY to ensure that this function is only called WHEN THE 4087f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// TESTS ARE NOT SHUFFLED. 4088f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// 4089f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// Arguments: 4090f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// 4091f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// test_case_name: name of the test case 4092f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// type_param: the name of the test case's type parameter, or NULL if 4093f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// this is not a typed or a type-parameterized test case. 4094f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// set_up_tc: pointer to the function that sets up the test case 4095f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// tear_down_tc: pointer to the function that tears down the test case 4096f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgTestCase* UnitTestImpl::GetTestCase(const char* test_case_name, 4097f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org const char* type_param, 4098f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org Test::SetUpTestCaseFunc set_up_tc, 4099f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org Test::TearDownTestCaseFunc tear_down_tc) { 4100f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // Can we find a TestCase with the given name? 4101f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org const std::vector<TestCase*>::const_iterator test_case = 4102f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org std::find_if(test_cases_.begin(), test_cases_.end(), 4103f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org TestCaseNameIs(test_case_name)); 4104f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 4105f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org if (test_case != test_cases_.end()) 4106f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org return *test_case; 4107f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 4108f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // No. Let's create one. 4109f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org TestCase* const new_test_case = 4110f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org new TestCase(test_case_name, type_param, set_up_tc, tear_down_tc); 4111f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 4112f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // Is this a death test case? 4113f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org if (internal::UnitTestOptions::MatchesFilter(String(test_case_name), 4114f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org kDeathTestCaseFilter)) { 4115f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // Yes. Inserts the test case after the last death test case 4116f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // defined so far. This only works when the test cases haven't 4117f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // been shuffled. Otherwise we may end up running a death test 4118f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // after a non-death test. 4119f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org ++last_death_test_case_; 4120f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org test_cases_.insert(test_cases_.begin() + last_death_test_case_, 4121f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org new_test_case); 4122f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org } else { 4123f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // No. Appends to the end of the list. 4124f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org test_cases_.push_back(new_test_case); 4125f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org } 4126f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 4127f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org test_case_indices_.push_back(static_cast<int>(test_case_indices_.size())); 4128f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org return new_test_case; 4129f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org} 4130f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 4131f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// Helpers for setting up / tearing down the given environment. They 4132f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// are for use in the ForEach() function. 4133f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgstatic void SetUpEnvironment(Environment* env) { env->SetUp(); } 4134f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgstatic void TearDownEnvironment(Environment* env) { env->TearDown(); } 4135f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 4136f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// Runs all tests in this UnitTest object, prints the result, and 4137f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// returns true if all tests are successful. If any exception is 4138f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// thrown during a test, the test is considered to be failed, but the 4139f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// rest of the tests will still be run. 4140f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// 4141f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// When parameterized tests are enabled, it expands and registers 4142f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// parameterized tests first in RegisterParameterizedTests(). 4143f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// All other functions called from RunAllTests() may safely assume that 4144f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// parameterized tests are ready to be counted and run. 4145f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgbool UnitTestImpl::RunAllTests() { 4146f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // Makes sure InitGoogleTest() was called. 4147f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org if (!GTestIsInitialized()) { 4148f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org printf("%s", 4149f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org "\nThis test program did NOT call ::testing::InitGoogleTest " 4150f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org "before calling RUN_ALL_TESTS(). Please fix it.\n"); 4151f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org return false; 4152f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org } 4153f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 4154f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // Do not run any test if the --help flag was specified. 4155f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org if (g_help_flag) 4156f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org return true; 4157f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 4158f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // Repeats the call to the post-flag parsing initialization in case the 4159f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // user didn't call InitGoogleTest. 4160f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org PostFlagParsingInit(); 4161f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 4162f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // Even if sharding is not on, test runners may want to use the 4163f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // GTEST_SHARD_STATUS_FILE to query whether the test supports the sharding 4164f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // protocol. 4165f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org internal::WriteToShardStatusFileIfNeeded(); 4166f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 4167f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // True iff we are in a subprocess for running a thread-safe-style 4168f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // death test. 4169f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org bool in_subprocess_for_death_test = false; 4170f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 4171f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#if GTEST_HAS_DEATH_TEST 4172f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org in_subprocess_for_death_test = (internal_run_death_test_flag_.get() != NULL); 4173f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#endif // GTEST_HAS_DEATH_TEST 4174f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 4175f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org const bool should_shard = ShouldShard(kTestTotalShards, kTestShardIndex, 4176f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org in_subprocess_for_death_test); 4177f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 4178f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // Compares the full test names with the filter to decide which 4179f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // tests to run. 4180f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org const bool has_tests_to_run = FilterTests(should_shard 4181f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org ? HONOR_SHARDING_PROTOCOL 4182f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org : IGNORE_SHARDING_PROTOCOL) > 0; 4183f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 4184f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // Lists the tests and exits if the --gtest_list_tests flag was specified. 4185f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org if (GTEST_FLAG(list_tests)) { 4186f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // This must be called *after* FilterTests() has been called. 4187f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org ListTestsMatchingFilter(); 4188f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org return true; 4189f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org } 4190f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 4191f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org random_seed_ = GTEST_FLAG(shuffle) ? 4192f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org GetRandomSeedFromFlag(GTEST_FLAG(random_seed)) : 0; 4193f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 4194f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // True iff at least one test has failed. 4195f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org bool failed = false; 4196f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 4197f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org TestEventListener* repeater = listeners()->repeater(); 4198f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 4199f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org repeater->OnTestProgramStart(*parent_); 4200f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 4201f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // How many times to repeat the tests? We don't want to repeat them 4202f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // when we are inside the subprocess of a death test. 4203f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org const int repeat = in_subprocess_for_death_test ? 1 : GTEST_FLAG(repeat); 4204f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // Repeats forever if the repeat count is negative. 4205f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org const bool forever = repeat < 0; 4206f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org for (int i = 0; forever || i != repeat; i++) { 4207f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // We want to preserve failures generated by ad-hoc test 4208f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // assertions executed before RUN_ALL_TESTS(). 4209f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org ClearNonAdHocTestResult(); 4210f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 4211f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org const TimeInMillis start = GetTimeInMillis(); 4212f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 4213f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // Shuffles test cases and tests if requested. 4214f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org if (has_tests_to_run && GTEST_FLAG(shuffle)) { 4215f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org random()->Reseed(random_seed_); 4216f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // This should be done before calling OnTestIterationStart(), 4217f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // such that a test event listener can see the actual test order 4218f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // in the event. 4219f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org ShuffleTests(); 4220f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org } 4221f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 4222f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // Tells the unit test event listeners that the tests are about to start. 4223f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org repeater->OnTestIterationStart(*parent_, i); 4224f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 4225f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // Runs each test case if there is at least one test to run. 4226f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org if (has_tests_to_run) { 4227f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // Sets up all environments beforehand. 4228f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org repeater->OnEnvironmentsSetUpStart(*parent_); 4229f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org ForEach(environments_, SetUpEnvironment); 4230f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org repeater->OnEnvironmentsSetUpEnd(*parent_); 4231f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 4232f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // Runs the tests only if there was no fatal failure during global 4233f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // set-up. 4234f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org if (!Test::HasFatalFailure()) { 4235f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org for (int test_index = 0; test_index < total_test_case_count(); 4236f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org test_index++) { 4237f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org GetMutableTestCase(test_index)->Run(); 4238f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org } 4239f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org } 4240f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 4241f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // Tears down all environments in reverse order afterwards. 4242f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org repeater->OnEnvironmentsTearDownStart(*parent_); 4243f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org std::for_each(environments_.rbegin(), environments_.rend(), 4244f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org TearDownEnvironment); 4245f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org repeater->OnEnvironmentsTearDownEnd(*parent_); 4246f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org } 4247f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 4248f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org elapsed_time_ = GetTimeInMillis() - start; 4249f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 4250f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // Tells the unit test event listener that the tests have just finished. 4251f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org repeater->OnTestIterationEnd(*parent_, i); 4252f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 4253f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // Gets the result and clears it. 4254f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org if (!Passed()) { 4255f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org failed = true; 4256f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org } 4257f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 4258f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // Restores the original test order after the iteration. This 4259f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // allows the user to quickly repro a failure that happens in the 4260f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // N-th iteration without repeating the first (N - 1) iterations. 4261f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // This is not enclosed in "if (GTEST_FLAG(shuffle)) { ... }", in 4262f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // case the user somehow changes the value of the flag somewhere 4263f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // (it's always safe to unshuffle the tests). 4264f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org UnshuffleTests(); 4265f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 4266f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org if (GTEST_FLAG(shuffle)) { 4267f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // Picks a new random seed for each iteration. 4268f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org random_seed_ = GetNextRandomSeed(random_seed_); 4269f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org } 4270f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org } 4271f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 4272f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org repeater->OnTestProgramEnd(*parent_); 4273f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 4274f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org return !failed; 4275f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org} 4276f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 4277f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// Reads the GTEST_SHARD_STATUS_FILE environment variable, and creates the file 4278f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// if the variable is present. If a file already exists at this location, this 4279f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// function will write over it. If the variable is present, but the file cannot 4280f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// be created, prints an error and exits. 4281f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgvoid WriteToShardStatusFileIfNeeded() { 4282f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org const char* const test_shard_file = posix::GetEnv(kTestShardStatusFile); 4283f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org if (test_shard_file != NULL) { 4284f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org FILE* const file = posix::FOpen(test_shard_file, "w"); 4285f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org if (file == NULL) { 4286f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org ColoredPrintf(COLOR_RED, 4287f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org "Could not write to the test shard status file \"%s\" " 4288f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org "specified by the %s environment variable.\n", 4289f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org test_shard_file, kTestShardStatusFile); 4290f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org fflush(stdout); 4291f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org exit(EXIT_FAILURE); 4292f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org } 4293f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org fclose(file); 4294f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org } 4295f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org} 4296f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 4297f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// Checks whether sharding is enabled by examining the relevant 4298f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// environment variable values. If the variables are present, 4299f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// but inconsistent (i.e., shard_index >= total_shards), prints 4300f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// an error and exits. If in_subprocess_for_death_test, sharding is 4301f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// disabled because it must only be applied to the original test 4302f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// process. Otherwise, we could filter out death tests we intended to execute. 4303f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgbool ShouldShard(const char* total_shards_env, 4304f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org const char* shard_index_env, 4305f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org bool in_subprocess_for_death_test) { 4306f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org if (in_subprocess_for_death_test) { 4307f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org return false; 4308f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org } 4309f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 4310f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org const Int32 total_shards = Int32FromEnvOrDie(total_shards_env, -1); 4311f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org const Int32 shard_index = Int32FromEnvOrDie(shard_index_env, -1); 4312f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 4313f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org if (total_shards == -1 && shard_index == -1) { 4314f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org return false; 4315f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org } else if (total_shards == -1 && shard_index != -1) { 4316f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org const Message msg = Message() 4317f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org << "Invalid environment variables: you have " 4318f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org << kTestShardIndex << " = " << shard_index 4319f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org << ", but have left " << kTestTotalShards << " unset.\n"; 4320f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org ColoredPrintf(COLOR_RED, msg.GetString().c_str()); 4321f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org fflush(stdout); 4322f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org exit(EXIT_FAILURE); 4323f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org } else if (total_shards != -1 && shard_index == -1) { 4324f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org const Message msg = Message() 4325f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org << "Invalid environment variables: you have " 4326f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org << kTestTotalShards << " = " << total_shards 4327f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org << ", but have left " << kTestShardIndex << " unset.\n"; 4328f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org ColoredPrintf(COLOR_RED, msg.GetString().c_str()); 4329f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org fflush(stdout); 4330f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org exit(EXIT_FAILURE); 4331f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org } else if (shard_index < 0 || shard_index >= total_shards) { 4332f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org const Message msg = Message() 4333f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org << "Invalid environment variables: we require 0 <= " 4334f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org << kTestShardIndex << " < " << kTestTotalShards 4335f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org << ", but you have " << kTestShardIndex << "=" << shard_index 4336f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org << ", " << kTestTotalShards << "=" << total_shards << ".\n"; 4337f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org ColoredPrintf(COLOR_RED, msg.GetString().c_str()); 4338f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org fflush(stdout); 4339f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org exit(EXIT_FAILURE); 4340f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org } 4341f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 4342f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org return total_shards > 1; 4343f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org} 4344f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 4345f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// Parses the environment variable var as an Int32. If it is unset, 4346f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// returns default_val. If it is not an Int32, prints an error 4347f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// and aborts. 4348f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgInt32 Int32FromEnvOrDie(const char* var, Int32 default_val) { 4349f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org const char* str_val = posix::GetEnv(var); 4350f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org if (str_val == NULL) { 4351f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org return default_val; 4352f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org } 4353f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 4354f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org Int32 result; 4355f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org if (!ParseInt32(Message() << "The value of environment variable " << var, 4356f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org str_val, &result)) { 4357f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org exit(EXIT_FAILURE); 4358f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org } 4359f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org return result; 4360f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org} 4361f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 4362f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// Given the total number of shards, the shard index, and the test id, 4363f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// returns true iff the test should be run on this shard. The test id is 4364f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// some arbitrary but unique non-negative integer assigned to each test 4365f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// method. Assumes that 0 <= shard_index < total_shards. 4366f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgbool ShouldRunTestOnShard(int total_shards, int shard_index, int test_id) { 4367f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org return (test_id % total_shards) == shard_index; 4368f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org} 4369f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 4370f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// Compares the name of each test with the user-specified filter to 4371f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// decide whether the test should be run, then records the result in 4372f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// each TestCase and TestInfo object. 4373f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// If shard_tests == true, further filters tests based on sharding 4374f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// variables in the environment - see 4375f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// http://code.google.com/p/googletest/wiki/GoogleTestAdvancedGuide. 4376f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// Returns the number of tests that should run. 4377f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgint UnitTestImpl::FilterTests(ReactionToSharding shard_tests) { 4378f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org const Int32 total_shards = shard_tests == HONOR_SHARDING_PROTOCOL ? 4379f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org Int32FromEnvOrDie(kTestTotalShards, -1) : -1; 4380f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org const Int32 shard_index = shard_tests == HONOR_SHARDING_PROTOCOL ? 4381f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org Int32FromEnvOrDie(kTestShardIndex, -1) : -1; 4382f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 4383f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // num_runnable_tests are the number of tests that will 4384f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // run across all shards (i.e., match filter and are not disabled). 4385f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // num_selected_tests are the number of tests to be run on 4386f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // this shard. 4387f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org int num_runnable_tests = 0; 4388f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org int num_selected_tests = 0; 4389f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org for (size_t i = 0; i < test_cases_.size(); i++) { 4390f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org TestCase* const test_case = test_cases_[i]; 4391f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org const String &test_case_name = test_case->name(); 4392f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org test_case->set_should_run(false); 4393f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 4394f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org for (size_t j = 0; j < test_case->test_info_list().size(); j++) { 4395f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org TestInfo* const test_info = test_case->test_info_list()[j]; 4396f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org const String test_name(test_info->name()); 4397f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // A test is disabled if test case name or test name matches 4398f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // kDisableTestFilter. 4399f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org const bool is_disabled = 4400f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org internal::UnitTestOptions::MatchesFilter(test_case_name, 4401f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org kDisableTestFilter) || 4402f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org internal::UnitTestOptions::MatchesFilter(test_name, 4403f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org kDisableTestFilter); 4404f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org test_info->is_disabled_ = is_disabled; 4405f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 4406f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org const bool matches_filter = 4407f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org internal::UnitTestOptions::FilterMatchesTest(test_case_name, 4408f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org test_name); 4409f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org test_info->matches_filter_ = matches_filter; 4410f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 4411f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org const bool is_runnable = 4412f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org (GTEST_FLAG(also_run_disabled_tests) || !is_disabled) && 4413f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org matches_filter; 4414f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 4415f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org const bool is_selected = is_runnable && 4416f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org (shard_tests == IGNORE_SHARDING_PROTOCOL || 4417f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org ShouldRunTestOnShard(total_shards, shard_index, 4418f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org num_runnable_tests)); 4419f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 4420f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org num_runnable_tests += is_runnable; 4421f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org num_selected_tests += is_selected; 4422f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 4423f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org test_info->should_run_ = is_selected; 4424f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org test_case->set_should_run(test_case->should_run() || is_selected); 4425f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org } 4426f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org } 4427f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org return num_selected_tests; 4428f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org} 4429f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 4430f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// Prints the names of the tests matching the user-specified filter flag. 4431f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgvoid UnitTestImpl::ListTestsMatchingFilter() { 4432f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org for (size_t i = 0; i < test_cases_.size(); i++) { 4433f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org const TestCase* const test_case = test_cases_[i]; 4434f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org bool printed_test_case_name = false; 4435f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 4436f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org for (size_t j = 0; j < test_case->test_info_list().size(); j++) { 4437f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org const TestInfo* const test_info = 4438f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org test_case->test_info_list()[j]; 4439f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org if (test_info->matches_filter_) { 4440f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org if (!printed_test_case_name) { 4441f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org printed_test_case_name = true; 4442f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org printf("%s.\n", test_case->name()); 4443f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org } 4444f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org printf(" %s\n", test_info->name()); 4445f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org } 4446f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org } 4447f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org } 4448f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org fflush(stdout); 4449f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org} 4450f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 4451f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// Sets the OS stack trace getter. 4452f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// 4453f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// Does nothing if the input and the current OS stack trace getter are 4454f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// the same; otherwise, deletes the old getter and makes the input the 4455f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// current getter. 4456f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgvoid UnitTestImpl::set_os_stack_trace_getter( 4457f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org OsStackTraceGetterInterface* getter) { 4458f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org if (os_stack_trace_getter_ != getter) { 4459f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org delete os_stack_trace_getter_; 4460f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org os_stack_trace_getter_ = getter; 4461f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org } 4462f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org} 4463f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 4464f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// Returns the current OS stack trace getter if it is not NULL; 4465f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// otherwise, creates an OsStackTraceGetter, makes it the current 4466f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// getter, and returns it. 4467f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgOsStackTraceGetterInterface* UnitTestImpl::os_stack_trace_getter() { 4468f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org if (os_stack_trace_getter_ == NULL) { 4469f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org os_stack_trace_getter_ = new OsStackTraceGetter; 4470f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org } 4471f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 4472f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org return os_stack_trace_getter_; 4473f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org} 4474f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 4475f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// Returns the TestResult for the test that's currently running, or 4476f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// the TestResult for the ad hoc test if no test is running. 4477f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgTestResult* UnitTestImpl::current_test_result() { 4478f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org return current_test_info_ ? 4479f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org &(current_test_info_->result_) : &ad_hoc_test_result_; 4480f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org} 4481f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 4482f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// Shuffles all test cases, and the tests within each test case, 4483f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// making sure that death tests are still run first. 4484f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgvoid UnitTestImpl::ShuffleTests() { 4485f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // Shuffles the death test cases. 4486f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org ShuffleRange(random(), 0, last_death_test_case_ + 1, &test_case_indices_); 4487f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 4488f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // Shuffles the non-death test cases. 4489f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org ShuffleRange(random(), last_death_test_case_ + 1, 4490f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org static_cast<int>(test_cases_.size()), &test_case_indices_); 4491f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 4492f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // Shuffles the tests inside each test case. 4493f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org for (size_t i = 0; i < test_cases_.size(); i++) { 4494f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org test_cases_[i]->ShuffleTests(random()); 4495f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org } 4496f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org} 4497f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 4498f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// Restores the test cases and tests to their order before the first shuffle. 4499f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgvoid UnitTestImpl::UnshuffleTests() { 4500f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org for (size_t i = 0; i < test_cases_.size(); i++) { 4501f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // Unshuffles the tests in each test case. 4502f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org test_cases_[i]->UnshuffleTests(); 4503f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // Resets the index of each test case. 4504f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org test_case_indices_[i] = static_cast<int>(i); 4505f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org } 4506f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org} 4507f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 4508f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// Returns the current OS stack trace as a String. 4509f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// 4510f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// The maximum number of stack frames to be included is specified by 4511f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// the gtest_stack_trace_depth flag. The skip_count parameter 4512f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// specifies the number of top frames to be skipped, which doesn't 4513f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// count against the number of frames to be included. 4514f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// 4515f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// For example, if Foo() calls Bar(), which in turn calls 4516f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// GetCurrentOsStackTraceExceptTop(..., 1), Foo() will be included in 4517f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// the trace but Bar() and GetCurrentOsStackTraceExceptTop() won't. 4518f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgString GetCurrentOsStackTraceExceptTop(UnitTest* /*unit_test*/, 4519f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org int skip_count) { 4520f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // We pass skip_count + 1 to skip this wrapper function in addition 4521f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // to what the user really wants to skip. 4522f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org return GetUnitTestImpl()->CurrentOsStackTraceExceptTop(skip_count + 1); 4523f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org} 4524f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 4525f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// Used by the GTEST_SUPPRESS_UNREACHABLE_CODE_WARNING_BELOW_ macro to 4526f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// suppress unreachable code warnings. 4527f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgnamespace { 4528f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgclass ClassUniqueToAlwaysTrue {}; 4529f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org} 4530f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 4531f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgbool IsTrue(bool condition) { return condition; } 4532f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 4533f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgbool AlwaysTrue() { 4534f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#if GTEST_HAS_EXCEPTIONS 4535f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // This condition is always false so AlwaysTrue() never actually throws, 4536f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // but it makes the compiler think that it may throw. 4537f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org if (IsTrue(false)) 4538f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org throw ClassUniqueToAlwaysTrue(); 4539f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#endif // GTEST_HAS_EXCEPTIONS 4540f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org return true; 4541f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org} 4542f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 4543f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// If *pstr starts with the given prefix, modifies *pstr to be right 4544f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// past the prefix and returns true; otherwise leaves *pstr unchanged 4545f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// and returns false. None of pstr, *pstr, and prefix can be NULL. 4546f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgbool SkipPrefix(const char* prefix, const char** pstr) { 4547f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org const size_t prefix_len = strlen(prefix); 4548f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org if (strncmp(*pstr, prefix, prefix_len) == 0) { 4549f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org *pstr += prefix_len; 4550f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org return true; 4551f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org } 4552f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org return false; 4553f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org} 4554f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 4555f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// Parses a string as a command line flag. The string should have 4556f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// the format "--flag=value". When def_optional is true, the "=value" 4557f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// part can be omitted. 4558f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// 4559f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// Returns the value of the flag, or NULL if the parsing failed. 4560f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgconst char* ParseFlagValue(const char* str, 4561f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org const char* flag, 4562f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org bool def_optional) { 4563f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // str and flag must not be NULL. 4564f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org if (str == NULL || flag == NULL) return NULL; 4565f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 4566f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // The flag must start with "--" followed by GTEST_FLAG_PREFIX_. 4567f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org const String flag_str = String::Format("--%s%s", GTEST_FLAG_PREFIX_, flag); 4568f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org const size_t flag_len = flag_str.length(); 4569f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org if (strncmp(str, flag_str.c_str(), flag_len) != 0) return NULL; 4570f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 4571f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // Skips the flag name. 4572f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org const char* flag_end = str + flag_len; 4573f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 4574f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // When def_optional is true, it's OK to not have a "=value" part. 4575f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org if (def_optional && (flag_end[0] == '\0')) { 4576f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org return flag_end; 4577f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org } 4578f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 4579f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // If def_optional is true and there are more characters after the 4580f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // flag name, or if def_optional is false, there must be a '=' after 4581f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // the flag name. 4582f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org if (flag_end[0] != '=') return NULL; 4583f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 4584f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // Returns the string after "=". 4585f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org return flag_end + 1; 4586f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org} 4587f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 4588f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// Parses a string for a bool flag, in the form of either 4589f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// "--flag=value" or "--flag". 4590f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// 4591f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// In the former case, the value is taken as true as long as it does 4592f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// not start with '0', 'f', or 'F'. 4593f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// 4594f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// In the latter case, the value is taken as true. 4595f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// 4596f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// On success, stores the value of the flag in *value, and returns 4597f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// true. On failure, returns false without changing *value. 4598f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgbool ParseBoolFlag(const char* str, const char* flag, bool* value) { 4599f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // Gets the value of the flag as a string. 4600f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org const char* const value_str = ParseFlagValue(str, flag, true); 4601f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 4602f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // Aborts if the parsing failed. 4603f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org if (value_str == NULL) return false; 4604f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 4605f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // Converts the string value to a bool. 4606f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org *value = !(*value_str == '0' || *value_str == 'f' || *value_str == 'F'); 4607f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org return true; 4608f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org} 4609f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 4610f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// Parses a string for an Int32 flag, in the form of 4611f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// "--flag=value". 4612f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// 4613f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// On success, stores the value of the flag in *value, and returns 4614f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// true. On failure, returns false without changing *value. 4615f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgbool ParseInt32Flag(const char* str, const char* flag, Int32* value) { 4616f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // Gets the value of the flag as a string. 4617f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org const char* const value_str = ParseFlagValue(str, flag, false); 4618f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 4619f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // Aborts if the parsing failed. 4620f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org if (value_str == NULL) return false; 4621f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 4622f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // Sets *value to the value of the flag. 4623f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org return ParseInt32(Message() << "The value of flag --" << flag, 4624f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org value_str, value); 4625f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org} 4626f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 4627f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// Parses a string for a string flag, in the form of 4628f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// "--flag=value". 4629f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// 4630f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// On success, stores the value of the flag in *value, and returns 4631f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// true. On failure, returns false without changing *value. 4632f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgbool ParseStringFlag(const char* str, const char* flag, String* value) { 4633f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // Gets the value of the flag as a string. 4634f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org const char* const value_str = ParseFlagValue(str, flag, false); 4635f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 4636f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // Aborts if the parsing failed. 4637f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org if (value_str == NULL) return false; 4638f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 4639f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // Sets *value to the value of the flag. 4640f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org *value = value_str; 4641f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org return true; 4642f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org} 4643f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 4644f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// Determines whether a string has a prefix that Google Test uses for its 4645f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// flags, i.e., starts with GTEST_FLAG_PREFIX_ or GTEST_FLAG_PREFIX_DASH_. 4646f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// If Google Test detects that a command line flag has its prefix but is not 4647f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// recognized, it will print its help message. Flags starting with 4648f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// GTEST_INTERNAL_PREFIX_ followed by "internal_" are considered Google Test 4649f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// internal flags and do not trigger the help message. 4650f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgstatic bool HasGoogleTestFlagPrefix(const char* str) { 4651f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org return (SkipPrefix("--", &str) || 4652f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org SkipPrefix("-", &str) || 4653f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org SkipPrefix("/", &str)) && 4654f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org !SkipPrefix(GTEST_FLAG_PREFIX_ "internal_", &str) && 4655f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org (SkipPrefix(GTEST_FLAG_PREFIX_, &str) || 4656f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org SkipPrefix(GTEST_FLAG_PREFIX_DASH_, &str)); 4657f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org} 4658f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 4659f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// Prints a string containing code-encoded text. The following escape 4660f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// sequences can be used in the string to control the text color: 4661f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// 4662f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// @@ prints a single '@' character. 4663f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// @R changes the color to red. 4664f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// @G changes the color to green. 4665f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// @Y changes the color to yellow. 4666f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// @D changes to the default terminal text color. 4667f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// 4668f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// TODO(wan@google.com): Write tests for this once we add stdout 4669f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// capturing to Google Test. 4670f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgstatic void PrintColorEncoded(const char* str) { 4671f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org GTestColor color = COLOR_DEFAULT; // The current color. 4672f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 4673f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // Conceptually, we split the string into segments divided by escape 4674f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // sequences. Then we print one segment at a time. At the end of 4675f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // each iteration, the str pointer advances to the beginning of the 4676f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // next segment. 4677f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org for (;;) { 4678f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org const char* p = strchr(str, '@'); 4679f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org if (p == NULL) { 4680f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org ColoredPrintf(color, "%s", str); 4681f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org return; 4682f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org } 4683f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 4684f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org ColoredPrintf(color, "%s", String(str, p - str).c_str()); 4685f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 4686f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org const char ch = p[1]; 4687f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org str = p + 2; 4688f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org if (ch == '@') { 4689f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org ColoredPrintf(color, "@"); 4690f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org } else if (ch == 'D') { 4691f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org color = COLOR_DEFAULT; 4692f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org } else if (ch == 'R') { 4693f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org color = COLOR_RED; 4694f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org } else if (ch == 'G') { 4695f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org color = COLOR_GREEN; 4696f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org } else if (ch == 'Y') { 4697f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org color = COLOR_YELLOW; 4698f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org } else { 4699f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org --str; 4700f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org } 4701f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org } 4702f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org} 4703f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 4704f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgstatic const char kColorEncodedHelpMessage[] = 4705f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org"This program contains tests written using " GTEST_NAME_ ". You can use the\n" 4706f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org"following command line flags to control its behavior:\n" 4707f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org"\n" 4708f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org"Test Selection:\n" 4709f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org" @G--" GTEST_FLAG_PREFIX_ "list_tests@D\n" 4710f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org" List the names of all tests instead of running them. The name of\n" 4711f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org" TEST(Foo, Bar) is \"Foo.Bar\".\n" 4712f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org" @G--" GTEST_FLAG_PREFIX_ "filter=@YPOSTIVE_PATTERNS" 4713f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org "[@G-@YNEGATIVE_PATTERNS]@D\n" 4714f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org" Run only the tests whose name matches one of the positive patterns but\n" 4715f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org" none of the negative patterns. '?' matches any single character; '*'\n" 4716f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org" matches any substring; ':' separates two patterns.\n" 4717f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org" @G--" GTEST_FLAG_PREFIX_ "also_run_disabled_tests@D\n" 4718f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org" Run all disabled tests too.\n" 4719f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org"\n" 4720f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org"Test Execution:\n" 4721f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org" @G--" GTEST_FLAG_PREFIX_ "repeat=@Y[COUNT]@D\n" 4722f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org" Run the tests repeatedly; use a negative count to repeat forever.\n" 4723f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org" @G--" GTEST_FLAG_PREFIX_ "shuffle@D\n" 4724f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org" Randomize tests' orders on every iteration.\n" 4725f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org" @G--" GTEST_FLAG_PREFIX_ "random_seed=@Y[NUMBER]@D\n" 4726f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org" Random number seed to use for shuffling test orders (between 1 and\n" 4727f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org" 99999, or 0 to use a seed based on the current time).\n" 4728f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org"\n" 4729f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org"Test Output:\n" 4730f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org" @G--" GTEST_FLAG_PREFIX_ "color=@Y(@Gyes@Y|@Gno@Y|@Gauto@Y)@D\n" 4731f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org" Enable/disable colored output. The default is @Gauto@D.\n" 4732f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org" -@G-" GTEST_FLAG_PREFIX_ "print_time=0@D\n" 4733f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org" Don't print the elapsed time of each test.\n" 4734f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org" @G--" GTEST_FLAG_PREFIX_ "output=xml@Y[@G:@YDIRECTORY_PATH@G" 4735f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org GTEST_PATH_SEP_ "@Y|@G:@YFILE_PATH]@D\n" 4736f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org" Generate an XML report in the given directory or with the given file\n" 4737f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org" name. @YFILE_PATH@D defaults to @Gtest_details.xml@D.\n" 4738f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#if GTEST_CAN_STREAM_RESULTS_ 4739f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org" @G--" GTEST_FLAG_PREFIX_ "stream_result_to=@YHOST@G:@YPORT@D\n" 4740f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org" Stream test results to the given server.\n" 4741f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#endif // GTEST_CAN_STREAM_RESULTS_ 4742f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org"\n" 4743f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org"Assertion Behavior:\n" 4744f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#if GTEST_HAS_DEATH_TEST && !GTEST_OS_WINDOWS 4745f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org" @G--" GTEST_FLAG_PREFIX_ "death_test_style=@Y(@Gfast@Y|@Gthreadsafe@Y)@D\n" 4746f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org" Set the default death test style.\n" 4747f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#endif // GTEST_HAS_DEATH_TEST && !GTEST_OS_WINDOWS 4748f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org" @G--" GTEST_FLAG_PREFIX_ "break_on_failure@D\n" 4749f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org" Turn assertion failures into debugger break-points.\n" 4750f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org" @G--" GTEST_FLAG_PREFIX_ "throw_on_failure@D\n" 4751f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org" Turn assertion failures into C++ exceptions.\n" 4752f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org" @G--" GTEST_FLAG_PREFIX_ "catch_exceptions=0@D\n" 4753f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org" Do not report exceptions as test failures. Instead, allow them\n" 4754f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org" to crash the program or throw a pop-up (on Windows).\n" 4755f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org"\n" 4756f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org"Except for @G--" GTEST_FLAG_PREFIX_ "list_tests@D, you can alternatively set " 4757f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org "the corresponding\n" 4758f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org"environment variable of a flag (all letters in upper-case). For example, to\n" 4759f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org"disable colored text output, you can either specify @G--" GTEST_FLAG_PREFIX_ 4760f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org "color=no@D or set\n" 4761f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org"the @G" GTEST_FLAG_PREFIX_UPPER_ "COLOR@D environment variable to @Gno@D.\n" 4762f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org"\n" 4763f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org"For more information, please read the " GTEST_NAME_ " documentation at\n" 4764f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org"@G" GTEST_PROJECT_URL_ "@D. If you find a bug in " GTEST_NAME_ "\n" 4765f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org"(not one in your own code or tests), please report it to\n" 4766f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org"@G<" GTEST_DEV_EMAIL_ ">@D.\n"; 4767f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 4768f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// Parses the command line for Google Test flags, without initializing 4769f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// other parts of Google Test. The type parameter CharType can be 4770f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// instantiated to either char or wchar_t. 4771f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgtemplate <typename CharType> 4772f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgvoid ParseGoogleTestFlagsOnlyImpl(int* argc, CharType** argv) { 4773f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org for (int i = 1; i < *argc; i++) { 4774f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org const String arg_string = StreamableToString(argv[i]); 4775f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org const char* const arg = arg_string.c_str(); 4776f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 4777f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org using internal::ParseBoolFlag; 4778f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org using internal::ParseInt32Flag; 4779f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org using internal::ParseStringFlag; 4780f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 4781f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // Do we see a Google Test flag? 4782f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org if (ParseBoolFlag(arg, kAlsoRunDisabledTestsFlag, 4783f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org >EST_FLAG(also_run_disabled_tests)) || 4784f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org ParseBoolFlag(arg, kBreakOnFailureFlag, 4785f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org >EST_FLAG(break_on_failure)) || 4786f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org ParseBoolFlag(arg, kCatchExceptionsFlag, 4787f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org >EST_FLAG(catch_exceptions)) || 4788f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org ParseStringFlag(arg, kColorFlag, >EST_FLAG(color)) || 4789f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org ParseStringFlag(arg, kDeathTestStyleFlag, 4790f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org >EST_FLAG(death_test_style)) || 4791f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org ParseBoolFlag(arg, kDeathTestUseFork, 4792f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org >EST_FLAG(death_test_use_fork)) || 4793f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org ParseStringFlag(arg, kFilterFlag, >EST_FLAG(filter)) || 4794f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org ParseStringFlag(arg, kInternalRunDeathTestFlag, 4795f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org >EST_FLAG(internal_run_death_test)) || 4796f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org ParseBoolFlag(arg, kListTestsFlag, >EST_FLAG(list_tests)) || 4797f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org ParseStringFlag(arg, kOutputFlag, >EST_FLAG(output)) || 4798f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org ParseBoolFlag(arg, kPrintTimeFlag, >EST_FLAG(print_time)) || 4799f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org ParseInt32Flag(arg, kRandomSeedFlag, >EST_FLAG(random_seed)) || 4800f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org ParseInt32Flag(arg, kRepeatFlag, >EST_FLAG(repeat)) || 4801f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org ParseBoolFlag(arg, kShuffleFlag, >EST_FLAG(shuffle)) || 4802f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org ParseInt32Flag(arg, kStackTraceDepthFlag, 4803f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org >EST_FLAG(stack_trace_depth)) || 4804f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org ParseStringFlag(arg, kStreamResultToFlag, 4805f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org >EST_FLAG(stream_result_to)) || 4806f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org ParseBoolFlag(arg, kThrowOnFailureFlag, 4807f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org >EST_FLAG(throw_on_failure)) 4808f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org ) { 4809f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // Yes. Shift the remainder of the argv list left by one. Note 4810f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // that argv has (*argc + 1) elements, the last one always being 4811f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // NULL. The following loop moves the trailing NULL element as 4812f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // well. 4813f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org for (int j = i; j != *argc; j++) { 4814f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org argv[j] = argv[j + 1]; 4815f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org } 4816f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 4817f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // Decrements the argument count. 4818f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org (*argc)--; 4819f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 4820f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // We also need to decrement the iterator as we just removed 4821f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // an element. 4822f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org i--; 4823f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org } else if (arg_string == "--help" || arg_string == "-h" || 4824f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org arg_string == "-?" || arg_string == "/?" || 4825f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org HasGoogleTestFlagPrefix(arg)) { 4826f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // Both help flag and unrecognized Google Test flags (excluding 4827f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // internal ones) trigger help display. 4828f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org g_help_flag = true; 4829f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org } 4830f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org } 4831f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 4832f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org if (g_help_flag) { 4833f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // We print the help here instead of in RUN_ALL_TESTS(), as the 4834f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // latter may not be called at all if the user is using Google 4835f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // Test with another testing framework. 4836f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org PrintColorEncoded(kColorEncodedHelpMessage); 4837f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org } 4838f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org} 4839f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 4840f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// Parses the command line for Google Test flags, without initializing 4841f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// other parts of Google Test. 4842f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgvoid ParseGoogleTestFlagsOnly(int* argc, char** argv) { 4843f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org ParseGoogleTestFlagsOnlyImpl(argc, argv); 4844f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org} 4845f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgvoid ParseGoogleTestFlagsOnly(int* argc, wchar_t** argv) { 4846f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org ParseGoogleTestFlagsOnlyImpl(argc, argv); 4847f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org} 4848f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 4849f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// The internal implementation of InitGoogleTest(). 4850f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// 4851f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// The type parameter CharType can be instantiated to either char or 4852f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// wchar_t. 4853f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgtemplate <typename CharType> 4854f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgvoid InitGoogleTestImpl(int* argc, CharType** argv) { 4855f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org g_init_gtest_count++; 4856f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 4857f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org // We don't want to run the initialization code twice. 4858f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org if (g_init_gtest_count != 1) return; 4859f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 4860f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org if (*argc <= 0) return; 4861f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 4862f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org internal::g_executable_path = internal::StreamableToString(argv[0]); 4863f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 4864f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#if GTEST_HAS_DEATH_TEST 4865f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 4866f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org g_argvs.clear(); 4867f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org for (int i = 0; i != *argc; i++) { 4868f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org g_argvs.push_back(StreamableToString(argv[i])); 4869f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org } 4870f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 4871f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#endif // GTEST_HAS_DEATH_TEST 4872f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 4873f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org ParseGoogleTestFlagsOnly(argc, argv); 4874f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org GetUnitTestImpl()->PostFlagParsingInit(); 4875f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org} 4876f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 4877f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org} // namespace internal 4878f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 4879f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// Initializes Google Test. This must be called before calling 4880f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// RUN_ALL_TESTS(). In particular, it parses a command line for the 4881f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// flags that Google Test recognizes. Whenever a Google Test flag is 4882f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// seen, it is removed from argv, and *argc is decremented. 4883f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// 4884f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// No value is returned. Instead, the Google Test flag variables are 4885f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// updated. 4886f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// 4887f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// Calling the function for the second time has no user-visible effect. 4888f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgvoid InitGoogleTest(int* argc, char** argv) { 4889f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org internal::InitGoogleTestImpl(argc, argv); 4890f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org} 4891f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 4892f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// This overloaded version can be used in Windows programs compiled in 4893f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org// UNICODE mode. 4894f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgvoid InitGoogleTest(int* argc, wchar_t** argv) { 4895f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org internal::InitGoogleTestImpl(argc, argv); 4896f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org} 4897f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org 4898f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org} // namespace testing 4899