Skip to content

Commit 00305e5

Browse files
committedApr 15, 2024
Add and update tests
1 parent f66aa15 commit 00305e5

File tree

5 files changed

+202
-29
lines changed

5 files changed

+202
-29
lines changed
 

‎Core/Package.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,10 @@ let package = Package(
6666
]
6767
),
6868

69+
.testTarget(
70+
name: "CodeCompletionServiceTests",
71+
dependencies: ["CodeCompletionService"]
72+
),
6973
.testTarget(
7074
name: "FundamentalTests",
7175
dependencies: ["Fundamental"]
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
import Foundation
2+
import XCTest
3+
4+
@testable import CodeCompletionService
5+
6+
class OpeningTagBasedStreamStopStrategyTests: XCTestCase {
7+
func test_no_opening_tag_found_and_not_hitting_limit() {
8+
let strategy = OpeningTagBasedStreamStopStrategy(
9+
openingTag: "<Code>",
10+
toleranceIfNoOpeningTagFound: 3
11+
)
12+
let limiter = StreamLineLimiter(lineLimit: 1, strategy: strategy)
13+
let content = """
14+
Hello World
15+
My Friend
16+
"""
17+
for character in content {
18+
let result = limiter.push(String(character))
19+
XCTAssertEqual(result, .continue)
20+
}
21+
XCTAssertEqual(limiter.result, content)
22+
}
23+
24+
func test_no_opening_tag_found_hitting_limit() {
25+
let strategy = OpeningTagBasedStreamStopStrategy(
26+
openingTag: "<Code>",
27+
toleranceIfNoOpeningTagFound: 3
28+
)
29+
let limiter = StreamLineLimiter(lineLimit: 1, strategy: strategy)
30+
let content = """
31+
Hello World
32+
My Friend
33+
How Are You
34+
I Am Fine
35+
Thank You
36+
"""
37+
38+
let expected = """
39+
Hello World
40+
My Friend
41+
How Are You
42+
I Am Fine
43+
44+
"""
45+
46+
for character in content {
47+
let result = limiter.push(String(character))
48+
if result == .finish(expected) {
49+
XCTAssertEqual(limiter.result, expected)
50+
return
51+
}
52+
}
53+
XCTFail("Should return in the loop\n\n\(limiter.result)")
54+
}
55+
56+
func test_opening_tag_found_not_hitting_limit() {
57+
let strategy = OpeningTagBasedStreamStopStrategy(
58+
openingTag: "<Code>",
59+
toleranceIfNoOpeningTagFound: 3
60+
)
61+
let limiter = StreamLineLimiter(lineLimit: 2, strategy: strategy)
62+
let content = """
63+
Hello World
64+
<Code>
65+
How Are You
66+
"""
67+
for character in content {
68+
let result = limiter.push(String(character))
69+
XCTAssertEqual(result, .continue)
70+
}
71+
XCTAssertEqual(limiter.result, content)
72+
}
73+
74+
func test_opening_tag_found_hitting_limit() {
75+
let strategy = OpeningTagBasedStreamStopStrategy(
76+
openingTag: "<Code>",
77+
toleranceIfNoOpeningTagFound: 3
78+
)
79+
let limiter = StreamLineLimiter(lineLimit: 2, strategy: strategy)
80+
let content = """
81+
Hello World
82+
<Code>
83+
How Are You
84+
I Am Fine
85+
Thank You
86+
"""
87+
88+
let expected = """
89+
Hello World
90+
<Code>
91+
How Are You
92+
I Am Fine
93+
94+
"""
95+
96+
for character in content {
97+
let result = limiter.push(String(character))
98+
if result == .finish(expected) {
99+
XCTAssertEqual(limiter.result, expected)
100+
return
101+
}
102+
}
103+
XCTFail("Should return in the loop\n\n\(limiter.result)")
104+
}
105+
}
106+
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
import Foundation
2+
import XCTest
3+
4+
@testable import CodeCompletionService
5+
6+
class StreamLineLimiterTests: XCTestCase {
7+
func test_pushing_characters_without_hitting_limit() {
8+
let limiter = StreamLineLimiter(lineLimit: 2, strategy: DefaultStreamStopStrategy())
9+
let content = "hello world\n"
10+
for character in content {
11+
let result = limiter.push(String(character))
12+
XCTAssertEqual(result, .continue)
13+
}
14+
XCTAssertEqual(limiter.result, content)
15+
}
16+
17+
func test_pushing_characters_hitting_limit() {
18+
let limiter = StreamLineLimiter(lineLimit: 2, strategy: DefaultStreamStopStrategy())
19+
let content = "hello world\nhello world\nhello world"
20+
for character in content {
21+
let result = limiter.push(String(character))
22+
if result == .finish("hello world\nhello world\n") {
23+
XCTAssertEqual(limiter.result, "hello world\nhello world\n")
24+
return
25+
}
26+
}
27+
XCTFail("Should return in the loop\n\(limiter.result)")
28+
}
29+
30+
func test_pushing_characters_with_early_exit_strategy() {
31+
struct Strategy: StreamStopStrategy {
32+
func shouldStop(
33+
existedLines: [String],
34+
currentLine: String,
35+
proposedLineLimit: Int
36+
) -> StreamStopStrategyResult {
37+
let hasPrefixP = currentLine.hasPrefix("p")
38+
let hasNewLine = existedLines.first?.hasSuffix("\n") ?? false
39+
if hasPrefixP && hasNewLine {
40+
return .stop(appendingNewContent: false)
41+
}
42+
return .continue
43+
}
44+
}
45+
46+
let limiter = StreamLineLimiter(lineLimit: 10, strategy: Strategy())
47+
let content = "hello world\npikachu\n"
48+
for character in content {
49+
let result = limiter.push(String(character))
50+
if result == .finish("hello world\n") {
51+
XCTAssertEqual(limiter.result, "hello world\n")
52+
return
53+
}
54+
}
55+
XCTFail("Should return in the loop\n\(limiter.result)")
56+
}
57+
58+
func test_receiving_multiple_line_ending_as_a_single_token() {
59+
let limiter = StreamLineLimiter(lineLimit: 4, strategy: DefaultStreamStopStrategy())
60+
let content = "hello world"
61+
for character in content {
62+
let result = limiter.push(String(character))
63+
XCTAssertEqual(result, .continue)
64+
}
65+
XCTAssertEqual(limiter.push("\n\n\n"), .continue)
66+
XCTAssertEqual(limiter.push("\n"), .finish("hello world\n\n\n\n"))
67+
}
68+
}
69+

‎Core/Tests/SuggestionServiceTests/DefaultRawSuggestionPostProcessingStrategyTests.swift

Lines changed: 14 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@ import XCTest
66
class DefaultRawSuggestionPostProcessingStrategyTests: XCTestCase {
77
func test_whenSuggestionHasCodeTagAtTheFirstLine_shouldExtractCodeInside() {
88
let strategy = DefaultRawSuggestionPostProcessingStrategy(
9-
openingCodeTag: "<Code>",
10-
closingCodeTag: "</Code>"
9+
codeWrappingTags: ("<Code>", "</Code>")
1110
)
1211
let result = strategy.extractSuggestion(
1312
from: """
@@ -21,8 +20,7 @@ class DefaultRawSuggestionPostProcessingStrategyTests: XCTestCase {
2120
func test_whenSuggestionHasCodeTagAtTheFirstLine_closingTagInOtherLines_shouldExtractCodeInside(
2221
) {
2322
let strategy = DefaultRawSuggestionPostProcessingStrategy(
24-
openingCodeTag: "<Code>",
25-
closingCodeTag: "</Code>"
23+
codeWrappingTags: ("<Code>", "</Code>")
2624
)
2725
let result = strategy.extractSuggestion(
2826
from: """
@@ -36,8 +34,7 @@ class DefaultRawSuggestionPostProcessingStrategyTests: XCTestCase {
3634

3735
func test_whenSuggestionHasCodeTag_butNoClosingTag_shouldExtractCodeAfterTheTag() {
3836
let strategy = DefaultRawSuggestionPostProcessingStrategy(
39-
openingCodeTag: "<Code>",
40-
closingCodeTag: "</Code>"
37+
codeWrappingTags: ("<Code>", "</Code>")
4138
)
4239
let result = strategy.extractSuggestion(
4340
from: """
@@ -51,8 +48,7 @@ class DefaultRawSuggestionPostProcessingStrategyTests: XCTestCase {
5148

5249
func test_whenMultipleOpeningTagFound_shouldTreatTheNextOneAsClosing() {
5350
let strategy = DefaultRawSuggestionPostProcessingStrategy(
54-
openingCodeTag: "<Code>",
55-
closingCodeTag: "</Code>"
51+
codeWrappingTags: ("<Code>", "</Code>")
5652
)
5753
let result = strategy.extractSuggestion(
5854
from: """
@@ -64,8 +60,7 @@ class DefaultRawSuggestionPostProcessingStrategyTests: XCTestCase {
6460

6561
func test_whenMarkdownCodeBlockFound_shouldExtractCodeInside() {
6662
let strategy = DefaultRawSuggestionPostProcessingStrategy(
67-
openingCodeTag: "<Code>",
68-
closingCodeTag: "</Code>"
63+
codeWrappingTags: ("<Code>", "</Code>")
6964
)
7065
let result = strategy.extractSuggestion(
7166
from: """
@@ -80,8 +75,7 @@ class DefaultRawSuggestionPostProcessingStrategyTests: XCTestCase {
8075

8176
func test_whenOnlyLinebreaksOrSpacesBeforeMarkdownCodeBlock_shouldExtractCodeInside() {
8277
let strategy = DefaultRawSuggestionPostProcessingStrategy(
83-
openingCodeTag: "<Code>",
84-
closingCodeTag: "</Code>"
78+
codeWrappingTags: ("<Code>", "</Code>")
8579
)
8680
let result = strategy.extractSuggestion(
8781
from: """
@@ -120,8 +114,7 @@ class DefaultRawSuggestionPostProcessingStrategyTests: XCTestCase {
120114

121115
func test_whenMarkdownCodeBlockAndCodeTagFound_firstlyExtractCodeTag_thenCodeTag() {
122116
let strategy = DefaultRawSuggestionPostProcessingStrategy(
123-
openingCodeTag: "<Code>",
124-
closingCodeTag: "</Code>"
117+
codeWrappingTags: ("<Code>", "</Code>")
125118
)
126119
let result = strategy.extractSuggestion(
127120
from: """
@@ -137,8 +130,7 @@ class DefaultRawSuggestionPostProcessingStrategyTests: XCTestCase {
137130
func test_whenMarkdownCodeBlockAndCodeTagFound_butNoClosingTag_firstlyExtractCodeTag_thenCodeTag(
138131
) {
139132
let strategy = DefaultRawSuggestionPostProcessingStrategy(
140-
openingCodeTag: "<Code>",
141-
closingCodeTag: "</Code>"
133+
codeWrappingTags: ("<Code>", "</Code>")
142134
)
143135
let result = strategy.extractSuggestion(
144136
from: """
@@ -153,8 +145,7 @@ class DefaultRawSuggestionPostProcessingStrategyTests: XCTestCase {
153145

154146
func test_whenSuggestionHasTheSamePrefix_removeThePrefix() {
155147
let strategy = DefaultRawSuggestionPostProcessingStrategy(
156-
openingCodeTag: "<Code>",
157-
closingCodeTag: "</Code>"
148+
codeWrappingTags: ("<Code>", "</Code>")
158149
)
159150
let result = strategy.extractSuggestion(
160151
from: "suggestion"
@@ -165,8 +156,7 @@ class DefaultRawSuggestionPostProcessingStrategyTests: XCTestCase {
165156

166157
func test_whenSuggestionLooksLikeAMessage_parseItCorrectly() {
167158
let strategy = DefaultRawSuggestionPostProcessingStrategy(
168-
openingCodeTag: "<Code>",
169-
closingCodeTag: "</Code>"
159+
codeWrappingTags: ("<Code>", "</Code>")
170160
)
171161
let result = strategy.extractSuggestion(
172162
from: """
@@ -182,8 +172,7 @@ class DefaultRawSuggestionPostProcessingStrategyTests: XCTestCase {
182172

183173
func test_whenSuggestionHasTheSamePrefix_inTags_removeThePrefix() {
184174
let strategy = DefaultRawSuggestionPostProcessingStrategy(
185-
openingCodeTag: "<Code>",
186-
closingCodeTag: "</Code>"
175+
codeWrappingTags: ("<Code>", "</Code>")
187176
)
188177
var suggestion = "prefix suggestion"
189178
strategy.removePrefix(from: &suggestion, infillPrefix: "prefix")
@@ -193,8 +182,7 @@ class DefaultRawSuggestionPostProcessingStrategyTests: XCTestCase {
193182

194183
func test_whenSuggestionHasTheSameSuffix_removeTheSuffix() {
195184
let strategy = DefaultRawSuggestionPostProcessingStrategy(
196-
openingCodeTag: "<Code>",
197-
closingCodeTag: "</Code>"
185+
codeWrappingTags: ("<Code>", "</Code>")
198186
)
199187
var suggestion = "suggestion\na\nb"
200188
strategy.removeSuffix(from: &suggestion, suffix: [
@@ -214,11 +202,10 @@ class DefaultRawSuggestionPostProcessingStrategyTests: XCTestCase {
214202

215203
XCTAssertEqual(suggestion3, "suggestion\na\n")
216204
}
217-
205+
218206
func test_case_1() {
219207
let strategy = DefaultRawSuggestionPostProcessingStrategy(
220-
openingCodeTag: "<Code>",
221-
closingCodeTag: "</Code>"
208+
codeWrappingTags: ("<Code>", "</Code>")
222209
)
223210
let result = strategy.postProcess(
224211
rawSuggestion: """

‎TestPlan.xctestplan

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@
1515
{
1616
"target" : {
1717
"containerPath" : "container:Core",
18-
"identifier" : "SuggestionServiceTests",
19-
"name" : "SuggestionServiceTests"
18+
"identifier" : "CodeCompletionServiceTests",
19+
"name" : "CodeCompletionServiceTests"
2020
}
2121
},
2222
{
@@ -25,6 +25,13 @@
2525
"identifier" : "FundamentalTests",
2626
"name" : "FundamentalTests"
2727
}
28+
},
29+
{
30+
"target" : {
31+
"containerPath" : "container:Core",
32+
"identifier" : "SuggestionServiceTests",
33+
"name" : "SuggestionServiceTests"
34+
}
2835
}
2936
],
3037
"version" : 1

0 commit comments

Comments
 (0)
Please sign in to comment.