Add development support for reloading templates when you render them (#30)
* Add support for reloading templates when you render them * comment * Ensure reload is only available in DEBUG * move preprocessor block * swift format * MustacheTemplate.init?(filename:) internal * Only pass reload flag down in DEBUG builds * Rebase with main
This commit is contained in:
@@ -2,7 +2,7 @@
|
||||
//
|
||||
// This source file is part of the Hummingbird server framework project
|
||||
//
|
||||
// Copyright (c) 2021-2021 the Hummingbird authors
|
||||
// Copyright (c) 2021-2024 the Hummingbird authors
|
||||
// Licensed under Apache License v2.0
|
||||
//
|
||||
// See LICENSE.txt for license information
|
||||
@@ -71,4 +71,43 @@ final class LibraryTests: XCTestCase {
|
||||
XCTAssertEqual(parserError.context.columnNumber, 10)
|
||||
}
|
||||
}
|
||||
|
||||
#if DEBUG
|
||||
func testReload() async throws {
|
||||
let fs = FileManager()
|
||||
try? fs.createDirectory(atPath: "templates", withIntermediateDirectories: false)
|
||||
defer { XCTAssertNoThrow(try fs.removeItem(atPath: "templates")) }
|
||||
let mustache = Data("<test>{{#value}}<value>{{.}}</value>{{/value}}</test>".utf8)
|
||||
try mustache.write(to: URL(fileURLWithPath: "templates/test.mustache"))
|
||||
defer { XCTAssertNoThrow(try fs.removeItem(atPath: "templates/test.mustache")) }
|
||||
|
||||
let library = try await MustacheLibrary(directory: "./templates")
|
||||
let object = ["value": ["value1", "value2"]]
|
||||
XCTAssertEqual(library.render(object, withTemplate: "test"), "<test><value>value1</value><value>value2</value></test>")
|
||||
let mustache2 = Data("<test2>{{#value}}<value>{{.}}</value>{{/value}}</test2>".utf8)
|
||||
try mustache2.write(to: URL(fileURLWithPath: "templates/test.mustache"))
|
||||
XCTAssertEqual(library.render(object, withTemplate: "test", reload: true), "<test2><value>value1</value><value>value2</value></test2>")
|
||||
}
|
||||
|
||||
func testReloadPartial() async throws {
|
||||
let fs = FileManager()
|
||||
try? fs.createDirectory(atPath: "templates", withIntermediateDirectories: false)
|
||||
let mustache = Data("<test>{{#value}}<value>{{.}}</value>{{/value}}</test>".utf8)
|
||||
try mustache.write(to: URL(fileURLWithPath: "templates/test-partial.mustache"))
|
||||
let mustache2 = Data("{{>test-partial}}".utf8)
|
||||
try mustache2.write(to: URL(fileURLWithPath: "templates/test.mustache"))
|
||||
defer {
|
||||
XCTAssertNoThrow(try fs.removeItem(atPath: "templates/test-partial.mustache"))
|
||||
XCTAssertNoThrow(try fs.removeItem(atPath: "templates/test.mustache"))
|
||||
XCTAssertNoThrow(try fs.removeItem(atPath: "templates"))
|
||||
}
|
||||
|
||||
let library = try await MustacheLibrary(directory: "./templates")
|
||||
let object = ["value": ["value1", "value2"]]
|
||||
XCTAssertEqual(library.render(object, withTemplate: "test"), "<test><value>value1</value><value>value2</value></test>")
|
||||
let mustache3 = Data("<test2>{{#value}}<value>{{.}}</value>{{/value}}</test2>".utf8)
|
||||
try mustache3.write(to: URL(fileURLWithPath: "templates/test-partial.mustache"))
|
||||
XCTAssertEqual(library.render(object, withTemplate: "test", reload: true), "<test2><value>value1</value><value>value2</value></test2>")
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user