77
88import XCTest
99@testable import CodeEditSourceEditor
10+ @testable import CodeEditTextView
11+ import CustomDump
1012
1113final class TextViewControllerIndentTests : XCTestCase {
1214 var controller : TextViewController !
@@ -21,73 +23,102 @@ final class TextViewControllerIndentTests: XCTestCase {
2123 controller. setText ( " This is a test string " )
2224 let cursorPositions = [ CursorPosition ( range: NSRange ( location: 0 , length: 0 ) ) ]
2325 controller. cursorPositions = cursorPositions
26+ controller. textView. selectionManager. textSelections = [ . init( range: NSRange ( location: 0 , length: 0 ) ) ]
2427 controller. handleIndent ( inwards: true )
2528
26- XCTAssertEqual ( controller. string, " This is a test string " )
29+ expectNoDifference ( controller. string, " This is a test string " )
2730
2831 // Normally, 4 spaces are used for indentation; however, now we only insert 2 leading spaces.
2932 // The outcome should be the same, though.
3033 controller. setText ( " This is a test string " )
3134 controller. cursorPositions = cursorPositions
35+ controller. textView. selectionManager. textSelections = [ . init( range: NSRange ( location: 0 , length: 0 ) ) ]
3236 controller. handleIndent ( inwards: true )
3337
34- XCTAssertEqual ( controller. string, " This is a test string " )
38+ expectNoDifference ( controller. string, " This is a test string " )
3539 }
3640
3741 func testHandleIndentWithSpacesOutwards( ) {
3842 controller. setText ( " This is a test string " )
3943 let cursorPositions = [ CursorPosition ( range: NSRange ( location: 0 , length: 0 ) ) ]
44+ controller. textView. selectionManager. textSelections = [ . init( range: NSRange ( location: 0 , length: 0 ) ) ]
4045 controller. cursorPositions = cursorPositions
4146
4247 controller. handleIndent ( inwards: false )
4348
44- XCTAssertEqual ( controller. string, " This is a test string " )
49+ expectNoDifference ( controller. string, " This is a test string " )
4550 }
4651
4752 func testHandleIndentWithTabsInwards( ) {
4853 controller. setText ( " \t This is a test string " )
4954 controller. indentOption = . tab
5055 let cursorPositions = [ CursorPosition ( range: NSRange ( location: 0 , length: 0 ) ) ]
56+ controller. textView. selectionManager. textSelections = [ . init( range: NSRange ( location: 0 , length: 0 ) ) ]
5157 controller. cursorPositions = cursorPositions
5258
5359 controller. handleIndent ( inwards: true )
5460
55- XCTAssertEqual ( controller. string, " This is a test string " )
61+ expectNoDifference ( controller. string, " This is a test string " )
5662 }
5763
5864 func testHandleIndentWithTabsOutwards( ) {
5965 controller. setText ( " This is a test string " )
6066 controller. indentOption = . tab
6167 let cursorPositions = [ CursorPosition ( range: NSRange ( location: 0 , length: 0 ) ) ]
68+ controller. textView. selectionManager. textSelections = [ . init( range: NSRange ( location: 0 , length: 0 ) ) ]
6269 controller. cursorPositions = cursorPositions
6370
6471 controller. handleIndent ( )
6572
6673 // Normally, we expect nothing to happen because only one line is selected.
6774 // However, this logic is not handled inside `handleIndent`.
68- XCTAssertEqual ( controller. string, " \t This is a test string " )
75+ expectNoDifference ( controller. string, " \t This is a test string " )
6976 }
7077
7178 func testHandleIndentMultiLine( ) {
7279 controller. indentOption = . tab
73- controller. setText ( " This is a test string \n With multiple lines \n And some indentation " )
74- let cursorPositions = [ CursorPosition ( range: NSRange ( location: 0 , length: 5 ) ) ]
80+ let strings : [ ( NSString , Int ) ] = [
81+ ( " This is a test string \n " , 0 ) ,
82+ ( " With multiple lines \n " , 22 ) ,
83+ ( " And some indentation " , 42 ) ,
84+ ]
85+ for (insertedString, location) in strings {
86+ controller. textView. replaceCharacters (
87+ in: [ NSRange ( location: location, length: 0 ) ] ,
88+ with: insertedString as String
89+ )
90+ }
91+
92+ let cursorPositions = [ CursorPosition ( range: NSRange ( location: 0 , length: 62 ) ) ]
93+ controller. textView. selectionManager. textSelections = [ . init( range: NSRange ( location: 0 , length: 62 ) ) ]
7594 controller. cursorPositions = cursorPositions
7695
7796 controller. handleIndent ( )
78- let expectedString = " \t This is a test string \n With multiple lines\n And some indentation"
79- XCTAssertEqual ( controller. string, expectedString)
97+ let expectedString = " \t This is a test string \n \t With multiple lines\n \t And some indentation"
98+ expectNoDifference ( controller. string, expectedString)
8099 }
81100
82101 func testHandleInwardIndentMultiLine( ) {
83102 controller. indentOption = . tab
84- controller. setText ( " \t This is a test string \n \t With multiple lines \n \t And some indentation " )
103+ let strings : [ ( NSString , NSRange ) ] = [
104+ ( " \t This is a test string \n " , NSRange ( location: 0 , length: 0 ) ) ,
105+ ( " \t With multiple lines \n " , NSRange ( location: 23 , length: 0 ) ) ,
106+ ( " \t And some indentation " , NSRange ( location: 44 , length: 0 ) ) ,
107+ ]
108+ for (insertedString, location) in strings {
109+ controller. textView. replaceCharacters (
110+ in: [ location] ,
111+ with: insertedString as String
112+ )
113+ }
114+
85115 let cursorPositions = [ CursorPosition ( range: NSRange ( location: 0 , length: controller. string. count) ) ]
116+ controller. textView. selectionManager. textSelections = [ . init( range: NSRange ( location: 0 , length: 62 ) ) ]
86117 controller. cursorPositions = cursorPositions
87118
88119 controller. handleIndent ( inwards: true )
89120 let expectedString = " This is a test string \n With multiple lines \n And some indentation "
90- XCTAssertEqual ( controller. string, expectedString)
121+ expectNoDifference ( controller. string, expectedString)
91122 }
92123
93124 func testMultipleLinesHighlighted( ) {
0 commit comments