commit d819da4350cc4325a7281ebeaf6057586b8eb0d7 (tree)
parent e246fe0f5bdf1168171f79570af16cd3e79665a6
Author: Vexu <git@vexu.eu>
Date: Wed, 9 Sep 2020 19:24:21 +0300
stage2: support multiple files in tests
Diffstat:
2 files changed, 53 insertions(+), 0 deletions(-)
diff --git a/src/test.zig b/src/test.zig
@@ -56,6 +56,12 @@ pub const TestContext = struct {
},
};
+ pub const File = struct {
+ /// Contents of the importable file. Doesn't yet support incremental updates.
+ src: [:0]const u8,
+ path: []const u8,
+ };
+
pub const TestType = enum {
Zig,
ZIR,
@@ -78,6 +84,8 @@ pub const TestContext = struct {
extension: TestType,
cbe: bool = false,
+ files: std.ArrayList(File),
+
/// Adds a subcase in which the module is updated with `src`, and the
/// resulting ZIR is validated against `result`.
pub fn addTransform(self: *Case, src: [:0]const u8, result: [:0]const u8) void {
@@ -156,6 +164,7 @@ pub const TestContext = struct {
.updates = std.ArrayList(Update).init(ctx.cases.allocator),
.output_mode = .Exe,
.extension = T,
+ .files = std.ArrayList(File).init(ctx.cases.allocator),
}) catch unreachable;
return &ctx.cases.items[ctx.cases.items.len - 1];
}
@@ -182,6 +191,7 @@ pub const TestContext = struct {
.updates = std.ArrayList(Update).init(ctx.cases.allocator),
.output_mode = .Obj,
.extension = T,
+ .files = std.ArrayList(File).init(ctx.cases.allocator),
}) catch unreachable;
return &ctx.cases.items[ctx.cases.items.len - 1];
}
@@ -204,6 +214,7 @@ pub const TestContext = struct {
.output_mode = .Obj,
.extension = T,
.cbe = true,
+ .files = std.ArrayList(File).init(ctx.cases.allocator),
}) catch unreachable;
return &ctx.cases.items[ctx.cases.items.len - 1];
}
@@ -505,6 +516,10 @@ pub const TestContext = struct {
});
defer comp.destroy();
+ for (case.files.items) |file| {
+ try tmp.dir.writeFile(file.path, file.src);
+ }
+
for (case.updates.items) |update, update_index| {
var update_node = root_node.start("update", 3);
update_node.activate();
diff --git a/test/stage2/test.zig b/test/stage2/test.zig
@@ -910,6 +910,44 @@ pub fn addCases(ctx: *TestContext) !void {
}
{
+ var case = ctx.exe("basic import", linux_x64);
+ case.addCompareOutput(
+ \\export fn _start() noreturn {
+ \\ @import("print.zig").print();
+ \\ exit();
+ \\}
+ \\
+ \\fn exit() noreturn {
+ \\ asm volatile ("syscall"
+ \\ :
+ \\ : [number] "{rax}" (231),
+ \\ [arg1] "{rdi}" (@as(usize, 0))
+ \\ : "rcx", "r11", "memory"
+ \\ );
+ \\ unreachable;
+ \\}
+ ,
+ "Hello, World!\n",
+ );
+ try case.files.append(.{
+ .src =
+ \\pub fn print() void {
+ \\ asm volatile ("syscall"
+ \\ :
+ \\ : [number] "{rax}" (@as(usize, 1)),
+ \\ [arg1] "{rdi}" (@as(usize, 1)),
+ \\ [arg2] "{rsi}" (@ptrToInt("Hello, World!\n")),
+ \\ [arg3] "{rdx}" (@as(usize, 14))
+ \\ : "rcx", "r11", "memory"
+ \\ );
+ \\ return;
+ \\}
+ ,
+ .path = "print.zig",
+ });
+ }
+
+ {
var case = ctx.exe("wasm function calls", wasi);
case.addCompareOutput(