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("./../expect.zig");
const util = @import("./../util.zig");
const flecszigble = @import("../../main.zig");
const c = flecszigble.c;
const flecs = @import("../../builtin/flecs.zig");
const OnUpdate = flecs.pipeline.OnUpdate;
const Context = flecszigble.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 {
const pos = it.field(Position, 1);
const vel = it.field(Velocity, 2);
util.Probe.probeIter(it) catch unreachable;
for (pos, vel) |*p, *v| {
p.x += v.x * it.deltaTime();
p.y += v.y * it.deltaTime();
}
}
test "World_progress_w_0" {
flecszigble.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, OnUpdate, "Position, Velocity");
var ctx = util.Probe{};
c.ecs_set_ctx(world.raw, &ctx, null);
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(Context.lookup(Position), ctx.c[0][0]);
try expect.equal(Context.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" {
flecszigble.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, OnUpdate, "Position, Velocity");
var ctx = util.Probe{};
c.ecs_set_ctx(world.raw, &ctx, null);
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(Context.lookup(Position), ctx.c[0][0]);
try expect.equal(Context.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);
}