Tests/OrgCoreTests/StructureCommandTests.swift
124 lines · 6475 bytes
1import Foundation
2import Testing
3@testable import OrgCore
4
5struct StructureCommandTests {
6 @Test func insertHeading() {
7 #expect(runCommand(InsertHeading(), "* a\nbody\n", caret: 3).text == "* a\n* \nbody\n")
8 #expect(runCommand(InsertHeading(), "* ab\n", caret: 3).text == "* a\n* b\n")
9 #expect(runCommand(InsertHeadingAfterSubtree(), "* a\nbody\n** b\n* c\n", caret: 1).text == "* a\nbody\n** b\n* \n* c\n")
10 #expect(runCommand(InsertTodoHeading(), "* TODO a\n", caret: 8).text == "* TODO a\n* TODO \n")
11 }
12
13 @Test func moveAndLevels() {
14 #expect(runCommand(MoveSubtreeDown(), "* a\nx\n* b\ny\n", caret: 0).text == "* b\ny\n* a\nx\n")
15 #expect(runCommand(MoveSubtreeUp(), "* a\n* b\n", caret: 0).failure != nil)
16 #expect(runCommand(DemoteSubtree(), "* a\n** b\n* c\n", caret: 0).text == "** a\n*** b\n* c\n")
17 #expect(runCommand(PromoteSubtree(), "* a\n", caret: 0).failure != nil)
18 }
19
20 @Test func motion() {
21 let text = "intro\n* a\n** b\n** c\n* d\n"
22 #expect(runCommand(NextHeading(), text, caret: 0).caret == 6)
23 #expect(runCommand(ForwardSameLevel(), text, caret: 10).caret == 15)
24 #expect(runCommand(BackwardSameLevel(), text, caret: 16).caret == 10)
25 #expect(runCommand(UpHeading(), text, caret: 16).caret == 6)
26 #expect(runCommand(UpHeading(), text, caret: 6).failure != nil)
27 }
28
29 @Test func nextHeadingSkipsHiddenOnes() {
30 let text = "* a\n** b\n* c\n"
31 let context = EditContext(revision: 0, text: text, tree: OrgParser.parse(text), selection: [0..<0], hidden: [3..<8])
32 guard case .commit(let result) = NextHeading().run(in: context) else {
33 Issue.record("expected a commit")
34 return
35 }
36 #expect(result.selection == [9..<9])
37 }
38
39 /// The caret at the end of a folded heading stands for Emacs's point behind the ellipsis:
40 /// the new heading goes after the subtree, not between the heading and its folded body.
41 @Test func newHeadingAtTheEndOfAFoldedHeadingGoesAfterTheSubtree() {
42 let text = "* One\nbody one\n** One child\nchild body\n* Two\nbody two\n"
43 // As the editors hide a folded heading: the lines after it, to its section's end.
44 let fold = 6..<(text as NSString).range(of: "* Two").location
45 let context = EditContext(revision: 0, text: text, tree: OrgParser.parse(text), selection: [5..<5], hidden: [fold])
46 guard case .commit(let result) = InsertHeading().run(in: context) else {
47 Issue.record("expected a commit")
48 return
49 }
50 let new = result.edits.sorted { $0.range.lowerBound > $1.range.lowerBound }.reduce(text) { $1.apply(to: $0) }
51 #expect(new == "* One\nbody one\n** One child\nchild body\n* \n* Two\nbody two\n")
52 }
53}
54
55struct StructureOracleTests {
56 static let variants = [
57 "* a\nbody\n* b\nmore\n",
58 "* a\n\n* b\n\n* c\n",
59 "intro\n* a\n** b\ntext\n** c\n* d\n",
60 "* TODO a :x:\n** DONE b\n* c :y:\n",
61 "* a\n** b\n*** c\n** d\n",
62 "* a\n* b",
63 "text\nmore\n",
64 "\n* a\n\nbody\n\n* b\n",
65 "* TODO [#A] title here :t:\nbody\n",
66 "* a 😀 b\n** c\n",
67 "* P [0/1]\n** TODO a\n",
68 ]
69
70 static let commands: [(command: any OrgCommand, form: String, headingLineOnly: Bool)] = [
71 (InsertHeading(), "(org-insert-heading)", false),
72 (InsertHeadingAfterSubtree(), "(org-insert-heading-respect-content)", false),
73 (InsertTodoHeading(), "(org-insert-todo-heading nil)", false),
74 (MoveSubtreeUp(), "(org-move-subtree-up)", false),
75 (MoveSubtreeDown(), "(org-move-subtree-down)", false),
76 (PromoteSubtree(), "(org-promote-subtree)", false),
77 (DemoteSubtree(), "(org-demote-subtree)", false),
78 (NextHeading(), "(org-next-visible-heading 1)", false),
79 (PreviousHeading(), "(org-previous-visible-heading 1)", false),
80 (ForwardSameLevel(), "(org-forward-heading-same-level 1)", false),
81 (BackwardSameLevel(), "(org-backward-heading-same-level 1)", false),
82 (UpHeading(), "(outline-up-heading 1)", false),
83 ]
84
85 @Test(.enabled(if: ProcessInfo.processInfo.environment["ORGSTAR_SKIP_ORACLE"] == nil))
86 func structureCommandsMatchEmacs() throws {
87 try compareWithEmacs(Self.variants.map { ($0, EmacsOracle.positions($0)) }, Self.commands)
88 }
89
90 @Test(.enabled(if: ProcessInfo.processInfo.environment["ORGSTAR_SKIP_ORACLE"] == nil))
91 func structureCommandsMatchEmacsWithDoomOptions() throws {
92 try compareWithEmacs(Self.variants.map { ($0, EmacsOracle.positions($0)) }, Self.commands, options: doomOptions)
93 }
94
95 /// Real files: `ORGSTAR_ORACLE_CORPUS=<folder>` runs every structure command in files up to
96 /// 20 KB, with the caret at the start and end of up to eight heading lines per file and on
97 /// a body line, for up to 60 files.
98 @Test(.enabled(if: ProcessInfo.processInfo.environment["ORGSTAR_ORACLE_CORPUS"] != nil))
99 func structureCommandsMatchEmacsOnACorpus() throws {
100 let root = URL(fileURLWithPath: ProcessInfo.processInfo.environment["ORGSTAR_ORACLE_CORPUS"]!)
101 let files = FileManager.default.enumerator(at: root, includingPropertiesForKeys: nil)!
102 .compactMap { $0 as? URL }.filter { $0.pathExtension == "org" }.sorted { $0.path < $1.path }
103 var samples: [(String, [Int])] = []
104 for file in files where samples.count < 60 {
105 guard let text = try? String(contentsOf: file, encoding: .utf8), text.utf16.count <= 20_000, !text.contains("\r") else { continue }
106 let ns = text as NSString
107 var carets: [Int] = []
108 var location = 0
109 while location < ns.length, carets.count < 17 {
110 let line = ns.lineRange(for: NSRange(location: location, length: 0))
111 let content = ns.substring(with: line)
112 if content.hasPrefix("* ") || content.hasPrefix("** ") || content.hasPrefix("*** ") {
113 carets.append(line.location)
114 carets.append(NSMaxRange(line) - (content.hasSuffix("\n") ? 1 : 0))
115 } else if carets.count == 2 {
116 carets.append(line.location)
117 }
118 location = NSMaxRange(line)
119 }
120 if !carets.isEmpty { samples.append((text, carets)) }
121 }
122 try compareWithEmacs(samples, Self.commands)
123 }
124}