Tests/OrgCoreTests/AgendaSearchTests.swift

eba8a09f88f681c8e9a6bb7aeab5fbdc303c4e16
orgstar/Tests/OrgCoreTests/AgendaSearchTests.swift

115 lines · 5069 bytes

  1import Foundation
  2import Testing
  3@testable import OrgCore
  4
  5/// `org-search-view` against Emacs.
  6struct AgendaSearchTests {
  7    static let first = """
  8    #+TODO: TODO NEXT | DONE
  9    Rent before the first heading.
 10    * TODO [#A] Pay rent :home:
 11    SCHEDULED: <2026-10-01 Thu>
 12    Ask the landlord about the boiler.
 13    * Meeting 10:00 with Bob
 14    Notes about the end of
 15    the line, and Bob's plan.
 16    * DONE Paid the rent
 17    * NEXT Report :work:
 18    ** Section with alpha and beta
 19    Alpha only here, and a boilerplate.
 20    ** Section with alpha
 21    Gamma.
 22    * todo list of chores
 23    Water the plants, alpha.
 24    * Archived :ARCHIVE:
 25    The rent is here too.
 26    ** TODO Under archived rent
 27    * COMMENT Commented rent
 28    * Two words
 29    The phrase two   words
 30    spans here; foo_bar and foo-bar.
 31    * The last
 32    word ends this entry with last
 33    * word starts the next heading
 34    * Ünïcode tïtle :tag:
 35    Straße and STRASSE.
 36    """
 37
 38    static let second = """
 39    #+CATEGORY: other
 40    * WAIT Second file rent
 41    * Plans
 42    alpha, beta, gamma in the body.
 43    ** HOLD Nested beta
 44    * Headline alpha
 45    """
 46
 47    static let strings = [
 48        // Phrases.
 49        "rent", "RENT", "pay rent", "end of the line", "end of\nthe", "two words", "last word",
 50        // A match ending in the next heading's line selects that entry.
 51        "last * word","Bob's", "boiler",
 52        "straße", "foo_bar", "10:00", "nothing at all", "alpha, beta",
 53        // Boolean.
 54        "+alpha", "+alpha -beta", "+alpha +gamma", "-alpha", "+beta -nested", "{alph?a}", "+{^Gam}", "-{b[eo]}",
 55        "+{\\<rent\\>}", "+\"two words\"", "+\"two   words\"", "+\"the line\"", "{boiler\\b}", "+alpha +\"in the body\"",
 56        "+{foo\\s-bar}", "+boiler -plate", "+{a b}", "+{alpha} -{x y}",
 57        // Headlines only.
 58        "*rent", "*alpha", "*+alpha -beta", "*+section", "*-alpha", "*pay rent", "*with alpha and",
 59        // TODO only and whole words.
 60        "!rent", "!+alpha", "*!section", ":+boiler", ":+alpha", "+:boiler", ":boiler", "*!:+rent",
 61    ]
 62
 63    static func lisp(_ s: String) -> String {
 64        "\"" + s.replacingOccurrences(of: "\\", with: "\\\\").replacingOccurrences(of: "\"", with: "\\\"").replacingOccurrences(of: "\n", with: "\\n") + "\""
 65    }
 66
 67    static let files = [("a.org", first), ("b.org", second)]
 68    static let today = Days.absolute(year: 2026, month: 10, day: 5)
 69
 70    @Test(.enabled(if: ProcessInfo.processInfo.environment["ORGSTAR_SKIP_ORACLE"] == nil))
 71    func searchMatchesEmacs() throws {
 72        for string in Self.strings {
 73            for todoOnly in [false, true] {
 74                try AgendaOracle.compareList(files: Self.files, form: "(org-search-view \(todoOnly ? "t" : "nil") \(Self.lisp(string)))", today: Self.today) {
 75                    Agenda.textSearch($0, string: string, todoOnly: todoOnly)
 76                }
 77            }
 78        }
 79    }
 80
 81    @Test(.enabled(if: ProcessInfo.processInfo.environment["ORGSTAR_SKIP_ORACLE"] == nil))
 82    func searchOptionsMatchEmacs() throws {
 83        for string in ["boiler", "+boiler", "alpha beta", "+alpha -beta", "{^Gam}", "two words", "*rent", "rent -paid", "+\"two words\""] {
 84            for (boolean, full) in [(true, false), (false, true), (true, true)] {
 85                var options = AgendaOptions()
 86                options.searchAlwaysBoolean = boolean
 87                options.searchForceFullWords = full
 88                let form = "(let ((org-agenda-search-view-always-boolean \(boolean ? "t" : "nil")) (org-agenda-search-view-force-full-words \(full ? "t" : "nil"))) (org-search-view nil \(Self.lisp(string))))"
 89                try AgendaOracle.compareList(files: Self.files, form: form, today: Self.today) {
 90                    Agenda.textSearch($0, string: string, options: options)
 91                }
 92            }
 93        }
 94    }
 95
 96    @Test(.enabled(if: ProcessInfo.processInfo.environment["ORGSTAR_SKIP_ORACLE"] == nil))
 97    func searchPrefixMatchesEmacs() throws {
 98        let formats = ["search": "%c %?-5:l %b", "agenda": " %i %-12:c%?-12t% s", "todo": " %i %-12:c", "tags": " %i %-12:c"]
 99        var options = AgendaOptions()
100        options.prefixFormat = formats
101        let form = "(let ((org-agenda-prefix-format \(AgendaOracle.prefixLisp(formats)))) (org-search-view nil \"alpha\"))"
102        try AgendaOracle.compareList(files: Self.files, form: form, today: Self.today) {
103            Agenda.textSearch($0, string: "alpha", options: options)
104        }
105    }
106
107    @Test func parsesSnippets() {
108        let query = Agenda.SearchQuery("*!:+alpha -\"two words\" {a b} back\\ slash", todoOnly: false, options: AgendaOptions())
109        #expect(query.headlinesOnly && query.todoOnly && query.boolean)
110        #expect(query.positive.map(\.emacs) == ["\\<back slash\\>","\\<alpha\\>", "a b"])
111        #expect(query.negative.map(\.emacs) == ["\\<two words\\>"])
112        let phrase = Agenda.SearchQuery("pay  rent.", todoOnly: false, options: AgendaOptions())
113        #expect(!phrase.boolean && phrase.positive.map(\.emacs) == ["pay\\s-+rent\\."])
114    }
115}
Keyboard shortcuts

Global

⌘K
Command palette & jump
/
Focus search
?
Show this help
g d
Dashboard
g e
Explore repositories
g n
Notifications

Repository & editor

t
Find file in repository
y
Copy immutable SHA permalink
g c
Code tab
g i
Issues tab
g m
Merge requests tab
g b
Builds tab
⌘↵
Submit active form