ryupold / raylib.zig

Idiomatic Zig bindings for raylib utilizing raylib_parser
MIT License
222 stars 36 forks source link

Instruction on how to use the zig test #33

Closed apotema closed 10 months ago

apotema commented 11 months ago

Currently, you can't test the code that uses this lib.

zig test src/some_test_that_uses_raylib.zig


                       ^~~~~~~~
referenced by:
    QuadTree: src/some_test_that_uses_raylib.zig:13:15
    QuadTree: src/some_test_that_uses_raylib.zig:11:22
    remaining reference traces hidden; use '-freference-trace' to see all reference trac```

Some kind of instruction would be a tremendous help.
ryupold commented 11 months ago

I think your example output was cropped somehow, at least I'm missing the part where you include the library. You can consolidate this example project to see how to use the bindings.

apotema commented 11 months ago

Let me see if I can put a better example.

This is the file foo.zig

const raylib = @import("raylib");
const std = @abs("std");

test {
    var box = raylib.Rectangle{ .x = 0, .y = 0, .width = 100, .height = 100 };
    std.testing.expect(box.x == 0);
}

When I run zig test src/foo.zig, I receive the error

src/foo.zig:1:24: error: no module named 'raylib' available within module root
const raylib = @import("raylib");
                       ^~~~~~~~
referenced by:
    test_0: src/foo.zig:5:15
    remaining reference traces hidden; use '-freference-trace' to see all reference traces

I was looking for a way to make this work to test any file requiring Raylib.

=================================

I have been doing this now, but it could be a cleaner solution, and it does not show the test results, just if it falis.

const internal_test = b.addTest(.{
        .root_source_file = std.build.FileSource{ .path = "src/tests.zig" },
        .optimize = optimize,
        .target = target,
        .name = "internal_tests",
    });
internal_test.addAnonymousModule("raylib", .{ .source_file = .{ .path = "tests/fake_raylib.zig" } });

const test_step = b.step("test", "Run unit tests");
const run_unit_tests = b.addRunArtifact(internal_test);
test_step.dependOn(&run_unit_tests.step);
ryupold commented 11 months ago

Ah, now I understand what you are trying to do :)

You can theoretically link raylib to the test compile step:

const internal_test = b.addTest(.{
    .root_source_file = .{ .path = "src/tests.zig" },
    .target = target,
    .optimize = mode,
});
const raylib = @import("src/raylib/build.zig");
raylib.addTo(b, internal_test, internal_test.target, internal_test.optimize);
raylib.linkSystemDependencies(internal_test);
const test_step = b.step("test", "Run unit tests");
test_step.dependOn(&internal_test.step);

This compiles but I have the the problem that the tests are not getting executed

apotema commented 11 months ago

Hi @ryupold Thanks for this new function, linkSystemDependencies. It solved the issue for me.

To see all tests running, you must pass extra parameters to the build test. zig build test --summary all

image