// Copyright © 2023 Rangeproof Pty Ltd. All rights reserved. import Foundation import GRDB import SessionUtilitiesKit // MARK: - Mocked protocol Mocked { static var mockValue: Self { get } } protocol MockedGeneric { associatedtype Generic static func mockValue(type: Generic.Type) -> Self } protocol MockedDoubleGeneric { associatedtype GenericA associatedtype GenericB static func mockValue(typeA: GenericA.Type, typeB: GenericB.Type) -> Self } // MARK: - DSL func any() -> R { R.mockValue } func any(type: R.Generic.Type) -> R { R.mockValue(type: type) } func any(typeA: R.GenericA.Type, typeB: R.GenericB.Type) -> R { R.mockValue(typeA: typeA, typeB: typeB) } func any() -> R { unsafeBitCast(0, to: R.self) } func any() -> [K: V] { [:] } func any() -> Float { 0 } func any() -> Double { 0 } func any() -> String { "" } func any() -> Data { Data() } func any() -> Bool { false } func any() -> Dependencies { Dependencies( storage: SynchronousStorage(customWriter: try! DatabaseQueue()), network: MockNetwork(), crypto: MockCrypto(), standardUserDefaults: MockUserDefaults(), caches: MockCaches(), jobRunner: MockJobRunner(), scheduler: .immediate, dateNow: Date(timeIntervalSince1970: 1234567890), fixedTime: 0, forceSynchronous: true ) } func anyAny() -> Any { 0 } // Unique name for compilation performance reasons func anyArray() -> [R] { [] } // Unique name for compilation performance reasons func anySet() -> Set { Set() } // Unique name for compilation performance reasons // MARK: - Extensions extension HTTP.BatchSubResponse: MockedGeneric where T: Mocked { typealias Generic = T static func mockValue(type: Generic.Type) -> HTTP.BatchSubResponse { return HTTP.BatchSubResponse( code: 200, headers: [:], body: Generic.mockValue, failedToParseBody: false ) } } extension HTTP.BatchSubResponse { static func mockArrayValue(type: M.Type) -> HTTP.BatchSubResponse> { return HTTP.BatchSubResponse( code: 200, headers: [:], body: [M.mockValue], failedToParseBody: false ) } } // MARK: - Encodable Convenience extension Mocked where Self: Encodable { func encoded() -> Data { try! JSONEncoder().with(outputFormatting: .sortedKeys).encode(self) } } extension MockedGeneric where Self: Encodable { func encoded() -> Data { try! JSONEncoder().with(outputFormatting: .sortedKeys).encode(self) } } extension Array where Element: Encodable { func encoded() -> Data { try! JSONEncoder().with(outputFormatting: .sortedKeys).encode(self) } }