master
1b: *std.Build,
2step: *std.Build.Step,
3test_index: usize,
4test_filters: []const []const u8,
5target: std.Build.ResolvedTarget,
6
7const TestCase = struct {
8 name: []const u8,
9 sources: std.array_list.Managed(SourceFile),
10 expected_stdout: []const u8,
11 allow_warnings: bool,
12
13 const SourceFile = struct {
14 filename: []const u8,
15 source: []const u8,
16 };
17
18 pub fn addSourceFile(self: *TestCase, filename: []const u8, source: []const u8) void {
19 self.sources.append(SourceFile{
20 .filename = filename,
21 .source = source,
22 }) catch unreachable;
23 }
24};
25
26pub fn create(
27 self: *RunTranslatedCContext,
28 allow_warnings: bool,
29 filename: []const u8,
30 name: []const u8,
31 source: []const u8,
32 expected_stdout: []const u8,
33) *TestCase {
34 const tc = self.b.allocator.create(TestCase) catch unreachable;
35 tc.* = TestCase{
36 .name = name,
37 .sources = std.array_list.Managed(TestCase.SourceFile).init(self.b.allocator),
38 .expected_stdout = expected_stdout,
39 .allow_warnings = allow_warnings,
40 };
41
42 tc.addSourceFile(filename, source);
43 return tc;
44}
45
46pub fn add(
47 self: *RunTranslatedCContext,
48 name: []const u8,
49 source: []const u8,
50 expected_stdout: []const u8,
51) void {
52 const tc = self.create(false, "source.c", name, source, expected_stdout);
53 self.addCase(tc);
54}
55
56pub fn addAllowWarnings(
57 self: *RunTranslatedCContext,
58 name: []const u8,
59 source: []const u8,
60 expected_stdout: []const u8,
61) void {
62 const tc = self.create(true, "source.c", name, source, expected_stdout);
63 self.addCase(tc);
64}
65
66pub fn addCase(self: *RunTranslatedCContext, case: *const TestCase) void {
67 const b = self.b;
68
69 const annotated_case_name = fmt.allocPrint(self.b.allocator, "run-translated-c {s}", .{case.name}) catch unreachable;
70 for (self.test_filters) |test_filter| {
71 if (mem.indexOf(u8, annotated_case_name, test_filter)) |_| break;
72 } else if (self.test_filters.len > 0) return;
73
74 const write_src = b.addWriteFiles();
75 const first_case = case.sources.items[0];
76 const root_source_file = write_src.add(first_case.filename, first_case.source);
77 for (case.sources.items[1..]) |src_file| {
78 _ = write_src.add(src_file.filename, src_file.source);
79 }
80 const translate_c = b.addTranslateC(.{
81 .root_source_file = root_source_file,
82 .target = b.graph.host,
83 .optimize = .Debug,
84 });
85
86 translate_c.step.name = b.fmt("{s} translate-c", .{annotated_case_name});
87 const exe = b.addExecutable(.{
88 .name = "translated_c",
89 .root_module = translate_c.createModule(),
90 });
91 exe.step.name = b.fmt("{s} build-exe", .{annotated_case_name});
92 exe.root_module.link_libc = true;
93 const run = b.addRunArtifact(exe);
94 run.step.name = b.fmt("{s} run", .{annotated_case_name});
95 if (!case.allow_warnings) {
96 run.expectStdErrEqual("");
97 }
98 run.expectStdOutEqual(case.expected_stdout);
99 run.skip_foreign_checks = true;
100
101 self.step.dependOn(&run.step);
102}
103
104const RunTranslatedCContext = @This();
105const std = @import("std");
106const fmt = std.fmt;
107const mem = std.mem;
108const fs = std.fs;