From 0797eff416d0e499c8a32a08b1b0b5f446e62e36 Mon Sep 17 00:00:00 2001 From: copygirl Date: Fri, 5 Apr 2024 16:55:11 +0200 Subject: [PATCH] Test to check reusing components --- src/world.zig | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/src/world.zig b/src/world.zig index 363a02e..34fe256 100644 --- a/src/world.zig +++ b/src/world.zig @@ -339,3 +339,28 @@ test "World REST API" { world.quit(); } + +test "World delete and reuse component" { + flecszigble.init(std.testing.allocator); + var world = try World(void).initMinimal(); + defer world.deinit(); + + const Position = struct { x: f32, y: f32 }; + + const position_1 = try world.component(Position); + // Remove default component cleanup policy to allow deletion of the component. + position_1.remove(.{ flecs.core.OnDelete, flecs.core.Panic }); + + const entity = try world.entity(.{}, .{Position}); + entity.set(Position, .{ .x = 1.0, .y = 2.0 }); + try expect.equal(.{ .x = 1.0, .y = 2.0 }, entity.get(Position)); + + // Deleting the component automatically removes it from any entity. + position_1.delete(); + + const position_2 = try world.component(Position); + try expect.equal(position_2, world.lookupType(Position)); + + entity.set(Position, .{ .x = 2.0, .y = 4.0 }); + try expect.equal(.{ .x = 2.0, .y = 4.0 }, entity.get(Position)); +}