import Foundation import Testing import OrgCore @testable import OrgEditorAppKit /// Sources/OrgApp/CommandTitles.swift names every command title as a `String(localized:)` /// literal, so scripts/sync-strings.sh puts them in the string catalog: the Mac commands /// (which include OrgCore's) and the iOS palette's own, read from its source. Rewrite it with /// `ORGSTAR_WRITE_COMMAND_TITLES=1 swift test --filter CommandTitleTests`. struct CommandTitleTests { static let root = URL(fileURLWithPath: #filePath).deletingLastPathComponent().deletingLastPathComponent().deletingLastPathComponent() static func titles() throws -> [String] { let ios = try String(contentsOf: root.appendingPathComponent("Sources/OrgstarMobile/EditorScreen.swift"), encoding: .utf8) let iosTitles = ios.matches(of: /\("(?:app|org|edit|editor|view)\.[^"]+", "([^"\\]+)"\)/).map { String($0.1) } return Array(Set(EditorCommands.all.map(\.title) + iosTitles)).sorted() } static func source(_ titles: [String]) -> String { func literal(_ s: String) -> String { "\"" + s.replacingOccurrences(of: "\\", with: "\\\\").replacingOccurrences(of: "\"", with: "\\\"") + "\"" } return """ // Generated by Tests/OrgEditorAppKitTests/CommandTitleTests.swift; rewrite with // ORGSTAR_WRITE_COMMAND_TITLES=1 swift test --filter CommandTitleTests. import Foundation /// Every command title, for the string catalog; `CommandTitle.localized` looks them up. enum CommandTitles { static let all: [String] = [ """ + titles.map { " String(localized: \(literal($0)), comment: \"Command title\"),\n" }.joined() + """ ] } """ } @Test func titlesFileIsCurrent() throws { let titles = try Self.titles() #expect(titles.count > 250) let file = Self.root.appendingPathComponent("Sources/OrgApp/CommandTitles.swift") let wanted = Self.source(titles) if ProcessInfo.processInfo.environment["ORGSTAR_WRITE_COMMAND_TITLES"] != nil { try wanted.write(to: file, atomically: true, encoding: .utf8) } #expect((try? String(contentsOf: file, encoding: .utf8)) == wanted, "Sources/OrgApp/CommandTitles.swift is out of date; see CommandTitleTests") } }