From 142fcf42c7fe303dc4d7335e061f69897ba77b78 Mon Sep 17 00:00:00 2001 From: "Grant J. Butler" Date: Mon, 28 Oct 2024 00:33:23 -0400 Subject: [PATCH 1/2] Set correct value for `application` key. --- Sources/Leaf/Application+Leaf.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sources/Leaf/Application+Leaf.swift b/Sources/Leaf/Application+Leaf.swift index d98feb0..208581c 100644 --- a/Sources/Leaf/Application+Leaf.swift +++ b/Sources/Leaf/Application+Leaf.swift @@ -21,7 +21,7 @@ extension Application { public var renderer: LeafRenderer { var userInfo = self.userInfo - userInfo["application"] = self + userInfo["application"] = self.application var cache = self.cache if self.application.environment == .development { From b7076d5ff98afb0979a66d9592eb3f31485561ca Mon Sep 17 00:00:00 2001 From: "Grant J. Butler" Date: Mon, 28 Oct 2024 00:39:47 -0400 Subject: [PATCH 2/2] Add custom tag to verify casting works. --- Tests/LeafTests/LeafTests.swift | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/Tests/LeafTests/LeafTests.swift b/Tests/LeafTests/LeafTests.swift index e180712..bba430f 100644 --- a/Tests/LeafTests/LeafTests.swift +++ b/Tests/LeafTests/LeafTests.swift @@ -117,7 +117,7 @@ final class LeafTests: XCTestCase { func testContextUserInfo() throws { var test = TestFiles() test.files["/foo.leaf"] = """ - Hello #custom()! @ #source() + Hello #custom()! @ #source() app nil? #application() """ struct CustomTag: LeafTag { @@ -133,6 +133,12 @@ final class LeafTests: XCTestCase { .string(ctx.request?.url.path ?? "application") } } + + struct ApplicationTag: LeafTag { + func render(_ ctx: LeafContext) throws -> LeafData { + .string(ctx.application != nil ? "non-nil app" : "nil app") + } + } let app = Application(.testing) defer { app.shutdown() } @@ -142,6 +148,7 @@ final class LeafTests: XCTestCase { app.leaf.cache.isEnabled = false app.leaf.tags["custom"] = CustomTag() app.leaf.tags["source"] = SourceTag() + app.leaf.tags["application"] = ApplicationTag() app.leaf.sources = .singleSource(test) app.leaf.userInfo["info"] = "World" @@ -152,7 +159,7 @@ final class LeafTests: XCTestCase { try app.test(.GET, "test-file") { res in XCTAssertEqual(res.status, .ok) XCTAssertEqual(res.headers.contentType, .html) - XCTAssertEqual(res.body.string, "Hello World! @ /test-file") + XCTAssertEqual(res.body.string, "Hello World! @ /test-file app nil? non-nil app") } app.get("test-file-with-application-renderer") { req in @@ -162,7 +169,7 @@ final class LeafTests: XCTestCase { try app.test(.GET, "test-file-with-application-renderer") { res in XCTAssertEqual(res.status, .ok) XCTAssertEqual(res.headers.contentType, .html) - XCTAssertEqual(res.body.string, "Hello World! @ application") + XCTAssertEqual(res.body.string, "Hello World! @ application app nil? non-nil app") } }