-
-
Notifications
You must be signed in to change notification settings - Fork 18
/
dependency_injection.cpp
93 lines (75 loc) · 1.94 KB
/
dependency_injection.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
#include "hsm/hsm.h"
#include <boost/hana.hpp>
#include <gtest/gtest.h>
#include <future>
#include <memory>
namespace {
// States
struct S1 {
};
struct S2 {
};
struct Exit {
};
// Events
struct e1 {
};
struct writeDepsEvent {
};
struct readDepsEvent {
};
// Guards
constexpr auto guard = [](auto /*event*/, auto /*source*/, auto /*target*/, auto& dependency) {
dependency.callCount++;
return true;
};
// Actions
constexpr auto action = [](auto /*event*/, auto /*source*/, auto /*target*/, auto& dependency) {
dependency.callCount++;
};
using namespace ::testing;
struct MainState {
static constexpr auto make_transition_table()
{
// clang-format off
return hsm::transition_table(
* hsm::state<S1> + hsm::event<e1> [guard] / action = hsm::state<S1>
);
// clang-format on
}
};
}
class DependencyInjectionTests : public Test {
protected:
struct Dependency {
explicit Dependency(int callCount)
: callCount(callCount)
{
}
// Dependency is not copied, assigned, or moved
Dependency(const Dependency&) = delete;
Dependency(Dependency&&) = delete;
Dependency& operator=(const Dependency&) = delete;
Dependency& operator=(Dependency&&) = delete;
int callCount = 0;
};
};
TEST_F(DependencyInjectionTests, should_inject_dependency)
{
Dependency dependency { 0 };
hsm::sm<MainState, Dependency> sm { dependency };
sm.process_event(e1 {});
ASSERT_EQ(2, dependency.callCount);
}
TEST_F(DependencyInjectionTests, should_set_dependency)
{
Dependency dependency { 0 };
Dependency newDependency { 0 };
hsm::sm<MainState, Dependency> sm { dependency };
sm.process_event(e1 {});
ASSERT_EQ(2, dependency.callCount);
sm.set_dependency(newDependency);
sm.process_event(e1 {});
// ASSERT_EQ(2, dependency.callCount);
// ASSERT_EQ(2, newDependency.callCount);
}