2011-01-07 20:57:32 +01:00
|
|
|
/*
|
2012-08-14 20:30:30 +02:00
|
|
|
* Created by Phil on 14/08/2012.
|
|
|
|
* Copyright 2012 Two Blue Cubes Ltd. All rights reserved.
|
2011-01-07 20:57:32 +01:00
|
|
|
*
|
|
|
|
* 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)
|
|
|
|
*/
|
|
|
|
|
2012-08-14 20:30:30 +02:00
|
|
|
#include "catch_test_case_info.h"
|
2017-08-01 18:46:33 +02:00
|
|
|
#include "catch_enforce.h"
|
2017-09-07 12:24:33 +02:00
|
|
|
#include "catch_test_spec.h"
|
2011-01-07 20:57:32 +01:00
|
|
|
#include "catch_interfaces_testcase.h"
|
2017-07-25 21:57:35 +02:00
|
|
|
#include "catch_string_manip.h"
|
2011-01-07 20:57:32 +01:00
|
|
|
|
2017-02-12 12:17:07 +01:00
|
|
|
#include <cctype>
|
2017-06-04 21:39:27 +02:00
|
|
|
#include <exception>
|
2017-07-27 22:31:27 +02:00
|
|
|
#include <algorithm>
|
2017-11-07 19:01:10 +01:00
|
|
|
#include <sstream>
|
2017-02-12 12:17:07 +01:00
|
|
|
|
2012-05-16 00:58:23 +02:00
|
|
|
namespace Catch {
|
|
|
|
|
2018-06-12 15:09:30 +02:00
|
|
|
namespace {
|
2019-11-07 12:39:07 +01:00
|
|
|
using TCP_underlying_type = uint8_t;
|
|
|
|
static_assert(sizeof(TestCaseProperties) == sizeof(TCP_underlying_type),
|
|
|
|
"The size of the TestCaseProperties is different from the assumed size");
|
|
|
|
|
|
|
|
TestCaseProperties operator|(TestCaseProperties lhs, TestCaseProperties rhs) {
|
|
|
|
return static_cast<TestCaseProperties>(
|
|
|
|
static_cast<TCP_underlying_type>(lhs) | static_cast<TCP_underlying_type>(rhs)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
TestCaseProperties& operator|=(TestCaseProperties& lhs, TestCaseProperties rhs) {
|
|
|
|
lhs = static_cast<TestCaseProperties>(
|
|
|
|
static_cast<TCP_underlying_type>(lhs) | static_cast<TCP_underlying_type>(rhs)
|
|
|
|
);
|
|
|
|
return lhs;
|
|
|
|
}
|
|
|
|
|
|
|
|
TestCaseProperties operator&(TestCaseProperties lhs, TestCaseProperties rhs) {
|
|
|
|
return static_cast<TestCaseProperties>(
|
|
|
|
static_cast<TCP_underlying_type>(lhs) & static_cast<TCP_underlying_type>(rhs)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool applies(TestCaseProperties tcp) {
|
|
|
|
static_assert(static_cast<TCP_underlying_type>(TestCaseProperties::None) == 0,
|
|
|
|
"TestCaseProperties::None must be equal to 0");
|
|
|
|
return tcp != TestCaseProperties::None;
|
|
|
|
}
|
|
|
|
|
|
|
|
TestCaseProperties parseSpecialTag( StringRef tag ) {
|
|
|
|
if( (!tag.empty() && tag[0] == '.')
|
|
|
|
|| tag == "!hide"_sr )
|
|
|
|
return TestCaseProperties::IsHidden;
|
|
|
|
else if( tag == "!throws"_sr )
|
|
|
|
return TestCaseProperties::Throws;
|
|
|
|
else if( tag == "!shouldfail"_sr )
|
|
|
|
return TestCaseProperties::ShouldFail;
|
|
|
|
else if( tag == "!mayfail"_sr )
|
|
|
|
return TestCaseProperties::MayFail;
|
|
|
|
else if( tag == "!nonportable"_sr )
|
|
|
|
return TestCaseProperties::NonPortable;
|
|
|
|
else if( tag == "!benchmark"_sr )
|
|
|
|
return static_cast<TestCaseProperties>(TestCaseProperties::Benchmark | TestCaseProperties::IsHidden );
|
2018-06-12 15:09:30 +02:00
|
|
|
else
|
2019-11-07 12:39:07 +01:00
|
|
|
return TestCaseProperties::None;
|
2018-06-12 15:09:30 +02:00
|
|
|
}
|
2019-11-07 12:39:07 +01:00
|
|
|
bool isReservedTag( StringRef tag ) {
|
|
|
|
return parseSpecialTag( tag ) == TestCaseProperties::None
|
|
|
|
&& tag.size() > 0
|
|
|
|
&& !std::isalnum( static_cast<unsigned char>(tag[0]) );
|
2018-06-12 15:09:30 +02:00
|
|
|
}
|
2019-11-07 12:39:07 +01:00
|
|
|
void enforceNotReservedTag( StringRef tag, SourceLineInfo const& _lineInfo ) {
|
2018-06-12 15:09:30 +02:00
|
|
|
CATCH_ENFORCE( !isReservedTag(tag),
|
|
|
|
"Tag name: [" << tag << "] is not allowed.\n"
|
2019-04-08 23:30:28 +02:00
|
|
|
<< "Tag names starting with non alphanumeric characters are reserved\n"
|
2018-06-12 15:09:30 +02:00
|
|
|
<< _lineInfo );
|
|
|
|
}
|
2019-11-04 13:45:39 +01:00
|
|
|
|
|
|
|
std::string makeDefaultName() {
|
|
|
|
static size_t counter = 0;
|
|
|
|
return "Anonymous test case " + std::to_string(++counter);
|
|
|
|
}
|
2014-04-15 19:44:37 +02:00
|
|
|
|
2019-11-07 12:39:07 +01:00
|
|
|
StringRef extractFilenamePart(StringRef filename) {
|
|
|
|
size_t lastDot = filename.size();
|
|
|
|
while (lastDot > 0 && filename[lastDot - 1] != '.') {
|
|
|
|
--lastDot;
|
|
|
|
}
|
|
|
|
--lastDot;
|
2014-05-16 19:52:55 +02:00
|
|
|
|
2019-11-07 12:39:07 +01:00
|
|
|
size_t nameStart = lastDot;
|
|
|
|
while (nameStart > 0 && filename[nameStart - 1] != '/' && filename[nameStart - 1] != '\\') {
|
|
|
|
--nameStart;
|
2014-04-15 19:44:37 +02:00
|
|
|
}
|
2019-11-07 12:39:07 +01:00
|
|
|
|
|
|
|
return filename.substr(nameStart, lastDot - nameStart);
|
2014-05-20 19:11:23 +02:00
|
|
|
}
|
2015-11-04 19:01:28 +01:00
|
|
|
|
2019-11-07 12:39:07 +01:00
|
|
|
// Returns the upper bound on size of extra tags ([#file]+[.])
|
|
|
|
size_t sizeOfExtraTags(StringRef filepath) {
|
|
|
|
// [.] is 3, [#] is another 3
|
|
|
|
const size_t extras = 3 + 3;
|
|
|
|
return extractFilenamePart(filepath).size() + extras;
|
|
|
|
}
|
2012-09-15 18:53:27 +02:00
|
|
|
}
|
2011-01-07 20:57:32 +01:00
|
|
|
|
2019-11-07 12:39:07 +01:00
|
|
|
std::unique_ptr<TestCaseInfo>
|
|
|
|
makeTestCaseInfo(std::string const& _className,
|
|
|
|
NameAndTags const& nameAndTags,
|
|
|
|
SourceLineInfo const& _lineInfo ) {
|
|
|
|
return std::make_unique<TestCaseInfo>(_className, nameAndTags, _lineInfo);
|
2015-07-02 09:20:18 +02:00
|
|
|
}
|
2015-11-04 19:01:28 +01:00
|
|
|
|
2019-11-07 12:39:07 +01:00
|
|
|
TestCaseInfo::TestCaseInfo(std::string const& _className,
|
|
|
|
NameAndTags const& _nameAndTags,
|
|
|
|
SourceLineInfo const& _lineInfo):
|
|
|
|
name( _nameAndTags.name.empty() ? makeDefaultName() : _nameAndTags.name ),
|
2012-11-25 12:19:55 +01:00
|
|
|
className( _className ),
|
2019-11-07 12:39:07 +01:00
|
|
|
lineInfo( _lineInfo )
|
2013-03-28 23:13:31 +01:00
|
|
|
{
|
2019-11-07 12:39:07 +01:00
|
|
|
StringRef originalTags = _nameAndTags.tags;
|
|
|
|
// We need to reserve enough space to store all of the tags
|
|
|
|
// (including optional hidden tag and filename tag)
|
|
|
|
auto requiredSize = originalTags.size() + sizeOfExtraTags(_lineInfo.file);
|
|
|
|
backingTags.reserve(requiredSize);
|
|
|
|
backingLCaseTags.reserve(requiredSize);
|
|
|
|
|
|
|
|
// We cannot copy the tags directly, as we need to normalize
|
|
|
|
// some tags, so that [.foo] is copied as [.][foo].
|
|
|
|
size_t tagStart = 0;
|
|
|
|
size_t tagEnd = 0;
|
|
|
|
bool inTag = false;
|
|
|
|
for (size_t idx = 0; idx < originalTags.size(); ++idx) {
|
|
|
|
auto c = originalTags[idx];
|
|
|
|
if (c == '[') {
|
|
|
|
assert(!inTag);
|
|
|
|
inTag = true;
|
|
|
|
tagStart = idx;
|
|
|
|
}
|
|
|
|
if (c == ']') {
|
|
|
|
assert(inTag);
|
|
|
|
inTag = false;
|
|
|
|
tagEnd = idx;
|
|
|
|
assert(tagStart < tagEnd);
|
|
|
|
|
|
|
|
// We need to check the tag for special meanings, copy
|
|
|
|
// it over to backing storage and actually reference the
|
|
|
|
// backing storage in the saved tags
|
|
|
|
StringRef tagStr = originalTags.substr(tagStart+1, tagEnd - tagStart - 1);
|
|
|
|
enforceNotReservedTag(tagStr, lineInfo);
|
|
|
|
properties |= parseSpecialTag(tagStr);
|
|
|
|
// When copying a tag to the backing storage, we need to
|
|
|
|
// check if it is a merged hide tag, such as [.foo], and
|
|
|
|
// if it is, we need to handle it as if it was [foo].
|
|
|
|
if (tagStr.size() > 1 && tagStr[0] == '.') {
|
|
|
|
tagStr = tagStr.substr(1, tagStr.size() - 1);
|
|
|
|
}
|
|
|
|
// We skip over dealing with the [.] tag, as we will add
|
|
|
|
// it later unconditionally and then sort and unique all
|
|
|
|
// the tags.
|
|
|
|
internalAppendTag(tagStr);
|
|
|
|
}
|
|
|
|
(void)inTag; // Silence "set-but-unused" warning in release mode.
|
|
|
|
}
|
|
|
|
// Add [.] if relevant
|
|
|
|
if (isHidden()) {
|
|
|
|
internalAppendTag("."_sr);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Sort and prepare tags
|
|
|
|
toLowerInPlace(backingLCaseTags);
|
|
|
|
std::sort(begin(tags), end(tags), [](Tag lhs, Tag rhs) { return lhs.lowerCased < rhs.lowerCased; });
|
|
|
|
tags.erase(std::unique(begin(tags), end(tags), [](Tag lhs, Tag rhs) {return lhs.lowerCased == rhs.lowerCased; }),
|
|
|
|
end(tags));
|
2013-03-28 23:13:31 +01:00
|
|
|
}
|
2012-08-14 09:38:22 +02:00
|
|
|
|
2014-07-03 09:09:57 +02:00
|
|
|
bool TestCaseInfo::isHidden() const {
|
2019-11-07 12:39:07 +01:00
|
|
|
return applies( properties & TestCaseProperties::IsHidden );
|
2014-07-03 09:09:57 +02:00
|
|
|
}
|
|
|
|
bool TestCaseInfo::throws() const {
|
2019-11-07 12:39:07 +01:00
|
|
|
return applies( properties & TestCaseProperties::Throws );
|
2014-07-03 09:09:57 +02:00
|
|
|
}
|
|
|
|
bool TestCaseInfo::okToFail() const {
|
2019-11-07 12:39:07 +01:00
|
|
|
return applies( properties & (TestCaseProperties::ShouldFail | TestCaseProperties::MayFail ) );
|
2014-07-03 09:09:57 +02:00
|
|
|
}
|
|
|
|
bool TestCaseInfo::expectedToFail() const {
|
2019-11-07 12:39:07 +01:00
|
|
|
return applies( properties & (TestCaseProperties::ShouldFail) );
|
|
|
|
}
|
|
|
|
|
|
|
|
void TestCaseInfo::addFilenameTag() {
|
|
|
|
std::string combined("#");
|
|
|
|
combined += extractFilenamePart(lineInfo.file);
|
|
|
|
internalAppendTag(combined);
|
2014-07-03 09:09:57 +02:00
|
|
|
}
|
|
|
|
|
2017-07-27 22:31:27 +02:00
|
|
|
std::string TestCaseInfo::tagsAsString() const {
|
|
|
|
std::string ret;
|
|
|
|
// '[' and ']' per tag
|
2017-09-18 18:13:17 +02:00
|
|
|
std::size_t full_size = 2 * tags.size();
|
2017-07-27 22:31:27 +02:00
|
|
|
for (const auto& tag : tags) {
|
2019-11-07 12:39:07 +01:00
|
|
|
full_size += tag.original.size();
|
2017-07-27 22:31:27 +02:00
|
|
|
}
|
|
|
|
ret.reserve(full_size);
|
|
|
|
for (const auto& tag : tags) {
|
|
|
|
ret.push_back('[');
|
2019-11-07 12:39:07 +01:00
|
|
|
ret += tag.original;
|
2017-07-27 22:31:27 +02:00
|
|
|
ret.push_back(']');
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2019-11-07 12:39:07 +01:00
|
|
|
void TestCaseInfo::internalAppendTag(StringRef tagStr) {
|
|
|
|
backingTags += '[';
|
|
|
|
const auto backingStart = backingTags.size();
|
|
|
|
backingTags += tagStr;
|
|
|
|
const auto backingEnd = backingTags.size();
|
|
|
|
backingTags += ']';
|
|
|
|
backingLCaseTags += '[';
|
|
|
|
// We append the tag to the lower-case backing storage as-is,
|
|
|
|
// because we will perform the lower casing later, in bulk
|
|
|
|
backingLCaseTags += tagStr;
|
|
|
|
backingLCaseTags += ']';
|
|
|
|
tags.emplace_back(StringRef(backingTags.c_str() + backingStart, backingEnd - backingStart),
|
|
|
|
StringRef(backingLCaseTags.c_str() + backingStart, backingEnd - backingStart));
|
|
|
|
}
|
|
|
|
|
2014-07-03 09:09:57 +02:00
|
|
|
|
2019-11-04 21:35:57 +01:00
|
|
|
bool TestCaseHandle::operator == ( TestCaseHandle const& rhs ) const {
|
|
|
|
return m_invoker == rhs.m_invoker
|
|
|
|
&& m_info->name == rhs.m_info->name
|
|
|
|
&& m_info->className == rhs.m_info->className;
|
2012-08-14 20:30:30 +02:00
|
|
|
}
|
|
|
|
|
2019-11-04 21:35:57 +01:00
|
|
|
bool TestCaseHandle::operator < ( TestCaseHandle const& rhs ) const {
|
|
|
|
return m_info->name < rhs.m_info->name;
|
2012-08-14 20:30:30 +02:00
|
|
|
}
|
|
|
|
|
2019-11-04 21:35:57 +01:00
|
|
|
TestCaseInfo const& TestCaseHandle::getTestCaseInfo() const {
|
|
|
|
return *m_info;
|
2012-11-25 12:19:55 +01:00
|
|
|
}
|
|
|
|
|
2012-09-21 08:48:03 +02:00
|
|
|
} // end namespace Catch
|