2014-05-16 08:23:31 +02:00
|
|
|
/*
|
|
|
|
* Created by Phil on 15/5/2013.
|
|
|
|
* Copyright 2014 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_SPEC_PARSER_HPP_INCLUDED
|
|
|
|
#define TWOBLUECUBES_CATCH_TEST_SPEC_PARSER_HPP_INCLUDED
|
|
|
|
|
2014-05-16 19:24:07 +02:00
|
|
|
#ifdef __clang__
|
|
|
|
#pragma clang diagnostic push
|
|
|
|
#pragma clang diagnostic ignored "-Wpadded"
|
|
|
|
#endif
|
|
|
|
|
2014-05-16 19:28:58 +02:00
|
|
|
#include "catch_test_spec.hpp"
|
2014-06-30 08:33:17 +02:00
|
|
|
#include "catch_interfaces_tag_alias_registry.h"
|
2014-05-16 08:23:31 +02:00
|
|
|
|
|
|
|
namespace Catch {
|
|
|
|
|
|
|
|
class TestSpecParser {
|
2017-01-11 17:24:00 +01:00
|
|
|
enum Mode{ None, Name, QuotedName, Tag, EscapedName };
|
2014-05-16 08:23:31 +02:00
|
|
|
Mode m_mode;
|
|
|
|
bool m_exclusion;
|
|
|
|
std::size_t m_start, m_pos;
|
|
|
|
std::string m_arg;
|
2017-01-11 17:24:00 +01:00
|
|
|
std::vector<std::size_t> m_escapeChars;
|
2014-05-16 08:23:31 +02:00
|
|
|
TestSpec::Filter m_currentFilter;
|
2014-05-16 19:24:07 +02:00
|
|
|
TestSpec m_testSpec;
|
2014-07-03 09:09:57 +02:00
|
|
|
ITagAliasRegistry const* m_tagAliases;
|
2014-05-16 08:23:31 +02:00
|
|
|
|
|
|
|
public:
|
2014-07-03 09:09:57 +02:00
|
|
|
TestSpecParser( ITagAliasRegistry const& tagAliases ) : m_tagAliases( &tagAliases ) {}
|
2014-06-30 08:33:17 +02:00
|
|
|
|
|
|
|
TestSpecParser& parse( std::string const& arg ) {
|
2014-05-16 19:24:07 +02:00
|
|
|
m_mode = None;
|
|
|
|
m_exclusion = false;
|
|
|
|
m_start = std::string::npos;
|
2014-07-03 09:09:57 +02:00
|
|
|
m_arg = m_tagAliases->expandAliases( arg );
|
2017-01-11 17:24:00 +01:00
|
|
|
m_escapeChars.clear();
|
2014-05-16 08:23:31 +02:00
|
|
|
for( m_pos = 0; m_pos < m_arg.size(); ++m_pos )
|
|
|
|
visitChar( m_arg[m_pos] );
|
2014-05-16 19:24:07 +02:00
|
|
|
if( m_mode == Name )
|
|
|
|
addPattern<TestSpec::NamePattern>();
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
TestSpec testSpec() {
|
2014-05-20 19:28:19 +02:00
|
|
|
addFilter();
|
2014-05-16 19:24:07 +02:00
|
|
|
return m_testSpec;
|
2014-05-16 08:23:31 +02:00
|
|
|
}
|
|
|
|
private:
|
|
|
|
void visitChar( char c ) {
|
|
|
|
if( m_mode == None ) {
|
|
|
|
switch( c ) {
|
|
|
|
case ' ': return;
|
|
|
|
case '~': m_exclusion = true; return;
|
|
|
|
case '[': return startNewMode( Tag, ++m_pos );
|
|
|
|
case '"': return startNewMode( QuotedName, ++m_pos );
|
2017-01-11 17:24:00 +01:00
|
|
|
case '\\': return escape();
|
2014-05-16 08:23:31 +02:00
|
|
|
default: startNewMode( Name, m_pos ); break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if( m_mode == Name ) {
|
|
|
|
if( c == ',' ) {
|
|
|
|
addPattern<TestSpec::NamePattern>();
|
|
|
|
addFilter();
|
|
|
|
}
|
|
|
|
else if( c == '[' ) {
|
|
|
|
if( subString() == "exclude:" )
|
|
|
|
m_exclusion = true;
|
|
|
|
else
|
|
|
|
addPattern<TestSpec::NamePattern>();
|
|
|
|
startNewMode( Tag, ++m_pos );
|
|
|
|
}
|
2017-01-11 17:24:00 +01:00
|
|
|
else if( c == '\\' )
|
|
|
|
escape();
|
2014-05-16 08:23:31 +02:00
|
|
|
}
|
2017-01-11 17:24:00 +01:00
|
|
|
else if( m_mode == EscapedName )
|
|
|
|
m_mode = Name;
|
2014-05-16 08:23:31 +02:00
|
|
|
else if( m_mode == QuotedName && c == '"' )
|
|
|
|
addPattern<TestSpec::NamePattern>();
|
|
|
|
else if( m_mode == Tag && c == ']' )
|
|
|
|
addPattern<TestSpec::TagPattern>();
|
|
|
|
}
|
|
|
|
void startNewMode( Mode mode, std::size_t start ) {
|
|
|
|
m_mode = mode;
|
|
|
|
m_start = start;
|
|
|
|
}
|
2017-01-11 17:24:00 +01:00
|
|
|
void escape() {
|
2017-02-01 15:13:10 +01:00
|
|
|
if( m_mode == None )
|
|
|
|
m_start = m_pos;
|
2017-01-11 17:24:00 +01:00
|
|
|
m_mode = EscapedName;
|
|
|
|
m_escapeChars.push_back( m_pos );
|
|
|
|
}
|
2014-05-16 08:23:31 +02:00
|
|
|
std::string subString() const { return m_arg.substr( m_start, m_pos - m_start ); }
|
|
|
|
template<typename T>
|
|
|
|
void addPattern() {
|
|
|
|
std::string token = subString();
|
2017-01-11 17:24:00 +01:00
|
|
|
for( size_t i = 0; i < m_escapeChars.size(); ++i )
|
2017-02-22 08:49:38 +01:00
|
|
|
token = token.substr( 0, m_escapeChars[i]-m_start-i ) + token.substr( m_escapeChars[i]-m_start-i+1 );
|
2017-01-11 17:24:00 +01:00
|
|
|
m_escapeChars.clear();
|
2014-05-16 08:23:31 +02:00
|
|
|
if( startsWith( token, "exclude:" ) ) {
|
|
|
|
m_exclusion = true;
|
|
|
|
token = token.substr( 8 );
|
|
|
|
}
|
|
|
|
if( !token.empty() ) {
|
|
|
|
Ptr<TestSpec::Pattern> pattern = new T( token );
|
|
|
|
if( m_exclusion )
|
|
|
|
pattern = new TestSpec::ExcludedPattern( pattern );
|
|
|
|
m_currentFilter.m_patterns.push_back( pattern );
|
|
|
|
}
|
|
|
|
m_exclusion = false;
|
|
|
|
m_mode = None;
|
|
|
|
}
|
|
|
|
void addFilter() {
|
|
|
|
if( !m_currentFilter.m_patterns.empty() ) {
|
2014-05-16 19:24:07 +02:00
|
|
|
m_testSpec.m_filters.push_back( m_currentFilter );
|
2014-05-16 08:23:31 +02:00
|
|
|
m_currentFilter = TestSpec::Filter();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
inline TestSpec parseTestSpec( std::string const& arg ) {
|
2014-06-30 08:33:17 +02:00
|
|
|
return TestSpecParser( ITagAliasRegistry::get() ).parse( arg ).testSpec();
|
2014-05-16 08:23:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace Catch
|
|
|
|
|
2014-05-16 19:24:07 +02:00
|
|
|
#ifdef __clang__
|
|
|
|
#pragma clang diagnostic pop
|
|
|
|
#endif
|
|
|
|
|
2014-05-16 08:23:31 +02:00
|
|
|
#endif // TWOBLUECUBES_CATCH_TEST_SPEC_PARSER_HPP_INCLUDED
|