//===----------------------------------------------------------------------===//
//
// 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 Mustache
import XCTest
final class PartialTests: XCTestCase {
/// Testing partials
func testMustacheManualExample9() throws {
let template = try MustacheTemplate(string: """
Names
{{#names}}
{{> user}}
{{/names}}
""")
let template2 = try MustacheTemplate(string: """
{{.}}
""")
let library = MustacheLibrary(templates: ["base": template, "user": template2])
let object: [String: Any] = ["names": ["john", "adam", "claire"]]
XCTAssertEqual(library.render(object, withTemplate: "base"), """
Names
john
adam
claire
""")
}
/// Test where last line of partial generates no content. It should not add a
/// tab either
func testPartialEmptyLineTabbing() throws {
let template = try MustacheTemplate(string: """
Names
{{#names}}
{{> user}}
{{/names}}
Text after
""")
let template2 = try MustacheTemplate(string: """
{{^empty(.)}}
{{.}}
{{/empty(.)}}
{{#empty(.)}}
empty
{{/empty(.)}}
""")
var library = MustacheLibrary()
library.register(template, named: "base")
library.register(template2, named: "user") // , withTemplate: String)// = MustacheLibrary(templates: ["base": template, "user": template2])
let object: [String: Any] = ["names": ["john", "adam", "claire"]]
XCTAssertEqual(library.render(object, withTemplate: "base"), """
Names
john
adam
claire
Text after
""")
}
/// Testing dynamic partials
func testDynamicPartials() throws {
let template = try MustacheTemplate(string: """
Names
{{partial}}
""")
let template2 = try MustacheTemplate(string: """
{{#names}}
{{.}}
{{/names}}
""")
let library = MustacheLibrary(templates: ["base": template])
let object: [String: Any] = ["names": ["john", "adam", "claire"], "partial": template2]
XCTAssertEqual(library.render(object, withTemplate: "base"), """
Names
john
adam
claire
""")
}
/// test inheritance
func testInheritance() throws {
var library = MustacheLibrary()
try library.register(
"""
{{$title}}Default title{{/title}}
""",
named: "header"
)
try library.register(
"""
{{$header}}{{/header}}
{{$content}}{{/content}}
""",
named: "base"
)
try library.register(
"""
{{Hello world{{/content}}
{{/base}}
""",
named: "mypage"
)
XCTAssertEqual(library.render({}, withTemplate: "mypage")!, """
My page title
Hello world
""")
}
}