|
| 1 | +import XCTest |
| 2 | +@testable import CodeEditTextView |
| 3 | + |
| 4 | +class MarkedTextTests: XCTestCase { |
| 5 | + func test_markedTextSingleChar() { |
| 6 | + let textView = TextView(string: "") |
| 7 | + textView.selectionManager.setSelectedRange(.zero) |
| 8 | + |
| 9 | + textView.setMarkedText("´", selectedRange: .notFound, replacementRange: .notFound) |
| 10 | + XCTAssertEqual(textView.string, "´") |
| 11 | + |
| 12 | + textView.insertText("é", replacementRange: .notFound) |
| 13 | + XCTAssertEqual(textView.string, "é") |
| 14 | + XCTAssertEqual(textView.selectionManager.textSelections.map(\.range), [NSRange(location: 1, length: 0)]) |
| 15 | + } |
| 16 | + |
| 17 | + func test_markedTextSingleCharInStrings() { |
| 18 | + let textView = TextView(string: "Lorem Ipsum") |
| 19 | + textView.selectionManager.setSelectedRange(NSRange(location: 5, length: 0)) |
| 20 | + |
| 21 | + textView.setMarkedText("´", selectedRange: .notFound, replacementRange: .notFound) |
| 22 | + XCTAssertEqual(textView.string, "Lorem´ Ipsum") |
| 23 | + |
| 24 | + textView.insertText("é", replacementRange: .notFound) |
| 25 | + XCTAssertEqual(textView.string, "Loremé Ipsum") |
| 26 | + XCTAssertEqual(textView.selectionManager.textSelections.map(\.range), [NSRange(location: 6, length: 0)]) |
| 27 | + } |
| 28 | + |
| 29 | + func test_markedTextReplaceSelection() { |
| 30 | + let textView = TextView(string: "ABCDE") |
| 31 | + textView.selectionManager.setSelectedRange(NSRange(location: 4, length: 1)) |
| 32 | + |
| 33 | + textView.setMarkedText("´", selectedRange: .notFound, replacementRange: .notFound) |
| 34 | + XCTAssertEqual(textView.string, "ABCD´") |
| 35 | + |
| 36 | + textView.insertText("é", replacementRange: .notFound) |
| 37 | + XCTAssertEqual(textView.string, "ABCDé") |
| 38 | + XCTAssertEqual(textView.selectionManager.textSelections.map(\.range), [NSRange(location: 5, length: 0)]) |
| 39 | + } |
| 40 | + |
| 41 | + func test_markedTextMultipleSelection() { |
| 42 | + let textView = TextView(string: "ABC") |
| 43 | + textView.selectionManager.setSelectedRanges([NSRange(location: 1, length: 0), NSRange(location: 2, length: 0)]) |
| 44 | + |
| 45 | + textView.setMarkedText("´", selectedRange: .notFound, replacementRange: .notFound) |
| 46 | + XCTAssertEqual(textView.string, "A´B´C") |
| 47 | + |
| 48 | + textView.insertText("é", replacementRange: .notFound) |
| 49 | + XCTAssertEqual(textView.string, "AéBéC") |
| 50 | + XCTAssertEqual( |
| 51 | + textView.selectionManager.textSelections.map(\.range).sorted(by: { $0.location < $1.location }), |
| 52 | + [NSRange(location: 2, length: 0), NSRange(location: 4, length: 0)] |
| 53 | + ) |
| 54 | + } |
| 55 | + |
| 56 | + func test_markedTextMultipleSelectionReplaceSelection() { |
| 57 | + let textView = TextView(string: "ABCDE") |
| 58 | + textView.selectionManager.setSelectedRanges([NSRange(location: 0, length: 1), NSRange(location: 4, length: 1)]) |
| 59 | + |
| 60 | + textView.setMarkedText("´", selectedRange: .notFound, replacementRange: .notFound) |
| 61 | + XCTAssertEqual(textView.string, "´BCD´") |
| 62 | + |
| 63 | + textView.insertText("é", replacementRange: .notFound) |
| 64 | + XCTAssertEqual(textView.string, "éBCDé") |
| 65 | + XCTAssertEqual( |
| 66 | + textView.selectionManager.textSelections.map(\.range).sorted(by: { $0.location < $1.location }), |
| 67 | + [NSRange(location: 1, length: 0), NSRange(location: 5, length: 0)] |
| 68 | + ) |
| 69 | + } |
| 70 | + |
| 71 | + func test_markedTextMultipleSelectionMultipleChar() { |
| 72 | + let textView = TextView(string: "ABCDE") |
| 73 | + textView.selectionManager.setSelectedRanges([NSRange(location: 0, length: 1), NSRange(location: 4, length: 1)]) |
| 74 | + |
| 75 | + textView.setMarkedText("´", selectedRange: .notFound, replacementRange: .notFound) |
| 76 | + XCTAssertEqual(textView.string, "´BCD´") |
| 77 | + |
| 78 | + textView.setMarkedText("´´´", selectedRange: .notFound, replacementRange: .notFound) |
| 79 | + XCTAssertEqual(textView.string, "´´´BCD´´´") |
| 80 | + XCTAssertEqual( |
| 81 | + textView.selectionManager.textSelections.map(\.range).sorted(by: { $0.location < $1.location }), |
| 82 | + [NSRange(location: 3, length: 0), NSRange(location: 9, length: 0)] |
| 83 | + ) |
| 84 | + |
| 85 | + textView.insertText("é", replacementRange: .notFound) |
| 86 | + XCTAssertEqual(textView.string, "éBCDé") |
| 87 | + XCTAssertEqual( |
| 88 | + textView.selectionManager.textSelections.map(\.range).sorted(by: { $0.location < $1.location }), |
| 89 | + [NSRange(location: 1, length: 0), NSRange(location: 5, length: 0)] |
| 90 | + ) |
| 91 | + } |
| 92 | + |
| 93 | + func test_cancelMarkedText() { |
| 94 | + let textView = TextView(string: "") |
| 95 | + textView.selectionManager.setSelectedRange(.zero) |
| 96 | + |
| 97 | + textView.setMarkedText("´", selectedRange: .notFound, replacementRange: .notFound) |
| 98 | + XCTAssertEqual(textView.string, "´") |
| 99 | + |
| 100 | + // The NSTextInputContext performs the following actions when a marked text segment is ended w/o replacing the |
| 101 | + // marked text: |
| 102 | + textView.insertText("´", replacementRange: .notFound) |
| 103 | + textView.insertText("4", replacementRange: .notFound) |
| 104 | + |
| 105 | + XCTAssertEqual(textView.string, "´4") |
| 106 | + XCTAssertEqual(textView.selectionManager.textSelections.map(\.range), [NSRange(location: 2, length: 0)]) |
| 107 | + } |
| 108 | + |
| 109 | + func test_cancelMarkedTextMultipleCursor() { |
| 110 | + let textView = TextView(string: "ABC") |
| 111 | + textView.selectionManager.setSelectedRanges([NSRange(location: 1, length: 0), NSRange(location: 2, length: 0)]) |
| 112 | + |
| 113 | + textView.setMarkedText("´", selectedRange: .notFound, replacementRange: .notFound) |
| 114 | + XCTAssertEqual(textView.string, "A´B´C") |
| 115 | + |
| 116 | + // The NSTextInputContext performs the following actions when a marked text segment is ended w/o replacing the |
| 117 | + // marked text: |
| 118 | + textView.insertText("´", replacementRange: .notFound) |
| 119 | + textView.insertText("4", replacementRange: .notFound) |
| 120 | + |
| 121 | + XCTAssertEqual(textView.string, "A´4B´4C") |
| 122 | + XCTAssertEqual( |
| 123 | + textView.selectionManager.textSelections.map(\.range).sorted(by: { $0.location < $1.location }), |
| 124 | + [NSRange(location: 3, length: 0), NSRange(location: 6, length: 0)] |
| 125 | + ) |
| 126 | + } |
| 127 | +} |
0 commit comments