From 50c5a83c90bd5281cba6c26df13cd57917053fca Mon Sep 17 00:00:00 2001 From: copygirl Date: Fri, 8 Sep 2023 09:52:14 +0200 Subject: [PATCH] Add Entity tests involving World.setScope --- test/entity.zig | 105 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 105 insertions(+) diff --git a/test/entity.zig b/test/entity.zig index 2731b1f..8af1e25 100644 --- a/test/entity.zig +++ b/test/entity.zig @@ -82,3 +82,108 @@ test "Entity_init_id_add_2_comp" { 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}); +}