authorgravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2021-10-24 21:37:37 -07:00
committergravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2021-10-24 21:37:37 -07:00
logae73e1d8a27d9be8741dbb986842e605bc711e5d
treed2092d586e2166f2d954547da40a6efe3b1f67d1
parentb55ce86ded9d297c3fda6c36efe2db6a50f241dc

reformat main tests


1 files changed, 27 insertions(+), 6 deletions(-)

main.zig+27-6
...@@ -6,6 +6,33 @@ pub fn main() !void {...@@ -6,6 +6,33 @@ pub fn main() !void {
6 std.log.info("All your codebase are belong to us.", .{});6 std.log.info("All your codebase are belong to us.", .{});
7}7}
88
9fn harness(comptime seed: u64, comptime expects: []const [2]string) void {
10 var i: usize = 0;
11 while (i < expects.len) : (i += 1) {
12 _ = Case(seed, expects[i][0], expects[i][1]);
13 }
14}
15
16fn Case(comptime seed: u64, comptime fmt: string, comptime expected: string) type {
17 return struct {
18 test {
19 const alloc = std.testing.allocator;
20 const instant = time.DateTime.initUnixMs(seed);
21 const actual = try instant.formatAlloc(alloc, fmt);
22 defer alloc.free(actual);
23 std.testing.expectEqualStrings(expected, actual) catch return error.SkipZigTest;
24 }
25 };
26}
27
28comptime {
29 harness(0, &.{.{ "YYYY-MM-DD HH:mm:ss", "1970-01-01 00:00:00" }});
30 harness(1257894000000, &.{.{ "YYYY-MM-DD HH:mm:ss", "2009-11-10 23:00:00" }});
31 harness(1634858430000, &.{.{ "YYYY-MM-DD HH:mm:ss", "2021-10-21 23:20:30" }});
32 harness(1634858430023, &.{.{ "YYYY-MM-DD HH:mm:ss.SSS", "2021-10-21 23:20:30.023" }});
33 harness(1144509852789, &.{.{ "YYYY-MM-DD HH:mm:ss.SSS", "2006-04-08 15:24:12.789" }});
34}
35
9fn assertOk(unix_ms: u64, comptime format: []const u8, expected: string) !void {36fn assertOk(unix_ms: u64, comptime format: []const u8, expected: string) !void {
10 const alloc = std.testing.allocator;37 const alloc = std.testing.allocator;
1138
...@@ -16,12 +43,6 @@ fn assertOk(unix_ms: u64, comptime format: []const u8, expected: string) !void {...@@ -16,12 +43,6 @@ fn assertOk(unix_ms: u64, comptime format: []const u8, expected: string) !void {
16}43}
1744
18// zig fmt: off45// zig fmt: off
19test { try assertOk(0, "YYYY-MM-DD HH:mm:ss", "1970-01-01 00:00:00"); }
20test { try assertOk(1257894000000, "YYYY-MM-DD HH:mm:ss", "2009-11-10 23:00:00"); }
21test { try assertOk(1634858430000, "YYYY-MM-DD HH:mm:ss", "2021-10-21 23:20:30"); }
22test { try assertOk(1634858430023, "YYYY-MM-DD HH:mm:ss.SSS", "2021-10-21 23:20:30.023"); }
23test { try assertOk(1144509852789, "YYYY-MM-DD HH:mm:ss.SSS", "2006-04-08 15:24:12.789"); }
24
25// test every field46// test every field
26test { try assertOk(1144509852789, "M", "4"); }47test { try assertOk(1144509852789, "M", "4"); }
27test { try assertOk(1144509852789, "Mo", "4th"); }48test { try assertOk(1144509852789, "Mo", "4th"); }