diff options
| author | Andrew Kelley <superjoe30@gmail.com> | 2017-04-19 14:00:12 -0400 |
|---|---|---|
| committer | Andrew Kelley <superjoe30@gmail.com> | 2017-04-19 14:00:12 -0400 |
| commit | d1e01e43d3b2078bfb07defb693d819e99eaa6c5 (patch) | |
| tree | ee5a96ae18383a89b62533c9aada1d867a485959 /test/compare_output.zig | |
| parent | 666435195fff867417f92e1b4f8ef7e0608470ee (diff) | |
| download | zig-d1e01e43d3b2078bfb07defb693d819e99eaa6c5.tar.gz zig-d1e01e43d3b2078bfb07defb693d819e99eaa6c5.zip | |
convert assemble and link tests to zig build system
Diffstat (limited to 'test/compare_output.zig')
| -rw-r--r-- | test/compare_output.zig | 189 |
1 files changed, 3 insertions, 186 deletions
diff --git a/test/compare_output.zig b/test/compare_output.zig index f971d7eef1..33ec04995d 100644 --- a/test/compare_output.zig +++ b/test/compare_output.zig @@ -1,26 +1,7 @@ -const std = @import("std"); -const debug = std.debug; -const build = std.build; -const os = std.os; -const StdIo = os.ChildProcess.StdIo; -const Term = os.ChildProcess.Term; -const Buffer0 = std.cstr.Buffer0; -const io = std.io; -const mem = std.mem; -const fmt = std.fmt; -const List = std.list.List; - -error TestFailed; - -pub fn addCompareOutputTests(b: &build.Builder, test_filter: ?[]const u8) -> &build.Step { - const cases = %%b.allocator.create(CompareOutputContext); - *cases = CompareOutputContext { - .b = b, - .compare_output_tests = b.step("test-compare-output", "Run the compare output tests"), - .test_index = 0, - .test_filter = test_filter, - }; +const os = @import("std").os; +const tests = @import("tests.zig"); +pub fn addCases(cases: &tests.CompareOutputContext) { cases.addC("hello world with libc", \\const c = @cImport(@cInclude("stdio.h")); \\export fn main(argc: c_int, argv: &&u8) -> c_int { @@ -420,168 +401,4 @@ pub fn addCompareOutputTests(b: &build.Builder, test_filter: ?[]const u8) -> &bu tc }); - - return cases.compare_output_tests; } - -const CompareOutputContext = struct { - b: &build.Builder, - compare_output_tests: &build.Step, - test_index: usize, - test_filter: ?[]const u8, - - const TestCase = struct { - name: []const u8, - sources: List(SourceFile), - expected_output: []const u8, - link_libc: bool, - - const SourceFile = struct { - filename: []const u8, - source: []const u8, - }; - - pub fn addSourceFile(self: &TestCase, filename: []const u8, source: []const u8) { - %%self.sources.append(SourceFile { - .filename = filename, - .source = source, - }); - } - }; - - pub fn create(self: &CompareOutputContext, name: []const u8, source: []const u8, - expected_output: []const u8) -> TestCase - { - var tc = TestCase { - .name = name, - .sources = List(TestCase.SourceFile).init(self.b.allocator), - .expected_output = expected_output, - .link_libc = false, - }; - tc.addSourceFile("source.zig", source); - return tc; - } - - pub fn addC(self: &CompareOutputContext, name: []const u8, source: []const u8, expected_output: []const u8) { - var tc = self.create(name, source, expected_output); - tc.link_libc = true; - self.addCase(tc); - } - - pub fn add(self: &CompareOutputContext, name: []const u8, source: []const u8, expected_output: []const u8) { - const tc = self.create(name, source, expected_output); - self.addCase(tc); - } - - pub fn addCase(self: &CompareOutputContext, case: &const TestCase) { - const b = self.b; - - const root_src = %%os.path.join(b.allocator, "test_artifacts", case.sources.items[0].filename); - const exe_path = %%os.path.join(b.allocator, "test_artifacts", "test"); - - for ([]bool{false, true}) |release| { - const annotated_case_name = %%fmt.allocPrint(self.b.allocator, "{} ({})", - case.name, if (release) "release" else "debug"); - if (const filter ?= self.test_filter) { - if (mem.indexOf(u8, annotated_case_name, filter) == null) - continue; - } - - const exe = b.addExecutable("test", root_src); - exe.setOutputPath(exe_path); - exe.setRelease(release); - if (case.link_libc) { - exe.linkLibrary("c"); - } - - for (case.sources.toSliceConst()) |src_file| { - const expanded_src_path = %%os.path.join(b.allocator, "test_artifacts", src_file.filename); - const write_src = b.addWriteFile(expanded_src_path, src_file.source); - exe.step.dependOn(&write_src.step); - } - - const run_and_cmp_output = RunCompareOutputStep.create(self, exe_path, annotated_case_name, - case.expected_output); - run_and_cmp_output.step.dependOn(&exe.step); - - self.compare_output_tests.dependOn(&run_and_cmp_output.step); - } - } -}; - -const RunCompareOutputStep = struct { - step: build.Step, - context: &CompareOutputContext, - exe_path: []const u8, - name: []const u8, - expected_output: []const u8, - test_index: usize, - - pub fn create(context: &CompareOutputContext, exe_path: []const u8, - name: []const u8, expected_output: []const u8) -> &RunCompareOutputStep - { - const allocator = context.b.allocator; - const ptr = %%allocator.create(RunCompareOutputStep); - *ptr = RunCompareOutputStep { - .context = context, - .exe_path = exe_path, - .name = name, - .expected_output = expected_output, - .test_index = context.test_index, - .step = build.Step.init("RunCompareOutput", allocator, make), - }; - context.test_index += 1; - return ptr; - } - - fn make(step: &build.Step) -> %void { - const self = @fieldParentPtr(RunCompareOutputStep, "step", step); - const b = self.context.b; - - const full_exe_path = b.pathFromRoot(self.exe_path); - - %%io.stderr.printf("Test {}/{} {}...", self.test_index+1, self.context.test_index, self.name); - - var child = os.ChildProcess.spawn(full_exe_path, [][]u8{}, &b.env_map, - StdIo.Ignore, StdIo.Pipe, StdIo.Pipe, b.allocator) %% |err| - { - debug.panic("Unable to spawn {}: {}\n", full_exe_path, @errorName(err)); - }; - - const term = child.wait() %% |err| { - debug.panic("Unable to spawn {}: {}\n", full_exe_path, @errorName(err)); - }; - switch (term) { - Term.Clean => |code| { - if (code != 0) { - %%io.stderr.printf("Process {} exited with error code {}\n", full_exe_path, code); - return error.TestFailed; - } - }, - else => { - %%io.stderr.printf("Process {} terminated unexpectedly\n", full_exe_path); - return error.TestFailed; - }, - }; - - var stdout = %%Buffer0.initEmpty(b.allocator); - var stderr = %%Buffer0.initEmpty(b.allocator); - - %%(??child.stdout).readAll(&stdout); - %%(??child.stderr).readAll(&stderr); - - if (!mem.eql(u8, self.expected_output, stdout.toSliceConst())) { - %%io.stderr.printf( - \\ - \\========= Expected this output: ========= - \\{} - \\================================================ - \\{} - \\ - , self.expected_output, stdout.toSliceConst()); - return error.TestFailed; - } - %%io.stderr.printf("OK\n"); - } -}; - |
