Tests/OrgCoreTests/StructureCommandTests.swift
103 lines · 5159 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
40struct StructureOracleTests {
41 static let variants = [
42 "* a\nbody\n* b\nmore\n",
43 "* a\n\n* b\n\n* c\n",
44 "intro\n* a\n** b\ntext\n** c\n* d\n",
45 "* TODO a :x:\n** DONE b\n* c :y:\n",
46 "* a\n** b\n*** c\n** d\n",
47 "* a\n* b",
48 "text\nmore\n",
49 "\n* a\n\nbody\n\n* b\n",
50 "* TODO [#A] title here :t:\nbody\n",
51 "* a 😀 b\n** c\n",
52 ]
53
54 static let commands: [(command: any OrgCommand, form: String, headingLineOnly: Bool)] = [
55 (InsertHeading(), "(org-insert-heading)", false),
56 (InsertHeadingAfterSubtree(), "(org-insert-heading-respect-content)", false),
57 (InsertTodoHeading(), "(org-insert-todo-heading nil)", false),
58 (MoveSubtreeUp(), "(org-move-subtree-up)", false),
59 (MoveSubtreeDown(), "(org-move-subtree-down)", false),
60 (PromoteSubtree(), "(org-promote-subtree)", false),
61 (DemoteSubtree(), "(org-demote-subtree)", false),
62 (NextHeading(), "(org-next-visible-heading 1)", false),
63 (PreviousHeading(), "(org-previous-visible-heading 1)", false),
64 (ForwardSameLevel(), "(org-forward-heading-same-level 1)", false),
65 (BackwardSameLevel(), "(org-backward-heading-same-level 1)", false),
66 (UpHeading(), "(outline-up-heading 1)", false),
67 ]
68
69 @Test(.enabled(if: ProcessInfo.processInfo.environment["ORGSTAR_SKIP_ORACLE"] == nil))
70 func structureCommandsMatchEmacs() throws {
71 try compareWithEmacs(Self.variants.map { ($0, EmacsOracle.positions($0)) }, Self.commands)
72 }
73
74 /// Real files: `ORGSTAR_ORACLE_CORPUS=<folder>` runs every structure command in files up to
75 /// 20 KB, with the caret at the start and end of up to eight heading lines per file and on
76 /// a body line, for up to 60 files.
77 @Test(.enabled(if: ProcessInfo.processInfo.environment["ORGSTAR_ORACLE_CORPUS"] != nil))
78 func structureCommandsMatchEmacsOnACorpus() throws {
79 let root = URL(fileURLWithPath: ProcessInfo.processInfo.environment["ORGSTAR_ORACLE_CORPUS"]!)
80 let files = FileManager.default.enumerator(at: root, includingPropertiesForKeys: nil)!
81 .compactMap { $0 as? URL }.filter { $0.pathExtension == "org" }.sorted { $0.path < $1.path }
82 var samples: [(String, [Int])] = []
83 for file in files where samples.count < 60 {
84 guard let text = try? String(contentsOf: file, encoding: .utf8), text.utf16.count <= 20_000, !text.contains("\r") else { continue }
85 let ns = text as NSString
86 var carets: [Int] = []
87 var location = 0
88 while location < ns.length, carets.count < 17 {
89 let line = ns.lineRange(for: NSRange(location: location, length: 0))
90 let content = ns.substring(with: line)
91 if content.hasPrefix("* ") || content.hasPrefix("** ") || content.hasPrefix("*** ") {
92 carets.append(line.location)
93 carets.append(NSMaxRange(line) - (content.hasSuffix("\n") ? 1 : 0))
94 } else if carets.count == 2 {
95 carets.append(line.location)
96 }
97 location = NSMaxRange(line)
98 }
99 if !carets.isEmpty { samples.append((text, carets)) }
100 }
101 try compareWithEmacs(samples, Self.commands)
102 }
103}