-
Notifications
You must be signed in to change notification settings - Fork 56
Expand file tree
/
Copy pathgmocktestfixture.h
More file actions
99 lines (84 loc) · 2.17 KB
/
Copy pathgmocktestfixture.h
File metadata and controls
99 lines (84 loc) · 2.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
// Copyright (c) 2026 The Bitcoin Core developers
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
#ifndef BITCOIN_QML_TEST_GMOCKTESTFIXTURE_H
#define BITCOIN_QML_TEST_GMOCKTESTFIXTURE_H
#include <QObject>
#include <QtTest/QtTest>
#ifdef Assert
#pragma push_macro("Assert")
#undef Assert
#define BITCOIN_QML_RESTORE_ASSERT_MACRO
#endif
#include <gmock/gmock.h>
#ifdef BITCOIN_QML_RESTORE_ASSERT_MACRO
#pragma pop_macro("Assert")
#undef BITCOIN_QML_RESTORE_ASSERT_MACRO
#endif
#include <mutex>
#include <string>
#include <vector>
class GmockFailureLog
{
public:
void add(std::string msg)
{
std::lock_guard<std::mutex> lock(m_mutex);
m_failures.push_back(std::move(msg));
}
std::vector<std::string> drain()
{
std::lock_guard<std::mutex> lock(m_mutex);
std::vector<std::string> out;
out.swap(m_failures);
return out;
}
private:
std::mutex m_mutex;
std::vector<std::string> m_failures;
};
inline GmockFailureLog& gmockFailureLog()
{
static GmockFailureLog log;
return log;
}
class QtestGmockListener : public testing::EmptyTestEventListener
{
public:
void OnTestPartResult(const testing::TestPartResult& result) override
{
if (result.failed()) {
std::string msg;
if (result.file_name()) {
msg += result.file_name();
msg += ":";
msg += std::to_string(result.line_number());
msg += "\n";
}
msg += result.message();
gmockFailureLog().add(std::move(msg));
}
}
};
class GmockTestFixture : public QObject
{
Q_OBJECT
private Q_SLOTS:
void init()
{
gmockFailureLog().drain();
}
void cleanup()
{
auto failures = gmockFailureLog().drain();
if (!failures.empty()) {
std::string combined{"GMock failure(s):\n"};
for (const auto& f : failures) {
combined += f;
combined += "\n";
}
QFAIL(combined.c_str());
}
}
};
#endif // BITCOIN_QML_TEST_GMOCKTESTFIXTURE_H