Files

95 lines
5.6 KiB
Swift

// SPDX-License-Identifier: GPL-3.0-or-later
import XCTest
import AppKit
final class CMOCEditingTests: XCTestCase {
private func edited(_ text: String, _ range: NSRange, _ action: CMOCEditorAction) throws -> (String, NSRange) {
let edit = try XCTUnwrap(CMOCEditing.lineEdit(text, selection: range, action: action))
return ((text as NSString).replacingCharacters(in: edit.range, with: edit.text), edit.selection)
}
func testSelectedLinesExcludeFollowingLineAndRoundTripIndent() throws {
let source = "one();\ntwo();\nthree();\n"
let result = try edited(source, NSRange(location: 2, length: 12), .indent)
XCTAssertEqual(result.0, " one();\n two();\nthree();\n")
XCTAssertEqual(try edited(result.0, result.1, .outdent).0, source)
}
func testCaretTabsCRLFAndUnicode() throws {
let source = "// 🙂\r\n\tvalue();\r\n"
let caret = (source as NSString).range(of: "value").location + 2
let result = try edited(source, NSRange(location: caret, length: 0), .outdent)
XCTAssertEqual(result.0, "// 🙂\r\nvalue();\r\n")
XCTAssertEqual(result.1.location, caret - 1)
let indented = try edited("", NSRange(location: 0, length: 0), .indent)
XCTAssertEqual(indented.0, " "); XCTAssertEqual(indented.1.location, 4)
}
func testCommentTogglePreservesIndentBlankLinesAndFinalNewline() throws {
let source = " one();\n\n\ttwo();"
let result = try edited(source, NSRange(location: 0, length: source.utf16.count), .comment)
XCTAssertEqual(result.0, " // one();\n\n\t// two();")
XCTAssertEqual(try edited(result.0, result.1, .comment).0, source)
let mixed = "// old\nnew\n"
XCTAssertEqual(try edited(mixed, NSRange(location: 0, length: mixed.utf16.count), .comment).0, "// // old\n// new\n")
XCTAssertNil(CMOCEditing.lineEdit(" \n", selection: .init(location: 0, length: 3), action: .comment))
}
func testBracketPairsIgnoreLiteralsCommentsAndContinuedDirectives() {
let source = "#define X { \\\n[\nvoid f() { /* ] */ puts(\"}\\\"[\"); // ( \\\n)\nvalues[2] = '('; }"
let pairs = CMOCEditing.bracketPairs(source)
let u = source as NSString
let open = u.range(of: "{ /*").location, close = u.length - 1
XCTAssertEqual(pairs[open], close); XCTAssertEqual(pairs[close], open)
XCTAssertNil(pairs[u.range(of: "{ \\").location])
XCTAssertEqual(pairs.count, 8)
XCTAssertTrue(CMOCEditing.bracketPairs("([)]").isEmpty)
}
func testNewlineCopiesIndentAndAddsLevelOnlyAfterCodeBrace() {
for (source, expected) in [(" if (ok) {", "\n "), (" if (ok) { ", "\n "), ("\tif (ok) {", "\n\t\t"), (" // {", "\n "), (" puts(\"{\");", "\n "), ("x\r\n y", "\r\n ")] {
XCTAssertEqual(CMOCEditing.newline(source, selection: .init(location: source.utf16.count, length: 0)), expected)
}
}
@MainActor private final class Delegate: NSObject, NSTextViewDelegate {
let undo = UndoManager()
func undoManager(for view: NSTextView) -> UndoManager? { undo }
}
@MainActor private func withEditor(_ body: (CMOCEditingTextView, UndoManager) -> Void) {
let delegate = Delegate(), editor = CMOCEditingTextView(frame: .init(x: 0, y: 0, width: 400, height: 300))
editor.delegate = delegate; editor.isRichText = false; editor.isEditable = true; editor.allowsUndo = true
body(editor, delegate.undo)
}
@MainActor func testNativeLineCommandsUndoRedoAndReadOnly() {
for action in [CMOCEditorAction.indent, .outdent, .comment] {
withEditor { editor, undo in
let source = " first();\n second();\n"
editor.string = source; editor.setSelectedRange(.init(location: 0, length: source.utf16.count))
editor.performSourceAction(action)
let changed = editor.string
XCTAssertNotEqual(changed, source); XCTAssertTrue(undo.canUndo)
undo.undo(); XCTAssertEqual(editor.string, source); XCTAssertFalse(undo.canUndo)
undo.redo(); XCTAssertEqual(editor.string, changed)
editor.isEditable = false; editor.performSourceAction(action); XCTAssertEqual(editor.string, changed)
}
}
}
@MainActor func testNativeReturnTabAndShiftTab() {
withEditor { editor, undo in
editor.string = "int main(void) {"; editor.setSelectedRange(.init(location: editor.string.utf16.count, length: 0))
editor.insertNewline(nil); XCTAssertTrue(editor.string.hasSuffix("\n "))
undo.undo(); XCTAssertEqual(editor.string, "int main(void) {")
editor.string = "one\ntwo\n"; undo.removeAllActions()
editor.setSelectedRange(.init(location: 0, length: 8)); editor.insertTab(nil)
XCTAssertEqual(editor.string, " one\n two\n")
editor.insertBacktab(nil); XCTAssertEqual(editor.string, "one\ntwo\n")
}
}
@MainActor func testMatchingNavigationDoesNotEditReadOnlyTextOrUndo() {
withEditor { editor, undo in
editor.string = "f(a[2]);"; editor.isEditable = false
editor.setSelectedRange(.init(location: 1, length: 0)); editor.performSourceAction(.match)
XCTAssertEqual(editor.selectedRange().location, 6)
XCTAssertEqual(editor.string, "f(a[2]);"); XCTAssertFalse(undo.canUndo)
editor.performSourceAction(.match); XCTAssertEqual(editor.selectedRange().location, 1)
editor.sourceEditingEnabled = false; editor.performSourceAction(.match)
XCTAssertEqual(editor.selectedRange().location, 1)
}
}
}