catch_list.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. /*
  2. * Created by Phil on 5/11/2010.
  3. * Copyright 2010 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_list.h"
  9. #include "catch_interfaces_registry_hub.h"
  10. #include "catch_interfaces_reporter.h"
  11. #include "catch_interfaces_testcase.h"
  12. #include "catch_context.h"
  13. #include "catch_stream.h"
  14. #include "catch_text.h"
  15. #include "catch_console_colour.h"
  16. #include "catch_test_spec_parser.h"
  17. #include "catch_tostring.h"
  18. #include "catch_string_manip.h"
  19. #include <limits>
  20. #include <algorithm>
  21. #include <iomanip>
  22. namespace Catch {
  23. std::size_t listTests( Config const& config ) {
  24. TestSpec const& testSpec = config.testSpec();
  25. if( config.hasTestFilters() )
  26. Catch::cout() << "Matching test cases:\n";
  27. else {
  28. Catch::cout() << "All available test cases:\n";
  29. }
  30. auto matchedTestCases = filterTests( getAllTestCasesSorted( config ), testSpec, config );
  31. for( auto const& testCaseInfo : matchedTestCases ) {
  32. Colour::Code colour = testCaseInfo.isHidden()
  33. ? Colour::SecondaryText
  34. : Colour::None;
  35. Colour colourGuard( colour );
  36. Catch::cout() << Column( testCaseInfo.name ).initialIndent( 2 ).indent( 4 ) << "\n";
  37. if( config.verbosity() >= Verbosity::High ) {
  38. Catch::cout() << Column( Catch::Detail::stringify( testCaseInfo.lineInfo ) ).indent(4) << std::endl;
  39. std::string description = testCaseInfo.description;
  40. if( description.empty() )
  41. description = "(NO DESCRIPTION)";
  42. Catch::cout() << Column( description ).indent(4) << std::endl;
  43. }
  44. if( !testCaseInfo.tags.empty() )
  45. Catch::cout() << Column( testCaseInfo.tagsAsString() ).indent( 6 ) << "\n";
  46. }
  47. if( !config.hasTestFilters() )
  48. Catch::cout() << pluralise( matchedTestCases.size(), "test case" ) << '\n' << std::endl;
  49. else
  50. Catch::cout() << pluralise( matchedTestCases.size(), "matching test case" ) << '\n' << std::endl;
  51. return matchedTestCases.size();
  52. }
  53. std::size_t listTestsNamesOnly( Config const& config ) {
  54. TestSpec const& testSpec = config.testSpec();
  55. std::size_t matchedTests = 0;
  56. std::vector<TestCase> matchedTestCases = filterTests( getAllTestCasesSorted( config ), testSpec, config );
  57. for( auto const& testCaseInfo : matchedTestCases ) {
  58. matchedTests++;
  59. if( startsWith( testCaseInfo.name, '#' ) )
  60. Catch::cout() << '"' << testCaseInfo.name << '"';
  61. else
  62. Catch::cout() << testCaseInfo.name;
  63. if ( config.verbosity() >= Verbosity::High )
  64. Catch::cout() << "\t@" << testCaseInfo.lineInfo;
  65. Catch::cout() << std::endl;
  66. }
  67. return matchedTests;
  68. }
  69. void TagInfo::add( std::string const& spelling ) {
  70. ++count;
  71. spellings.insert( spelling );
  72. }
  73. std::string TagInfo::all() const {
  74. size_t size = 0;
  75. for (auto const& spelling : spellings) {
  76. // Add 2 for the brackes
  77. size += spelling.size() + 2;
  78. }
  79. std::string out; out.reserve(size);
  80. for (auto const& spelling : spellings) {
  81. out += '[';
  82. out += spelling;
  83. out += ']';
  84. }
  85. return out;
  86. }
  87. std::size_t listTags( Config const& config ) {
  88. TestSpec const& testSpec = config.testSpec();
  89. if( config.hasTestFilters() )
  90. Catch::cout() << "Tags for matching test cases:\n";
  91. else {
  92. Catch::cout() << "All available tags:\n";
  93. }
  94. std::map<std::string, TagInfo> tagCounts;
  95. std::vector<TestCase> matchedTestCases = filterTests( getAllTestCasesSorted( config ), testSpec, config );
  96. for( auto const& testCase : matchedTestCases ) {
  97. for( auto const& tagName : testCase.getTestCaseInfo().tags ) {
  98. std::string lcaseTagName = toLower( tagName );
  99. auto countIt = tagCounts.find( lcaseTagName );
  100. if( countIt == tagCounts.end() )
  101. countIt = tagCounts.insert( std::make_pair( lcaseTagName, TagInfo() ) ).first;
  102. countIt->second.add( tagName );
  103. }
  104. }
  105. for( auto const& tagCount : tagCounts ) {
  106. ReusableStringStream rss;
  107. rss << " " << std::setw(2) << tagCount.second.count << " ";
  108. auto str = rss.str();
  109. auto wrapper = Column( tagCount.second.all() )
  110. .initialIndent( 0 )
  111. .indent( str.size() )
  112. .width( CATCH_CONFIG_CONSOLE_WIDTH-10 );
  113. Catch::cout() << str << wrapper << '\n';
  114. }
  115. Catch::cout() << pluralise( tagCounts.size(), "tag" ) << '\n' << std::endl;
  116. return tagCounts.size();
  117. }
  118. std::size_t listReporters() {
  119. Catch::cout() << "Available reporters:\n";
  120. IReporterRegistry::FactoryMap const& factories = getRegistryHub().getReporterRegistry().getFactories();
  121. std::size_t maxNameLen = 0;
  122. for( auto const& factoryKvp : factories )
  123. maxNameLen = (std::max)( maxNameLen, factoryKvp.first.size() );
  124. for( auto const& factoryKvp : factories ) {
  125. Catch::cout()
  126. << Column( factoryKvp.first + ":" )
  127. .indent(2)
  128. .width( 5+maxNameLen )
  129. + Column( factoryKvp.second->getDescription() )
  130. .initialIndent(0)
  131. .indent(2)
  132. .width( CATCH_CONFIG_CONSOLE_WIDTH - maxNameLen-8 )
  133. << "\n";
  134. }
  135. Catch::cout() << std::endl;
  136. return factories.size();
  137. }
  138. Option<std::size_t> list( std::shared_ptr<Config> const& config ) {
  139. Option<std::size_t> listedCount;
  140. getCurrentMutableContext().setConfig( config );
  141. if( config->listTests() )
  142. listedCount = listedCount.valueOr(0) + listTests( *config );
  143. if( config->listTestNamesOnly() )
  144. listedCount = listedCount.valueOr(0) + listTestsNamesOnly( *config );
  145. if( config->listTags() )
  146. listedCount = listedCount.valueOr(0) + listTags( *config );
  147. if( config->listReporters() )
  148. listedCount = listedCount.valueOr(0) + listReporters();
  149. return listedCount;
  150. }
  151. } // end namespace Catch