Skip to content

Commit

Permalink
Make HBMustacheLibrary.loadTemplates async
Browse files Browse the repository at this point in the history
  • Loading branch information
adam-fowler committed Jan 31, 2024
1 parent 11b0c7f commit 214c96c
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 13 deletions.
2 changes: 1 addition & 1 deletion Sources/HummingbirdMustache/Library+FileSystem.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import Foundation

extension HBMustacheLibrary {
/// Load templates from a folder
static func loadTemplates(from directory: String, withExtension extension: String = "mustache") throws -> [String: HBMustacheTemplate] {
static func loadTemplates(from directory: String, withExtension extension: String = "mustache") async throws -> [String: HBMustacheTemplate] {
var directory = directory
if !directory.hasSuffix("/") {
directory += "/"
Expand Down
4 changes: 2 additions & 2 deletions Sources/HummingbirdMustache/Library.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ public struct HBMustacheLibrary: Sendable {
/// the folder is recursive and templates in subfolders will be registered with the name `subfolder/template`.
/// - Parameter directory: Directory to look for mustache templates
/// - Parameter extension: Extension of files to look for
public init(directory: String, withExtension extension: String = "mustache") throws {
self.templates = try Self.loadTemplates(from: directory, withExtension: `extension`)
public init(directory: String, withExtension extension: String = "mustache") async throws {
self.templates = try await Self.loadTemplates(from: directory, withExtension: `extension`)
}

/// Register template under name
Expand Down
18 changes: 8 additions & 10 deletions Tests/HummingbirdMustacheTests/LibraryTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,20 @@
import XCTest

final class LibraryTests: XCTestCase {
func testDirectoryLoad() throws {
func testDirectoryLoad() 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 HBMustacheLibrary(directory: "./templates")
let library = try await HBMustacheLibrary(directory: "./templates")
let object = ["value": ["value1", "value2"]]
XCTAssertEqual(library.render(object, withTemplate: "test"), "<test><value>value1</value><value>value2</value></test>")
}

func testPartial() throws {
func testPartial() async throws {
let fs = FileManager()
try? fs.createDirectory(atPath: "templates", withIntermediateDirectories: false)
let mustache = Data("<test>{{#value}}<value>{{.}}</value>{{/value}}</test>".utf8)
Expand All @@ -42,12 +42,12 @@ final class LibraryTests: XCTestCase {
XCTAssertNoThrow(try fs.removeItem(atPath: "templates"))
}

let library = try HBMustacheLibrary(directory: "./templates")
let library = try await HBMustacheLibrary(directory: "./templates")
let object = ["value": ["value1", "value2"]]
XCTAssertEqual(library.render(object, withTemplate: "test"), "<test><value>value1</value><value>value2</value></test>")
}

func testLibraryParserError() throws {
func testLibraryParserError() async throws {
let fs = FileManager()
try? fs.createDirectory(atPath: "templates", withIntermediateDirectories: false)
defer { XCTAssertNoThrow(try fs.removeItem(atPath: "templates")) }
Expand All @@ -62,11 +62,9 @@ final class LibraryTests: XCTestCase {
try mustache2.write(to: URL(fileURLWithPath: "templates/error.mustache"))
defer { XCTAssertNoThrow(try fs.removeItem(atPath: "templates/error.mustache")) }

XCTAssertThrowsError(try HBMustacheLibrary(directory: "./templates")) { error in
guard let parserError = error as? HBMustacheLibrary.ParserError else {
XCTFail("\(error)")
return
}
do {
_ = try await HBMustacheLibrary(directory: "./templates")
} catch let parserError as HBMustacheLibrary.ParserError {
XCTAssertEqual(parserError.filename, "error.mustache")
XCTAssertEqual(parserError.context.line, "{{{name}}")
XCTAssertEqual(parserError.context.lineNumber, 2)
Expand Down

0 comments on commit 214c96c

Please sign in to comment.