Skip to content

Commit 2aa8450

Browse files
committed
Add tests
1 parent 37d304e commit 2aa8450

File tree

2 files changed

+97
-1
lines changed

2 files changed

+97
-1
lines changed
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
// Copyright 2024 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
import Foundation
16+
import XCTest
17+
18+
@testable import GoogleGenerativeAI
19+
20+
@available(iOS 15.0, macOS 11.0, macCatalyst 15.0, *)
21+
final class GenerationConfigTests: XCTestCase {
22+
let encoder = JSONEncoder()
23+
24+
override func setUp() {
25+
encoder.outputFormatting = .init(
26+
arrayLiteral: .prettyPrinted, .sortedKeys, .withoutEscapingSlashes
27+
)
28+
}
29+
30+
// MARK: GenerationConfig Encoding
31+
32+
func testEncodeGenerationConfig_default() throws {
33+
let generationConfig = GenerationConfig()
34+
35+
let jsonData = try encoder.encode(generationConfig)
36+
37+
let json = try XCTUnwrap(String(data: jsonData, encoding: .utf8))
38+
XCTAssertEqual(json, """
39+
{
40+
41+
}
42+
""")
43+
}
44+
45+
func testEncodeGenerationConfig_allOptions() throws {
46+
let temperature: Float = 0.5
47+
let topP: Float = 0.95
48+
let topK = 40
49+
let candidateCount = 2
50+
let maxOutputTokens = 256
51+
let stopSequences = ["END", "DONE"]
52+
let responseMIMEType = "text/plain"
53+
let generationConfig = GenerationConfig(
54+
temperature: temperature,
55+
topP: topP,
56+
topK: topK,
57+
candidateCount: candidateCount,
58+
maxOutputTokens: maxOutputTokens,
59+
stopSequences: stopSequences,
60+
responseMIMEType: responseMIMEType
61+
)
62+
63+
let jsonData = try encoder.encode(generationConfig)
64+
65+
let json = try XCTUnwrap(String(data: jsonData, encoding: .utf8))
66+
XCTAssertEqual(json, """
67+
{
68+
"candidateCount" : \(candidateCount),
69+
"maxOutputTokens" : \(maxOutputTokens),
70+
"responseMIMEType" : "\(responseMIMEType)",
71+
"stopSequences" : [
72+
"END",
73+
"DONE"
74+
],
75+
"temperature" : \(temperature),
76+
"topK" : \(topK),
77+
"topP" : \(topP)
78+
}
79+
""")
80+
}
81+
82+
func testEncodeGenerationConfig_responseMIMEType() throws {
83+
let mimeType = "image/jpeg"
84+
let generationConfig = GenerationConfig(responseMIMEType: mimeType)
85+
86+
let jsonData = try encoder.encode(generationConfig)
87+
88+
let json = try XCTUnwrap(String(data: jsonData, encoding: .utf8))
89+
XCTAssertEqual(json, """
90+
{
91+
"responseMIMEType" : "\(mimeType)"
92+
}
93+
""")
94+
}
95+
}

Tests/GoogleAITests/GoogleAITests.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@ final class GoogleGenerativeAITests: XCTestCase {
2828
topK: 16,
2929
candidateCount: 4,
3030
maxOutputTokens: 256,
31-
stopSequences: ["..."])
31+
stopSequences: ["..."],
32+
responseMIMEType: "text/plain")
3233
let filters = [SafetySetting(harmCategory: .dangerousContent, threshold: .blockOnlyHigh)]
3334
let systemInstruction = ModelContent(role: "system", parts: [.text("Talk like a pirate.")])
3435

0 commit comments

Comments
 (0)