Tests/OrgEditorAppKitTests/CommandTitleTests.swift

design-themes-widgets
orgstar/Tests/OrgEditorAppKitTests/CommandTitleTests.swift history · blame · raw

47 lines · 2363 bytes

 1import Foundation
 2import Testing
 3import OrgCore
 4@testable import OrgEditorAppKit
 5
 6/// Sources/OrgApp/CommandTitles.swift names every command title as a `String(localized:)`
 7/// literal, so scripts/sync-strings.sh puts them in the string catalog: the Mac commands
 8/// (which include OrgCore's) and the iOS palette's own, read from its source. Rewrite it with
 9/// `ORGSTAR_WRITE_COMMAND_TITLES=1 swift test --filter CommandTitleTests`.
10struct CommandTitleTests {
11    static let root = URL(fileURLWithPath: #filePath).deletingLastPathComponent().deletingLastPathComponent().deletingLastPathComponent()
12
13    static func titles() throws -> [String] {
14        let ios = try String(contentsOf: root.appendingPathComponent("Sources/OrgstarMobile/EditorScreen.swift"), encoding: .utf8)
15        let iosTitles = ios.matches(of: /\("(?:app|org|edit|editor|view)\.[^"]+", "([^"\\]+)"\)/).map { String($0.1) }
16        return Array(Set(EditorCommands.all.map(\.title) + iosTitles)).sorted()
17    }
18
19    static func source(_ titles: [String]) -> String {
20        func literal(_ s: String) -> String { "\"" + s.replacingOccurrences(of: "\\", with: "\\\\").replacingOccurrences(of: "\"", with: "\\\"") + "\"" }
21        return """
22            // Generated by Tests/OrgEditorAppKitTests/CommandTitleTests.swift; rewrite with
23            // ORGSTAR_WRITE_COMMAND_TITLES=1 swift test --filter CommandTitleTests.
24            import Foundation
25
26            /// Every command title, for the string catalog; `CommandTitle.localized` looks them up.
27            enum CommandTitles {
28                static let all: [String] = [
29
30            """ + titles.map { "        String(localized: \(literal($0)), comment: \"Command title\"),\n" }.joined() + """
31                ]
32            }
33
34            """
35    }
36
37    @Test func titlesFileIsCurrent() throws {
38        let titles = try Self.titles()
39        #expect(titles.count > 250)
40        let file = Self.root.appendingPathComponent("Sources/OrgApp/CommandTitles.swift")
41        let wanted = Self.source(titles)
42        if ProcessInfo.processInfo.environment["ORGSTAR_WRITE_COMMAND_TITLES"] != nil {
43            try wanted.write(to: file, atomically: true, encoding: .utf8)
44        }
45        #expect((try? String(contentsOf: file, encoding: .utf8)) == wanted, "Sources/OrgApp/CommandTitles.swift is out of date; see CommandTitleTests")
46    }
47}