Skip to content

Commit da15965

Browse files
committed
Bindings tests
1 parent 30b1c03 commit da15965

File tree

5 files changed

+84
-10
lines changed

5 files changed

+84
-10
lines changed

build.zig

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ const std = @import("std");
33
const assert = std.debug.assert;
44

55
pub fn build(b: *std.Build) void {
6+
const optimize = b.standardOptimizeOption(.{});
7+
const target = b.standardTargetOptions(.{});
8+
69
const zsdl2_module = b.addModule("zsdl2", .{
710
.root_source_file = b.path("src/sdl2.zig"),
811
});
@@ -25,6 +28,42 @@ pub fn build(b: *std.Build) void {
2528
_ = b.addModule("zsdl3", .{
2629
.root_source_file = b.path("src/sdl3.zig"),
2730
});
31+
32+
const test_step = b.step("test", "Run bindings tests");
33+
{ // Test SDL2 bindings
34+
const zsdl2_tests = addTests(test_step, target, optimize, "zsdl2-tests", "src/sdl2.zig");
35+
link_SDL2(zsdl2_tests);
36+
}
37+
{ // Test SDL2_ttf bindings
38+
const zsdl2_ttf_tests = addTestsAux(
39+
test_step,
40+
target,
41+
optimize,
42+
"zsdl2_ttf-tests",
43+
"src/sdl2_ttf.zig",
44+
"zsdl2",
45+
zsdl2_module,
46+
);
47+
link_SDL2(zsdl2_ttf_tests);
48+
link_SDL2_ttf(zsdl2_ttf_tests);
49+
}
50+
{ // Test SDL2_image bindings
51+
const zsdl2_image_tests = addTestsAux(
52+
test_step,
53+
target,
54+
optimize,
55+
"zsdl2_ttf-image",
56+
"src/sdl2_image.zig",
57+
"zsdl2",
58+
zsdl2_module,
59+
);
60+
link_SDL2(zsdl2_image_tests);
61+
link_SDL2_image(zsdl2_image_tests);
62+
}
63+
{ // Test SDL3 bindings
64+
const zsdl3_tests = addTests(test_step, target, optimize, "zsdl3-tests", "src/sdl3.zig");
65+
link_SDL3(zsdl3_tests);
66+
}
2867
}
2968

3069
pub fn link_SDL2(compile_step: *std.Build.Step.Compile) void {
@@ -279,3 +318,36 @@ pub const prebuilt = struct {
279318
return null;
280319
}
281320
};
321+
322+
fn addTests(
323+
test_step: *std.Build.Step,
324+
target: std.Build.ResolvedTarget,
325+
optimize: std.builtin.OptimizeMode,
326+
name: []const u8,
327+
root_src_path: []const u8,
328+
) *std.Build.Step.Compile {
329+
const b = test_step.owner;
330+
const tests = b.addTest(.{
331+
.name = name,
332+
.root_source_file = b.path(root_src_path),
333+
.target = target,
334+
.optimize = optimize,
335+
});
336+
b.installArtifact(tests);
337+
test_step.dependOn(&b.addRunArtifact(tests).step);
338+
return tests;
339+
}
340+
341+
fn addTestsAux(
342+
test_step: *std.Build.Step,
343+
target: std.Build.ResolvedTarget,
344+
optimize: std.builtin.OptimizeMode,
345+
name: []const u8,
346+
root_src_path: []const u8,
347+
parent_module_name: []const u8,
348+
parent_module: *std.Build.Module,
349+
) *std.Build.Step.Compile {
350+
const tests = addTests(test_step, target, optimize, name, root_src_path);
351+
tests.root_module.addImport(parent_module_name, parent_module);
352+
return tests;
353+
}

src/sdl2.zig

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@ const builtin = @import("builtin");
22
const std = @import("std");
33
const assert = std.debug.assert;
44

5-
comptime {
6-
_ = std.testing.refAllDecls(@This());
7-
}
8-
95
const sdl2 = @This();
106

7+
test {
8+
_ = std.testing.refAllDeclsRecursive(sdl2);
9+
}
10+
1111
//--------------------------------------------------------------------------------------------------
1212
//
1313
// Initialzation and Shutdown

src/sdl2_image.zig

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
const std = @import("std");
22
const sdl = @import("zsdl2");
33

4-
comptime {
5-
_ = std.testing.refAllDecls(@This());
4+
test {
5+
_ = std.testing.refAllDeclsRecursive(@This());
66
}
77

88
/// Load an image from a filesystem path into a software surface.

src/sdl2_ttf.zig

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
const std = @import("std");
22
const sdl = @import("zsdl2");
33

4-
comptime {
5-
_ = std.testing.refAllDecls(@This());
4+
test {
5+
_ = std.testing.refAllDeclsRecursive(@This());
66
}
77

88
pub fn init() !void {

src/sdl3.zig

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@ const builtin = @import("builtin");
22
const std = @import("std");
33
const assert = std.debug.assert;
44

5-
comptime {
6-
_ = std.testing.refAllDecls(@This());
5+
const sdl3 = @This();
6+
7+
test {
8+
_ = std.testing.refAllDeclsRecursive(sdl3);
79
}
810

911
//--------------------------------------------------------------------------------------------------

0 commit comments

Comments
 (0)