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