catch2/include/internal/catch_test_case_info.hpp

183 lines
5.8 KiB
C++
Raw Normal View History

/*
2012-08-14 20:30:30 +02:00
* Created by Phil on 14/08/2012.
* Copyright 2012 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_TEST_CASE_INFO_HPP_INCLUDED
#define TWOBLUECUBES_CATCH_TEST_CASE_INFO_HPP_INCLUDED
#include "catch_test_spec.hpp"
2012-08-14 20:30:30 +02:00
#include "catch_test_case_info.h"
#include "catch_interfaces_testcase.h"
2013-03-22 20:00:42 +01:00
#include "catch_common.h"
2012-05-16 00:58:23 +02:00
namespace Catch {
inline bool isSpecialTag( std::string const& tag ) {
return tag == "." ||
tag == "hide" ||
tag == "!hide" ||
tag == "!throws";
}
inline bool isReservedTag( std::string const& tag ) {
return !isSpecialTag( tag ) && tag.size() > 0 && !isalnum( tag[0] );
}
inline void enforceNotReservedTag( std::string const& tag, SourceLineInfo const& _lineInfo ) {
if( isReservedTag( tag ) ) {
{
Colour colourGuard( Colour::Red );
std::cerr
<< "Tag name [" << tag << "] not allowed.\n"
<< "Tag names starting with non alpha-numeric characters are reserved\n";
}
{
Colour colourGuard( Colour::FileName );
std::cerr << _lineInfo << std::endl;
}
exit(1);
}
}
TestCase makeTestCase( ITestCase* _testCase,
std::string const& _className,
std::string const& _name,
std::string const& _descOrTags,
SourceLineInfo const& _lineInfo )
2012-09-15 18:53:27 +02:00
{
bool isHidden( startsWith( _name, "./" ) ); // Legacy support
// Parse out tags
std::set<std::string> tags;
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 == ']' ) {
enforceNotReservedTag( tag, _lineInfo );
inTag = false;
if( tag == "hide" || tag == "." )
2014-05-19 18:50:58 +02:00
isHidden = true;
else
tags.insert( tag );
tag.clear();
}
else
tag += c;
}
2013-12-04 08:58:39 +01:00
}
if( isHidden ) {
tags.insert( "hide" );
tags.insert( "." );
}
TestCaseInfo info( _name, _className, desc, tags, isHidden, _lineInfo );
return TestCase( _testCase, info );
2012-09-15 18:53:27 +02:00
}
TestCaseInfo::TestCaseInfo( std::string const& _name,
std::string const& _className,
std::string const& _description,
std::set<std::string> const& _tags,
bool _isHidden,
SourceLineInfo const& _lineInfo )
: name( _name ),
className( _className ),
description( _description ),
tags( _tags ),
lineInfo( _lineInfo ),
isHidden( _isHidden ),
throws( false )
{
std::ostringstream oss;
for( std::set<std::string>::const_iterator it = _tags.begin(), itEnd = _tags.end(); it != itEnd; ++it ) {
oss << "[" << *it << "]";
if( *it == "!throws" )
throws = true;
lcaseTags.insert( toLower( *it ) );
}
tagsAsString = oss.str();
}
2012-08-14 09:38:22 +02:00
TestCaseInfo::TestCaseInfo( TestCaseInfo const& other )
: name( other.name ),
className( other.className ),
description( other.description ),
tags( other.tags ),
lcaseTags( other.lcaseTags ),
tagsAsString( other.tagsAsString ),
lineInfo( other.lineInfo ),
isHidden( other.isHidden ),
throws( other.throws )
2012-08-14 20:30:30 +02:00
{}
TestCase::TestCase( ITestCase* testCase, TestCaseInfo const& info ) : TestCaseInfo( info ), test( testCase ) {}
TestCase::TestCase( TestCase const& other )
: TestCaseInfo( other ),
test( other.test )
{}
TestCase TestCase::withName( std::string const& _newName ) const {
TestCase other( *this );
other.name = _newName;
return other;
2012-08-14 20:30:30 +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 );
std::swap( TestCaseInfo::isHidden, static_cast<TestCaseInfo&>( other ).isHidden );
std::swap( TestCaseInfo::throws, static_cast<TestCaseInfo&>( other ).throws );
std::swap( lineInfo, other.lineInfo );
}
void TestCase::invoke() const {
test->invoke();
2012-08-14 20:30:30 +02:00
}
2012-11-22 20:17:20 +01:00
bool TestCase::isHidden() const {
return TestCaseInfo::isHidden;
2012-08-14 20:30:30 +02:00
}
bool TestCase::throws() const {
return TestCaseInfo::throws;
}
2012-08-14 20:30:30 +02:00
bool TestCase::operator == ( TestCase const& other ) const {
return test.get() == other.test.get() &&
name == other.name &&
className == other.className;
2012-08-14 20:30:30 +02:00
}
bool TestCase::operator < ( TestCase const& other ) const {
return name < other.name;
2012-08-14 20:30:30 +02:00
}
TestCase& TestCase::operator = ( TestCase const& other ) {
2012-11-22 20:17:20 +01:00
TestCase temp( other );
swap( temp );
return *this;
}
TestCaseInfo const& TestCase::getTestCaseInfo() const
{
return *this;
}
} // end namespace Catch
#endif // TWOBLUECUBES_CATCH_TEST_CASE_INFO_HPP_INCLUDED