Tests/OrgAppTests/TableReasonTests.swift
45 lines · 2190 bytes
1import Foundation
2import Testing
3@testable import OrgApp
4
5/// Every reason OrgCore gives for sending a table to Emacs has a translation.
6struct TableReasonTests {
7 static let root = URL(fileURLWithPath: #filePath).deletingLastPathComponent().deletingLastPathComponent().deletingLastPathComponent()
8
9 @Test func everyReasonIsKnown() throws {
10 let files = FileManager.default.enumerator(atPath: Self.root.appendingPathComponent("Sources/OrgCore").path)?.allObjects as? [String] ?? []
11 var reasons: [String] = [], lisp: [String] = []
12 for file in files where file.hasSuffix(".swift") {
13 let text = try String(contentsOf: Self.root.appendingPathComponent("Sources/OrgCore/" + file), encoding: .utf8)
14 // An interpolated value stands as "x", as it would read at run time.
15 func sample(_ literal: Substring) -> String {
16 var out = "", depth = 0, chars = Array(literal), i = 0
17 while i < chars.count {
18 if depth == 0, chars[i] == "\\", i + 1 < chars.count, chars[i + 1] == "(" {
19 out += "x"
20 depth = 1
21 i += 2
22 continue
23 }
24 if depth > 0 {
25 if chars[i] == "(" { depth += 1 } else if chars[i] == ")" { depth -= 1 }
26 } else {
27 out.append(chars[i])
28 }
29 i += 1
30 }
31 return out
32 }
33 reasons += text.matches(of: /Unsupported\("((?:[^"\\]|\\\([^)]*\)+)*)"/).map { sample($0.1) }
34 lisp += text.matches(of: /Unsupported\(what: "((?:[^"\\]|\\\([^)]*\)+)*)"/).map { sample($0.1) }
35 }
36 #expect(reasons.count > 30 && lisp.count >= 5)
37 for reason in Set(reasons) where !reason.hasPrefix("Lisp ") {
38 #expect(TableReason.translated(reason) != nil, "\(reason)")
39 }
40 for what in Set(lisp) {
41 #expect(TableReason.translated("Lisp " + what) != nil, "Lisp \(what)")
42 }
43 #expect(TableReason.localized("something new") == "something new")
44 }
45}