gtest-typed-test.cc 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. #include "gtest/gtest-typed-test.h"
  30. #include "gtest/gtest.h"
  31. namespace testing {
  32. namespace internal {
  33. // Skips to the first non-space char in str. Returns an empty string if str
  34. // contains only whitespace characters.
  35. static const char* SkipSpaces(const char* str) {
  36. while (IsSpace(*str)) str++;
  37. return str;
  38. }
  39. static std::vector<std::string> SplitIntoTestNames(const char* src) {
  40. std::vector<std::string> name_vec;
  41. src = SkipSpaces(src);
  42. for (; src != nullptr; src = SkipComma(src)) {
  43. name_vec.push_back(StripTrailingSpaces(GetPrefixUntilComma(src)));
  44. }
  45. return name_vec;
  46. }
  47. // Verifies that registered_tests match the test names in
  48. // registered_tests_; returns registered_tests if successful, or
  49. // aborts the program otherwise.
  50. const char* TypedTestSuitePState::VerifyRegisteredTestNames(
  51. const char* test_suite_name, const char* file, int line,
  52. const char* registered_tests) {
  53. RegisterTypeParameterizedTestSuite(test_suite_name, CodeLocation(file, line));
  54. typedef RegisteredTestsMap::const_iterator RegisteredTestIter;
  55. registered_ = true;
  56. std::vector<std::string> name_vec = SplitIntoTestNames(registered_tests);
  57. Message errors;
  58. std::set<std::string> tests;
  59. for (std::vector<std::string>::const_iterator name_it = name_vec.begin();
  60. name_it != name_vec.end(); ++name_it) {
  61. const std::string& name = *name_it;
  62. if (tests.count(name) != 0) {
  63. errors << "Test " << name << " is listed more than once.\n";
  64. continue;
  65. }
  66. if (registered_tests_.count(name) != 0) {
  67. tests.insert(name);
  68. } else {
  69. errors << "No test named " << name
  70. << " can be found in this test suite.\n";
  71. }
  72. }
  73. for (RegisteredTestIter it = registered_tests_.begin();
  74. it != registered_tests_.end(); ++it) {
  75. if (tests.count(it->first) == 0) {
  76. errors << "You forgot to list test " << it->first << ".\n";
  77. }
  78. }
  79. const std::string& errors_str = errors.GetString();
  80. if (errors_str != "") {
  81. fprintf(stderr, "%s %s", FormatFileLocation(file, line).c_str(),
  82. errors_str.c_str());
  83. fflush(stderr);
  84. posix::Abort();
  85. }
  86. return registered_tests;
  87. }
  88. } // namespace internal
  89. } // namespace testing