2.x.x Template struct (#22)

* Start of turning template into a struct

* Everything is Sendable now, just doesnt work

* Add library to context

* Make sure render is initialized with library

* comment about inheritance spec

* Add register back in

* Re-instate register functions

* Re-instate commented out print

* Fix tabbing in Partial tests

* Make HBMustacheLibrary.loadTemplates async

* Update platforms, swift version
This commit is contained in:
Adam Fowler
2024-01-31 15:43:46 +00:00
committed by GitHub
parent bec77d1f2b
commit 071b182a00
11 changed files with 67 additions and 65 deletions

View File

@@ -16,7 +16,7 @@
import XCTest
final class LibraryTests: XCTestCase {
func testDirectoryLoad() throws {
func testDirectoryLoad() async throws {
let fs = FileManager()
try? fs.createDirectory(atPath: "templates", withIntermediateDirectories: false)
defer { XCTAssertNoThrow(try fs.removeItem(atPath: "templates")) }
@@ -24,12 +24,12 @@ final class LibraryTests: XCTestCase {
try mustache.write(to: URL(fileURLWithPath: "templates/test.mustache"))
defer { XCTAssertNoThrow(try fs.removeItem(atPath: "templates/test.mustache")) }
let library = try HBMustacheLibrary(directory: "./templates")
let library = try await HBMustacheLibrary(directory: "./templates")
let object = ["value": ["value1", "value2"]]
XCTAssertEqual(library.render(object, withTemplate: "test"), "<test><value>value1</value><value>value2</value></test>")
}
func testPartial() throws {
func testPartial() async throws {
let fs = FileManager()
try? fs.createDirectory(atPath: "templates", withIntermediateDirectories: false)
let mustache = Data("<test>{{#value}}<value>{{.}}</value>{{/value}}</test>".utf8)
@@ -42,12 +42,12 @@ final class LibraryTests: XCTestCase {
XCTAssertNoThrow(try fs.removeItem(atPath: "templates"))
}
let library = try HBMustacheLibrary(directory: "./templates")
let library = try await HBMustacheLibrary(directory: "./templates")
let object = ["value": ["value1", "value2"]]
XCTAssertEqual(library.render(object, withTemplate: "test"), "<test><value>value1</value><value>value2</value></test>")
}
func testLibraryParserError() throws {
func testLibraryParserError() async throws {
let fs = FileManager()
try? fs.createDirectory(atPath: "templates", withIntermediateDirectories: false)
defer { XCTAssertNoThrow(try fs.removeItem(atPath: "templates")) }
@@ -62,11 +62,9 @@ final class LibraryTests: XCTestCase {
try mustache2.write(to: URL(fileURLWithPath: "templates/error.mustache"))
defer { XCTAssertNoThrow(try fs.removeItem(atPath: "templates/error.mustache")) }
XCTAssertThrowsError(try HBMustacheLibrary(directory: "./templates")) { error in
guard let parserError = error as? HBMustacheLibrary.ParserError else {
XCTFail("\(error)")
return
}
do {
_ = try await HBMustacheLibrary(directory: "./templates")
} catch let parserError as HBMustacheLibrary.ParserError {
XCTAssertEqual(parserError.filename, "error.mustache")
XCTAssertEqual(parserError.context.line, "{{{name}}")
XCTAssertEqual(parserError.context.lineNumber, 2)

View File

@@ -18,7 +18,6 @@ import XCTest
final class PartialTests: XCTestCase {
/// Testing partials
func testMustacheManualExample9() throws {
let library = HBMustacheLibrary()
let template = try HBMustacheTemplate(string: """
<h2>Names</h2>
{{#names}}
@@ -29,8 +28,7 @@ final class PartialTests: XCTestCase {
<strong>{{.}}</strong>
""")
library.register(template, named: "base")
library.register(template2, named: "user")
let library = HBMustacheLibrary(templates: ["base": template, "user": template2])
let object: [String: Any] = ["names": ["john", "adam", "claire"]]
XCTAssertEqual(library.render(object, withTemplate: "base"), """
@@ -45,7 +43,6 @@ 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: """
<h2>Names</h2>
{{#names}}
@@ -63,8 +60,9 @@ final class PartialTests: XCTestCase {
{{/empty(.)}}
""")
var library = HBMustacheLibrary()
library.register(template, named: "base")
library.register(template2, named: "user")
library.register(template2, named: "user") // , withTemplate: String)// = HBMustacheLibrary(templates: ["base": template, "user": template2])
let object: [String: Any] = ["names": ["john", "adam", "claire"]]
XCTAssertEqual(library.render(object, withTemplate: "base"), """
@@ -79,7 +77,6 @@ final class PartialTests: XCTestCase {
/// Testing dynamic partials
func testDynamicPartials() throws {
let library = HBMustacheLibrary()
let template = try HBMustacheTemplate(string: """
<h2>Names</h2>
{{partial}}
@@ -89,7 +86,7 @@ final class PartialTests: XCTestCase {
<strong>{{.}}</strong>
{{/names}}
""")
library.register(template, named: "base")
let library = HBMustacheLibrary(templates: ["base": template])
let object: [String: Any] = ["names": ["john", "adam", "claire"], "partial": template2]
XCTAssertEqual(library.render(object, withTemplate: "base"), """
@@ -103,7 +100,7 @@ final class PartialTests: XCTestCase {
/// test inheritance
func testInheritance() throws {
let library = HBMustacheLibrary()
var library = HBMustacheLibrary()
try library.register(
"""
<head>

View File

@@ -66,15 +66,15 @@ final class MustacheSpecTests: XCTestCase {
let expected: String
func run() throws {
print("Test: \(self.name)")
// print("Test: \(self.name)")
if let partials = self.partials {
let library = HBMustacheLibrary()
let template = try HBMustacheTemplate(string: self.template)
library.register(template, named: "__test__")
var templates: [String: HBMustacheTemplate] = ["__test__": template]
for (key, value) in partials {
let template = try HBMustacheTemplate(string: value)
library.register(template, named: key)
templates[key] = template
}
let library = HBMustacheLibrary(templates: templates)
let result = library.render(self.data.value, withTemplate: "__test__")
self.XCTAssertSpecEqual(result, self)
} else {
@@ -105,10 +105,12 @@ final class MustacheSpecTests: XCTestCase {
let spec = try JSONDecoder().decode(Spec.self, from: data)
print(spec.overview)
let date = Date()
for test in spec.tests {
guard !ignoring.contains(test.name) else { continue }
XCTAssertNoThrow(try test.run())
}
print(-date.timeIntervalSinceNow)
}
func testCommentsSpec() throws {
@@ -136,6 +138,7 @@ final class MustacheSpecTests: XCTestCase {
}
func testInheritanceSpec() throws {
try XCTSkipIf(true) // inheritance spec has been updated and has added requirements, we don't yet support
try self.testSpec(name: "~inheritance")
}
}