From 5ad3647da098387fe86b37c7732de54bf5c2a36f Mon Sep 17 00:00:00 2001 From: copygirl Date: Sat, 2 Sep 2023 21:30:12 +0200 Subject: [PATCH] Add first Entity tests --- src/c.zig | 3 +++ test/entity.zig | 40 ++++++++++++++++++++++++++++++++++++++++ test/main.zig | 1 + 3 files changed, 44 insertions(+) create mode 100644 test/entity.zig diff --git a/src/c.zig b/src/c.zig index 016c88f..a707ebb 100644 --- a/src/c.zig +++ b/src/c.zig @@ -4,5 +4,8 @@ pub usingnamespace @cImport({ if (@import("builtin").mode == .Debug) @cDefine("FLECS_SANITIZE", {}); + // Flecs' default NULL macro causes issues. + @cDefine("NULL", "0"); + @cInclude("flecs.h"); }); diff --git a/test/entity.zig b/test/entity.zig new file mode 100644 index 0000000..5066600 --- /dev/null +++ b/test/entity.zig @@ -0,0 +1,40 @@ +// Reimplementations of the following tests from Flecs: +// https://github.com/SanderMertens/flecs/blob/master/test/api/src/Entity.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 Entity = context.Entity; + +test "Entity_init_id" { + var world = try World.initMinimal(std.testing.allocator); + defer world.deinit(); + + const e = try world.entity(.{}, .{}); + // try expect(e.raw != 0); -- Not necessary, world.entity() returns error if result would be 0. + try expect(c.ecs_get_type(world.raw, e.raw) == null); +} + +test "Entity_init_id_name" { + var world = try World.initMinimal(std.testing.allocator); + defer world.deinit(); + + const e = try world.entity(.{ .name = "foo" }, .{}); + // try expect(e.raw != 0); -- Not necessary, world.entity() returns error if result would be 0. + try expectStrEql("foo", e.getName().?); + + // TODO: Implement EntityPath. + const path = c.ecs_get_fullpath(world.raw, e.raw); + defer c.ecs_os_api.free_.?(path); + try expect(path != null); + try expectStrEql("foo", std.mem.sliceTo(path.?, 0)); +} diff --git a/test/main.zig b/test/main.zig index 437b4fa..682e66f 100644 --- a/test/main.zig +++ b/test/main.zig @@ -1,4 +1,5 @@ test { _ = @import("../src/main.zig"); + _ = @import("./entity.zig"); _ = @import("./world.zig"); }