* Start of turning template into a struct * Everything is Sendable now, just doesnt work * Add library to context * Make sure render is initialized with library * comment about inheritance spec * Add register back in * Re-instate register functions * Re-instate commented out print * Fix tabbing in Partial tests * Make HBMustacheLibrary.loadTemplates async * Update platforms, swift version
75 lines
3.5 KiB
Swift
75 lines
3.5 KiB
Swift
//===----------------------------------------------------------------------===//
|
|
//
|
|
// This source file is part of the Hummingbird server framework project
|
|
//
|
|
// Copyright (c) 2021-2021 the Hummingbird authors
|
|
// Licensed under Apache License v2.0
|
|
//
|
|
// See LICENSE.txt for license information
|
|
// See hummingbird/CONTRIBUTORS.txt for the list of Hummingbird authors
|
|
//
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
@testable import HummingbirdMustache
|
|
import XCTest
|
|
|
|
final class LibraryTests: XCTestCase {
|
|
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 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() 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 HBMustacheLibrary(directory: "./templates")
|
|
let object = ["value": ["value1", "value2"]]
|
|
XCTAssertEqual(library.render(object, withTemplate: "test"), "<test><value>value1</value><value>value2</value></test>")
|
|
}
|
|
|
|
func testLibraryParserError() 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 mustache2 = Data("""
|
|
{{#test}}
|
|
{{{name}}
|
|
{{/test2}}
|
|
""".utf8)
|
|
try mustache2.write(to: URL(fileURLWithPath: "templates/error.mustache"))
|
|
defer { XCTAssertNoThrow(try fs.removeItem(atPath: "templates/error.mustache")) }
|
|
|
|
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)
|
|
XCTAssertEqual(parserError.context.columnNumber, 10)
|
|
}
|
|
}
|
|
}
|