message: play around with unit testing

I think I am probably going to move unit tests to a separate
directory/file structure. This will allow me to add a bunch of utility
functions that don't get analyzed for the library compilation and also
avoid testing-only imports in the main modules.
This commit is contained in:
torque 2023-08-15 22:22:36 -07:00
parent 970d274593
commit 3ced6db69d
Signed by: torque
SSH Key Fingerprint: SHA256:nCrXefBNo6EbjNSQhv0nXmEg/VuNq3sMF5b8zETw3Tk
3 changed files with 66 additions and 0 deletions

View File

@ -33,6 +33,8 @@ pub fn build(b: *std.Build) void {
.optimize = optimize,
});
main_tests.linkLibrary(nats_c);
const run_main_tests = b.addRunArtifact(main_tests);
const test_step = b.step("test", "Run tests");
test_step.dependOn(&run_main_tests.step);

View File

@ -113,3 +113,63 @@ pub const Message = opaque {
// NATS_EXTERN const char* stanMsg_GetData(const stanMsg *msg);
// NATS_EXTERN int stanMsg_GetDataLength(const stanMsg *msg);
// NATS_EXTERN void stanMsg_Destroy(stanMsg *msg);
test "message: create message" {
const subject = "hello";
const reply = "reply";
const data = "world";
const nats = @import("./nats.zig");
// have to initialize the library so the reference counter can correctly destroy
// objects, otherwise we segfault on trying to free the memory.
try nats.init(-1);
defer nats.deinit();
const message = try Message.create(subject, reply, data);
defer message.destroy();
const message2 = try Message.create(subject, null, data);
defer message2.destroy();
const message3 = try Message.create(subject, data, null);
defer message3.destroy();
const message4 = try Message.create(subject, null, null);
defer message4.destroy();
}
test "message: get subject" {
const nats = @import("./nats.zig");
try nats.init(-1);
defer nats.deinit();
const subject = "hello";
const message = try Message.create(subject, null, null);
defer message.destroy();
const received = message.getSubject();
try std.testing.expectEqualStrings(subject, received);
}
test "message: get reply" {
const nats = @import("./nats.zig");
try nats.init(-1);
defer nats.deinit();
const subject = "hello";
const reply = "reply";
const message = try Message.create(subject, reply, null);
defer message.destroy();
const received = message.getReply() orelse return error.TestUnexpectedResult;
try std.testing.expectEqualStrings(reply, received);
const message2 = try Message.create(subject, null, null);
defer message2.destroy();
const received2 = message2.getReply();
try std.testing.expect(received2 == null);
}

View File

@ -150,6 +150,10 @@ pub const Statistics = opaque {
}
};
test {
std.testing.refAllDecls(@This());
}
// NATS_EXTERN natsStatus nats_Sign(const char *encodedSeed, const char *input, unsigned char **signature, int *signatureLength);
// NATS_EXTERN natsStatus natsOptions_Create(natsOptions **newOpts);