2013-02-02 20:58:04 +01:00
|
|
|
/*
|
|
|
|
* Created by Phil Nash on 1/2/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)
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "catch_message.h"
|
2017-07-06 22:28:42 +02:00
|
|
|
#include "catch_interfaces_capture.h"
|
2018-01-26 16:59:47 +01:00
|
|
|
#include "catch_uncaught_exceptions.h"
|
2019-05-01 19:12:44 +02:00
|
|
|
#include "catch_enforce.h"
|
2013-02-02 20:58:04 +01:00
|
|
|
|
2018-06-27 22:34:35 +02:00
|
|
|
#include <cassert>
|
2018-11-19 23:06:06 +01:00
|
|
|
#include <stack>
|
2018-06-27 22:34:35 +02:00
|
|
|
|
2013-02-02 20:58:04 +01:00
|
|
|
namespace Catch {
|
2013-07-03 20:14:59 +02:00
|
|
|
|
2018-07-23 13:42:44 +02:00
|
|
|
MessageInfo::MessageInfo( StringRef const& _macroName,
|
2013-02-02 20:58:04 +01:00
|
|
|
SourceLineInfo const& _lineInfo,
|
|
|
|
ResultWas::OfType _type )
|
|
|
|
: macroName( _macroName ),
|
|
|
|
lineInfo( _lineInfo ),
|
|
|
|
type( _type ),
|
|
|
|
sequence( ++globalCount )
|
|
|
|
{}
|
2013-07-03 20:14:59 +02:00
|
|
|
|
2017-07-06 22:28:42 +02:00
|
|
|
bool MessageInfo::operator==( MessageInfo const& other ) const {
|
|
|
|
return sequence == other.sequence;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool MessageInfo::operator<( MessageInfo const& other ) const {
|
|
|
|
return sequence < other.sequence;
|
|
|
|
}
|
|
|
|
|
2013-02-02 20:58:04 +01:00
|
|
|
// This may need protecting if threading support is added
|
|
|
|
unsigned int MessageInfo::globalCount = 0;
|
2013-07-03 20:14:59 +02:00
|
|
|
|
|
|
|
|
2013-02-02 20:58:04 +01:00
|
|
|
////////////////////////////////////////////////////////////////////////////
|
2013-07-03 20:14:59 +02:00
|
|
|
|
2018-07-23 13:42:44 +02:00
|
|
|
Catch::MessageBuilder::MessageBuilder( StringRef const& macroName,
|
2017-07-06 22:28:42 +02:00
|
|
|
SourceLineInfo const& lineInfo,
|
|
|
|
ResultWas::OfType type )
|
|
|
|
:m_info(macroName, lineInfo, type) {}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
|
2013-06-28 18:09:57 +02:00
|
|
|
ScopedMessage::ScopedMessage( MessageBuilder const& builder )
|
2019-02-01 16:11:30 +01:00
|
|
|
: m_info( builder.m_info ), m_moved()
|
2013-06-28 18:09:57 +02:00
|
|
|
{
|
|
|
|
m_info.message = builder.m_stream.str();
|
|
|
|
getResultCapture().pushScopedMessage( m_info );
|
|
|
|
}
|
2014-05-19 19:57:14 +02:00
|
|
|
|
2019-02-01 16:11:30 +01:00
|
|
|
ScopedMessage::ScopedMessage( ScopedMessage&& old )
|
|
|
|
: m_info( old.m_info ), m_moved()
|
|
|
|
{
|
|
|
|
old.m_moved = true;
|
|
|
|
}
|
|
|
|
|
2013-06-28 18:09:57 +02:00
|
|
|
ScopedMessage::~ScopedMessage() {
|
2019-02-01 16:11:30 +01:00
|
|
|
if ( !uncaught_exceptions() && !m_moved ){
|
2017-03-31 00:06:35 +02:00
|
|
|
getResultCapture().popScopedMessage(m_info);
|
|
|
|
}
|
2013-02-02 20:58:04 +01:00
|
|
|
}
|
2018-06-27 22:34:35 +02:00
|
|
|
|
|
|
|
|
|
|
|
Capturer::Capturer( StringRef macroName, SourceLineInfo const& lineInfo, ResultWas::OfType resultType, StringRef names ) {
|
2018-11-19 23:06:06 +01:00
|
|
|
auto trimmed = [&] (size_t start, size_t end) {
|
|
|
|
while (names[start] == ',' || isspace(names[start])) {
|
|
|
|
++start;
|
|
|
|
}
|
|
|
|
while (names[end] == ',' || isspace(names[end])) {
|
|
|
|
--end;
|
|
|
|
}
|
|
|
|
return names.substr(start, end - start + 1);
|
|
|
|
};
|
2019-05-01 19:12:44 +02:00
|
|
|
auto skipq = [&] (size_t start, char quote) {
|
|
|
|
for (auto i = start + 1; i < names.size() ; ++i) {
|
|
|
|
if (names[i] == quote)
|
|
|
|
return i;
|
|
|
|
if (names[i] == '\\')
|
|
|
|
++i;
|
|
|
|
}
|
|
|
|
CATCH_INTERNAL_ERROR("CAPTURE parsing encountered unmatched parentheses");
|
|
|
|
};
|
2018-11-19 23:06:06 +01:00
|
|
|
|
|
|
|
size_t start = 0;
|
|
|
|
std::stack<char> openings;
|
|
|
|
for (size_t pos = 0; pos < names.size(); ++pos) {
|
2018-06-27 22:34:35 +02:00
|
|
|
char c = names[pos];
|
2018-11-19 23:06:06 +01:00
|
|
|
switch (c) {
|
|
|
|
case '[':
|
|
|
|
case '{':
|
|
|
|
case '(':
|
|
|
|
// It is basically impossible to disambiguate between
|
|
|
|
// comparison and start of template args in this context
|
|
|
|
// case '<':
|
|
|
|
openings.push(c);
|
|
|
|
break;
|
|
|
|
case ']':
|
|
|
|
case '}':
|
|
|
|
case ')':
|
|
|
|
// case '>':
|
|
|
|
openings.pop();
|
|
|
|
break;
|
2019-05-01 19:12:44 +02:00
|
|
|
case '"':
|
|
|
|
case '\'':
|
|
|
|
pos = skipq(pos, c);
|
|
|
|
break;
|
2018-11-19 23:06:06 +01:00
|
|
|
case ',':
|
|
|
|
if (start != pos && openings.size() == 0) {
|
|
|
|
m_messages.emplace_back(macroName, lineInfo, resultType);
|
|
|
|
m_messages.back().message = trimmed(start, pos);
|
|
|
|
m_messages.back().message += " := ";
|
|
|
|
start = pos;
|
2018-06-27 22:34:35 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-11-19 23:06:06 +01:00
|
|
|
assert(openings.size() == 0 && "Mismatched openings");
|
|
|
|
m_messages.emplace_back(macroName, lineInfo, resultType);
|
|
|
|
m_messages.back().message = trimmed(start, names.size() - 1);
|
|
|
|
m_messages.back().message += " := ";
|
2018-06-27 22:34:35 +02:00
|
|
|
}
|
|
|
|
Capturer::~Capturer() {
|
|
|
|
if ( !uncaught_exceptions() ){
|
|
|
|
assert( m_captured == m_messages.size() );
|
|
|
|
for( size_t i = 0; i < m_captured; ++i )
|
|
|
|
m_resultCapture.popScopedMessage( m_messages[i] );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-19 23:06:06 +01:00
|
|
|
void Capturer::captureValue( size_t index, std::string const& value ) {
|
2018-06-27 22:34:35 +02:00
|
|
|
assert( index < m_messages.size() );
|
|
|
|
m_messages[index].message += value;
|
|
|
|
m_resultCapture.pushScopedMessage( m_messages[index] );
|
|
|
|
m_captured++;
|
|
|
|
}
|
|
|
|
|
2013-02-02 20:58:04 +01:00
|
|
|
} // end namespace Catch
|