mirror of
https://github.com/catchorg/Catch2.git
synced 2024-12-23 03:43:28 +01:00
Refactored last usages of old tag parser and removed all the, now redundant, tag parsing code
This commit is contained in:
parent
766491a7c4
commit
9bf43e7875
@ -25,7 +25,6 @@
|
||||
#include "catch_assertionresult.hpp"
|
||||
#include "catch_expressionresult_builder.hpp"
|
||||
#include "catch_test_case_info.hpp"
|
||||
#include "catch_tags.hpp"
|
||||
#include "catch_test_spec.hpp"
|
||||
#include "catch_version.hpp"
|
||||
#include "catch_message.hpp"
|
||||
|
@ -1,109 +0,0 @@
|
||||
/*
|
||||
* Created by Phil on 2/12/2013.
|
||||
* Copyright 2013 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_TAGS_H_INCLUDED
|
||||
#define TWOBLUECUBES_CATCH_TAGS_H_INCLUDED
|
||||
|
||||
#include "catch_common.h"
|
||||
|
||||
#include <string>
|
||||
#include <set>
|
||||
#include <map>
|
||||
#include <vector>
|
||||
|
||||
#ifdef __clang__
|
||||
#pragma clang diagnostic ignored "-Wpadded"
|
||||
#endif
|
||||
|
||||
namespace Catch {
|
||||
class TagParser {
|
||||
public:
|
||||
virtual ~TagParser();
|
||||
|
||||
void parse( std::string const& str );
|
||||
|
||||
protected:
|
||||
virtual void acceptTag( std::string const& tag ) = 0;
|
||||
virtual void acceptChar( char c ) = 0;
|
||||
virtual void endParse() {}
|
||||
|
||||
private:
|
||||
};
|
||||
|
||||
class TagExtracter : public TagParser {
|
||||
public:
|
||||
|
||||
TagExtracter( std::set<std::string>& tags );
|
||||
virtual ~TagExtracter();
|
||||
|
||||
void parse( std::string& description );
|
||||
|
||||
private:
|
||||
virtual void acceptTag( std::string const& tag );
|
||||
virtual void acceptChar( char c );
|
||||
|
||||
TagExtracter& operator=(TagExtracter const&);
|
||||
|
||||
std::set<std::string>& m_tags;
|
||||
std::string m_remainder;
|
||||
};
|
||||
|
||||
class Tag {
|
||||
public:
|
||||
Tag();
|
||||
Tag( std::string const& name, bool isNegated );
|
||||
std::string getName() const;
|
||||
bool isNegated() const;
|
||||
bool operator ! () const;
|
||||
|
||||
private:
|
||||
std::string m_name;
|
||||
bool m_isNegated;
|
||||
};
|
||||
|
||||
class TagSet {
|
||||
typedef std::map<std::string, Tag> TagMap;
|
||||
public:
|
||||
void add( Tag const& tag );
|
||||
bool empty() const;
|
||||
bool matches( std::set<std::string> const& tags ) const;
|
||||
|
||||
private:
|
||||
TagMap m_tags;
|
||||
};
|
||||
|
||||
|
||||
class TagExpression {
|
||||
public:
|
||||
bool matches( std::set<std::string> const& tags ) const;
|
||||
|
||||
private:
|
||||
friend class TagExpressionParser;
|
||||
|
||||
std::vector<TagSet> m_tagSets;
|
||||
};
|
||||
|
||||
class TagExpressionParser : public TagParser {
|
||||
public:
|
||||
TagExpressionParser( TagExpression& exp );
|
||||
~TagExpressionParser();
|
||||
|
||||
private:
|
||||
virtual void acceptTag( std::string const& tag );
|
||||
virtual void acceptChar( char c );
|
||||
virtual void endParse();
|
||||
|
||||
TagExpressionParser& operator=(TagExpressionParser const&);
|
||||
|
||||
bool m_isNegated;
|
||||
TagSet m_currentTagSet;
|
||||
TagExpression& m_exp;
|
||||
};
|
||||
|
||||
} // end namespace Catch
|
||||
|
||||
#endif // TWOBLUECUBES_CATCH_TAGS_HPP_INCLUDED
|
@ -1,137 +0,0 @@
|
||||
/*
|
||||
* 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_TAGS_HPP_INCLUDED
|
||||
#define TWOBLUECUBES_CATCH_TAGS_HPP_INCLUDED
|
||||
|
||||
#include "catch_tags.h"
|
||||
|
||||
namespace Catch {
|
||||
TagParser::~TagParser() {}
|
||||
|
||||
void TagParser::parse( std::string const& str ) {
|
||||
std::size_t pos = 0;
|
||||
while( pos < str.size() ) {
|
||||
char c = str[pos];
|
||||
if( c == '[' ) {
|
||||
std::size_t end = str.find_first_of( ']', pos );
|
||||
if( end != std::string::npos ) {
|
||||
acceptTag( str.substr( pos+1, end-pos-1 ) );
|
||||
pos = end+1;
|
||||
}
|
||||
else {
|
||||
acceptChar( c );
|
||||
pos++;
|
||||
}
|
||||
}
|
||||
else {
|
||||
acceptChar( c );
|
||||
pos++;
|
||||
}
|
||||
}
|
||||
endParse();
|
||||
}
|
||||
|
||||
TagExtracter::TagExtracter( std::set<std::string>& tags )
|
||||
: m_tags( tags )
|
||||
{}
|
||||
|
||||
TagExtracter::~TagExtracter() {}
|
||||
|
||||
void TagExtracter::parse( std::string& description ) {
|
||||
TagParser::parse( description );
|
||||
description = m_remainder;
|
||||
}
|
||||
|
||||
void TagExtracter::acceptTag( std::string const& tag ) {
|
||||
m_tags.insert( toLower( tag ) );
|
||||
}
|
||||
void TagExtracter::acceptChar( char c ) {
|
||||
m_remainder += c;
|
||||
}
|
||||
|
||||
Tag::Tag() : m_isNegated( false ) {}
|
||||
Tag::Tag( std::string const& name, bool isNegated )
|
||||
: m_name( name ),
|
||||
m_isNegated( isNegated )
|
||||
{}
|
||||
|
||||
std::string Tag::getName() const {
|
||||
return m_name;
|
||||
}
|
||||
bool Tag::isNegated() const {
|
||||
return m_isNegated;
|
||||
}
|
||||
|
||||
bool Tag::operator ! () const {
|
||||
return m_name.empty();
|
||||
}
|
||||
|
||||
|
||||
void TagSet::add( Tag const& tag ) {
|
||||
m_tags.insert( std::make_pair( toLower( tag.getName() ), tag ) );
|
||||
}
|
||||
|
||||
bool TagSet::empty() const {
|
||||
return m_tags.empty();
|
||||
}
|
||||
|
||||
bool TagSet::matches( std::set<std::string> const& tags ) const {
|
||||
for( TagMap::const_iterator
|
||||
it = m_tags.begin(), itEnd = m_tags.end();
|
||||
it != itEnd;
|
||||
++it ) {
|
||||
bool found = tags.find( it->first ) != tags.end();
|
||||
if( found == it->second.isNegated() )
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
bool TagExpression::matches( std::set<std::string> const& tags ) const {
|
||||
for( std::vector<TagSet>::const_iterator
|
||||
it = m_tagSets.begin(), itEnd = m_tagSets.end();
|
||||
it != itEnd;
|
||||
++it )
|
||||
if( it->matches( tags ) )
|
||||
return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
TagExpressionParser::TagExpressionParser( TagExpression& exp )
|
||||
: m_isNegated( false ),
|
||||
m_exp( exp )
|
||||
{}
|
||||
|
||||
TagExpressionParser::~TagExpressionParser() {}
|
||||
|
||||
void TagExpressionParser::acceptTag( std::string const& tag ) {
|
||||
m_currentTagSet.add( Tag( tag, m_isNegated ) );
|
||||
m_isNegated = false;
|
||||
}
|
||||
|
||||
void TagExpressionParser::acceptChar( char c ) {
|
||||
switch( c ) {
|
||||
case '~':
|
||||
m_isNegated = true;
|
||||
break;
|
||||
case ',':
|
||||
m_exp.m_tagSets.push_back( m_currentTagSet );
|
||||
m_currentTagSet = TagSet();
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void TagExpressionParser::endParse() {
|
||||
if( !m_currentTagSet.empty() )
|
||||
m_exp.m_tagSets.push_back( m_currentTagSet );
|
||||
}
|
||||
|
||||
} // end namespace Catch
|
||||
|
||||
#endif // TWOBLUECUBES_CATCH_TAGS_HPP_INCLUDED
|
@ -57,9 +57,6 @@ namespace Catch {
|
||||
|
||||
bool isHidden() const;
|
||||
bool throws() const;
|
||||
bool hasTag( std::string const& tag ) const;
|
||||
bool matchesTags( std::string const& tagPattern ) const;
|
||||
std::set<std::string> const& getTags() const;
|
||||
|
||||
void swap( TestCase& other );
|
||||
bool operator == ( TestCase const& other ) const;
|
||||
|
@ -11,7 +11,6 @@
|
||||
#include "catch_test_spec.hpp"
|
||||
#include "catch_test_case_info.h"
|
||||
#include "catch_interfaces_testcase.h"
|
||||
#include "catch_tags.h"
|
||||
#include "catch_common.h"
|
||||
|
||||
namespace Catch {
|
||||
@ -25,6 +24,21 @@ namespace Catch {
|
||||
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,
|
||||
@ -32,33 +46,37 @@ namespace Catch {
|
||||
std::string const& _descOrTags,
|
||||
SourceLineInfo const& _lineInfo )
|
||||
{
|
||||
std::string desc = _descOrTags;
|
||||
bool isHidden( startsWith( _name, "./" ) ); // Legacy support
|
||||
|
||||
// Parse out tags
|
||||
std::set<std::string> tags;
|
||||
TagExtracter( tags ).parse( desc );
|
||||
for( std::set<std::string>::const_iterator it = tags.begin(), itEnd = tags.end();
|
||||
it != itEnd;
|
||||
++it )
|
||||
if( isReservedTag( *it ) ) {
|
||||
{
|
||||
Colour colourGuard( Colour::Red );
|
||||
std::cerr
|
||||
<< "Tag name [" << *it << "] 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);
|
||||
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 );
|
||||
|
||||
if( tags.find( "hide" ) != tags.end() || tags.find( "." ) != tags.end() )
|
||||
isHidden = true;
|
||||
|
||||
if( isHidden ) {
|
||||
tags.insert( "hide" );
|
||||
tags.insert( "." );
|
||||
inTag = false;
|
||||
if( tag == "hide" || tag == "." ) {
|
||||
tags.insert( "hide" );
|
||||
tags.insert( "." );
|
||||
}
|
||||
else {
|
||||
tags.insert( tag );
|
||||
}
|
||||
tag.clear();
|
||||
}
|
||||
else
|
||||
tag += c;
|
||||
}
|
||||
}
|
||||
TestCaseInfo info( _name, _className, desc, tags, isHidden, _lineInfo );
|
||||
return TestCase( _testCase, info );
|
||||
@ -122,18 +140,6 @@ namespace Catch {
|
||||
return TestCaseInfo::throws;
|
||||
}
|
||||
|
||||
bool TestCase::hasTag( std::string const& tag ) const {
|
||||
return tags.find( toLower( tag ) ) != tags.end();
|
||||
}
|
||||
bool TestCase::matchesTags( std::string const& tagPattern ) const {
|
||||
TagExpression exp;
|
||||
TagExpressionParser( exp ).parse( tagPattern );
|
||||
return exp.matches( tags );
|
||||
}
|
||||
std::set<std::string> const& TestCase::getTags() const {
|
||||
return tags;
|
||||
}
|
||||
|
||||
void TestCase::swap( TestCase& other ) {
|
||||
test.swap( other.test );
|
||||
className.swap( other.className );
|
||||
|
@ -1,2 +0,0 @@
|
||||
// This file is only here to verify (to the extent possible) the self sufficiency of the header
|
||||
#include "catch_tags.h"
|
@ -8,7 +8,6 @@
|
||||
|
||||
/* Begin PBXBuildFile section */
|
||||
2656C2211925E7330040DB02 /* catch_test_spec.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 2656C2201925E7330040DB02 /* catch_test_spec.cpp */; };
|
||||
2656C2251925EC870040DB02 /* catch_tags.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 2656C2241925EC870040DB02 /* catch_tags.cpp */; };
|
||||
266B06B816F3A60A004ED264 /* VariadicMacrosTests.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 266B06B616F3A60A004ED264 /* VariadicMacrosTests.cpp */; };
|
||||
266ECD74170F3C620030D735 /* BDDTests.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 266ECD73170F3C620030D735 /* BDDTests.cpp */; };
|
||||
26847E5F16BBADB40043B9C1 /* catch_message.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 26847E5D16BBADB40043B9C1 /* catch_message.cpp */; };
|
||||
@ -68,9 +67,6 @@
|
||||
263FD06117AF8DF200988A20 /* catch_timer.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = catch_timer.h; sourceTree = "<group>"; };
|
||||
2656C21F1925E5100040DB02 /* catch_test_spec_parser.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; path = catch_test_spec_parser.hpp; sourceTree = "<group>"; };
|
||||
2656C2201925E7330040DB02 /* catch_test_spec.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = catch_test_spec.cpp; path = ../../../SelfTest/SurrogateCpps/catch_test_spec.cpp; sourceTree = "<group>"; };
|
||||
2656C2221925EC6F0040DB02 /* catch_tags.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = catch_tags.h; sourceTree = "<group>"; };
|
||||
2656C2231925EC6F0040DB02 /* catch_tags.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; path = catch_tags.hpp; sourceTree = "<group>"; };
|
||||
2656C2241925EC870040DB02 /* catch_tags.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = catch_tags.cpp; path = ../../../SelfTest/SurrogateCpps/catch_tags.cpp; sourceTree = "<group>"; };
|
||||
266B06B616F3A60A004ED264 /* VariadicMacrosTests.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = VariadicMacrosTests.cpp; path = ../../../SelfTest/VariadicMacrosTests.cpp; sourceTree = "<group>"; };
|
||||
266ECD73170F3C620030D735 /* BDDTests.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = BDDTests.cpp; path = ../../../SelfTest/BDDTests.cpp; sourceTree = "<group>"; };
|
||||
266ECD8C1713614B0030D735 /* catch_legacy_reporter_adapter.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; path = catch_legacy_reporter_adapter.hpp; sourceTree = "<group>"; };
|
||||
@ -304,7 +300,6 @@
|
||||
4A8E4DCF160A34E200194CBD /* SurrogateCpps */ = {
|
||||
isa = PBXGroup;
|
||||
children = (
|
||||
2656C2241925EC870040DB02 /* catch_tags.cpp */,
|
||||
2656C2201925E7330040DB02 /* catch_test_spec.cpp */,
|
||||
4AEE031F16142F910071E950 /* catch_common.cpp */,
|
||||
4AEE032216142FC70071E950 /* catch_debugger.cpp */,
|
||||
@ -387,8 +382,6 @@
|
||||
isa = PBXGroup;
|
||||
children = (
|
||||
261488FA184C81130041FBEB /* catch_test_spec.hpp */,
|
||||
2656C2221925EC6F0040DB02 /* catch_tags.h */,
|
||||
2656C2231925EC6F0040DB02 /* catch_tags.hpp */,
|
||||
2656C21F1925E5100040DB02 /* catch_test_spec_parser.hpp */,
|
||||
4A6D0C4A149B3E3D00DB3EAA /* catch_config.hpp */,
|
||||
4A6D0C51149B3E3D00DB3EAA /* catch_context.h */,
|
||||
@ -521,7 +514,6 @@
|
||||
4A6D0C3F149B3D9E00DB3EAA /* TrickyTests.cpp in Sources */,
|
||||
4AEE032016142F910071E950 /* catch_common.cpp in Sources */,
|
||||
4AEE032316142FC70071E950 /* catch_debugger.cpp in Sources */,
|
||||
2656C2251925EC870040DB02 /* catch_tags.cpp in Sources */,
|
||||
4AEE032516142FF10071E950 /* catch_stream.cpp in Sources */,
|
||||
4AEE0328161434FD0071E950 /* catch_xmlwriter.cpp in Sources */,
|
||||
4A45DA2416161EF9004F8D6B /* catch_console_colour.cpp in Sources */,
|
||||
|
Loading…
Reference in New Issue
Block a user