-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathundo_gtest_annotation.h
86 lines (72 loc) · 2.56 KB
/
undo_gtest_annotation.h
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
#ifndef UNDO_GTEST_ANNOTATION_H_
#define UNDO_GTEST_ANNOTATION_H_
#include <stdio.h>
#include <undoex-test-annotations.h>
#include <mutex>
#include <thread>
#include <unordered_map>
namespace undo_annotation
{
class UndoAnnotationListener : public ::testing::EmptyTestEventListener
{
private:
/** Per-thread instances of the annotation state */
std::unordered_map<std::thread::id, undoex_test_annotation_t *> m_thread_state;
/** A lock protecting access to m_thread_state */
std::mutex m_thread_state_lock;
public:
virtual void OnTestStart(const testing::TestInfo &testInfo)
{
// Create an instance of an annotation context, and associate it
// with this thread.
undoex_test_annotation_t *annotation = undoex_test_annotation_new(testInfo.name(), true);
undoex_test_annotation_start(annotation);
const std::thread::id thread_id = std::this_thread::get_id();
std::lock_guard<std::mutex> lock(m_thread_state_lock);
if (m_thread_state.count(thread_id))
{
// We don't expect there to be an existing entry here.
// Handle it silently out of politeness.
auto old = m_thread_state[thread_id];
if (old)
{
undoex_test_annotation_free(old);
}
}
m_thread_state[thread_id] = annotation;
}
virtual void OnTestEnd(const testing::TestInfo &testInfo)
{
const std::thread::id thread_id = std::this_thread::get_id();
undoex_test_annotation_t *annotation;
{
std::lock_guard<std::mutex> lock(m_thread_state_lock);
if (!m_thread_state.count(thread_id))
{
// We don't expect this to happen, but swallow
// the error silently out of politeness.
return;
}
annotation = m_thread_state.at(thread_id);
m_thread_state.erase(thread_id);
}
undoex_test_result_t test_result = undoex_test_result_UNKNOWN;
if (testInfo.result()->Failed())
{
test_result = undoex_test_result_FAILURE;
}
else if (testInfo.result()->Skipped())
{
test_result = undoex_test_result_SKIPPED;
}
else if (testInfo.result()->Passed())
{
test_result = undoex_test_result_SUCCESS;
}
undoex_test_annotation_end(annotation);
undoex_test_annotation_set_result(annotation, test_result);
undoex_test_annotation_free(annotation);
}
};
} // namespace undo_annotation
#endif