gtest-param-util.h 27 KB

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