|
| 1 | +// |
| 2 | +// TextViewController+MoveLinesTests.swift |
| 3 | +// CodeEditSourceEditor |
| 4 | +// |
| 5 | +// Created by Bogdan Belogurov on 01/06/2025. |
| 6 | +// |
| 7 | + |
| 8 | +import XCTest |
| 9 | +@testable import CodeEditSourceEditor |
| 10 | +@testable import CodeEditTextView |
| 11 | +import CustomDump |
| 12 | + |
| 13 | +final class TextViewControllerMoveLinesTests: XCTestCase { |
| 14 | + var controller: TextViewController! |
| 15 | + |
| 16 | + override func setUpWithError() throws { |
| 17 | + controller = Mock.textViewController(theme: Mock.theme()) |
| 18 | + |
| 19 | + controller.loadView() |
| 20 | + } |
| 21 | + |
| 22 | + func testHandleMoveLinesUpForSingleLine() { |
| 23 | + let strings: [(NSString, Int)] = [ |
| 24 | + ("This is a test string\n", 0), |
| 25 | + ("With multiple lines\n", 22) |
| 26 | + ] |
| 27 | + for (insertedString, location) in strings { |
| 28 | + controller.textView.replaceCharacters( |
| 29 | + in: [NSRange(location: location, length: 0)], |
| 30 | + with: insertedString as String |
| 31 | + ) |
| 32 | + } |
| 33 | + |
| 34 | + let cursorRange = NSRange(location: 40, length: 0) |
| 35 | + controller.textView.selectionManager.textSelections = [.init(range: cursorRange)] |
| 36 | + controller.cursorPositions = [CursorPosition(range: cursorRange)] |
| 37 | + |
| 38 | + controller.moveLinesUp() |
| 39 | + let expectedString = "With multiple lines\nThis is a test string\n" |
| 40 | + expectNoDifference(controller.string, expectedString) |
| 41 | + } |
| 42 | + |
| 43 | + func testHandleMoveLinesDownForSingleLine() { |
| 44 | + let strings: [(NSString, Int)] = [ |
| 45 | + ("This is a test string\n", 0), |
| 46 | + ("With multiple lines\n", 22) |
| 47 | + ] |
| 48 | + for (insertedString, location) in strings { |
| 49 | + controller.textView.replaceCharacters( |
| 50 | + in: [NSRange(location: location, length: 0)], |
| 51 | + with: insertedString as String |
| 52 | + ) |
| 53 | + } |
| 54 | + |
| 55 | + let cursorRange = NSRange(location: 0, length: 0) |
| 56 | + controller.textView.selectionManager.textSelections = [.init(range: cursorRange)] |
| 57 | + controller.cursorPositions = [CursorPosition(range: cursorRange)] |
| 58 | + |
| 59 | + controller.moveLinesDown() |
| 60 | + let expectedString = "With multiple lines\nThis is a test string\n" |
| 61 | + expectNoDifference(controller.string, expectedString) |
| 62 | + } |
| 63 | + |
| 64 | + func testHandleMoveLinesUpForMultiLine() { |
| 65 | + let strings: [(NSString, Int)] = [ |
| 66 | + ("This is a test string\n", 0), |
| 67 | + ("With multiple lines\n", 22), |
| 68 | + ("And additional info\n", 42) |
| 69 | + ] |
| 70 | + for (insertedString, location) in strings { |
| 71 | + controller.textView.replaceCharacters( |
| 72 | + in: [NSRange(location: location, length: 0)], |
| 73 | + with: insertedString as String |
| 74 | + ) |
| 75 | + } |
| 76 | + |
| 77 | + let cursorRange = NSRange(location: 40, length: 15) |
| 78 | + controller.textView.selectionManager.textSelections = [.init(range: cursorRange)] |
| 79 | + controller.cursorPositions = [CursorPosition(range: cursorRange)] |
| 80 | + |
| 81 | + controller.moveLinesUp() |
| 82 | + let expectedString = "With multiple lines\nAnd additional info\nThis is a test string\n" |
| 83 | + expectNoDifference(controller.string, expectedString) |
| 84 | + } |
| 85 | + |
| 86 | + func testHandleMoveLinesDownForMultiLine() { |
| 87 | + let strings: [(NSString, Int)] = [ |
| 88 | + ("This is a test string\n", 0), |
| 89 | + ("With multiple lines\n", 22), |
| 90 | + ("And additional info\n", 42) |
| 91 | + ] |
| 92 | + for (insertedString, location) in strings { |
| 93 | + controller.textView.replaceCharacters( |
| 94 | + in: [NSRange(location: location, length: 0)], |
| 95 | + with: insertedString as String |
| 96 | + ) |
| 97 | + } |
| 98 | + |
| 99 | + let cursorRange = NSRange(location: 0, length: 30) |
| 100 | + controller.textView.selectionManager.textSelections = [.init(range: cursorRange)] |
| 101 | + controller.cursorPositions = [CursorPosition(range: cursorRange)] |
| 102 | + |
| 103 | + controller.moveLinesDown() |
| 104 | + let expectedString = "And additional info\nThis is a test string\nWith multiple lines\n" |
| 105 | + expectNoDifference(controller.string, expectedString) |
| 106 | + } |
| 107 | +} |
0 commit comments