gtest-param-util.h 27 KB

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