Tests/OrgCoreTests/EntryFieldsTests.swift
85 lines · 3853 bytes
1import Foundation
2import Testing
3@testable import OrgCore
4
5struct EntryFieldsTests {
6 static let text = """
7 * TODO [#A] Call bank :work:home:
8 SCHEDULED: <2026-10-17 Sat 18:00> DEADLINE: <2026-10-20 Tue>
9 :PROPERTIES:
10 :ID: x
11 :END:
12 :LOGBOOK:
13 CLOCK: [2026-10-16 Fri 09:00]--[2026-10-16 Fri 09:30] => 0:30
14 :END:
15 Line one
16 Line two
17
18 ** Child
19 child body
20
21 """
22
23 @Test func readsTheHeadingPlanningAndNotes() throws {
24 let fields = try #require(EntryFields.read(Self.text, at: 0, settings: .default))
25 #expect(fields.todo == "TODO")
26 #expect(fields.priority == "A")
27 #expect(fields.title == "Call bank")
28 #expect(fields.tags == ["work", "home"])
29 #expect(fields.scheduled == "2026-10-17 Sat 18:00")
30 #expect(fields.deadline == "2026-10-20 Tue")
31 #expect(fields.notes == "Line one\nLine two")
32 }
33
34 @Test func readsAPlainHeading() throws {
35 let fields = try #require(EntryFields.read("* Just a title\n", at: 0, settings: .default))
36 #expect(fields == EntryFields(todo: nil, priority: nil, title: "Just a title", tags: [], scheduled: nil, deadline: nil, notes: ""))
37 #expect(EntryFields.read("no heading\n", at: 0, settings: .default) == nil)
38 }
39
40 @Test func setsTheTitleKeepingKeywordCookieAndTags() {
41 let result = runCommand(SetHeadingTitle(title: "Call the bank"), Self.text, caret: 0)
42 #expect(result.failure == nil)
43 let line = result.text.prefix { $0 != "\n" }
44 #expect(line.hasPrefix("* TODO [#A] Call the bank "))
45 #expect(line.hasSuffix(" :work:home:"))
46 #expect(runCommand(SetHeadingTitle(title: "Named"), "* TODO\nbody\n", caret: 0).text == "* TODO Named\nbody\n")
47 #expect(runCommand(SetHeadingTitle(title: " Trimmed "), "* Old\n", caret: 0).text == "* Trimmed\n")
48 #expect(runCommand(SetHeadingTitle(title: "Two\nlines\n"), "* Old\n", caret: 0).text == "* Two lines\n")
49 }
50
51 @Test func replacesNotesKeepingMetaDataAndChildren() {
52 let result = runCommand(SetEntryNotes(notes: "New note\n\n"), Self.text, caret: 0)
53 #expect(result.failure == nil)
54 #expect(result.text == Self.text.replacingOccurrences(of: "Line one\nLine two\n", with: "New note\n"))
55 let cleared = runCommand(SetEntryNotes(notes: ""), Self.text, caret: 0).text
56 #expect(cleared == Self.text.replacingOccurrences(of: "Line one\nLine two\n", with: ""))
57 }
58
59 @Test func addsNotesWhereThereWereNone() {
60 #expect(runCommand(SetEntryNotes(notes: "Body"), "* A\n* B\n", caret: 0).text == "* A\nBody\n* B\n")
61 #expect(runCommand(SetEntryNotes(notes: "Body"), "* A", caret: 0).text == "* A\nBody\n")
62 #expect(runCommand(SetEntryNotes(notes: "Body"), "* A\nSCHEDULED: <2026-10-17 Sat>\n", caret: 0).text == "* A\nSCHEDULED: <2026-10-17 Sat>\nBody\n")
63 }
64}
65
66struct ReplaceChangedTextTests {
67 @Test func writesTheDifferenceAsOneEdit() throws {
68 let old = "* TODO Call bank\nbody\n* Next\n"
69 let new = "* DONE Call bank\nbody\nmore\n* Next\n"
70 let command = try #require(ReplaceChangedText(from: old, to: new))
71 #expect(command.edit.range == 2..<21)
72 #expect(runCommand(command, old, caret: 0).text == new)
73 #expect(ReplaceChangedText(from: old, to: old) == nil)
74 }
75
76 @Test func refusesTextThatChangedSince() throws {
77 let command = try #require(ReplaceChangedText(from: "* TODO A\n", to: "* DONE A\n"))
78 #expect(runCommand(command, "* WAIT A\n", caret: 0).failure != nil)
79 }
80
81 @Test func keepsSurrogatePairsWhole() throws {
82 let command = try #require(ReplaceChangedText(from: "* 😀\n", to: "* 😃\n"))
83 #expect(runCommand(command, "* 😀\n", caret: 0).text == "* 😃\n")
84 }
85}