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-09-17 07:42:29 +02:00
|
|
|
#ifndef TWOBLUECUBES_CATCH_TEST_CASE_INFO_HPP_INCLUDED
|
|
|
|
#define TWOBLUECUBES_CATCH_TEST_CASE_INFO_HPP_INCLUDED
|
2011-01-07 20:57:32 +01:00
|
|
|
|
2014-05-16 19:28:58 +02:00
|
|
|
#include "catch_test_spec.hpp"
|
2012-08-14 20:30:30 +02:00
|
|
|
#include "catch_test_case_info.h"
|
2011-01-07 20:57:32 +01:00
|
|
|
#include "catch_interfaces_testcase.h"
|
2013-03-22 20:00:42 +01:00
|
|
|
#include "catch_common.h"
|
2011-01-07 20:57:32 +01:00
|
|
|
|
2017-02-12 12:17:07 +01:00
|
|
|
#include <cctype>
|
|
|
|
|
2012-05-16 00:58:23 +02:00
|
|
|
namespace Catch {
|
|
|
|
|
2014-07-03 09:09:57 +02:00
|
|
|
inline TestCaseInfo::SpecialProperties parseSpecialTag( std::string const& tag ) {
|
2017-01-15 09:41:33 +01:00
|
|
|
if( startsWith( tag, '.' ) ||
|
2014-07-03 09:09:57 +02:00
|
|
|
tag == "hide" ||
|
|
|
|
tag == "!hide" )
|
|
|
|
return TestCaseInfo::IsHidden;
|
|
|
|
else if( tag == "!throws" )
|
|
|
|
return TestCaseInfo::Throws;
|
|
|
|
else if( tag == "!shouldfail" )
|
|
|
|
return TestCaseInfo::ShouldFail;
|
|
|
|
else if( tag == "!mayfail" )
|
|
|
|
return TestCaseInfo::MayFail;
|
2017-01-23 18:44:55 +01:00
|
|
|
else if( tag == "!nonportable" )
|
|
|
|
return TestCaseInfo::NonPortable;
|
2014-07-03 09:09:57 +02:00
|
|
|
else
|
|
|
|
return TestCaseInfo::None;
|
2014-04-15 19:44:37 +02:00
|
|
|
}
|
|
|
|
inline bool isReservedTag( std::string const& tag ) {
|
2017-01-26 19:11:20 +01:00
|
|
|
return parseSpecialTag( tag ) == TestCaseInfo::None && tag.size() > 0 && !std::isalnum( tag[0] );
|
2014-04-15 19:44:37 +02:00
|
|
|
}
|
2014-05-16 19:52:55 +02:00
|
|
|
inline void enforceNotReservedTag( std::string const& tag, SourceLineInfo const& _lineInfo ) {
|
|
|
|
if( isReservedTag( tag ) ) {
|
2017-03-22 17:53:22 +01:00
|
|
|
std::ostringstream ss;
|
|
|
|
ss << Colour(Colour::Red)
|
|
|
|
<< "Tag name [" << tag << "] not allowed.\n"
|
|
|
|
<< "Tag names starting with non alpha-numeric characters are reserved\n"
|
|
|
|
<< Colour(Colour::FileName)
|
|
|
|
<< _lineInfo << '\n';
|
|
|
|
throw std::runtime_error(ss.str());
|
2014-05-16 19:52:55 +02:00
|
|
|
}
|
|
|
|
}
|
2014-04-15 19:44:37 +02:00
|
|
|
|
2012-11-25 12:19:55 +01:00
|
|
|
TestCase makeTestCase( ITestCase* _testCase,
|
2013-04-23 19:58:56 +02:00
|
|
|
std::string const& _className,
|
|
|
|
std::string const& _name,
|
|
|
|
std::string const& _descOrTags,
|
|
|
|
SourceLineInfo const& _lineInfo )
|
2012-09-15 18:53:27 +02:00
|
|
|
{
|
2013-11-19 08:21:03 +01:00
|
|
|
bool isHidden( startsWith( _name, "./" ) ); // Legacy support
|
2014-05-16 19:52:55 +02:00
|
|
|
|
|
|
|
// Parse out tags
|
2012-11-25 12:19:55 +01:00
|
|
|
std::set<std::string> tags;
|
2014-05-16 19:52:55 +02:00
|
|
|
std::string desc, tag;
|
|
|
|
bool inTag = false;
|
|
|
|
for( std::size_t i = 0; i < _descOrTags.size(); ++i ) {
|
|
|
|
char c = _descOrTags[i];
|
|
|
|
if( !inTag ) {
|
|
|
|
if( c == '[' )
|
|
|
|
inTag = true;
|
|
|
|
else
|
|
|
|
desc += c;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if( c == ']' ) {
|
2014-12-15 08:25:34 +01:00
|
|
|
TestCaseInfo::SpecialProperties prop = parseSpecialTag( tag );
|
|
|
|
if( prop == TestCaseInfo::IsHidden )
|
2014-05-19 18:50:58 +02:00
|
|
|
isHidden = true;
|
2014-12-15 08:25:34 +01:00
|
|
|
else if( prop == TestCaseInfo::None )
|
|
|
|
enforceNotReservedTag( tag, _lineInfo );
|
|
|
|
|
|
|
|
tags.insert( tag );
|
2014-05-16 19:52:55 +02:00
|
|
|
tag.clear();
|
2014-12-15 08:25:34 +01:00
|
|
|
inTag = false;
|
2014-04-15 19:44:37 +02:00
|
|
|
}
|
2014-05-16 19:52:55 +02:00
|
|
|
else
|
|
|
|
tag += c;
|
2014-04-15 19:44:37 +02:00
|
|
|
}
|
2013-12-04 08:58:39 +01:00
|
|
|
}
|
2014-05-20 19:11:23 +02:00
|
|
|
if( isHidden ) {
|
|
|
|
tags.insert( "hide" );
|
|
|
|
tags.insert( "." );
|
|
|
|
}
|
2015-11-04 19:01:28 +01:00
|
|
|
|
2014-07-03 09:09:57 +02:00
|
|
|
TestCaseInfo info( _name, _className, desc, tags, _lineInfo );
|
2012-11-25 12:19:55 +01:00
|
|
|
return TestCase( _testCase, info );
|
2012-09-15 18:53:27 +02:00
|
|
|
}
|
2011-01-07 20:57:32 +01:00
|
|
|
|
2015-07-02 09:20:18 +02:00
|
|
|
void setTags( TestCaseInfo& testCaseInfo, std::set<std::string> const& tags )
|
|
|
|
{
|
|
|
|
testCaseInfo.tags = tags;
|
|
|
|
testCaseInfo.lcaseTags.clear();
|
2015-11-04 19:01:28 +01:00
|
|
|
|
2015-07-02 09:20:18 +02:00
|
|
|
std::ostringstream oss;
|
2017-04-25 12:06:52 +02:00
|
|
|
for( auto const& tag : tags ) {
|
|
|
|
oss << '[' << tag << ']';
|
|
|
|
std::string lcaseTag = toLower( tag );
|
2015-07-02 09:20:18 +02:00
|
|
|
testCaseInfo.properties = static_cast<TestCaseInfo::SpecialProperties>( testCaseInfo.properties | parseSpecialTag( lcaseTag ) );
|
|
|
|
testCaseInfo.lcaseTags.insert( lcaseTag );
|
|
|
|
}
|
|
|
|
testCaseInfo.tagsAsString = oss.str();
|
|
|
|
}
|
2015-11-04 19:01:28 +01:00
|
|
|
|
2013-04-23 19:58:56 +02:00
|
|
|
TestCaseInfo::TestCaseInfo( std::string const& _name,
|
|
|
|
std::string const& _className,
|
|
|
|
std::string const& _description,
|
|
|
|
std::set<std::string> const& _tags,
|
|
|
|
SourceLineInfo const& _lineInfo )
|
2012-11-25 12:19:55 +01:00
|
|
|
: name( _name ),
|
|
|
|
className( _className ),
|
|
|
|
description( _description ),
|
2012-12-05 09:40:53 +01:00
|
|
|
lineInfo( _lineInfo ),
|
2014-07-03 09:09:57 +02:00
|
|
|
properties( None )
|
2013-03-28 23:13:31 +01:00
|
|
|
{
|
2015-07-02 09:20:18 +02:00
|
|
|
setTags( *this, _tags );
|
2013-03-28 23:13:31 +01:00
|
|
|
}
|
2012-08-14 09:38:22 +02:00
|
|
|
|
2013-04-23 19:58:56 +02:00
|
|
|
TestCaseInfo::TestCaseInfo( TestCaseInfo const& other )
|
2012-11-25 12:19:55 +01:00
|
|
|
: name( other.name ),
|
|
|
|
className( other.className ),
|
|
|
|
description( other.description ),
|
|
|
|
tags( other.tags ),
|
2014-05-20 19:03:54 +02:00
|
|
|
lcaseTags( other.lcaseTags ),
|
2013-03-28 23:13:31 +01:00
|
|
|
tagsAsString( other.tagsAsString ),
|
2012-12-05 09:40:53 +01:00
|
|
|
lineInfo( other.lineInfo ),
|
2014-07-03 09:09:57 +02:00
|
|
|
properties( other.properties )
|
2012-08-14 20:30:30 +02:00
|
|
|
{}
|
2011-01-07 20:57:32 +01:00
|
|
|
|
2014-07-03 09:09:57 +02:00
|
|
|
bool TestCaseInfo::isHidden() const {
|
|
|
|
return ( properties & IsHidden ) != 0;
|
|
|
|
}
|
|
|
|
bool TestCaseInfo::throws() const {
|
|
|
|
return ( properties & Throws ) != 0;
|
|
|
|
}
|
|
|
|
bool TestCaseInfo::okToFail() const {
|
|
|
|
return ( properties & (ShouldFail | MayFail ) ) != 0;
|
|
|
|
}
|
|
|
|
bool TestCaseInfo::expectedToFail() const {
|
|
|
|
return ( properties & (ShouldFail ) ) != 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-04-23 19:58:56 +02:00
|
|
|
TestCase::TestCase( ITestCase* testCase, TestCaseInfo const& info ) : TestCaseInfo( info ), test( testCase ) {}
|
2012-11-25 12:19:55 +01:00
|
|
|
|
2013-04-23 19:58:56 +02:00
|
|
|
TestCase::TestCase( TestCase const& other )
|
2012-11-25 12:19:55 +01:00
|
|
|
: TestCaseInfo( other ),
|
|
|
|
test( other.test )
|
2012-08-23 09:38:27 +02:00
|
|
|
{}
|
|
|
|
|
2013-04-23 19:58:56 +02:00
|
|
|
TestCase TestCase::withName( std::string const& _newName ) const {
|
2012-11-25 12:19:55 +01:00
|
|
|
TestCase other( *this );
|
|
|
|
other.name = _newName;
|
|
|
|
return other;
|
2012-08-14 20:30:30 +02:00
|
|
|
}
|
2011-01-14 09:47:43 +01:00
|
|
|
|
2014-05-20 19:03:54 +02:00
|
|
|
void TestCase::swap( TestCase& other ) {
|
|
|
|
test.swap( other.test );
|
|
|
|
name.swap( other.name );
|
|
|
|
className.swap( other.className );
|
|
|
|
description.swap( other.description );
|
|
|
|
tags.swap( other.tags );
|
|
|
|
lcaseTags.swap( other.lcaseTags );
|
|
|
|
tagsAsString.swap( other.tagsAsString );
|
2014-07-03 09:09:57 +02:00
|
|
|
std::swap( TestCaseInfo::properties, static_cast<TestCaseInfo&>( other ).properties );
|
2014-05-20 19:03:54 +02:00
|
|
|
std::swap( lineInfo, other.lineInfo );
|
|
|
|
}
|
|
|
|
|
2012-11-25 12:19:55 +01:00
|
|
|
void TestCase::invoke() const {
|
|
|
|
test->invoke();
|
2012-08-14 20:30:30 +02:00
|
|
|
}
|
|
|
|
|
2013-04-23 19:58:56 +02:00
|
|
|
bool TestCase::operator == ( TestCase const& other ) const {
|
2012-11-25 12:19:55 +01:00
|
|
|
return test.get() == other.test.get() &&
|
|
|
|
name == other.name &&
|
|
|
|
className == other.className;
|
2012-08-14 20:30:30 +02:00
|
|
|
}
|
|
|
|
|
2013-04-23 19:58:56 +02:00
|
|
|
bool TestCase::operator < ( TestCase const& other ) const {
|
2012-11-25 12:19:55 +01:00
|
|
|
return name < other.name;
|
2012-08-14 20:30:30 +02:00
|
|
|
}
|
2013-04-23 19:58:56 +02:00
|
|
|
TestCase& TestCase::operator = ( TestCase const& other ) {
|
2012-11-22 20:17:20 +01:00
|
|
|
TestCase temp( other );
|
2012-08-23 09:38:27 +02:00
|
|
|
swap( temp );
|
|
|
|
return *this;
|
|
|
|
}
|
2012-09-21 08:48:03 +02:00
|
|
|
|
2013-04-23 19:58:56 +02:00
|
|
|
TestCaseInfo const& TestCase::getTestCaseInfo() const
|
2012-11-25 12:19:55 +01:00
|
|
|
{
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2012-09-21 08:48:03 +02:00
|
|
|
} // end namespace Catch
|
2011-01-07 20:57:32 +01:00
|
|
|
|
2012-09-17 07:42:29 +02:00
|
|
|
#endif // TWOBLUECUBES_CATCH_TEST_CASE_INFO_HPP_INCLUDED
|