High-level wrapper around Flecs, a powerful ECS (Entity Component System) library, written in Zig language
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

107 lines
3.0 KiB

// Reimplementations of the following tests from Flecs:
// https://github.com/SanderMertens/flecs/blob/master/test/api/src/World.c
const std = @import("std");
const alloc = std.testing.allocator;
const expect = @import("../src/expect.zig");
const util = @import("./util.zig");
const flecs = @import("../src/main.zig");
const c = flecs.c;
const context = flecs.Context(void);
const World = context.World;
const Iter = context.Iter;
const Entity = context.Entity;
const Position = struct { x: f32, y: f32 };
const Velocity = struct { x: f32, y: f32 };
fn move(it: Iter) void {
var pos = it.field(Position, 1);
var vel = it.field(Velocity, 2);
util.Probe.probeIter(it) catch unreachable;
for (pos, vel) |*p, *v| {
p.x += v.x * it.getDeltaTime();
p.y += v.y * it.getDeltaTime();
}
}
fn lookup(comptime T: type) c.ecs_entity_t {
return flecs.Lookup(void, T).id;
}
test "World_progress_w_0" {
flecs.init(std.testing.allocator);
var world = try World.init();
defer world.deinit();
_ = try world.component(Position);
_ = try world.component(Velocity);
const e1 = try world.entity(.{}, .{ Position, Velocity });
const move_system = try world.system("move", move, c.EcsOnUpdate, "Position, Velocity");
var ctx = util.Probe.init();
c.ecs_set_context(world.raw, &ctx);
e1.set(Position, .{ .x = 0, .y = 0 });
e1.set(Velocity, .{ .x = 1, .y = 2 });
_ = world.progress(0);
try expect.equal(1, ctx.count);
try expect.equal(1, ctx.invoked);
try expect.equal(move_system.raw, ctx.system);
try expect.equal(2, ctx.termCount);
try expect.equal(null, ctx.param);
try expect.equal(e1.raw, ctx.e[0]);
try expect.equal(lookup(Position), ctx.c[0][0]);
try expect.equal(lookup(Velocity), ctx.c[0][1]);
try expect.equal(0, ctx.s[0][0]);
try expect.equal(0, ctx.s[0][1]);
const p = e1.get(Position).?;
try expect.true(p.x != 0);
try expect.true(p.y != 0);
}
test "World_progress_w_t" {
flecs.init(std.testing.allocator);
var world = try World.init();
defer world.deinit();
_ = try world.component(Position);
_ = try world.component(Velocity);
const e1 = try world.entity(.{}, .{ Position, Velocity });
const move_system = try world.system("move", move, c.EcsOnUpdate, "Position, Velocity");
var ctx = util.Probe.init();
c.ecs_set_context(world.raw, &ctx);
e1.set(Position, .{ .x = 0, .y = 0 });
e1.set(Velocity, .{ .x = 1, .y = 2 });
_ = world.progress(2);
try expect.equal(1, ctx.count);
try expect.equal(1, ctx.invoked);
try expect.equal(move_system.raw, ctx.system);
try expect.equal(2, ctx.termCount);
try expect.equal(null, ctx.param);
try expect.equal(e1.raw, ctx.e[0]);
try expect.equal(lookup(Position), ctx.c[0][0]);
try expect.equal(lookup(Velocity), ctx.c[0][1]);
try expect.equal(0, ctx.s[0][0]);
try expect.equal(0, ctx.s[0][1]);
const p = e1.get(Position).?;
try expect.equal(2, p.x);
try expect.equal(4, p.y);
}