Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
88 changes: 0 additions & 88 deletions FirebaseAI/Tests/Unit/JSONValueTests+Coverage.swift

This file was deleted.

68 changes: 68 additions & 0 deletions FirebaseAI/Tests/Unit/JSONValueTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,48 @@ final class JSONValueTests: XCTestCase {
XCTAssertEqual(json, "null")
}

func testDecodeNestedObject() throws {
let nestedObject: JSONObject = [
"nestedKey": .string("nestedValue"),
]
let expectedObject: JSONObject = [
"numberKey": .number(numberValue),
"objectKey": .object(nestedObject),
]
let json = """
{
"numberKey": \(numberValue),
"objectKey": {
"nestedKey": "nestedValue"
}
}
"""
let jsonData = try XCTUnwrap(json.data(using: .utf8))

let jsonObject = try XCTUnwrap(decoder.decode(JSONValue.self, from: jsonData))

XCTAssertEqual(jsonObject, .object(expectedObject))
}

func testDecodeNestedArray() throws {
let nestedArray: [JSONValue] = [.string("a"), .string("b")]
let expectedObject: JSONObject = [
"numberKey": .number(numberValue),
"arrayKey": .array(nestedArray),
]
let json = """
{
"numberKey": \(numberValue),
"arrayKey": ["a", "b"]
}
"""
let jsonData = try XCTUnwrap(json.data(using: .utf8))

let jsonObject = try XCTUnwrap(decoder.decode(JSONValue.self, from: jsonData))

XCTAssertEqual(jsonObject, .object(expectedObject))
}

func testEncodeNumber() throws {
let jsonData = try encoder.encode(JSONValue.number(numberValue))

Expand Down Expand Up @@ -143,4 +185,30 @@ final class JSONValueTests: XCTestCase {
let json = try XCTUnwrap(String(data: jsonData, encoding: .utf8))
XCTAssertEqual(json, "[null,\(numberValueEncoded)]")
}

func testEncodeNestedObject() throws {
let nestedObject: JSONObject = [
"nestedKey": .string("nestedValue"),
]
let objectValue: JSONObject = [
"numberKey": .number(numberValue),
"objectKey": .object(nestedObject),
]

let jsonData = try encoder.encode(JSONValue.object(objectValue))
let jsonObject = try XCTUnwrap(decoder.decode(JSONValue.self, from: jsonData))
XCTAssertEqual(jsonObject, .object(objectValue))
}

func testEncodeNestedArray() throws {
let nestedArray: [JSONValue] = [.string("a"), .string("b")]
let objectValue: JSONObject = [
"numberKey": .number(numberValue),
"arrayKey": .array(nestedArray),
]

let jsonData = try encoder.encode(JSONValue.object(objectValue))
let jsonObject = try XCTUnwrap(decoder.decode(JSONValue.self, from: jsonData))
XCTAssertEqual(jsonObject, .object(objectValue))
}
}
76 changes: 0 additions & 76 deletions FirebaseAI/Tests/Unit/PartsRepresentableTests+Coverage.swift

This file was deleted.

51 changes: 51 additions & 0 deletions FirebaseAI/Tests/Unit/PartsRepresentableTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -121,4 +121,55 @@ final class PartsRepresentableTests: XCTestCase {
}
}
#endif

func testMixedParts() throws {
let text = "This is a test"
let data = try XCTUnwrap("This is some data".data(using: .utf8))
let inlineData = InlineDataPart(data: data, mimeType: "text/plain")

let parts: [any PartsRepresentable] = [text, inlineData]
let modelContent = ModelContent(parts: parts)

XCTAssertEqual(modelContent.parts.count, 2)
let textPart = try XCTUnwrap(modelContent.parts[0] as? TextPart)
XCTAssertEqual(textPart.text, text)
let dataPart = try XCTUnwrap(modelContent.parts[1] as? InlineDataPart)
XCTAssertEqual(dataPart, inlineData)
}

#if canImport(UIKit)
func testMixedParts_withImage() throws {
let text = "This is a test"
let image = try XCTUnwrap(UIImage(systemName: "star"))
let parts: [any PartsRepresentable] = [text, image]
let modelContent = ModelContent(parts: parts)

XCTAssertEqual(modelContent.parts.count, 2)
let textPart = try XCTUnwrap(modelContent.parts[0] as? TextPart)
XCTAssertEqual(textPart.text, text)
let imagePart = try XCTUnwrap(modelContent.parts[1] as? InlineDataPart)
XCTAssertEqual(imagePart.mimeType, "image/jpeg")
XCTAssertFalse(imagePart.data.isEmpty)
}

#elseif canImport(AppKit)
func testMixedParts_withImage() throws {
let text = "This is a test"
let coreImage = CIImage(color: CIColor.blue)
.cropped(to: CGRect(origin: CGPoint.zero, size: CGSize(width: 16, height: 16)))
let rep = NSCIImageRep(ciImage: coreImage)
let image = NSImage(size: rep.size)
image.addRepresentation(rep)

let parts: [any PartsRepresentable] = [text, image]
let modelContent = ModelContent(parts: parts)

XCTAssertEqual(modelContent.parts.count, 2)
let textPart = try XCTUnwrap(modelContent.parts[0] as? TextPart)
XCTAssertEqual(textPart.text, text)
let imagePart = try XCTUnwrap(modelContent.parts[1] as? InlineDataPart)
XCTAssertEqual(imagePart.mimeType, "image/jpeg")
XCTAssertFalse(imagePart.data.isEmpty)
}
#endif
}
19 changes: 15 additions & 4 deletions FirebaseAI/Tests/Unit/SafetyTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@ final class SafetyTests: XCTestCase {
let decoder = JSONDecoder()
let encoder = JSONEncoder()

override func setUp() {
encoder.outputFormatting = .init(
arrayLiteral: .prettyPrinted, .sortedKeys, .withoutEscapingSlashes
)
}

// MARK: - SafetyRating Decoding

func testDecodeSafetyRating_allFieldsPresent() throws {
Expand Down Expand Up @@ -87,12 +93,15 @@ final class SafetyTests: XCTestCase {
threshold: .blockMediumAndAbove,
method: .severity
)
encoder.outputFormatting = .sortedKeys
let jsonData = try encoder.encode(setting)
let jsonString = try XCTUnwrap(String(data: jsonData, encoding: .utf8))

XCTAssertEqual(jsonString, """
{"category":"HARM_CATEGORY_HATE_SPEECH","method":"SEVERITY","threshold":"BLOCK_MEDIUM_AND_ABOVE"}
{
"category" : "HARM_CATEGORY_HATE_SPEECH",
"method" : "SEVERITY",
"threshold" : "BLOCK_MEDIUM_AND_ABOVE"
}
""")
}

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

XCTAssertEqual(jsonString, """
{"category":"HARM_CATEGORY_SEXUALLY_EXPLICIT","threshold":"BLOCK_ONLY_HIGH"}
{
"category" : "HARM_CATEGORY_SEXUALLY_EXPLICIT",
"threshold" : "BLOCK_ONLY_HIGH"
}
""")
}
}
Loading