From 8de26d5382a0f34db62f6e7d3b45b73ef801588f Mon Sep 17 00:00:00 2001 From: Josh Wolfe Date: Sat, 30 Aug 2025 23:46:10 -0400 Subject: [PATCH] add tests for new help stuff --- lib/std/cli.zig | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/lib/std/cli.zig b/lib/std/cli.zig index d9c380c31a..c3e3fd84d8 100644 --- a/lib/std/cli.zig +++ b/lib/std/cli.zig @@ -1532,3 +1532,43 @@ test "custom help" { try testing.expectError(error.Help, parseSlice(Args, allocator, &[_][]const u8{"--help"}, options)); try testing.expectEqualStrings(Args.help, aw.written()); } + +test "description" { + var arena: std.heap.ArenaAllocator = .init(testing.allocator); + defer arena.deinit(); + const allocator = arena.allocator(); + + var aw: Writer.Allocating = .init(allocator); + const options = Options{ .prog = "unused-prog", .writer = &aw.writer }; + + const Args = struct { + pub const description = + \\This is a description + ; + }; + try testing.expectError(error.Help, parseSlice(Args, allocator, &[_][]const u8{"--help"}, options)); + try testing.expect(mem.indexOf(u8, aw.written(), Args.description) != null); +} + +test "field help" { + var arena: std.heap.ArenaAllocator = .init(testing.allocator); + defer arena.deinit(); + const allocator = arena.allocator(); + + var aw: Writer.Allocating = .init(allocator); + const options = Options{ .prog = "unused-prog", .writer = &aw.writer }; + + const Args = struct { + named: struct { + output: []const u8, + pub const output_help = "help for output"; + }, + positional: struct { + args: []const []const u8 = &.{}, + pub const args_help = "help for args"; + }, + }; + try testing.expectError(error.Help, parseSlice(Args, allocator, &[_][]const u8{"--help"}, options)); + try testing.expect(mem.indexOf(u8, aw.written(), @FieldType(Args, "named").output_help) != null); + try testing.expect(mem.indexOf(u8, aw.written(), @FieldType(Args, "positional").args_help) != null); +}