1 // Copyright 2008 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 // Author: vladl@google.com (Vlad Losev) 31 32 // Type and function utilities for implementing parameterized tests. 33 34 #ifndef GTEST_INCLUDE_GTEST_INTERNAL_GTEST_PARAM_UTIL_H_ 35 #define GTEST_INCLUDE_GTEST_INTERNAL_GTEST_PARAM_UTIL_H_ 36 37 #include <iterator> 38 #include <utility> 39 #include <vector> 40 41 // scripts/fuse_gtest.py depends on gtest's own header being #included 42 // *unconditionally*. Therefore these #includes cannot be moved 43 // inside #if GTEST_HAS_PARAM_TEST. 44 #include <gtest/internal/gtest-internal.h> 45 #include <gtest/internal/gtest-linked_ptr.h> 46 #include <gtest/internal/gtest-port.h> 47 48 #if GTEST_HAS_PARAM_TEST 49 50 namespace testing { 51 namespace internal { 52 53 // INTERNAL IMPLEMENTATION - DO NOT USE IN USER CODE. 54 // 55 // Outputs a message explaining invalid registration of different 56 // fixture class for the same test case. This may happen when 57 // TEST_P macro is used to define two tests with the same name 58 // but in different namespaces. 59 GTEST_API_ void ReportInvalidTestCaseType(const char* test_case_name, 60 const char* file, int line); 61 62 template <typename> class ParamGeneratorInterface; 63 template <typename> class ParamGenerator; 64 65 // Interface for iterating over elements provided by an implementation 66 // of ParamGeneratorInterface<T>. 67 template <typename T> 68 class ParamIteratorInterface { 69 public: ~ParamIteratorInterface()70 virtual ~ParamIteratorInterface() {} 71 // A pointer to the base generator instance. 72 // Used only for the purposes of iterator comparison 73 // to make sure that two iterators belong to the same generator. 74 virtual const ParamGeneratorInterface<T>* BaseGenerator() const = 0; 75 // Advances iterator to point to the next element 76 // provided by the generator. The caller is responsible 77 // for not calling Advance() on an iterator equal to 78 // BaseGenerator()->End(). 79 virtual void Advance() = 0; 80 // Clones the iterator object. Used for implementing copy semantics 81 // of ParamIterator<T>. 82 virtual ParamIteratorInterface* Clone() const = 0; 83 // Dereferences the current iterator and provides (read-only) access 84 // to the pointed value. It is the caller's responsibility not to call 85 // Current() on an iterator equal to BaseGenerator()->End(). 86 // Used for implementing ParamGenerator<T>::operator*(). 87 virtual const T* Current() const = 0; 88 // Determines whether the given iterator and other point to the same 89 // element in the sequence generated by the generator. 90 // Used for implementing ParamGenerator<T>::operator==(). 91 virtual bool Equals(const ParamIteratorInterface& other) const = 0; 92 }; 93 94 // Class iterating over elements provided by an implementation of 95 // ParamGeneratorInterface<T>. It wraps ParamIteratorInterface<T> 96 // and implements the const forward iterator concept. 97 template <typename T> 98 class ParamIterator { 99 public: 100 typedef T value_type; 101 typedef const T& reference; 102 typedef ptrdiff_t difference_type; 103 104 // ParamIterator assumes ownership of the impl_ pointer. ParamIterator(const ParamIterator & other)105 ParamIterator(const ParamIterator& other) : impl_(other.impl_->Clone()) {} 106 ParamIterator& operator=(const ParamIterator& other) { 107 if (this != &other) 108 impl_.reset(other.impl_->Clone()); 109 return *this; 110 } 111 112 const T& operator*() const { return *impl_->Current(); } 113 const T* operator->() const { return impl_->Current(); } 114 // Prefix version of operator++. 115 ParamIterator& operator++() { 116 impl_->Advance(); 117 return *this; 118 } 119 // Postfix version of operator++. 120 ParamIterator operator++(int /*unused*/) { 121 ParamIteratorInterface<T>* clone = impl_->Clone(); 122 impl_->Advance(); 123 return ParamIterator(clone); 124 } 125 bool operator==(const ParamIterator& other) const { 126 return impl_.get() == other.impl_.get() || impl_->Equals(*other.impl_); 127 } 128 bool operator!=(const ParamIterator& other) const { 129 return !(*this == other); 130 } 131 132 private: 133 friend class ParamGenerator<T>; ParamIterator(ParamIteratorInterface<T> * impl)134 explicit ParamIterator(ParamIteratorInterface<T>* impl) : impl_(impl) {} 135 scoped_ptr<ParamIteratorInterface<T> > impl_; 136 }; 137 138 // ParamGeneratorInterface<T> is the binary interface to access generators 139 // defined in other translation units. 140 template <typename T> 141 class ParamGeneratorInterface { 142 public: 143 typedef T ParamType; 144 ~ParamGeneratorInterface()145 virtual ~ParamGeneratorInterface() {} 146 147 // Generator interface definition 148 virtual ParamIteratorInterface<T>* Begin() const = 0; 149 virtual ParamIteratorInterface<T>* End() const = 0; 150 }; 151 152 // Wraps ParamGeneratorInterface<T> and provides general generator syntax 153 // compatible with the STL Container concept. 154 // This class implements copy initialization semantics and the contained 155 // ParamGeneratorInterface<T> instance is shared among all copies 156 // of the original object. This is possible because that instance is immutable. 157 template<typename T> 158 class ParamGenerator { 159 public: 160 typedef ParamIterator<T> iterator; 161 ParamGenerator(ParamGeneratorInterface<T> * impl)162 explicit ParamGenerator(ParamGeneratorInterface<T>* impl) : impl_(impl) {} ParamGenerator(const ParamGenerator & other)163 ParamGenerator(const ParamGenerator& other) : impl_(other.impl_) {} 164 165 ParamGenerator& operator=(const ParamGenerator& other) { 166 impl_ = other.impl_; 167 return *this; 168 } 169 begin()170 iterator begin() const { return iterator(impl_->Begin()); } end()171 iterator end() const { return iterator(impl_->End()); } 172 173 private: 174 ::testing::internal::linked_ptr<const ParamGeneratorInterface<T> > impl_; 175 }; 176 177 // Generates values from a range of two comparable values. Can be used to 178 // generate sequences of user-defined types that implement operator+() and 179 // operator<(). 180 // This class is used in the Range() function. 181 template <typename T, typename IncrementT> 182 class RangeGenerator : public ParamGeneratorInterface<T> { 183 public: RangeGenerator(T begin,T end,IncrementT step)184 RangeGenerator(T begin, T end, IncrementT step) 185 : begin_(begin), end_(end), 186 step_(step), end_index_(CalculateEndIndex(begin, end, step)) {} ~RangeGenerator()187 virtual ~RangeGenerator() {} 188 Begin()189 virtual ParamIteratorInterface<T>* Begin() const { 190 return new Iterator(this, begin_, 0, step_); 191 } End()192 virtual ParamIteratorInterface<T>* End() const { 193 return new Iterator(this, end_, end_index_, step_); 194 } 195 196 private: 197 class Iterator : public ParamIteratorInterface<T> { 198 public: Iterator(const ParamGeneratorInterface<T> * base,T value,int index,IncrementT step)199 Iterator(const ParamGeneratorInterface<T>* base, T value, int index, 200 IncrementT step) 201 : base_(base), value_(value), index_(index), step_(step) {} ~Iterator()202 virtual ~Iterator() {} 203 BaseGenerator()204 virtual const ParamGeneratorInterface<T>* BaseGenerator() const { 205 return base_; 206 } Advance()207 virtual void Advance() { 208 value_ = value_ + step_; 209 index_++; 210 } Clone()211 virtual ParamIteratorInterface<T>* Clone() const { 212 return new Iterator(*this); 213 } Current()214 virtual const T* Current() const { return &value_; } Equals(const ParamIteratorInterface<T> & other)215 virtual bool Equals(const ParamIteratorInterface<T>& other) const { 216 // Having the same base generator guarantees that the other 217 // iterator is of the same type and we can downcast. 218 GTEST_CHECK_(BaseGenerator() == other.BaseGenerator()) 219 << "The program attempted to compare iterators " 220 << "from different generators." << std::endl; 221 const int other_index = 222 CheckedDowncastToActualType<const Iterator>(&other)->index_; 223 return index_ == other_index; 224 } 225 226 private: Iterator(const Iterator & other)227 Iterator(const Iterator& other) 228 : ParamIteratorInterface<T>(), 229 base_(other.base_), value_(other.value_), index_(other.index_), 230 step_(other.step_) {} 231 232 // No implementation - assignment is unsupported. 233 void operator=(const Iterator& other); 234 235 const ParamGeneratorInterface<T>* const base_; 236 T value_; 237 int index_; 238 const IncrementT step_; 239 }; // class RangeGenerator::Iterator 240 CalculateEndIndex(const T & begin,const T & end,const IncrementT & step)241 static int CalculateEndIndex(const T& begin, 242 const T& end, 243 const IncrementT& step) { 244 int end_index = 0; 245 for (T i = begin; i < end; i = i + step) 246 end_index++; 247 return end_index; 248 } 249 250 // No implementation - assignment is unsupported. 251 void operator=(const RangeGenerator& other); 252 253 const T begin_; 254 const T end_; 255 const IncrementT step_; 256 // The index for the end() iterator. All the elements in the generated 257 // sequence are indexed (0-based) to aid iterator comparison. 258 const int end_index_; 259 }; // class RangeGenerator 260 261 262 // Generates values from a pair of STL-style iterators. Used in the 263 // ValuesIn() function. The elements are copied from the source range 264 // since the source can be located on the stack, and the generator 265 // is likely to persist beyond that stack frame. 266 template <typename T> 267 class ValuesInIteratorRangeGenerator : public ParamGeneratorInterface<T> { 268 public: 269 template <typename ForwardIterator> ValuesInIteratorRangeGenerator(ForwardIterator begin,ForwardIterator end)270 ValuesInIteratorRangeGenerator(ForwardIterator begin, ForwardIterator end) 271 : container_(begin, end) {} ~ValuesInIteratorRangeGenerator()272 virtual ~ValuesInIteratorRangeGenerator() {} 273 Begin()274 virtual ParamIteratorInterface<T>* Begin() const { 275 return new Iterator(this, container_.begin()); 276 } End()277 virtual ParamIteratorInterface<T>* End() const { 278 return new Iterator(this, container_.end()); 279 } 280 281 private: 282 typedef typename ::std::vector<T> ContainerType; 283 284 class Iterator : public ParamIteratorInterface<T> { 285 public: Iterator(const ParamGeneratorInterface<T> * base,typename ContainerType::const_iterator iterator)286 Iterator(const ParamGeneratorInterface<T>* base, 287 typename ContainerType::const_iterator iterator) 288 : base_(base), iterator_(iterator) {} ~Iterator()289 virtual ~Iterator() {} 290 BaseGenerator()291 virtual const ParamGeneratorInterface<T>* BaseGenerator() const { 292 return base_; 293 } Advance()294 virtual void Advance() { 295 ++iterator_; 296 value_.reset(); 297 } Clone()298 virtual ParamIteratorInterface<T>* Clone() const { 299 return new Iterator(*this); 300 } 301 // We need to use cached value referenced by iterator_ because *iterator_ 302 // can return a temporary object (and of type other then T), so just 303 // having "return &*iterator_;" doesn't work. 304 // value_ is updated here and not in Advance() because Advance() 305 // can advance iterator_ beyond the end of the range, and we cannot 306 // detect that fact. The client code, on the other hand, is 307 // responsible for not calling Current() on an out-of-range iterator. Current()308 virtual const T* Current() const { 309 if (value_.get() == NULL) 310 value_.reset(new T(*iterator_)); 311 return value_.get(); 312 } Equals(const ParamIteratorInterface<T> & other)313 virtual bool Equals(const ParamIteratorInterface<T>& other) const { 314 // Having the same base generator guarantees that the other 315 // iterator is of the same type and we can downcast. 316 GTEST_CHECK_(BaseGenerator() == other.BaseGenerator()) 317 << "The program attempted to compare iterators " 318 << "from different generators." << std::endl; 319 return iterator_ == 320 CheckedDowncastToActualType<const Iterator>(&other)->iterator_; 321 } 322 323 private: Iterator(const Iterator & other)324 Iterator(const Iterator& other) 325 // The explicit constructor call suppresses a false warning 326 // emitted by gcc when supplied with the -Wextra option. 327 : ParamIteratorInterface<T>(), 328 base_(other.base_), 329 iterator_(other.iterator_) {} 330 331 const ParamGeneratorInterface<T>* const base_; 332 typename ContainerType::const_iterator iterator_; 333 // A cached value of *iterator_. We keep it here to allow access by 334 // pointer in the wrapping iterator's operator->(). 335 // value_ needs to be mutable to be accessed in Current(). 336 // Use of scoped_ptr helps manage cached value's lifetime, 337 // which is bound by the lifespan of the iterator itself. 338 mutable scoped_ptr<const T> value_; 339 }; // class ValuesInIteratorRangeGenerator::Iterator 340 341 // No implementation - assignment is unsupported. 342 void operator=(const ValuesInIteratorRangeGenerator& other); 343 344 const ContainerType container_; 345 }; // class ValuesInIteratorRangeGenerator 346 347 // INTERNAL IMPLEMENTATION - DO NOT USE IN USER CODE. 348 // 349 // Stores a parameter value and later creates tests parameterized with that 350 // value. 351 template <class TestClass> 352 class ParameterizedTestFactory : public TestFactoryBase { 353 public: 354 typedef typename TestClass::ParamType ParamType; ParameterizedTestFactory(ParamType parameter)355 explicit ParameterizedTestFactory(ParamType parameter) : 356 parameter_(parameter) {} CreateTest()357 virtual Test* CreateTest() { 358 TestClass::SetParam(¶meter_); 359 return new TestClass(); 360 } 361 362 private: 363 const ParamType parameter_; 364 365 GTEST_DISALLOW_COPY_AND_ASSIGN_(ParameterizedTestFactory); 366 }; 367 368 // INTERNAL IMPLEMENTATION - DO NOT USE IN USER CODE. 369 // 370 // TestMetaFactoryBase is a base class for meta-factories that create 371 // test factories for passing into MakeAndRegisterTestInfo function. 372 template <class ParamType> 373 class TestMetaFactoryBase { 374 public: ~TestMetaFactoryBase()375 virtual ~TestMetaFactoryBase() {} 376 377 virtual TestFactoryBase* CreateTestFactory(ParamType parameter) = 0; 378 }; 379 380 // INTERNAL IMPLEMENTATION - DO NOT USE IN USER CODE. 381 // 382 // TestMetaFactory creates test factories for passing into 383 // MakeAndRegisterTestInfo function. Since MakeAndRegisterTestInfo receives 384 // ownership of test factory pointer, same factory object cannot be passed 385 // into that method twice. But ParameterizedTestCaseInfo is going to call 386 // it for each Test/Parameter value combination. Thus it needs meta factory 387 // creator class. 388 template <class TestCase> 389 class TestMetaFactory 390 : public TestMetaFactoryBase<typename TestCase::ParamType> { 391 public: 392 typedef typename TestCase::ParamType ParamType; 393 TestMetaFactory()394 TestMetaFactory() {} 395 CreateTestFactory(ParamType parameter)396 virtual TestFactoryBase* CreateTestFactory(ParamType parameter) { 397 return new ParameterizedTestFactory<TestCase>(parameter); 398 } 399 400 private: 401 GTEST_DISALLOW_COPY_AND_ASSIGN_(TestMetaFactory); 402 }; 403 404 // INTERNAL IMPLEMENTATION - DO NOT USE IN USER CODE. 405 // 406 // ParameterizedTestCaseInfoBase is a generic interface 407 // to ParameterizedTestCaseInfo classes. ParameterizedTestCaseInfoBase 408 // accumulates test information provided by TEST_P macro invocations 409 // and generators provided by INSTANTIATE_TEST_CASE_P macro invocations 410 // and uses that information to register all resulting test instances 411 // in RegisterTests method. The ParameterizeTestCaseRegistry class holds 412 // a collection of pointers to the ParameterizedTestCaseInfo objects 413 // and calls RegisterTests() on each of them when asked. 414 class ParameterizedTestCaseInfoBase { 415 public: ~ParameterizedTestCaseInfoBase()416 virtual ~ParameterizedTestCaseInfoBase() {} 417 418 // Base part of test case name for display purposes. 419 virtual const String& GetTestCaseName() const = 0; 420 // Test case id to verify identity. 421 virtual TypeId GetTestCaseTypeId() const = 0; 422 // UnitTest class invokes this method to register tests in this 423 // test case right before running them in RUN_ALL_TESTS macro. 424 // This method should not be called more then once on any single 425 // instance of a ParameterizedTestCaseInfoBase derived class. 426 virtual void RegisterTests() = 0; 427 428 protected: ParameterizedTestCaseInfoBase()429 ParameterizedTestCaseInfoBase() {} 430 431 private: 432 GTEST_DISALLOW_COPY_AND_ASSIGN_(ParameterizedTestCaseInfoBase); 433 }; 434 435 // INTERNAL IMPLEMENTATION - DO NOT USE IN USER CODE. 436 // 437 // ParameterizedTestCaseInfo accumulates tests obtained from TEST_P 438 // macro invocations for a particular test case and generators 439 // obtained from INSTANTIATE_TEST_CASE_P macro invocations for that 440 // test case. It registers tests with all values generated by all 441 // generators when asked. 442 template <class TestCase> 443 class ParameterizedTestCaseInfo : public ParameterizedTestCaseInfoBase { 444 public: 445 // ParamType and GeneratorCreationFunc are private types but are required 446 // for declarations of public methods AddTestPattern() and 447 // AddTestCaseInstantiation(). 448 typedef typename TestCase::ParamType ParamType; 449 // A function that returns an instance of appropriate generator type. 450 typedef ParamGenerator<ParamType>(GeneratorCreationFunc)(); 451 ParameterizedTestCaseInfo(const char * name)452 explicit ParameterizedTestCaseInfo(const char* name) 453 : test_case_name_(name) {} 454 455 // Test case base name for display purposes. GetTestCaseName()456 virtual const String& GetTestCaseName() const { return test_case_name_; } 457 // Test case id to verify identity. GetTestCaseTypeId()458 virtual TypeId GetTestCaseTypeId() const { return GetTypeId<TestCase>(); } 459 // TEST_P macro uses AddTestPattern() to record information 460 // about a single test in a LocalTestInfo structure. 461 // test_case_name is the base name of the test case (without invocation 462 // prefix). test_base_name is the name of an individual test without 463 // parameter index. For the test SequenceA/FooTest.DoBar/1 FooTest is 464 // test case base name and DoBar is test base name. AddTestPattern(const char * test_case_name,const char * test_base_name,TestMetaFactoryBase<ParamType> * meta_factory)465 void AddTestPattern(const char* test_case_name, 466 const char* test_base_name, 467 TestMetaFactoryBase<ParamType>* meta_factory) { 468 tests_.push_back(linked_ptr<TestInfo>(new TestInfo(test_case_name, 469 test_base_name, 470 meta_factory))); 471 } 472 // INSTANTIATE_TEST_CASE_P macro uses AddGenerator() to record information 473 // about a generator. AddTestCaseInstantiation(const char * instantiation_name,GeneratorCreationFunc * func,const char *,int)474 int AddTestCaseInstantiation(const char* instantiation_name, 475 GeneratorCreationFunc* func, 476 const char* /* file */, 477 int /* line */) { 478 instantiations_.push_back(::std::make_pair(instantiation_name, func)); 479 return 0; // Return value used only to run this method in namespace scope. 480 } 481 // UnitTest class invokes this method to register tests in this test case 482 // test cases right before running tests in RUN_ALL_TESTS macro. 483 // This method should not be called more then once on any single 484 // instance of a ParameterizedTestCaseInfoBase derived class. 485 // UnitTest has a guard to prevent from calling this method more then once. RegisterTests()486 virtual void RegisterTests() { 487 for (typename TestInfoContainer::iterator test_it = tests_.begin(); 488 test_it != tests_.end(); ++test_it) { 489 linked_ptr<TestInfo> test_info = *test_it; 490 for (typename InstantiationContainer::iterator gen_it = 491 instantiations_.begin(); gen_it != instantiations_.end(); 492 ++gen_it) { 493 const String& instantiation_name = gen_it->first; 494 ParamGenerator<ParamType> generator((*gen_it->second)()); 495 496 Message test_case_name_stream; 497 if ( !instantiation_name.empty() ) 498 test_case_name_stream << instantiation_name.c_str() << "/"; 499 test_case_name_stream << test_info->test_case_base_name.c_str(); 500 501 int i = 0; 502 for (typename ParamGenerator<ParamType>::iterator param_it = 503 generator.begin(); 504 param_it != generator.end(); ++param_it, ++i) { 505 Message test_name_stream; 506 test_name_stream << test_info->test_base_name.c_str() << "/" << i; 507 ::testing::internal::MakeAndRegisterTestInfo( 508 test_case_name_stream.GetString().c_str(), 509 test_name_stream.GetString().c_str(), 510 "", // test_case_comment 511 "", // comment; TODO(vladl@google.com): provide parameter value 512 // representation. 513 GetTestCaseTypeId(), 514 TestCase::SetUpTestCase, 515 TestCase::TearDownTestCase, 516 test_info->test_meta_factory->CreateTestFactory(*param_it)); 517 } // for param_it 518 } // for gen_it 519 } // for test_it 520 } // RegisterTests 521 522 private: 523 // LocalTestInfo structure keeps information about a single test registered 524 // with TEST_P macro. 525 struct TestInfo { TestInfoTestInfo526 TestInfo(const char* a_test_case_base_name, 527 const char* a_test_base_name, 528 TestMetaFactoryBase<ParamType>* a_test_meta_factory) : 529 test_case_base_name(a_test_case_base_name), 530 test_base_name(a_test_base_name), 531 test_meta_factory(a_test_meta_factory) {} 532 533 const String test_case_base_name; 534 const String test_base_name; 535 const scoped_ptr<TestMetaFactoryBase<ParamType> > test_meta_factory; 536 }; 537 typedef ::std::vector<linked_ptr<TestInfo> > TestInfoContainer; 538 // Keeps pairs of <Instantiation name, Sequence generator creation function> 539 // received from INSTANTIATE_TEST_CASE_P macros. 540 typedef ::std::vector<std::pair<String, GeneratorCreationFunc*> > 541 InstantiationContainer; 542 543 const String test_case_name_; 544 TestInfoContainer tests_; 545 InstantiationContainer instantiations_; 546 547 GTEST_DISALLOW_COPY_AND_ASSIGN_(ParameterizedTestCaseInfo); 548 }; // class ParameterizedTestCaseInfo 549 550 // INTERNAL IMPLEMENTATION - DO NOT USE IN USER CODE. 551 // 552 // ParameterizedTestCaseRegistry contains a map of ParameterizedTestCaseInfoBase 553 // classes accessed by test case names. TEST_P and INSTANTIATE_TEST_CASE_P 554 // macros use it to locate their corresponding ParameterizedTestCaseInfo 555 // descriptors. 556 class ParameterizedTestCaseRegistry { 557 public: ParameterizedTestCaseRegistry()558 ParameterizedTestCaseRegistry() {} ~ParameterizedTestCaseRegistry()559 ~ParameterizedTestCaseRegistry() { 560 for (TestCaseInfoContainer::iterator it = test_case_infos_.begin(); 561 it != test_case_infos_.end(); ++it) { 562 delete *it; 563 } 564 } 565 566 // Looks up or creates and returns a structure containing information about 567 // tests and instantiations of a particular test case. 568 template <class TestCase> GetTestCasePatternHolder(const char * test_case_name,const char * file,int line)569 ParameterizedTestCaseInfo<TestCase>* GetTestCasePatternHolder( 570 const char* test_case_name, 571 const char* file, 572 int line) { 573 ParameterizedTestCaseInfo<TestCase>* typed_test_info = NULL; 574 for (TestCaseInfoContainer::iterator it = test_case_infos_.begin(); 575 it != test_case_infos_.end(); ++it) { 576 if ((*it)->GetTestCaseName() == test_case_name) { 577 if ((*it)->GetTestCaseTypeId() != GetTypeId<TestCase>()) { 578 // Complain about incorrect usage of Google Test facilities 579 // and terminate the program since we cannot guaranty correct 580 // test case setup and tear-down in this case. 581 ReportInvalidTestCaseType(test_case_name, file, line); 582 abort(); 583 } else { 584 // At this point we are sure that the object we found is of the same 585 // type we are looking for, so we downcast it to that type 586 // without further checks. 587 typed_test_info = CheckedDowncastToActualType< 588 ParameterizedTestCaseInfo<TestCase> >(*it); 589 } 590 break; 591 } 592 } 593 if (typed_test_info == NULL) { 594 typed_test_info = new ParameterizedTestCaseInfo<TestCase>(test_case_name); 595 test_case_infos_.push_back(typed_test_info); 596 } 597 return typed_test_info; 598 } RegisterTests()599 void RegisterTests() { 600 for (TestCaseInfoContainer::iterator it = test_case_infos_.begin(); 601 it != test_case_infos_.end(); ++it) { 602 (*it)->RegisterTests(); 603 } 604 } 605 606 private: 607 typedef ::std::vector<ParameterizedTestCaseInfoBase*> TestCaseInfoContainer; 608 609 TestCaseInfoContainer test_case_infos_; 610 611 GTEST_DISALLOW_COPY_AND_ASSIGN_(ParameterizedTestCaseRegistry); 612 }; 613 614 } // namespace internal 615 } // namespace testing 616 617 #endif // GTEST_HAS_PARAM_TEST 618 619 #endif // GTEST_INCLUDE_GTEST_INTERNAL_GTEST_PARAM_UTIL_H_ 620