catch_test_case_info.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. /*
  2. * Created by Phil on 14/08/2012.
  3. * Copyright 2012 Two Blue Cubes Ltd. All rights reserved.
  4. *
  5. * Distributed under the Boost Software License, Version 1.0. (See accompanying
  6. * file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  7. */
  8. #include "catch_test_case_info.h"
  9. #include "catch_enforce.h"
  10. #include "catch_test_spec.h"
  11. #include "catch_interfaces_testcase.h"
  12. #include "catch_string_manip.h"
  13. #include <cctype>
  14. #include <exception>
  15. #include <algorithm>
  16. #include <sstream>
  17. namespace Catch {
  18. namespace {
  19. TestCaseInfo::SpecialProperties parseSpecialTag( std::string const& tag ) {
  20. if( startsWith( tag, '.' ) ||
  21. tag == "!hide" )
  22. return TestCaseInfo::IsHidden;
  23. else if( tag == "!throws" )
  24. return TestCaseInfo::Throws;
  25. else if( tag == "!shouldfail" )
  26. return TestCaseInfo::ShouldFail;
  27. else if( tag == "!mayfail" )
  28. return TestCaseInfo::MayFail;
  29. else if( tag == "!nonportable" )
  30. return TestCaseInfo::NonPortable;
  31. else if( tag == "!benchmark" )
  32. return static_cast<TestCaseInfo::SpecialProperties>( TestCaseInfo::Benchmark | TestCaseInfo::IsHidden );
  33. else
  34. return TestCaseInfo::None;
  35. }
  36. bool isReservedTag( std::string const& tag ) {
  37. return parseSpecialTag( tag ) == TestCaseInfo::None && tag.size() > 0 && !std::isalnum( static_cast<unsigned char>(tag[0]) );
  38. }
  39. void enforceNotReservedTag( std::string const& tag, SourceLineInfo const& _lineInfo ) {
  40. CATCH_ENFORCE( !isReservedTag(tag),
  41. "Tag name: [" << tag << "] is not allowed.\n"
  42. << "Tag names starting with non alphanumeric characters are reserved\n"
  43. << _lineInfo );
  44. }
  45. }
  46. TestCase makeTestCase( ITestInvoker* _testCase,
  47. std::string const& _className,
  48. NameAndTags const& nameAndTags,
  49. SourceLineInfo const& _lineInfo )
  50. {
  51. bool isHidden = false;
  52. // Parse out tags
  53. std::vector<std::string> tags;
  54. std::string desc, tag;
  55. bool inTag = false;
  56. for (char c : nameAndTags.tags) {
  57. if( !inTag ) {
  58. if( c == '[' )
  59. inTag = true;
  60. else
  61. desc += c;
  62. }
  63. else {
  64. if( c == ']' ) {
  65. TestCaseInfo::SpecialProperties prop = parseSpecialTag( tag );
  66. if( ( prop & TestCaseInfo::IsHidden ) != 0 )
  67. isHidden = true;
  68. else if( prop == TestCaseInfo::None )
  69. enforceNotReservedTag( tag, _lineInfo );
  70. // Merged hide tags like `[.approvals]` should be added as
  71. // `[.][approvals]`. The `[.]` is added at later point, so
  72. // we only strip the prefix
  73. if (startsWith(tag, '.') && tag.size() > 1) {
  74. tag.erase(0, 1);
  75. }
  76. tags.push_back( tag );
  77. tag.clear();
  78. inTag = false;
  79. }
  80. else
  81. tag += c;
  82. }
  83. }
  84. if( isHidden ) {
  85. // Add all "hidden" tags to make them behave identically
  86. tags.insert( tags.end(), { ".", "!hide" } );
  87. }
  88. TestCaseInfo info( static_cast<std::string>(nameAndTags.name), _className, desc, tags, _lineInfo );
  89. return TestCase( _testCase, std::move(info) );
  90. }
  91. void setTags( TestCaseInfo& testCaseInfo, std::vector<std::string> tags ) {
  92. std::sort(begin(tags), end(tags));
  93. tags.erase(std::unique(begin(tags), end(tags)), end(tags));
  94. testCaseInfo.lcaseTags.clear();
  95. for( auto const& tag : tags ) {
  96. std::string lcaseTag = toLower( tag );
  97. testCaseInfo.properties = static_cast<TestCaseInfo::SpecialProperties>( testCaseInfo.properties | parseSpecialTag( lcaseTag ) );
  98. testCaseInfo.lcaseTags.push_back( lcaseTag );
  99. }
  100. testCaseInfo.tags = std::move(tags);
  101. }
  102. TestCaseInfo::TestCaseInfo( std::string const& _name,
  103. std::string const& _className,
  104. std::string const& _description,
  105. std::vector<std::string> const& _tags,
  106. SourceLineInfo const& _lineInfo )
  107. : name( _name ),
  108. className( _className ),
  109. description( _description ),
  110. lineInfo( _lineInfo ),
  111. properties( None )
  112. {
  113. setTags( *this, _tags );
  114. }
  115. bool TestCaseInfo::isHidden() const {
  116. return ( properties & IsHidden ) != 0;
  117. }
  118. bool TestCaseInfo::throws() const {
  119. return ( properties & Throws ) != 0;
  120. }
  121. bool TestCaseInfo::okToFail() const {
  122. return ( properties & (ShouldFail | MayFail ) ) != 0;
  123. }
  124. bool TestCaseInfo::expectedToFail() const {
  125. return ( properties & (ShouldFail ) ) != 0;
  126. }
  127. std::string TestCaseInfo::tagsAsString() const {
  128. std::string ret;
  129. // '[' and ']' per tag
  130. std::size_t full_size = 2 * tags.size();
  131. for (const auto& tag : tags) {
  132. full_size += tag.size();
  133. }
  134. ret.reserve(full_size);
  135. for (const auto& tag : tags) {
  136. ret.push_back('[');
  137. ret.append(tag);
  138. ret.push_back(']');
  139. }
  140. return ret;
  141. }
  142. TestCase::TestCase( ITestInvoker* testCase, TestCaseInfo&& info ) : TestCaseInfo( std::move(info) ), test( testCase ) {}
  143. TestCase TestCase::withName( std::string const& _newName ) const {
  144. TestCase other( *this );
  145. other.name = _newName;
  146. return other;
  147. }
  148. void TestCase::invoke() const {
  149. test->invoke();
  150. }
  151. bool TestCase::operator == ( TestCase const& other ) const {
  152. return test.get() == other.test.get() &&
  153. name == other.name &&
  154. className == other.className;
  155. }
  156. bool TestCase::operator < ( TestCase const& other ) const {
  157. return name < other.name;
  158. }
  159. TestCaseInfo const& TestCase::getTestCaseInfo() const
  160. {
  161. return *this;
  162. }
  163. } // end namespace Catch