Skip to content

Commit

Permalink
Add tests to ipv4 parsing
Browse files Browse the repository at this point in the history
Signed-off-by: emneo <emneo@kreog.com>
  • Loading branch information
emneo-dev committed Sep 8, 2024
1 parent d962ba1 commit 80cf399
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 0 deletions.
2 changes: 2 additions & 0 deletions build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,8 @@ pub fn build(b: *std.Build) !void {
.target = std_target,
.optimize = optimize,
});
unit_tests.root_module.addOptions("build_config", options);
unit_tests.root_module.addImport("ini", pkg.module("ini"));

const run_unit_tests = b.addRunArtifact(unit_tests);
test_step.dependOn(&run_unit_tests.step);
Expand Down
28 changes: 28 additions & 0 deletions src/config.zig
Original file line number Diff line number Diff line change
Expand Up @@ -138,3 +138,31 @@ pub fn parse(filepath: []const u8, allocator: std.mem.Allocator) !config {
try map_value(f, &tmp);
return map_opt_struct_to_struct(tmp_config, config, &tmp);
}

test "ipv4 loopback default port" {
const parsed = try parse_full_ip("127.0.0.1:2105");
try std.testing.expectEqual(parsed.getPort(), 2105);
try std.testing.expectEqual(parsed.in.sa.addr, 0x100007f);
}

test "ipv4 loopback random port" {
const parsed = try parse_full_ip("127.0.0.1:0");
try std.testing.expectEqual(parsed.getPort(), 0);
try std.testing.expectEqual(parsed.in.sa.addr, 0x100007f);
}

test "ipv4 wildcard default port" {
const parsed = try parse_full_ip("0.0.0.0:2105");
try std.testing.expectEqual(parsed.getPort(), 2105);
try std.testing.expectEqual(parsed.in.sa.addr, 0);
}

test "ipv4 invalid no port" {
const parsed = parse_full_ip("127.0.0.1");
try std.testing.expectError(error.BadKey, parsed);
}

test "ipv4 invalid no ip" {
const parsed = parse_full_ip("2105");
try std.testing.expectError(error.BadKey, parsed);
}
4 changes: 4 additions & 0 deletions src/main.zig
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,7 @@ pub fn main() !void {
const conf = try config.parse("config.ini", allocator);
std.debug.print("Got conf: {any}\n", .{conf});
}

test {
std.testing.refAllDecls(@This());
}

0 comments on commit 80cf399

Please sign in to comment.