Skip to content

Commit 7c3cb3a

Browse files
authored
[Firebase AI] Merge +Coverage files into existing test files (#15136)
1 parent 76bf729 commit 7c3cb3a

File tree

5 files changed

+134
-168
lines changed

5 files changed

+134
-168
lines changed

FirebaseAI/Tests/Unit/JSONValueTests+Coverage.swift

Lines changed: 0 additions & 88 deletions
This file was deleted.

FirebaseAI/Tests/Unit/JSONValueTests.swift

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,48 @@ final class JSONValueTests: XCTestCase {
9797
XCTAssertEqual(json, "null")
9898
}
9999

100+
func testDecodeNestedObject() throws {
101+
let nestedObject: JSONObject = [
102+
"nestedKey": .string("nestedValue"),
103+
]
104+
let expectedObject: JSONObject = [
105+
"numberKey": .number(numberValue),
106+
"objectKey": .object(nestedObject),
107+
]
108+
let json = """
109+
{
110+
"numberKey": \(numberValue),
111+
"objectKey": {
112+
"nestedKey": "nestedValue"
113+
}
114+
}
115+
"""
116+
let jsonData = try XCTUnwrap(json.data(using: .utf8))
117+
118+
let jsonObject = try XCTUnwrap(decoder.decode(JSONValue.self, from: jsonData))
119+
120+
XCTAssertEqual(jsonObject, .object(expectedObject))
121+
}
122+
123+
func testDecodeNestedArray() throws {
124+
let nestedArray: [JSONValue] = [.string("a"), .string("b")]
125+
let expectedObject: JSONObject = [
126+
"numberKey": .number(numberValue),
127+
"arrayKey": .array(nestedArray),
128+
]
129+
let json = """
130+
{
131+
"numberKey": \(numberValue),
132+
"arrayKey": ["a", "b"]
133+
}
134+
"""
135+
let jsonData = try XCTUnwrap(json.data(using: .utf8))
136+
137+
let jsonObject = try XCTUnwrap(decoder.decode(JSONValue.self, from: jsonData))
138+
139+
XCTAssertEqual(jsonObject, .object(expectedObject))
140+
}
141+
100142
func testEncodeNumber() throws {
101143
let jsonData = try encoder.encode(JSONValue.number(numberValue))
102144

@@ -143,4 +185,30 @@ final class JSONValueTests: XCTestCase {
143185
let json = try XCTUnwrap(String(data: jsonData, encoding: .utf8))
144186
XCTAssertEqual(json, "[null,\(numberValueEncoded)]")
145187
}
188+
189+
func testEncodeNestedObject() throws {
190+
let nestedObject: JSONObject = [
191+
"nestedKey": .string("nestedValue"),
192+
]
193+
let objectValue: JSONObject = [
194+
"numberKey": .number(numberValue),
195+
"objectKey": .object(nestedObject),
196+
]
197+
198+
let jsonData = try encoder.encode(JSONValue.object(objectValue))
199+
let jsonObject = try XCTUnwrap(decoder.decode(JSONValue.self, from: jsonData))
200+
XCTAssertEqual(jsonObject, .object(objectValue))
201+
}
202+
203+
func testEncodeNestedArray() throws {
204+
let nestedArray: [JSONValue] = [.string("a"), .string("b")]
205+
let objectValue: JSONObject = [
206+
"numberKey": .number(numberValue),
207+
"arrayKey": .array(nestedArray),
208+
]
209+
210+
let jsonData = try encoder.encode(JSONValue.object(objectValue))
211+
let jsonObject = try XCTUnwrap(decoder.decode(JSONValue.self, from: jsonData))
212+
XCTAssertEqual(jsonObject, .object(objectValue))
213+
}
146214
}

FirebaseAI/Tests/Unit/PartsRepresentableTests+Coverage.swift

Lines changed: 0 additions & 76 deletions
This file was deleted.

FirebaseAI/Tests/Unit/PartsRepresentableTests.swift

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,4 +121,55 @@ final class PartsRepresentableTests: XCTestCase {
121121
}
122122
}
123123
#endif
124+
125+
func testMixedParts() throws {
126+
let text = "This is a test"
127+
let data = try XCTUnwrap("This is some data".data(using: .utf8))
128+
let inlineData = InlineDataPart(data: data, mimeType: "text/plain")
129+
130+
let parts: [any PartsRepresentable] = [text, inlineData]
131+
let modelContent = ModelContent(parts: parts)
132+
133+
XCTAssertEqual(modelContent.parts.count, 2)
134+
let textPart = try XCTUnwrap(modelContent.parts[0] as? TextPart)
135+
XCTAssertEqual(textPart.text, text)
136+
let dataPart = try XCTUnwrap(modelContent.parts[1] as? InlineDataPart)
137+
XCTAssertEqual(dataPart, inlineData)
138+
}
139+
140+
#if canImport(UIKit)
141+
func testMixedParts_withImage() throws {
142+
let text = "This is a test"
143+
let image = try XCTUnwrap(UIImage(systemName: "star"))
144+
let parts: [any PartsRepresentable] = [text, image]
145+
let modelContent = ModelContent(parts: parts)
146+
147+
XCTAssertEqual(modelContent.parts.count, 2)
148+
let textPart = try XCTUnwrap(modelContent.parts[0] as? TextPart)
149+
XCTAssertEqual(textPart.text, text)
150+
let imagePart = try XCTUnwrap(modelContent.parts[1] as? InlineDataPart)
151+
XCTAssertEqual(imagePart.mimeType, "image/jpeg")
152+
XCTAssertFalse(imagePart.data.isEmpty)
153+
}
154+
155+
#elseif canImport(AppKit)
156+
func testMixedParts_withImage() throws {
157+
let text = "This is a test"
158+
let coreImage = CIImage(color: CIColor.blue)
159+
.cropped(to: CGRect(origin: CGPoint.zero, size: CGSize(width: 16, height: 16)))
160+
let rep = NSCIImageRep(ciImage: coreImage)
161+
let image = NSImage(size: rep.size)
162+
image.addRepresentation(rep)
163+
164+
let parts: [any PartsRepresentable] = [text, image]
165+
let modelContent = ModelContent(parts: parts)
166+
167+
XCTAssertEqual(modelContent.parts.count, 2)
168+
let textPart = try XCTUnwrap(modelContent.parts[0] as? TextPart)
169+
XCTAssertEqual(textPart.text, text)
170+
let imagePart = try XCTUnwrap(modelContent.parts[1] as? InlineDataPart)
171+
XCTAssertEqual(imagePart.mimeType, "image/jpeg")
172+
XCTAssertFalse(imagePart.data.isEmpty)
173+
}
174+
#endif
124175
}

