2017-07-19 10:13:47 +02:00
|
|
|
/*
|
|
|
|
* Created by Martin on 19/07/2017.
|
|
|
|
*
|
|
|
|
* 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)
|
|
|
|
*/
|
|
|
|
|
2017-09-07 12:24:33 +02:00
|
|
|
#include "catch_test_spec_parser.h"
|
2017-07-19 10:13:47 +02:00
|
|
|
|
2019-10-08 12:55:08 +02:00
|
|
|
|
2017-07-19 10:13:47 +02:00
|
|
|
namespace Catch {
|
|
|
|
|
|
|
|
TestSpecParser::TestSpecParser( ITagAliasRegistry const& tagAliases ) : m_tagAliases( &tagAliases ) {}
|
|
|
|
|
|
|
|
TestSpecParser& TestSpecParser::parse( std::string const& arg ) {
|
|
|
|
m_mode = None;
|
|
|
|
m_exclusion = false;
|
|
|
|
m_arg = m_tagAliases->expandAliases( arg );
|
|
|
|
m_escapeChars.clear();
|
2019-08-06 20:51:19 +02:00
|
|
|
m_substring.reserve(m_arg.size());
|
|
|
|
m_patternName.reserve(m_arg.size());
|
2019-10-08 12:55:08 +02:00
|
|
|
m_realPatternPos = 0;
|
2019-10-19 15:50:46 +02:00
|
|
|
|
2017-07-19 10:13:47 +02:00
|
|
|
for( m_pos = 0; m_pos < m_arg.size(); ++m_pos )
|
2019-10-19 15:50:46 +02:00
|
|
|
//if visitChar fails
|
|
|
|
if( !visitChar( m_arg[m_pos] ) ){
|
|
|
|
m_testSpec.m_invalidArgs.push_back(arg);
|
|
|
|
break;
|
|
|
|
}
|
2019-08-06 20:51:19 +02:00
|
|
|
endMode();
|
2017-07-19 10:13:47 +02:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
TestSpec TestSpecParser::testSpec() {
|
|
|
|
addFilter();
|
|
|
|
return m_testSpec;
|
|
|
|
}
|
2019-10-19 15:50:46 +02:00
|
|
|
bool TestSpecParser::visitChar( char c ) {
|
2019-09-14 11:38:47 +02:00
|
|
|
if( (m_mode != EscapedName) && (c == '\\') ) {
|
|
|
|
escape();
|
2019-10-19 15:50:46 +02:00
|
|
|
addCharToPattern(c);
|
|
|
|
return true;
|
2019-09-14 11:38:47 +02:00
|
|
|
}else if((m_mode != EscapedName) && (c == ',') ) {
|
2019-10-19 15:50:46 +02:00
|
|
|
return separate();
|
2019-08-06 20:51:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
switch( m_mode ) {
|
|
|
|
case None:
|
|
|
|
if( processNoneChar( c ) )
|
2019-10-19 15:50:46 +02:00
|
|
|
return true;
|
2019-08-06 20:51:19 +02:00
|
|
|
break;
|
|
|
|
case Name:
|
|
|
|
processNameChar( c );
|
|
|
|
break;
|
|
|
|
case EscapedName:
|
|
|
|
endMode();
|
2019-10-19 15:50:46 +02:00
|
|
|
addCharToPattern(c);
|
|
|
|
return true;
|
2019-08-06 20:51:19 +02:00
|
|
|
default:
|
|
|
|
case Tag:
|
|
|
|
case QuotedName:
|
|
|
|
if( processOtherChar( c ) )
|
2019-10-19 15:50:46 +02:00
|
|
|
return true;
|
2019-08-06 20:51:19 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
m_substring += c;
|
2019-10-08 12:55:08 +02:00
|
|
|
if( !isControlChar( c ) ) {
|
2019-08-06 20:51:19 +02:00
|
|
|
m_patternName += c;
|
2019-10-08 12:55:08 +02:00
|
|
|
m_realPatternPos++;
|
|
|
|
}
|
2019-10-19 15:50:46 +02:00
|
|
|
return true;
|
2019-08-06 20:51:19 +02:00
|
|
|
}
|
|
|
|
// Two of the processing methods return true to signal the caller to return
|
|
|
|
// without adding the given character to the current pattern strings
|
|
|
|
bool TestSpecParser::processNoneChar( char c ) {
|
|
|
|
switch( c ) {
|
|
|
|
case ' ':
|
|
|
|
return true;
|
|
|
|
case '~':
|
|
|
|
m_exclusion = true;
|
|
|
|
return false;
|
|
|
|
case '[':
|
|
|
|
startNewMode( Tag );
|
|
|
|
return false;
|
|
|
|
case '"':
|
|
|
|
startNewMode( QuotedName );
|
|
|
|
return false;
|
|
|
|
default:
|
|
|
|
startNewMode( Name );
|
|
|
|
return false;
|
2017-07-19 10:13:47 +02:00
|
|
|
}
|
2019-08-06 20:51:19 +02:00
|
|
|
}
|
|
|
|
void TestSpecParser::processNameChar( char c ) {
|
|
|
|
if( c == '[' ) {
|
|
|
|
if( m_substring == "exclude:" )
|
|
|
|
m_exclusion = true;
|
|
|
|
else
|
|
|
|
endMode();
|
|
|
|
startNewMode( Tag );
|
2017-07-19 10:13:47 +02:00
|
|
|
}
|
|
|
|
}
|
2019-08-06 20:51:19 +02:00
|
|
|
bool TestSpecParser::processOtherChar( char c ) {
|
|
|
|
if( !isControlChar( c ) )
|
|
|
|
return false;
|
|
|
|
m_substring += c;
|
|
|
|
endMode();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
void TestSpecParser::startNewMode( Mode mode ) {
|
2017-07-19 10:13:47 +02:00
|
|
|
m_mode = mode;
|
2019-08-06 20:51:19 +02:00
|
|
|
}
|
|
|
|
void TestSpecParser::endMode() {
|
|
|
|
switch( m_mode ) {
|
|
|
|
case Name:
|
|
|
|
case QuotedName:
|
|
|
|
return addPattern<TestSpec::NamePattern>();
|
|
|
|
case Tag:
|
|
|
|
return addPattern<TestSpec::TagPattern>();
|
|
|
|
case EscapedName:
|
2019-09-14 11:38:47 +02:00
|
|
|
revertBackToLastMode();
|
|
|
|
return;
|
2019-08-06 20:51:19 +02:00
|
|
|
case None:
|
|
|
|
default:
|
|
|
|
return startNewMode( None );
|
|
|
|
}
|
2017-07-19 10:13:47 +02:00
|
|
|
}
|
|
|
|
void TestSpecParser::escape() {
|
2019-09-14 11:38:47 +02:00
|
|
|
saveLastMode();
|
2017-07-19 10:13:47 +02:00
|
|
|
m_mode = EscapedName;
|
2019-10-08 12:55:08 +02:00
|
|
|
m_escapeChars.push_back(m_realPatternPos);
|
2017-07-19 10:13:47 +02:00
|
|
|
}
|
2019-08-06 20:51:19 +02:00
|
|
|
bool TestSpecParser::isControlChar( char c ) const {
|
|
|
|
switch( m_mode ) {
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
case None:
|
|
|
|
return c == '~';
|
|
|
|
case Name:
|
|
|
|
return c == '[';
|
|
|
|
case EscapedName:
|
|
|
|
return true;
|
|
|
|
case QuotedName:
|
|
|
|
return c == '"';
|
|
|
|
case Tag:
|
|
|
|
return c == '[' || c == ']';
|
|
|
|
}
|
|
|
|
}
|
2017-07-19 10:13:47 +02:00
|
|
|
|
|
|
|
void TestSpecParser::addFilter() {
|
2019-06-22 20:26:03 +02:00
|
|
|
if( !m_currentFilter.m_required.empty() || !m_currentFilter.m_forbidden.empty() ) {
|
2017-07-19 10:13:47 +02:00
|
|
|
m_testSpec.m_filters.push_back( m_currentFilter );
|
|
|
|
m_currentFilter = TestSpec::Filter();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-14 11:38:47 +02:00
|
|
|
void TestSpecParser::saveLastMode() {
|
|
|
|
lastMode = m_mode;
|
|
|
|
}
|
|
|
|
|
|
|
|
void TestSpecParser::revertBackToLastMode() {
|
|
|
|
m_mode = lastMode;
|
|
|
|
}
|
|
|
|
|
2019-10-19 15:50:46 +02:00
|
|
|
bool TestSpecParser::separate() {
|
|
|
|
if( (m_mode==QuotedName) || (m_mode==Tag) ){
|
|
|
|
//invalid argument, signal failure to previous scope.
|
|
|
|
m_mode = None;
|
|
|
|
m_pos = m_arg.size();
|
|
|
|
m_substring.clear();
|
|
|
|
m_patternName.clear();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
endMode();
|
|
|
|
addFilter();
|
|
|
|
return true; //success
|
|
|
|
}
|
|
|
|
|
2017-07-19 10:13:47 +02:00
|
|
|
TestSpec parseTestSpec( std::string const& arg ) {
|
|
|
|
return TestSpecParser( ITagAliasRegistry::get() ).parse( arg ).testSpec();
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace Catch
|