80 lines
2.9 KiB
Swift
80 lines
2.9 KiB
Swift
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
|
|
}
|
|
}
|