Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

ZIG := zig
BC := $(dir $(abspath $(lastword $(MAKEFILE_LIST))))
# option test filter make unittest F="server"
F=

# OS and ARCH
kernel = $(shell uname -ms)
Expand Down Expand Up @@ -42,7 +44,7 @@ help:

# $(ZIG) commands
# ------------
.PHONY: build build-dev run run-release shell test bench download-zig wpt
.PHONY: build build-dev run run-release shell test bench download-zig wpt unittest

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

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

unittest:
@TEST_FILTER='${F}' $(ZIG) build unittest -freference-trace --summary all

# Install and build required dependencies commands
# ------------
.PHONY: install-submodule
Expand Down
25 changes: 23 additions & 2 deletions build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,8 @@ pub fn build(b: *std.Build) !void {

// compile
const tests = b.addTest(.{
.root_source_file = b.path("src/run_tests.zig"),
.test_runner = b.path("src/test_runner.zig"),
.root_source_file = b.path("src/main_tests.zig"),
.test_runner = b.path("src/main_tests.zig"),
.target = target,
.optimize = mode,
});
Expand All @@ -119,6 +119,27 @@ pub fn build(b: *std.Build) !void {
const test_step = b.step("test", "Run unit tests");
test_step.dependOn(&run_tests.step);

// unittest
// ----

// compile
const unit_tests = b.addTest(.{
.root_source_file = b.path("src/unit_tests.zig"),
.test_runner = b.path("src/unit_tests.zig"),
.target = target,
.optimize = mode,
});
try common(b, unit_tests, options);

const run_unit_tests = b.addRunArtifact(unit_tests);
if (b.args) |args| {
run_unit_tests.addArgs(args);
}

// step
const unit_test_step = b.step("unittest", "Run unit tests");
unit_test_step.dependOn(&run_unit_tests.step);

// wpt
// -----

Expand Down
12 changes: 9 additions & 3 deletions src/browser/loader.zig
Original file line number Diff line number Diff line change
Expand Up @@ -79,13 +79,19 @@ pub const Loader = struct {
}
};

test "basic url get" {
test "loader: get" {
const alloc = std.testing.allocator;
var loader = Loader.init(alloc);
defer loader.deinit();

var result = try loader.get(alloc, "https://en.wikipedia.org/wiki/Main_Page");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would like to keep a test using TLS also...
But I agree having something locally is really better.

Maybe we could imagine an optional slower test suite for these kind of test we could run in CI.
But It's not a blocker for now.

const uri = try std.Uri.parse("http://localhost:9582/loader");
var result = try loader.get(alloc, uri);
defer result.deinit();

try std.testing.expect(result.req.response.status == std.http.Status.ok);
try std.testing.expectEqual(.ok, result.req.response.status);

var res: [128]u8 = undefined;
const size = try result.req.readAll(&res);
try std.testing.expectEqual(6, size);
try std.testing.expectEqualStrings("Hello!", res[0..6]);
}
8 changes: 7 additions & 1 deletion src/run_tests.zig → src/main_tests.zig
Original file line number Diff line number Diff line change
Expand Up @@ -223,8 +223,14 @@ pub fn main() !void {
try parser.init();
defer parser.deinit();

std.testing.allocator_instance = .{};
try test_fn.func();
std.debug.print("{s}\tOK\n", .{test_fn.name});

if (std.testing.allocator_instance.deinit() == .leak) {
std.debug.print("======Memory Leak: {s}======\n", .{test_fn.name});
} else {
std.debug.print("{s}\tOK\n", .{test_fn.name});
}
}
}
}
Expand Down
41 changes: 24 additions & 17 deletions src/storage/storage.zig
Original file line number Diff line number Diff line change
Expand Up @@ -149,20 +149,22 @@ pub const Bottle = struct {
}

pub fn _setItem(self: *Bottle, k: []const u8, v: []const u8) !void {
const old = self.map.get(k);
if (old != null and std.mem.eql(u8, v, old.?)) return;

// owns k and v by copying them.
const kk = try self.alloc.dupe(u8, k);
errdefer self.alloc.free(kk);
const vv = try self.alloc.dupe(u8, v);
errdefer self.alloc.free(vv);

self.map.put(self.alloc, kk, vv) catch |e| {
const gop = self.map.getOrPut(self.alloc, k) catch |e| {
log.debug("set item: {any}", .{e});
return DOMError.QuotaExceeded;
};

if (gop.found_existing == false) {
gop.key_ptr.* = try self.alloc.dupe(u8, k);
gop.value_ptr.* = try self.alloc.dupe(u8, v);
return;
}

if (std.mem.eql(u8, v, gop.value_ptr.*) == false) {
self.alloc.free(gop.value_ptr.*);
gop.value_ptr.* = try self.alloc.dupe(u8, v);
}

// > Broadcast this with key, oldValue, and value.
// https://html.spec.whatwg.org/multipage/webstorage.html#the-storageevent-interface
//
Expand All @@ -175,8 +177,10 @@ pub const Bottle = struct {
}

pub fn _removeItem(self: *Bottle, k: []const u8) !void {
const old = self.map.fetchRemove(k);
if (old == null) return;
if (self.map.fetchRemove(k)) |kv| {
self.alloc.free(kv.key);
self.alloc.free(kv.value);
}

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

try std.testing.expect(0 == bottle.get_length());
try std.testing.expect(null == bottle._getItem("foo"));
try std.testing.expectEqual(0, bottle.get_length());
try std.testing.expectEqual(null, bottle._getItem("foo"));

try bottle._setItem("foo", "bar");
try std.testing.expect(std.mem.eql(u8, "bar", bottle._getItem("foo").?));
try std.testing.expectEqualStrings("bar", bottle._getItem("foo").?);

try bottle._setItem("foo", "other");
try std.testing.expectEqualStrings("other", bottle._getItem("foo").?);

try bottle._removeItem("foo");

try std.testing.expect(0 == bottle.get_length());
try std.testing.expect(null == bottle._getItem("foo"));
try std.testing.expectEqual(0, bottle.get_length());
try std.testing.expectEqual(null, bottle._getItem("foo"));
}
29 changes: 0 additions & 29 deletions src/test_runner.zig

This file was deleted.

Loading