From fa5617622333da6334252a9d7e3633129b89120d Mon Sep 17 00:00:00 2001 From: Adam Fowler Date: Sat, 14 Jan 2023 08:50:51 +0000 Subject: [PATCH] Fix issue with indented partials (#19) * Fix issue with indented partials * Add test * Add empty transform for string --- .../HummingbirdMustache/Template+Render.swift | 8 +++-- Sources/HummingbirdMustache/Transform.swift | 2 ++ .../PartialTests.swift | 35 +++++++++++++++++++ .../TemplateRendererTests.swift | 18 +++++++++- 4 files changed, 59 insertions(+), 4 deletions(-) diff --git a/Sources/HummingbirdMustache/Template+Render.swift b/Sources/HummingbirdMustache/Template+Render.swift index 89ca803..d7ef5b2 100644 --- a/Sources/HummingbirdMustache/Template+Render.swift +++ b/Sources/HummingbirdMustache/Template+Render.swift @@ -27,14 +27,16 @@ extension HBMustacheTemplate { if let indentation = context.indentation, indentation != "" { for token in tokens { - if string.last == "\n" { + let renderedString = self.renderToken(token, context: &context) + if renderedString != "", string.last == "\n" { string += indentation } - string += self.renderToken(token, context: &context) + string += renderedString } } else { for token in tokens { - string += self.renderToken(token, context: &context) + let result = self.renderToken(token, context: &context) + string += result } } return string diff --git a/Sources/HummingbirdMustache/Transform.swift b/Sources/HummingbirdMustache/Transform.swift index 592f3d9..63c68fe 100644 --- a/Sources/HummingbirdMustache/Transform.swift +++ b/Sources/HummingbirdMustache/Transform.swift @@ -39,6 +39,8 @@ public extension StringProtocol { /// - Returns: Result func transform(_ name: String) -> Any? { switch name { + case "empty": + return isEmpty case "capitalized": return capitalized case "lowercased": diff --git a/Tests/HummingbirdMustacheTests/PartialTests.swift b/Tests/HummingbirdMustacheTests/PartialTests.swift index 8e8818d..65bb9cb 100644 --- a/Tests/HummingbirdMustacheTests/PartialTests.swift +++ b/Tests/HummingbirdMustacheTests/PartialTests.swift @@ -42,6 +42,41 @@ final class PartialTests: XCTestCase { """) } + /// Test where last line of partial generates no content. It should not add a + /// tab either + func testPartialEmptyLineTabbing() throws { + let library = HBMustacheLibrary() + let template = try HBMustacheTemplate(string: """ +

Names

+ {{#names}} + {{> user}} + {{/names}} + Text after + + """) + let template2 = try HBMustacheTemplate(string: """ + {{^empty(.)}} + {{.}} + {{/empty(.)}} + {{#empty(.)}} + empty + {{/empty(.)}} + + """) + library.register(template, named: "base") + library.register(template2, named: "user") + + 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 library = HBMustacheLibrary() diff --git a/Tests/HummingbirdMustacheTests/TemplateRendererTests.swift b/Tests/HummingbirdMustacheTests/TemplateRendererTests.swift index c181361..c22f3e4 100644 --- a/Tests/HummingbirdMustacheTests/TemplateRendererTests.swift +++ b/Tests/HummingbirdMustacheTests/TemplateRendererTests.swift @@ -82,7 +82,7 @@ final class TemplateRendererTests: XCTestCase { XCTAssertEqual(template.render(Test(string: nil)), "test ") } - func testOptionalSequence() throws { + func testOptionalSection() throws { struct Test { let string: String? } @@ -94,6 +94,22 @@ final class TemplateRendererTests: XCTestCase { XCTAssertEqual(template2.render(Test(string: nil)), "test *") } + func testOptionalSequence() throws { + struct Test { + let string: String? + } + let template = try HBMustacheTemplate(string: "test {{#.}}{{string}}{{/.}}") + XCTAssertEqual(template.render([Test(string: "string")]), "test string") + } + + func testOptionalSequenceSection() throws { + struct Test { + let string: String? + } + let template = try HBMustacheTemplate(string: "test {{#.}}{{#string}}*{{.}}{{/string}}{{/.}}") + XCTAssertEqual(template.render([Test(string: "string")]), "test *string") + } + func testStructureInStructure() throws { struct SubTest { let string: String?