From 5e353e60bb58771380d8a618b7b5d7ab3db0a6c6 Mon Sep 17 00:00:00 2001 From: Stephen Gutekanst Date: Sat, 9 Mar 2024 14:56:50 -0700 Subject: [PATCH] module: improve testing Signed-off-by: Stephen Gutekanst --- src/main.zig | 1 + src/module.zig | 29 +++++++++++++++-------------- 2 files changed, 16 insertions(+), 14 deletions(-) diff --git a/src/main.zig b/src/main.zig index 16f10bf1..545b3a02 100644 --- a/src/main.zig +++ b/src/main.zig @@ -38,6 +38,7 @@ test { _ = gfx; _ = math; _ = testing; + std.testing.refAllDeclsRecursive(@import("module.zig")); std.testing.refAllDeclsRecursive(ecs); std.testing.refAllDeclsRecursive(gamemode); std.testing.refAllDeclsRecursive(math); diff --git a/src/module.zig b/src/module.zig index 377357bc..eb189e4f 100644 --- a/src/module.zig +++ b/src/module.zig @@ -4,7 +4,7 @@ const testing = std.testing; // TODO: eliminate dependency on ECS here. const EntityID = @import("ecs/entities.zig").EntityID; -/// Verifies that T matches the basic layout of a mach.Module +/// Verifies that T matches the basic layout of a Mach module pub fn Module(comptime T: type) type { if (@typeInfo(T) != .Struct) @compileError("Module must be a struct type. Found:" ++ @typeName(T)); if (!@hasDecl(T, "name")) @compileError("Module must have `pub const name = .foobar;`"); @@ -15,6 +15,18 @@ pub fn Module(comptime T: type) type { return T; } +/// Verifies that the given list of module structs `.{Foo, Bar}` satisfy `Module(M)`. +pub fn Modules(comptime mods: anytype) type { + inline for (mods) |M| _ = Module(M); + return struct { + pub const modules = mods; + + pub const components = NamespacedComponents(mods){}; + + pub const State = NamespacedState(mods); + }; +} + fn NamespacedComponents(comptime modules: anytype) type { var fields: []const std.builtin.Type.StructField = &[0]std.builtin.Type.StructField{}; inline for (modules) |M| { @@ -80,18 +92,7 @@ fn NamespacedState(comptime modules: anytype) type { }); } -pub fn Modules(comptime mods: anytype) type { - inline for (mods) |M| _ = Module(M); - return struct { - pub const modules = mods; - - pub const components = NamespacedComponents(mods){}; - - pub const State = NamespacedState(mods); - }; -} - -test "module" { +test Module { _ = Module(struct { // Physics module state pointer: usize, @@ -111,7 +112,7 @@ test "module" { }); } -test "modules" { +test Modules { const Physics = Module(struct { // Physics module state pointer: usize,