Skip to content

Commit 3199d94

Browse files
committed
Add decoding and encoding tests in PartTests
1 parent b4b5676 commit 3199d94

File tree

1 file changed

+66
-0
lines changed

1 file changed

+66
-0
lines changed

FirebaseAI/Tests/Unit/PartTests.swift

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,40 @@ final class PartTests: XCTestCase {
8686
XCTAssertEqual(functionResponse.response, [resultParameter: .string(resultValue)])
8787
}
8888

89+
func testDecodeExecutableCodePart() throws {
90+
let json = """
91+
{
92+
"executableCode": {
93+
"language": "PYTHON",
94+
"code": "print('hello')"
95+
}
96+
}
97+
"""
98+
let jsonData = try XCTUnwrap(json.data(using: .utf8))
99+
100+
let part = try decoder.decode(ExecutableCodePart.self, from: jsonData)
101+
102+
XCTAssertEqual(part.language, .python)
103+
XCTAssertEqual(part.code, "print('hello')")
104+
}
105+
106+
func testDecodeCodeExecutionResultPart() throws {
107+
let json = """
108+
{
109+
"codeExecutionResult": {
110+
"outcome": "OUTCOME_OK",
111+
"output": "hello"
112+
}
113+
}
114+
"""
115+
let jsonData = try XCTUnwrap(json.data(using: .utf8))
116+
117+
let part = try decoder.decode(CodeExecutionResultPart.self, from: jsonData)
118+
119+
XCTAssertEqual(part.outcome, .ok)
120+
XCTAssertEqual(part.output, "hello")
121+
}
122+
89123
// MARK: - Part Encoding
90124

91125
func testEncodeTextPart() throws {
@@ -139,6 +173,38 @@ final class PartTests: XCTestCase {
139173
""")
140174
}
141175

176+
func testEncodeExecutableCodePart() throws {
177+
let executableCodePart = ExecutableCodePart(language: .python, code: "print('hello')")
178+
179+
let jsonData = try encoder.encode(executableCodePart)
180+
181+
let json = try XCTUnwrap(String(data: jsonData, encoding: .utf8))
182+
XCTAssertEqual(json, """
183+
{
184+
"executableCode" : {
185+
"code" : "print('hello')",
186+
"language" : "PYTHON"
187+
}
188+
}
189+
""")
190+
}
191+
192+
func testEncodeCodeExecutionResultPart() throws {
193+
let codeExecutionResultPart = CodeExecutionResultPart(outcome: .ok, output: "hello")
194+
195+
let jsonData = try encoder.encode(codeExecutionResultPart)
196+
197+
let json = try XCTUnwrap(String(data: jsonData, encoding: .utf8))
198+
XCTAssertEqual(json, """
199+
{
200+
"codeExecutionResult" : {
201+
"outcome" : "OUTCOME_OK",
202+
"output" : "hello"
203+
}
204+
}
205+
""")
206+
}
207+
142208
// MARK: - Helpers
143209

144210
private static func bundle() -> Bundle {

0 commit comments

Comments
 (0)