|
| 1 | +// Copyright 2025 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 FirebaseAI |
| 16 | +import XCTest |
| 17 | + |
| 18 | +@available(iOS 15.0, macOS 12.0, macCatalyst 15.0, tvOS 15.0, watchOS 8.0, *) |
| 19 | +final class CitationTests: XCTestCase { |
| 20 | + let decoder = JSONDecoder() |
| 21 | + |
| 22 | + // MARK: - Decoding Tests |
| 23 | + |
| 24 | + func testDecodeCitation_minimalParameters() throws { |
| 25 | + let expectedEndIndex = 150 |
| 26 | + let json = """ |
| 27 | + { |
| 28 | + "endIndex" : \(expectedEndIndex) |
| 29 | + } |
| 30 | + """ |
| 31 | + let jsonData = try XCTUnwrap(json.data(using: .utf8)) |
| 32 | + |
| 33 | + let citation = try decoder.decode(Citation.self, from: jsonData) |
| 34 | + |
| 35 | + XCTAssertEqual(citation.startIndex, 0, "Omitted startIndex should be decoded as 0.") |
| 36 | + XCTAssertEqual(citation.endIndex, expectedEndIndex) |
| 37 | + XCTAssertNil(citation.uri) |
| 38 | + XCTAssertNil(citation.title) |
| 39 | + XCTAssertNil(citation.license) |
| 40 | + XCTAssertNil(citation.publicationDate) |
| 41 | + } |
| 42 | + |
| 43 | + func testDecodeCitation_allParameters() throws { |
| 44 | + let expectedStartIndex = 100 |
| 45 | + let expectedEndIndex = 200 |
| 46 | + let expectedURI = "https://example.com/citation-1" |
| 47 | + let expectedTitle = "Example Citation Title" |
| 48 | + let expectedLicense = "mit" |
| 49 | + let expectedYear = 2023 |
| 50 | + let expectedMonth = 10 |
| 51 | + let expectedDay = 26 |
| 52 | + let json = """ |
| 53 | + { |
| 54 | + "startIndex" : \(expectedStartIndex), |
| 55 | + "endIndex" : \(expectedEndIndex), |
| 56 | + "uri" : "\(expectedURI)", |
| 57 | + "title" : "\(expectedTitle)", |
| 58 | + "license" : "\(expectedLicense)", |
| 59 | + "publicationDate" : { |
| 60 | + "year" : \(expectedYear), |
| 61 | + "month" : \(expectedMonth), |
| 62 | + "day" : \(expectedDay) |
| 63 | + } |
| 64 | + } |
| 65 | + """ |
| 66 | + let jsonData = try XCTUnwrap(json.data(using: .utf8)) |
| 67 | + |
| 68 | + let citation = try decoder.decode(Citation.self, from: jsonData) |
| 69 | + |
| 70 | + XCTAssertEqual(citation.startIndex, expectedStartIndex) |
| 71 | + XCTAssertEqual(citation.endIndex, expectedEndIndex) |
| 72 | + XCTAssertEqual(citation.uri, expectedURI) |
| 73 | + XCTAssertEqual(citation.title, expectedTitle) |
| 74 | + XCTAssertEqual(citation.license, expectedLicense) |
| 75 | + let publicationDate = try XCTUnwrap(citation.publicationDate) |
| 76 | + XCTAssertEqual(publicationDate.year, expectedYear) |
| 77 | + XCTAssertEqual(publicationDate.month, expectedMonth) |
| 78 | + XCTAssertEqual(publicationDate.day, expectedDay) |
| 79 | + } |
| 80 | + |
| 81 | + func testDecodeCitation_emptyStringsForOptionals_setsToNil() throws { |
| 82 | + let expectedEndIndex = 300 |
| 83 | + let json = """ |
| 84 | + { |
| 85 | + "endIndex" : \(expectedEndIndex), |
| 86 | + "uri" : "", |
| 87 | + "title" : "", |
| 88 | + "license" : "" |
| 89 | + } |
| 90 | + """ |
| 91 | + let jsonData = try XCTUnwrap(json.data(using: .utf8)) |
| 92 | + |
| 93 | + let citation = try decoder.decode(Citation.self, from: jsonData) |
| 94 | + |
| 95 | + XCTAssertEqual(citation.startIndex, 0, "Omitted startIndex should be decoded as 0.") |
| 96 | + XCTAssertEqual(citation.endIndex, expectedEndIndex) |
| 97 | + XCTAssertNil(citation.uri, "Empty URI string should be decoded as nil.") |
| 98 | + XCTAssertNil(citation.title, "Empty title string should be decoded as nil.") |
| 99 | + XCTAssertNil(citation.license, "Empty license string should be decoded as nil.") |
| 100 | + XCTAssertNil(citation.publicationDate) |
| 101 | + } |
| 102 | + |
| 103 | + func testDecodeCitation_missingEndIndex_throws() throws { |
| 104 | + let json = """ |
| 105 | + { |
| 106 | + "startIndex" : 10 |
| 107 | + } |
| 108 | + """ |
| 109 | + let jsonData = try XCTUnwrap(json.data(using: .utf8)) |
| 110 | + |
| 111 | + XCTAssertThrowsError(try decoder.decode(Citation.self, from: jsonData)) |
| 112 | + } |
| 113 | +} |
0 commit comments