2010-11-10 00:24:00 +01:00
|
|
|
/*
|
|
|
|
* Created by Phil on 5/11/2010.
|
|
|
|
* Copyright 2010 Two Blue Cubes Ltd. All rights reserved.
|
|
|
|
*
|
|
|
|
* Distributed under the Boost Software License, Version 1.0. (See accompanying
|
|
|
|
* file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
|
|
|
*/
|
|
|
|
#ifndef TWOBLUECUBES_CATCH_LIST_HPP_INCLUDED
|
|
|
|
#define TWOBLUECUBES_CATCH_LIST_HPP_INCLUDED
|
|
|
|
|
|
|
|
#include "catch_commandline.hpp"
|
2013-04-20 20:36:40 +02:00
|
|
|
#include "catch_text.h"
|
2013-03-29 22:55:19 +01:00
|
|
|
#include "catch_console_colour.hpp"
|
2013-12-03 19:52:41 +01:00
|
|
|
#include "catch_interfaces_reporter.h"
|
2014-05-16 19:24:07 +02:00
|
|
|
#include "catch_test_spec_parser.hpp"
|
2013-03-28 23:13:31 +01:00
|
|
|
|
2010-11-11 21:42:16 +01:00
|
|
|
#include <limits>
|
2013-03-28 23:13:31 +01:00
|
|
|
#include <algorithm>
|
2010-11-11 21:56:38 +01:00
|
|
|
|
2012-05-16 00:58:23 +02:00
|
|
|
namespace Catch {
|
2013-03-28 23:13:31 +01:00
|
|
|
|
2013-06-07 19:41:22 +02:00
|
|
|
inline std::size_t listTests( Config const& config ) {
|
2014-05-16 19:24:07 +02:00
|
|
|
|
|
|
|
TestSpec testSpec = config.testSpec();
|
|
|
|
if( config.testSpec().hasFilters() )
|
2014-10-02 20:08:19 +02:00
|
|
|
Catch::cout() << "Matching test cases:\n";
|
2014-05-16 19:24:07 +02:00
|
|
|
else {
|
2014-10-02 20:08:19 +02:00
|
|
|
Catch::cout() << "All available test cases:\n";
|
2014-06-30 08:33:17 +02:00
|
|
|
testSpec = TestSpecParser( ITagAliasRegistry::get() ).parse( "*" ).testSpec();
|
2014-05-16 19:24:07 +02:00
|
|
|
}
|
2013-03-28 23:13:31 +01:00
|
|
|
|
|
|
|
std::size_t matchedTests = 0;
|
2013-11-12 19:59:34 +01:00
|
|
|
TextAttributes nameAttr, tagsAttr;
|
|
|
|
nameAttr.setInitialIndent( 2 ).setIndent( 4 );
|
|
|
|
tagsAttr.setIndent( 6 );
|
|
|
|
|
2015-08-05 00:11:56 +02:00
|
|
|
std::vector<TestCase> matchedTestCases = filterTests( getAllTestCasesSorted( config ), testSpec, config );
|
2014-04-15 19:44:37 +02:00
|
|
|
for( std::vector<TestCase>::const_iterator it = matchedTestCases.begin(), itEnd = matchedTestCases.end();
|
2013-11-12 19:59:34 +01:00
|
|
|
it != itEnd;
|
2014-04-15 19:44:37 +02:00
|
|
|
++it ) {
|
|
|
|
matchedTests++;
|
|
|
|
TestCaseInfo const& testCaseInfo = it->getTestCaseInfo();
|
2014-07-03 09:09:57 +02:00
|
|
|
Colour::Code colour = testCaseInfo.isHidden()
|
2014-04-15 19:44:37 +02:00
|
|
|
? Colour::SecondaryText
|
|
|
|
: Colour::None;
|
|
|
|
Colour colourGuard( colour );
|
2013-11-12 19:59:34 +01:00
|
|
|
|
2014-10-02 20:08:19 +02:00
|
|
|
Catch::cout() << Text( testCaseInfo.name, nameAttr ) << std::endl;
|
2014-04-15 19:44:37 +02:00
|
|
|
if( !testCaseInfo.tags.empty() )
|
2014-10-02 20:08:19 +02:00
|
|
|
Catch::cout() << Text( testCaseInfo.tagsAsString, tagsAttr ) << std::endl;
|
2014-04-15 19:44:37 +02:00
|
|
|
}
|
2013-11-12 19:59:34 +01:00
|
|
|
|
2014-05-16 19:24:07 +02:00
|
|
|
if( !config.testSpec().hasFilters() )
|
2017-01-15 09:41:33 +01:00
|
|
|
Catch::cout() << pluralise( matchedTests, "test case" ) << '\n' << std::endl;
|
2013-03-28 23:13:31 +01:00
|
|
|
else
|
2017-01-15 09:41:33 +01:00
|
|
|
Catch::cout() << pluralise( matchedTests, "matching test case" ) << '\n' << std::endl;
|
2013-06-06 23:54:42 +02:00
|
|
|
return matchedTests;
|
2013-03-28 23:13:31 +01:00
|
|
|
}
|
2013-07-03 20:14:59 +02:00
|
|
|
|
2013-11-26 21:57:34 +01:00
|
|
|
inline std::size_t listTestsNamesOnly( Config const& config ) {
|
2014-05-16 19:24:07 +02:00
|
|
|
TestSpec testSpec = config.testSpec();
|
|
|
|
if( !config.testSpec().hasFilters() )
|
2014-06-30 08:33:17 +02:00
|
|
|
testSpec = TestSpecParser( ITagAliasRegistry::get() ).parse( "*" ).testSpec();
|
2013-11-26 21:57:34 +01:00
|
|
|
std::size_t matchedTests = 0;
|
2015-08-05 00:11:56 +02:00
|
|
|
std::vector<TestCase> matchedTestCases = filterTests( getAllTestCasesSorted( config ), testSpec, config );
|
2014-04-15 19:44:37 +02:00
|
|
|
for( std::vector<TestCase>::const_iterator it = matchedTestCases.begin(), itEnd = matchedTestCases.end();
|
2013-11-26 21:57:34 +01:00
|
|
|
it != itEnd;
|
2014-04-15 19:44:37 +02:00
|
|
|
++it ) {
|
|
|
|
matchedTests++;
|
|
|
|
TestCaseInfo const& testCaseInfo = it->getTestCaseInfo();
|
2017-01-15 09:41:33 +01:00
|
|
|
if( startsWith( testCaseInfo.name, '#' ) )
|
2017-01-29 23:07:15 +01:00
|
|
|
Catch::cout() << '"' << testCaseInfo.name << '"' << std::endl;
|
2016-09-27 11:43:03 +02:00
|
|
|
else
|
|
|
|
Catch::cout() << testCaseInfo.name << std::endl;
|
2014-04-15 19:44:37 +02:00
|
|
|
}
|
2013-11-26 21:57:34 +01:00
|
|
|
return matchedTests;
|
|
|
|
}
|
|
|
|
|
2014-05-20 20:02:10 +02:00
|
|
|
struct TagInfo {
|
|
|
|
TagInfo() : count ( 0 ) {}
|
|
|
|
void add( std::string const& spelling ) {
|
|
|
|
++count;
|
|
|
|
spellings.insert( spelling );
|
|
|
|
}
|
|
|
|
std::string all() const {
|
|
|
|
std::string out;
|
|
|
|
for( std::set<std::string>::const_iterator it = spellings.begin(), itEnd = spellings.end();
|
|
|
|
it != itEnd;
|
|
|
|
++it )
|
|
|
|
out += "[" + *it + "]";
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
std::set<std::string> spellings;
|
|
|
|
std::size_t count;
|
|
|
|
};
|
|
|
|
|
2013-06-07 19:41:22 +02:00
|
|
|
inline std::size_t listTags( Config const& config ) {
|
2014-05-16 19:24:07 +02:00
|
|
|
TestSpec testSpec = config.testSpec();
|
|
|
|
if( config.testSpec().hasFilters() )
|
2014-10-02 20:08:19 +02:00
|
|
|
Catch::cout() << "Tags for matching test cases:\n";
|
2014-05-16 19:24:07 +02:00
|
|
|
else {
|
2014-10-02 20:08:19 +02:00
|
|
|
Catch::cout() << "All available tags:\n";
|
2014-06-30 08:33:17 +02:00
|
|
|
testSpec = TestSpecParser( ITagAliasRegistry::get() ).parse( "*" ).testSpec();
|
2014-05-16 19:24:07 +02:00
|
|
|
}
|
2013-03-29 12:42:10 +01:00
|
|
|
|
2014-05-20 19:03:54 +02:00
|
|
|
std::map<std::string, TagInfo> tagCounts;
|
2013-07-03 20:14:59 +02:00
|
|
|
|
2015-08-05 00:11:56 +02:00
|
|
|
std::vector<TestCase> matchedTestCases = filterTests( getAllTestCasesSorted( config ), testSpec, config );
|
2014-04-15 19:44:37 +02:00
|
|
|
for( std::vector<TestCase>::const_iterator it = matchedTestCases.begin(), itEnd = matchedTestCases.end();
|
2013-11-12 19:59:34 +01:00
|
|
|
it != itEnd;
|
|
|
|
++it ) {
|
2014-04-15 19:44:37 +02:00
|
|
|
for( std::set<std::string>::const_iterator tagIt = it->getTestCaseInfo().tags.begin(),
|
|
|
|
tagItEnd = it->getTestCaseInfo().tags.end();
|
|
|
|
tagIt != tagItEnd;
|
|
|
|
++tagIt ) {
|
|
|
|
std::string tagName = *tagIt;
|
2014-05-20 19:03:54 +02:00
|
|
|
std::string lcaseTagName = toLower( tagName );
|
|
|
|
std::map<std::string, TagInfo>::iterator countIt = tagCounts.find( lcaseTagName );
|
2014-04-15 19:44:37 +02:00
|
|
|
if( countIt == tagCounts.end() )
|
2014-05-20 19:03:54 +02:00
|
|
|
countIt = tagCounts.insert( std::make_pair( lcaseTagName, TagInfo() ) ).first;
|
|
|
|
countIt->second.add( tagName );
|
2013-03-29 12:42:10 +01:00
|
|
|
}
|
|
|
|
}
|
2013-07-03 20:14:59 +02:00
|
|
|
|
2014-05-20 19:03:54 +02:00
|
|
|
for( std::map<std::string, TagInfo>::const_iterator countIt = tagCounts.begin(),
|
|
|
|
countItEnd = tagCounts.end();
|
2013-03-29 12:42:10 +01:00
|
|
|
countIt != countItEnd;
|
2013-07-03 20:14:59 +02:00
|
|
|
++countIt ) {
|
2013-11-12 19:59:34 +01:00
|
|
|
std::ostringstream oss;
|
2014-05-20 19:03:54 +02:00
|
|
|
oss << " " << std::setw(2) << countIt->second.count << " ";
|
|
|
|
Text wrapper( countIt->second.all(), TextAttributes()
|
|
|
|
.setInitialIndent( 0 )
|
|
|
|
.setIndent( oss.str().size() )
|
|
|
|
.setWidth( CATCH_CONFIG_CONSOLE_WIDTH-10 ) );
|
2017-01-29 23:07:15 +01:00
|
|
|
Catch::cout() << oss.str() << wrapper << '\n';
|
2013-03-29 12:42:10 +01:00
|
|
|
}
|
2017-01-29 23:07:15 +01:00
|
|
|
Catch::cout() << pluralise( tagCounts.size(), "tag" ) << '\n' << std::endl;
|
2013-06-06 23:54:42 +02:00
|
|
|
return tagCounts.size();
|
2013-03-29 12:42:10 +01:00
|
|
|
}
|
2013-03-28 23:13:31 +01:00
|
|
|
|
2013-06-07 19:41:22 +02:00
|
|
|
inline std::size_t listReporters( Config const& /*config*/ ) {
|
2014-12-19 18:52:33 +01:00
|
|
|
Catch::cout() << "Available reporters:\n";
|
2013-03-28 23:13:31 +01:00
|
|
|
IReporterRegistry::FactoryMap const& factories = getRegistryHub().getReporterRegistry().getFactories();
|
2013-06-07 19:56:43 +02:00
|
|
|
IReporterRegistry::FactoryMap::const_iterator itBegin = factories.begin(), itEnd = factories.end(), it;
|
|
|
|
std::size_t maxNameLen = 0;
|
|
|
|
for(it = itBegin; it != itEnd; ++it )
|
|
|
|
maxNameLen = (std::max)( maxNameLen, it->first.size() );
|
2013-07-03 20:14:59 +02:00
|
|
|
|
2013-06-07 19:56:43 +02:00
|
|
|
for(it = itBegin; it != itEnd; ++it ) {
|
|
|
|
Text wrapper( it->second->getDescription(), TextAttributes()
|
|
|
|
.setInitialIndent( 0 )
|
|
|
|
.setIndent( 7+maxNameLen )
|
|
|
|
.setWidth( CATCH_CONFIG_CONSOLE_WIDTH - maxNameLen-8 ) );
|
2014-10-02 20:08:19 +02:00
|
|
|
Catch::cout() << " "
|
2013-06-07 19:56:43 +02:00
|
|
|
<< it->first
|
2017-01-29 23:07:15 +01:00
|
|
|
<< ':'
|
2013-06-07 19:56:43 +02:00
|
|
|
<< std::string( maxNameLen - it->first.size() + 2, ' ' )
|
2017-01-29 23:07:15 +01:00
|
|
|
<< wrapper << '\n';
|
2013-06-07 19:56:43 +02:00
|
|
|
}
|
2014-10-02 20:08:19 +02:00
|
|
|
Catch::cout() << std::endl;
|
2013-06-06 23:54:42 +02:00
|
|
|
return factories.size();
|
2013-03-28 23:13:31 +01:00
|
|
|
}
|
2013-07-03 20:14:59 +02:00
|
|
|
|
2013-06-07 19:41:22 +02:00
|
|
|
inline Option<std::size_t> list( Config const& config ) {
|
2013-06-06 23:54:42 +02:00
|
|
|
Option<std::size_t> listedCount;
|
2013-06-07 19:41:22 +02:00
|
|
|
if( config.listTests() )
|
2013-06-06 23:54:42 +02:00
|
|
|
listedCount = listedCount.valueOr(0) + listTests( config );
|
2013-11-26 21:57:34 +01:00
|
|
|
if( config.listTestNamesOnly() )
|
|
|
|
listedCount = listedCount.valueOr(0) + listTestsNamesOnly( config );
|
2013-06-07 19:41:22 +02:00
|
|
|
if( config.listTags() )
|
2013-06-06 23:54:42 +02:00
|
|
|
listedCount = listedCount.valueOr(0) + listTags( config );
|
2013-06-07 19:41:22 +02:00
|
|
|
if( config.listReporters() )
|
2013-06-06 23:54:42 +02:00
|
|
|
listedCount = listedCount.valueOr(0) + listReporters( config );
|
|
|
|
return listedCount;
|
2010-11-10 00:24:00 +01:00
|
|
|
}
|
2013-07-03 20:14:59 +02:00
|
|
|
|
2010-11-10 00:24:00 +01:00
|
|
|
} // end namespace Catch
|
|
|
|
|
2010-11-11 21:42:16 +01:00
|
|
|
#endif // TWOBLUECUBES_CATCH_LIST_HPP_INCLUDED
|