Fix Entity.name / symbol not returning null, add tests

main
copygirl 2 months ago
parent ae34763f71
commit ffddcdf9b9
  1. 38
      src/entity.zig

@ -227,7 +227,7 @@ pub fn Entity(comptime ctx: anytype) type {
/// the entity is alive and its name doesn't change.
pub fn name(self: Self) ?[:0]const u8 {
const result = c.ecs_get_name(self.world.raw, self.raw);
return std.mem.sliceTo(result, 0);
return if (result != 0) std.mem.sliceTo(result, 0) else null;
}
/// Sets the name of this `Entity` to the specified value, if any.
@ -243,7 +243,7 @@ pub fn Entity(comptime ctx: anytype) type {
/// the entity is alive and its symbol doesn't change.
pub fn symbol(self: Self) ?[:0]const u8 {
const result = c.ecs_get_symbol(self.world.raw, self.raw);
return std.mem.sliceTo(result, 0);
return if (result != 0) std.mem.sliceTo(result, 0) else null;
}
/// Sets the symbol of this `Entity` to the specified value, if any.
@ -375,6 +375,35 @@ pub fn Entity(comptime ctx: anytype) type {
const expect = @import("./test/expect.zig");
const flecszigble = @import("./main.zig");
test "Entity with name and symbol" {
flecszigble.init(std.testing.allocator);
var world = try flecszigble.World(void).initMinimal();
defer world.deinit();
const entity = try world.entity(.{ .name = "name", .symbol = "symbol" }, .{});
try expect.equalStrings("name", entity.name());
try expect.equalStrings("symbol", entity.symbol());
try expect.equal(entity, world.lookupChild(null, "name"));
try expect.equal(entity, world.lookupSymbol("symbol"));
}
test "Entity get and set name" {
flecszigble.init(std.testing.allocator);
var world = try flecszigble.World(void).initMinimal();
defer world.deinit();
const entity = try world.entity(.{}, .{});
try expect.equal(null, entity.name());
entity.setName("hello");
try expect.equalStrings("hello", entity.name());
entity.setName(null);
try expect.equal(null, entity.name());
}
test "Entity get and set" {
flecszigble.init(std.testing.allocator);
var world = try flecszigble.World(void).initMinimal();
@ -472,4 +501,9 @@ test "Entity format" {
try expect.fmt("/grandparent", "{unix}", .{grandparent});
try expect.fmt("/grandparent/parent", "{unix}", .{parent});
try expect.fmt("/grandparent/parent/child", "{unix}", .{child});
parent.setName(null);
try expect.fmt("411", "{s}", .{parent});
try expect.fmt("/grandparent/411", "{unix}", .{parent});
try expect.fmt("/grandparent/411/child", "{unix}", .{child});
}

Loading…
Cancel
Save