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.

103 lines
2.9 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 expect = std.testing.expect;
const expectEql = std.testing.expectEqual;
const expectStrEql = std.testing.expectEqualStrings;
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();
}
}
test "World_progress_w_0" {
var world = try World.init(std.testing.allocator);
defer world.deinit();
_ = world.component(Position);
_ = world.component(Velocity);
const e1 = world.entity(.{}, .{ Position, Velocity });
const move_system = 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 expectEql(ctx.count, 1);
try expectEql(ctx.invoked, 1);
try expectEql(ctx.system, move_system.raw);
try expectEql(ctx.termCount, 2);
try expectEql(ctx.param, null);
try expectEql(ctx.e[0], e1.raw);
try expectEql(ctx.c[0][0], (try world.lookupByType(Position)).raw);
try expectEql(ctx.c[0][1], (try world.lookupByType(Velocity)).raw);
try expectEql(ctx.s[0][0], 0);
try expectEql(ctx.s[0][1], 0);
const p = e1.get(Position).?;
try expect(p.x != 0);
try expect(p.y != 0);
}
test "World_progress_w_t" {
var world = try World.init(std.testing.allocator);
defer world.deinit();
_ = world.component(Position);
_ = world.component(Velocity);
const e1 = world.entity(.{}, .{ Position, Velocity });
const move_system = 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 expectEql(ctx.count, 1);
try expectEql(ctx.invoked, 1);
try expectEql(ctx.system, move_system.raw);
try expectEql(ctx.termCount, 2);
try expectEql(ctx.param, null);
try expectEql(ctx.e[0], e1.raw);
try expectEql(ctx.c[0][0], (try world.lookupByType(Position)).raw);
try expectEql(ctx.c[0][1], (try world.lookupByType(Velocity)).raw);
try expectEql(ctx.s[0][0], 0);
try expectEql(ctx.s[0][1], 0);
const p = e1.get(Position).?;
try expect(p.x == 2);
try expect(p.y == 4);
}