Skip to content

Commit 1f1fac1

Browse files
committed
Update tests
1 parent d189798 commit 1f1fac1

2 files changed

Lines changed: 82 additions & 16 deletions

File tree

Tool/Tests/LangChainTests/TextSplitterTests/RecursiveCharacterTextSplitterTests.swift

Lines changed: 52 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,16 @@ final class RecursiveCharacterTextSplitterTests: XCTestCase {
1717
let result = try await splitter.split(text: text)
1818

1919
XCTAssertEqual(result, [
20-
"Madam Speaker, Madam Vice President, our First Lady and Second Gentleman. Members of Congress and",
21-
"of Congress and the Cabinet. Justices of the Supreme Court. My fellow Americans.",
20+
.init(
21+
text: "Madam Speaker, Madam Vice President, our First Lady and Second Gentleman. Members of Congress and",
22+
startUTF16Offset: 0,
23+
endUTF16Offset: 97
24+
),
25+
.init(
26+
text: "of Congress and the Cabinet. Justices of the Supreme Court. My fellow Americans.",
27+
startUTF16Offset: 97,
28+
endUTF16Offset: 110
29+
),
2230
])
2331
}
2432

@@ -65,14 +73,48 @@ final class RecursiveCharacterTextSplitterTests: XCTestCase {
6573
let result = try await splitter.split(text: code)
6674
XCTAssertEqual(
6775
result,
68-
["protocol Animal {\n var name: String { get }\n var legs: Int { get }\n func makeSound()\n}\n",
69-
"\n@MainActor",
70-
"\nprivate class Dog: Animal {\n var name: String\n var legs: Int\n init(name: String, legs:",
71-
"String, legs: Int) {\n self.name = name\n self.legs = legs\n }\n func makeSound()",
72-
"func makeSound() {\n print(\"Woof!\")\n }\n}\n",
73-
"\nfinal class Cat: Animal {\n var name: String\n var legs: Int\n init(name: String, legs: Int)",
74-
"String, legs: Int) {\n self.name = name\n self.legs = legs\n }\n func makeSound()",
75-
"func makeSound() {\n print(\"Meow!\")\n }\n}"]
76+
[
77+
.init(
78+
text: "protocol Animal {\n var name: String { get }\n var legs: Int { get }\n func makeSound()\n}\n",
79+
startUTF16Offset: 97,
80+
endUTF16Offset: 110
81+
),
82+
.init(
83+
text: "\n@MainActor",
84+
startUTF16Offset: 97,
85+
endUTF16Offset: 110
86+
),
87+
.init(
88+
text: "\nprivate class Dog: Animal {\n var name: String\n var legs: Int\n init(name: String, legs:",
89+
startUTF16Offset: 97,
90+
endUTF16Offset: 110
91+
),
92+
.init(
93+
text: "String, legs: Int) {\n self.name = name\n self.legs = legs\n }\n func makeSound()",
94+
startUTF16Offset: 97,
95+
endUTF16Offset: 110
96+
),
97+
.init(
98+
text: "func makeSound() {\n print(\"Woof!\")\n }\n}\n",
99+
startUTF16Offset: 97,
100+
endUTF16Offset: 110
101+
),
102+
.init(
103+
text: "\nfinal class Cat: Animal {\n var name: String\n var legs: Int\n init(name: String, legs: Int)",
104+
startUTF16Offset: 97,
105+
endUTF16Offset: 110
106+
),
107+
.init(
108+
text: "String, legs: Int) {\n self.name = name\n self.legs = legs\n }\n func makeSound()",
109+
startUTF16Offset: 97,
110+
endUTF16Offset: 110
111+
),
112+
.init(
113+
text: "func makeSound() {\n print(\"Meow!\")\n }\n}",
114+
startUTF16Offset: 97,
115+
endUTF16Offset: 110
116+
),
117+
]
76118
)
77119
}
78120
}

Tool/Tests/LangChainTests/TextSplitterTests/TextSplitterTests.swift

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ final class TextSplitterTests: XCTestCase {
77
var chunkSize: Int
88
var chunkOverlap: Int
99
var lengthFunction: (String) -> Int = { $0.count }
10-
func split(text: String) async throws -> [String] {
10+
func split(text: String) async throws -> [TextChunk] {
1111
[]
1212
}
1313
}
@@ -25,7 +25,15 @@ final class TextSplitterTests: XCTestCase {
2525

2626
XCTAssertEqual(
2727
result,
28-
["Madam", " Speaker,", " Madam", " Vice", " President,", " our", " First"]
28+
[
29+
.init(text: "Madam", startUTF16Offset: 0, endUTF16Offset: 5),
30+
.init(text: " Speaker,", startUTF16Offset: 5, endUTF16Offset: 14),
31+
.init(text: " Madam", startUTF16Offset: 14, endUTF16Offset: 20),
32+
.init(text: " Vice", startUTF16Offset: 20, endUTF16Offset: 25),
33+
.init(text: " President,", startUTF16Offset: 25, endUTF16Offset: 36),
34+
.init(text: " our", startUTF16Offset: 36, endUTF16Offset: 40),
35+
.init(text: " First", startUTF16Offset: 40, endUTF16Offset: 46),
36+
]
2937
)
3038
}
3139

@@ -42,7 +50,10 @@ final class TextSplitterTests: XCTestCase {
4250

4351
XCTAssertEqual(
4452
result,
45-
["Madam Speaker, Madam", " Vice President, our First"]
53+
[
54+
.init(text: "Madam Speaker, Madam", startUTF16Offset: 0, endUTF16Offset: 20),
55+
.init(text: " Vice President, our First", startUTF16Offset: 20, endUTF16Offset: 46),
56+
]
4657
)
4758
}
4859

@@ -53,14 +64,27 @@ final class TextSplitterTests: XCTestCase {
5364
)
5465

5566
let result = splitter.mergeSplits(
56-
["Madam", " Speaker,", " Madam", " Vice", " President,", " our", " First"]
67+
[
68+
.init(text: "Madam", startUTF16Offset: 0, endUTF16Offset: 5),
69+
.init(text: " Speaker,", startUTF16Offset: 5, endUTF16Offset: 14),
70+
.init(text: " Madam", startUTF16Offset: 14, endUTF16Offset: 20),
71+
.init(text: " Vice", startUTF16Offset: 20, endUTF16Offset: 25),
72+
.init(text: " President,", startUTF16Offset: 25, endUTF16Offset: 36),
73+
.init(text: " our", startUTF16Offset: 36, endUTF16Offset: 40),
74+
.init(text: " First", startUTF16Offset: 40, endUTF16Offset: 46),
75+
]
5776
)
5877

5978
XCTAssertEqual(
6079
result,
61-
["Madam Speaker,", "Madam Vice", "President, our", "our First"]
80+
[
81+
.init(text: "Madam Speaker,", startUTF16Offset: 0, endUTF16Offset: 14),
82+
.init(text: " Madam Vice", startUTF16Offset: 14, endUTF16Offset: 25),
83+
.init(text: " President, our", startUTF16Offset: 25, endUTF16Offset: 40),
84+
.init(text: " our First", startUTF16Offset: 36, endUTF16Offset: 46),
85+
]
6286
)
63-
XCTAssertTrue(result.allSatisfy { $0.count <= 15 })
87+
XCTAssertTrue(result.allSatisfy { $0.text.count <= 15 })
6488
}
6589
}
6690

0 commit comments

Comments
 (0)