Add expect testing utility namespace

main
copygirl 1 year ago
parent f04bc1b093
commit e664ee4afd
  1. 49
      src/expect.zig
  2. 43
      src/path.zig
  3. 248
      test/entity.zig
  4. 58
      test/world.zig

@ -0,0 +1,49 @@
//! Provides helper functions that replace the standard `std.testing.expect*`
//! functions. The goal is to switch the type coersion such that `actual`
//! determines the type of `expected`, avoiding the use of `@as`.
const std = @import("std");
pub fn @"true"(value: anytype) !void {
return equal(true, value);
}
pub fn @"false"(value: anytype) !void {
return equal(false, value);
}
pub fn @"null"(value: anytype) !void {
return equal(null, value);
}
pub fn err(expected: anyerror, actual: anytype) !void {
return std.testing.expectError(expected, actual);
}
pub fn equal(expected: anytype, actual: anytype) !void {
return if (comptime std.meta.trait.isZigString(@TypeOf(expected)))
equalStrings(expected, actual)
else
std.testing.expectEqual(@as(@TypeOf(actual), expected), actual);
}
pub fn equalDeep(expected: anytype, actual: anytype) !void {
return std.testing.expectEqualDeep(@as(@TypeOf(actual), expected), actual);
}
pub fn equalStrings(expected: []const u8, actual: anyerror!?[]const u8) !void {
const actual_not_error = actual catch |e| {
std.debug.print("expected string, found {}", .{e});
return error.TestExpectedEqual;
};
if (actual_not_error) |actual_not_optional| {
return std.testing.expectEqualStrings(expected, actual_not_optional);
} else {
std.debug.print("expected string, found null", .{});
return error.TestExpectedEqual;
}
}
pub fn fmt(expected: []const u8, comptime template: []const u8, args: anytype) !void {
return std.testing.expectFmt(expected, template, args);
}

