-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFeaturesTests.cpp
More file actions
336 lines (295 loc) 路 9.25 KB
/
Copy pathFeaturesTests.cpp
File metadata and controls
336 lines (295 loc) 路 9.25 KB
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
/// @file
/// @brief Unit tests for the feature framework (Feature base class + Features::Execute).
// Unit tests for the feature framework (Feature base class + Features::Execute).
//
// The concrete, game-specific features live behind the UE SDK and cannot be
// exercised off the game; instead we register fake features against the same
// Feature interface and drive them through Features::Execute().
#define NOMINMAX
#include <gtest/gtest.h>
#include <chrono>
#include <memory>
#include <stdexcept>
#include <string>
#include "FeatureRunner.h"
namespace
{
/// A Feature stand-in that records how the framework drove it.
struct FakeFeature : public Feature
{
int initCount = 0;
int checkCount = 0;
int runCount = 0;
int destroyCount = 0;
bool checkResult = true;
bool throwOnRun = false;
bool legacyCheck = false; // emulate the old contract where Check() returns Enabled
explicit FakeFeature(std::string name = "Fake") { Name = name; }
void Init() override
{
initCount++;
Initialized = true;
}
void UpdateEnabled() override {}
bool Check() override
{
checkCount++;
return legacyCheck ? Enabled : checkResult;
}
void Destroy() override { destroyCount++; }
void Run() override
{
runCount++;
if (throwOnRun) throw std::runtime_error("boom");
}
};
/// A feature that overrides the event-only Run(event) overload, to test the runner forwards the
/// triggering event and the base delegates the (event, payload) call down to it.
struct EventFake : public Feature
{
int runs = 0;
Events::Type lastEvent = Events::Type::Render;
EventFake() { Name = "EventFake"; }
void Init() override { Initialized = true; }
void UpdateEnabled() override {}
bool Check() override { return true; }
void Destroy() override {}
void Run(Events::Type event) override
{
runs++;
lastEvent = event;
}
};
/// A feature that overrides the full Run(event, payload) overload, to test both are forwarded.
struct PayloadFake : public Feature
{
int runs = 0;
Events::Type lastEvent = Events::Type::Render;
float lastValue = -1.f;
std::string lastName;
PayloadFake() { Name = "PayloadFake"; }
void Init() override { Initialized = true; }
void UpdateEnabled() override {}
bool Check() override { return true; }
void Destroy() override {}
void Run(Events::Type event, const Events::Payload& payload) override
{
runs++;
lastEvent = event;
lastValue = payload.value;
lastName = payload.name ? payload.name : "";
}
};
/// Registers a new FakeFeature in the global registry and returns a non-owning pointer to it.
FakeFeature* add(std::string name = "Fake")
{
auto feature = std::make_unique<FakeFeature>(name);
FakeFeature* raw = feature.get();
Features::Features.push_back(std::move(feature));
return raw;
}
/// Fixture that clears the feature registry and event bus around every test.
class FeaturesTest : public ::testing::Test
{
protected:
void SetUp() override
{
Features::Features.clear();
Events::Clear();
}
void TearDown() override
{
Features::Features.clear();
Events::Clear();
}
};
TEST_F(FeaturesTest, InitializesUninitializedFeatures)
{
FakeFeature* f = add();
ASSERT_FALSE(f->Initialized);
Features::Execute();
EXPECT_EQ(1, f->initCount);
EXPECT_TRUE(f->Initialized);
}
TEST_F(FeaturesTest, DoesNotReinitializeInitializedFeatures)
{
FakeFeature* f = add();
Features::Execute();
Features::Execute();
EXPECT_EQ(1, f->initCount);
}
TEST_F(FeaturesTest, SkipsRunAndDestroyWhenCheckFails)
{
FakeFeature* f = add();
f->checkResult = false;
f->Enabled = true;
Features::Execute();
EXPECT_EQ(1, f->checkCount);
EXPECT_EQ(0, f->runCount);
EXPECT_EQ(0, f->destroyCount);
}
TEST_F(FeaturesTest, RunsWhenEnabledAndCheckPasses)
{
FakeFeature* f = add();
f->Enabled = true;
Features::Execute();
EXPECT_EQ(1, f->runCount);
EXPECT_EQ(0, f->destroyCount);
}
TEST_F(FeaturesTest, DoesNotDestroyWhenNeverApplied)
{
FakeFeature* f = add();
f->Enabled = false; // disabled from the start, so nothing was ever applied
Features::Execute();
Features::Execute();
EXPECT_EQ(0, f->runCount);
EXPECT_EQ(0, f->destroyCount);
}
TEST_F(FeaturesTest, SkipsValidityCheckForIdleDisabledFeatures)
{
FakeFeature* f = add();
f->Enabled = false; // disabled, nothing applied
Features::Execute();
Features::Execute();
EXPECT_EQ(0, f->checkCount); // Check() (the validity walk) is skipped
EXPECT_EQ(1, f->initCount); // but it is still initialized once
}
TEST_F(FeaturesTest, DestroysOnceOnDisableTransition)
{
FakeFeature* f = add();
f->Enabled = true;
Features::Execute(); // applied
EXPECT_EQ(1, f->runCount);
f->Enabled = false;
Features::Execute(); // enabled -> disabled edge: revert once
Features::Execute(); // still disabled: no repeated Destroy
Features::Execute();
EXPECT_EQ(1, f->destroyCount);
}
TEST_F(FeaturesTest, OneTimeRunsOncePerEnable)
{
FakeFeature* f = add();
f->OneTime = true;
f->Enabled = true;
Features::Execute();
Features::Execute();
Features::Execute();
EXPECT_EQ(1, f->runCount);
}
TEST_F(FeaturesTest, OneTimeReArmsAfterDisable)
{
FakeFeature* f = add();
f->OneTime = true;
f->Enabled = true;
Features::Execute(); // run #1
f->Enabled = false;
Features::Execute(); // disable re-arms
f->Enabled = true;
Features::Execute(); // run #2
EXPECT_EQ(2, f->runCount);
}
// A legacy feature whose Check() returns Enabled keeps its old behavior: when
// disabled, Check() returns false, the loop skips it, and Destroy() is never
// reached. This guards the migration path for features still on the old contract.
TEST_F(FeaturesTest, LegacyCheckReturningEnabledNeverDestroys)
{
FakeFeature* f = add();
f->legacyCheck = true;
f->Enabled = true;
Features::Execute(); // Check() true, Run
f->Enabled = false;
Features::Execute(); // Check() returns Enabled == false -> skipped
Features::Execute();
EXPECT_EQ(1, f->runCount);
EXPECT_EQ(0, f->destroyCount);
}
TEST_F(FeaturesTest, ProcessesEveryRegisteredFeature)
{
FakeFeature* a = add("A");
FakeFeature* b = add("B");
a->Enabled = true;
b->Enabled = true;
Features::Execute();
EXPECT_EQ(1, a->runCount);
EXPECT_EQ(1, b->runCount);
}
// Execute only guards against a thrown char* (the codebase's error convention);
// its logging path also requires the game's Logger, which a standalone test
// process can't initialize. So we document the boundary instead: any other
// exception type propagates out of Execute. If the catch is ever broadened,
// this test is the deliberate place to revisit.
TEST_F(FeaturesTest, PropagatesNonCharPointerExceptions)
{
FakeFeature* f = add();
f->Enabled = true;
f->throwOnRun = true;
EXPECT_THROW(Features::Execute(), std::runtime_error);
}
// Execute() only drives render features; event-driven ones run from the bus.
TEST_F(FeaturesTest, ExecuteSkipsEventDrivenFeatures)
{
FakeFeature* f = add();
f->Triggers = {Events::Type::Shutdown};
f->Enabled = true;
Features::Execute();
EXPECT_EQ(0, f->runCount);
}
// An event feature runs when its event is dispatched (this is how Features::Init
// wires non-render features onto the bus).
TEST_F(FeaturesTest, EventFeatureRunsWhenDispatched)
{
FakeFeature f;
f.Triggers = {Events::Type::Shutdown};
f.Enabled = true;
Events::Register(Events::Type::Shutdown, [&]
{ Features::RunFeature(f); });
Events::Dispatch(Events::Type::Render);
EXPECT_EQ(0, f.runCount); // wrong event
Events::Dispatch(Events::Type::Shutdown);
EXPECT_EQ(1, f.runCount); // fires on its event
}
// A feature can list several triggers; Execute() drives it as long as Render is one of them.
TEST_F(FeaturesTest, ExecuteRunsWhenRenderIsAmongTriggers)
{
FakeFeature* f = add();
f->Triggers = {Events::Type::Shutdown, Events::Type::Render};
f->Enabled = true;
Features::Execute();
EXPECT_EQ(1, f->runCount);
}
// ThrottleMs caps how often Run() fires: a second immediate tick is skipped, and it runs again
// once the interval has elapsed (simulated by back-dating lastRun rather than sleeping).
TEST_F(FeaturesTest, ThrottleGatesRepeatedRuns)
{
FakeFeature* f = add();
f->Enabled = true;
f->ThrottleMs = 10000;
Features::Execute(); // first run stamps lastRun
Features::Execute(); // within the interval -> throttled
EXPECT_EQ(1, f->runCount);
f->lastRun = std::chrono::steady_clock::now() - std::chrono::milliseconds(20000); // interval elapsed
Features::Execute();
EXPECT_EQ(2, f->runCount);
}
// The runner forwards the triggering event to a feature that overrides Run(event) (the base
// delegates its Run(event, payload) down to it).
TEST_F(FeaturesTest, ForwardsEventToRunOverload)
{
EventFake f;
f.Enabled = true;
Features::RunFeature(f, Events::Type::PlayerDeath);
EXPECT_EQ(1, f.runs);
EXPECT_EQ(Events::Type::PlayerDeath, f.lastEvent);
}
// The runner forwards both the event and the payload to a feature that overrides the full overload.
TEST_F(FeaturesTest, ForwardsEventAndPayloadToRunOverload)
{
PayloadFake f;
f.Enabled = true;
Features::RunFeature(f, Events::Type::HotKeyPressed, Events::Payload{.value = 3.f, .name = "hello"});
EXPECT_EQ(1, f.runs);
EXPECT_EQ(Events::Type::HotKeyPressed, f.lastEvent);
EXPECT_EQ(3.f, f.lastValue);
EXPECT_EQ("hello", f.lastName);
}
} // namespace