HART  0.2.0
High level Audio Regression and Testing
Loading...
Searching...
No Matches
hart_assertion_dispatcher.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <ostream>
4#include <sstream>
5#include <string>
6#include <utility>
7
11#include "hart_utils.hpp"
12
13namespace hart
14{
15
16/// @brief Communicates the assertion's result to the test runner
17/// @details Meant to be instantiated via @FreeStandingAssertions
18/// macros, like `HART_EXPECT_EQUAL()`. Also, supplies the "<<"
19/// interface for appending extra label, for example:
20/// @code
21/// HART_EXPECT_EQUAL (a, b) << "Some label " << someValue;
22/// @endcode
23/// @private
24class AssertionDispatcher
25{
26public:
27 explicit AssertionDispatcher (Assertion assertion) :
28 m_assertion (std::move (assertion))
29 {
30 if (! m_assertion.hasFailed())
31 return;
32
33 m_messageStream
34 << m_assertion.getFile() << ':' << m_assertion.getLine() << '\n'
35 << (m_assertion.getLevel() == Assertion::Level::expect ? "HART_EXPECT()" : "HART_ASSERT()")
36 << " failed: \nCondition: ";
37 m_assertion.representWithTokens (m_messageStream);
38 m_messageStream << "\nEvaluated: ";
39 m_assertion.representWithStringRepresentations (m_messageStream);
40 }
41
42 AssertionDispatcher (const AssertionDispatcher&) = delete;
43 AssertionDispatcher (AssertionDispatcher&&) = delete;
44 AssertionDispatcher& operator= (const AssertionDispatcher&) = delete;
45 AssertionDispatcher& operator= (AssertionDispatcher&&) = delete;
46
47 template <typename Type>
48 AssertionDispatcher& operator<< (const Type& value)
49 {
50 if (m_assertion.hasFailed())
51 {
52 if (! m_hasUserLabel)
53 {
54 m_hasUserLabel = true;
55 m_messageStream << "\nLabel: \"";
56 }
57
58 m_messageStream << value;
59 }
60
61 return *this;
62 }
63
64 ~AssertionDispatcher() noexcept(false)
65 {
66 if (! m_assertion.hasFailed())
67 return;
68
69 if (m_hasUserLabel)
70 m_messageStream << '\"';
71
72 const std::string message = m_messageStream.str();
73
74 if (! isExceptionUnwinding() && m_assertion.getLevel() == Assertion::Level::assert)
75 throw hart::TestAssertException (message);
76
77 ExpectationFailureMessages::get().emplace_back (message);
78 }
79
80private:
81 const Assertion m_assertion;
82 bool m_hasUserLabel = false;
83 std::ostringstream m_messageStream;
84};
85
86} // namespace hart
Thrown by test asserts like HART_ASSERT_TRUE() and AudioTestBuilder::assertFalse()
static bool isExceptionUnwinding()
Returns true if an exception is currently being unwound.