Template inheritance (#9)
* Move all context variables into HBMustacheContext * Add support for reading inherited sections * Render inherited tokens * Test inheritance spec, fix two minor issues * fix warning * swift format
This commit is contained in:
@@ -51,4 +51,51 @@ final class PartialTests: XCTestCase {
|
||||
|
||||
""")
|
||||
}
|
||||
|
||||
/// test inheritance
|
||||
func testInheritance() throws {
|
||||
let library = HBMustacheLibrary()
|
||||
try library.register(
|
||||
"""
|
||||
<head>
|
||||
<title>{{$title}}Default title{{/title}}</title>
|
||||
</head>
|
||||
|
||||
""",
|
||||
named: "header"
|
||||
)
|
||||
try library.register(
|
||||
"""
|
||||
<html>
|
||||
{{$header}}{{/header}}
|
||||
{{$content}}{{/content}}
|
||||
</html>
|
||||
|
||||
""",
|
||||
named: "base"
|
||||
)
|
||||
try library.register(
|
||||
"""
|
||||
{{<base}}
|
||||
{{$header}}
|
||||
{{<header}}
|
||||
{{$title}}My page title{{/title}}
|
||||
{{/header}}
|
||||
{{/header}}
|
||||
{{$content}}<h1>Hello world</h1>{{/content}}
|
||||
{{/base}}
|
||||
|
||||
""",
|
||||
named: "mypage"
|
||||
)
|
||||
XCTAssertEqual(library.render({}, withTemplate: "mypage")!, """
|
||||
<html>
|
||||
<head>
|
||||
<title>My page title</title>
|
||||
</head>
|
||||
<h1>Hello world</h1>
|
||||
</html>
|
||||
|
||||
""")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -46,11 +46,6 @@ public extension AnyDecodable {
|
||||
/// Verify implementation against formal standard for Mustache.
|
||||
/// https://github.com/mustache/spec
|
||||
final class MustacheSpecTests: XCTestCase {
|
||||
func loadSpec(name: String) throws -> Data {
|
||||
let url = URL(string: "https://raw.githubusercontent.com/mustache/spec/master/specs/\(name).json")!
|
||||
return try Data(contentsOf: url)
|
||||
}
|
||||
|
||||
struct Spec: Decodable {
|
||||
struct Test: Decodable {
|
||||
let name: String
|
||||
@@ -91,7 +86,12 @@ final class MustacheSpecTests: XCTestCase {
|
||||
}
|
||||
|
||||
func testSpec(name: String, ignoring: [String] = []) throws {
|
||||
let data = try loadSpec(name: name)
|
||||
let url = URL(string: "https://raw.githubusercontent.com/mustache/spec/master/specs/\(name).json")!
|
||||
try testSpec(url: url, ignoring: ignoring)
|
||||
}
|
||||
|
||||
func testSpec(url: URL, ignoring: [String] = []) throws {
|
||||
let data = try Data(contentsOf: url)
|
||||
let spec = try JSONDecoder().decode(Spec.self, from: data)
|
||||
|
||||
print(spec.overview)
|
||||
@@ -124,4 +124,11 @@ final class MustacheSpecTests: XCTestCase {
|
||||
func testSectionsSpec() throws {
|
||||
try self.testSpec(name: "sections", ignoring: ["Variable test"])
|
||||
}
|
||||
|
||||
func testInheritanceSpec() throws {
|
||||
let url = URL(
|
||||
string: "https://raw.githubusercontent.com/mustache/spec/ab227509e64961943ca374c09c08b63f59da014a/specs/inheritance.json"
|
||||
)!
|
||||
try self.testSpec(url: url)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -50,7 +50,7 @@ extension HBMustacheTemplate.Token: Equatable {
|
||||
return lhs1 == rhs1 && lhs2 == rhs2 && lhs3 == rhs3
|
||||
case (.invertedSection(let lhs1, let lhs2, let lhs3), .invertedSection(let rhs1, let rhs2, let rhs3)):
|
||||
return lhs1 == rhs1 && lhs2 == rhs2 && lhs3 == rhs3
|
||||
case (.partial(let name1, let indent1), .partial(let name2, let indent2)):
|
||||
case (.partial(let name1, let indent1, _), .partial(let name2, let indent2, _)):
|
||||
return name1 == name2 && indent1 == indent2
|
||||
default:
|
||||
return false
|
||||
|
||||
Reference in New Issue
Block a user