Commit 649da2df52

Noam Preil <pleasantatk@gmail.com>
2020-06-26 08:42:02
Stage2/Testing: Add convenience wrappers
1 parent c88edbc
Changed files (4)
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) };
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 {
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)
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)