Add testPartialInSubfolder (#72)

* Add testPartialInSubfolder

* Replace windows folder separator with mac/linux folder separator
This commit is contained in:
Adam Fowler
2025-09-01 09:59:05 +01:00
committed by GitHub
parent 6df64896f4
commit 534f9f9e60
2 changed files with 22 additions and 0 deletions

View File

@@ -31,7 +31,11 @@ extension MustacheLibrary {
guard let template = try MustacheTemplate(filename: directory + path) else { continue } guard let template = try MustacheTemplate(filename: directory + path) else { continue }
// drop ".mustache" from path to get name // drop ".mustache" from path to get name
let name = String(path.dropLast(extWithDot.count)) let name = String(path.dropLast(extWithDot.count))
#if os(Windows)
templates[name.replacingOccurrences(of: "\\", with: "/")] = template
#else
templates[name] = template templates[name] = template
#endif
} catch let error as MustacheTemplate.ParserError { } catch let error as MustacheTemplate.ParserError {
throw ParserError(filename: path, context: error.context, error: error.error) throw ParserError(filename: path, context: error.context, error: error.error)
} }

View File

@@ -48,6 +48,24 @@ final class LibraryTests: XCTestCase {
XCTAssertEqual(library.render(object, withTemplate: "test"), "<test><value>value1</value><value>value2</value></test>") XCTAssertEqual(library.render(object, withTemplate: "test"), "<test><value>value1</value><value>value2</value></test>")
} }
func testPartialInSubfolder() async throws {
let fs = FileManager()
try? fs.createDirectory(atPath: "templates/subfolder", withIntermediateDirectories: true)
let mustache = Data("<test>{{#value}}<value>{{.}}</value>{{/value}}</test>".utf8)
try mustache.write(to: URL(fileURLWithPath: "templates/subfolder/test-partial.mustache"))
let mustache2 = Data("{{>subfolder/test-partial}}".utf8)
try mustache2.write(to: URL(fileURLWithPath: "templates/test.mustache"))
defer {
XCTAssertNoThrow(try fs.removeItem(atPath: "templates/subfolder/test-partial.mustache"))
XCTAssertNoThrow(try fs.removeItem(atPath: "templates/test.mustache"))
XCTAssertNoThrow(try fs.removeItem(atPath: "templates"))
}
let library = try await MustacheLibrary(directory: "./templates")
let object = ["value": ["value1", "value2"]]
XCTAssertEqual(library.render(object, withTemplate: "test"), "<test><value>value1</value><value>value2</value></test>")
}
func testLibraryParserError() async throws { func testLibraryParserError() async throws {
let fs = FileManager() let fs = FileManager()
try? fs.createDirectory(atPath: "templates", withIntermediateDirectories: false) try? fs.createDirectory(atPath: "templates", withIntermediateDirectories: false)