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.
189 lines
5.4 KiB
189 lines
5.4 KiB
// 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 expectFmt = std.testing.expectFmt; |
|
const expectEqual = std.testing.expectEqual; |
|
const expectEqualStrings = std.testing.expectEqualStrings; |
|
|
|
const util = @import("./util.zig"); |
|
|
|
const flecs = @import("../src/main.zig"); |
|
const c = flecs.c; |
|
|
|
const context = flecs.Context(void); |
|
const Entity = context.Entity; |
|
const Path = context.Path; |
|
const World = context.World; |
|
|
|
test "Entity_init_id" { |
|
flecs.init(std.testing.allocator); |
|
var world = try World.initMinimal(); |
|
defer world.deinit(); |
|
|
|
const e = try world.entity(.{}, .{}); |
|
try expect(c.ecs_get_type(world.raw, e.raw) == null); |
|
} |
|
|
|
test "Entity_init_id_name" { |
|
flecs.init(std.testing.allocator); |
|
var world = try World.initMinimal(); |
|
defer world.deinit(); |
|
|
|
const e = try world.entity(.{ .name = "foo" }, .{}); |
|
try expectEqualStrings("foo", e.getName().?); |
|
|
|
const path2 = try e.getPath(flecs.allocator); |
|
defer path2.deinit(); |
|
try expectFmt("foo", "{}", .{path2}); |
|
} |
|
|
|
test "Entity_init_id_path" { |
|
flecs.init(std.testing.allocator); |
|
var world = try World.initMinimal(); |
|
defer world.deinit(); |
|
|
|
const p = try Path.fromString("parent.child", null, flecs.allocator); |
|
defer p.deinit(); |
|
const e = try world.entity(.{ .path = p }, .{}); |
|
try expectEqualStrings("child", e.getName().?); |
|
|
|
const path = try e.getPath(flecs.allocator); |
|
defer path.deinit(); |
|
try expectFmt("parent.child", "{}", .{path}); |
|
} |
|
|
|
test "Entity_init_id_add_1_comp" { |
|
const TagA = struct {}; |
|
|
|
flecs.init(std.testing.allocator); |
|
var world = try World.initMinimal(); |
|
defer world.deinit(); |
|
|
|
_ = try world.tag(TagA); |
|
|
|
const e = try world.entity(.{}, .{TagA}); |
|
try expect(e.has(TagA)); |
|
} |
|
|
|
test "Entity_init_id_add_2_comp" { |
|
const TagA = struct {}; |
|
const TagB = struct {}; |
|
|
|
flecs.init(std.testing.allocator); |
|
var world = try World.initMinimal(); |
|
defer world.deinit(); |
|
|
|
_ = try world.tag(TagA); |
|
_ = try world.tag(TagB); |
|
|
|
const e = try world.entity(.{}, .{ TagA, TagB }); |
|
try expect(e.has(TagA)); |
|
try expect(e.has(TagB)); |
|
} |
|
|
|
test "Entity_init_id_w_scope" { |
|
flecs.init(std.testing.allocator); |
|
var world = try World.initMinimal(); |
|
defer world.deinit(); |
|
|
|
const scope = try world.entity(.{}, .{}); |
|
|
|
_ = world.setScope(scope); |
|
try expectEqual(scope, world.getScope().?); |
|
|
|
const e = try world.entity(.{}, .{}); |
|
|
|
try expect(e.has(.{ c.EcsChildOf, scope })); |
|
} |
|
|
|
test "Entity_init_id_name_w_scope" { |
|
flecs.init(std.testing.allocator); |
|
var world = try World.initMinimal(); |
|
defer world.deinit(); |
|
|
|
const scope = try world.entity(.{ .name = "parent" }, .{}); |
|
try expectEqualStrings("parent", scope.getName().?); |
|
|
|
_ = world.setScope(scope); |
|
try expectEqual(scope, world.getScope().?); |
|
|
|
const e = try world.entity(.{ .name = "child" }, .{}); |
|
|
|
try expect(e.has(.{ c.EcsChildOf, scope })); |
|
try expectEqualStrings("child", e.getName().?); |
|
|
|
const path = try e.getPath(flecs.allocator); |
|
defer path.deinit(); |
|
try expectFmt("parent.child", "{}", .{path}); |
|
} |
|
|
|
test "Entity_init_id_path_w_scope" { |
|
flecs.init(std.testing.allocator); |
|
var world = try World.initMinimal(); |
|
defer world.deinit(); |
|
|
|
const scope = try world.entity(.{ .name = "parent" }, .{}); |
|
try expectEqualStrings("parent", scope.getName().?); |
|
|
|
_ = world.setScope(scope); |
|
try expectEqual(scope, world.getScope().?); |
|
|
|
const p = try Path.fromString("child.grandchild", null, flecs.allocator); |
|
defer p.deinit(); |
|
const e = try world.entity(.{ .path = p }, .{}); |
|
|
|
try expectEqualStrings("grandchild", e.getName().?); |
|
|
|
const path = try e.getPath(flecs.allocator); |
|
defer path.deinit(); |
|
try expectFmt("parent.child.grandchild", "{}", .{path}); |
|
} |
|
|
|
test "Entity_init_id_fullpath_w_scope" { |
|
flecs.init(std.testing.allocator); |
|
var world = try World.initMinimal(); |
|
defer world.deinit(); |
|
|
|
const scope = try world.entity(.{ .name = "parent" }, .{}); |
|
try expectEqualStrings("parent", scope.getName().?); |
|
|
|
_ = world.setScope(scope); |
|
try expectEqual(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().?); |
|
|
|
const path = try e.getPath(flecs.allocator); |
|
defer path.deinit(); |
|
try expectFmt("parent.child.grandchild", "{}", .{path}); |
|
} |
|
|
|
test "Entity_init_id_fullpath_w_scope_existing" { |
|
flecs.init(std.testing.allocator); |
|
var world = try World.initMinimal(); |
|
defer world.deinit(); |
|
|
|
const scope = try world.entity(.{ .name = "parent" }, .{}); |
|
try expectEqualStrings("parent", scope.getName().?); |
|
|
|
_ = world.setScope(scope); |
|
try expectEqual(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 expectEqualStrings("grandchild", e.getName().?); |
|
|
|
const path = try e.getPath(flecs.allocator); |
|
defer path.deinit(); |
|
try expectFmt("parent.child.grandchild", "{}", .{path}); |
|
}
|
|
|