Skip to content

Commit c78b582

Browse files
Merge pull request #409 from karlseguin/unittest_build
Add a new unittest build step
2 parents 0c1a486 + d01d43e commit c78b582

File tree

9 files changed

+441
-57
lines changed

9 files changed

+441
-57
lines changed

Makefile

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33

44
ZIG := zig
55
BC := $(dir $(abspath $(lastword $(MAKEFILE_LIST))))
6+
# option test filter make unittest F="server"
7+
F=
68

79
# OS and ARCH
810
kernel = $(shell uname -ms)
@@ -42,7 +44,7 @@ help:
4244

4345
# $(ZIG) commands
4446
# ------------
45-
.PHONY: build build-dev run run-release shell test bench download-zig wpt
47+
.PHONY: build build-dev run run-release shell test bench download-zig wpt unittest
4648

4749
zig_version = $(shell grep 'recommended_zig_version = "' "vendor/zig-js-runtime/build.zig" | cut -d'"' -f2)
4850

@@ -91,6 +93,9 @@ test:
9193
@$(ZIG) build test -Dengine=v8 || (printf "\e[33mTest ERROR\e[0m\n"; exit 1;)
9294
@printf "\e[33mTest OK\e[0m\n"
9395

96+
unittest:
97+
@TEST_FILTER='${F}' $(ZIG) build unittest -freference-trace --summary all
98+
9499
# Install and build required dependencies commands
95100
# ------------
96101
.PHONY: install-submodule

