|
6 | 6 | //
|
7 | 7 |
|
8 | 8 | import XCTest
|
| 9 | +@testable import CodeEditSourceEditor |
9 | 10 |
|
10 |
| -final class TextViewController_IndentTests: XCTestCase { |
| 11 | +final class TextViewControllerIndentTests: XCTestCase { |
| 12 | + var controller: TextViewController! |
11 | 13 |
|
12 | 14 | override func setUpWithError() throws {
|
13 |
| - // Put setup code here. This method is called before the invocation of each test method in the class. |
| 15 | + controller = Mock.textViewController(theme: Mock.theme()) |
| 16 | + |
| 17 | + controller.loadView() |
| 18 | + } |
| 19 | + |
| 20 | + func testHandleIndentWithSpacesInwards() { |
| 21 | + controller.setText(" This is a test string") |
| 22 | + let cursorPositions = [CursorPosition(range: NSRange(location: 0, length: 0))] |
| 23 | + controller.cursorPositions = cursorPositions |
| 24 | + controller.handleIndent(inwards: true) |
| 25 | + |
| 26 | + XCTAssertEqual(controller.string, "This is a test string") |
| 27 | + |
| 28 | + // Normally, 4 spaces are used for indentation; however, now we only insert 2 leading spaces. |
| 29 | + // The outcome should be the same, though. |
| 30 | + controller.setText(" This is a test string") |
| 31 | + controller.cursorPositions = cursorPositions |
| 32 | + controller.handleIndent(inwards: true) |
| 33 | + |
| 34 | + XCTAssertEqual(controller.string, "This is a test string") |
| 35 | + } |
| 36 | + |
| 37 | + func testHandleIndentWithSpacesOutwards() { |
| 38 | + controller.setText("This is a test string") |
| 39 | + let cursorPositions = [CursorPosition(range: NSRange(location: 0, length: 0))] |
| 40 | + controller.cursorPositions = cursorPositions |
| 41 | + |
| 42 | + controller.handleIndent(inwards: false) |
| 43 | + |
| 44 | + XCTAssertEqual(controller.string, " This is a test string") |
| 45 | + } |
| 46 | + |
| 47 | + func testHandleIndentWithTabsInwards() { |
| 48 | + controller.setText("\tThis is a test string") |
| 49 | + controller.indentOption = .tab |
| 50 | + let cursorPositions = [CursorPosition(range: NSRange(location: 0, length: 0))] |
| 51 | + controller.cursorPositions = cursorPositions |
| 52 | + |
| 53 | + controller.handleIndent(inwards: true) |
| 54 | + |
| 55 | + XCTAssertEqual(controller.string, "This is a test string") |
14 | 56 | }
|
15 | 57 |
|
16 |
| - override func tearDownWithError() throws { |
17 |
| - // Put teardown code here. This method is called after the invocation of each test method in the class. |
| 58 | + func testHandleIndentWithTabsOutwards() { |
| 59 | + controller.setText("This is a test string") |
| 60 | + controller.indentOption = .tab |
| 61 | + let cursorPositions = [CursorPosition(range: NSRange(location: 0, length: 0))] |
| 62 | + controller.cursorPositions = cursorPositions |
| 63 | + |
| 64 | + controller.handleIndent() |
| 65 | + |
| 66 | + // Normally, we expect nothing to happen because only one line is selected. |
| 67 | + // However, this logic is not handled inside `handleIndent`. |
| 68 | + XCTAssertEqual(controller.string, "\tThis is a test string") |
18 | 69 | }
|
19 | 70 |
|
20 |
| - func testExample() throws { |
21 |
| - // This is an example of a functional test case. |
22 |
| - // Use XCTAssert and related functions to verify your tests produce the correct results. |
23 |
| - // Any test you write for XCTest can be annotated as throws and async. |
24 |
| - // Mark your test throws to produce an unexpected failure when your test encounters an uncaught error. |
25 |
| - // Mark your test async to allow awaiting for asynchronous code to complete. Check the results with assertions afterwards. |
| 71 | + func testHandleIndentMultiLine() { |
| 72 | + controller.indentOption = .tab |
| 73 | + controller.setText("This is a test string\nWith multiple lines\nAnd some indentation") |
| 74 | + let cursorPositions = [CursorPosition(range: NSRange(location: 0, length: 5))] |
| 75 | + controller.cursorPositions = cursorPositions |
| 76 | + |
| 77 | + controller.handleIndent() |
| 78 | + let expectedString = "\tThis is a test string\nWith multiple lines\nAnd some indentation" |
| 79 | + XCTAssertEqual(controller.string, expectedString) |
26 | 80 | }
|
27 | 81 |
|
28 |
| - func testPerformanceExample() throws { |
29 |
| - // This is an example of a performance test case. |
30 |
| - self.measure { |
31 |
| - // Put the code you want to measure the time of here. |
32 |
| - } |
| 82 | + func testHandleInwardIndentMultiLine() { |
| 83 | + controller.indentOption = .tab |
| 84 | + controller.setText("\tThis is a test string\n\tWith multiple lines\n\tAnd some indentation") |
| 85 | + let cursorPositions = [CursorPosition(range: NSRange(location: 0, length: controller.string.count))] |
| 86 | + controller.cursorPositions = cursorPositions |
| 87 | + |
| 88 | + controller.handleIndent(inwards: true) |
| 89 | + let expectedString = "This is a test string\nWith multiple lines\nAnd some indentation" |
| 90 | + XCTAssertEqual(controller.string, expectedString) |
33 | 91 | }
|
34 | 92 |
|
| 93 | + func testMultipleLinesHighlighted() { |
| 94 | + controller.setText("\tThis is a test string\n\tWith multiple lines\n\tAnd some indentation") |
| 95 | + var cursorPositions = [CursorPosition(range: NSRange(location: 0, length: controller.string.count))] |
| 96 | + controller.cursorPositions = cursorPositions |
| 97 | + |
| 98 | + XCTAssert(controller.multipleLinesHighlighted()) |
| 99 | + |
| 100 | + cursorPositions = [CursorPosition(range: NSRange(location: 0, length: 5))] |
| 101 | + controller.cursorPositions = cursorPositions |
| 102 | + |
| 103 | + XCTAssertFalse(controller.multipleLinesHighlighted()) |
| 104 | + } |
35 | 105 | }
|
0 commit comments