From 649da2df5234f504f2f22afe9e0b4cf7eeb9ef35 Mon Sep 17 00:00:00 2001 From: Noam Preil Date: Fri, 26 Jun 2020 02:42:02 -0400 Subject: [PATCH] Stage2/Testing: Add convenience wrappers --- src-self-hosted/test.zig | 74 ++++++++++++++++++++++++++++++++++ test/stage2/compare_output.zig | 2 +- test/stage2/compile_errors.zig | 6 +-- test/stage2/zir.zig | 10 ++--- 4 files changed, 83 insertions(+), 9 deletions(-) diff --git a/src-self-hosted/test.zig b/src-self-hosted/test.zig index 3f7ff3303a..89532378a5 100644 --- a/src-self-hosted/test.zig +++ b/src-self-hosted/test.zig @@ -138,6 +138,14 @@ pub const TestContext = struct { return &ctx.cases.items[ctx.cases.items.len - 1]; } + pub fn exe(ctx: *TestContext, name: []const u8, target: std.zig.CrossTarget) *Case { + return ctx.addExe(name, target, .Zig); + } + + pub fn exeZIR(ctx: *TestContext, name: []const u8, target: std.zig.CrossTarget) *Case { + return ctx.addExe(name, target, .ZIR); + } + pub fn addObj( ctx: *TestContext, name: []const u8, @@ -154,6 +162,14 @@ pub const TestContext = struct { return &ctx.cases.items[ctx.cases.items.len - 1]; } + pub fn obj(ctx: *TestContext, name: []const u8, target: std.zig.CrossTarget) *Case { + return ctx.addObj(name, target, .Zig); + } + + pub fn objZIR(ctx: *TestContext, name: []const u8, target: std.zig.CrossTarget) *Case { + return ctx.addObj(name, target, .ZIR); + } + pub fn addCompareOutput( ctx: *TestContext, name: []const u8, @@ -164,6 +180,24 @@ pub const TestContext = struct { ctx.addExe(name, .{}, T).addCompareOutput(src, expected_stdout); } + pub fn compareOutput( + ctx: *TestContext, + name: []const u8, + src: [:0]const u8, + expected_stdout: []const u8, + ) void { + return ctx.addCompareOutput(name, .Zig, src, expected_stdout); + } + + pub fn compareOutputZIR( + ctx: *TestContext, + name: []const u8, + src: [:0]const u8, + expected_stdout: []const u8, + ) void { + ctx.addCompareOutput(name, .ZIR, src, expected_stdout); + } + pub fn addTransform( ctx: *TestContext, name: []const u8, @@ -175,6 +209,26 @@ pub const TestContext = struct { ctx.addObj(name, target, T).addTransform(src, result); } + pub fn transform( + ctx: *TestContext, + name: []const u8, + target: std.zig.CrossTarget, + src: [:0]const u8, + result: [:0]const u8, + ) void { + ctx.addTransform(name, target, .Zig, src, result); + } + + pub fn transformZIR( + ctx: *TestContext, + name: []const u8, + target: std.zig.CrossTarget, + src: [:0]const u8, + result: [:0]const u8, + ) void { + ctx.addTransform(name, target, .ZIR, src, result); + } + pub fn addError( ctx: *TestContext, name: []const u8, @@ -186,6 +240,26 @@ pub const TestContext = struct { ctx.addObj(name, target, T).addError(src, expected_errors); } + pub fn compileError( + ctx: *TestContext, + name: []const u8, + target: std.zig.CrossTarget, + src: [:0]const u8, + expected_errors: []const []const u8, + ) void { + ctx.addError(name, target, .Zig, src, expected_errors); + } + + pub fn compileErrorZIR( + ctx: *TestContext, + name: []const u8, + target: std.zig.CrossTarget, + src: [:0]const u8, + expected_errors: []const []const u8, + ) void { + ctx.addError(name, target, .ZIR, src, expected_errors); + } + fn init() TestContext { const allocator = std.heap.page_allocator; return .{ .cases = std.ArrayList(Case).init(allocator) }; diff --git a/test/stage2/compare_output.zig b/test/stage2/compare_output.zig index 902c1e493f..d49f16876e 100644 --- a/test/stage2/compare_output.zig +++ b/test/stage2/compare_output.zig @@ -17,7 +17,7 @@ pub fn addCases(ctx: *TestContext) !void { } { - var case = ctx.addExe("hello world with updates", linux_x64, .Zig); + var case = ctx.exe("hello world with updates", linux_x64); // Regular old hello world case.addCompareOutput( \\export fn _start() noreturn { diff --git a/test/stage2/compile_errors.zig b/test/stage2/compile_errors.zig index 1ee8e9184b..905f106f94 100644 --- a/test/stage2/compile_errors.zig +++ b/test/stage2/compile_errors.zig @@ -9,7 +9,7 @@ const linux_x64 = std.zig.CrossTarget{ }; pub fn addCases(ctx: *TestContext) !void { - ctx.addError("call undefined local", linux_x64, .ZIR, + ctx.compileErrorZIR("call undefined local", linux_x64, \\@noreturn = primitive(noreturn) \\ \\@start_fnty = fntype([], @noreturn, cc=Naked) @@ -19,7 +19,7 @@ pub fn addCases(ctx: *TestContext) !void { // TODO: address inconsistency in this message and the one in the next test , &[_][]const u8{":5:13: error: unrecognized identifier: %test"}); - ctx.addError("call with non-existent target", linux_x64, .ZIR, + ctx.compileErrorZIR("call with non-existent target", linux_x64, \\@noreturn = primitive(noreturn) \\ \\@start_fnty = fntype([], @noreturn, cc=Naked) @@ -31,7 +31,7 @@ pub fn addCases(ctx: *TestContext) !void { , &[_][]const u8{":5:13: error: decl 'notafunc' not found"}); // TODO: this error should occur at the call site, not the fntype decl - ctx.addError("call naked function", linux_x64, .ZIR, + ctx.compileErrorZIR("call naked function", linux_x64, \\@noreturn = primitive(noreturn) \\ \\@start_fnty = fntype([], @noreturn, cc=Naked) diff --git a/test/stage2/zir.zig b/test/stage2/zir.zig index 17d5ce9b5b..052ada667e 100644 --- a/test/stage2/zir.zig +++ b/test/stage2/zir.zig @@ -9,7 +9,7 @@ const linux_x64 = std.zig.CrossTarget{ }; pub fn addCases(ctx: *TestContext) !void { - ctx.addTransform("referencing decls which appear later in the file", linux_x64, .ZIR, + ctx.transformZIR("referencing decls which appear later in the file", linux_x64, \\@void = primitive(void) \\@fnty = fntype([], @void, cc=C) \\ @@ -32,7 +32,7 @@ pub fn addCases(ctx: *TestContext) !void { \\}) \\ ); - ctx.addTransform("elemptr, add, cmp, condbr, return, breakpoint", linux_x64, .ZIR, + ctx.transformZIR("elemptr, add, cmp, condbr, return, breakpoint", linux_x64, \\@void = primitive(void) \\@usize = primitive(usize) \\@fnty = fntype([], @void, cc=C) @@ -86,7 +86,7 @@ pub fn addCases(ctx: *TestContext) !void { ); { - var case = ctx.addObj("reference cycle with compile error in the cycle", linux_x64, .ZIR); + var case = ctx.objZIR("reference cycle with compile error in the cycle", linux_x64); case.addTransform( \\@void = primitive(void) \\@fnty = fntype([], @void, cc=C) @@ -207,7 +207,7 @@ pub fn addCases(ctx: *TestContext) !void { return; } - ctx.addCompareOutput("hello world ZIR", .ZIR, + ctx.compareOutputZIR("hello world ZIR", \\@noreturn = primitive(noreturn) \\@void = primitive(void) \\@usize = primitive(usize) @@ -265,7 +265,7 @@ pub fn addCases(ctx: *TestContext) !void { \\ ); - ctx.addCompareOutput("function call with no args no return value", .ZIR, + ctx.compareOutputZIR("function call with no args no return value", \\@noreturn = primitive(noreturn) \\@void = primitive(void) \\@usize = primitive(usize)