Wed, 31 Dec 2014 07:53:36 +0100
Correct small whitespace inconsistency, lost while renaming variables.
michael@0 | 1 | // Copyright 2005, Google Inc. |
michael@0 | 2 | // All rights reserved. |
michael@0 | 3 | // |
michael@0 | 4 | // Redistribution and use in source and binary forms, with or without |
michael@0 | 5 | // modification, are permitted provided that the following conditions are |
michael@0 | 6 | // met: |
michael@0 | 7 | // |
michael@0 | 8 | // * Redistributions of source code must retain the above copyright |
michael@0 | 9 | // notice, this list of conditions and the following disclaimer. |
michael@0 | 10 | // * Redistributions in binary form must reproduce the above |
michael@0 | 11 | // copyright notice, this list of conditions and the following disclaimer |
michael@0 | 12 | // in the documentation and/or other materials provided with the |
michael@0 | 13 | // distribution. |
michael@0 | 14 | // * Neither the name of Google Inc. nor the names of its |
michael@0 | 15 | // contributors may be used to endorse or promote products derived from |
michael@0 | 16 | // this software without specific prior written permission. |
michael@0 | 17 | // |
michael@0 | 18 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
michael@0 | 19 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
michael@0 | 20 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
michael@0 | 21 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
michael@0 | 22 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
michael@0 | 23 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
michael@0 | 24 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
michael@0 | 25 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
michael@0 | 26 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
michael@0 | 27 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
michael@0 | 28 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
michael@0 | 29 | |
michael@0 | 30 | // Utility functions and classes used by the Google C++ testing framework. |
michael@0 | 31 | // |
michael@0 | 32 | // Author: wan@google.com (Zhanyong Wan) |
michael@0 | 33 | // |
michael@0 | 34 | // This file contains purely Google Test's internal implementation. Please |
michael@0 | 35 | // DO NOT #INCLUDE IT IN A USER PROGRAM. |
michael@0 | 36 | |
michael@0 | 37 | #ifndef GTEST_SRC_GTEST_INTERNAL_INL_H_ |
michael@0 | 38 | #define GTEST_SRC_GTEST_INTERNAL_INL_H_ |
michael@0 | 39 | |
michael@0 | 40 | // GTEST_IMPLEMENTATION_ is defined to 1 iff the current translation unit is |
michael@0 | 41 | // part of Google Test's implementation; otherwise it's undefined. |
michael@0 | 42 | #if !GTEST_IMPLEMENTATION_ |
michael@0 | 43 | // A user is trying to include this from his code - just say no. |
michael@0 | 44 | # error "gtest-internal-inl.h is part of Google Test's internal implementation." |
michael@0 | 45 | # error "It must not be included except by Google Test itself." |
michael@0 | 46 | #endif // GTEST_IMPLEMENTATION_ |
michael@0 | 47 | |
michael@0 | 48 | #ifndef _WIN32_WCE |
michael@0 | 49 | # include <errno.h> |
michael@0 | 50 | #endif // !_WIN32_WCE |
michael@0 | 51 | #include <stddef.h> |
michael@0 | 52 | #include <stdlib.h> // For strtoll/_strtoul64/malloc/free. |
michael@0 | 53 | #include <string.h> // For memmove. |
michael@0 | 54 | |
michael@0 | 55 | #include <algorithm> |
michael@0 | 56 | #include <string> |
michael@0 | 57 | #include <vector> |
michael@0 | 58 | |
michael@0 | 59 | #include "gtest/internal/gtest-port.h" |
michael@0 | 60 | |
michael@0 | 61 | #if GTEST_OS_WINDOWS |
michael@0 | 62 | # include <windows.h> // NOLINT |
michael@0 | 63 | #endif // GTEST_OS_WINDOWS |
michael@0 | 64 | |
michael@0 | 65 | #include "gtest/gtest.h" // NOLINT |
michael@0 | 66 | #include "gtest/gtest-spi.h" |
michael@0 | 67 | |
michael@0 | 68 | namespace testing { |
michael@0 | 69 | |
michael@0 | 70 | // Declares the flags. |
michael@0 | 71 | // |
michael@0 | 72 | // We don't want the users to modify this flag in the code, but want |
michael@0 | 73 | // Google Test's own unit tests to be able to access it. Therefore we |
michael@0 | 74 | // declare it here as opposed to in gtest.h. |
michael@0 | 75 | GTEST_DECLARE_bool_(death_test_use_fork); |
michael@0 | 76 | |
michael@0 | 77 | namespace internal { |
michael@0 | 78 | |
michael@0 | 79 | // The value of GetTestTypeId() as seen from within the Google Test |
michael@0 | 80 | // library. This is solely for testing GetTestTypeId(). |
michael@0 | 81 | GTEST_API_ extern const TypeId kTestTypeIdInGoogleTest; |
michael@0 | 82 | |
michael@0 | 83 | // Names of the flags (needed for parsing Google Test flags). |
michael@0 | 84 | const char kAlsoRunDisabledTestsFlag[] = "also_run_disabled_tests"; |
michael@0 | 85 | const char kBreakOnFailureFlag[] = "break_on_failure"; |
michael@0 | 86 | const char kCatchExceptionsFlag[] = "catch_exceptions"; |
michael@0 | 87 | const char kColorFlag[] = "color"; |
michael@0 | 88 | const char kFilterFlag[] = "filter"; |
michael@0 | 89 | const char kListTestsFlag[] = "list_tests"; |
michael@0 | 90 | const char kOutputFlag[] = "output"; |
michael@0 | 91 | const char kPrintTimeFlag[] = "print_time"; |
michael@0 | 92 | const char kRandomSeedFlag[] = "random_seed"; |
michael@0 | 93 | const char kRepeatFlag[] = "repeat"; |
michael@0 | 94 | const char kShuffleFlag[] = "shuffle"; |
michael@0 | 95 | const char kStackTraceDepthFlag[] = "stack_trace_depth"; |
michael@0 | 96 | const char kStreamResultToFlag[] = "stream_result_to"; |
michael@0 | 97 | const char kThrowOnFailureFlag[] = "throw_on_failure"; |
michael@0 | 98 | |
michael@0 | 99 | // A valid random seed must be in [1, kMaxRandomSeed]. |
michael@0 | 100 | const int kMaxRandomSeed = 99999; |
michael@0 | 101 | |
michael@0 | 102 | // g_help_flag is true iff the --help flag or an equivalent form is |
michael@0 | 103 | // specified on the command line. |
michael@0 | 104 | GTEST_API_ extern bool g_help_flag; |
michael@0 | 105 | |
michael@0 | 106 | // Returns the current time in milliseconds. |
michael@0 | 107 | GTEST_API_ TimeInMillis GetTimeInMillis(); |
michael@0 | 108 | |
michael@0 | 109 | // Returns true iff Google Test should use colors in the output. |
michael@0 | 110 | GTEST_API_ bool ShouldUseColor(bool stdout_is_tty); |
michael@0 | 111 | |
michael@0 | 112 | // Formats the given time in milliseconds as seconds. |
michael@0 | 113 | GTEST_API_ std::string FormatTimeInMillisAsSeconds(TimeInMillis ms); |
michael@0 | 114 | |
michael@0 | 115 | // Converts the given time in milliseconds to a date string in the ISO 8601 |
michael@0 | 116 | // format, without the timezone information. N.B.: due to the use the |
michael@0 | 117 | // non-reentrant localtime() function, this function is not thread safe. Do |
michael@0 | 118 | // not use it in any code that can be called from multiple threads. |
michael@0 | 119 | GTEST_API_ std::string FormatEpochTimeInMillisAsIso8601(TimeInMillis ms); |
michael@0 | 120 | |
michael@0 | 121 | // Parses a string for an Int32 flag, in the form of "--flag=value". |
michael@0 | 122 | // |
michael@0 | 123 | // On success, stores the value of the flag in *value, and returns |
michael@0 | 124 | // true. On failure, returns false without changing *value. |
michael@0 | 125 | GTEST_API_ bool ParseInt32Flag( |
michael@0 | 126 | const char* str, const char* flag, Int32* value); |
michael@0 | 127 | |
michael@0 | 128 | // Returns a random seed in range [1, kMaxRandomSeed] based on the |
michael@0 | 129 | // given --gtest_random_seed flag value. |
michael@0 | 130 | inline int GetRandomSeedFromFlag(Int32 random_seed_flag) { |
michael@0 | 131 | const unsigned int raw_seed = (random_seed_flag == 0) ? |
michael@0 | 132 | static_cast<unsigned int>(GetTimeInMillis()) : |
michael@0 | 133 | static_cast<unsigned int>(random_seed_flag); |
michael@0 | 134 | |
michael@0 | 135 | // Normalizes the actual seed to range [1, kMaxRandomSeed] such that |
michael@0 | 136 | // it's easy to type. |
michael@0 | 137 | const int normalized_seed = |
michael@0 | 138 | static_cast<int>((raw_seed - 1U) % |
michael@0 | 139 | static_cast<unsigned int>(kMaxRandomSeed)) + 1; |
michael@0 | 140 | return normalized_seed; |
michael@0 | 141 | } |
michael@0 | 142 | |
michael@0 | 143 | // Returns the first valid random seed after 'seed'. The behavior is |
michael@0 | 144 | // undefined if 'seed' is invalid. The seed after kMaxRandomSeed is |
michael@0 | 145 | // considered to be 1. |
michael@0 | 146 | inline int GetNextRandomSeed(int seed) { |
michael@0 | 147 | GTEST_CHECK_(1 <= seed && seed <= kMaxRandomSeed) |
michael@0 | 148 | << "Invalid random seed " << seed << " - must be in [1, " |
michael@0 | 149 | << kMaxRandomSeed << "]."; |
michael@0 | 150 | const int next_seed = seed + 1; |
michael@0 | 151 | return (next_seed > kMaxRandomSeed) ? 1 : next_seed; |
michael@0 | 152 | } |
michael@0 | 153 | |
michael@0 | 154 | // This class saves the values of all Google Test flags in its c'tor, and |
michael@0 | 155 | // restores them in its d'tor. |
michael@0 | 156 | class GTestFlagSaver { |
michael@0 | 157 | public: |
michael@0 | 158 | // The c'tor. |
michael@0 | 159 | GTestFlagSaver() { |
michael@0 | 160 | also_run_disabled_tests_ = GTEST_FLAG(also_run_disabled_tests); |
michael@0 | 161 | break_on_failure_ = GTEST_FLAG(break_on_failure); |
michael@0 | 162 | catch_exceptions_ = GTEST_FLAG(catch_exceptions); |
michael@0 | 163 | color_ = GTEST_FLAG(color); |
michael@0 | 164 | death_test_style_ = GTEST_FLAG(death_test_style); |
michael@0 | 165 | death_test_use_fork_ = GTEST_FLAG(death_test_use_fork); |
michael@0 | 166 | filter_ = GTEST_FLAG(filter); |
michael@0 | 167 | internal_run_death_test_ = GTEST_FLAG(internal_run_death_test); |
michael@0 | 168 | list_tests_ = GTEST_FLAG(list_tests); |
michael@0 | 169 | output_ = GTEST_FLAG(output); |
michael@0 | 170 | print_time_ = GTEST_FLAG(print_time); |
michael@0 | 171 | random_seed_ = GTEST_FLAG(random_seed); |
michael@0 | 172 | repeat_ = GTEST_FLAG(repeat); |
michael@0 | 173 | shuffle_ = GTEST_FLAG(shuffle); |
michael@0 | 174 | stack_trace_depth_ = GTEST_FLAG(stack_trace_depth); |
michael@0 | 175 | stream_result_to_ = GTEST_FLAG(stream_result_to); |
michael@0 | 176 | throw_on_failure_ = GTEST_FLAG(throw_on_failure); |
michael@0 | 177 | } |
michael@0 | 178 | |
michael@0 | 179 | // The d'tor is not virtual. DO NOT INHERIT FROM THIS CLASS. |
michael@0 | 180 | ~GTestFlagSaver() { |
michael@0 | 181 | GTEST_FLAG(also_run_disabled_tests) = also_run_disabled_tests_; |
michael@0 | 182 | GTEST_FLAG(break_on_failure) = break_on_failure_; |
michael@0 | 183 | GTEST_FLAG(catch_exceptions) = catch_exceptions_; |
michael@0 | 184 | GTEST_FLAG(color) = color_; |
michael@0 | 185 | GTEST_FLAG(death_test_style) = death_test_style_; |
michael@0 | 186 | GTEST_FLAG(death_test_use_fork) = death_test_use_fork_; |
michael@0 | 187 | GTEST_FLAG(filter) = filter_; |
michael@0 | 188 | GTEST_FLAG(internal_run_death_test) = internal_run_death_test_; |
michael@0 | 189 | GTEST_FLAG(list_tests) = list_tests_; |
michael@0 | 190 | GTEST_FLAG(output) = output_; |
michael@0 | 191 | GTEST_FLAG(print_time) = print_time_; |
michael@0 | 192 | GTEST_FLAG(random_seed) = random_seed_; |
michael@0 | 193 | GTEST_FLAG(repeat) = repeat_; |
michael@0 | 194 | GTEST_FLAG(shuffle) = shuffle_; |
michael@0 | 195 | GTEST_FLAG(stack_trace_depth) = stack_trace_depth_; |
michael@0 | 196 | GTEST_FLAG(stream_result_to) = stream_result_to_; |
michael@0 | 197 | GTEST_FLAG(throw_on_failure) = throw_on_failure_; |
michael@0 | 198 | } |
michael@0 | 199 | |
michael@0 | 200 | private: |
michael@0 | 201 | // Fields for saving the original values of flags. |
michael@0 | 202 | bool also_run_disabled_tests_; |
michael@0 | 203 | bool break_on_failure_; |
michael@0 | 204 | bool catch_exceptions_; |
michael@0 | 205 | String color_; |
michael@0 | 206 | String death_test_style_; |
michael@0 | 207 | bool death_test_use_fork_; |
michael@0 | 208 | String filter_; |
michael@0 | 209 | String internal_run_death_test_; |
michael@0 | 210 | bool list_tests_; |
michael@0 | 211 | String output_; |
michael@0 | 212 | bool print_time_; |
michael@0 | 213 | bool pretty_; |
michael@0 | 214 | internal::Int32 random_seed_; |
michael@0 | 215 | internal::Int32 repeat_; |
michael@0 | 216 | bool shuffle_; |
michael@0 | 217 | internal::Int32 stack_trace_depth_; |
michael@0 | 218 | String stream_result_to_; |
michael@0 | 219 | bool throw_on_failure_; |
michael@0 | 220 | } GTEST_ATTRIBUTE_UNUSED_; |
michael@0 | 221 | |
michael@0 | 222 | // Converts a Unicode code point to a narrow string in UTF-8 encoding. |
michael@0 | 223 | // code_point parameter is of type UInt32 because wchar_t may not be |
michael@0 | 224 | // wide enough to contain a code point. |
michael@0 | 225 | // The output buffer str must containt at least 32 characters. |
michael@0 | 226 | // The function returns the address of the output buffer. |
michael@0 | 227 | // If the code_point is not a valid Unicode code point |
michael@0 | 228 | // (i.e. outside of Unicode range U+0 to U+10FFFF) it will be output |
michael@0 | 229 | // as '(Invalid Unicode 0xXXXXXXXX)'. |
michael@0 | 230 | GTEST_API_ char* CodePointToUtf8(UInt32 code_point, char* str); |
michael@0 | 231 | |
michael@0 | 232 | // Converts a wide string to a narrow string in UTF-8 encoding. |
michael@0 | 233 | // The wide string is assumed to have the following encoding: |
michael@0 | 234 | // UTF-16 if sizeof(wchar_t) == 2 (on Windows, Cygwin, Symbian OS) |
michael@0 | 235 | // UTF-32 if sizeof(wchar_t) == 4 (on Linux) |
michael@0 | 236 | // Parameter str points to a null-terminated wide string. |
michael@0 | 237 | // Parameter num_chars may additionally limit the number |
michael@0 | 238 | // of wchar_t characters processed. -1 is used when the entire string |
michael@0 | 239 | // should be processed. |
michael@0 | 240 | // If the string contains code points that are not valid Unicode code points |
michael@0 | 241 | // (i.e. outside of Unicode range U+0 to U+10FFFF) they will be output |
michael@0 | 242 | // as '(Invalid Unicode 0xXXXXXXXX)'. If the string is in UTF16 encoding |
michael@0 | 243 | // and contains invalid UTF-16 surrogate pairs, values in those pairs |
michael@0 | 244 | // will be encoded as individual Unicode characters from Basic Normal Plane. |
michael@0 | 245 | GTEST_API_ String WideStringToUtf8(const wchar_t* str, int num_chars); |
michael@0 | 246 | |
michael@0 | 247 | // Reads the GTEST_SHARD_STATUS_FILE environment variable, and creates the file |
michael@0 | 248 | // if the variable is present. If a file already exists at this location, this |
michael@0 | 249 | // function will write over it. If the variable is present, but the file cannot |
michael@0 | 250 | // be created, prints an error and exits. |
michael@0 | 251 | void WriteToShardStatusFileIfNeeded(); |
michael@0 | 252 | |
michael@0 | 253 | // Checks whether sharding is enabled by examining the relevant |
michael@0 | 254 | // environment variable values. If the variables are present, |
michael@0 | 255 | // but inconsistent (e.g., shard_index >= total_shards), prints |
michael@0 | 256 | // an error and exits. If in_subprocess_for_death_test, sharding is |
michael@0 | 257 | // disabled because it must only be applied to the original test |
michael@0 | 258 | // process. Otherwise, we could filter out death tests we intended to execute. |
michael@0 | 259 | GTEST_API_ bool ShouldShard(const char* total_shards_str, |
michael@0 | 260 | const char* shard_index_str, |
michael@0 | 261 | bool in_subprocess_for_death_test); |
michael@0 | 262 | |
michael@0 | 263 | // Parses the environment variable var as an Int32. If it is unset, |
michael@0 | 264 | // returns default_val. If it is not an Int32, prints an error and |
michael@0 | 265 | // and aborts. |
michael@0 | 266 | GTEST_API_ Int32 Int32FromEnvOrDie(const char* env_var, Int32 default_val); |
michael@0 | 267 | |
michael@0 | 268 | // Given the total number of shards, the shard index, and the test id, |
michael@0 | 269 | // returns true iff the test should be run on this shard. The test id is |
michael@0 | 270 | // some arbitrary but unique non-negative integer assigned to each test |
michael@0 | 271 | // method. Assumes that 0 <= shard_index < total_shards. |
michael@0 | 272 | GTEST_API_ bool ShouldRunTestOnShard( |
michael@0 | 273 | int total_shards, int shard_index, int test_id); |
michael@0 | 274 | |
michael@0 | 275 | // STL container utilities. |
michael@0 | 276 | |
michael@0 | 277 | // Returns the number of elements in the given container that satisfy |
michael@0 | 278 | // the given predicate. |
michael@0 | 279 | template <class Container, typename Predicate> |
michael@0 | 280 | inline int CountIf(const Container& c, Predicate predicate) { |
michael@0 | 281 | // Implemented as an explicit loop since std::count_if() in libCstd on |
michael@0 | 282 | // Solaris has a non-standard signature. |
michael@0 | 283 | int count = 0; |
michael@0 | 284 | for (typename Container::const_iterator it = c.begin(); it != c.end(); ++it) { |
michael@0 | 285 | if (predicate(*it)) |
michael@0 | 286 | ++count; |
michael@0 | 287 | } |
michael@0 | 288 | return count; |
michael@0 | 289 | } |
michael@0 | 290 | |
michael@0 | 291 | // Applies a function/functor to each element in the container. |
michael@0 | 292 | template <class Container, typename Functor> |
michael@0 | 293 | void ForEach(const Container& c, Functor functor) { |
michael@0 | 294 | std::for_each(c.begin(), c.end(), functor); |
michael@0 | 295 | } |
michael@0 | 296 | |
michael@0 | 297 | // Returns the i-th element of the vector, or default_value if i is not |
michael@0 | 298 | // in range [0, v.size()). |
michael@0 | 299 | template <typename E> |
michael@0 | 300 | inline E GetElementOr(const std::vector<E>& v, int i, E default_value) { |
michael@0 | 301 | return (i < 0 || i >= static_cast<int>(v.size())) ? default_value : v[i]; |
michael@0 | 302 | } |
michael@0 | 303 | |
michael@0 | 304 | // Performs an in-place shuffle of a range of the vector's elements. |
michael@0 | 305 | // 'begin' and 'end' are element indices as an STL-style range; |
michael@0 | 306 | // i.e. [begin, end) are shuffled, where 'end' == size() means to |
michael@0 | 307 | // shuffle to the end of the vector. |
michael@0 | 308 | template <typename E> |
michael@0 | 309 | void ShuffleRange(internal::Random* random, int begin, int end, |
michael@0 | 310 | std::vector<E>* v) { |
michael@0 | 311 | const int size = static_cast<int>(v->size()); |
michael@0 | 312 | GTEST_CHECK_(0 <= begin && begin <= size) |
michael@0 | 313 | << "Invalid shuffle range start " << begin << ": must be in range [0, " |
michael@0 | 314 | << size << "]."; |
michael@0 | 315 | GTEST_CHECK_(begin <= end && end <= size) |
michael@0 | 316 | << "Invalid shuffle range finish " << end << ": must be in range [" |
michael@0 | 317 | << begin << ", " << size << "]."; |
michael@0 | 318 | |
michael@0 | 319 | // Fisher-Yates shuffle, from |
michael@0 | 320 | // http://en.wikipedia.org/wiki/Fisher-Yates_shuffle |
michael@0 | 321 | for (int range_width = end - begin; range_width >= 2; range_width--) { |
michael@0 | 322 | const int last_in_range = begin + range_width - 1; |
michael@0 | 323 | const int selected = begin + random->Generate(range_width); |
michael@0 | 324 | std::swap((*v)[selected], (*v)[last_in_range]); |
michael@0 | 325 | } |
michael@0 | 326 | } |
michael@0 | 327 | |
michael@0 | 328 | // Performs an in-place shuffle of the vector's elements. |
michael@0 | 329 | template <typename E> |
michael@0 | 330 | inline void Shuffle(internal::Random* random, std::vector<E>* v) { |
michael@0 | 331 | ShuffleRange(random, 0, static_cast<int>(v->size()), v); |
michael@0 | 332 | } |
michael@0 | 333 | |
michael@0 | 334 | // A function for deleting an object. Handy for being used as a |
michael@0 | 335 | // functor. |
michael@0 | 336 | template <typename T> |
michael@0 | 337 | static void Delete(T* x) { |
michael@0 | 338 | delete x; |
michael@0 | 339 | } |
michael@0 | 340 | |
michael@0 | 341 | // A predicate that checks the key of a TestProperty against a known key. |
michael@0 | 342 | // |
michael@0 | 343 | // TestPropertyKeyIs is copyable. |
michael@0 | 344 | class TestPropertyKeyIs { |
michael@0 | 345 | public: |
michael@0 | 346 | // Constructor. |
michael@0 | 347 | // |
michael@0 | 348 | // TestPropertyKeyIs has NO default constructor. |
michael@0 | 349 | explicit TestPropertyKeyIs(const char* key) |
michael@0 | 350 | : key_(key) {} |
michael@0 | 351 | |
michael@0 | 352 | // Returns true iff the test name of test property matches on key_. |
michael@0 | 353 | bool operator()(const TestProperty& test_property) const { |
michael@0 | 354 | return String(test_property.key()).Compare(key_) == 0; |
michael@0 | 355 | } |
michael@0 | 356 | |
michael@0 | 357 | private: |
michael@0 | 358 | String key_; |
michael@0 | 359 | }; |
michael@0 | 360 | |
michael@0 | 361 | // Class UnitTestOptions. |
michael@0 | 362 | // |
michael@0 | 363 | // This class contains functions for processing options the user |
michael@0 | 364 | // specifies when running the tests. It has only static members. |
michael@0 | 365 | // |
michael@0 | 366 | // In most cases, the user can specify an option using either an |
michael@0 | 367 | // environment variable or a command line flag. E.g. you can set the |
michael@0 | 368 | // test filter using either GTEST_FILTER or --gtest_filter. If both |
michael@0 | 369 | // the variable and the flag are present, the latter overrides the |
michael@0 | 370 | // former. |
michael@0 | 371 | class GTEST_API_ UnitTestOptions { |
michael@0 | 372 | public: |
michael@0 | 373 | // Functions for processing the gtest_output flag. |
michael@0 | 374 | |
michael@0 | 375 | // Returns the output format, or "" for normal printed output. |
michael@0 | 376 | static String GetOutputFormat(); |
michael@0 | 377 | |
michael@0 | 378 | // Returns the absolute path of the requested output file, or the |
michael@0 | 379 | // default (test_detail.xml in the original working directory) if |
michael@0 | 380 | // none was explicitly specified. |
michael@0 | 381 | static String GetAbsolutePathToOutputFile(); |
michael@0 | 382 | |
michael@0 | 383 | // Functions for processing the gtest_filter flag. |
michael@0 | 384 | |
michael@0 | 385 | // Returns true iff the wildcard pattern matches the string. The |
michael@0 | 386 | // first ':' or '\0' character in pattern marks the end of it. |
michael@0 | 387 | // |
michael@0 | 388 | // This recursive algorithm isn't very efficient, but is clear and |
michael@0 | 389 | // works well enough for matching test names, which are short. |
michael@0 | 390 | static bool PatternMatchesString(const char *pattern, const char *str); |
michael@0 | 391 | |
michael@0 | 392 | // Returns true iff the user-specified filter matches the test case |
michael@0 | 393 | // name and the test name. |
michael@0 | 394 | static bool FilterMatchesTest(const String &test_case_name, |
michael@0 | 395 | const String &test_name); |
michael@0 | 396 | |
michael@0 | 397 | #if GTEST_OS_WINDOWS |
michael@0 | 398 | // Function for supporting the gtest_catch_exception flag. |
michael@0 | 399 | |
michael@0 | 400 | // Returns EXCEPTION_EXECUTE_HANDLER if Google Test should handle the |
michael@0 | 401 | // given SEH exception, or EXCEPTION_CONTINUE_SEARCH otherwise. |
michael@0 | 402 | // This function is useful as an __except condition. |
michael@0 | 403 | static int GTestShouldProcessSEH(DWORD exception_code); |
michael@0 | 404 | #endif // GTEST_OS_WINDOWS |
michael@0 | 405 | |
michael@0 | 406 | // Returns true if "name" matches the ':' separated list of glob-style |
michael@0 | 407 | // filters in "filter". |
michael@0 | 408 | static bool MatchesFilter(const String& name, const char* filter); |
michael@0 | 409 | }; |
michael@0 | 410 | |
michael@0 | 411 | // Returns the current application's name, removing directory path if that |
michael@0 | 412 | // is present. Used by UnitTestOptions::GetOutputFile. |
michael@0 | 413 | GTEST_API_ FilePath GetCurrentExecutableName(); |
michael@0 | 414 | |
michael@0 | 415 | // The role interface for getting the OS stack trace as a string. |
michael@0 | 416 | class OsStackTraceGetterInterface { |
michael@0 | 417 | public: |
michael@0 | 418 | OsStackTraceGetterInterface() {} |
michael@0 | 419 | virtual ~OsStackTraceGetterInterface() {} |
michael@0 | 420 | |
michael@0 | 421 | // Returns the current OS stack trace as a String. Parameters: |
michael@0 | 422 | // |
michael@0 | 423 | // max_depth - the maximum number of stack frames to be included |
michael@0 | 424 | // in the trace. |
michael@0 | 425 | // skip_count - the number of top frames to be skipped; doesn't count |
michael@0 | 426 | // against max_depth. |
michael@0 | 427 | virtual String CurrentStackTrace(int max_depth, int skip_count) = 0; |
michael@0 | 428 | |
michael@0 | 429 | // UponLeavingGTest() should be called immediately before Google Test calls |
michael@0 | 430 | // user code. It saves some information about the current stack that |
michael@0 | 431 | // CurrentStackTrace() will use to find and hide Google Test stack frames. |
michael@0 | 432 | virtual void UponLeavingGTest() = 0; |
michael@0 | 433 | |
michael@0 | 434 | private: |
michael@0 | 435 | GTEST_DISALLOW_COPY_AND_ASSIGN_(OsStackTraceGetterInterface); |
michael@0 | 436 | }; |
michael@0 | 437 | |
michael@0 | 438 | // A working implementation of the OsStackTraceGetterInterface interface. |
michael@0 | 439 | class OsStackTraceGetter : public OsStackTraceGetterInterface { |
michael@0 | 440 | public: |
michael@0 | 441 | OsStackTraceGetter() : caller_frame_(NULL) {} |
michael@0 | 442 | |
michael@0 | 443 | virtual String CurrentStackTrace(int max_depth, int skip_count) |
michael@0 | 444 | GTEST_LOCK_EXCLUDED_(mutex_); |
michael@0 | 445 | |
michael@0 | 446 | virtual void UponLeavingGTest() GTEST_LOCK_EXCLUDED_(mutex_); |
michael@0 | 447 | |
michael@0 | 448 | // This string is inserted in place of stack frames that are part of |
michael@0 | 449 | // Google Test's implementation. |
michael@0 | 450 | static const char* const kElidedFramesMarker; |
michael@0 | 451 | |
michael@0 | 452 | private: |
michael@0 | 453 | Mutex mutex_; // protects all internal state |
michael@0 | 454 | |
michael@0 | 455 | // We save the stack frame below the frame that calls user code. |
michael@0 | 456 | // We do this because the address of the frame immediately below |
michael@0 | 457 | // the user code changes between the call to UponLeavingGTest() |
michael@0 | 458 | // and any calls to CurrentStackTrace() from within the user code. |
michael@0 | 459 | void* caller_frame_; |
michael@0 | 460 | |
michael@0 | 461 | GTEST_DISALLOW_COPY_AND_ASSIGN_(OsStackTraceGetter); |
michael@0 | 462 | }; |
michael@0 | 463 | |
michael@0 | 464 | // Information about a Google Test trace point. |
michael@0 | 465 | struct TraceInfo { |
michael@0 | 466 | const char* file; |
michael@0 | 467 | int line; |
michael@0 | 468 | String message; |
michael@0 | 469 | }; |
michael@0 | 470 | |
michael@0 | 471 | // This is the default global test part result reporter used in UnitTestImpl. |
michael@0 | 472 | // This class should only be used by UnitTestImpl. |
michael@0 | 473 | class DefaultGlobalTestPartResultReporter |
michael@0 | 474 | : public TestPartResultReporterInterface { |
michael@0 | 475 | public: |
michael@0 | 476 | explicit DefaultGlobalTestPartResultReporter(UnitTestImpl* unit_test); |
michael@0 | 477 | // Implements the TestPartResultReporterInterface. Reports the test part |
michael@0 | 478 | // result in the current test. |
michael@0 | 479 | virtual void ReportTestPartResult(const TestPartResult& result); |
michael@0 | 480 | |
michael@0 | 481 | private: |
michael@0 | 482 | UnitTestImpl* const unit_test_; |
michael@0 | 483 | |
michael@0 | 484 | GTEST_DISALLOW_COPY_AND_ASSIGN_(DefaultGlobalTestPartResultReporter); |
michael@0 | 485 | }; |
michael@0 | 486 | |
michael@0 | 487 | // This is the default per thread test part result reporter used in |
michael@0 | 488 | // UnitTestImpl. This class should only be used by UnitTestImpl. |
michael@0 | 489 | class DefaultPerThreadTestPartResultReporter |
michael@0 | 490 | : public TestPartResultReporterInterface { |
michael@0 | 491 | public: |
michael@0 | 492 | explicit DefaultPerThreadTestPartResultReporter(UnitTestImpl* unit_test); |
michael@0 | 493 | // Implements the TestPartResultReporterInterface. The implementation just |
michael@0 | 494 | // delegates to the current global test part result reporter of *unit_test_. |
michael@0 | 495 | virtual void ReportTestPartResult(const TestPartResult& result); |
michael@0 | 496 | |
michael@0 | 497 | private: |
michael@0 | 498 | UnitTestImpl* const unit_test_; |
michael@0 | 499 | |
michael@0 | 500 | GTEST_DISALLOW_COPY_AND_ASSIGN_(DefaultPerThreadTestPartResultReporter); |
michael@0 | 501 | }; |
michael@0 | 502 | |
michael@0 | 503 | // The private implementation of the UnitTest class. We don't protect |
michael@0 | 504 | // the methods under a mutex, as this class is not accessible by a |
michael@0 | 505 | // user and the UnitTest class that delegates work to this class does |
michael@0 | 506 | // proper locking. |
michael@0 | 507 | class GTEST_API_ UnitTestImpl { |
michael@0 | 508 | public: |
michael@0 | 509 | explicit UnitTestImpl(UnitTest* parent); |
michael@0 | 510 | virtual ~UnitTestImpl(); |
michael@0 | 511 | |
michael@0 | 512 | // There are two different ways to register your own TestPartResultReporter. |
michael@0 | 513 | // You can register your own repoter to listen either only for test results |
michael@0 | 514 | // from the current thread or for results from all threads. |
michael@0 | 515 | // By default, each per-thread test result repoter just passes a new |
michael@0 | 516 | // TestPartResult to the global test result reporter, which registers the |
michael@0 | 517 | // test part result for the currently running test. |
michael@0 | 518 | |
michael@0 | 519 | // Returns the global test part result reporter. |
michael@0 | 520 | TestPartResultReporterInterface* GetGlobalTestPartResultReporter(); |
michael@0 | 521 | |
michael@0 | 522 | // Sets the global test part result reporter. |
michael@0 | 523 | void SetGlobalTestPartResultReporter( |
michael@0 | 524 | TestPartResultReporterInterface* reporter); |
michael@0 | 525 | |
michael@0 | 526 | // Returns the test part result reporter for the current thread. |
michael@0 | 527 | TestPartResultReporterInterface* GetTestPartResultReporterForCurrentThread(); |
michael@0 | 528 | |
michael@0 | 529 | // Sets the test part result reporter for the current thread. |
michael@0 | 530 | void SetTestPartResultReporterForCurrentThread( |
michael@0 | 531 | TestPartResultReporterInterface* reporter); |
michael@0 | 532 | |
michael@0 | 533 | // Gets the number of successful test cases. |
michael@0 | 534 | int successful_test_case_count() const; |
michael@0 | 535 | |
michael@0 | 536 | // Gets the number of failed test cases. |
michael@0 | 537 | int failed_test_case_count() const; |
michael@0 | 538 | |
michael@0 | 539 | // Gets the number of all test cases. |
michael@0 | 540 | int total_test_case_count() const; |
michael@0 | 541 | |
michael@0 | 542 | // Gets the number of all test cases that contain at least one test |
michael@0 | 543 | // that should run. |
michael@0 | 544 | int test_case_to_run_count() const; |
michael@0 | 545 | |
michael@0 | 546 | // Gets the number of successful tests. |
michael@0 | 547 | int successful_test_count() const; |
michael@0 | 548 | |
michael@0 | 549 | // Gets the number of failed tests. |
michael@0 | 550 | int failed_test_count() const; |
michael@0 | 551 | |
michael@0 | 552 | // Gets the number of disabled tests. |
michael@0 | 553 | int disabled_test_count() const; |
michael@0 | 554 | |
michael@0 | 555 | // Gets the number of all tests. |
michael@0 | 556 | int total_test_count() const; |
michael@0 | 557 | |
michael@0 | 558 | // Gets the number of tests that should run. |
michael@0 | 559 | int test_to_run_count() const; |
michael@0 | 560 | |
michael@0 | 561 | // Gets the time of the test program start, in ms from the start of the |
michael@0 | 562 | // UNIX epoch. |
michael@0 | 563 | TimeInMillis start_timestamp() const { return start_timestamp_; } |
michael@0 | 564 | |
michael@0 | 565 | // Gets the elapsed time, in milliseconds. |
michael@0 | 566 | TimeInMillis elapsed_time() const { return elapsed_time_; } |
michael@0 | 567 | |
michael@0 | 568 | // Returns true iff the unit test passed (i.e. all test cases passed). |
michael@0 | 569 | bool Passed() const { return !Failed(); } |
michael@0 | 570 | |
michael@0 | 571 | // Returns true iff the unit test failed (i.e. some test case failed |
michael@0 | 572 | // or something outside of all tests failed). |
michael@0 | 573 | bool Failed() const { |
michael@0 | 574 | return failed_test_case_count() > 0 || ad_hoc_test_result()->Failed(); |
michael@0 | 575 | } |
michael@0 | 576 | |
michael@0 | 577 | // Gets the i-th test case among all the test cases. i can range from 0 to |
michael@0 | 578 | // total_test_case_count() - 1. If i is not in that range, returns NULL. |
michael@0 | 579 | const TestCase* GetTestCase(int i) const { |
michael@0 | 580 | const int index = GetElementOr(test_case_indices_, i, -1); |
michael@0 | 581 | return index < 0 ? NULL : test_cases_[i]; |
michael@0 | 582 | } |
michael@0 | 583 | |
michael@0 | 584 | // Gets the i-th test case among all the test cases. i can range from 0 to |
michael@0 | 585 | // total_test_case_count() - 1. If i is not in that range, returns NULL. |
michael@0 | 586 | TestCase* GetMutableTestCase(int i) { |
michael@0 | 587 | const int index = GetElementOr(test_case_indices_, i, -1); |
michael@0 | 588 | return index < 0 ? NULL : test_cases_[index]; |
michael@0 | 589 | } |
michael@0 | 590 | |
michael@0 | 591 | // Provides access to the event listener list. |
michael@0 | 592 | TestEventListeners* listeners() { return &listeners_; } |
michael@0 | 593 | |
michael@0 | 594 | // Returns the TestResult for the test that's currently running, or |
michael@0 | 595 | // the TestResult for the ad hoc test if no test is running. |
michael@0 | 596 | TestResult* current_test_result(); |
michael@0 | 597 | |
michael@0 | 598 | // Returns the TestResult for the ad hoc test. |
michael@0 | 599 | const TestResult* ad_hoc_test_result() const { return &ad_hoc_test_result_; } |
michael@0 | 600 | |
michael@0 | 601 | // Sets the OS stack trace getter. |
michael@0 | 602 | // |
michael@0 | 603 | // Does nothing if the input and the current OS stack trace getter |
michael@0 | 604 | // are the same; otherwise, deletes the old getter and makes the |
michael@0 | 605 | // input the current getter. |
michael@0 | 606 | void set_os_stack_trace_getter(OsStackTraceGetterInterface* getter); |
michael@0 | 607 | |
michael@0 | 608 | // Returns the current OS stack trace getter if it is not NULL; |
michael@0 | 609 | // otherwise, creates an OsStackTraceGetter, makes it the current |
michael@0 | 610 | // getter, and returns it. |
michael@0 | 611 | OsStackTraceGetterInterface* os_stack_trace_getter(); |
michael@0 | 612 | |
michael@0 | 613 | // Returns the current OS stack trace as a String. |
michael@0 | 614 | // |
michael@0 | 615 | // The maximum number of stack frames to be included is specified by |
michael@0 | 616 | // the gtest_stack_trace_depth flag. The skip_count parameter |
michael@0 | 617 | // specifies the number of top frames to be skipped, which doesn't |
michael@0 | 618 | // count against the number of frames to be included. |
michael@0 | 619 | // |
michael@0 | 620 | // For example, if Foo() calls Bar(), which in turn calls |
michael@0 | 621 | // CurrentOsStackTraceExceptTop(1), Foo() will be included in the |
michael@0 | 622 | // trace but Bar() and CurrentOsStackTraceExceptTop() won't. |
michael@0 | 623 | String CurrentOsStackTraceExceptTop(int skip_count) GTEST_NO_INLINE_; |
michael@0 | 624 | |
michael@0 | 625 | // Finds and returns a TestCase with the given name. If one doesn't |
michael@0 | 626 | // exist, creates one and returns it. |
michael@0 | 627 | // |
michael@0 | 628 | // Arguments: |
michael@0 | 629 | // |
michael@0 | 630 | // test_case_name: name of the test case |
michael@0 | 631 | // type_param: the name of the test's type parameter, or NULL if |
michael@0 | 632 | // this is not a typed or a type-parameterized test. |
michael@0 | 633 | // set_up_tc: pointer to the function that sets up the test case |
michael@0 | 634 | // tear_down_tc: pointer to the function that tears down the test case |
michael@0 | 635 | TestCase* GetTestCase(const char* test_case_name, |
michael@0 | 636 | const char* type_param, |
michael@0 | 637 | Test::SetUpTestCaseFunc set_up_tc, |
michael@0 | 638 | Test::TearDownTestCaseFunc tear_down_tc); |
michael@0 | 639 | |
michael@0 | 640 | // Adds a TestInfo to the unit test. |
michael@0 | 641 | // |
michael@0 | 642 | // Arguments: |
michael@0 | 643 | // |
michael@0 | 644 | // set_up_tc: pointer to the function that sets up the test case |
michael@0 | 645 | // tear_down_tc: pointer to the function that tears down the test case |
michael@0 | 646 | // test_info: the TestInfo object |
michael@0 | 647 | void AddTestInfo(Test::SetUpTestCaseFunc set_up_tc, |
michael@0 | 648 | Test::TearDownTestCaseFunc tear_down_tc, |
michael@0 | 649 | TestInfo* test_info) { |
michael@0 | 650 | // In order to support thread-safe death tests, we need to |
michael@0 | 651 | // remember the original working directory when the test program |
michael@0 | 652 | // was first invoked. We cannot do this in RUN_ALL_TESTS(), as |
michael@0 | 653 | // the user may have changed the current directory before calling |
michael@0 | 654 | // RUN_ALL_TESTS(). Therefore we capture the current directory in |
michael@0 | 655 | // AddTestInfo(), which is called to register a TEST or TEST_F |
michael@0 | 656 | // before main() is reached. |
michael@0 | 657 | if (original_working_dir_.IsEmpty()) { |
michael@0 | 658 | original_working_dir_.Set(FilePath::GetCurrentDir()); |
michael@0 | 659 | GTEST_CHECK_(!original_working_dir_.IsEmpty()) |
michael@0 | 660 | << "Failed to get the current working directory."; |
michael@0 | 661 | } |
michael@0 | 662 | |
michael@0 | 663 | GetTestCase(test_info->test_case_name(), |
michael@0 | 664 | test_info->type_param(), |
michael@0 | 665 | set_up_tc, |
michael@0 | 666 | tear_down_tc)->AddTestInfo(test_info); |
michael@0 | 667 | } |
michael@0 | 668 | |
michael@0 | 669 | #if GTEST_HAS_PARAM_TEST |
michael@0 | 670 | // Returns ParameterizedTestCaseRegistry object used to keep track of |
michael@0 | 671 | // value-parameterized tests and instantiate and register them. |
michael@0 | 672 | internal::ParameterizedTestCaseRegistry& parameterized_test_registry() { |
michael@0 | 673 | return parameterized_test_registry_; |
michael@0 | 674 | } |
michael@0 | 675 | #endif // GTEST_HAS_PARAM_TEST |
michael@0 | 676 | |
michael@0 | 677 | // Sets the TestCase object for the test that's currently running. |
michael@0 | 678 | void set_current_test_case(TestCase* a_current_test_case) { |
michael@0 | 679 | current_test_case_ = a_current_test_case; |
michael@0 | 680 | } |
michael@0 | 681 | |
michael@0 | 682 | // Sets the TestInfo object for the test that's currently running. If |
michael@0 | 683 | // current_test_info is NULL, the assertion results will be stored in |
michael@0 | 684 | // ad_hoc_test_result_. |
michael@0 | 685 | void set_current_test_info(TestInfo* a_current_test_info) { |
michael@0 | 686 | current_test_info_ = a_current_test_info; |
michael@0 | 687 | } |
michael@0 | 688 | |
michael@0 | 689 | // Registers all parameterized tests defined using TEST_P and |
michael@0 | 690 | // INSTANTIATE_TEST_CASE_P, creating regular tests for each test/parameter |
michael@0 | 691 | // combination. This method can be called more then once; it has guards |
michael@0 | 692 | // protecting from registering the tests more then once. If |
michael@0 | 693 | // value-parameterized tests are disabled, RegisterParameterizedTests is |
michael@0 | 694 | // present but does nothing. |
michael@0 | 695 | void RegisterParameterizedTests(); |
michael@0 | 696 | |
michael@0 | 697 | // Runs all tests in this UnitTest object, prints the result, and |
michael@0 | 698 | // returns true if all tests are successful. If any exception is |
michael@0 | 699 | // thrown during a test, this test is considered to be failed, but |
michael@0 | 700 | // the rest of the tests will still be run. |
michael@0 | 701 | bool RunAllTests(); |
michael@0 | 702 | |
michael@0 | 703 | // Clears the results of all tests, except the ad hoc tests. |
michael@0 | 704 | void ClearNonAdHocTestResult() { |
michael@0 | 705 | ForEach(test_cases_, TestCase::ClearTestCaseResult); |
michael@0 | 706 | } |
michael@0 | 707 | |
michael@0 | 708 | // Clears the results of ad-hoc test assertions. |
michael@0 | 709 | void ClearAdHocTestResult() { |
michael@0 | 710 | ad_hoc_test_result_.Clear(); |
michael@0 | 711 | } |
michael@0 | 712 | |
michael@0 | 713 | enum ReactionToSharding { |
michael@0 | 714 | HONOR_SHARDING_PROTOCOL, |
michael@0 | 715 | IGNORE_SHARDING_PROTOCOL |
michael@0 | 716 | }; |
michael@0 | 717 | |
michael@0 | 718 | // Matches the full name of each test against the user-specified |
michael@0 | 719 | // filter to decide whether the test should run, then records the |
michael@0 | 720 | // result in each TestCase and TestInfo object. |
michael@0 | 721 | // If shard_tests == HONOR_SHARDING_PROTOCOL, further filters tests |
michael@0 | 722 | // based on sharding variables in the environment. |
michael@0 | 723 | // Returns the number of tests that should run. |
michael@0 | 724 | int FilterTests(ReactionToSharding shard_tests); |
michael@0 | 725 | |
michael@0 | 726 | // Prints the names of the tests matching the user-specified filter flag. |
michael@0 | 727 | void ListTestsMatchingFilter(); |
michael@0 | 728 | |
michael@0 | 729 | const TestCase* current_test_case() const { return current_test_case_; } |
michael@0 | 730 | TestInfo* current_test_info() { return current_test_info_; } |
michael@0 | 731 | const TestInfo* current_test_info() const { return current_test_info_; } |
michael@0 | 732 | |
michael@0 | 733 | // Returns the vector of environments that need to be set-up/torn-down |
michael@0 | 734 | // before/after the tests are run. |
michael@0 | 735 | std::vector<Environment*>& environments() { return environments_; } |
michael@0 | 736 | |
michael@0 | 737 | // Getters for the per-thread Google Test trace stack. |
michael@0 | 738 | std::vector<TraceInfo>& gtest_trace_stack() { |
michael@0 | 739 | return *(gtest_trace_stack_.pointer()); |
michael@0 | 740 | } |
michael@0 | 741 | const std::vector<TraceInfo>& gtest_trace_stack() const { |
michael@0 | 742 | return gtest_trace_stack_.get(); |
michael@0 | 743 | } |
michael@0 | 744 | |
michael@0 | 745 | #if GTEST_HAS_DEATH_TEST |
michael@0 | 746 | void InitDeathTestSubprocessControlInfo() { |
michael@0 | 747 | internal_run_death_test_flag_.reset(ParseInternalRunDeathTestFlag()); |
michael@0 | 748 | } |
michael@0 | 749 | // Returns a pointer to the parsed --gtest_internal_run_death_test |
michael@0 | 750 | // flag, or NULL if that flag was not specified. |
michael@0 | 751 | // This information is useful only in a death test child process. |
michael@0 | 752 | // Must not be called before a call to InitGoogleTest. |
michael@0 | 753 | const InternalRunDeathTestFlag* internal_run_death_test_flag() const { |
michael@0 | 754 | return internal_run_death_test_flag_.get(); |
michael@0 | 755 | } |
michael@0 | 756 | |
michael@0 | 757 | // Returns a pointer to the current death test factory. |
michael@0 | 758 | internal::DeathTestFactory* death_test_factory() { |
michael@0 | 759 | return death_test_factory_.get(); |
michael@0 | 760 | } |
michael@0 | 761 | |
michael@0 | 762 | void SuppressTestEventsIfInSubprocess(); |
michael@0 | 763 | |
michael@0 | 764 | friend class ReplaceDeathTestFactory; |
michael@0 | 765 | #endif // GTEST_HAS_DEATH_TEST |
michael@0 | 766 | |
michael@0 | 767 | // Initializes the event listener performing XML output as specified by |
michael@0 | 768 | // UnitTestOptions. Must not be called before InitGoogleTest. |
michael@0 | 769 | void ConfigureXmlOutput(); |
michael@0 | 770 | |
michael@0 | 771 | #if GTEST_CAN_STREAM_RESULTS_ |
michael@0 | 772 | // Initializes the event listener for streaming test results to a socket. |
michael@0 | 773 | // Must not be called before InitGoogleTest. |
michael@0 | 774 | void ConfigureStreamingOutput(); |
michael@0 | 775 | #endif |
michael@0 | 776 | |
michael@0 | 777 | // Performs initialization dependent upon flag values obtained in |
michael@0 | 778 | // ParseGoogleTestFlagsOnly. Is called from InitGoogleTest after the call to |
michael@0 | 779 | // ParseGoogleTestFlagsOnly. In case a user neglects to call InitGoogleTest |
michael@0 | 780 | // this function is also called from RunAllTests. Since this function can be |
michael@0 | 781 | // called more than once, it has to be idempotent. |
michael@0 | 782 | void PostFlagParsingInit(); |
michael@0 | 783 | |
michael@0 | 784 | // Gets the random seed used at the start of the current test iteration. |
michael@0 | 785 | int random_seed() const { return random_seed_; } |
michael@0 | 786 | |
michael@0 | 787 | // Gets the random number generator. |
michael@0 | 788 | internal::Random* random() { return &random_; } |
michael@0 | 789 | |
michael@0 | 790 | // Shuffles all test cases, and the tests within each test case, |
michael@0 | 791 | // making sure that death tests are still run first. |
michael@0 | 792 | void ShuffleTests(); |
michael@0 | 793 | |
michael@0 | 794 | // Restores the test cases and tests to their order before the first shuffle. |
michael@0 | 795 | void UnshuffleTests(); |
michael@0 | 796 | |
michael@0 | 797 | // Returns the value of GTEST_FLAG(catch_exceptions) at the moment |
michael@0 | 798 | // UnitTest::Run() starts. |
michael@0 | 799 | bool catch_exceptions() const { return catch_exceptions_; } |
michael@0 | 800 | |
michael@0 | 801 | private: |
michael@0 | 802 | friend class ::testing::UnitTest; |
michael@0 | 803 | |
michael@0 | 804 | // Used by UnitTest::Run() to capture the state of |
michael@0 | 805 | // GTEST_FLAG(catch_exceptions) at the moment it starts. |
michael@0 | 806 | void set_catch_exceptions(bool value) { catch_exceptions_ = value; } |
michael@0 | 807 | |
michael@0 | 808 | // The UnitTest object that owns this implementation object. |
michael@0 | 809 | UnitTest* const parent_; |
michael@0 | 810 | |
michael@0 | 811 | // The working directory when the first TEST() or TEST_F() was |
michael@0 | 812 | // executed. |
michael@0 | 813 | internal::FilePath original_working_dir_; |
michael@0 | 814 | |
michael@0 | 815 | // The default test part result reporters. |
michael@0 | 816 | DefaultGlobalTestPartResultReporter default_global_test_part_result_reporter_; |
michael@0 | 817 | DefaultPerThreadTestPartResultReporter |
michael@0 | 818 | default_per_thread_test_part_result_reporter_; |
michael@0 | 819 | |
michael@0 | 820 | // Points to (but doesn't own) the global test part result reporter. |
michael@0 | 821 | TestPartResultReporterInterface* global_test_part_result_repoter_; |
michael@0 | 822 | |
michael@0 | 823 | // Protects read and write access to global_test_part_result_reporter_. |
michael@0 | 824 | internal::Mutex global_test_part_result_reporter_mutex_; |
michael@0 | 825 | |
michael@0 | 826 | // Points to (but doesn't own) the per-thread test part result reporter. |
michael@0 | 827 | internal::ThreadLocal<TestPartResultReporterInterface*> |
michael@0 | 828 | per_thread_test_part_result_reporter_; |
michael@0 | 829 | |
michael@0 | 830 | // The vector of environments that need to be set-up/torn-down |
michael@0 | 831 | // before/after the tests are run. |
michael@0 | 832 | std::vector<Environment*> environments_; |
michael@0 | 833 | |
michael@0 | 834 | // The vector of TestCases in their original order. It owns the |
michael@0 | 835 | // elements in the vector. |
michael@0 | 836 | std::vector<TestCase*> test_cases_; |
michael@0 | 837 | |
michael@0 | 838 | // Provides a level of indirection for the test case list to allow |
michael@0 | 839 | // easy shuffling and restoring the test case order. The i-th |
michael@0 | 840 | // element of this vector is the index of the i-th test case in the |
michael@0 | 841 | // shuffled order. |
michael@0 | 842 | std::vector<int> test_case_indices_; |
michael@0 | 843 | |
michael@0 | 844 | #if GTEST_HAS_PARAM_TEST |
michael@0 | 845 | // ParameterizedTestRegistry object used to register value-parameterized |
michael@0 | 846 | // tests. |
michael@0 | 847 | internal::ParameterizedTestCaseRegistry parameterized_test_registry_; |
michael@0 | 848 | |
michael@0 | 849 | // Indicates whether RegisterParameterizedTests() has been called already. |
michael@0 | 850 | bool parameterized_tests_registered_; |
michael@0 | 851 | #endif // GTEST_HAS_PARAM_TEST |
michael@0 | 852 | |
michael@0 | 853 | // Index of the last death test case registered. Initially -1. |
michael@0 | 854 | int last_death_test_case_; |
michael@0 | 855 | |
michael@0 | 856 | // This points to the TestCase for the currently running test. It |
michael@0 | 857 | // changes as Google Test goes through one test case after another. |
michael@0 | 858 | // When no test is running, this is set to NULL and Google Test |
michael@0 | 859 | // stores assertion results in ad_hoc_test_result_. Initially NULL. |
michael@0 | 860 | TestCase* current_test_case_; |
michael@0 | 861 | |
michael@0 | 862 | // This points to the TestInfo for the currently running test. It |
michael@0 | 863 | // changes as Google Test goes through one test after another. When |
michael@0 | 864 | // no test is running, this is set to NULL and Google Test stores |
michael@0 | 865 | // assertion results in ad_hoc_test_result_. Initially NULL. |
michael@0 | 866 | TestInfo* current_test_info_; |
michael@0 | 867 | |
michael@0 | 868 | // Normally, a user only writes assertions inside a TEST or TEST_F, |
michael@0 | 869 | // or inside a function called by a TEST or TEST_F. Since Google |
michael@0 | 870 | // Test keeps track of which test is current running, it can |
michael@0 | 871 | // associate such an assertion with the test it belongs to. |
michael@0 | 872 | // |
michael@0 | 873 | // If an assertion is encountered when no TEST or TEST_F is running, |
michael@0 | 874 | // Google Test attributes the assertion result to an imaginary "ad hoc" |
michael@0 | 875 | // test, and records the result in ad_hoc_test_result_. |
michael@0 | 876 | TestResult ad_hoc_test_result_; |
michael@0 | 877 | |
michael@0 | 878 | // The list of event listeners that can be used to track events inside |
michael@0 | 879 | // Google Test. |
michael@0 | 880 | TestEventListeners listeners_; |
michael@0 | 881 | |
michael@0 | 882 | // The OS stack trace getter. Will be deleted when the UnitTest |
michael@0 | 883 | // object is destructed. By default, an OsStackTraceGetter is used, |
michael@0 | 884 | // but the user can set this field to use a custom getter if that is |
michael@0 | 885 | // desired. |
michael@0 | 886 | OsStackTraceGetterInterface* os_stack_trace_getter_; |
michael@0 | 887 | |
michael@0 | 888 | // True iff PostFlagParsingInit() has been called. |
michael@0 | 889 | bool post_flag_parse_init_performed_; |
michael@0 | 890 | |
michael@0 | 891 | // The random number seed used at the beginning of the test run. |
michael@0 | 892 | int random_seed_; |
michael@0 | 893 | |
michael@0 | 894 | // Our random number generator. |
michael@0 | 895 | internal::Random random_; |
michael@0 | 896 | |
michael@0 | 897 | // The time of the test program start, in ms from the start of the |
michael@0 | 898 | // UNIX epoch. |
michael@0 | 899 | TimeInMillis start_timestamp_; |
michael@0 | 900 | |
michael@0 | 901 | // How long the test took to run, in milliseconds. |
michael@0 | 902 | TimeInMillis elapsed_time_; |
michael@0 | 903 | |
michael@0 | 904 | #if GTEST_HAS_DEATH_TEST |
michael@0 | 905 | // The decomposed components of the gtest_internal_run_death_test flag, |
michael@0 | 906 | // parsed when RUN_ALL_TESTS is called. |
michael@0 | 907 | internal::scoped_ptr<InternalRunDeathTestFlag> internal_run_death_test_flag_; |
michael@0 | 908 | internal::scoped_ptr<internal::DeathTestFactory> death_test_factory_; |
michael@0 | 909 | #endif // GTEST_HAS_DEATH_TEST |
michael@0 | 910 | |
michael@0 | 911 | // A per-thread stack of traces created by the SCOPED_TRACE() macro. |
michael@0 | 912 | internal::ThreadLocal<std::vector<TraceInfo> > gtest_trace_stack_; |
michael@0 | 913 | |
michael@0 | 914 | // The value of GTEST_FLAG(catch_exceptions) at the moment RunAllTests() |
michael@0 | 915 | // starts. |
michael@0 | 916 | bool catch_exceptions_; |
michael@0 | 917 | |
michael@0 | 918 | GTEST_DISALLOW_COPY_AND_ASSIGN_(UnitTestImpl); |
michael@0 | 919 | }; // class UnitTestImpl |
michael@0 | 920 | |
michael@0 | 921 | // Convenience function for accessing the global UnitTest |
michael@0 | 922 | // implementation object. |
michael@0 | 923 | inline UnitTestImpl* GetUnitTestImpl() { |
michael@0 | 924 | return UnitTest::GetInstance()->impl(); |
michael@0 | 925 | } |
michael@0 | 926 | |
michael@0 | 927 | #if GTEST_USES_SIMPLE_RE |
michael@0 | 928 | |
michael@0 | 929 | // Internal helper functions for implementing the simple regular |
michael@0 | 930 | // expression matcher. |
michael@0 | 931 | GTEST_API_ bool IsInSet(char ch, const char* str); |
michael@0 | 932 | GTEST_API_ bool IsAsciiDigit(char ch); |
michael@0 | 933 | GTEST_API_ bool IsAsciiPunct(char ch); |
michael@0 | 934 | GTEST_API_ bool IsRepeat(char ch); |
michael@0 | 935 | GTEST_API_ bool IsAsciiWhiteSpace(char ch); |
michael@0 | 936 | GTEST_API_ bool IsAsciiWordChar(char ch); |
michael@0 | 937 | GTEST_API_ bool IsValidEscape(char ch); |
michael@0 | 938 | GTEST_API_ bool AtomMatchesChar(bool escaped, char pattern, char ch); |
michael@0 | 939 | GTEST_API_ bool ValidateRegex(const char* regex); |
michael@0 | 940 | GTEST_API_ bool MatchRegexAtHead(const char* regex, const char* str); |
michael@0 | 941 | GTEST_API_ bool MatchRepetitionAndRegexAtHead( |
michael@0 | 942 | bool escaped, char ch, char repeat, const char* regex, const char* str); |
michael@0 | 943 | GTEST_API_ bool MatchRegexAnywhere(const char* regex, const char* str); |
michael@0 | 944 | |
michael@0 | 945 | #endif // GTEST_USES_SIMPLE_RE |
michael@0 | 946 | |
michael@0 | 947 | // Parses the command line for Google Test flags, without initializing |
michael@0 | 948 | // other parts of Google Test. |
michael@0 | 949 | GTEST_API_ void ParseGoogleTestFlagsOnly(int* argc, char** argv); |
michael@0 | 950 | GTEST_API_ void ParseGoogleTestFlagsOnly(int* argc, wchar_t** argv); |
michael@0 | 951 | |
michael@0 | 952 | #if GTEST_HAS_DEATH_TEST |
michael@0 | 953 | |
michael@0 | 954 | // Returns the message describing the last system error, regardless of the |
michael@0 | 955 | // platform. |
michael@0 | 956 | GTEST_API_ String GetLastErrnoDescription(); |
michael@0 | 957 | |
michael@0 | 958 | # if GTEST_OS_WINDOWS |
michael@0 | 959 | // Provides leak-safe Windows kernel handle ownership. |
michael@0 | 960 | class AutoHandle { |
michael@0 | 961 | public: |
michael@0 | 962 | AutoHandle() : handle_(INVALID_HANDLE_VALUE) {} |
michael@0 | 963 | explicit AutoHandle(HANDLE handle) : handle_(handle) {} |
michael@0 | 964 | |
michael@0 | 965 | ~AutoHandle() { Reset(); } |
michael@0 | 966 | |
michael@0 | 967 | HANDLE Get() const { return handle_; } |
michael@0 | 968 | void Reset() { Reset(INVALID_HANDLE_VALUE); } |
michael@0 | 969 | void Reset(HANDLE handle) { |
michael@0 | 970 | if (handle != handle_) { |
michael@0 | 971 | if (handle_ != INVALID_HANDLE_VALUE) |
michael@0 | 972 | ::CloseHandle(handle_); |
michael@0 | 973 | handle_ = handle; |
michael@0 | 974 | } |
michael@0 | 975 | } |
michael@0 | 976 | |
michael@0 | 977 | private: |
michael@0 | 978 | HANDLE handle_; |
michael@0 | 979 | |
michael@0 | 980 | GTEST_DISALLOW_COPY_AND_ASSIGN_(AutoHandle); |
michael@0 | 981 | }; |
michael@0 | 982 | # endif // GTEST_OS_WINDOWS |
michael@0 | 983 | |
michael@0 | 984 | // Attempts to parse a string into a positive integer pointed to by the |
michael@0 | 985 | // number parameter. Returns true if that is possible. |
michael@0 | 986 | // GTEST_HAS_DEATH_TEST implies that we have ::std::string, so we can use |
michael@0 | 987 | // it here. |
michael@0 | 988 | template <typename Integer> |
michael@0 | 989 | bool ParseNaturalNumber(const ::std::string& str, Integer* number) { |
michael@0 | 990 | // Fail fast if the given string does not begin with a digit; |
michael@0 | 991 | // this bypasses strtoXXX's "optional leading whitespace and plus |
michael@0 | 992 | // or minus sign" semantics, which are undesirable here. |
michael@0 | 993 | if (str.empty() || !IsDigit(str[0])) { |
michael@0 | 994 | return false; |
michael@0 | 995 | } |
michael@0 | 996 | errno = 0; |
michael@0 | 997 | |
michael@0 | 998 | char* end; |
michael@0 | 999 | // BiggestConvertible is the largest integer type that system-provided |
michael@0 | 1000 | // string-to-number conversion routines can return. |
michael@0 | 1001 | |
michael@0 | 1002 | # if GTEST_OS_WINDOWS && !defined(__GNUC__) |
michael@0 | 1003 | |
michael@0 | 1004 | // MSVC and C++ Builder define __int64 instead of the standard long long. |
michael@0 | 1005 | typedef unsigned __int64 BiggestConvertible; |
michael@0 | 1006 | const BiggestConvertible parsed = _strtoui64(str.c_str(), &end, 10); |
michael@0 | 1007 | |
michael@0 | 1008 | # else |
michael@0 | 1009 | |
michael@0 | 1010 | typedef unsigned long long BiggestConvertible; // NOLINT |
michael@0 | 1011 | const BiggestConvertible parsed = strtoull(str.c_str(), &end, 10); |
michael@0 | 1012 | |
michael@0 | 1013 | # endif // GTEST_OS_WINDOWS && !defined(__GNUC__) |
michael@0 | 1014 | |
michael@0 | 1015 | const bool parse_success = *end == '\0' && errno == 0; |
michael@0 | 1016 | |
michael@0 | 1017 | // TODO(vladl@google.com): Convert this to compile time assertion when it is |
michael@0 | 1018 | // available. |
michael@0 | 1019 | GTEST_CHECK_(sizeof(Integer) <= sizeof(parsed)); |
michael@0 | 1020 | |
michael@0 | 1021 | const Integer result = static_cast<Integer>(parsed); |
michael@0 | 1022 | if (parse_success && static_cast<BiggestConvertible>(result) == parsed) { |
michael@0 | 1023 | *number = result; |
michael@0 | 1024 | return true; |
michael@0 | 1025 | } |
michael@0 | 1026 | return false; |
michael@0 | 1027 | } |
michael@0 | 1028 | #endif // GTEST_HAS_DEATH_TEST |
michael@0 | 1029 | |
michael@0 | 1030 | // TestResult contains some private methods that should be hidden from |
michael@0 | 1031 | // Google Test user but are required for testing. This class allow our tests |
michael@0 | 1032 | // to access them. |
michael@0 | 1033 | // |
michael@0 | 1034 | // This class is supplied only for the purpose of testing Google Test's own |
michael@0 | 1035 | // constructs. Do not use it in user tests, either directly or indirectly. |
michael@0 | 1036 | class TestResultAccessor { |
michael@0 | 1037 | public: |
michael@0 | 1038 | static void RecordProperty(TestResult* test_result, |
michael@0 | 1039 | const TestProperty& property) { |
michael@0 | 1040 | test_result->RecordProperty(property); |
michael@0 | 1041 | } |
michael@0 | 1042 | |
michael@0 | 1043 | static void ClearTestPartResults(TestResult* test_result) { |
michael@0 | 1044 | test_result->ClearTestPartResults(); |
michael@0 | 1045 | } |
michael@0 | 1046 | |
michael@0 | 1047 | static const std::vector<testing::TestPartResult>& test_part_results( |
michael@0 | 1048 | const TestResult& test_result) { |
michael@0 | 1049 | return test_result.test_part_results(); |
michael@0 | 1050 | } |
michael@0 | 1051 | }; |
michael@0 | 1052 | |
michael@0 | 1053 | } // namespace internal |
michael@0 | 1054 | } // namespace testing |
michael@0 | 1055 | |
michael@0 | 1056 | #endif // GTEST_SRC_GTEST_INTERNAL_INL_H_ |