Skip to content

Commit a4f474e

Browse files
committed
Fix unit tests
1 parent 6bdbf0b commit a4f474e

File tree

8 files changed

+87
-53
lines changed

8 files changed

+87
-53
lines changed

Core/Tests/ServiceTests/Environment.swift

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,19 +14,19 @@ func completion(text: String, range: CursorRange, uuid: String = "") -> CodeSugg
1414
}
1515

1616
class MockSuggestionService: GitHubCopilotSuggestionServiceType {
17-
func terminate() async {
17+
func notifyChangeTextDocument(fileURL: URL, content: String, version: Int) async throws {
1818
fatalError()
1919
}
2020

21-
func cancelRequest() async {
21+
func terminate() async {
2222
fatalError()
2323
}
2424

25-
func notifyOpenTextDocument(fileURL: URL, content: String) async throws {
25+
func cancelRequest() async {
2626
fatalError()
2727
}
2828

29-
func notifyChangeTextDocument(fileURL: URL, content: String) async throws {
29+
func notifyOpenTextDocument(fileURL: URL, content: String) async throws {
3030
fatalError()
3131
}
3232

@@ -49,6 +49,7 @@ class MockSuggestionService: GitHubCopilotSuggestionServiceType {
4949
func getCompletions(
5050
fileURL: URL,
5151
content: String,
52+
originalContent: String,
5253
cursorPosition: SuggestionModel.CursorPosition,
5354
tabSize: Int,
5455
indentSize: Int,

Pro

Submodule Pro updated from b049af9 to 06a18fd

Tool/Package.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,7 @@ let package = Package(
301301
"SuggestionModel",
302302
"UserDefaultsObserver",
303303
"Preferences",
304+
"Logger",
304305
.product(name: "CopilotForXcodeKit", package: "CopilotForXcodeKit"),
305306
]),
306307

@@ -318,6 +319,7 @@ let package = Package(
318319
"Terminal",
319320
"BuiltinExtension",
320321
"Toast",
322+
"SuggestionProvider",
321323
.product(name: "LanguageServerProtocol", package: "LanguageServerProtocol"),
322324
.product(name: "CopilotForXcodeKit", package: "CopilotForXcodeKit"),
323325
],

Tool/Sources/GitHubCopilotService/GitHubCopilotExtension.swift

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,11 @@ public final class GitHubCopilotExtension: BuiltinExtension {
2121

2222
let workspacePool: WorkspacePool
2323

24-
let serviceLocator: ServiceLocator
24+
let serviceLocator: ServiceLocatorType
2525

2626
public init(workspacePool: WorkspacePool) {
2727
self.workspacePool = workspacePool
28-
serviceLocator = .init(workspacePool: workspacePool)
28+
serviceLocator = ServiceLocator(workspacePool: workspacePool)
2929
suggestionService = .init(serviceLocator: serviceLocator)
3030
}
3131

@@ -130,7 +130,11 @@ public final class GitHubCopilotExtension: BuiltinExtension {
130130
}
131131
}
132132

133-
final class ServiceLocator {
133+
protocol ServiceLocatorType {
134+
func getService(from workspace: WorkspaceInfo) async -> GitHubCopilotService?
135+
}
136+
137+
class ServiceLocator: ServiceLocatorType {
134138
let workspacePool: WorkspacePool
135139

136140
init(workspacePool: WorkspacePool) {

Tool/Sources/GitHubCopilotService/Services/GitHubCopilotSuggestionService.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ public final class GitHubCopilotSuggestionService: SuggestionServiceType {
1212
)
1313
}
1414

15-
let serviceLocator: ServiceLocator
15+
let serviceLocator: ServiceLocatorType
1616

17-
init(serviceLocator: ServiceLocator) {
17+
init(serviceLocator: ServiceLocatorType) {
1818
self.serviceLocator = serviceLocator
1919
}
2020

Tool/Tests/GitHubCopilotServiceTests/FetchSuggestionsTests.swift

Lines changed: 61 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,65 @@
1+
import CopilotForXcodeKit
12
import LanguageServerProtocol
23
import XCTest
34

45
@testable import GitHubCopilotService
56

7+
struct TestServiceLocator: ServiceLocatorType {
8+
let server: GitHubCopilotLSP
9+
func getService(from workspace: WorkspaceInfo) async -> GitHubCopilotService? {
10+
.init(designatedServer: server)
11+
}
12+
}
13+
614
final class FetchSuggestionTests: XCTestCase {
715
func test_process_suggestions_from_server() async throws {
816
struct TestServer: GitHubCopilotLSP {
917
func sendNotification(_: LanguageServerProtocol.ClientNotification) async throws {
10-
fatalError()
18+
throw CancellationError()
1119
}
1220

1321
func sendRequest<E>(_: E) async throws -> E.Response where E: GitHubCopilotRequestType {
14-
return GitHubCopilotRequest.GetCompletionsCycling.Response(completions: [
22+
return GitHubCopilotRequest.InlineCompletion.Response(items: [
1523
.init(
16-
text: "Hello World\n",
17-
position: .init((0, 0)),
18-
uuid: "uuid",
24+
insertText: "Hello World\n",
25+
filterText: nil,
1926
range: .init(start: .init((0, 0)), end: .init((0, 4))),
20-
displayText: ""
27+
command: nil
2128
),
2229
.init(
23-
text: " ",
24-
position: .init((0, 0)),
25-
uuid: "uuid",
30+
insertText: " ",
31+
filterText: nil,
2632
range: .init(start: .init((0, 0)), end: .init((0, 1))),
27-
displayText: ""
33+
command: nil
2834
),
2935
.init(
30-
text: " \n",
31-
position: .init((0, 0)),
32-
uuid: "uuid",
36+
insertText: " \n",
37+
filterText: nil,
3338
range: .init(start: .init((0, 0)), end: .init((0, 2))),
34-
displayText: ""
39+
command: nil
3540
),
3641
]) as! E.Response
3742
}
3843
}
39-
let service = GitHubCopilotSuggestionService(designatedServer: TestServer())
40-
let completions = try await service.getCompletions(
41-
fileURL: .init(fileURLWithPath: "/file.swift"),
42-
content: "",
43-
cursorPosition: .outOfScope,
44-
tabSize: 4,
45-
indentSize: 4,
46-
usesTabsForIndentation: false
44+
let service =
45+
GitHubCopilotSuggestionService(serviceLocator: TestServiceLocator(server: TestServer()))
46+
let completions = try await service.getSuggestions(
47+
.init(
48+
fileURL: .init(fileURLWithPath: "/file.swift"),
49+
relativePath: "",
50+
language: .builtIn(.swift),
51+
content: "",
52+
originalContent: "",
53+
cursorPosition: .outOfScope,
54+
tabSize: 4,
55+
indentSize: 4,
56+
usesTabsForIndentation: false,
57+
relevantCodeSnippets: []
58+
),
59+
workspace: .init(
60+
workspaceURL: .init(fileURLWithPath: "/"),
61+
projectURL: .init(fileURLWithPath: "/file.swift")
62+
)
4763
)
4864
XCTAssertEqual(completions.count, 3)
4965
}
@@ -55,26 +71,36 @@ final class FetchSuggestionTests: XCTestCase {
5571
}
5672

5773
func sendRequest<E>(_: E) async throws -> E.Response where E: GitHubCopilotRequestType {
58-
return GitHubCopilotRequest.GetCompletionsCycling.Response(completions: [
74+
return GitHubCopilotRequest.InlineCompletion.Response(items: [
5975
.init(
60-
text: "Hello World\n",
61-
position: .init((0, 0)),
62-
uuid: "uuid",
76+
insertText: "Hello World\n",
77+
filterText: nil,
6378
range: .init(start: .init((0, 0)), end: .init((0, 4))),
64-
displayText: ""
79+
command: nil
6580
),
6681
]) as! E.Response
6782
}
6883
}
6984
let testServer = TestServer()
70-
let service = GitHubCopilotSuggestionService(designatedServer: testServer)
71-
let completions = try await service.getCompletions(
72-
fileURL: .init(fileURLWithPath: "/"),
73-
content: "",
74-
cursorPosition: .outOfScope,
75-
tabSize: 4,
76-
indentSize: 4,
77-
usesTabsForIndentation: false
85+
let service =
86+
GitHubCopilotSuggestionService(serviceLocator: TestServiceLocator(server: testServer))
87+
let completions = try await service.getSuggestions(
88+
.init(
89+
fileURL: .init(fileURLWithPath: "/"),
90+
relativePath: "",
91+
language: .builtIn(.swift),
92+
content: "",
93+
originalContent: "",
94+
cursorPosition: .outOfScope,
95+
tabSize: 4,
96+
indentSize: 4,
97+
usesTabsForIndentation: false,
98+
relevantCodeSnippets: []
99+
),
100+
workspace: .init(
101+
workspaceURL: .init(fileURLWithPath: "/"),
102+
projectURL: .init(fileURLWithPath: "/file.swift")
103+
)
78104
)
79105
XCTAssertEqual(completions.count, 1)
80106
XCTAssertEqual(completions.first?.text, "Hello World\n")

Tool/Tests/SharedUIComponentsTests/ConvertToCodeLinesTests.swift

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ final class ConvertToCodeLinesTests: XCTestCase {
88
struct Cat {
99
}
1010
"""
11-
let (result, spaceCount) = highlighted(
11+
let (result, spaceCount) = CodeHighlighting.highlighted(
1212
code: code,
1313
language: "swift",
1414
scenario: "a",
@@ -30,7 +30,7 @@ final class ConvertToCodeLinesTests: XCTestCase {
3030
struct Cat {
3131
}
3232
"""
33-
let (result, spaceCount) = highlighted(
33+
let (result, spaceCount) = CodeHighlighting.highlighted(
3434
code: code,
3535
language: "md",
3636
scenario: "a",
@@ -51,7 +51,7 @@ final class ConvertToCodeLinesTests: XCTestCase {
5151
struct Cat {
5252
}
5353
"""
54-
let (result, spaceCount) = highlighted(
54+
let (result, spaceCount) = CodeHighlighting.highlighted(
5555
code: code,
5656
language: "md",
5757
scenario: "a",
@@ -72,7 +72,7 @@ final class ConvertToCodeLinesTests: XCTestCase {
7272
struct Cat {
7373
}
7474
"""
75-
let (result, spaceCount) = highlighted(
75+
let (result, spaceCount) = CodeHighlighting.highlighted(
7676
code: code,
7777
language: "md",
7878
scenario: "a",
@@ -94,7 +94,7 @@ final class ConvertToCodeLinesTests: XCTestCase {
9494
9595
}
9696
"""
97-
let (result, spaceCount) = highlighted(
97+
let (result, spaceCount) = CodeHighlighting.highlighted(
9898
code: code,
9999
language: "md",
100100
scenario: "a",
@@ -117,7 +117,7 @@ final class ConvertToCodeLinesTests: XCTestCase {
117117
//
118118
}
119119
"""
120-
let (result, spaceCount) = highlighted(
120+
let (result, spaceCount) = CodeHighlighting.highlighted(
121121
code: code,
122122
language: "md",
123123
scenario: "a",
@@ -140,7 +140,7 @@ final class ConvertToCodeLinesTests: XCTestCase {
140140
//
141141
}
142142
"""
143-
let (result, spaceCount) = highlighted(
143+
let (result, spaceCount) = CodeHighlighting.highlighted(
144144
code: code,
145145
language: "md",
146146
scenario: "a",

Tool/Tests/SuggestionProviderTests/PostProcessingSuggestionServiceMiddlewareTests.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ class PostProcessingSuggestionServiceMiddlewareTests: XCTestCase {
1313
return SuggestionRequest(
1414
fileURL: URL(fileURLWithPath: "/path/to/file.swift"),
1515
relativePath: "file.swift",
16-
content: code,
16+
content: code,
17+
originalContent: code,
1718
lines: lines,
1819
cursorPosition: cursorPosition,
1920
cursorOffset: {

0 commit comments

Comments
 (0)