|
| 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 Combine |
| 17 | +import XCTest |
| 18 | + |
| 19 | +import FirebaseCore |
| 20 | +@testable import FirebaseDataConnect |
| 21 | + |
| 22 | +@available(iOS 15.0, macOS 12.0, tvOS 15.0, watchOS 8.0, *) |
| 23 | +final class ErrorTypesTests: XCTestCase { |
| 24 | + |
| 25 | + private var pubCancellable: AnyCancellable? |
| 26 | + private let errPublisher = PassthroughSubject<Result<String, AnyDataConnectError>, Never>() |
| 27 | + |
| 28 | + func throwInitError() throws { |
| 29 | + throw DataConnectInitError.appNotConfigured() |
| 30 | + } |
| 31 | + |
| 32 | + func testCatchAsGenericTypes() throws { |
| 33 | + do { |
| 34 | + try throwInitError() |
| 35 | + } catch let dcerr as DataConnectError { |
| 36 | + XCTAssertTrue(true) |
| 37 | + } |
| 38 | + } |
| 39 | + |
| 40 | + func testPublisherErrorType() throws { |
| 41 | + |
| 42 | + let errExpectation = XCTestExpectation(description: "Expect a Domain Erro") |
| 43 | + |
| 44 | + pubCancellable = errPublisher.sink( receiveValue: { result in |
| 45 | + switch result { |
| 46 | + case .success(_): |
| 47 | + XCTFail("Unexpectedly got success. We expect a failure with error") |
| 48 | + case let .failure(dcerror): |
| 49 | + if let initErr = dcerror.dataConnectError as? DataConnectInitError, |
| 50 | + initErr.code == .appNotConfigured |
| 51 | + { |
| 52 | + // got Init domain error |
| 53 | + errExpectation.fulfill() |
| 54 | + } else { |
| 55 | + XCTFail("Did not get DataConnectInitError.appNotConfigured as expected") |
| 56 | + } |
| 57 | + } |
| 58 | + }) |
| 59 | + |
| 60 | + errPublisher |
| 61 | + .send( |
| 62 | + .failure(AnyDataConnectError(dataConnectError: DataConnectInitError.appNotConfigured())) |
| 63 | + ) |
| 64 | + |
| 65 | + wait(for: [errExpectation], timeout: 1.0) |
| 66 | + } |
| 67 | +} |
0 commit comments