Files
2026-11-25 07:14:49 +01:00

85 lines
3.2 KiB
Swift

import AppKit
import PlakCore
@testable import PlakUI
import Testing
/// `#lorem` typed at the start of a line becomes lorem text.
@MainActor
@Suite struct LoremTests {
private func parts(_ text: String) -> [String] {
text.components(separatedBy: "\n\n")
}
@Test("typing #lorem at the start of a line replaces it with lorem text in seven blocks, caret at the end")
func typedAtLineStart() throws {
let page = try TestPage()
page.type("#lorem")
let text = page.textView.string
#expect(!text.contains(LoremText.trigger))
#expect(parts(text).count == 7)
#expect(parts(text)[0] == "=lorem")
#expect(LoremText.wordCount(parts(text)[1]) == 16)
#expect(page.textView.selectedRange() == NSRange(location: page.textView.length, length: 0))
}
@Test("lorem text goes through the normal change path: its seven blocks, =lorem included, get a created date")
func stampsDates() throws {
let page = try TestPage()
page.type("#lorem")
let now = dateLine(2026, 10, 17, 10, 0)
#expect(page.fileText.components(separatedBy: now).count - 1 == 7)
#expect(page.fileText.hasPrefix("\(now)\n=lorem\n\n\(now)\n"))
}
@Test("typed lorem text starts with the bookmark lorem, in the bookmark index with the date of its block")
func bookmark() throws {
let page = try TestPage()
page.type("#lorem")
#expect(page.textView.bookmarks.map(\.title) == ["lorem"])
#expect(page.textView.bookmarks.first?.range.location == 0)
#expect(page.page.bookmarkIndex.labels == ["lorem - 17 okt 10:00", "end"])
}
@Test("on a later line, with text after #lorem, the text stays after the lorem text")
func keepsTextAfter() throws {
let page = try TestPage("first\n\ntail")
page.caret(7)
page.type("#lorem")
let text = page.textView.string
#expect(text.hasPrefix("first\n\n"))
#expect(text.hasSuffix("tail"))
#expect(!text.contains(LoremText.trigger))
#expect(parts(text).count == 8)
#expect(page.textView.selectedRange() == NSRange(location: page.textView.length - 4, length: 0))
}
@Test("#lorem after other text on the line stays as typed")
func notAtLineStart() throws {
let page = try TestPage()
page.type("a #lorem")
#expect(page.textView.string == "a #lorem")
page.type("\n #lorem")
#expect(page.textView.string == "a #lorem\n #lorem")
}
@Test("pasting #lorem stays as pasted")
func pasteStays() throws {
let page = try TestPage()
let board = NSPasteboard(name: NSPasteboard.Name("plak-tests-\(UUID().uuidString)"))
defer { board.releaseGlobally() }
board.clearContents()
board.setString("#lorem", forType: .string)
page.textView.paste(from: board)
#expect(page.textView.string == "#lorem")
}
@Test("one undo brings back #lorem")
func undoRestoresTrigger() throws {
let page = try TestPage()
page.type("#lorem")
#expect(page.textView.string != "#lorem")
try #require(page.textView.undoManager).undo()
#expect(page.textView.string == "#lorem")
}
}