forked from erikzenker/hsm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathentry_exit_actions.cpp
150 lines (129 loc) · 4.18 KB
/
entry_exit_actions.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#include "hsm/hsm.h"
#include <gtest/gtest.h>
#include <future>
namespace {
using namespace ::testing;
// States
struct S1 {
};
struct S2 {
constexpr auto on_entry()
{
return [](auto event, auto /*source*/, auto /*target*/) { event.called->set_value(); };
}
constexpr auto on_exit()
{
return [](auto event, auto /*source*/, auto /*target*/) { event.called->set_value(); };
}
};
// Events
struct e1 {
e1(const std::shared_ptr<std::promise<void>>& called)
: called(called)
{
}
std::shared_ptr<std::promise<void>> called;
};
struct e2 {
e2(const std::shared_ptr<std::promise<void>>& called)
: called(called)
{
}
std::shared_ptr<std::promise<void>> called;
};
struct e3 {
e3(const std::shared_ptr<std::promise<void>>& called)
: called(called)
{
}
std::shared_ptr<std::promise<void>> called;
};
struct SubState {
static constexpr auto make_transition_table()
{
// clang-format off
return hsm::transition_table(
* hsm::state<S1> {} + hsm::event<e1> {} = hsm::state<S1> {}
);
// clang-format on
}
constexpr auto on_entry()
{
return [](auto event, auto /*source*/, auto /*target*/) { event.called->set_value(); };
}
constexpr auto on_exit()
{
return [](auto event, auto /*source*/, auto /*target*/) { event.called->set_value(); };
}
};
struct SubState2 {
static constexpr auto make_transition_table()
{
// clang-format off
return hsm::transition_table(
* hsm::state<S2> {} + hsm::event<e1> {} = hsm::state<S2> {}
);
// clang-format on
}
};
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<S2> {} + hsm::event<e1> {} = hsm::state<S1> {},
hsm::state<S1> {} + hsm::event<e2> {} = hsm::state<SubState> {},
hsm::state<S1> {} + hsm::event<e3> {} = hsm::state<SubState2> {},
hsm::state<SubState> {} + hsm::event<e2> {} = hsm::state<S1> {}
);
// clang-format on
}
};
}
class EntryExitActionsTests : public Test {
protected:
hsm::sm<MainState> sm;
};
TEST_F(EntryExitActionsTests, should_call_entry_and_exit_action)
{
auto entryActionCalled = std::make_shared<std::promise<void>>();
auto exitActionCalled = std::make_shared<std::promise<void>>();
ASSERT_TRUE(sm.is(hsm::state<S1> {}));
sm.process_event(e1 { entryActionCalled });
ASSERT_TRUE(sm.is(hsm::state<S2> {}));
sm.process_event(e1 { exitActionCalled });
ASSERT_TRUE(sm.is(hsm::state<S1> {}));
ASSERT_EQ(
std::future_status::ready,
entryActionCalled->get_future().wait_for(std::chrono::seconds(1)));
ASSERT_EQ(
std::future_status::ready,
exitActionCalled->get_future().wait_for(std::chrono::seconds(1)));
}
TEST_F(EntryExitActionsTests, should_call_entry_and_exit_action_of_substate)
{
auto entryActionCalled = std::make_shared<std::promise<void>>();
auto exitActionCalled = std::make_shared<std::promise<void>>();
ASSERT_TRUE(sm.is(hsm::state<S1> {}));
sm.process_event(e2{ entryActionCalled });
ASSERT_TRUE(sm.is(hsm::state<SubState> {}, hsm::state<S1> {}));
sm.process_event(e2 { exitActionCalled });
ASSERT_TRUE(sm.is(hsm::state<S1> {}));
ASSERT_EQ(
std::future_status::ready,
entryActionCalled->get_future().wait_for(std::chrono::seconds(1)));
ASSERT_EQ(
std::future_status::ready,
exitActionCalled->get_future().wait_for(std::chrono::seconds(1)));
}
// Entry action of sub state initial state is not called see github #104
TEST_F(EntryExitActionsTests, should_call_entry_action_of_substate_initial_state)
{
auto entryActionCalled = std::make_shared<std::promise<void>>();
sm.process_event(e3 { entryActionCalled });
ASSERT_TRUE(sm.is(hsm::state<SubState2> {}, hsm::state<S2> {}));
ASSERT_EQ(
std::future_status::ready,
entryActionCalled->get_future().wait_for(std::chrono::seconds(1)));
}