catch_test_case_info.cpp 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. // Copyright Catch2 Authors
  2. // Distributed under the Boost Software License, Version 1.0.
  3. // (See accompanying file LICENSE_1_0.txt or copy at
  4. // https://www.boost.org/LICENSE_1_0.txt)
  5. // SPDX-License-Identifier: BSL-1.0
  6. #include <catch2/catch_test_case_info.hpp>
  7. #include <catch2/internal/catch_enforce.hpp>
  8. #include <catch2/internal/catch_string_manip.hpp>
  9. #include <catch2/internal/catch_case_insensitive_comparisons.hpp>
  10. #include <cassert>
  11. #include <cctype>
  12. #include <algorithm>
  13. namespace Catch {
  14. namespace {
  15. using TCP_underlying_type = uint8_t;
  16. static_assert(sizeof(TestCaseProperties) == sizeof(TCP_underlying_type),
  17. "The size of the TestCaseProperties is different from the assumed size");
  18. TestCaseProperties operator|(TestCaseProperties lhs, TestCaseProperties rhs) {
  19. return static_cast<TestCaseProperties>(
  20. static_cast<TCP_underlying_type>(lhs) | static_cast<TCP_underlying_type>(rhs)
  21. );
  22. }
  23. TestCaseProperties& operator|=(TestCaseProperties& lhs, TestCaseProperties rhs) {
  24. lhs = static_cast<TestCaseProperties>(
  25. static_cast<TCP_underlying_type>(lhs) | static_cast<TCP_underlying_type>(rhs)
  26. );
  27. return lhs;
  28. }
  29. TestCaseProperties operator&(TestCaseProperties lhs, TestCaseProperties rhs) {
  30. return static_cast<TestCaseProperties>(
  31. static_cast<TCP_underlying_type>(lhs) & static_cast<TCP_underlying_type>(rhs)
  32. );
  33. }
  34. bool applies(TestCaseProperties tcp) {
  35. static_assert(static_cast<TCP_underlying_type>(TestCaseProperties::None) == 0,
  36. "TestCaseProperties::None must be equal to 0");
  37. return tcp != TestCaseProperties::None;
  38. }
  39. TestCaseProperties parseSpecialTag( StringRef tag ) {
  40. if( !tag.empty() && tag[0] == '.' )
  41. return TestCaseProperties::IsHidden;
  42. else if( tag == "!throws"_sr )
  43. return TestCaseProperties::Throws;
  44. else if( tag == "!shouldfail"_sr )
  45. return TestCaseProperties::ShouldFail;
  46. else if( tag == "!mayfail"_sr )
  47. return TestCaseProperties::MayFail;
  48. else if( tag == "!nonportable"_sr )
  49. return TestCaseProperties::NonPortable;
  50. else if( tag == "!benchmark"_sr )
  51. return TestCaseProperties::Benchmark | TestCaseProperties::IsHidden;
  52. else
  53. return TestCaseProperties::None;
  54. }
  55. bool isReservedTag( StringRef tag ) {
  56. return parseSpecialTag( tag ) == TestCaseProperties::None
  57. && tag.size() > 0
  58. && !std::isalnum( static_cast<unsigned char>(tag[0]) );
  59. }
  60. void enforceNotReservedTag( StringRef tag, SourceLineInfo const& _lineInfo ) {
  61. CATCH_ENFORCE( !isReservedTag(tag),
  62. "Tag name: [" << tag << "] is not allowed.\n"
  63. << "Tag names starting with non alphanumeric characters are reserved\n"
  64. << _lineInfo );
  65. }
  66. std::string makeDefaultName() {
  67. static size_t counter = 0;
  68. return "Anonymous test case " + std::to_string(++counter);
  69. }
  70. StringRef extractFilenamePart(StringRef filename) {
  71. size_t lastDot = filename.size();
  72. while (lastDot > 0 && filename[lastDot - 1] != '.') {
  73. --lastDot;
  74. }
  75. --lastDot;
  76. size_t nameStart = lastDot;
  77. while (nameStart > 0 && filename[nameStart - 1] != '/' && filename[nameStart - 1] != '\\') {
  78. --nameStart;
  79. }
  80. return filename.substr(nameStart, lastDot - nameStart);
  81. }
  82. // Returns the upper bound on size of extra tags ([#file]+[.])
  83. size_t sizeOfExtraTags(StringRef filepath) {
  84. // [.] is 3, [#] is another 3
  85. const size_t extras = 3 + 3;
  86. return extractFilenamePart(filepath).size() + extras;
  87. }
  88. } // end unnamed namespace
  89. bool operator<( Tag const& lhs, Tag const& rhs ) {
  90. Detail::CaseInsensitiveLess cmp;
  91. return cmp( lhs.original, rhs.original );
  92. }
  93. bool operator==( Tag const& lhs, Tag const& rhs ) {
  94. Detail::CaseInsensitiveEqualTo cmp;
  95. return cmp( lhs.original, rhs.original );
  96. }
  97. Detail::unique_ptr<TestCaseInfo>
  98. makeTestCaseInfo(StringRef _className,
  99. NameAndTags const& nameAndTags,
  100. SourceLineInfo const& _lineInfo ) {
  101. return Detail::make_unique<TestCaseInfo>(_className, nameAndTags, _lineInfo);
  102. }
  103. TestCaseInfo::TestCaseInfo(StringRef _className,
  104. NameAndTags const& _nameAndTags,
  105. SourceLineInfo const& _lineInfo):
  106. name( _nameAndTags.name.empty() ? makeDefaultName() : _nameAndTags.name ),
  107. className( _className ),
  108. lineInfo( _lineInfo )
  109. {
  110. StringRef originalTags = _nameAndTags.tags;
  111. // We need to reserve enough space to store all of the tags
  112. // (including optional hidden tag and filename tag)
  113. auto requiredSize = originalTags.size() + sizeOfExtraTags(_lineInfo.file);
  114. backingTags.reserve(requiredSize);
  115. // We cannot copy the tags directly, as we need to normalize
  116. // some tags, so that [.foo] is copied as [.][foo].
  117. size_t tagStart = 0;
  118. size_t tagEnd = 0;
  119. bool inTag = false;
  120. for (size_t idx = 0; idx < originalTags.size(); ++idx) {
  121. auto c = originalTags[idx];
  122. if (c == '[') {
  123. assert(!inTag);
  124. inTag = true;
  125. tagStart = idx;
  126. }
  127. if (c == ']') {
  128. assert(inTag);
  129. inTag = false;
  130. tagEnd = idx;
  131. assert(tagStart < tagEnd);
  132. // We need to check the tag for special meanings, copy
  133. // it over to backing storage and actually reference the
  134. // backing storage in the saved tags
  135. StringRef tagStr = originalTags.substr(tagStart+1, tagEnd - tagStart - 1);
  136. CATCH_ENFORCE(!tagStr.empty(), "Empty tags are not allowed");
  137. enforceNotReservedTag(tagStr, lineInfo);
  138. properties |= parseSpecialTag(tagStr);
  139. // When copying a tag to the backing storage, we need to
  140. // check if it is a merged hide tag, such as [.foo], and
  141. // if it is, we need to handle it as if it was [foo].
  142. if (tagStr.size() > 1 && tagStr[0] == '.') {
  143. tagStr = tagStr.substr(1, tagStr.size() - 1);
  144. }
  145. // We skip over dealing with the [.] tag, as we will add
  146. // it later unconditionally and then sort and unique all
  147. // the tags.
  148. internalAppendTag(tagStr);
  149. }
  150. (void)inTag; // Silence "set-but-unused" warning in release mode.
  151. }
  152. // Add [.] if relevant
  153. if (isHidden()) {
  154. internalAppendTag("."_sr);
  155. }
  156. // Sort and prepare tags
  157. std::sort(begin(tags), end(tags));
  158. tags.erase(std::unique(begin(tags), end(tags)),
  159. end(tags));
  160. }
  161. bool TestCaseInfo::isHidden() const {
  162. return applies( properties & TestCaseProperties::IsHidden );
  163. }
  164. bool TestCaseInfo::throws() const {
  165. return applies( properties & TestCaseProperties::Throws );
  166. }
  167. bool TestCaseInfo::okToFail() const {
  168. return applies( properties & (TestCaseProperties::ShouldFail | TestCaseProperties::MayFail ) );
  169. }
  170. bool TestCaseInfo::expectedToFail() const {
  171. return applies( properties & (TestCaseProperties::ShouldFail) );
  172. }
  173. void TestCaseInfo::addFilenameTag() {
  174. std::string combined("#");
  175. combined += extractFilenamePart(lineInfo.file);
  176. internalAppendTag(combined);
  177. }
  178. std::string TestCaseInfo::tagsAsString() const {
  179. std::string ret;
  180. // '[' and ']' per tag
  181. std::size_t full_size = 2 * tags.size();
  182. for (const auto& tag : tags) {
  183. full_size += tag.original.size();
  184. }
  185. ret.reserve(full_size);
  186. for (const auto& tag : tags) {
  187. ret.push_back('[');
  188. ret += tag.original;
  189. ret.push_back(']');
  190. }
  191. return ret;
  192. }
  193. void TestCaseInfo::internalAppendTag(StringRef tagStr) {
  194. backingTags += '[';
  195. const auto backingStart = backingTags.size();
  196. backingTags += tagStr;
  197. const auto backingEnd = backingTags.size();
  198. backingTags += ']';
  199. tags.emplace_back(StringRef(backingTags.c_str() + backingStart, backingEnd - backingStart));
  200. }
  201. bool operator<( TestCaseInfo const& lhs, TestCaseInfo const& rhs ) {
  202. // We want to avoid redoing the string comparisons multiple times,
  203. // so we store the result of a three-way comparison before using
  204. // it in the actual comparison logic.
  205. const auto cmpName = lhs.name.compare( rhs.name );
  206. if ( cmpName != 0 ) {
  207. return cmpName < 0;
  208. }
  209. const auto cmpClassName = lhs.className.compare( rhs.className );
  210. if ( cmpClassName != 0 ) {
  211. return cmpClassName < 0;
  212. }
  213. return lhs.tags < rhs.tags;
  214. }
  215. TestCaseInfo const& TestCaseHandle::getTestCaseInfo() const {
  216. return *m_info;
  217. }
  218. } // end namespace Catch