64 lines
1.9 KiB
Swift
64 lines
1.9 KiB
Swift
import Foundation
|
|
import Testing
|
|
@testable import SstCore
|
|
|
|
final class FakePasteboard: PasteboardProviding {
|
|
var items: [PasteboardItem] = []
|
|
|
|
func readItems() -> [PasteboardItem] { items }
|
|
func writeString(_ text: String) {
|
|
items = [["public.utf8-plain-text": Data(text.utf8)]]
|
|
}
|
|
func writeItems(_ items: [PasteboardItem]) { self.items = items }
|
|
}
|
|
|
|
final class FakePaster: KeystrokePosting {
|
|
var shouldFail = false
|
|
var onPaste: (() -> Void)?
|
|
private(set) var pasteCount = 0
|
|
|
|
func postPaste() throws {
|
|
pasteCount += 1
|
|
if shouldFail { throw TestError() }
|
|
onPaste?()
|
|
}
|
|
}
|
|
|
|
struct TextInserterTests {
|
|
let pasteboard = FakePasteboard()
|
|
let paster = FakePaster()
|
|
|
|
func makeInserter() -> TextInserter {
|
|
TextInserter(pasteboard: pasteboard, paster: paster, restoreDelay: .zero)
|
|
}
|
|
|
|
@Test func pastesTextThenRestoresPreviousClipboard() async throws {
|
|
let original: [PasteboardItem] = [["public.utf8-plain-text": Data("oud".utf8)]]
|
|
pasteboard.items = original
|
|
var seenAtPaste: Data?
|
|
paster.onPaste = { seenAtPaste = pasteboard.items.first?["public.utf8-plain-text"] }
|
|
|
|
try await makeInserter().insert("hallo")
|
|
|
|
#expect(seenAtPaste == Data("hallo".utf8))
|
|
#expect(paster.pasteCount == 1)
|
|
#expect(pasteboard.items == original)
|
|
}
|
|
|
|
@Test func failedPasteLeavesTextOnClipboard() async {
|
|
pasteboard.items = [["public.utf8-plain-text": Data("oud".utf8)]]
|
|
paster.shouldFail = true
|
|
|
|
await #expect(throws: TestError.self) {
|
|
try await makeInserter().insert("hallo")
|
|
}
|
|
|
|
#expect(pasteboard.items.first?["public.utf8-plain-text"] == Data("hallo".utf8))
|
|
}
|
|
|
|
@Test func passthroughCleanerReturnsTextUnchanged() async throws {
|
|
let result = try await PassthroughCleaner().clean("hoi", context: CleaningContext())
|
|
#expect(result == "hoi")
|
|
}
|
|
}
|