From ea14480246c309b8dd89537c3479e90a1bff2fd1 Mon Sep 17 00:00:00 2001 From: Stephen Gutekanst Date: Wed, 27 Mar 2024 06:14:53 -0700 Subject: [PATCH] module: improve isString, add tests Signed-off-by: Stephen Gutekanst --- src/module.zig | 30 ++++++++++++++++++++++++------ 1 file changed, 24 insertions(+), 6 deletions(-) diff --git a/src/module.zig b/src/module.zig index 7d9b0090..cb0efe78 100644 --- a/src/module.zig +++ b/src/module.zig @@ -817,8 +817,7 @@ fn MComponents(comptime M: anytype) type { fn NamespacedState(comptime modules: anytype) type { var fields: []const std.builtin.Type.StructField = &[0]std.builtin.Type.StructField{}; inline for (modules) |M| { - // TODO: can't verify module here because it would introduce a dependency loop - // _ = ModuleInterface(M); + _ = ModuleInterface(M); // Validate the module const state_fields = std.meta.fields(M); const State = if (state_fields.len > 0) @Type(.{ .Struct = .{ @@ -846,18 +845,37 @@ fn NamespacedState(comptime modules: anytype) type { }); } -// TODO: tests -// TODO: stricter enforcement fn isString(comptime S: type) bool { return switch (@typeInfo(S)) { - .Pointer => |p| switch (@typeInfo(p.child)) { - .Array => |a| a.child == u8, + .Pointer => |p| switch (p.size) { + .Many, .Slice => p.child == u8, + .One => switch (@typeInfo(p.child)) { + .Array => |a| a.child == u8, + else => false, + }, else => false, }, else => false, }; } +test isString { + const x: [*:0]const u8 = "foobar"; + const y: []const u8 = "foobar"; + const z: *const [6:0]u8 = "foobar"; + try testing.expect(bool, true).eql(isString(@TypeOf(x))); + try testing.expect(bool, true).eql(isString(@TypeOf(y))); + try testing.expect(bool, true).eql(isString(@TypeOf(z))); + try testing.expect(bool, true).eql(isString(@TypeOf("baz"))); + + const v0: []const u32 = undefined; + const v1: u32 = undefined; + const v2: *u8 = undefined; + try testing.expect(bool, false).eql(isString(@TypeOf(v0))); + try testing.expect(bool, false).eql(isString(@TypeOf(v1))); + try testing.expect(bool, false).eql(isString(@TypeOf(v2))); +} + test { testing.refAllDeclsRecursive(@This()); }