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

93 lines
2.8 KiB
Swift

// 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: Mocked>() -> R { R.mockValue }
func any<R: MockedGeneric>(type: R.Generic.Type) -> R { R.mockValue(type: type) }
func any<R: MockedDoubleGeneric>(typeA: R.GenericA.Type, typeB: R.GenericB.Type) -> R {
R.mockValue(typeA: typeA, typeB: typeB)
}
func any<R: FixedWidthInteger>() -> R { unsafeBitCast(0, to: R.self) }
func any<K: Hashable, V>() -> [K: V] { [:] }
func any() -> Float { 0 }
func any() -> Double { 0 }
func any() -> String { "" }
func any() -> Data { Data() }
func any() -> Bool { false }
func any() -> SessionId { SessionId.invalid }
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
func any() -> TestDependencies {
TestDependencies { dependencies in
dependencies.dateNow = Date(timeIntervalSince1970: 1234567890)
dependencies.forceSynchronous = true
}
}
func anyAny() -> Any { 0 } // Unique name for compilation performance reasons
func anyArray<R>() -> [R] { [] } // Unique name for compilation performance reasons
func anySet<R>() -> Set<R> { 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<Generic> {
return HTTP.BatchSubResponse(
code: 200,
headers: [:],
body: Generic.mockValue,
failedToParseBody: false
)
}
}
extension HTTP.BatchSubResponse {
static func mockArrayValue<M: Mocked>(type: M.Type) -> HTTP.BatchSubResponse<Array<M>> {
return HTTP.BatchSubResponse(
code: 200,
headers: [:],
body: [M.mockValue],
failedToParseBody: false
)
}
}
// 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)
}
}