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
79 lines (68 loc) · 2.66 KB
/
RecursiveCharacterTextSplitterTests.swift
File metadata and controls
79 lines (68 loc) · 2.66 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
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, [
"Madam Speaker, Madam Vice President, our First Lady and Second Gentleman. Members of Congress and",
"of Congress and the Cabinet. Justices of the Supreme Court. My fellow Americans.",
])
}
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,
["protocol Animal {\n var name: String { get }\n var legs: Int { get }\n func makeSound()\n}\n",
"\n@MainActor",
"\nprivate class Dog: Animal {\n var name: String\n var legs: Int\n init(name: String, legs:",
"String, legs: Int) {\n self.name = name\n self.legs = legs\n }\n func makeSound()",
"func makeSound() {\n print(\"Woof!\")\n }\n}\n",
"\nfinal class Cat: Animal {\n var name: String\n var legs: Int\n init(name: String, legs: Int)",
"String, legs: Int) {\n self.name = name\n self.legs = legs\n }\n func makeSound()",
"func makeSound() {\n print(\"Meow!\")\n }\n}"]
)
}
}