Files
VCCPlusOSS/platforms/macos/Tests/CMOCCompletionEditorTests.swift

94 lines
5.5 KiB
Swift

// SPDX-License-Identifier: GPL-3.0-or-later
import XCTest
import AppKit
final class CMOCCompletionEditorTests: XCTestCase {
@MainActor private final class UndoDelegate: NSObject, NSTextViewDelegate {
let manager = UndoManager()
func undoManager(for view: NSTextView) -> UndoManager? { manager }
}
@MainActor private func fixture(_ body: (CMOCCompletionTextView, UndoManager) -> Void) {
let delegate = UndoDelegate()
let editor = CMOCEditingTextView(frame: NSRect(x: 0, y: 0, width: 400, height: 200))
editor.delegate = delegate; editor.isRichText = false; editor.allowsUndo = true; editor.isEditable = true
editor.string = "int main(void) { prinOld; }"
editor.setSelectedRange(NSRange(location: 20, length: 0)) // pri|nOld
editor.completionProvider = { _, _ in [
.init(name: "printf", detail: "Function", conditional: false,
replacementRange: NSRange(location: 17, length: 7), insertion: "printf")
] }
body(editor, delegate.manager)
}
@MainActor func testFinalAcceptanceReplacesWholeIdentifierAndHasOneUndoRedo() {
fixture { editor, undo in
let original = editor.string
XCTAssertTrue(editor.prepareCompletion())
let range = editor.rangeForUserCompletion
editor.insertCompletion("printf", forPartialWordRange: range, movement: NSTextMovement.return.rawValue, isFinal: true)
XCTAssertEqual(editor.string, "int main(void) { printf; }")
XCTAssertEqual(editor.selectedRange().location, 23)
XCTAssertTrue(undo.canUndo)
undo.undo(); XCTAssertEqual(editor.string, original)
XCTAssertFalse(undo.canUndo)
undo.redo(); XCTAssertEqual(editor.string, "int main(void) { printf; }")
}
}
@MainActor func testPreviewAndFinalCancellationDoNotModifyTextOrUndo() {
fixture { editor, undo in
let original = editor.string
XCTAssertTrue(editor.prepareCompletion())
let range = editor.rangeForUserCompletion
editor.insertCompletion("printf", forPartialWordRange: range, movement: NSTextMovement.down.rawValue, isFinal: false)
XCTAssertEqual(editor.string, original); XCTAssertFalse(undo.canUndo)
editor.insertCompletion("printf", forPartialWordRange: range, movement: NSTextMovement.cancel.rawValue, isFinal: true)
XCTAssertEqual(editor.string, original); XCTAssertFalse(undo.canUndo)
}
}
@MainActor func testChangedTextOrCaretRejectsStaleCompletion() {
fixture { editor, _ in
XCTAssertTrue(editor.prepareCompletion()); let range = editor.rangeForUserCompletion
editor.setSelectedRange(NSRange(location: 0, length: 0))
editor.insertCompletion("printf", forPartialWordRange: range, movement: NSTextMovement.return.rawValue, isFinal: true)
XCTAssertEqual(editor.string, "int main(void) { prinOld; }")
editor.setSelectedRange(NSRange(location: 20, length: 0))
XCTAssertTrue(editor.prepareCompletion())
editor.string = "extern int other;"
editor.insertCompletion("printf", forPartialWordRange: range, movement: NSTextMovement.return.rawValue, isFinal: true)
XCTAssertEqual(editor.string, "extern int other;")
}
}
@MainActor func testReadOnlySelectionAndInvalidatedRequestCannotInsert() {
fixture { editor, _ in
editor.isEditable = false; XCTAssertFalse(editor.prepareCompletion())
editor.isEditable = true; editor.setSelectedRange(NSRange(location: 17, length: 7))
XCTAssertFalse(editor.prepareCompletion())
editor.setSelectedRange(NSRange(location: 20, length: 0)); XCTAssertTrue(editor.prepareCompletion())
let range = editor.rangeForUserCompletion
editor.invalidateCompletion()
editor.insertCompletion("printf", forPartialWordRange: range, movement: NSTextMovement.return.rawValue, isFinal: true)
XCTAssertEqual(editor.string, "int main(void) { prinOld; }")
XCTAssertTrue(editor.prepareCompletion()); editor.isEditable = false
editor.insertCompletion("printf", forPartialWordRange: range, movement: NSTextMovement.return.rawValue, isFinal: true)
XCTAssertEqual(editor.string, "int main(void) { prinOld; }")
}
}
@MainActor func testUnlistedCandidateAndWrongReplacementRangeCannotInsert() {
fixture { editor, _ in
XCTAssertTrue(editor.prepareCompletion()); let range = editor.rangeForUserCompletion
editor.insertCompletion("system", forPartialWordRange: range, movement: NSTextMovement.return.rawValue, isFinal: true)
XCTAssertEqual(editor.string, "int main(void) { prinOld; }")
XCTAssertTrue(editor.prepareCompletion())
editor.insertCompletion("printf", forPartialWordRange: NSRange(location: 0, length: 7), movement: NSTextMovement.return.rawValue, isFinal: true)
XCTAssertEqual(editor.string, "int main(void) { prinOld; }")
}
}
@MainActor func testOrdinaryTypingAndLineBreakUseNativeEditingWithoutAssistance() {
fixture { editor, undo in
editor.setSelectedRange(NSRange(location: (editor.string as NSString).length, length: 0))
editor.insertText("\n\t", replacementRange: editor.selectedRange())
XCTAssertTrue(editor.string.hasSuffix("\n\t"))
XCTAssertTrue(undo.canUndo)
}
}
}