Clean up tests

This commit is contained in:
jaina heartles 2022-05-19 21:01:28 -07:00
parent b24d96233d
commit 64004cbd82

View file

@ -64,78 +64,97 @@ const _tests = struct {
test "Router(T).dispatch" {
const mock_a = CallTracker(.{}, dummyHandler);
const mock_a_post = CallTracker(.{}, dummyHandler);
const mock_b = CallTracker(.{}, dummyHandler);
const mock_c = CallTracker(.{}, dummyHandler);
const mock_404 = CallTracker(.{}, dummyHandler);
const R = Router(Context).Route;
const routes = [_]R{
.{ .handler = mock_a.func, .method = .GET, .path = "/a" },
.{ .handler = mock_a_post.func, .method = .POST, .path = "/a" },
.{ .handler = mock_b.func, .method = .GET, .path = "/b" },
.{ .handler = mock_c.func, .method = .GET, .path = "/c" },
};
const router = Router(Context){ .routes = &routes, .route_404 = mock_404.func };
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", 10);
router.dispatch(.GET, "/b", 0);
try mock_a.expectNotCalled();
try mock_a_post.expectNotCalled();
try mock_b.expectCalledOnceWith(10);
try mock_c.expectNotCalled();
try mock_b.expectCalledOnceWith(0);
try mock_404.expectNotCalled();
mock_a.reset();
mock_a_post.reset();
mock_b.reset();
mock_c.reset();
mock_404.reset();
}
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();
test "Router(T).dispatch 404" {
const mock_a = CallTracker(.{}, dummyHandler);
const mock_b = CallTracker(.{}, dummyHandler);
const mock_404 = CallTracker(.{}, dummyHandler);
router.dispatch(.GET, "/abcd", 11);
const R = Router(Context).Route;
const routes = [_]R{
.{ .handler = mock_a.func, .method = .GET, .path = "/a" },
.{ .handler = mock_b.func, .method = .GET, .path = "/b" },
};
const router = Router(Context){ .routes = &routes, .route_404 = mock_404.func };
router.dispatch(.GET, "/c", 10);
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();
try mock_404.expectCalledOnceWith(10);
mock_404.reset();
router.dispatch(.POST, "/a", 0);
router.dispatch(.POST, "/a", 10);
try mock_a.expectNotCalled();
try mock_a_post.expectCalledOnceWith(0);
try mock_b.expectNotCalled();
try mock_c.expectNotCalled();
try mock_404.expectCalledOnceWith(10);
}
test "Router(T).dispatch same path different methods" {
const mock_get = CallTracker(.{}, dummyHandler);
const mock_post = CallTracker(.{}, dummyHandler);
const mock_404 = CallTracker(.{}, dummyHandler);
const R = Router(Context).Route;
const routes = [_]R{
.{ .handler = mock_get.func, .method = .GET, .path = "/a" },
.{ .handler = mock_post.func, .method = .POST, .path = "/a" },
};
const router = Router(Context){ .routes = &routes, .route_404 = mock_404.func };
router.dispatch(.GET, "/a", 10);
try mock_get.expectCalledOnceWith(10);
try mock_post.expectNotCalled();
try mock_404.expectNotCalled();
mock_get.reset();
router.dispatch(.POST, "/a", 10);
try mock_get.expectNotCalled();
try mock_post.expectCalledOnceWith(10);
try mock_404.expectNotCalled();
}
test "Router(T).dispatch route under subpath" {
const mock_a = CallTracker(.{}, dummyHandler);
const mock_b = CallTracker(.{}, dummyHandler);
const mock_404 = CallTracker(.{}, dummyHandler);
const R = Router(Context).Route;
const routes = [_]R{
.{ .handler = mock_a.func, .method = .GET, .path = "/a" },
.{ .handler = mock_b.func, .method = .GET, .path = "/a/b" },
};
const router = Router(Context){ .routes = &routes, .route_404 = mock_404.func };
router.dispatch(.GET, "/a", 10);
try mock_a.expectCalledOnceWith(10);
try mock_b.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/b", 11);
try mock_a.expectNotCalled();
try mock_b.expectCalledOnceWith(11);
try mock_404.expectNotCalled();
}
};