You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
session-ios/_SharedTestUtilities/Mocked.swift

165 lines
4.8 KiB
Swift

// Copyright © 2023 Rangeproof Pty Ltd. All rights reserved.
import Foundation
import UIKit.UIApplication
import Combine
import GRDB
import SessionUtilitiesKit
// MARK: - Mocked
protocol Mocked { static var mock: Self { get } }
protocol MockedGeneric {
associatedtype Generic
static func mock(type: Generic.Type) -> Self
}
protocol MockedDoubleGeneric {
associatedtype GenericA
associatedtype GenericB
static func mock(typeA: GenericA.Type, typeB: GenericB.Type) -> Self
}
// MARK: - DSL
/// Needs to be a function as you can't extend 'Any'
func anyAny() -> Any { 0 }
extension Mocked { static var any: Self { mock } }
extension Int: Mocked { static var mock: Int { 0 } }
extension Dictionary: Mocked { static var mock: Self { [:] } }
extension Float: Mocked { static var mock: Float { 0 } }
extension Double: Mocked { static var mock: Double { 0 } }
extension String: Mocked { static var mock: String { "" } }
extension Data: Mocked { static var mock: Data { Data() } }
extension Bool: Mocked { static var mock: Bool { false } }
// The below types either can't be mocked or use the 'MockedGeneric' or 'MockedDoubleGeneric' types
// so need their own direct 'any' values
extension Array { static var any: Self { mock(type: Element.self) } }
extension Dictionary { static var any: Self { mock(typeA: Key.self, typeB: Value.self) } }
extension Error { static var any: Error { TestError.mock } }
extension UIApplication.State { static var any: UIApplication.State { .active } }
extension TimeInterval { static var any: TimeInterval { 0 } }
extension SessionId { static var any: SessionId { SessionId.invalid } }
extension Dependencies {
static var any: Dependencies {
TestDependencies { dependencies in
dependencies.dateNow = Date(timeIntervalSince1970: 1234567890)
dependencies.forceSynchronous = true
}
Merge remote-tracking branch 'upstream/dev' into feature/groups-rebuild # Conflicts: # Session.xcodeproj/project.pbxproj # Session/Conversations/Settings/ThreadSettingsViewModel.swift # Session/Meta/Translations/de.lproj/Localizable.strings # Session/Meta/Translations/en.lproj/Localizable.strings # Session/Meta/Translations/es-ES.lproj/Localizable.strings # Session/Meta/Translations/fa.lproj/Localizable.strings # Session/Meta/Translations/fi.lproj/Localizable.strings # Session/Meta/Translations/fil.lproj/Localizable.strings # Session/Meta/Translations/fr.lproj/Localizable.strings # Session/Meta/Translations/hi.lproj/Localizable.strings # Session/Meta/Translations/hr.lproj/Localizable.strings # Session/Meta/Translations/it.lproj/Localizable.strings # Session/Meta/Translations/ja.lproj/Localizable.strings # Session/Meta/Translations/nl.lproj/Localizable.strings # Session/Meta/Translations/pl.lproj/Localizable.strings # Session/Meta/Translations/pt-BR.lproj/Localizable.strings # Session/Meta/Translations/ru.lproj/Localizable.strings # Session/Meta/Translations/sk.lproj/Localizable.strings # Session/Meta/Translations/sl.lproj/Localizable.strings # Session/Meta/Translations/sv-SE.lproj/Localizable.strings # Session/Meta/Translations/th.lproj/Localizable.strings # Session/Meta/Translations/vi.lproj/Localizable.strings # Session/Meta/Translations/zh-CN.lproj/Localizable.strings # Session/Meta/Translations/zh-TW.lproj/Localizable.strings # SessionMessagingKit/Calls/WebRTCSession.swift # SessionMessagingKit/Configuration.swift # SessionMessagingKit/Database/Migrations/_003_YDBToGRDBMigration.swift # SessionMessagingKit/Sending & Receiving/Message Handling/MessageReceiver+VisibleMessages.swift # SessionMessagingKit/SessionUtil/Config Handling/SessionUtil+Contacts.swift # SessionMessagingKit/Utilities/ProfileManager.swift # SessionMessagingKitTests/Jobs/Types/MessageSendJobSpec.swift # SessionMessagingKitTests/LibSessionUtil/LibSessionSpec.swift # SessionMessagingKitTests/LibSessionUtil/SessionUtilSpec.swift # SessionMessagingKitTests/Open Groups/Models/BatchRequestInfoSpec.swift # SessionMessagingKitTests/Open Groups/Models/SOGSMessageSpec.swift # SessionMessagingKitTests/Open Groups/OpenGroupAPISpec.swift # SessionMessagingKitTests/Open Groups/OpenGroupManagerSpec.swift # SessionMessagingKitTests/Open Groups/Types/SOGSEndpointSpec.swift # SessionMessagingKitTests/Sending & Receiving/MessageReceiverDecryptionSpec.swift # SessionMessagingKitTests/Sending & Receiving/MessageSenderEncryptionSpec.swift # SessionMessagingKitTests/Shared Models/SessionThreadViewModelSpec.swift # SessionMessagingKitTests/Utilities/CryptoSMKSpec.swift # SessionTests/Conversations/Settings/ThreadDisappearingMessagesViewModelSpec.swift # SessionTests/Conversations/Settings/ThreadSettingsViewModelSpec.swift # SessionTests/Settings/NotificationContentViewModelSpec.swift # SessionUtilitiesKitTests/Database/Models/IdentitySpec.swift # SessionUtilitiesKitTests/Database/Utilities/PersistableRecordUtilitiesSpec.swift # SessionUtilitiesKitTests/General/DependenciesSpec.swift # SessionUtilitiesKitTests/JobRunner/JobRunnerSpec.swift # _SharedTestUtilities/MockCaches.swift
2 years ago
}
}
// MARK: - Conformance
extension Database: Mocked {
static var mock: Database {
var result: Database!
try! DatabaseQueue().read { result = $0 }
return result!
}
}
extension URLRequest: Mocked {
static var mock: URLRequest = URLRequest(url: URL(fileURLWithPath: "mock"))
}
extension Array: MockedGeneric {
typealias Generic = Element
static func mock(type: Element.Type) -> [Element] { return [] }
}
extension Dictionary: MockedDoubleGeneric {
typealias GenericA = Key
typealias GenericB = Value
static func mock(typeA: Key.Type, typeB: Value.Type) -> [Key: Value] { return [:] }
}
extension AnyPublisher: MockedGeneric where Failure == Error {
typealias Generic = Output
static func any(type: Output.Type) -> AnyPublisher<Output, Error> { mock(type: type) }
static func mock(type: Output.Type) -> AnyPublisher<Output, Error> {
return Fail(error: MockError.mockedData).eraseToAnyPublisher()
}
}
extension HTTP.BatchSubResponse: MockedGeneric where T: Mocked {
typealias Generic = T
static func mock(type: Generic.Type) -> HTTP.BatchSubResponse<Generic> {
return HTTP.BatchSubResponse(
code: 200,
headers: [:],
body: Generic.mock,
failedToParseBody: false
)
}
}
extension HTTP.BatchSubResponse {
static func mockArray<M: Mocked>(type: M.Type) -> HTTP.BatchSubResponse<Array<M>> {
return HTTP.BatchSubResponse(
code: 200,
headers: [:],
body: [M.mock],
failedToParseBody: false
)
}
}
extension KeyPair: Mocked {
static var mock: KeyPair = KeyPair(
publicKey: Data(hex: TestConstants.publicKey).bytes,
secretKey: Data(hex: TestConstants.edSecretKey).bytes
)
}
extension Job: Mocked {
static var mock: Job = Job(variant: .mock)
}
extension Job.Variant: Mocked {
static var mock: Job.Variant = .messageSend
}
extension Network.RequestType: MockedGeneric {
typealias Generic = T
static func mock(type: T.Type) -> Network.RequestType<T> {
return Network.RequestType(id: "mock") { Fail(error: MockError.mockedData).eraseToAnyPublisher() }
}
}
extension NoResponse: Mocked {
static var mock: NoResponse = NoResponse()
}
// MARK: - Encodable Convenience
extension Mocked where Self: Encodable {
func encoded(using dependencies: Dependencies) -> Data {
try! JSONEncoder(using: dependencies).with(outputFormatting: .sortedKeys).encode(self)
}
}
extension MockedGeneric where Self: Encodable {
func encoded(using dependencies: Dependencies) -> Data {
try! JSONEncoder(using: dependencies).with(outputFormatting: .sortedKeys).encode(self)
}
}
extension Array where Element: Encodable {
func encoded(using dependencies: Dependencies) -> Data {
try! JSONEncoder(using: dependencies).with(outputFormatting: .sortedKeys).encode(self)
}
}