Skip to content

Commit 5284ecc

Browse files
committed
add more tests
1 parent 6135549 commit 5284ecc

File tree

1 file changed

+60
-14
lines changed

1 file changed

+60
-14
lines changed

lib/std/Build/Step/Options.zig

Lines changed: 60 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -287,12 +287,11 @@ fn printValue(options: *Options, out: Writer, comptime T: type, value: T, indent
287287
try out.writeByteNTimes(' ', indent);
288288
try out.writeAll("}");
289289
},
290-
.@"union" => |@"union"| {
290+
.@"union" => {
291291
try out.writeAll(".{ ");
292292
switch (value) {
293293
inline else => |payload, tag| {
294-
try printValue(options, out, @"union".tag_type.?, tag, indent);
295-
try out.writeAll(" = ");
294+
try out.print(".{p_} = ", .{std.zig.fmtId(@tagName(tag))});
296295
try printValue(options, out, @TypeOf(payload), payload, indent);
297296
},
298297
}
@@ -477,32 +476,45 @@ test Options {
477476
world: bool = true,
478477
};
479478

479+
const NormalUnion = union(NormalEnum) {
480+
foo: u16,
481+
bar: [2]u8,
482+
};
483+
480484
const NestedStruct = struct {
481485
normal_struct: NormalStruct,
482486
normal_enum: NormalEnum = .foo,
483487
};
484488

489+
const NonExhaustiveEnum = enum(u8) {
490+
one,
491+
two,
492+
three,
493+
_,
494+
};
495+
485496
options.addOption(usize, "option1", 1);
486497
options.addOption(?usize, "option2", null);
487498
options.addOption(?usize, "option3", 3);
488499
options.addOption(comptime_int, "option4", 4);
500+
options.addOption(comptime_float, "option5", 0.125);
489501
options.addOption([]const u8, "string", "zigisthebest");
490502
options.addOption(?[]const u8, "optional_string", null);
491503
options.addOption([2][2]u16, "nested_array", nested_array);
492504
options.addOption([]const []const u16, "nested_slice", nested_slice);
493505
options.addOption(KeywordEnum, "keyword_enum", .@"0.8.1");
494506
options.addOption(std.SemanticVersion, "semantic_version", try std.SemanticVersion.parse("0.1.2-foo+bar"));
495-
options.addOption(NormalEnum, "normal1_enum", NormalEnum.foo);
496-
options.addOption(NormalEnum, "normal2_enum", NormalEnum.bar);
497-
options.addOption(NormalStruct, "normal1_struct", NormalStruct{
498-
.hello = "foo",
499-
});
500-
options.addOption(NormalStruct, "normal2_struct", NormalStruct{
501-
.hello = null,
502-
.world = false,
503-
});
504-
options.addOption(NestedStruct, "nested_struct", NestedStruct{
505-
.normal_struct = .{ .hello = "bar" },
507+
options.addOption(NormalEnum, "normal1_enum", .foo);
508+
options.addOption(NormalEnum, "normal2_enum", .bar);
509+
options.addOption(NormalStruct, "normal1_struct", .{ .hello = "foo" });
510+
options.addOption(NormalStruct, "normal2_struct", .{ .hello = null, .world = false });
511+
options.addOption(NormalUnion, "normal_union", .{ .bar = .{ 42, 64 } });
512+
options.addOption(NestedStruct, "nested_struct", .{ .normal_struct = .{ .hello = "bar" } });
513+
options.addOption(NonExhaustiveEnum, "non_exhaustive_enum1", .two);
514+
options.addOption(NonExhaustiveEnum, "non_exhaustive_enum2", @enumFromInt(20));
515+
options.addOption([2]NormalStruct, "array_of_structs", .{
516+
.{ .hello = null },
517+
.{ .hello = "zig", .world = true },
506518
});
507519

508520
try std.testing.expectEqualStrings(
@@ -514,6 +526,8 @@ test Options {
514526
\\
515527
\\pub const option4: comptime_int = 4;
516528
\\
529+
\\pub const option5: comptime_float = 1.25e-1;
530+
\\
517531
\\pub const string: []const u8 = "zigisthebest";
518532
\\
519533
\\pub const optional_string: ?[]const u8 = null;
@@ -578,6 +592,16 @@ test Options {
578592
\\ .world = false,
579593
\\};
580594
\\
595+
\\pub const @"Build.Step.Options.decltest.Options.NormalUnion" = union(@"Build.Step.Options.decltest.Options.NormalEnum") {
596+
\\ foo: u16,
597+
\\ bar: [2]u8,
598+
\\};
599+
\\
600+
\\pub const normal_union: @"Build.Step.Options.decltest.Options.NormalUnion" = .{ .bar = .{
601+
\\ 42,
602+
\\ 64,
603+
\\} };
604+
\\
581605
\\pub const @"Build.Step.Options.decltest.Options.NestedStruct" = struct {
582606
\\ normal_struct: @"Build.Step.Options.decltest.Options.NormalStruct",
583607
\\ normal_enum: @"Build.Step.Options.decltest.Options.NormalEnum" = .foo,
@@ -591,6 +615,28 @@ test Options {
591615
\\ .normal_enum = .foo,
592616
\\};
593617
\\
618+
\\pub const @"Build.Step.Options.decltest.Options.NonExhaustiveEnum" = enum(u8) {
619+
\\ one = 0,
620+
\\ two = 1,
621+
\\ three = 2,
622+
\\ _,
623+
\\};
624+
\\
625+
\\pub const non_exhaustive_enum1: @"Build.Step.Options.decltest.Options.NonExhaustiveEnum" = .two;
626+
\\
627+
\\pub const non_exhaustive_enum2: @"Build.Step.Options.decltest.Options.NonExhaustiveEnum" = @enumFromInt(20);
628+
\\
629+
\\pub const array_of_structs: [2]@"Build.Step.Options.decltest.Options.NormalStruct" = .{
630+
\\ .{
631+
\\ .hello = null,
632+
\\ .world = true,
633+
\\ },
634+
\\ .{
635+
\\ .hello = "zig",
636+
\\ .world = true,
637+
\\ },
638+
\\};
639+
\\
594640
\\
595641
, options.contents.items);
596642

0 commit comments

Comments
 (0)