-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_script.cpp
More file actions
509 lines (397 loc) · 16.6 KB
/
Copy pathtest_script.cpp
File metadata and controls
509 lines (397 loc) · 16.6 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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
#include "catch.hpp"
#ifdef CF_HAS_SCRIPTING
#include "../src/script/ScriptEngine.hpp"
#include "../src/script/ScriptSystem.hpp"
#include "../src/script/ScriptTypes.hpp"
#include "../src/ecs/World.hpp"
#include "../src/ecs/Components.hpp"
#include "../src/input/InputManager.hpp"
#include "../src/events/EventBus.hpp"
#include "../src/debug/LogSystem.hpp"
#include <string>
using namespace Caffeine;
using namespace Caffeine::Script;
// ============================================================================
// Helpers
// ============================================================================
struct ScriptTestFixture {
ECS::World world;
Input::InputManager input;
Events::EventBus events;
ScriptEngine engine;
ScriptTestFixture() {
engine.init({&world, &input, &events});
}
ScriptTestFixture(const ScriptTestFixture&) = delete;
ScriptTestFixture& operator=(const ScriptTestFixture&) = delete;
~ScriptTestFixture() {
engine.shutdown();
}
ECS::Entity createEntity(const char* scriptPath) {
auto e = world.create("TestEntity");
ScriptComponent sc;
sc.scriptPath = scriptPath;
e.add<ScriptComponent>(std::move(sc));
return e;
}
};
// ============================================================================
// 1. ScriptEngine init/shutdown
// ============================================================================
TEST_CASE("ScriptEngine init and shutdown", "[script]") {
ECS::World world;
Input::InputManager input;
Events::EventBus events;
ScriptEngine engine;
REQUIRE(engine.init({&world, &input, &events}));
// shutdown in destructor
}
// ============================================================================
// 2. loadString stores script and isLoaded reflects it
// ============================================================================
TEST_CASE("loadString marks script as loaded", "[script]") {
ScriptTestFixture fix;
REQUIRE(fix.engine.loadString("function onCreate(eid) end", "test.lua"));
REQUIRE(fix.engine.isLoaded("test.lua"));
REQUIRE_FALSE(fix.engine.isLoaded("nonexistent.lua"));
}
// ============================================================================
// 3. callOnCreate receives correct entity ID — positions via world binding
// ============================================================================
TEST_CASE("callOnCreate moves entity via world transform binding", "[script]") {
ScriptTestFixture fix;
const char* code = R"(
function onCreate(eid)
caffeine.world.setTransform(eid, {x = 42.5, y = 99.5, rotation = 1.5, scaleX = 2.0, scaleY = 3.0})
end
)";
REQUIRE(fix.engine.loadString(code, "create_test.lua"));
auto entity = fix.world.create("Test");
REQUIRE(fix.engine.callOnCreate("create_test.lua", entity));
auto* pos = entity.get<ECS::Position2D>();
REQUIRE(pos != nullptr);
REQUIRE(pos->x == Approx(42.5f));
REQUIRE(pos->y == Approx(99.5f));
auto* rot = entity.get<ECS::Rotation>();
REQUIRE(rot != nullptr);
REQUIRE(rot->angle == Approx(1.5f));
}
// ============================================================================
// 4. callOnUpdate receives dt
// ============================================================================
TEST_CASE("callOnUpdate receives correct delta time", "[script]") {
ScriptTestFixture fix;
const char* code = R"(
function onCreate(eid) end
function onUpdate(eid, dt)
local t = caffeine.world.getTransform(eid)
caffeine.world.setTransform(eid, {x = t.x + dt, y = t.y + dt})
end
)";
REQUIRE(fix.engine.loadString(code, "update_test.lua"));
auto entity = fix.world.create("Test");
entity.add<ECS::Position2D>(ECS::Position2D{10.0f, 20.0f});
REQUIRE(fix.engine.callOnCreate("update_test.lua", entity));
REQUIRE(fix.engine.callOnUpdate("update_test.lua", entity, 3.0f));
auto* pos = entity.get<ECS::Position2D>();
REQUIRE(pos != nullptr);
REQUIRE(pos->x == Approx(13.0f));
REQUIRE(pos->y == Approx(23.0f));
}
// ============================================================================
// 5. callOnDestroy fires without error
// ============================================================================
TEST_CASE("callOnDestroy fires cleanly", "[script]") {
ScriptTestFixture fix;
const char* code = R"(
function onDestroy(eid)
-- cleanup: reset position to zero
caffeine.world.setTransform(eid, {x = 0, y = 0})
end
)";
REQUIRE(fix.engine.loadString(code, "destroy_test.lua"));
auto entity = fix.world.create("Test");
entity.add<ECS::Position2D>(ECS::Position2D{100.0f, 200.0f});
REQUIRE(fix.engine.callOnDestroy("destroy_test.lua", entity));
auto* pos = entity.get<ECS::Position2D>();
REQUIRE(pos != nullptr);
REQUIRE(pos->x == Approx(0.0f));
REQUIRE(pos->y == Approx(0.0f));
}
// ============================================================================
// 6. ScriptSystem dispatches onCreate and onUpdate
// ============================================================================
TEST_CASE("ScriptSystem calls onCreate on first frame", "[script]") {
ScriptTestFixture fix;
const char* code = R"(
function onCreate(eid)
caffeine.world.setTransform(eid, {x = 77.0, y = 88.0})
end
function onUpdate(eid, dt) end
)";
REQUIRE(fix.engine.loadString(code, "sys_test.lua"));
auto entity = fix.createEntity("sys_test.lua");
ScriptSystem system(&fix.engine);
system.onUpdate(fix.world, 0.0f);
auto* pos = entity.get<ECS::Position2D>();
REQUIRE(pos != nullptr);
REQUIRE(pos->x == Approx(77.0f));
REQUIRE(pos->y == Approx(88.0f));
}
TEST_CASE("ScriptSystem calls onUpdate every frame after onCreate", "[script]") {
ScriptTestFixture fix;
const char* code = R"(
function onCreate(eid)
caffeine.world.setTransform(eid, {x = 0, y = 0})
end
function onUpdate(eid, dt)
local t = caffeine.world.getTransform(eid)
caffeine.world.setTransform(eid, {x = t.x + 1, y = t.y + dt})
end
)";
REQUIRE(fix.engine.loadString(code, "sys_update.lua"));
auto entity = fix.createEntity("sys_update.lua");
ScriptSystem system(&fix.engine);
system.onUpdate(fix.world, 1.0f); // frame 1: onCreate + onUpdate(1.0)
auto* pos = entity.get<ECS::Position2D>();
REQUIRE(pos != nullptr);
REQUIRE(pos->x == Approx(1.0f)); // 0 + 1
REQUIRE(pos->y == Approx(1.0f)); // 0 + 1.0
system.onUpdate(fix.world, 2.0f); // frame 2: onUpdate(2.0) only
REQUIRE(pos->x == Approx(2.0f)); // 1 + 1
REQUIRE(pos->y == Approx(3.0f)); // 1 + 2.0
}
// ============================================================================
// 7. Input binding — isKeyDown
// ============================================================================
TEST_CASE("Input isKeyDown reflects injected key state", "[script]") {
ScriptTestFixture fix;
const char* code = R"(
function onUpdate(eid, dt)
if caffeine.input.isKeyDown("Space") then
caffeine.world.setTransform(eid, {x = 1, y = 0})
else
caffeine.world.setTransform(eid, {x = 0, y = 0})
end
end
)";
REQUIRE(fix.engine.loadString(code, "input_key.lua"));
auto entity = fix.createEntity("input_key.lua");
ScriptSystem system(&fix.engine);
system.onUpdate(fix.world, 0.0f); // frame: key not pressed
auto* pos = entity.get<ECS::Position2D>();
REQUIRE(pos != nullptr);
REQUIRE(pos->x == Approx(0.0f));
fix.input.injectKeyDown(Input::Key::Space);
system.onUpdate(fix.world, 0.0f); // frame: Space pressed
REQUIRE(pos->x == Approx(1.0f));
}
// ============================================================================
// 8. Input binding — getAxis
// ============================================================================
TEST_CASE("Input getAxis returns correct axis value", "[script]") {
ScriptTestFixture fix;
const char* code = R"(
function onUpdate(eid, dt)
local h = caffeine.input.getAxis("Horizontal")
caffeine.world.setTransform(eid, {x = h, y = 0})
end
)";
REQUIRE(fix.engine.loadString(code, "input_axis.lua"));
auto entity = fix.createEntity("input_axis.lua");
// Bind axis: A = negative, D = positive
fix.input.bindAxis(Input::Axis::MoveX,
Input::Binding::fromKey(Input::Key::A),
Input::Binding::fromKey(Input::Key::D));
ScriptSystem system(&fix.engine);
system.onUpdate(fix.world, 0.0f); // frame 1: axis is 0
auto* pos = entity.get<ECS::Position2D>();
REQUIRE(pos != nullptr);
REQUIRE(pos->x == Approx(0.0f));
fix.input.injectKeyDown(Input::Key::D);
system.onUpdate(fix.world, 0.0f); // frame 2: axis is +1
REQUIRE(pos->x == Approx(1.0f));
}
// ============================================================================
// 9. Input binding — mousePosition
// ============================================================================
TEST_CASE("Input mousePosition returns correct coords", "[script]") {
ScriptTestFixture fix;
const char* code = R"(
function onUpdate(eid, dt)
local mx, my = caffeine.input.mousePosition()
caffeine.world.setTransform(eid, {x = mx, y = my})
end
)";
REQUIRE(fix.engine.loadString(code, "input_mouse.lua"));
auto entity = fix.createEntity("input_mouse.lua");
fix.input.injectMouseMove(320.0f, 240.0f);
ScriptSystem system(&fix.engine);
system.onUpdate(fix.world, 0.0f);
auto* pos = entity.get<ECS::Position2D>();
REQUIRE(pos != nullptr);
REQUIRE(pos->x == Approx(320.0f));
REQUIRE(pos->y == Approx(240.0f));
}
// ============================================================================
// 10. Math binding — length and normalize
// ============================================================================
TEST_CASE("Math vec2 length and normalize work", "[script]") {
ScriptTestFixture fix;
const char* code = R"(
function onCreate(eid)
local a = caffeine.math.vec2(3, 4)
local len = caffeine.math.length(a)
local n = caffeine.math.normalize(a)
caffeine.world.setTransform(eid, {x = len, y = n.x})
end
)";
REQUIRE(fix.engine.loadString(code, "math_len.lua"));
auto entity = fix.world.create("Test");
REQUIRE(fix.engine.callOnCreate("math_len.lua", entity));
auto* pos = entity.get<ECS::Position2D>();
REQUIRE(pos != nullptr);
REQUIRE(pos->x == Approx(5.0f)); // length of (3,4)
REQUIRE(pos->y == Approx(3.0f / 5.0f)); // 0.6
}
// ============================================================================
// 11. Math binding — add, sub, dot, scale
// ============================================================================
TEST_CASE("Math vec2 arithmetic operations work", "[script]") {
ScriptTestFixture fix;
const char* code = R"(
function onCreate(eid)
local a = caffeine.math.vec2(10, 20)
local b = caffeine.math.vec2(3, 4)
local sum = caffeine.math.add(a, b)
local diff = caffeine.math.sub(a, b)
local d = caffeine.math.dot(a, b)
local s = caffeine.math.scale(a, 2)
caffeine.world.setTransform(eid, {x = sum.x, y = d})
end
)";
REQUIRE(fix.engine.loadString(code, "math_arith.lua"));
auto entity = fix.world.create("Test");
REQUIRE(fix.engine.callOnCreate("math_arith.lua", entity));
auto* pos = entity.get<ECS::Position2D>();
REQUIRE(pos != nullptr);
REQUIRE(pos->x == Approx(13.0f)); // 10 + 3
REQUIRE(pos->y == Approx(10*3 + 20*4.0f)); // 110
}
// ============================================================================
// 12. Sandbox — dangerous globals are blocked
// ============================================================================
TEST_CASE("Sandbox blocks dofile and load", "[script]") {
ScriptTestFixture fix;
const char* code = R"(
function onCreate(eid)
-- dofile, load, loadfile should be nil
if dofile ~= nil then
caffeine.world.setTransform(eid, {x = 1, y = 0})
end
end
)";
REQUIRE(fix.engine.loadString(code, "sandbox.lua"));
auto entity = fix.world.create("Test");
entity.add<ECS::Position2D>(ECS::Position2D{0.0f, 0.0f});
REQUIRE(fix.engine.callOnCreate("sandbox.lua", entity));
auto* pos = entity.get<ECS::Position2D>();
REQUIRE(pos != nullptr);
REQUIRE(pos->x == Approx(0.0f)); // dofile was nil so transform wasn't set
}
// ============================================================================
// 13. Lua error in lifecycle doesn't crash
// ============================================================================
TEST_CASE("Lua runtime error in callOnCreate doesn't crash", "[script]") {
ScriptTestFixture fix;
const char* code = R"(
function onCreate(eid)
error("intentional crash test")
end
)";
REQUIRE(fix.engine.loadString(code, "crash_test.lua"));
auto entity = fix.world.create("Test");
// Should return false but not crash
REQUIRE_FALSE(fix.engine.callOnCreate("crash_test.lua", entity));
// Engine is still usable after error
REQUIRE(fix.engine.loadString("function test() end", "still_works.lua"));
}
// ============================================================================
// 14. Multiple entities with same script
// ============================================================================
TEST_CASE("Multiple entities with same script each get onCreate", "[script]") {
ScriptTestFixture fix;
const char* code = R"(
counter = 0
function onCreate(eid)
counter = counter + 1
caffeine.world.setTransform(eid, {x = counter, y = 0})
end
function onUpdate(eid, dt) end
)";
REQUIRE(fix.engine.loadString(code, "multi.lua"));
auto e1 = fix.createEntity("multi.lua");
auto e2 = fix.createEntity("multi.lua");
auto e3 = fix.createEntity("multi.lua");
ScriptSystem system(&fix.engine);
system.onUpdate(fix.world, 0.0f);
auto* p1 = e1.get<ECS::Position2D>();
auto* p2 = e2.get<ECS::Position2D>();
auto* p3 = e3.get<ECS::Position2D>();
REQUIRE(p1 != nullptr);
REQUIRE(p2 != nullptr);
REQUIRE(p3 != nullptr);
REQUIRE(p1->x == Approx(1.0f));
REQUIRE(p2->x == Approx(2.0f));
REQUIRE(p3->x == Approx(3.0f));
}
// ============================================================================
// 15. Event binding — on/emit/off (smoke test, observable via components)
// ============================================================================
TEST_CASE("Event on and emit trigger Lua callbacks", "[script]") {
ScriptTestFixture fix;
const char* code = R"(
function onCreate(eid)
caffeine.events.on("move", function(x, y)
caffeine.world.setTransform(eid, {x = x, y = y})
end)
end
function onUpdate(eid, dt)
caffeine.events.emit("move", dt, dt * 2)
end
)";
REQUIRE(fix.engine.loadString(code, "event_test.lua"));
auto entity = fix.createEntity("event_test.lua");
ScriptSystem system(&fix.engine);
system.onUpdate(fix.world, 10.0f); // onCreate + onUpdate -> emit(10, 20)
auto* pos = entity.get<ECS::Position2D>();
REQUIRE(pos != nullptr);
REQUIRE(pos->x == Approx(10.0f));
REQUIRE(pos->y == Approx(20.0f));
}
// ============================================================================
// 16. Velocity bindings
// ============================================================================
TEST_CASE("World getVelocity and setVelocity work", "[script]") {
ScriptTestFixture fix;
const char* code = R"(
function onCreate(eid)
caffeine.world.setVelocity(eid, {x = 5.0, y = 10.0})
local v = caffeine.world.getVelocity(eid)
caffeine.world.setTransform(eid, {x = v.x, y = v.y})
end
)";
REQUIRE(fix.engine.loadString(code, "vel_test.lua"));
auto entity = fix.world.create("Test");
REQUIRE(fix.engine.callOnCreate("vel_test.lua", entity));
auto* pos = entity.get<ECS::Position2D>();
REQUIRE(pos != nullptr);
REQUIRE(pos->x == Approx(5.0f));
REQUIRE(pos->y == Approx(10.0f));
}
#else // !CF_HAS_SCRIPTING
TEST_CASE("Scripting disabled — placeholder", "[script]") {
// This file compiles to nothing when scripting is disabled
REQUIRE(true);
}
#endif // CF_HAS_SCRIPTING