Tests/OrgCoreTests/TableFormulaTests.swift
cmc Parse inline tasks, file property drawers, alphabetical bullets; table constants
8b9aa86b71 1 month ago112 lines · 7316 bytes
1import Foundation
2import Testing
3@testable import OrgCore
4
5struct TableFormulaTests {
6 static let tables = [
7 "| a | b |\n|---+---|\n| 1 | |\n| 2 | |\n| 3 | |\n#+TBLFM: $2=$1*2\n",
8 "| x | y | sum |\n|---+---+-----|\n| 1 | 2 | |\n| 3.5 | 4 | |\n|---+---+-----|\n| | | |\n#+TBLFM: $3=$1+$2::@>$1=vsum(@I..@II)::@>$2=vmean(@2$2..@3$2)\n",
9 "| qty | price | total | share |\n|-----+-------+-------+-------|\n| 3 | 19.99 | | |\n| 1 | 5 | | |\n| 12 | 0.5 | | |\n|-----+-------+-------+-------|\n| | | | |\n#+TBLFM: $3=$1*$2;%.2f::$4=$3/@>$3;f3::@>$3=vsum(@I..@II)\n",
10 "| n | sq | cube | root |\n|---+----+------+------|\n| 1 | | | |\n| 2 | | | |\n| 10 | | | |\n#+TBLFM: $2=$1^2::$3=$1^3::$4=sqrt($1)\n",
11 "| a | b |\n| 1 | |\n| 2 | |\n| 7 | |\n#+TBLFM: $2=$1/2\n",
12 "| a | prev | next |\n|---+------+------|\n| 1 | | |\n| 2 | | |\n| 3 | | |\n#+TBLFM: $2=@-1$1::$3=@+1$1\n",
13 "| a | b | c |\n|---+---+---|\n| 1 | 2 | |\n| 4 | 5 | |\n#+TBLFM: $3=$-1*$-2::@2$3=100\n",
14 "| r | v |\n|---+---|\n| 1 | |\n| 2 | |\n| 3 | |\n#+TBLFM: @2$2..@4$2=$1*10\n",
15 "| r | v |\n|---+---|\n| 1 | |\n| 2 | |\n#+TBLFM: $>=@#*$#\n",
16 "| a | b | c |\n|---+---+---|\n| 1 | | |\n| | 2 | |\n#+TBLFM: $3=$1+$2\n",
17 "| a | b |\n|---+---|\n| 5 | |\n| 6 | |\n#+TBLFM: $2=$1*3\n#+TBLFM: $2=$1+100\n",
18 "| a | b |\n|---+---|\n| 1 |\n| 2 | |\n#+TBLFM: $3=$1*$1\n",
19 "| a | b | c |\n|---+---+---|\n| 1 | 2 | |\n| 3 | 4 | |\n#+TBLFM: $3=vsum($1..$2)::@2=7\n",
20 "| a | b |\n|---+---|\n| 1.25 | |\n| 0.1 | |\n| 1e3 | |\n| -2 | |\n#+TBLFM: $2=$1/7\n",
21 "| deg | sin | cos |\n|-----+-----+-----|\n| 0 | | |\n| 30 | | |\n| 45 | | |\n| 90 | | |\n#+TBLFM: $2=sin($1)::$3=cos($1);%.3f\n",
22 " | a | b |\n |---+---|\n | 2 | |\n | 3 | |\n #+TBLFM: $2=$1^0.5;n4\n",
23 "| a | b |\n|---+---|\n| x | |\n| 2 | |\n#+TBLFM: $2=$1*2\n",
24 "| a | b |\n|---+---|\n| 1 | |\n|---+---|\n| 2 | |\n| 3 | |\n#+TBLFM: $2=@I$1+@II$1+@-I$1\n",
25 "| a | b | c |\n|---+---+---|\n| 1 | 2 | |\n#+TBLFM: $3=($1+$2)*2/3-1::$1=$1\n",
26 "| a |\n|---|\n| 1 |\n| 2 |\n|---|\n| |\n#+TBLFM: @>$1=vmax(@I..@II)-vmin(@I..@II)+vcount(@I..@II)+vmedian(@I..@II)\n",
27 "#+CONSTANTS: c=10 k=2.5\n| a | b |\n|---+---|\n| 1 | |\n| 2 | |\n#+TBLFM: $2=$1*$c+$k\n",
28 "* Rates\n:PROPERTIES:\n:Rate: 3\n:END:\n| h | pay |\n|---+-----|\n| 2 | |\n#+TBLFM: $2=$1*$PROP_Rate\n",
29 ]
30
31 static func carets(_ text: String) -> [Int] {
32 let ns = text as NSString
33 var result: [Int] = []
34 var location = 0
35 while location < ns.length {
36 let line = ns.lineRange(for: NSRange(location: location, length: 0))
37 let content = ns.substring(with: line)
38 if let bar = content.firstIndex(of: "|"), !content.contains("|-") {
39 // Inside the first field of the row.
40 result.append(line.location + content.distance(from: content.startIndex, to: bar) + 2)
41 }
42 if content.uppercased().contains("#+TBLFM") { result.append(line.location + 3) }
43 location = NSMaxRange(line)
44 }
45 return result
46 }
47
48 @Test(.enabled(if: ProcessInfo.processInfo.environment["ORGSTAR_SKIP_ORACLE"] == nil))
49 func matchesEmacs() throws {
50 var cases: [EmacsOracle.Case] = []
51 var ours: [(text: String, caret: Int, failure: String?)] = []
52 for text in Self.tables {
53 for caret in Self.carets(text) {
54 let onTblfm = (text as NSString).substring(with: (text as NSString).lineRange(for: NSRange(location: caret, length: 0))).uppercased().contains("#+TBLFM")
55 let commands: [(any OrgCommand, String)] = onTblfm
56 ? [(TableRecalculate(all: true), "(org-ctrl-c-ctrl-c)")]
57 : [(TableRecalculate(all: true), "(org-table-recalculate t)"), (TableRecalculate(all: false), "(org-table-recalculate nil)")]
58 for (command, form) in commands {
59 cases.append(EmacsOracle.Case(text: text, point: EmacsOracle.point(caret, in: text), form: form))
60 ours.append(runCommand(command, text, caret: caret))
61 }
62 }
63 }
64 let emacs = try EmacsOracle.run(cases)
65 var fallbacks = 0
66 for (i, (mine, theirs)) in zip(ours, emacs).enumerated() {
67 if let failure = mine.failure, failure.hasPrefix("external:") {
68 fallbacks += 1
69 // Emacs gives symbolic results for text and division by zero.
70 #expect(failure.contains("non-numeric field") || failure.contains("division by zero"), "\(failure)")
71 continue
72 }
73 if !theirs.error.isEmpty {
74 // Emacs keeps the fields written before the error; nothing changes here.
75 #expect(mine.failure == theirs.error && mine.text == cases[i].text)
76 continue
77 }
78 let expected = (theirs.text, EmacsOracle.offset(theirs.point, in: theirs.text), theirs.error.isEmpty ? nil : theirs.error)
79 #expect(mine.text == expected.0 && mine.caret == expected.1 && mine.failure == expected.2,
80 "\(cases[i].form) at \(cases[i].point) in\n\(cases[i].text)\nours:\n\(mine.text)@\(mine.caret) \(mine.failure ?? "")\nemacs:\n\(theirs.text)@\(expected.1) \(theirs.error)")
81 }
82 #expect(fallbacks < ours.count / 2, "\(fallbacks) fallbacks")
83 }
84
85 /// `ORGSTAR_ORACLE_CORPUS=<folder>`: C-c C-c on every `#+TBLFM` line below the folder.
86 @Test(.enabled(if: ProcessInfo.processInfo.environment["ORGSTAR_ORACLE_CORPUS"] != nil))
87 func corpusMatchesEmacs() throws {
88 let root = URL(fileURLWithPath: ProcessInfo.processInfo.environment["ORGSTAR_ORACLE_CORPUS"]!)
89 let files = FileManager.default.enumerator(at: root, includingPropertiesForKeys: nil)!
90 .compactMap { $0 as? URL }.filter { $0.pathExtension == "org" }.sorted { $0.path < $1.path }
91 var cases: [EmacsOracle.Case] = []
92 var ours: [(text: String, caret: Int, failure: String?)] = []
93 for file in files {
94 guard let text = try? String(contentsOf: file, encoding: .utf8), text.uppercased().contains("#+TBLFM") else { continue }
95 let ns = text as NSString
96 var location = 0
97 while location < ns.length {
98 let line = ns.lineRange(for: NSRange(location: location, length: 0))
99 if ns.substring(with: line).trimmingCharacters(in: .whitespaces).uppercased().hasPrefix("#+TBLFM") {
100 cases.append(EmacsOracle.Case(text: text, point: EmacsOracle.point(line.location, in: text), form: "(org-ctrl-c-ctrl-c)"))
101 ours.append(runCommand(TableRecalculate(all: true), text, caret: line.location))
102 }
103 location = NSMaxRange(line)
104 }
105 }
106 let emacs = try EmacsOracle.run(cases)
107 for (mine, theirs) in zip(ours, emacs) where !(mine.failure ?? "").hasPrefix("external:") {
108 #expect(mine.text == theirs.text, "\(mine.failure ?? "")")
109 }
110 print("TBLFM lines: \(cases.count), native: \(ours.filter { !($0.failure ?? "").hasPrefix("external:") }.count)")
111 }
112}