ifw-daq  2.1.0-pre1
IFW Data Acquisition modules
testEventLog.cpp
Go to the documentation of this file.
1 /**
2  * @file
3  * @ingroup daq_ocm_libdaq_test
4  * @copyright 2022 ESO - European Southern Observatory
5  *
6  * @brief Unit test for types declared in daq/event.hpp
7  */
8 #include <daq/eventLog.hpp>
9 #include <sstream>
10 
11 #include <gtest/gtest.h>
12 #include <gmock/gmock.h>
13 
14 using namespace ::testing;
15 using namespace ::daq;
16 
17 struct EventObserver {
18  MOCK_METHOD1(CallOperator, void(daq::EventLog::EventType const&));
19  void operator()(daq::EventLog::EventType const& event) {
20  CallOperator(event);
21  }
22 };
23 
24 TEST(TestEventLog, Constructors) {
25  EXPECT_NO_THROW(EventLog());
26 }
27 
28 TEST(TestObservableEventLog, Constructors) {
29  EXPECT_NO_THROW(ObservableEventLog());
30 }
31 
32 TEST(TestObservableEventLog, ConvertibleToEventLog) {
33  auto observable_events = ObservableEventLog();
34  auto events1 = static_cast<EventLog const&>(observable_events);
35  auto events2 = observable_events.GetEventLog();
36 
37  EXPECT_EQ(events1.events, events2.events);
38 }
39 
40 TEST(TestObservableEventLog, ObserversAreInvokedWhenEventIsAdded) {
41  // Setup
42  auto event = ErrorEvent("id", "test error", {}, "origin");
43  auto mock_observer = EventObserver();
44  EXPECT_CALL(mock_observer, CallOperator(EventLog::EventType(event)));
45 
46  // Run
47  auto events = ObservableEventLog();
48  events.ConnectObserver(std::reference_wrapper(mock_observer));
49  events.AddEvent(event);
50 }
51 
52 TEST(TestObservableEventLog, EventLogAreAddedToContainer) {
53  // Setup
54  auto event = ErrorEvent("id", "test error", {}, "origin");
55 
56  // Run
57  auto events = ObservableEventLog();
58  EXPECT_EQ(events.GetEventContainer().size(), std::size_t(0));
59  events.AddEvent(event);
60  EXPECT_EQ(events.GetEventContainer().size(), std::size_t(1));
61  EXPECT_EQ(events.GetEventContainer()[0], EventLog::EventType(event));
62 }
63 
64 TEST(TestErrorEvent, Comparison) {
65  // Setup
66  auto event1 = ErrorEvent("id1", "test error", {}, "origin");
67  auto event2 = ErrorEvent("id2", "test error", {}, "origin");
68 
69  // Run
70  EXPECT_EQ(event1, event1);
71  EXPECT_NE(event1, event2);
72 }
73 
74 TEST(TestGenericEvent, Comparison) {
75  // Setup
76  auto event1 = GenericEvent("id1", "test error", {});
77  auto event2 = GenericEvent("id2", "test error", {});
78 
79  // Run
80  EXPECT_EQ(event1, event1);
81  EXPECT_NE(event1, event2);
82 }
83 
84 TEST(TestError, Ostream) {
85  // Setup
86  auto event1 = ErrorEvent("id1", "test error", {}, "origin");
87  auto event2 = GenericEvent("id2", "test error", {});
88  std::stringstream ss;
89 
90  EXPECT_NO_THROW(
91  ss << event1 << event2
92  );
93 }
EventObserver::MOCK_METHOD1
MOCK_METHOD1(CallOperator, void(daq::EventLog::EventType const &))
daq
Definition: asyncProcess.cpp:15
EventObserver
Definition: testEventLog.cpp:17
eventLog.hpp
Contains declaration for EventLog, ObservableEventLog and related events.
daq::EventLog::EventType
std::variant< ActionEvent, UserActionEvent, GenericEvent, ErrorEvent > EventType
Definition: eventLog.hpp:90
EventObserver::operator()
void operator()(daq::EventLog::EventType const &event)
Definition: testEventLog.cpp:19
TEST
TEST(TestEventLog, Constructors)
Definition: testEventLog.cpp:24