|
| 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 XCTest |
| 16 | + |
| 17 | +@testable import FirebaseAI |
| 18 | + |
| 19 | +final class ProtoDurationTests: XCTestCase { |
| 20 | + let decoder = JSONDecoder() |
| 21 | + |
| 22 | + private func decodeProtoDuration(_ jsonString: String) throws -> ProtoDuration { |
| 23 | + let escapedString = "\"\(jsonString)\"" |
| 24 | + let jsonData = try XCTUnwrap(escapedString.data(using: .utf8)) |
| 25 | + |
| 26 | + return try decoder.decode(ProtoDuration.self, from: jsonData) |
| 27 | + } |
| 28 | + |
| 29 | + private func expectDecodeFailure(_ jsonString: String) throws -> DecodingError.Context? { |
| 30 | + do { |
| 31 | + let _ = try decodeProtoDuration(jsonString) |
| 32 | + XCTFail("Expected decoding to fail") |
| 33 | + return nil |
| 34 | + } catch { |
| 35 | + let decodingError = try XCTUnwrap(error as? DecodingError) |
| 36 | + guard case let .dataCorrupted(dataCorrupted) = decodingError else { |
| 37 | + XCTFail("Error was not a data corrupted error") |
| 38 | + return nil |
| 39 | + } |
| 40 | + |
| 41 | + return dataCorrupted |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + func testDecodeProtoDuration_standardDuration() throws { |
| 46 | + let duration = try decodeProtoDuration("120.000000123s") |
| 47 | + XCTAssertEqual(duration.seconds, 120) |
| 48 | + XCTAssertEqual(duration.nanos, 123) |
| 49 | + |
| 50 | + XCTAssertEqual(duration.timeInterval, 120.000000123, accuracy: 1e-9) |
| 51 | + } |
| 52 | + |
| 53 | + func testDecodeProtoDuration_withoutNanoseconds() throws { |
| 54 | + let duration = try decodeProtoDuration("120s") |
| 55 | + XCTAssertEqual(duration.seconds, 120) |
| 56 | + XCTAssertEqual(duration.nanos, 0) |
| 57 | + |
| 58 | + XCTAssertEqual(duration.timeInterval, 120, accuracy: 1e-9) |
| 59 | + } |
| 60 | + |
| 61 | + func testDecodeProtoDuration_maxNanosecondDigits() throws { |
| 62 | + let duration = try decodeProtoDuration("15.123456789s") |
| 63 | + XCTAssertEqual(duration.seconds, 15) |
| 64 | + XCTAssertEqual(duration.nanos, 123_456_789) |
| 65 | + |
| 66 | + XCTAssertEqual(duration.timeInterval, 15.123456789, accuracy: 1e-9) |
| 67 | + } |
| 68 | + |
| 69 | + func testDecodeProtoDuration_withMilliseconds() throws { |
| 70 | + let duration = try decodeProtoDuration("15.123s") |
| 71 | + XCTAssertEqual(duration.seconds, 15) |
| 72 | + XCTAssertEqual(duration.nanos, 123_000_000) |
| 73 | + |
| 74 | + XCTAssertEqual(duration.timeInterval, 15.123, accuracy: 1e-9) |
| 75 | + } |
| 76 | + |
| 77 | + func testDecodeProtoDuration_invalidSeconds() throws { |
| 78 | + guard let error = try expectDecodeFailure("invalid.123s") else { return } |
| 79 | + XCTAssertContains(error.debugDescription, "Invalid proto duration seconds") |
| 80 | + } |
| 81 | + |
| 82 | + func testDecodeProtoDuration_invalidNanoseconds() throws { |
| 83 | + guard let error = try expectDecodeFailure("123.invalid") else { return } |
| 84 | + XCTAssertContains(error.debugDescription, "Invalid proto duration nanoseconds") |
| 85 | + } |
| 86 | + |
| 87 | + func testDecodeProtoDuration_tooManyDecimals() throws { |
| 88 | + guard let error = try expectDecodeFailure("123.45.67") else { return } |
| 89 | + XCTAssertContains(error.debugDescription, "Invalid proto duration string") |
| 90 | + } |
| 91 | + |
| 92 | + func testDecodeProtoDuration_withoutSuffix() throws { |
| 93 | + let duration = try decodeProtoDuration("123.456") |
| 94 | + XCTAssertEqual(duration.seconds, 123) |
| 95 | + XCTAssertEqual(duration.nanos, 456_000_000) |
| 96 | + |
| 97 | + XCTAssertEqual(duration.timeInterval, 123.456, accuracy: 1e-9) |
| 98 | + } |
| 99 | +} |
0 commit comments