Update from Hummingbird Project Template (#58)

* Update from hummingbird-project-template 572d468b2cabeca286314c5a35196bd42445c8ef

* run swift-format

* Remove .swiftformat

---------

Co-authored-by: adam-fowler <adam-fowler@users.noreply.github.com>
Co-authored-by: Adam Fowler <adamfowler71@gmail.com>
This commit is contained in:
hummingbird-automation[bot]
2024-11-28 07:31:09 +00:00
committed by GitHub
parent 8c5c8ead74
commit ec4ef9aa04
21 changed files with 663 additions and 430 deletions

View File

@@ -12,120 +12,156 @@
//
//===----------------------------------------------------------------------===//
@testable import Mustache
import XCTest
@testable import Mustache
final class PartialTests: XCTestCase {
/// Testing partials
func testMustacheManualExample9() throws {
let template = try MustacheTemplate(string: """
<h2>Names</h2>
{{#names}}
{{> user}}
{{/names}}
""")
let template2 = try MustacheTemplate(string: """
<strong>{{.}}</strong>
let template = try MustacheTemplate(
string: """
<h2>Names</h2>
{{#names}}
{{> user}}
{{/names}}
"""
)
let template2 = try MustacheTemplate(
string: """
<strong>{{.}}</strong>
""")
"""
)
let library = MustacheLibrary(templates: ["base": template, "user": template2])
let object: [String: Any] = ["names": ["john", "adam", "claire"]]
XCTAssertEqual(library.render(object, withTemplate: "base"), """
<h2>Names</h2>
<strong>john</strong>
<strong>adam</strong>
<strong>claire</strong>
XCTAssertEqual(
library.render(object, withTemplate: "base"),
"""
<h2>Names</h2>
<strong>john</strong>
<strong>adam</strong>
<strong>claire</strong>
""")
"""
)
}
/// Test where last line of partial generates no content. It should not add a
/// tab either
func testPartialEmptyLineTabbing() throws {
let template = try MustacheTemplate(string: """
<h2>Names</h2>
{{#names}}
{{> user}}
{{/names}}
Text after
let template = try MustacheTemplate(
string: """
<h2>Names</h2>
{{#names}}
{{> user}}
{{/names}}
Text after
""")
let template2 = try MustacheTemplate(string: """
{{^empty(.)}}
<strong>{{.}}</strong>
{{/empty(.)}}
{{#empty(.)}}
<strong>empty</strong>
{{/empty(.)}}
"""
)
let template2 = try MustacheTemplate(
string: """
{{^empty(.)}}
<strong>{{.}}</strong>
{{/empty(.)}}
{{#empty(.)}}
<strong>empty</strong>
{{/empty(.)}}
""")
"""
)
var library = MustacheLibrary()
library.register(template, named: "base")
library.register(template2, named: "user")
let object: [String: Any] = ["names": ["john", "adam", "claire"]]
XCTAssertEqual(library.render(object, withTemplate: "base"), """
<h2>Names</h2>
<strong>john</strong>
<strong>adam</strong>
<strong>claire</strong>
Text after
XCTAssertEqual(
library.render(object, withTemplate: "base"),
"""
<h2>Names</h2>
<strong>john</strong>
<strong>adam</strong>
<strong>claire</strong>
Text after
""")
"""
)
}
func testTrailingNewLines() throws {
let template1 = try MustacheTemplate(string: """
{{> withNewLine }}
>> {{> withNewLine }}
[ {{> withNewLine }} ]
""")
let template2 = try MustacheTemplate(string: """
{{> withoutNewLine }}
>> {{> withoutNewLine }}
[ {{> withoutNewLine }} ]
""")
let withNewLine = try MustacheTemplate(string: """
{{#things}}{{.}}, {{/things}}
let template1 = try MustacheTemplate(
string: """
{{> withNewLine }}
>> {{> withNewLine }}
[ {{> withNewLine }} ]
"""
)
let template2 = try MustacheTemplate(
string: """
{{> withoutNewLine }}
>> {{> withoutNewLine }}
[ {{> withoutNewLine }} ]
"""
)
let withNewLine = try MustacheTemplate(
string: """
{{#things}}{{.}}, {{/things}}
""")
"""
)
let withoutNewLine = try MustacheTemplate(string: "{{#things}}{{.}}, {{/things}}")
let library = MustacheLibrary(templates: ["base1": template1, "base2": template2, "withNewLine": withNewLine, "withoutNewLine": withoutNewLine])
let library = MustacheLibrary(templates: [
"base1": template1, "base2": template2, "withNewLine": withNewLine, "withoutNewLine": withoutNewLine,
])
let object = ["things": [1, 2, 3, 4, 5]]
XCTAssertEqual(library.render(object, withTemplate: "base1"), """
1, 2, 3, 4, 5,
>> 1, 2, 3, 4, 5,
XCTAssertEqual(
library.render(object, withTemplate: "base1"),
"""
1, 2, 3, 4, 5,
>> 1, 2, 3, 4, 5,
[ 1, 2, 3, 4, 5,
]
""")
XCTAssertEqual(library.render(object, withTemplate: "base2"), """
1, 2, 3, 4, 5, >> 1, 2, 3, 4, 5,
[ 1, 2, 3, 4, 5, ]
""")
[ 1, 2, 3, 4, 5,
]
"""
)
XCTAssertEqual(
library.render(object, withTemplate: "base2"),
"""
1, 2, 3, 4, 5, >> 1, 2, 3, 4, 5,
[ 1, 2, 3, 4, 5, ]
"""
)
}
/// Testing dynamic partials
func testDynamicPartials() throws {
let template = try MustacheTemplate(string: """
<h2>Names</h2>
{{partial}}
""")
let template2 = try MustacheTemplate(string: """
{{#names}}
<strong>{{.}}</strong>
{{/names}}
""")
let template = try MustacheTemplate(
string: """
<h2>Names</h2>
{{partial}}
"""
)
let template2 = try MustacheTemplate(
string: """
{{#names}}
<strong>{{.}}</strong>
{{/names}}
"""
)
let library = MustacheLibrary(templates: ["base": template])
let object: [String: Any] = ["names": ["john", "adam", "claire"], "partial": template2]
XCTAssertEqual(library.render(object, withTemplate: "base"), """
<h2>Names</h2>
<strong>john</strong>
<strong>adam</strong>
<strong>claire</strong>
XCTAssertEqual(
library.render(object, withTemplate: "base"),
"""
<h2>Names</h2>
<strong>john</strong>
<strong>adam</strong>
<strong>claire</strong>
""")
"""
)
}
/// test inheritance
@@ -163,15 +199,18 @@ final class PartialTests: XCTestCase {
""",
named: "mypage"
)
XCTAssertEqual(library.render({}, withTemplate: "mypage")!, """
<html>
<head>
<title>My page title</title>
</head>
<h1>Hello world</h1>
</html>
XCTAssertEqual(
library.render({}, withTemplate: "mypage")!,
"""
<html>
<head>
<title>My page title</title>
</head>
<h1>Hello world</h1>
</html>
""")
"""
)
}
func testInheritanceIndentation() throws {
@@ -194,11 +233,14 @@ final class PartialTests: XCTestCase {
""",
named: "template"
)
XCTAssertEqual(library.render({}, withTemplate: "template"), """
Hi,
one
two
XCTAssertEqual(
library.render({}, withTemplate: "template"),
"""
Hi,
one
two
""")
"""
)
}
}