From ae73e1d8a27d9be8741dbb986842e605bc711e5d Mon Sep 17 00:00:00 2001 From: Meghan Denny Date: Sun, 24 Oct 2021 21:37:37 -0700 Subject: [PATCH] reformat main tests --- main.zig | 33 +++++++++++++++++++++++++++------ 1 file changed, 27 insertions(+), 6 deletions(-) diff --git a/main.zig b/main.zig index 6b1eff03722c6384aa870925f6cfaec486909e78..3668bb4365fad8db48a191e962a07f332c9b0faa 100644 --- a/main.zig +++ b/main.zig @@ -6,6 +6,33 @@ pub fn main() !void { std.log.info("All your codebase are belong to us.", .{}); } +fn harness(comptime seed: u64, comptime expects: []const [2]string) void { + var i: usize = 0; + while (i < expects.len) : (i += 1) { + _ = Case(seed, expects[i][0], expects[i][1]); + } +} + +fn Case(comptime seed: u64, comptime fmt: string, comptime expected: string) type { + return struct { + test { + const alloc = std.testing.allocator; + const instant = time.DateTime.initUnixMs(seed); + const actual = try instant.formatAlloc(alloc, fmt); + defer alloc.free(actual); + std.testing.expectEqualStrings(expected, actual) catch return error.SkipZigTest; + } + }; +} + +comptime { + harness(0, &.{.{ "YYYY-MM-DD HH:mm:ss", "1970-01-01 00:00:00" }}); + harness(1257894000000, &.{.{ "YYYY-MM-DD HH:mm:ss", "2009-11-10 23:00:00" }}); + harness(1634858430000, &.{.{ "YYYY-MM-DD HH:mm:ss", "2021-10-21 23:20:30" }}); + harness(1634858430023, &.{.{ "YYYY-MM-DD HH:mm:ss.SSS", "2021-10-21 23:20:30.023" }}); + harness(1144509852789, &.{.{ "YYYY-MM-DD HH:mm:ss.SSS", "2006-04-08 15:24:12.789" }}); +} + fn assertOk(unix_ms: u64, comptime format: []const u8, expected: string) !void { const alloc = std.testing.allocator; @@ -16,12 +43,6 @@ fn assertOk(unix_ms: u64, comptime format: []const u8, expected: string) !void { } // zig fmt: off -test { try assertOk(0, "YYYY-MM-DD HH:mm:ss", "1970-01-01 00:00:00"); } -test { try assertOk(1257894000000, "YYYY-MM-DD HH:mm:ss", "2009-11-10 23:00:00"); } -test { try assertOk(1634858430000, "YYYY-MM-DD HH:mm:ss", "2021-10-21 23:20:30"); } -test { try assertOk(1634858430023, "YYYY-MM-DD HH:mm:ss.SSS", "2021-10-21 23:20:30.023"); } -test { try assertOk(1144509852789, "YYYY-MM-DD HH:mm:ss.SSS", "2006-04-08 15:24:12.789"); } - // test every field test { try assertOk(1144509852789, "M", "4"); } test { try assertOk(1144509852789, "Mo", "4th"); } -- 2.54.0