Files
Catch2/src/catch2/catch_message.cpp
T

116 lines
3.9 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_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 ):
2025-08-26 12:36:06 +02:00
m_messageId( builder.m_info.sequence ) {
MessageInfo info( CATCH_MOVE( builder.m_info ) );
info.message = builder.m_stream.str();
getResultCapture().pushScopedMessage( CATCH_MOVE(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:
2025-08-26 12:36:06 +02:00
m_messageId( old.m_messageId ) {
2019-02-01 09:11:30 -06:00
old.m_moved = true;
}
2013-06-28 17:09:57 +01:00
ScopedMessage::~ScopedMessage() {
2025-08-26 12:36:06 +02:00
if ( !m_moved ) { getResultCapture().popScopedMessage( m_messageId ); }
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);
2025-08-26 12:40:13 +02:00
m_messages.back().message += trimmed(start, pos);
m_messages.back().message += " := "_sr;
2018-11-19 23:06:06 +01:00
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);
2025-08-26 12:40:13 +02:00
m_messages.back().message += trimmed(start, names.size() - 1);
m_messages.back().message += " := "_sr;
2018-06-27 21:34:35 +01:00
}
Capturer::~Capturer() {
assert( m_captured == m_messages.size() );
for (auto const& message : m_messages) {
m_resultCapture.popScopedMessage( message.sequence );
}
2018-06-27 21:34:35 +01:00
}
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;
2025-08-26 12:36:06 +02:00
m_resultCapture.pushScopedMessage( CATCH_MOVE(m_messages[index]) );
2018-06-27 21:34:35 +01:00
m_captured++;
}
2013-02-02 19:58:04 +00:00
} // end namespace Catch