Files
Catch2/src/catch2/catch_message.cpp
T

119 lines
4.0 KiB
C++
Raw Normal View History

// Copyright Catch2 Authors
// Distributed under the Boost Software License, Version 1.0.
2022-10-28 11:22:53 +02:00
// (See accompanying file LICENSE.txt or copy at
// https://www.boost.org/LICENSE_1_0.txt)
// SPDX-License-Identifier: BSL-1.0
2020-03-30 10:34:21 +02:00
#include <catch2/catch_message.hpp>
#include <catch2/interfaces/catch_interfaces_capture.hpp>
#include <catch2/internal/catch_uncaught_exceptions.hpp>
#include <catch2/internal/catch_enforce.hpp>
#include <catch2/internal/catch_move_and_forward.hpp>
2013-02-02 19:58:04 +00:00
2018-06-27 21:34:35 +01:00
#include <cassert>
2018-11-19 23:06:06 +01:00
#include <stack>
2018-06-27 21:34:35 +01:00
2013-02-02 19:58:04 +00:00
namespace Catch {
2013-07-03 19:14:59 +01:00
2013-02-02 19:58:04 +00:00
////////////////////////////////////////////////////////////////////////////
2013-07-03 19:14:59 +01:00
2023-01-28 22:01:34 +01:00
ScopedMessage::ScopedMessage( MessageBuilder&& builder ):
m_info( CATCH_MOVE(builder.m_info) ) {
2013-06-28 17:09:57 +01:00
m_info.message = builder.m_stream.str();
getResultCapture().pushScopedMessage( m_info );
2013-02-02 19:58:04 +00:00
}
2014-05-19 18:57:14 +01:00
2020-08-19 17:44:22 +02:00
ScopedMessage::ScopedMessage( ScopedMessage&& old ) noexcept:
m_info( CATCH_MOVE( old.m_info ) ) {
2019-02-01 09:11:30 -06:00
old.m_moved = true;
}
2013-06-28 17:09:57 +01:00
ScopedMessage::~ScopedMessage() {
2019-02-01 09:11:30 -06:00
if ( !uncaught_exceptions() && !m_moved ){
getResultCapture().popScopedMessage(m_info);
}
2013-02-02 19:58:04 +00:00
}
2018-06-27 21:34:35 +01:00
Capturer::Capturer( StringRef macroName,
SourceLineInfo const& lineInfo,
ResultWas::OfType resultType,
StringRef names ):
m_resultCapture( getResultCapture() ) {
2018-11-19 23:06:06 +01:00
auto trimmed = [&] (size_t start, size_t end) {
while (names[start] == ',' || isspace(static_cast<unsigned char>(names[start]))) {
2018-11-19 23:06:06 +01:00
++start;
}
while (names[end] == ',' || isspace(static_cast<unsigned char>(names[end]))) {
2018-11-19 23:06:06 +01:00
--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 quote");
2019-05-01 19:12:44 +02:00
};
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 21:34:35 +01: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 ',':
2020-01-21 21:04:42 +01:00
if (start != pos && openings.empty()) {
2018-11-19 23:06:06 +01:00
m_messages.emplace_back(macroName, lineInfo, resultType);
m_messages.back().message = static_cast<std::string>(trimmed(start, pos));
2018-11-19 23:06:06 +01:00
m_messages.back().message += " := ";
start = pos;
2018-06-27 21:34:35 +01:00
}
break;
2024-03-01 11:15:27 +01:00
default:; // noop
2018-06-27 21:34:35 +01:00
}
}
2020-01-21 21:04:42 +01:00
assert(openings.empty() && "Mismatched openings");
2018-11-19 23:06:06 +01:00
m_messages.emplace_back(macroName, lineInfo, resultType);
m_messages.back().message = static_cast<std::string>(trimmed(start, names.size() - 1));
2018-11-19 23:06:06 +01:00
m_messages.back().message += " := ";
2018-06-27 21:34:35 +01: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 21:34:35 +01:00
assert( index < m_messages.size() );
m_messages[index].message += value;
m_resultCapture.pushScopedMessage( m_messages[index] );
m_captured++;
}
2013-02-02 19:58:04 +00:00
} // end namespace Catch