Zig build completes but zig build test shows errors

When I run this code that I’m working on, zig build will complete successfully with no warnings or errors, but when I run zig build test then it reports errors in the library (not the test).

I mean, there are definitely errors (this is my first zig project), but it makes no sense to me why they’re not reported during the build.

My background is mostly C.

I’m not sure if this is right, but could it be that your tests are invoking code that isn’t actually used or exported in the library and so is not complied? Hard to say without the error.

1 Like

Hi @Calder-Ty … so the code is here

The current error is

└─ run test
   └─ zig test Debug native 1 errors
src/root.zig:40:37: error: expected type 'usize', found 'root.Card'
            const temp = deck.cards[i];
src/root.zig:10:14: note: struct declared here
const Card = struct {

Hi @andy5995, welcome to ziggit :slight_smile:

In the following line i is actually a Card

const i = deck.cards[item];

When you use i in:

const temp = deck.cards[i];

compiler expects an index of type usize.

1 Like

Try to test each function.

test "Deck.init" {
    const deck = Deck.init();
    try std.testing.expectEqual(Suit.Clubs, deck.cards[0].suit);
    try std.testing.expectEqual(1, deck.cards[0].value);
    try std.testing.expectEqual(Suit.Clubs, deck.cards[1].suit);
    try std.testing.expectEqual(2, deck.cards[1].value);
    try std.testing.expectEqual(Suit.Diamonds, deck.cards[13].suit);
    try std.testing.expectEqual(1, deck.cards[13].value);

An init that pass these tests follow.

    pub fn init() Deck {
        var deck: Deck = .{ .cards = undefined, .top = 0 };
        for (0..3) |suit| {
            for (0..12) |i| {
                deck.cards[suit * 13 + i] = Card{ .suit = @enumFromInt(suit), .value = @as(u4,@intCast(i+1)) };
        return deck;
1 Like

Excuse me, I forgot to mention, on Manjaro, I’m using the zig-dev-bin package (0.12.0-dev.3192+e2cbbd0c2) and the GitHub CI workflow the version is zig-linux-x86_64-0.12.0-dev.3193+4ba4f94c9

Thanks for the tips @dimdin

1 Like

This is because of lazy evaluation. My approach to this is:

const SomeStruct = struct {
    test "foo" {}

test {
    _ = SomeStruct;

Alternatively, you may use std,testing.refAllDeclsRecursive.

1 Like