Skip to content

Commit

Permalink
Start implementing a test runner.
Browse files Browse the repository at this point in the history
  • Loading branch information
Makosai committed Aug 16, 2024
1 parent ad040bb commit bdec112
Show file tree
Hide file tree
Showing 5 changed files with 374 additions and 35 deletions.
69 changes: 46 additions & 23 deletions build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,29 @@ pub fn build(b: *std.Build) void {
// set a preferred release mode, allowing the user to decide how to optimize.
const optimize = b.standardOptimizeOption(.{});

const lib = b.addStaticLibrary(.{
.name = "Sustenet",
// In this case the main source file is merely a path, however, in more
// complicated build scripts, this could be a generated file.
.root_source_file = b.path("src/root.zig"),
.target = target,
.optimize = optimize,
const main_module = b.addModule("main", .{
.root_source_file = b.path("src/main.zig"),
});

// This declares intent for the library to be installed into the standard
// location when the user invokes the "install" step (the default step when
// running `zig build`).
b.installArtifact(lib);
{
const options = b.addOptions();
options.addOption(bool, "main_blocking", false);
main_module.addOptions("build", options);
}

// const lib = b.addStaticLibrary(.{
// .name = "Sustenet",
// // In this case the main source file is merely a path, however, in more
// // complicated build scripts, this could be a generated file.
// .root_source_file = b.path("src/root.zig"),
// .target = target,
// .optimize = optimize,
// });

// // This declares intent for the library to be installed into the standard
// // location when the user invokes the "install" step (the default step when
// // running `zig build`).
// b.installArtifact(lib);

const exe = b.addExecutable(.{
.name = "Sustenet",
Expand Down Expand Up @@ -66,26 +76,39 @@ pub fn build(b: *std.Build) void {

// Creates a step for unit testing. This only builds the test executable
// but does not run it.
const lib_unit_tests = b.addTest(.{
.root_source_file = b.path("src/root.zig"),
.target = target,
.optimize = optimize,
});
// const lib_unit_tests = b.addTest(.{
// .root_source_file = b.path("src/root.zig"),
// .target = target,
// .optimize = optimize,
// });

const run_lib_unit_tests = b.addRunArtifact(lib_unit_tests);
// const run_lib_unit_tests = b.addRunArtifact(lib_unit_tests);

const exe_unit_tests = b.addTest(.{
.root_source_file = b.path("src/main.zig"),
// const exe_unit_tests = b.addTest(.{
// .root_source_file = b.path("src/main.zig"),
// .target = target,
// .optimize = optimize,
// });

const tests = b.addTest(.{
.target = target,
.optimize = optimize,
.root_source_file = b.path("src/main.zig"),
.test_runner = b.path("test_runner.zig"),
});

const run_exe_unit_tests = b.addRunArtifact(exe_unit_tests);
const run_tests = b.addRunArtifact(tests);
run_tests.has_side_effects = true;

const test_step = b.step("test", "Run tests");
test_step.dependOn(&tests.step);

// const run_exe_unit_tests = b.addRunArtifact(exe_unit_tests);

// Similar to creating the run step earlier, this exposes a `test` step to
// the `zig build --help` menu, providing a way for the user to request
// running the unit tests.
const test_step = b.step("test", "Run unit tests");
test_step.dependOn(&run_lib_unit_tests.step);
test_step.dependOn(&run_exe_unit_tests.step);
// const test_step = b.step("test", "Run unit tests");
// test_step.dependOn(&run_lib_unit_tests.step);
// test_step.dependOn(&run_exe_unit_tests.step);
}
23 changes: 23 additions & 0 deletions src/Transport/BaseClient.zig
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
const std = @import("std");
const net = std.net;
const print = std.debug.print;
const testing = std.testing; // Add import for testing package
const BaseClient = @This();

const bufferSize = 4028;
Expand Down Expand Up @@ -39,3 +40,25 @@ pub fn send(self: *BaseClient, data: []const u8) !void {
const size = try writer.write(data);
print("Sending '{s}' to peer, total written: {d} bytes\n", .{ data, size });
}

test "setup client" {
const client = BaseClient.new(4337);
try client.connect();

try testing.expect(client.id == 1); // Use expect from testing package
}

test "send data" {
const client = BaseClient.new(4337);
try client.connect();
try client.send("hello ziggy!");

try testing.expect(client.stream != null);
}

test "send data with null stream" {
const client = BaseClient.new(4337);
try client.send("hello ziggy!");

try testing.expect(client.stream == null);
}
8 changes: 6 additions & 2 deletions src/main.zig
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ const clients = sustenet.clients;
const BaseServer = transport.BaseServer;

pub var client_list: std.ArrayList(clients.Client) = undefined;
pub var cluster = undefined;
pub var master = undefined;
// pub var cluster = undefined;
// pub var master = undefined;

fn entrypoint() !void {
// Get allocator
Expand Down Expand Up @@ -71,3 +71,7 @@ test "simple test" {
try list.append(42);
try std.testing.expectEqual(@as(i32, 42), list.pop());
}

test "all" {
std.testing.refAllDecls(@This());
}
10 changes: 0 additions & 10 deletions src/root.zig

This file was deleted.

Loading

0 comments on commit bdec112

Please sign in to comment.