Skip to content

Commit e0ab576

Browse files
committed
Fixup tests
1 parent fde4174 commit e0ab576

File tree

3 files changed

+63
-12
lines changed

3 files changed

+63
-12
lines changed

Example/CodeEditSourceEditorExample/CodeEditSourceEditorExample.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved

Lines changed: 18 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Package.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ let package = Package(
3333
.package(
3434
url: "https://github.com/ChimeHQ/TextFormation",
3535
from: "0.8.2"
36-
)
36+
),
37+
.package(url: "https://github.com/pointfreeco/swift-custom-dump", from: "1.0.0")
3738
],
3839
targets: [
3940
// A source editor with useful features for code editing.
@@ -55,6 +56,7 @@ let package = Package(
5556
dependencies: [
5657
"CodeEditSourceEditor",
5758
"CodeEditLanguages",
59+
.product(name: "CustomDump", package: "swift-custom-dump")
5860
],
5961
plugins: [
6062
.plugin(name: "SwiftLint", package: "SwiftLintPlugin")

Tests/CodeEditSourceEditorTests/Controller/TextViewController+IndentTests.swift

Lines changed: 42 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77

88
import XCTest
99
@testable import CodeEditSourceEditor
10+
@testable import CodeEditTextView
11+
import CustomDump
1012

1113
final 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("\tThis 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, "\tThis is a test string")
75+
expectNoDifference(controller.string, "\tThis is a test string")
6976
}
7077

7178
func testHandleIndentMultiLine() {
7279
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))]
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 = "\tThis is a test string\nWith multiple lines\nAnd some indentation"
79-
XCTAssertEqual(controller.string, expectedString)
97+
let expectedString = "\tThis is a test string\n\tWith multiple lines\n\tAnd some indentation"
98+
expectNoDifference(controller.string, expectedString)
8099
}
81100

82101
func testHandleInwardIndentMultiLine() {
83102
controller.indentOption = .tab
84-
controller.setText("\tThis is a test string\n\tWith multiple lines\n\tAnd some indentation")
103+
let strings: [(NSString, NSRange)] = [
104+
("\tThis is a test string\n", NSRange(location: 0, length: 0)),
105+
("\tWith multiple lines\n", NSRange(location: 23, length: 0)),
106+
("\tAnd 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\nWith multiple lines\nAnd some indentation"
90-
XCTAssertEqual(controller.string, expectedString)
121+
expectNoDifference(controller.string, expectedString)
91122
}
92123

93124
func testMultipleLinesHighlighted() {

0 commit comments

Comments
 (0)