FirebaseAI/Tests/Unit/SafetyTests.swift

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,12 @@ final class SafetyTests: XCTestCase {
2121
let decoder = JSONDecoder()
2222
let encoder = JSONEncoder()
2323

24+
override func setUp() {
25+
encoder.outputFormatting = .init(
26+
arrayLiteral: .prettyPrinted, .sortedKeys, .withoutEscapingSlashes
27+
)
28+
}
29+
2430
// MARK: - SafetyRating Decoding
2531

2632
func testDecodeSafetyRating_allFieldsPresent() throws {
@@ -87,12 +93,15 @@ final class SafetyTests: XCTestCase {
8793
threshold: .blockMediumAndAbove,
8894
method: .severity
8995
)
90-
encoder.outputFormatting = .sortedKeys
9196
let jsonData = try encoder.encode(setting)
9297
let jsonString = try XCTUnwrap(String(data: jsonData, encoding: .utf8))
9398

9499
XCTAssertEqual(jsonString, """
95-
{"category":"HARM_CATEGORY_HATE_SPEECH","method":"SEVERITY","threshold":"BLOCK_MEDIUM_AND_ABOVE"}
100+
{
101+
"category" : "HARM_CATEGORY_HATE_SPEECH",
102+
"method" : "SEVERITY",
103+
"threshold" : "BLOCK_MEDIUM_AND_ABOVE"
104+
}
96105
""")
97106
}
98107

@@ -101,12 +110,14 @@ final class SafetyTests: XCTestCase {
101110
harmCategory: .sexuallyExplicit,
102111
threshold: .blockOnlyHigh
103112
)
104-
encoder.outputFormatting = .sortedKeys
105113
let jsonData = try encoder.encode(setting)
106114
let jsonString = try XCTUnwrap(String(data: jsonData, encoding: .utf8))
107115

108116
XCTAssertEqual(jsonString, """
109-
{"category":"HARM_CATEGORY_SEXUALLY_EXPLICIT","threshold":"BLOCK_ONLY_HIGH"}
117+
{
118+
"category" : "HARM_CATEGORY_SEXUALLY_EXPLICIT",
119+
"threshold" : "BLOCK_ONLY_HIGH"
120+
}
110121
""")
111122
}
112123
}

0 commit comments

Comments
 (0)