|
| 1 | +import Foundation |
| 2 | +import SuggestionModel |
| 3 | +import XCTest |
| 4 | + |
| 5 | +@testable import SuggestionProvider |
| 6 | + |
| 7 | +class PostProcessingSuggestionServiceMiddlewareTests: XCTestCase { |
| 8 | + func createRequest( |
| 9 | + _ code: String = "", |
| 10 | + _ cursorPosition: CursorPosition = .zero |
| 11 | + ) -> SuggestionRequest { |
| 12 | + let lines = code.breakLines() |
| 13 | + return SuggestionRequest( |
| 14 | + fileURL: URL(fileURLWithPath: "/path/to/file.swift"), |
| 15 | + relativePath: "file.swift", |
| 16 | + content: code, |
| 17 | + lines: lines, |
| 18 | + cursorPosition: cursorPosition, |
| 19 | + cursorOffset: { |
| 20 | + if cursorPosition == .outOfScope { return 0 } |
| 21 | + let prefixLines = if cursorPosition.line > 0 { |
| 22 | + lines[0..<cursorPosition.line] |
| 23 | + } else { |
| 24 | + [] as ArraySlice<String> |
| 25 | + } |
| 26 | + let offset = prefixLines.reduce(0) { $0 + $1.utf8.count } |
| 27 | + return offset |
| 28 | + + lines[cursorPosition.line].prefix(cursorPosition.character).utf8.count |
| 29 | + }(), |
| 30 | + tabSize: 4, |
| 31 | + indentSize: 4, |
| 32 | + usesTabsForIndentation: false, |
| 33 | + relevantCodeSnippets: [] |
| 34 | + ) |
| 35 | + } |
| 36 | + |
| 37 | + func test_trailing_whitespaces_and_new_lines_should_be_removed() async throws { |
| 38 | + let middleware = PostProcessingSuggestionServiceMiddleware() |
| 39 | + |
| 40 | + let handler: PostProcessingSuggestionServiceMiddleware.Next = { _ in |
| 41 | + [ |
| 42 | + .init( |
| 43 | + id: "1", |
| 44 | + text: "hello world \n \n", |
| 45 | + position: .init(line: 0, character: 0), |
| 46 | + range: .init(startPair: (0, 0), endPair: (0, 0)) |
| 47 | + ), |
| 48 | + .init( |
| 49 | + id: "2", |
| 50 | + text: " \n hello world \n \n", |
| 51 | + position: .init(line: 0, character: 0), |
| 52 | + range: .init(startPair: (0, 0), endPair: (0, 0)) |
| 53 | + ), |
| 54 | + ] |
| 55 | + } |
| 56 | + |
| 57 | + let suggestions = try await middleware.getSuggestion( |
| 58 | + createRequest(), |
| 59 | + configuration: .init( |
| 60 | + acceptsRelevantCodeSnippets: true, |
| 61 | + mixRelevantCodeSnippetsInSource: true, |
| 62 | + acceptsRelevantSnippetsFromOpenedFiles: true |
| 63 | + ), |
| 64 | + next: handler |
| 65 | + ) |
| 66 | + |
| 67 | + XCTAssertEqual(suggestions, [ |
| 68 | + .init( |
| 69 | + id: "1", |
| 70 | + text: "hello world", |
| 71 | + position: .init(line: 0, character: 0), |
| 72 | + range: .init(startPair: (0, 0), endPair: (0, 0)) |
| 73 | + ), |
| 74 | + .init( |
| 75 | + id: "2", |
| 76 | + text: " \n hello world", |
| 77 | + position: .init(line: 0, character: 0), |
| 78 | + range: .init(startPair: (0, 0), endPair: (0, 0)) |
| 79 | + ), |
| 80 | + ]) |
| 81 | + } |
| 82 | + |
| 83 | + func test_remove_suggestions_that_contains_only_whitespaces_and_new_lines() async throws { |
| 84 | + let middleware = PostProcessingSuggestionServiceMiddleware() |
| 85 | + |
| 86 | + let handler: PostProcessingSuggestionServiceMiddleware.Next = { _ in |
| 87 | + [ |
| 88 | + .init( |
| 89 | + id: "1", |
| 90 | + text: "hello world \n \n", |
| 91 | + position: .init(line: 0, character: 0), |
| 92 | + range: .init(startPair: (0, 0), endPair: (0, 0)) |
| 93 | + ), |
| 94 | + .init( |
| 95 | + id: "2", |
| 96 | + text: " \n\n\r", |
| 97 | + position: .init(line: 0, character: 0), |
| 98 | + range: .init(startPair: (0, 0), endPair: (0, 0)) |
| 99 | + ), |
| 100 | + .init( |
| 101 | + id: "3", |
| 102 | + text: " ", |
| 103 | + position: .init(line: 0, character: 0), |
| 104 | + range: .init(startPair: (0, 0), endPair: (0, 0)) |
| 105 | + ), |
| 106 | + .init( |
| 107 | + id: "4", |
| 108 | + text: "\n\n\n", |
| 109 | + position: .init(line: 0, character: 0), |
| 110 | + range: .init(startPair: (0, 0), endPair: (0, 0)) |
| 111 | + ), |
| 112 | + ] |
| 113 | + } |
| 114 | + |
| 115 | + let suggestions = try await middleware.getSuggestion( |
| 116 | + createRequest(), |
| 117 | + configuration: .init( |
| 118 | + acceptsRelevantCodeSnippets: true, |
| 119 | + mixRelevantCodeSnippetsInSource: true, |
| 120 | + acceptsRelevantSnippetsFromOpenedFiles: true |
| 121 | + ), |
| 122 | + next: handler |
| 123 | + ) |
| 124 | + |
| 125 | + XCTAssertEqual(suggestions, [ |
| 126 | + .init( |
| 127 | + id: "1", |
| 128 | + text: "hello world", |
| 129 | + position: .init(line: 0, character: 0), |
| 130 | + range: .init(startPair: (0, 0), endPair: (0, 0)) |
| 131 | + ), |
| 132 | + ]) |
| 133 | + } |
| 134 | + |
| 135 | + func test_remove_suggestion_that_takes_no_effect_after_being_accepted() async throws { |
| 136 | + let middleware = PostProcessingSuggestionServiceMiddleware() |
| 137 | + |
| 138 | + let handler: PostProcessingSuggestionServiceMiddleware.Next = { _ in |
| 139 | + [ |
| 140 | + .init( |
| 141 | + id: "1", |
| 142 | + text: "hello world \n \n", |
| 143 | + position: .init(line: 0, character: 0), |
| 144 | + range: .init(startPair: (0, 0), endPair: (0, 0)) |
| 145 | + ), |
| 146 | + .init( |
| 147 | + id: "2", |
| 148 | + text: "let cat = 100", |
| 149 | + position: .init(line: 0, character: 13), |
| 150 | + range: .init(startPair: (0, 0), endPair: (0, 13)) |
| 151 | + ), |
| 152 | + .init( |
| 153 | + id: "3", |
| 154 | + text: "let cat = 10", |
| 155 | + position: .init(line: 0, character: 13), |
| 156 | + range: .init(startPair: (0, 0), endPair: (0, 13)) |
| 157 | + ), |
| 158 | + ] |
| 159 | + } |
| 160 | + |
| 161 | + let suggestions = try await middleware.getSuggestion( |
| 162 | + createRequest("let cat = 100", .init(line: 0, character: 3)), |
| 163 | + configuration: .init( |
| 164 | + acceptsRelevantCodeSnippets: true, |
| 165 | + mixRelevantCodeSnippetsInSource: true, |
| 166 | + acceptsRelevantSnippetsFromOpenedFiles: true |
| 167 | + ), |
| 168 | + next: handler |
| 169 | + ) |
| 170 | + |
| 171 | + XCTAssertEqual(suggestions, [ |
| 172 | + .init( |
| 173 | + id: "1", |
| 174 | + text: "hello world", |
| 175 | + position: .init(line: 0, character: 0), |
| 176 | + range: .init(startPair: (0, 0), endPair: (0, 0)) |
| 177 | + ), |
| 178 | + .init( |
| 179 | + id: "3", |
| 180 | + text: "let cat = 10", |
| 181 | + position: .init(line: 0, character: 13), |
| 182 | + range: .init(startPair: (0, 0), endPair: (0, 13)) |
| 183 | + ), |
| 184 | + ]) |
| 185 | + } |
| 186 | +} |
| 187 | + |
0 commit comments