Dynamic names support (#49)

* Dynamic names support

* Add support for dynamic names in parent tags

* Support all dynamic names spec

* Swift 5.8 compile fix
This commit is contained in:
Adam Fowler
2024-08-28 08:31:06 +01:00
committed by GitHub
parent a010f172c5
commit 01b1f21ed6
5 changed files with 178 additions and 8 deletions

View File

@@ -69,7 +69,7 @@ final class MustacheSpecTests: XCTestCase {
let expected: String
func run() throws {
// print("Test: \(self.name)")
print("Test: \(self.name)")
if let partials = self.partials {
let template = try MustacheTemplate(string: self.template)
var templates: [String: MustacheTemplate] = ["__test__": template]
@@ -188,4 +188,8 @@ final class MustacheSpecTests: XCTestCase {
]
)
}
func testDynamicNamesSpec() async throws {
try await self.testSpec(name: "~dynamic-names")
}
}

View File

@@ -261,6 +261,119 @@ final class TemplateRendererTests: XCTestCase {
""")
}
/// test dynamic names
func testMustacheManualDynamicNames() throws {
var library = MustacheLibrary()
try library.register(
"Hello {{>*dynamic}}",
named: "main"
)
try library.register(
"everyone!",
named: "world"
)
let object = ["dynamic": "world"]
XCTAssertEqual(library.render(object, withTemplate: "main"), "Hello everyone!")
}
/// test block with defaults
func testMustacheManualBlocksWithDefaults() throws {
let template = try MustacheTemplate(string: """
<h1>{{$title}}The News of Today{{/title}}</h1>
{{$body}}
<p>Nothing special happened.</p>
{{/body}}
""")
XCTAssertEqual(template.render([]), """
<h1>The News of Today</h1>
<p>Nothing special happened.</p>
""")
}
func testMustacheManualParents() throws {
var library = MustacheLibrary()
try library.register(
"""
{{<article}}
Never shown
{{$body}}
{{#headlines}}
<p>{{.}}</p>
{{/headlines}}
{{/body}}
{{/article}}
{{<article}}
{{$title}}Yesterday{{/title}}
{{/article}}
""",
named: "main"
)
try library.register(
"""
<h1>{{$title}}The News of Today{{/title}}</h1>
{{$body}}
<p>Nothing special happened.</p>
{{/body}}
""",
named: "article"
)
let object = [
"headlines": [
"A pug's handler grew mustaches.",
"What an exciting day!",
],
]
XCTAssertEqual(
library.render(object, withTemplate: "main"),
"""
<h1>The News of Today</h1>
<p>A pug&#39;s handler grew mustaches.</p>
<p>What an exciting day!</p>
<h1>Yesterday</h1>
<p>Nothing special happened.</p>
"""
)
}
func testMustacheManualDynamicNameParents() throws {
var library = MustacheLibrary()
try library.register(
"""
{{<*dynamic}}
{{$text}}Hello World!{{/text}}
{{/*dynamic}}
""",
named: "dynamic"
)
try library.register(
"""
{{$text}}Here goes nothing.{{/text}}
""",
named: "normal"
)
try library.register(
"""
<b>{{$text}}Here also goes nothing but it's bold.{{/text}}</b>
""",
named: "bold"
)
let object = ["dynamic": "bold"]
XCTAssertEqual(
library.render(object, withTemplate: "dynamic"),
"""
<b>Hello World!</b>
"""
)
}
/// test MustacheCustomRenderable
func testCustomRenderable() throws {
let template = try MustacheTemplate(string: "{{.}}")