Scaffold v1.0.0
This commit is contained in:
79
Tests/EmbedderToolTests/SwiftCodeGeneratorTests.swift
Normal file
79
Tests/EmbedderToolTests/SwiftCodeGeneratorTests.swift
Normal file
@@ -0,0 +1,79 @@
|
||||
import Foundation
|
||||
import Testing
|
||||
@testable import EmbedderTool
|
||||
|
||||
@Suite("SwiftCodeGenerator") struct SwiftCodeGeneratorTests {
|
||||
|
||||
@Test("generates a compilable file for top-level files")
|
||||
func topLevelFiles() throws {
|
||||
let directory = try TemporaryDirectory()
|
||||
let configFile = try directory.write(contents: #"{"key":"value"}"#, toRelativePath: "config.json")
|
||||
|
||||
let root = NamespaceNode(
|
||||
name: "Embedded",
|
||||
files: [
|
||||
EmbeddableFile(
|
||||
absoluteURL: configFile,
|
||||
relativePathComponents: ["config.json"]
|
||||
)
|
||||
]
|
||||
)
|
||||
let output = try SwiftCodeGenerator().generate(from: root)
|
||||
|
||||
#expect(output.contains("enum Embedded {"))
|
||||
#expect(output.contains("static let configJson: String = #\"\"\""))
|
||||
#expect(output.contains(#"{"key":"value"}"#))
|
||||
#expect(output.contains("\"\"\"#"))
|
||||
}
|
||||
|
||||
@Test("nests child enums for subdirectories")
|
||||
func nestedNamespaces() throws {
|
||||
let directory = try TemporaryDirectory()
|
||||
let welcomeFile = try directory.write(contents: "<html></html>", toRelativePath: "emails/welcome.html")
|
||||
|
||||
let root = NamespaceNode(
|
||||
name: "Embedded",
|
||||
subNamespaces: [
|
||||
NamespaceNode(
|
||||
name: "Emails",
|
||||
files: [
|
||||
EmbeddableFile(
|
||||
absoluteURL: welcomeFile,
|
||||
relativePathComponents: ["emails", "welcome.html"]
|
||||
)
|
||||
]
|
||||
)
|
||||
]
|
||||
)
|
||||
let output = try SwiftCodeGenerator().generate(from: root)
|
||||
|
||||
#expect(output.contains("enum Embedded {"))
|
||||
#expect(output.contains("enum Emails {"))
|
||||
#expect(output.contains("static let welcomeHtml: String = #\"\"\""))
|
||||
#expect(output.contains("<html></html>"))
|
||||
}
|
||||
|
||||
@Test("includes a generated-file header")
|
||||
func header() throws {
|
||||
let output = try SwiftCodeGenerator().generate(from: NamespaceNode(name: "Embedded"))
|
||||
#expect(output.hasPrefix("// Generated by the Embedder plugin"))
|
||||
}
|
||||
}
|
||||
|
||||
private struct TemporaryDirectory {
|
||||
let url: URL
|
||||
|
||||
init() throws {
|
||||
let baseURL = URL(fileURLWithPath: NSTemporaryDirectory(), isDirectory: true)
|
||||
self.url = baseURL.appending(path: "EmbedderToolTests-\(UUID().uuidString)")
|
||||
try FileManager.default.createDirectory(at: url, withIntermediateDirectories: true)
|
||||
}
|
||||
|
||||
func write(contents: String, toRelativePath relativePath: String) throws -> URL {
|
||||
let fileURL = url.appending(path: relativePath)
|
||||
let parent = fileURL.deletingLastPathComponent()
|
||||
try FileManager.default.createDirectory(at: parent, withIntermediateDirectories: true)
|
||||
try contents.write(to: fileURL, atomically: true, encoding: .utf8)
|
||||
return fileURL
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user