Test values passed to handler
This commit is contained in:
parent
d4d3226a39
commit
b24d96233d
1 changed files with 67 additions and 33 deletions
100
src/router.zig
100
src/router.zig
|
@ -35,22 +35,31 @@ const _tests = struct {
|
|||
_ = _uniq;
|
||||
return struct {
|
||||
var calls: u32 = 0;
|
||||
var last_arg: ?Context = null;
|
||||
|
||||
fn func(ctx: Context) void {
|
||||
calls += 1;
|
||||
last_arg = ctx;
|
||||
return next(ctx);
|
||||
}
|
||||
|
||||
fn expectCalled(times: u32) !void {
|
||||
return std.testing.expectEqual(times, calls);
|
||||
fn expectCalledOnceWith(expected: Context) !void {
|
||||
try std.testing.expectEqual(@as(u32, 1), calls);
|
||||
try std.testing.expectEqual(expected, last_arg.?);
|
||||
}
|
||||
|
||||
fn expectNotCalled() !void {
|
||||
try std.testing.expectEqual(@as(u32, 0), calls);
|
||||
}
|
||||
|
||||
fn reset() void {
|
||||
calls = 0;
|
||||
last_arg = null;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
const Context = struct {};
|
||||
const Context = u32;
|
||||
fn dummyHandler(_: Context) void {}
|
||||
|
||||
test "Router(T).dispatch" {
|
||||
|
@ -69,39 +78,64 @@ const _tests = struct {
|
|||
};
|
||||
|
||||
const router = Router(Context){ .routes = &routes, .route_404 = mock_404.func };
|
||||
router.dispatch(.GET, "/a", .{});
|
||||
try mock_a.expectCalled(1);
|
||||
try mock_a_post.expectCalled(0);
|
||||
try mock_b.expectCalled(0);
|
||||
try mock_c.expectCalled(0);
|
||||
try mock_404.expectCalled(0);
|
||||
router.dispatch(.GET, "/a", 10);
|
||||
try mock_a.expectCalledOnceWith(10);
|
||||
try mock_a_post.expectNotCalled();
|
||||
try mock_b.expectNotCalled();
|
||||
try mock_c.expectNotCalled();
|
||||
try mock_404.expectNotCalled();
|
||||
mock_a.reset();
|
||||
mock_a_post.reset();
|
||||
mock_b.reset();
|
||||
mock_c.reset();
|
||||
mock_404.reset();
|
||||
|
||||
router.dispatch(.GET, "/b", .{});
|
||||
try mock_a.expectCalled(1);
|
||||
try mock_a_post.expectCalled(0);
|
||||
try mock_b.expectCalled(1);
|
||||
try mock_c.expectCalled(0);
|
||||
try mock_404.expectCalled(0);
|
||||
router.dispatch(.GET, "/b", 10);
|
||||
try mock_a.expectNotCalled();
|
||||
try mock_a_post.expectNotCalled();
|
||||
try mock_b.expectCalledOnceWith(10);
|
||||
try mock_c.expectNotCalled();
|
||||
try mock_404.expectNotCalled();
|
||||
mock_a.reset();
|
||||
mock_a_post.reset();
|
||||
mock_b.reset();
|
||||
mock_c.reset();
|
||||
mock_404.reset();
|
||||
|
||||
router.dispatch(.GET, "/a", .{});
|
||||
try mock_a.expectCalled(2);
|
||||
try mock_a_post.expectCalled(0);
|
||||
try mock_b.expectCalled(1);
|
||||
try mock_c.expectCalled(0);
|
||||
try mock_404.expectCalled(0);
|
||||
router.dispatch(.GET, "/a", 3);
|
||||
try mock_a.expectCalledOnceWith(3);
|
||||
try mock_a_post.expectNotCalled();
|
||||
try mock_b.expectNotCalled();
|
||||
try mock_c.expectNotCalled();
|
||||
try mock_404.expectNotCalled();
|
||||
mock_a.reset();
|
||||
mock_a_post.reset();
|
||||
mock_b.reset();
|
||||
mock_c.reset();
|
||||
mock_404.reset();
|
||||
|
||||
router.dispatch(.GET, "/abcd", .{});
|
||||
try mock_a.expectCalled(2);
|
||||
try mock_a_post.expectCalled(0);
|
||||
try mock_b.expectCalled(1);
|
||||
try mock_c.expectCalled(0);
|
||||
try mock_404.expectCalled(1);
|
||||
router.dispatch(.GET, "/abcd", 11);
|
||||
try mock_a.expectNotCalled();
|
||||
try mock_a_post.expectNotCalled();
|
||||
try mock_b.expectNotCalled();
|
||||
try mock_c.expectNotCalled();
|
||||
try mock_404.expectCalledOnceWith(11);
|
||||
mock_a.reset();
|
||||
mock_a_post.reset();
|
||||
mock_b.reset();
|
||||
mock_c.reset();
|
||||
mock_404.reset();
|
||||
|
||||
router.dispatch(.POST, "/a", .{});
|
||||
try mock_a.expectCalled(2);
|
||||
try mock_a_post.expectCalled(1);
|
||||
try mock_b.expectCalled(1);
|
||||
try mock_c.expectCalled(0);
|
||||
try mock_404.expectCalled(1);
|
||||
router.dispatch(.POST, "/a", 0);
|
||||
try mock_a.expectNotCalled();
|
||||
try mock_a_post.expectCalledOnceWith(0);
|
||||
try mock_b.expectNotCalled();
|
||||
try mock_c.expectNotCalled();
|
||||
try mock_404.expectNotCalled();
|
||||
mock_a.reset();
|
||||
mock_a_post.reset();
|
||||
mock_b.reset();
|
||||
mock_c.reset();
|
||||
mock_404.reset();
|
||||
}
|
||||
};
|
||||
|
|
Loading…
Reference in a new issue