forked from intitni/CopilotForXcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRecursiveCharacterTextSplitterTests.swift
More file actions
121 lines (110 loc) · 4 KB
/
Copy pathRecursiveCharacterTextSplitterTests.swift
File metadata and controls
121 lines (110 loc) · 4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
import XCTest
@testable import LangChain
final class RecursiveCharacterTextSplitterTests: XCTestCase {
func test_split_text() async throws {
let splitter = RecursiveCharacterTextSplitter(
separators: ["\n\n", "\n", " ", ""],
chunkSize: 100,
chunkOverlap: 20
)
let text = """
Madam Speaker, Madam Vice President, our First Lady and Second Gentleman. Members of Congress and the Cabinet. Justices of the Supreme Court. My fellow Americans.
"""
let result = try await splitter.split(text: text)
XCTAssertEqual(result, [
.init(
text: "Madam Speaker, Madam Vice President, our First Lady and Second Gentleman. Members of Congress and",
startUTF16Offset: 0,
endUTF16Offset: 97
),
.init(
text: " of Congress and the Cabinet. Justices of the Supreme Court. My fellow Americans.",
startUTF16Offset: 81,
endUTF16Offset: 162
),
])
}
func test_split_swift_code() async throws {
let splitter = RecursiveCharacterTextSplitter(
separatorSet: .swift,
chunkSize: 100,
chunkOverlap: 20
)
let code = """
protocol Animal {
var name: String { get }
var legs: Int { get }
func makeSound()
}
@MainActor
private class Dog: Animal {
var name: String
var legs: Int
init(name: String, legs: Int) {
self.name = name
self.legs = legs
}
func makeSound() {
print("Woof!")
}
}
final class Cat: Animal {
var name: String
var legs: Int
init(name: String, legs: Int) {
self.name = name
self.legs = legs
}
func makeSound() {
print("Meow!")
}
}
"""
let result = try await splitter.split(text: code)
XCTAssertEqual(
result,
[
.init(
text: "protocol Animal {\n var name: String { get }\n var legs: Int { get }\n func makeSound()\n}\n",
startUTF16Offset: 0,
endUTF16Offset: 96
),
.init(
text: "\n@MainActor",
startUTF16Offset: 96,
endUTF16Offset: 107
),
.init(
text: "\nprivate class Dog: Animal {\n var name: String\n var legs: Int\n init(name: String, legs:",
startUTF16Offset: 107,
endUTF16Offset: 203
),
.init(
text: " String, legs: Int) {\n self.name = name\n self.legs = legs\n }\n func makeSound()",
startUTF16Offset: 189,
endUTF16Offset: 287
),
.init(
text: " func makeSound() {\n print(\"Woof!\")\n }\n}\n",
startUTF16Offset:267,
endUTF16Offset: 321
),
.init(
text: "\nfinal class Cat: Animal {\n var name: String\n var legs: Int\n init(name: String, legs: Int)",
startUTF16Offset: 321,
endUTF16Offset: 420
),
.init(
text: " String, legs: Int) {\n self.name = name\n self.legs = legs\n }\n func makeSound()",
startUTF16Offset: 401,
endUTF16Offset: 499
),
.init(
text: " func makeSound() {\n print(\"Meow!\")\n }\n}",
startUTF16Offset: 479,
endUTF16Offset: 532
),
]
)
}
}