-
-
Notifications
You must be signed in to change notification settings - Fork 18
/
unexpected_event_handler.cpp
80 lines (63 loc) · 1.57 KB
/
unexpected_event_handler.cpp
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
#include "hsm/hsm.h"
#include <boost/hana.hpp>
#include <gtest/gtest.h>
#include <future>
#include <memory>
namespace {
// States
struct S1 {
};
struct S2 {
};
struct S3 {
};
// Events
struct e1 {
bool called = false;
};
struct e2 {
bool called = false;
};
struct e3 {
bool called = false;
};
// Guards
constexpr auto alwaysFalse = [](auto...) { return false; };
using namespace ::testing;
using namespace boost::hana;
struct MainState {
static constexpr auto make_transition_table()
{
// clang-format off
return hsm::transition_table(
* hsm::state<S1> + hsm::event<e1> = hsm::state<S2>
, hsm::state<S1> + hsm::event<e3> = hsm::state<S3>
, hsm::state<S2> + hsm::event<e2> = hsm::state<S1>
, hsm::state<S3> + hsm::event<e2> [alwaysFalse]= hsm::state<S1>
);
// clang-format on
}
static constexpr auto on_unexpected_event()
{
return [](auto& event, auto /*currentState*/) { event.called = true; };
}
};
}
class UnexpectedEventHandler : public Test {
protected:
hsm::sm<MainState> sm;
hsm::sm<MainState> sm2;
};
TEST_F(UnexpectedEventHandler, should_call_unexpected_event_handler)
{
auto event = e2 {};
ASSERT_FALSE(sm.process_event(event));
ASSERT_TRUE(event.called);
}
TEST_F(UnexpectedEventHandler, should_not_call_unexpected_event_handler_when_guard_fails)
{
ASSERT_TRUE(sm.process_event(e3 {}));
auto event = e2 {};
ASSERT_FALSE(sm.process_event(event));
ASSERT_FALSE(event.called);
}