mirror of
https://github.com/catchorg/Catch2.git
synced 2024-11-22 13:26:10 +01:00
Pipe --list* through reporters
This allows us to provide machine-readable listings through the XMLReporter and it will also allow users to define their own listing format that does whatever their own tools need.
This commit is contained in:
parent
0f39438aae
commit
85b129c741
@ -6,7 +6,13 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
#include "catch_interfaces_reporter.h"
|
#include "catch_interfaces_reporter.h"
|
||||||
|
#include "catch_console_colour.h"
|
||||||
#include "../reporters/catch_reporter_listening.h"
|
#include "../reporters/catch_reporter_listening.h"
|
||||||
|
#include "catch_list.h"
|
||||||
|
#include "catch_text.h"
|
||||||
|
|
||||||
|
#include <algorithm>
|
||||||
|
#include <iomanip>
|
||||||
|
|
||||||
namespace Catch {
|
namespace Catch {
|
||||||
|
|
||||||
@ -108,6 +114,86 @@ namespace Catch {
|
|||||||
void IStreamingReporter::fatalErrorEncountered( StringRef ) {}
|
void IStreamingReporter::fatalErrorEncountered( StringRef ) {}
|
||||||
bool IStreamingReporter::isMulti() const { return false; }
|
bool IStreamingReporter::isMulti() const { return false; }
|
||||||
|
|
||||||
|
void IStreamingReporter::listReporters(std::vector<ReporterDescription> const& descriptions, Config const& config) {
|
||||||
|
Catch::cout() << "Available reporters:\n";
|
||||||
|
const auto maxNameLen = std::max_element(descriptions.begin(), descriptions.end(),
|
||||||
|
[](ReporterDescription const& lhs, ReporterDescription const& rhs) { return lhs.name.size() < rhs.name.size(); })
|
||||||
|
->name.size();
|
||||||
|
|
||||||
|
for (auto const& desc : descriptions) {
|
||||||
|
if (config.verbosity() == Verbosity::Quiet) {
|
||||||
|
Catch::cout()
|
||||||
|
<< Column(desc.name)
|
||||||
|
.indent(2)
|
||||||
|
.width(5 + maxNameLen) << '\n';
|
||||||
|
} else {
|
||||||
|
Catch::cout()
|
||||||
|
<< Column(desc.name + ":")
|
||||||
|
.indent(2)
|
||||||
|
.width(5 + maxNameLen)
|
||||||
|
+ Column(desc.description)
|
||||||
|
.initialIndent(0)
|
||||||
|
.indent(2)
|
||||||
|
.width(CATCH_CONFIG_CONSOLE_WIDTH - maxNameLen - 8)
|
||||||
|
<< '\n';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Catch::cout() << std::endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
void IStreamingReporter::listTests(std::vector<TestCase> const& tests, Config const& config) {
|
||||||
|
if (config.hasTestFilters())
|
||||||
|
Catch::cout() << "Matching test cases:\n";
|
||||||
|
else {
|
||||||
|
Catch::cout() << "All available test cases:\n";
|
||||||
|
}
|
||||||
|
|
||||||
|
for (auto const& testCaseInfo : tests) {
|
||||||
|
Colour::Code colour = testCaseInfo.isHidden()
|
||||||
|
? Colour::SecondaryText
|
||||||
|
: Colour::None;
|
||||||
|
Colour colourGuard(colour);
|
||||||
|
|
||||||
|
Catch::cout() << Column(testCaseInfo.name).initialIndent(2).indent(4) << '\n';
|
||||||
|
if (config.verbosity() >= Verbosity::High) {
|
||||||
|
Catch::cout() << Column(Catch::Detail::stringify(testCaseInfo.lineInfo)).indent(4) << std::endl;
|
||||||
|
std::string description = testCaseInfo.description;
|
||||||
|
if (description.empty())
|
||||||
|
description = "(NO DESCRIPTION)";
|
||||||
|
Catch::cout() << Column(description).indent(4) << std::endl;
|
||||||
|
}
|
||||||
|
if (!testCaseInfo.tags.empty() && config.verbosity() > Verbosity::Quiet) {
|
||||||
|
Catch::cout() << Column(testCaseInfo.tagsAsString()).indent(6) << '\n';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!config.hasTestFilters()) {
|
||||||
|
Catch::cout() << pluralise(tests.size(), "test case") << '\n' << std::endl;
|
||||||
|
} else {
|
||||||
|
Catch::cout() << pluralise(tests.size(), "matching test case") << '\n' << std::endl;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void IStreamingReporter::listTags(std::vector<TagInfo> const& tags, Config const& config) {
|
||||||
|
if (config.hasTestFilters()) {
|
||||||
|
Catch::cout() << "Tags for matching test cases:\n";
|
||||||
|
} else {
|
||||||
|
Catch::cout() << "All available tags:\n";
|
||||||
|
}
|
||||||
|
|
||||||
|
for (auto const& tagCount : tags) {
|
||||||
|
ReusableStringStream rss;
|
||||||
|
rss << " " << std::setw(2) << tagCount.count << " ";
|
||||||
|
auto str = rss.str();
|
||||||
|
auto wrapper = Column(tagCount.all())
|
||||||
|
.initialIndent(0)
|
||||||
|
.indent(str.size())
|
||||||
|
.width(CATCH_CONFIG_CONSOLE_WIDTH - 10);
|
||||||
|
Catch::cout() << str << wrapper << '\n';
|
||||||
|
}
|
||||||
|
Catch::cout() << pluralise(tags.size(), "tag") << '\n' << std::endl;
|
||||||
|
}
|
||||||
|
|
||||||
IReporterFactory::~IReporterFactory() = default;
|
IReporterFactory::~IReporterFactory() = default;
|
||||||
IReporterRegistry::~IReporterRegistry() = default;
|
IReporterRegistry::~IReporterRegistry() = default;
|
||||||
|
|
||||||
|
@ -33,6 +33,9 @@
|
|||||||
|
|
||||||
namespace Catch {
|
namespace Catch {
|
||||||
|
|
||||||
|
struct ReporterDescription;
|
||||||
|
struct TagInfo;
|
||||||
|
|
||||||
struct ReporterConfig {
|
struct ReporterConfig {
|
||||||
explicit ReporterConfig( IConfigPtr const& _fullConfig );
|
explicit ReporterConfig( IConfigPtr const& _fullConfig );
|
||||||
|
|
||||||
@ -244,6 +247,12 @@ namespace Catch {
|
|||||||
virtual void fatalErrorEncountered( StringRef name );
|
virtual void fatalErrorEncountered( StringRef name );
|
||||||
|
|
||||||
virtual bool isMulti() const;
|
virtual bool isMulti() const;
|
||||||
|
|
||||||
|
// Listing support
|
||||||
|
virtual void listReporters(std::vector<ReporterDescription> const& descriptions, Config const& config);
|
||||||
|
virtual void listTests(std::vector<TestCase> const& tests, Config const& config);
|
||||||
|
virtual void listTags(std::vector<TagInfo> const& tags, Config const& config);
|
||||||
|
|
||||||
};
|
};
|
||||||
using IStreamingReporterPtr = std::unique_ptr<IStreamingReporter>;
|
using IStreamingReporterPtr = std::unique_ptr<IStreamingReporter>;
|
||||||
|
|
||||||
|
@ -23,65 +23,22 @@
|
|||||||
|
|
||||||
#include <limits>
|
#include <limits>
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
#include <iomanip>
|
|
||||||
#include <set>
|
#include <set>
|
||||||
|
|
||||||
namespace Catch {
|
namespace Catch {
|
||||||
namespace {
|
namespace {
|
||||||
|
|
||||||
struct TagInfo {
|
void listTests(IStreamingReporter& reporter, Config const& config) {
|
||||||
void add(std::string const& spelling);
|
|
||||||
std::string all() const;
|
|
||||||
|
|
||||||
std::set<std::string> spellings;
|
|
||||||
std::size_t count = 0;
|
|
||||||
};
|
|
||||||
|
|
||||||
|
|
||||||
void listTests(Config const& config) {
|
|
||||||
TestSpec testSpec = config.testSpec();
|
TestSpec testSpec = config.testSpec();
|
||||||
if (config.hasTestFilters())
|
|
||||||
Catch::cout() << "Matching test cases:\n";
|
|
||||||
else {
|
|
||||||
Catch::cout() << "All available test cases:\n";
|
|
||||||
}
|
|
||||||
|
|
||||||
auto matchedTestCases = filterTests(getAllTestCasesSorted(config), testSpec, config);
|
auto matchedTestCases = filterTests(getAllTestCasesSorted(config), testSpec, config);
|
||||||
for (auto const& testCaseInfo : matchedTestCases) {
|
reporter.listTests(matchedTestCases, config);
|
||||||
Colour::Code colour = testCaseInfo.isHidden()
|
|
||||||
? Colour::SecondaryText
|
|
||||||
: Colour::None;
|
|
||||||
Colour colourGuard(colour);
|
|
||||||
|
|
||||||
Catch::cout() << Column(testCaseInfo.name).initialIndent(2).indent(4) << "\n";
|
|
||||||
if (config.verbosity() >= Verbosity::High) {
|
|
||||||
Catch::cout() << Column(Catch::Detail::stringify(testCaseInfo.lineInfo)).indent(4) << std::endl;
|
|
||||||
std::string description = testCaseInfo.description;
|
|
||||||
if (description.empty())
|
|
||||||
description = "(NO DESCRIPTION)";
|
|
||||||
Catch::cout() << Column(description).indent(4) << std::endl;
|
|
||||||
}
|
|
||||||
if (!testCaseInfo.tags.empty())
|
|
||||||
Catch::cout() << Column(testCaseInfo.tagsAsString()).indent(6) << "\n";
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!config.hasTestFilters())
|
|
||||||
Catch::cout() << pluralise(matchedTestCases.size(), "test case") << '\n' << std::endl;
|
|
||||||
else
|
|
||||||
Catch::cout() << pluralise(matchedTestCases.size(), "matching test case") << '\n' << std::endl;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void listTags(Config const& config) {
|
void listTags(IStreamingReporter& reporter, Config const& config) {
|
||||||
TestSpec testSpec = config.testSpec();
|
TestSpec testSpec = config.testSpec();
|
||||||
if (config.hasTestFilters())
|
std::vector<TestCase> matchedTestCases = filterTests(getAllTestCasesSorted(config), testSpec, config);
|
||||||
Catch::cout() << "Tags for matching test cases:\n";
|
|
||||||
else {
|
|
||||||
Catch::cout() << "All available tags:\n";
|
|
||||||
}
|
|
||||||
|
|
||||||
std::map<std::string, TagInfo> tagCounts;
|
std::map<std::string, TagInfo> tagCounts;
|
||||||
|
|
||||||
std::vector<TestCase> matchedTestCases = filterTests(getAllTestCasesSorted(config), testSpec, config);
|
|
||||||
for (auto const& testCase : matchedTestCases) {
|
for (auto const& testCase : matchedTestCases) {
|
||||||
for (auto const& tagName : testCase.getTestCaseInfo().tags) {
|
for (auto const& tagName : testCase.getTestCaseInfo().tags) {
|
||||||
std::string lcaseTagName = toLower(tagName);
|
std::string lcaseTagName = toLower(tagName);
|
||||||
@ -92,38 +49,24 @@ namespace Catch {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
for (auto const& tagCount : tagCounts) {
|
std::vector<TagInfo> infos; infos.reserve(tagCounts.size());
|
||||||
ReusableStringStream rss;
|
for (auto const& tagc : tagCounts) {
|
||||||
rss << " " << std::setw(2) << tagCount.second.count << " ";
|
infos.push_back(std::move(tagc.second));
|
||||||
auto str = rss.str();
|
|
||||||
auto wrapper = Column(tagCount.second.all())
|
|
||||||
.initialIndent(0)
|
|
||||||
.indent(str.size())
|
|
||||||
.width(CATCH_CONFIG_CONSOLE_WIDTH - 10);
|
|
||||||
Catch::cout() << str << wrapper << '\n';
|
|
||||||
}
|
}
|
||||||
Catch::cout() << pluralise(tagCounts.size(), "tag") << '\n' << std::endl;
|
|
||||||
|
reporter.listTags(infos, config);
|
||||||
}
|
}
|
||||||
|
|
||||||
void listReporters() {
|
void listReporters(IStreamingReporter& reporter, Config const& config) {
|
||||||
Catch::cout() << "Available reporters:\n";
|
std::vector<ReporterDescription> descriptions;
|
||||||
IReporterRegistry::FactoryMap const& factories = getRegistryHub().getReporterRegistry().getFactories();
|
|
||||||
std::size_t maxNameLen = 0;
|
|
||||||
for (auto const& factoryKvp : factories)
|
|
||||||
maxNameLen = (std::max)(maxNameLen, factoryKvp.first.size());
|
|
||||||
|
|
||||||
for (auto const& factoryKvp : factories) {
|
IReporterRegistry::FactoryMap const& factories = getRegistryHub().getReporterRegistry().getFactories();
|
||||||
Catch::cout()
|
descriptions.reserve(factories.size());
|
||||||
<< Column(factoryKvp.first + ":")
|
for (auto const& fac : factories) {
|
||||||
.indent(2)
|
descriptions.push_back({ fac.first, fac.second->getDescription() });
|
||||||
.width(5 + maxNameLen)
|
|
||||||
+ Column(factoryKvp.second->getDescription())
|
|
||||||
.initialIndent(0)
|
|
||||||
.indent(2)
|
|
||||||
.width(CATCH_CONFIG_CONSOLE_WIDTH - maxNameLen - 8)
|
|
||||||
<< "\n";
|
|
||||||
}
|
}
|
||||||
Catch::cout() << std::endl;
|
|
||||||
|
reporter.listReporters(descriptions, config);
|
||||||
}
|
}
|
||||||
|
|
||||||
} // end anonymous namespace
|
} // end anonymous namespace
|
||||||
@ -149,20 +92,20 @@ namespace Catch {
|
|||||||
return out;
|
return out;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool list( std::shared_ptr<Config> const& config ) {
|
bool list( IStreamingReporter& reporter, std::shared_ptr<Config> const& config ) {
|
||||||
bool listed = false;
|
bool listed = false;
|
||||||
getCurrentMutableContext().setConfig( config );
|
getCurrentMutableContext().setConfig( config );
|
||||||
if (config->listTests()) {
|
if (config->listTests()) {
|
||||||
listed = true;
|
listed = true;
|
||||||
listTests(*config);
|
listTests(reporter, *config);
|
||||||
}
|
}
|
||||||
if (config->listTags()) {
|
if (config->listTags()) {
|
||||||
listed = true;
|
listed = true;
|
||||||
listTags(*config);
|
listTags(reporter, *config);
|
||||||
}
|
}
|
||||||
if (config->listReporters()) {
|
if (config->listReporters()) {
|
||||||
listed = true;
|
listed = true;
|
||||||
listReporters();
|
listReporters(reporter, *config);
|
||||||
}
|
}
|
||||||
return listed;
|
return listed;
|
||||||
}
|
}
|
||||||
|
@ -10,9 +10,29 @@
|
|||||||
|
|
||||||
#include "catch_config.hpp"
|
#include "catch_config.hpp"
|
||||||
|
|
||||||
|
#include <set>
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
|
||||||
namespace Catch {
|
namespace Catch {
|
||||||
|
|
||||||
bool list( std::shared_ptr<Config> const& config );
|
struct IStreamingReporter;
|
||||||
|
|
||||||
|
struct ReporterDescription {
|
||||||
|
std::string name, description;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct TagInfo {
|
||||||
|
void add(std::string const& spelling);
|
||||||
|
std::string all() const;
|
||||||
|
|
||||||
|
std::set<std::string> spellings;
|
||||||
|
std::size_t count = 0;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct testClassInfo {};
|
||||||
|
|
||||||
|
bool list( IStreamingReporter& reporter, std::shared_ptr<Config> const& config );
|
||||||
|
|
||||||
} // end namespace Catch
|
} // end namespace Catch
|
||||||
|
|
||||||
|
@ -62,14 +62,14 @@ namespace Catch {
|
|||||||
|
|
||||||
class TestGroup {
|
class TestGroup {
|
||||||
public:
|
public:
|
||||||
explicit TestGroup(std::shared_ptr<Config> const& config)
|
explicit TestGroup(IStreamingReporterPtr&& reporter, std::shared_ptr<Config> const& config)
|
||||||
: m_config{config}
|
: m_config{config}
|
||||||
, m_context{config, makeReporter(config)}
|
, m_context{config, std::move(reporter)}
|
||||||
{
|
{
|
||||||
auto const& allTestCases = getAllTestCasesSorted(*m_config);
|
auto const& allTestCases = getAllTestCasesSorted(*m_config);
|
||||||
m_matches = m_config->testSpec().matchesByFilter(allTestCases, *m_config);
|
m_matches = m_config->testSpec().matchesByFilter(allTestCases, *m_config);
|
||||||
auto const& invalidArgs = m_config->testSpec().getInvalidArgs();
|
auto const& invalidArgs = m_config->testSpec().getInvalidArgs();
|
||||||
|
|
||||||
if (m_matches.empty() && invalidArgs.empty()) {
|
if (m_matches.empty() && invalidArgs.empty()) {
|
||||||
for (auto const& test : allTestCases)
|
for (auto const& test : allTestCases)
|
||||||
if (!test.isHidden())
|
if (!test.isHidden())
|
||||||
@ -97,12 +97,12 @@ namespace Catch {
|
|||||||
totals.error = -1;
|
totals.error = -1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!invalidArgs.empty()) {
|
if (!invalidArgs.empty()) {
|
||||||
for (auto const& invalidArg: invalidArgs)
|
for (auto const& invalidArg: invalidArgs)
|
||||||
m_context.reporter().reportInvalidArguments(invalidArg);
|
m_context.reporter().reportInvalidArguments(invalidArg);
|
||||||
}
|
}
|
||||||
|
|
||||||
m_context.testGroupEnded(m_config->name(), totals, 1, 1);
|
m_context.testGroupEnded(m_config->name(), totals, 1, 1);
|
||||||
return totals;
|
return totals;
|
||||||
}
|
}
|
||||||
@ -287,12 +287,15 @@ namespace Catch {
|
|||||||
if( m_configData.filenamesAsTags )
|
if( m_configData.filenamesAsTags )
|
||||||
applyFilenamesAsTags( *m_config );
|
applyFilenamesAsTags( *m_config );
|
||||||
|
|
||||||
|
// Create reporter(s) so we can route listings through them
|
||||||
|
auto reporter = makeReporter(m_config);
|
||||||
|
|
||||||
// Handle list request
|
// Handle list request
|
||||||
if (list(m_config)) {
|
if (list(*reporter, m_config)) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
TestGroup tests { m_config };
|
TestGroup tests { std::move(reporter), m_config };
|
||||||
auto const totals = tests.execute();
|
auto const totals = tests.execute();
|
||||||
|
|
||||||
if( m_config->warnAboutNoTests() && totals.error == -1 )
|
if( m_config->warnAboutNoTests() && totals.error == -1 )
|
||||||
|
Loading…
Reference in New Issue
Block a user