@ -344,10 +344,7 @@ pub const Path = struct {
test Path {
const alloc = std.testing.allocator;
const expect = std.testing.expect;
const expectFmt = std.testing.expectFmt;
const expectEqual = std.testing.expectEqual;
const expectEqualStrings = std.testing.expectEqualStrings;
const expect = @import("./expect.zig");
// Paths may be constructed by parsing strings.
const relative = try Path.fromString("some.relative.path", null, alloc);
@ -381,53 +378,53 @@ test Path {
// Use `.absolute` to test if the path is absolute.
// Relative paths can be treated as absolute in the absence of a context.
// Using absolute paths where a relative one is expected may cause an error.
try expectEqual(false, relative.absolute);
try expectEqual(true, absolute1.absolute);
try expectEqual(true, absolute2.absolute);
try expect.false(relative.absolute);
try expect.true(absolute1.absolute);
try expect.true(absolute2.absolute);
// The internal component parts of the path can be accessed with `.parts`.
try expectEqual(@as(usize, 3), relative.parts.len);
try expectEqualStrings("some", relative.parts[0].name);
try expectEqualStrings("relative", relative.parts[1].name);
try expectEqualStrings("path", relative.parts[2].name);
try expect.equal(3, relative.parts.len);
try expect.equal("some", relative.parts[0].name);
try expect.equal("relative", relative.parts[1].name);
try expect.equal("path", relative.parts[2].name);
// Parts can also be numeric ids, used for entities that don't have a name.
const numeric1 = try Path.fromString("100.101.bar", null, alloc);
defer numeric1.deinit();
try expectEqual(@as(usize, 3), numeric1.parts.len);
try expectEqual(@as(u32, 100), numeric1.parts[0].id);
try expectEqual(@as(u32, 101), numeric1.parts[1].id);
try expectEqualStrings("bar", numeric1.parts[2].name);
try expect.equal(3, numeric1.parts.len);
try expect.equal(100, numeric1.parts[0].id);
try expect.equal(101, numeric1.parts[1].id);
try expect.equal("bar", numeric1.parts[2].name);
// Numeric ids can also be passed to `buildParts`.
const numeric2_parts = Path.buildParts(.{ 100, 101, "bar" });
const numeric2 = Path.fromParts(false, &numeric2_parts);
try expect(numeric1.equals(numeric2));
try expect.equalDeep(numeric1.parts, numeric2.parts);
// Paths are formattable. As format specifier you can use options defined
// on the `FormatOptions` type, or an empty string to use the default.
try expectFmt("some.relative.path", "{}", .{relative});
try expectFmt("::I'm::absolute!", "{flecs_cpp}", .{absolute1});
try expectFmt("/home/copygirl", "{unix}", .{absolute2});
try expect.fmt("some.relative.path", "{}", .{relative});
try expect.fmt("::I'm::absolute!", "{flecs_cpp}", .{absolute1});
try expect.fmt("/home/copygirl", "{unix}", .{absolute2});
// They can also be turned directly into strings, which
// allows you to use entirely custom `FormatOptions`s:
const absolute1_str = try absolute1.toString(.{ .root_sep = "= ", .sep = " + " }, alloc);
defer alloc.free(absolute1_str);
try expectEqualStrings("= I'm + absolute!", absolute1_str);
try expect.equal("= I'm + absolute!", absolute1_str);
// The default `FormatOptions` may be changed.
Path.FormatOptions.default = Path.FormatOptions.unix;
// This affects functions that use them to parse or format strings.
try expectFmt("some/relative/path", "{}", .{relative});
try expect.fmt("some/relative/path", "{}", .{relative});
const another_relative = try Path.fromString("mom/sister/child", null, alloc);
defer another_relative.deinit();
try expectFmt("mom/sister/child", "{}", .{another_relative});
try expect.fmt("mom/sister/child", "{}", .{another_relative});
// A deep clone of a path can be allocated using `.clone()`.
// This clone owns the outer array and its inner strings.
const twin = try another_relative.clone(alloc);
try expectFmt("mom/sister/child", "{}", .{twin});
try expect.fmt("mom/sister/child", "{}", .{twin});
twin.deinit();
}

@ -2,15 +2,11 @@
// https://github.com/SanderMertens/flecs/blob/master/test/api/src/Entity.c
const std = @import("std");
const expect = std.testing.expect;
const expectFmt = std.testing.expectFmt;
const expectEqual = std.testing.expectEqual;
const expectEqualStrings = std.testing.expectEqualStrings;
const expectError = std.testing.expectError;
const alloc = std.testing.allocator;
const expect = @import("../src/expect.zig");
const util = @import("./util.zig");
const FlecsError = @import("../src/error.zig").FlecsError;
const flecs = @import("../src/main.zig");
const c = flecs.c;
@ -20,60 +16,60 @@ const Path = context.Path;
const World = context.World;
test "Entity_init_id" {
flecs.init(std.testing.allocator);
flecs.init(alloc);
var world = try World.initMinimal();
defer world.deinit();
const e = try world.entity(.{}, .{});
try expect(c.ecs_get_type(world.raw, e.raw) == null);
try expect.null(c.ecs_get_type(world.raw, e.raw));
}
test "Entity_init_id_name" {
flecs.init(std.testing.allocator);
flecs.init(alloc);
var world = try World.initMinimal();
defer world.deinit();
const e = try world.entity(.{ .name = "foo" }, .{});
try expectEqualStrings("foo", e.getName().?);
try expect.equal("foo", e.getName());
const path = try e.getPath(flecs.allocator);
defer path.deinit();
try expectFmt("foo", "{}", .{path});
try expect.fmt("foo", "{}", .{path});
}
test "Entity_init_id_path" {
flecs.init(std.testing.allocator);
flecs.init(alloc);
var world = try World.initMinimal();
defer world.deinit();
const e_parts = Path.buildParts(.{ "parent", "child" });
const e_path = Path.fromParts(false, &e_parts);
const e = try world.entity(.{ .path = e_path }, .{});
try expectEqualStrings("child", e.getName().?);
try expect.equal("child", e.getName());
const path = try e.getPath(flecs.allocator);
defer path.deinit();
try expectFmt("parent.child", "{}", .{path});
try expect.fmt("parent.child", "{}", .{path});
}
test "Entity_init_id_add_1_comp" {
const TagA = struct {};
flecs.init(std.testing.allocator);
flecs.init(alloc);
var world = try World.initMinimal();
defer world.deinit();
_ = try world.tag(TagA);
const e = try world.entity(.{}, .{TagA});
try expect(e.has(TagA));
try expect.true(e.has(TagA));
}
test "Entity_init_id_add_2_comp" {
const TagA = struct {};
const TagB = struct {};
flecs.init(std.testing.allocator);
flecs.init(alloc);
var world = try World.initMinimal();
defer world.deinit();
@ -81,138 +77,138 @@ test "Entity_init_id_add_2_comp" {
_ = try world.tag(TagB);
const e = try world.entity(.{}, .{ TagA, TagB });
try expect(e.has(TagA));
try expect(e.has(TagB));
try expect.true(e.has(TagA));
try expect.true(e.has(TagB));
}
test "Entity_init_id_w_scope" {
flecs.init(std.testing.allocator);
flecs.init(alloc);
var world = try World.initMinimal();
defer world.deinit();
const scope = try world.entity(.{}, .{});
_ = world.setScope(scope);
try expectEqual(scope, world.getScope().?);
try expect.equal(scope, world.getScope());
const e = try world.entity(.{}, .{});
try expect(e.has(.{ c.EcsChildOf, scope }));
try expect.true(e.has(.{ c.EcsChildOf, scope }));
}
test "Entity_init_id_name_w_scope" {
flecs.init(std.testing.allocator);
flecs.init(alloc);
var world = try World.initMinimal();
defer world.deinit();
const scope = try world.entity(.{ .name = "parent" }, .{});
try expectEqualStrings("parent", scope.getName().?);
try expect.equal("parent", scope.getName());
_ = world.setScope(scope);
try expectEqual(scope, world.getScope().?);
try expect.equal(scope, world.getScope());
const e = try world.entity(.{ .name = "child" }, .{});
try expect(e.has(.{ c.EcsChildOf, scope }));
try expectEqualStrings("child", e.getName().?);
try expect.true(e.has(.{ c.EcsChildOf, scope }));
try expect.equal("child", e.getName());
const path = try e.getPath(flecs.allocator);
defer path.deinit();
try expectFmt("parent.child", "{}", .{path});
try expect.fmt("parent.child", "{}", .{path});
}
test "Entity_init_id_path_w_scope" {
flecs.init(std.testing.allocator);
flecs.init(alloc);
var world = try World.initMinimal();
defer world.deinit();
const scope = try world.entity(.{ .name = "parent" }, .{});
try expectEqualStrings("parent", scope.getName().?);
try expect.equal("parent", scope.getName());
_ = world.setScope(scope);
try expectEqual(scope, world.getScope().?);
try expect.equal(scope, world.getScope());
const e_parts = Path.buildParts(.{ "child", "grandchild" });
const e_path = Path.fromParts(false, &e_parts);
const e = try world.entity(.{ .path = e_path }, .{});
try expectEqualStrings("grandchild", e.getName().?);
try expect.equal("grandchild", e.getName());
const path = try e.getPath(flecs.allocator);
defer path.deinit();
try expectFmt("parent.child.grandchild", "{}", .{path});
try expect.fmt("parent.child.grandchild", "{}", .{path});
}
test "Entity_init_id_fullpath_w_scope" {
flecs.init(std.testing.allocator);
flecs.init(alloc);
var world = try World.initMinimal();
defer world.deinit();
const scope = try world.entity(.{ .name = "parent" }, .{});
try expectEqualStrings("parent", scope.getName().?);
try expect.equal("parent", scope.getName());
_ = world.setScope(scope);
try expectEqual(scope, world.getScope().?);
try expect.equal(scope, world.getScope());
const p = try Path.fromString("::parent.child.grandchild", .{ .root_sep = "::", .sep = "." }, flecs.allocator);
defer p.deinit();
const e = try world.entity(.{ .path = p }, .{});
try expectEqualStrings("grandchild", e.getName().?);
try expect.equal("grandchild", e.getName());
const path = try e.getPath(flecs.allocator);
defer path.deinit();
try expectFmt("parent.child.grandchild", "{}", .{path});
try expect.fmt("parent.child.grandchild", "{}", .{path});
}
test "Entity_init_id_fullpath_w_scope_existing" {
flecs.init(std.testing.allocator);
flecs.init(alloc);
var world = try World.initMinimal();
defer world.deinit();
const scope = try world.entity(.{ .name = "parent" }, .{});
try expectEqualStrings("parent", scope.getName().?);
try expect.equal("parent", scope.getName());
_ = world.setScope(scope);
try expectEqual(scope, world.getScope().?);
try expect.equal(scope, world.getScope());
const p = try Path.fromString("::parent.child.grandchild", .{ .root_sep = "::", .sep = "." }, flecs.allocator);
defer p.deinit();
const e = try world.entity(.{ .path = p }, .{});
const r = try world.entity(.{ .path = p }, .{});
try expectEqual(e, r);
try expect.equal(e, r);
try expectEqualStrings("grandchild", e.getName().?);
try expect.equal("grandchild", e.getName());
const path = try e.getPath(flecs.allocator);
defer path.deinit();
try expectFmt("parent.child.grandchild", "{}", .{path});
try expect.fmt("parent.child.grandchild", "{}", .{path});
}
test "Entity_init_id_name_1_comp" {
const TagA = struct {};
flecs.init(std.testing.allocator);
flecs.init(alloc);
var world = try World.initMinimal();
defer world.deinit();
_ = try world.tag(TagA);
const e = try world.entity(.{ .name = "foo" }, .{TagA});
try expect(e.has(TagA));
try expectEqualStrings("foo", e.getName().?);
try expect.true(e.has(TagA));
try expect.equal("foo", e.getName());
const path = try e.getPath(flecs.allocator);
defer path.deinit();
try expectFmt("foo", "{}", .{path});
try expect.fmt("foo", "{}", .{path});
}
test "Entity_init_id_name_2_comp" {
const TagA = struct {};
const TagB = struct {};
flecs.init(std.testing.allocator);
flecs.init(alloc);
var world = try World.initMinimal();
defer world.deinit();
@ -220,19 +216,19 @@ test "Entity_init_id_name_2_comp" {
_ = try world.tag(TagB);
const e = try world.entity(.{ .name = "foo" }, .{ TagA, TagB });
try expect(e.has(TagA));
try expectEqualStrings("foo", e.getName().?);
try expect.true(e.has(TagA));
try expect.equal("foo", e.getName());
const path = try e.getPath(flecs.allocator);
defer path.deinit();
try expectFmt("foo", "{}", .{path});
try expect.fmt("foo", "{}", .{path});
}
test "Entity_init_id_name_2_comp_w_scope" {
const TagA = struct {};
const TagB = struct {};
flecs.init(std.testing.allocator);
flecs.init(alloc);
var world = try World.initMinimal();
defer world.deinit();
@ -240,24 +236,24 @@ test "Entity_init_id_name_2_comp_w_scope" {
_ = try world.tag(TagB);
const scope = try world.entity(.{ .name = "parent" }, .{});
try expectEqualStrings("parent", scope.getName().?);
try expect.equal("parent", scope.getName());
_ = world.setScope(scope);
try expectEqual(scope, world.getScope().?);
try expect.equal(scope, world.getScope());
const e = try world.entity(.{ .name = "child" }, .{ TagA, TagB });
try expect(e.has(TagA));
try expectEqualStrings("child", e.getName().?);
try expect.true(e.has(TagA));
try expect.equal("child", e.getName());
const path = try e.getPath(flecs.allocator);
defer path.deinit();
try expectFmt("parent.child", "{}", .{path});
try expect.fmt("parent.child", "{}", .{path});
}
test "Entity_id_add_1_comp" {
const TagA = struct {};
flecs.init(std.testing.allocator);
flecs.init(alloc);
var world = try World.initMinimal();
defer world.deinit();
@ -265,15 +261,15 @@ test "Entity_id_add_1_comp" {
const e = try world.entity(.{}, .{});
const r = try world.entity(.{ .id = e }, .{TagA});
try expectEqual(e, r);
try expect(e.has(TagA));
try expect.equal(e, r);
try expect.true(e.has(TagA));
}
test "Entity_id_add_2_comp" {
const TagA = struct {};
const TagB = struct {};
flecs.init(std.testing.allocator);
flecs.init(alloc);
var world = try World.initMinimal();
defer world.deinit();
@ -282,265 +278,265 @@ test "Entity_id_add_2_comp" {
const e = try world.entity(.{}, .{});
const r = try world.entity(.{ .id = e }, .{ TagA, TagB });
try expectEqual(e, r);
try expect(e.has(TagA));
try expect(e.has(TagB));
try expect.equal(e, r);
try expect.true(e.has(TagA));
try expect.true(e.has(TagB));
}
test "Entity_init_id_path_w_sep" {
flecs.init(std.testing.allocator);
flecs.init(alloc);
var world = try World.initMinimal();
defer world.deinit();
const p = try Path.fromString("parent::child", .{ .root_sep = null, .sep = "::" }, flecs.allocator);
defer p.deinit();
const e = try world.entity(.{ .path = p }, .{});
try expectEqualStrings("child", e.getName().?);
try expect.equal("child", e.getName());
const path = try e.getPath(flecs.allocator);
defer path.deinit();
try expectFmt("parent.child", "{}", .{path});
try expect.fmt("parent.child", "{}", .{path});
}
test "Entity_find_id_name" {
flecs.init(std.testing.allocator);
flecs.init(alloc);
var world = try World.initMinimal();
defer world.deinit();
const e = try world.entity(.{ .name = "foo" }, .{});
try expectEqualStrings("foo", e.getName().?);
try expect.equal("foo", e.getName());
const r = try world.entity(.{ .name = "foo" }, .{});
try expectEqual(e, r);
try expect.equal(e, r);
}
test "Entity_find_w_existing_id_name" {
flecs.init(std.testing.allocator);
flecs.init(alloc);
var world = try World.initMinimal();
defer world.deinit();
const id = try world.entity(.{}, .{});
const e = try world.entity(.{ .id = id, .name = "foo" }, .{});
try expectEqual(e, id);
try expectEqualStrings("foo", e.getName().?);
try expect.equal(e, id);
try expect.equal("foo", e.getName());
const r = try world.entity(.{ .name = "foo" }, .{});
try expectEqual(e, r);
try expect.equal(e, r);
}
test "Entity_find_id_name_w_scope" {
flecs.init(std.testing.allocator);
flecs.init(alloc);
var world = try World.initMinimal();
defer world.deinit();
const scope = try world.entity(.{ .name = "parent" }, .{});
try expectEqualStrings("parent", scope.getName().?);
try expect.equal("parent", scope.getName());
_ = world.setScope(scope);
try expectEqual(scope, world.getScope().?);
try expect.equal(scope, world.getScope());
const e = try world.entity(.{ .name = "child" }, .{});
try expectEqualStrings("child", e.getName().?);
try expect.equal("child", e.getName());
const path = try e.getPath(flecs.allocator);
defer path.deinit();
try expectFmt("parent.child", "{}", .{path});
try expect.fmt("parent.child", "{}", .{path});
const r = try world.entity(.{ .name = "child" }, .{});
try expectEqual(e, r);
try expect.equal(e, r);
}
test "Entity_find_id_path" {
flecs.init(std.testing.allocator);
flecs.init(alloc);
var world = try World.initMinimal();
defer world.deinit();
const e_parts = Path.buildParts(.{ "parent", "child" });
const e_path = Path.fromParts(false, &e_parts);
const e = try world.entity(.{ .path = e_path }, .{});
try expectEqualStrings("child", e.getName().?);
try expect.equal("child", e.getName());
const path = try e.getPath(flecs.allocator);
defer path.deinit();
try expectFmt("parent.child", "{}", .{path});
try expect.fmt("parent.child", "{}", .{path});
const r = try world.entity(.{ .path = e_path }, .{});
try expectEqual(e, r);
try expect.equal(e, r);
}
test "Entity_find_id_path_w_scope" {
flecs.init(std.testing.allocator);
flecs.init(alloc);
var world = try World.initMinimal();
defer world.deinit();
const scope = try world.entity(.{ .name = "parent" }, .{});
try expectEqualStrings("parent", scope.getName().?);
try expect.equal("parent", scope.getName());
_ = world.setScope(scope);
try expectEqual(scope, world.getScope().?);
try expect.equal(scope, world.getScope());
const e_parts = Path.buildParts(.{ "child", "grandchild" });
const e_path = Path.fromParts(false, &e_parts);
const e = try world.entity(.{ .path = e_path }, .{});
try expectEqualStrings("grandchild", e.getName().?);
try expect.equal("grandchild", e.getName());
const path = try e.getPath(flecs.allocator);
defer path.deinit();
try expectFmt("parent.child.grandchild", "{}", .{path});
try expect.fmt("parent.child.grandchild", "{}", .{path});
const r = try world.entity(.{ .path = e_path }, .{});
try expectEqual(e, r);
try expect.equal(e, r);
}
test "Entity_find_id_name_match" {
flecs.init(std.testing.allocator);
flecs.init(alloc);
var world = try World.initMinimal();
defer world.deinit();
const e = try world.entity(.{ .name = "foo" }, .{});
try expectEqualStrings("foo", e.getName().?);
try expect.equal("foo", e.getName());
const r = try world.entity(.{ .id = e, .name = "foo" }, .{});
try expectEqual(e, r);
try expect.equal(e, r);
}
test "Entity_find_id_name_match_w_scope" {
flecs.init(std.testing.allocator);
flecs.init(alloc);
var world = try World.initMinimal();
defer world.deinit();
const scope = try world.entity(.{ .name = "parent" }, .{});
try expectEqualStrings("parent", scope.getName().?);
try expect.equal("parent", scope.getName());
_ = world.setScope(scope);
try expectEqual(scope, world.getScope().?);
try expect.equal(scope, world.getScope());
const e = try world.entity(.{ .name = "child" }, .{});
try expectEqualStrings("child", e.getName().?);
try expect.equal("child", e.getName());
const path = try e.getPath(flecs.allocator);
defer path.deinit();
try expectFmt("parent.child", "{}", .{path});
try expect.fmt("parent.child", "{}", .{path});
const r1 = try world.entity(.{ .id = e, .name = "child" }, .{});
try expectEqual(e, r1);
try expect.equal(e, r1);
_ = world.setScope(null);
const r2_parts = Path.buildParts(.{ "parent", "child" });
const r2_path = Path.fromParts(false, &r2_parts);
const r2 = try world.entity(.{ .id = e, .path = r2_path }, .{});
try expectEqual(e, r2);
try expect.equal(e, r2);
}
test "Entity_find_id_path_match" {
flecs.init(std.testing.allocator);
flecs.init(alloc);
var world = try World.initMinimal();
defer world.deinit();
const e_parts = Path.buildParts(.{ "parent", "child" });
const e_path = Path.fromParts(false, &e_parts);
const e = try world.entity(.{ .path = e_path }, .{});
try expectEqualStrings("child", e.getName().?);
try expect.equal("child", e.getName());
const r = try world.entity(.{ .id = e, .path = e_path }, .{});
try expectEqual(e, r);
try expect.equal(e, r);
}
test "Entity_find_id_path_match_w_scope" {
flecs.init(std.testing.allocator);
flecs.init(alloc);
var world = try World.initMinimal();
defer world.deinit();
const scope = try world.entity(.{ .name = "parent" }, .{});
try expectEqualStrings("parent", scope.getName().?);
try expect.equal("parent", scope.getName());
_ = world.setScope(scope);
try expectEqual(scope, world.getScope().?);
try expect.equal(scope, world.getScope());
const e_parts = Path.buildParts(.{ "child", "grandchild" });
const e_path = Path.fromParts(false, &e_parts);
const e = try world.entity(.{ .path = e_path }, .{});
try expectEqualStrings("grandchild", e.getName().?);
try expect.equal("grandchild", e.getName());
const r1 = try world.entity(.{ .id = e, .path = e_path }, .{});
try expectEqual(e, r1);
try expect.equal(e, r1);
_ = world.setScope(null);
const r2_parts = Path.buildParts(.{ "parent", "child", "grandchild" });
const r2_path = Path.fromParts(false, &r2_parts);
const r2 = try world.entity(.{ .id = e, .path = r2_path }, .{});
try expectEqual(e, r2);
try expect.equal(e, r2);
}
test "Entity_find_id_name_mismatch" {
flecs.init(std.testing.allocator);
flecs.init(alloc);
var world = try World.initMinimal();
defer world.deinit();
const e = try world.entity(.{ .name = "foo" }, .{});
try expectEqualStrings("foo", e.getName().?);
try expect.equal("foo", e.getName());
_ = try world.entity(.{ .name = "bar" }, .{});
_ = c.ecs_log_set_level(-4);
const r = world.entity(.{ .id = e, .name = "bar" }, .{});
try expectError(FlecsError.Unknown, r);
try expect.err(FlecsError.Unknown, r);
}
test "Entity_find_id_name_mismatch_w_scope" {
flecs.init(std.testing.allocator);
flecs.init(alloc);
var world = try World.initMinimal();
defer world.deinit();
const scope = try world.entity(.{ .name = "parent" }, .{});
try expectEqualStrings("parent", scope.getName().?);
try expect.equal("parent", scope.getName());
_ = world.setScope(scope);
try expectEqual(scope, world.getScope().?);
try expect.equal(scope, world.getScope());
const e = try world.entity(.{ .name = "child" }, .{});
try expectEqualStrings("child", e.getName().?);
try expect.equal("child", e.getName());
_ = c.ecs_log_set_level(-4);
const r = world.entity(.{ .id = e, .name = "parent" }, .{});
try expectError(FlecsError.Unknown, r);
try expect.err(FlecsError.Unknown, r);
}
test "Entity_find_id_path_mismatch" {
flecs.init(std.testing.allocator);
flecs.init(alloc);
var world = try World.initMinimal();
defer world.deinit();
const e_parts = Path.buildParts(.{ "parent", "child" });
const e_path = Path.fromParts(false, &e_parts);
const e = try world.entity(.{ .path = e_path }, .{});
try expectEqualStrings("child", e.getName().?);
try expect.equal("child", e.getName());
_ = c.ecs_log_set_level(-4);
const r_parts = Path.buildParts(.{ "parent", "foo" });
const r_path = Path.fromParts(false, &r_parts);
const r = world.entity(.{ .id = e, .path = r_path }, .{});
try expectError(FlecsError.Unknown, r);
try expect.err(FlecsError.Unknown, r);
}
test "Entity_find_id_path_mismatch_w_scope" {
flecs.init(std.testing.allocator);
flecs.init(alloc);
var world = try World.initMinimal();
defer world.deinit();
const scope = try world.entity(.{ .name = "parent" }, .{});
try expectEqualStrings("parent", scope.getName().?);
try expect.equal("parent", scope.getName());
_ = world.setScope(scope);
try expectEqual(scope, world.getScope().?);
try expect.equal(scope, world.getScope());
const e_parts = Path.buildParts(.{ "child", "grandchild" });
const e_path = Path.fromParts(false, &e_parts);
const e = try world.entity(.{ .path = e_path }, .{});
try expectEqualStrings("grandchild", e.getName().?);
try expect.equal("grandchild", e.getName());
const unnamed_parts = Path.buildParts(.{ "child", "foo" });
const unnamed_path = Path.fromParts(false, &unnamed_parts);
@ -548,5 +544,5 @@ test "Entity_find_id_path_mismatch_w_scope" {
_ = c.ecs_log_set_level(-4);
const r = world.entity(.{ .id = e, .path = unnamed_path }, .{});
try expectError(FlecsError.Unknown, r);
try expect.err(FlecsError.Unknown, r);
}

@ -2,12 +2,10 @@
// 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 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;
@ -30,6 +28,10 @@ fn move(it: Iter) void {
}
}
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();
@ -50,21 +52,21 @@ test "World_progress_w_0" {
_ = 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 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 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);
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(p.x != 0);
try expect(p.y != 0);
try expect.true(p.x != 0);
try expect.true(p.y != 0);
}
test "World_progress_w_t" {
@ -87,19 +89,19 @@ test "World_progress_w_t" {
_ = 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 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 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);
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(p.x == 2);
try expect(p.y == 4);
try expect.equal(2, p.x);
try expect.equal(4, p.y);
}

Loading…
Cancel
Save