forked from intitni/CopilotForXcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTextSplitterTests.swift
More file actions
66 lines (54 loc) · 1.69 KB
/
TextSplitterTests.swift
File metadata and controls
66 lines (54 loc) · 1.69 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
import XCTest
@testable import LangChain
final class TextSplitterTests: XCTestCase {
struct Splitter: TextSplitter {
var chunkSize: Int
var chunkOverlap: Int
var lengthFunction: (String) -> Int = { $0.count }
func split(text: String) async throws -> [String] {
[]
}
}
func test_split_text_with_text_separator() async throws {
let splitter = Splitter(
chunkSize: 1,
chunkOverlap: 1
)
let result = splitter.split(
text: "Madam Speaker, Madam Vice President, our First",
separator: " "
)
XCTAssertEqual(
result,
["Madam", " Speaker,", " Madam", " Vice", " President,", " our", " First"]
)
}
func test_split_text_with_regex_separator() async throws {
let splitter = Splitter(
chunkSize: 1,
chunkOverlap: 1
)
let result = splitter.split(
text: "Madam Speaker, Madam Vice President, our First",
separator: "\\s\\w\\w\\w\\w\\s" // split at " Vice "
)
XCTAssertEqual(
result,
["Madam Speaker, Madam", " Vice President, our First"]
)
}
func test_merge_splits() async throws {
let splitter = Splitter(
chunkSize: 15,
chunkOverlap: 5
)
let result = splitter.mergeSplits(
["Madam", " Speaker,", " Madam", " Vice", " President,", " our", " First"]
)
XCTAssertEqual(
result,
["Madam Speaker,", "Madam Vice", "President, our", "our First"]
)
XCTAssertTrue(result.allSatisfy { $0.count <= 15 })
}
}