Today I ran into an issue where I had issues trying to get code to be tested. I’m trying to understand how I’m “supposed” to do this.
Project Structure
src/
|- main.zig
|- root.zig
|- sqlite/
|- varint.zig
I have sqlite setup where it is essentially a sub-module of the root.zig module. I have varint expose public structs with public functions, and a test is established in the module:
varint.zig (Yes none of this should compile)
/// A signed twos-complement variable integer, BigEndian
const std = @import("std");
const testing = std.testing;
pub const VarInt = struct {
const MAX_SIZE = 9;
value: i64,
/// Represent the varint in it's binary form
pub fn serialize(self: VarInt) [VarInt.MAX_SIZE]u8 {
const is_negative = self.value < 0;
_ = is_negative;
}
/// Read in the binary representation of a varint
fn deserialize() VarInt {}
};
test "serialize" {
const v = VarInt{ .value = 0 };
try testing.expectEqual([9]u8{ 0, 0, 1, 0, 0, 0, 0, 0, 0, 0 }, v.serialize());
}
Now when i run my tests I don’t get a compile error. I realize it was because my sub-module wasn’t referenced. But when i added const varint = @import("sqlite/varint.zig")
to my root.zig
folder, it still didn’t run the tests. I had to make it public. But I don’t want varint to be public outside of my project. I just want it to be used within the project.
I got around this by doing the following in the root.zig
test {
const x = VarInt{1};
_ = x;
}
My Question
I Understand that the test runner/compiler don’t want to compile things that are unused. That makes sense to me. However sometimes I’m writing code that
I haven’t integrated yet into my project, and still want to run those tests. When I program I usually just have a watchexec
process running that runs tests on save, but it isn’t helpful if what I’m working on isn’t being built/tested.
So My question is: Is there something i’m missing or not groking, to get all my test decls to be picked up and ran?