Compare commits

...

2 Commits

Author SHA1 Message Date
40d898a55e
tests: start building tests in separate directory
This approach seems nice, and as a bonus it makes it easier to run the
tests through the module interface rather than by importing the
sources directly, which I think is a good dog food approach.
2023-08-21 23:31:46 -07:00
ef185bc975
build: convert to being a module 2023-08-21 23:30:31 -07:00
6 changed files with 81 additions and 87 deletions

View File

@ -8,15 +8,8 @@ pub fn build(b: *std.Build) void {
const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{});
// const nats = b.addModule("nats", .{
// .source_file = .{ .path = "source/nats.zig" },
// });
const nats = b.addExecutable(.{
.name = "nats_test",
.root_source_file = .{ .path = "src/nats.zig" },
.target = target,
.optimize = optimize,
const nats = b.addModule("nats", .{
.source_file = .{ .path = "src/nats.zig" },
});
const nats_c = nats_build.nats_c_lib(
@ -24,18 +17,17 @@ pub fn build(b: *std.Build) void {
.{ .name = "nats-c", .target = target, .optimize = optimize },
);
nats.linkLibrary(nats_c);
b.installArtifact(nats);
const main_tests = b.addTest(.{
.root_source_file = .{ .path = "src/nats.zig" },
const tests = b.addTest(.{
.root_source_file = .{ .path = "tests/main.zig" },
.target = target,
.optimize = optimize,
});
main_tests.linkLibrary(nats_c);
tests.addModule("nats", nats);
tests.linkLibrary(nats_c);
const run_main_tests = b.addRunArtifact(main_tests);
b.installArtifact(tests);
const run_main_tests = b.addRunArtifact(tests);
const test_step = b.step("test", "Run tests");
test_step.dependOn(&run_main_tests.step);
}

View File

@ -19,31 +19,33 @@ pub fn nats_c_lib(
.optimize = options.optimize,
});
lib.disable_sanitize_c = true;
const cflags = [_][]const u8{
"-fno-sanitize=undefined",
};
lib.linkLibC();
lib.addCSourceFiles(&common_sources, &.{"-fno-sanitize=undefined"});
lib.addCSourceFiles(&common_sources, &cflags);
lib.addIncludePath(.{ .path = nats_src_prefix ++ "include" });
// if building with streaming support
// lib.addIncludePath(.{ .path = nats_src_prefix ++ "stan" });
// lib.addCSourceFiles(&streaming_sources, &.{"-fno-sanitize=undefined"});
// lib.addCSourceFiles(&streaming_sources, &cflags);
const tinfo = lib.target_info.target;
switch (tinfo.os.tag) {
.windows => {
lib.addCSourceFiles(&win_sources, &.{"-fno-sanitize=undefined"});
lib.addCSourceFiles(&win_sources, &cflags);
if (tinfo.abi != .msvc) {
lib.addCSourceFiles(&.{"src/win-crosshack.c"}, &.{"-fno-sanitize=undefined"});
lib.addCSourceFiles(&.{"src/win-crosshack.c"}, &cflags);
}
lib.defineCMacro("_WIN32", null);
lib.linkSystemLibrary("ws2_32");
},
.macos => {
lib.addCSourceFiles(&unix_sources, &.{"-fno-sanitize=undefined"});
lib.addCSourceFiles(&unix_sources, &cflags);
lib.defineCMacro("DARWIN", null);
},
else => {
lib.addCSourceFiles(&unix_sources, &.{"-fno-sanitize=undefined"});
lib.addCSourceFiles(&unix_sources, &cflags);
lib.defineCMacro("_GNU_SOURCE", null);
lib.defineCMacro("LINUX", null);
// may need to link pthread and rt. Not sure if those are inluded with linkLibC

View File

@ -113,63 +113,3 @@ 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

@ -158,7 +158,3 @@ pub const MessageList = opaque {
nats_c.natsMsgList_Destroy(@ptrCast(self));
}
};
test {
std.testing.refAllDecls(@This());
}

5
tests/main.zig Normal file
View File

@ -0,0 +1,5 @@
const std = @import("std");
test {
_ = @import("./message.zig");
}

59
tests/message.zig Normal file
View File

@ -0,0 +1,59 @@
const std = @import("std");
const nats = @import("nats");
// const nats = @import("../src/nats.zig");
// const message = @import("../src/message.zig");
test "message: create message" {
const subject = "hello";
const reply = "reply";
const data = "world";
// 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 nats.Message.create(subject, reply, data);
defer message.destroy();
const message2 = try nats.Message.create(subject, null, data);
defer message2.destroy();
const message3 = try nats.Message.create(subject, data, null);
defer message3.destroy();
const message4 = try nats.Message.create(subject, null, null);
defer message4.destroy();
}
test "message: get subject" {
try nats.init(-1);
defer nats.deinit();
const subject = "hello";
const message = try nats.Message.create(subject, null, null);
defer message.destroy();
const received = message.getSubject();
try std.testing.expectEqualStrings(subject, received);
}
test "message: get reply" {
try nats.init(-1);
defer nats.deinit();
const subject = "hello";
const reply = "reply";
const message = try nats.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 nats.Message.create(subject, null, null);
defer message2.destroy();
const received2 = message2.getReply();
try std.testing.expect(received2 == null);
}