Skip to content

Commit 1162598

Browse files
committed
Add decoding tests for missing fields in ExecutableCode and CodeExecutionResult
1 parent b78c4d2 commit 1162598

File tree

2 files changed

+214
-0
lines changed

2 files changed

+214
-0
lines changed

FirebaseAI/Tests/Unit/PartTests.swift

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,52 @@ final class PartTests: XCTestCase {
103103
XCTAssertEqual(part.code, "print('hello')")
104104
}
105105

106+
func testDecodeExecutableCodePart_missingLanguage() throws {
107+
let json = """
108+
{
109+
"executableCode": {
110+
"code": "print('hello')"
111+
}
112+
}
113+
"""
114+
let jsonData = try XCTUnwrap(json.data(using: .utf8))
115+
116+
let part = try decoder.decode(ExecutableCodePart.self, from: jsonData)
117+
118+
XCTAssertEqual(part.language.description, "LANGUAGE_UNSPECIFIED")
119+
XCTAssertEqual(part.code, "print('hello')")
120+
}
121+
122+
func testDecodeExecutableCodePart_missingCode() throws {
123+
let json = """
124+
{
125+
"executableCode": {
126+
"language": "PYTHON"
127+
}
128+
}
129+
"""
130+
let jsonData = try XCTUnwrap(json.data(using: .utf8))
131+
132+
let part = try decoder.decode(ExecutableCodePart.self, from: jsonData)
133+
134+
XCTAssertEqual(part.language, .python)
135+
XCTAssertEqual(part.code, "")
136+
}
137+
138+
func testDecodeExecutableCodePart_missingLanguageAndCode() throws {
139+
let json = """
140+
{
141+
"executableCode": {}
142+
}
143+
"""
144+
let jsonData = try XCTUnwrap(json.data(using: .utf8))
145+
146+
let part = try decoder.decode(ExecutableCodePart.self, from: jsonData)
147+
148+
XCTAssertEqual(part.language.description, "LANGUAGE_UNSPECIFIED")
149+
XCTAssertEqual(part.code, "")
150+
}
151+
106152
func testDecodeCodeExecutionResultPart() throws {
107153
let json = """
108154
{
@@ -120,6 +166,52 @@ final class PartTests: XCTestCase {
120166
XCTAssertEqual(part.output, "hello")
121167
}
122168

169+
func testDecodeCodeExecutionResultPart_missingOutcome() throws {
170+
let json = """
171+
{
172+
"codeExecutionResult": {
173+
"output": "hello"
174+
}
175+
}
176+
"""
177+
let jsonData = try XCTUnwrap(json.data(using: .utf8))
178+
179+
let part = try decoder.decode(CodeExecutionResultPart.self, from: jsonData)
180+
181+
XCTAssertEqual(part.outcome.description, "OUTCOME_UNSPECIFIED")
182+
XCTAssertEqual(part.output, "hello")
183+
}
184+
185+
func testDecodeCodeExecutionResultPart_missingOutput() throws {
186+
let json = """
187+
{
188+
"codeExecutionResult": {
189+
"outcome": "OUTCOME_OK"
190+
}
191+
}
192+
"""
193+
let jsonData = try XCTUnwrap(json.data(using: .utf8))
194+
195+
let part = try decoder.decode(CodeExecutionResultPart.self, from: jsonData)
196+
197+
XCTAssertEqual(part.outcome, .ok)
198+
XCTAssertNil(part.output)
199+
}
200+
201+
func testDecodeCodeExecutionResultPart_missingOutcomeAndOutput() throws {
202+
let json = """
203+
{
204+
"codeExecutionResult": {}
205+
}
206+
"""
207+
let jsonData = try XCTUnwrap(json.data(using: .utf8))
208+
209+
let part = try decoder.decode(CodeExecutionResultPart.self, from: jsonData)
210+
211+
XCTAssertEqual(part.outcome.description, "OUTCOME_UNSPECIFIED")
212+
XCTAssertNil(part.output)
213+
}
214+
123215
// MARK: - Part Encoding
124216

125217
func testEncodeTextPart() throws {

FirebaseAI/Tests/Unit/Types/InternalPartTests.swift

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,67 @@ final class InternalPartTests: XCTestCase {
306306
XCTAssertEqual(executableCode.code, "print('hello')")
307307
}
308308

309+
func testDecodeExecutableCodePart_missingLanguage() throws {
310+
let json = """
311+
{
312+
"executableCode": {
313+
"code": "print('hello')"
314+
}
315+
}
316+
"""
317+
let jsonData = try XCTUnwrap(json.data(using: .utf8))
318+
319+
let part = try decoder.decode(InternalPart.self, from: jsonData)
320+
321+
XCTAssertNil(part.isThought)
322+
guard case let .executableCode(executableCode) = part.data else {
323+
XCTFail("Decoded part is not an executableCode part.")
324+
return
325+
}
326+
XCTAssertNil(executableCode.language)
327+
XCTAssertEqual(executableCode.code, "print('hello')")
328+
}
329+
330+
func testDecodeExecutableCodePart_missingCode() throws {
331+
let json = """
332+
{
333+
"executableCode": {
334+
"language": "PYTHON"
335+
}
336+
}
337+
"""
338+
let jsonData = try XCTUnwrap(json.data(using: .utf8))
339+
340+
let part = try decoder.decode(InternalPart.self, from: jsonData)
341+
342+
XCTAssertNil(part.isThought)
343+
guard case let .executableCode(executableCode) = part.data else {
344+
XCTFail("Decoded part is not an executableCode part.")
345+
return
346+
}
347+
XCTAssertEqual(executableCode.language, .init(kind: .python))
348+
XCTAssertNil(executableCode.code)
349+
}
350+
351+
func testDecodeExecutableCodePart_missingLanguageAndCode() throws {
352+
let json = """
353+
{
354+
"executableCode": {}
355+
}
356+
"""
357+
let jsonData = try XCTUnwrap(json.data(using: .utf8))
358+
359+
let part = try decoder.decode(InternalPart.self, from: jsonData)
360+
361+
XCTAssertNil(part.isThought)
362+
guard case let .executableCode(executableCode) = part.data else {
363+
XCTFail("Decoded part is not an executableCode part.")
364+
return
365+
}
366+
XCTAssertNil(executableCode.language)
367+
XCTAssertNil(executableCode.code)
368+
}
369+
309370
func testDecodeCodeExecutionResultPart() throws {
310371
let json = """
311372
{
@@ -327,4 +388,65 @@ final class InternalPartTests: XCTestCase {
327388
XCTAssertEqual(codeExecutionResult.outcome, .init(kind: .ok))
328389
XCTAssertEqual(codeExecutionResult.output, "hello")
329390
}
391+
392+
func testDecodeCodeExecutionResultPart_missingOutcome() throws {
393+
let json = """
394+
{
395+
"codeExecutionResult": {
396+
"output": "hello"
397+
}
398+
}
399+
"""
400+
let jsonData = try XCTUnwrap(json.data(using: .utf8))
401+
402+
let part = try decoder.decode(InternalPart.self, from: jsonData)
403+
404+
XCTAssertNil(part.isThought)
405+
guard case let .codeExecutionResult(codeExecutionResult) = part.data else {
406+
XCTFail("Decoded part is not a codeExecutionResult part.")
407+
return
408+
}
409+
XCTAssertNil(codeExecutionResult.outcome)
410+
XCTAssertEqual(codeExecutionResult.output, "hello")
411+
}
412+
413+
func testDecodeCodeExecutionResultPart_missingOutput() throws {
414+
let json = """
415+
{
416+
"codeExecutionResult": {
417+
"outcome": "OUTCOME_OK"
418+
}
419+
}
420+
"""
421+
let jsonData = try XCTUnwrap(json.data(using: .utf8))
422+
423+
let part = try decoder.decode(InternalPart.self, from: jsonData)
424+
425+
XCTAssertNil(part.isThought)
426+
guard case let .codeExecutionResult(codeExecutionResult) = part.data else {
427+
XCTFail("Decoded part is not a codeExecutionResult part.")
428+
return
429+
}
430+
XCTAssertEqual(codeExecutionResult.outcome, .init(kind: .ok))
431+
XCTAssertNil(codeExecutionResult.output)
432+
}
433+
434+
func testDecodeCodeExecutionResultPart_missingOutcomeAndOutput() throws {
435+
let json = """
436+
{
437+
"codeExecutionResult": {}
438+
}
439+
"""
440+
let jsonData = try XCTUnwrap(json.data(using: .utf8))
441+
442+
let part = try decoder.decode(InternalPart.self, from: jsonData)
443+
444+
XCTAssertNil(part.isThought)
445+
guard case let .codeExecutionResult(codeExecutionResult) = part.data else {
446+
XCTFail("Decoded part is not a codeExecutionResult part.")
447+
return
448+
}
449+
XCTAssertNil(codeExecutionResult.outcome)
450+
XCTAssertNil(codeExecutionResult.output)
451+
}
330452
}

0 commit comments

Comments
 (0)