diff options
| author | Andrew Kelley <andrew@ziglang.org> | 2020-01-05 14:50:02 -0500 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-01-05 14:50:02 -0500 |
| commit | a0ca34979ea5ff9be0d353f539e7c86dedbf8693 (patch) | |
| tree | e81c0dbbc2b7b81ce1d6f46b79ee781e31326ebc /test/src/run_translated_c.zig | |
| parent | 2e5342512f32384777ebcb0bf3dcb177b9380080 (diff) | |
| parent | 242f5d10d57eb9239e7a89d4e705fc05785abe7c (diff) | |
| download | zig-a0ca34979ea5ff9be0d353f539e7c86dedbf8693.tar.gz zig-a0ca34979ea5ff9be0d353f539e7c86dedbf8693.zip | |
Merge pull request #4053 from ziglang/test-run-translated-c
add test harness for "run translated C" tests
Diffstat (limited to 'test/src/run_translated_c.zig')
| -rw-r--r-- | test/src/run_translated_c.zig | 104 |
1 files changed, 104 insertions, 0 deletions
diff --git a/test/src/run_translated_c.zig b/test/src/run_translated_c.zig new file mode 100644 index 0000000000..d136a5fc94 --- /dev/null +++ b/test/src/run_translated_c.zig @@ -0,0 +1,104 @@ +// This is the implementation of the test harness for running translated +// C code. For the actual test cases, see test/run_translated_c.zig. +const std = @import("std"); +const build = std.build; +const ArrayList = std.ArrayList; +const fmt = std.fmt; +const mem = std.mem; +const fs = std.fs; +const warn = std.debug.warn; + +pub const RunTranslatedCContext = struct { + b: *build.Builder, + step: *build.Step, + test_index: usize, + test_filter: ?[]const u8, + + const TestCase = struct { + name: []const u8, + sources: ArrayList(SourceFile), + expected_stdout: []const u8, + allow_warnings: bool, + + const SourceFile = struct { + filename: []const u8, + source: []const u8, + }; + + pub fn addSourceFile(self: *TestCase, filename: []const u8, source: []const u8) void { + self.sources.append(SourceFile{ + .filename = filename, + .source = source, + }) catch unreachable; + } + }; + + pub fn create( + self: *RunTranslatedCContext, + allow_warnings: bool, + filename: []const u8, + name: []const u8, + source: []const u8, + expected_stdout: []const u8, + ) *TestCase { + const tc = self.b.allocator.create(TestCase) catch unreachable; + tc.* = TestCase{ + .name = name, + .sources = ArrayList(TestCase.SourceFile).init(self.b.allocator), + .expected_stdout = expected_stdout, + .allow_warnings = allow_warnings, + }; + + tc.addSourceFile(filename, source); + return tc; + } + + pub fn add( + self: *RunTranslatedCContext, + name: []const u8, + source: []const u8, + expected_stdout: []const u8, + ) void { + const tc = self.create(false, "source.c", name, source, expected_stdout); + self.addCase(tc); + } + + pub fn addAllowWarnings( + self: *RunTranslatedCContext, + name: []const u8, + source: []const u8, + expected_stdout: []const u8, + ) void { + const tc = self.create(true, "source.c", name, source, expected_stdout); + self.addCase(tc); + } + + pub fn addCase(self: *RunTranslatedCContext, case: *const TestCase) void { + const b = self.b; + + const annotated_case_name = fmt.allocPrint(self.b.allocator, "run-translated-c {}", .{case.name}) catch unreachable; + if (self.test_filter) |filter| { + if (mem.indexOf(u8, annotated_case_name, filter) == null) return; + } + + const write_src = b.addWriteFiles(); + for (case.sources.toSliceConst()) |src_file| { + write_src.add(src_file.filename, src_file.source); + } + const translate_c = b.addTranslateC(.{ + .write_file = .{ + .step = write_src, + .basename = case.sources.toSliceConst()[0].filename, + }, + }); + const exe = translate_c.addExecutable(); + exe.linkLibC(); + const run = exe.run(); + if (!case.allow_warnings) { + run.expectStdErrEqual(""); + } + run.expectStdOutEqual(case.expected_stdout); + + self.step.dependOn(&run.step); + } +}; |
