Skip to content

Commit 20b560f

Browse files
committed
Add tests for SourceEditor.Cache
1 parent 8d9b257 commit 20b560f

File tree

5 files changed

+90
-1
lines changed

5 files changed

+90
-1
lines changed

TestPlan.xctestplan

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,13 @@
133133
"identifier" : "FocusedCodeFinderTests",
134134
"name" : "FocusedCodeFinderTests"
135135
}
136+
},
137+
{
138+
"target" : {
139+
"containerPath" : "container:Tool",
140+
"identifier" : "XcodeInspectorTests",
141+
"name" : "XcodeInspectorTests"
142+
}
136143
}
137144
],
138145
"version" : 1

Tool/Package.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,8 @@ let package = Package(
175175
]
176176
),
177177

178+
.testTarget(name: "XcodeInspectorTests", dependencies: ["XcodeInspector"]),
179+
178180
.target(name: "UserDefaultsObserver"),
179181

180182
.target(

Tool/Sources/XcodeInspector/SourceEditor.swift

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,12 +121,24 @@ extension SourceEditor {
121121
private var sourceSelectedTextRange: ClosedRange<Int>?
122122
private var cachedSelections = [CursorRange]()
123123

124+
init(
125+
sourceContent: String? = nil,
126+
cachedLines: [String] = [String](),
127+
sourceSelectedTextRange: ClosedRange<Int>? = nil,
128+
cachedSelections: [CursorRange] = [CursorRange]()
129+
) {
130+
self.sourceContent = sourceContent
131+
self.cachedLines = cachedLines
132+
self.sourceSelectedTextRange = sourceSelectedTextRange
133+
self.cachedSelections = cachedSelections
134+
}
135+
124136
func get(content: String, selectedTextRange: ClosedRange<Int>?) -> (
125137
lines: [String],
126138
selections: [CursorRange]
127139
) {
128140
Self.queue.sync {
129-
let contentMatch = content.hashValue == sourceContent?.hashValue
141+
let contentMatch = content == sourceContent
130142
let selectedRangeMatch = selectedTextRange == sourceSelectedTextRange
131143
let lines: [String] = {
132144
if contentMatch {
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import Foundation
2+
import XCTest
3+
4+
@testable import XcodeInspector
5+
6+
class SourceEditorCachePerformanceTests: XCTestCase {
7+
func test_source_editor_cache_get_content_comparison() {
8+
let content = String(repeating: """
9+
struct Cat: Animal {
10+
var name: String
11+
}
12+
13+
""", count: 500)
14+
let cache = SourceEditor.Cache(sourceContent: content + "Yes")
15+
16+
measure {
17+
for _ in 1 ... 10000 {
18+
_ = cache.get(content: content, selectedTextRange: nil)
19+
}
20+
}
21+
}
22+
}
23+
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import Foundation
2+
import XCTest
3+
4+
@testable import XcodeInspector
5+
6+
class SourceEditorCacheTests: XCTestCase {
7+
func test_source_editor_cache_get_content_thread_safe() {
8+
func randomContent() -> String {
9+
String(repeating: """
10+
struct Cat: Animal {
11+
var name: String
12+
}
13+
14+
""", count: Int.random(in: 2...10))
15+
}
16+
17+
func randomSelectionRange() -> ClosedRange<Int> {
18+
let random = Int.random(in: 0...20)
19+
return random...random
20+
}
21+
22+
let cache = SourceEditor.Cache()
23+
24+
let max = 5000
25+
let exp = expectation(description: "test_source_editor_cache_get_content_thread_safe")
26+
DispatchQueue.concurrentPerform(iterations: max) { count in
27+
let content = randomContent()
28+
let selectionRange = randomSelectionRange()
29+
let result = cache.get(content: content, selectedTextRange: selectionRange)
30+
31+
XCTAssertEqual(result.lines, content.breakLines(appendLineBreakToLastLine: false))
32+
XCTAssertEqual(result.selections, [SourceEditor.convertRangeToCursorRange(
33+
selectionRange,
34+
in: result.lines
35+
)])
36+
37+
if max == count + 1 {
38+
exp.fulfill()
39+
}
40+
}
41+
42+
wait(for: [exp], timeout: 10)
43+
}
44+
}
45+

0 commit comments

Comments
 (0)