Is this the best way to use expectEqual?

fn add(a: i64, b: i64) i64 {
    return a + b;
}

test "basic test" {
    try std.testing.expectEqual(@as(i64, 10), add(3, 7));
}

Without the @as the test doesn’t even compile:

./src/main.zig:12:40: error: expected type 'comptime_int', found 'i64'
    try std.testing.expectEqual(10, add(3, 7));

Having to use @as seems makes the test less readable. It seems to me inferring the type of expected from the type of actual would make more sense.

You are viewing a single thread.
View all comments
2 points

I am not sure if there is already an issue for this. But I usually just switch expected with actual in the arguments. As you mentioned, reading the failing test output is then harder obviously.

permalink
report
reply

Community stats

  • 1

    Monthly active users

  • 3

    Posts

  • 4

    Comments

Community moderators