aboutsummaryrefslogtreecommitdiff
path: root/test/src/RunTranslatedC.zig
blob: 59f87c6a8298d536e63a71b0ebef5afcb7fb944d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
b: *std.Build,
step: *std.Build.Step,
test_index: usize,
test_filters: []const []const u8,
target: std.Build.ResolvedTarget,

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 {s}", .{case.name}) catch unreachable;
    for (self.test_filters) |test_filter| {
        if (mem.indexOf(u8, annotated_case_name, test_filter)) |_| break;
    } else if (self.test_filters.len > 0) return;

    const write_src = b.addWriteFiles();
    const first_case = case.sources.items[0];
    const root_source_file = write_src.add(first_case.filename, first_case.source);
    for (case.sources.items[1..]) |src_file| {
        _ = write_src.add(src_file.filename, src_file.source);
    }
    const translate_c = b.addTranslateC(.{
        .root_source_file = root_source_file,
        .target = b.graph.host,
        .optimize = .Debug,
    });

    translate_c.step.name = b.fmt("{s} translate-c", .{annotated_case_name});
    const exe = translate_c.addExecutable(.{});
    exe.step.name = b.fmt("{s} build-exe", .{annotated_case_name});
    exe.linkLibC();
    const run = b.addRunArtifact(exe);
    run.step.name = b.fmt("{s} run", .{annotated_case_name});
    if (!case.allow_warnings) {
        run.expectStdErrEqual("");
    }
    run.expectStdOutEqual(case.expected_stdout);
    run.skip_foreign_checks = true;

    self.step.dependOn(&run.step);
}

const RunTranslatedCContext = @This();
const std = @import("std");
const ArrayList = std.ArrayList;
const fmt = std.fmt;
const mem = std.mem;
const fs = std.fs;