Start work on new router
This commit is contained in:
parent
ab471020b7
commit
c26ca37bc0
1 changed files with 55 additions and 0 deletions
55
src/router.zig
Normal file
55
src/router.zig
Normal file
|
@ -0,0 +1,55 @@
|
||||||
|
const std = @import("std");
|
||||||
|
|
||||||
|
pub fn Router(comptime Context: type) type {
|
||||||
|
return struct {
|
||||||
|
const Self = @This();
|
||||||
|
const H = fn (Context) void;
|
||||||
|
|
||||||
|
handler: H,
|
||||||
|
|
||||||
|
pub fn dispatch(self: *const Self, ctx: Context) void {
|
||||||
|
self.handler(ctx);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
test {
|
||||||
|
_ = _tests;
|
||||||
|
}
|
||||||
|
const _tests = struct {
|
||||||
|
fn CallTracker(comptime _uniq: anytype, comptime next: fn (Context) void) type {
|
||||||
|
_ = _uniq;
|
||||||
|
return struct {
|
||||||
|
var calls: u32 = 0;
|
||||||
|
fn func(ctx: Context) void {
|
||||||
|
calls += 1;
|
||||||
|
return next(ctx);
|
||||||
|
}
|
||||||
|
|
||||||
|
fn expectCalled(times: u32) !void {
|
||||||
|
return std.testing.expectEqual(times, calls);
|
||||||
|
}
|
||||||
|
|
||||||
|
fn reset() void {
|
||||||
|
calls = 0;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
const Context = struct {};
|
||||||
|
fn dummyHandler(_: Context) void {}
|
||||||
|
|
||||||
|
test "Router(T).dispatch" {
|
||||||
|
const call_tracker = CallTracker(.{}, dummyHandler);
|
||||||
|
|
||||||
|
const router = Router(Context){ .handler = call_tracker.func };
|
||||||
|
router.dispatch(.{});
|
||||||
|
try call_tracker.expectCalled(1);
|
||||||
|
|
||||||
|
call_tracker.reset();
|
||||||
|
router.dispatch(.{});
|
||||||
|
router.dispatch(.{});
|
||||||
|
router.dispatch(.{});
|
||||||
|
try call_tracker.expectCalled(3);
|
||||||
|
}
|
||||||
|
};
|
Loading…
Add table
Add a link
Reference in a new issue