Tests/OrgAppTests/SuggestionRowTests.swift

f24016fbc9384e5bae26abb48db212a9330b488b
orgstar/Tests/OrgAppTests/SuggestionRowTests.swift

62 lines · 2849 bytes

 1import Foundation
 2import Testing
 3import OrgCore
 4@testable import OrgApp
 5
 6/// The iOS suggestion row (#145).
 7struct SuggestionRowTests {
 8    func context(_ text: String, options: EditingOptions = .org) -> EditContext {
 9        let caret = text.utf16.count
10        return EditContext(revision: 0, text: text, tree: OrgParser.parse(text), selection: [caret..<caret], options: options)
11    }
12
13    @Test func rowOnlyWithTheOnScreenKeyboard() {
14        #expect(SuggestionRow.usesRow(hardwareKeyboard: false))
15        #expect(!SuggestionRow.usesRow(hardwareKeyboard: true))
16    }
17
18    @Test func showsWhileThereAreCandidates() {
19        let completion = OrgCompletion.at(context("#+TIT"))
20        #expect(SuggestionRow.shows(completion, dismissed: false))
21        #expect(!SuggestionRow.shows(completion, dismissed: true))
22        #expect(!SuggestionRow.shows(nil, dismissed: false))
23        // Nothing matches: the key bar stays.
24        #expect(!SuggestionRow.shows(OrgCompletion.at(context("#+XYZQ")), dismissed: false))
25    }
26
27    @Test func showsWhenTheListWouldOpenByItself() {
28        let options = CompletionOptions(auto: true, prefix: 3)
29        for text in ["#+TI", "#+TIT"] {
30            let completion = OrgCompletion.at(context(text))
31            let opened = CompletionAuto.opens(completion, options) ? completion : nil
32            #expect(SuggestionRow.shows(opened, dismissed: false) == (text == "#+TIT"))
33        }
34    }
35
36    @Test func itemsFollowTheListsOrder() throws {
37        let completion = try #require(OrgCompletion.at(context("#+TIT")))
38        let items = SuggestionRow.items(completion)
39        #expect(items.map(\.candidate) == completion.candidates)
40        #expect(items.allSatisfy { !$0.snippet && $0.detail == nil })
41        #expect(SuggestionRow.items(completion, limit: 1).count == min(1, completion.candidates.count))
42    }
43
44    @Test func snippetsWithTheirNames() throws {
45        var options = EditingOptions.org
46        options.snippets = [Snippet(trigger: "meeting", name: "Meeting notes", body: "x"), Snippet(trigger: "mail", body: "y")]
47        let completion = try #require(OrgCompletion.at(context("m", options: options)))
48        #expect(completion.kind == .snippet)
49        let items = SuggestionRow.items(completion)
50        #expect(Set(items.map(\.candidate)) == ["meeting", "mail"])
51        #expect(items.allSatisfy { $0.snippet })
52        #expect(items.first { $0.candidate == "meeting" }?.detail == "Meeting notes")
53        #expect(items.first { $0.candidate == "mail" }?.detail == nil)
54    }
55
56    @Test func templatesWithTheirBlocks() throws {
57        let completion = try #require(OrgCompletion.at(context("<s")))
58        #expect(completion.kind == .template)
59        let item = try #require(SuggestionRow.items(completion).first { $0.candidate == "<s" })
60        #expect(item.detail == "src")
61    }
62}
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