build.zig

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,8 +98,8 @@ pub fn build(b: *std.Build) !void {
9898

9999
// compile
100100
const tests = b.addTest(.{
101-
.root_source_file = b.path("src/run_tests.zig"),
102-
.test_runner = b.path("src/test_runner.zig"),
101+
.root_source_file = b.path("src/main_tests.zig"),
102+
.test_runner = b.path("src/main_tests.zig"),
103103
.target = target,
104104
.optimize = mode,
105105
});
@@ -119,6 +119,27 @@ pub fn build(b: *std.Build) !void {
119119
const test_step = b.step("test", "Run unit tests");
120120
test_step.dependOn(&run_tests.step);
121121

122+
// unittest
123+
// ----
124+
125+
// compile
126+
const unit_tests = b.addTest(.{
127+
.root_source_file = b.path("src/unit_tests.zig"),
128+
.test_runner = b.path("src/unit_tests.zig"),
129+
.target = target,
130+
.optimize = mode,
131+
});
132+
try common(b, unit_tests, options);
133+
134+
const run_unit_tests = b.addRunArtifact(unit_tests);
135+
if (b.args) |args| {
136+
run_unit_tests.addArgs(args);
137+
}
138+
139+
// step
140+
const unit_test_step = b.step("unittest", "Run unit tests");
141+
unit_test_step.dependOn(&run_unit_tests.step);
142+
122143
// wpt
123144
// -----
124145

src/browser/loader.zig

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,13 +79,19 @@ pub const Loader = struct {
7979
}
8080
};
8181

82-
test "basic url get" {
82+
test "loader: get" {
8383
const alloc = std.testing.allocator;
8484
var loader = Loader.init(alloc);
8585
defer loader.deinit();
8686

87-
var result = try loader.get(alloc, "https://en.wikipedia.org/wiki/Main_Page");
87+
const uri = try std.Uri.parse("http://localhost:9582/loader");
88+
var result = try loader.get(alloc, uri);
8889
defer result.deinit();
8990

90-
try std.testing.expect(result.req.response.status == std.http.Status.ok);
91+
try std.testing.expectEqual(.ok, result.req.response.status);
92+
93+
var res: [128]u8 = undefined;
94+
const size = try result.req.readAll(&res);
95+
try std.testing.expectEqual(6, size);
96+
try std.testing.expectEqualStrings("Hello!", res[0..6]);
9197
}

src/iterator/iterator.zig

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,39 @@ pub const U32Iterator = struct {
1515
done: bool,
1616
};
1717

18-
pub fn _next(self: *U32Iterator) !Return {
18+
pub fn _next(self: *U32Iterator) Return {
1919
const i = self.index;
2020
if (i >= self.length) {
21-
return Return{
21+
return .{
2222
.value = 0,
2323
.done = true,
2424
};
2525
}
2626

27-
self.index += 1;
28-
return Return{
27+
self.index = i + 1;
28+
return .{
2929
.value = i,
3030
.done = false,
3131
};
3232
}
3333
};
34+
35+
const testing = std.testing;
36+
test "U32Iterator" {
37+
const Return = U32Iterator.Return;
38+
39+
{
40+
var it = U32Iterator{ .length = 0 };
41+
try testing.expectEqual(Return{ .value = 0, .done = true }, it._next());
42+
try testing.expectEqual(Return{ .value = 0, .done = true }, it._next());
43+
}
44+
45+
{
46+
var it = U32Iterator{ .length = 3 };
47+
try testing.expectEqual(Return{ .value = 0, .done = false }, it._next());
48+
try testing.expectEqual(Return{ .value = 1, .done = false }, it._next());
49+
try testing.expectEqual(Return{ .value = 2, .done = false }, it._next());
50+
try testing.expectEqual(Return{ .value = 0, .done = true }, it._next());
51+
try testing.expectEqual(Return{ .value = 0, .done = true }, it._next());
52+
}
53+
}

src/run_tests.zig renamed to src/main_tests.zig

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -223,8 +223,14 @@ pub fn main() !void {
223223
try parser.init();
224224
defer parser.deinit();
225225

226+
std.testing.allocator_instance = .{};
226227
try test_fn.func();
227-
std.debug.print("{s}\tOK\n", .{test_fn.name});
228+
229+
if (std.testing.allocator_instance.deinit() == .leak) {
230+
std.debug.print("======Memory Leak: {s}======\n", .{test_fn.name});
231+
} else {
232+
std.debug.print("{s}\tOK\n", .{test_fn.name});
233+
}
228234
}
229235
}
230236
}

src/storage/storage.zig

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -149,20 +149,22 @@ pub const Bottle = struct {
149149
}
150150

151151
pub fn _setItem(self: *Bottle, k: []const u8, v: []const u8) !void {
152-
const old = self.map.get(k);
153-
if (old != null and std.mem.eql(u8, v, old.?)) return;
154-
155-
// owns k and v by copying them.
156-
const kk = try self.alloc.dupe(u8, k);
157-
errdefer self.alloc.free(kk);
158-
const vv = try self.alloc.dupe(u8, v);
159-
errdefer self.alloc.free(vv);
160-
161-
self.map.put(self.alloc, kk, vv) catch |e| {
152+
const gop = self.map.getOrPut(self.alloc, k) catch |e| {
162153
log.debug("set item: {any}", .{e});
163154
return DOMError.QuotaExceeded;
164155
};
165156

157+
if (gop.found_existing == false) {
158+
gop.key_ptr.* = try self.alloc.dupe(u8, k);
159+
gop.value_ptr.* = try self.alloc.dupe(u8, v);
160+
return;
161+
}
162+
163+
if (std.mem.eql(u8, v, gop.value_ptr.*) == false) {
164+
self.alloc.free(gop.value_ptr.*);
165+
gop.value_ptr.* = try self.alloc.dupe(u8, v);
166+
}
167+
166168
// > Broadcast this with key, oldValue, and value.
167169
// https://html.spec.whatwg.org/multipage/webstorage.html#the-storageevent-interface
168170
//
@@ -175,8 +177,10 @@ pub const Bottle = struct {
175177
}
176178

177179
pub fn _removeItem(self: *Bottle, k: []const u8) !void {
178-
const old = self.map.fetchRemove(k);
179-
if (old == null) return;
180+
if (self.map.fetchRemove(k)) |kv| {
181+
self.alloc.free(kv.key);
182+
self.alloc.free(kv.value);
183+
}
180184

181185
// > Broadcast this with key, oldValue, and null.
182186
// https://html.spec.whatwg.org/multipage/webstorage.html#the-storageevent-interface
@@ -235,14 +239,17 @@ test "storage bottle" {
235239
var bottle = Bottle.init(std.testing.allocator);
236240
defer bottle.deinit();
237241

238-
try std.testing.expect(0 == bottle.get_length());
239-
try std.testing.expect(null == bottle._getItem("foo"));
242+
try std.testing.expectEqual(0, bottle.get_length());
243+
try std.testing.expectEqual(null, bottle._getItem("foo"));
240244

241245
try bottle._setItem("foo", "bar");
242-
try std.testing.expect(std.mem.eql(u8, "bar", bottle._getItem("foo").?));
246+
try std.testing.expectEqualStrings("bar", bottle._getItem("foo").?);
247+
248+
try bottle._setItem("foo", "other");
249+
try std.testing.expectEqualStrings("other", bottle._getItem("foo").?);
243250

244251
try bottle._removeItem("foo");
245252

246-
try std.testing.expect(0 == bottle.get_length());
247-
try std.testing.expect(null == bottle._getItem("foo"));
253+
try std.testing.expectEqual(0, bottle.get_length());
254+
try std.testing.expectEqual(null, bottle._getItem("foo"));
248255
}

src/test_runner.zig

Lines changed: 0 additions & 29 deletions
This file was deleted.

0 commit comments

Comments
 (0)