Introduce lambdaError instead of using TODO, to handle error when a lambda is invoked and it should not.
This commit is contained in:
@@ -18,9 +18,10 @@ package io.element.android.libraries.matrix.test.permalink
|
||||
|
||||
import io.element.android.libraries.matrix.api.permalink.PermalinkData
|
||||
import io.element.android.libraries.matrix.api.permalink.PermalinkParser
|
||||
import io.element.android.tests.testutils.lambda.lambdaError
|
||||
|
||||
class FakePermalinkParser(
|
||||
private var result: () -> PermalinkData = { TODO("Not implemented") }
|
||||
private var result: () -> PermalinkData = { lambdaError() }
|
||||
) : PermalinkParser {
|
||||
fun givenResult(result: PermalinkData) {
|
||||
this.result = { result }
|
||||
|
||||
@@ -19,10 +19,11 @@ package io.element.android.libraries.matrix.test.pushers
|
||||
import io.element.android.libraries.matrix.api.pusher.PushersService
|
||||
import io.element.android.libraries.matrix.api.pusher.SetHttpPusherData
|
||||
import io.element.android.libraries.matrix.api.pusher.UnsetHttpPusherData
|
||||
import io.element.android.tests.testutils.lambda.lambdaError
|
||||
|
||||
class FakePushersService(
|
||||
private val setHttpPusherResult: (SetHttpPusherData) -> Result<Unit> = { TODO() },
|
||||
private val unsetHttpPusherResult: (UnsetHttpPusherData) -> Result<Unit> = { TODO() },
|
||||
private val setHttpPusherResult: (SetHttpPusherData) -> Result<Unit> = { lambdaError() },
|
||||
private val unsetHttpPusherResult: (UnsetHttpPusherData) -> Result<Unit> = { lambdaError() },
|
||||
) : PushersService {
|
||||
override suspend fun setHttpPusher(setHttpPusherData: SetHttpPusherData) = setHttpPusherResult(setHttpPusherData)
|
||||
override suspend fun unsetHttpPusher(unsetHttpPusherData: UnsetHttpPusherData): Result<Unit> = unsetHttpPusherResult(unsetHttpPusherData)
|
||||
|
||||
@@ -20,9 +20,10 @@ import io.element.android.libraries.matrix.api.core.EventId
|
||||
import io.element.android.libraries.matrix.api.core.RoomId
|
||||
import io.element.android.libraries.matrix.api.core.SessionId
|
||||
import io.element.android.libraries.push.impl.notifications.model.NotifiableEvent
|
||||
import io.element.android.tests.testutils.lambda.lambdaError
|
||||
|
||||
class FakeNotifiableEventResolver(
|
||||
private val notifiableEventResult: (SessionId, RoomId, EventId) -> NotifiableEvent? = { _, _, _ -> TODO() }
|
||||
private val notifiableEventResult: (SessionId, RoomId, EventId) -> NotifiableEvent? = { _, _, _ -> lambdaError() }
|
||||
) : NotifiableEventResolver {
|
||||
override suspend fun resolveEvent(sessionId: SessionId, roomId: RoomId, eventId: EventId): NotifiableEvent? {
|
||||
return notifiableEventResult(sessionId, roomId, eventId)
|
||||
|
||||
@@ -41,6 +41,7 @@ import io.element.android.libraries.pushstore.api.clientsecret.PushClientSecret
|
||||
import io.element.android.libraries.pushstore.test.userpushstore.FakeUserPushStore
|
||||
import io.element.android.libraries.pushstore.test.userpushstore.FakeUserPushStoreFactory
|
||||
import io.element.android.libraries.pushstore.test.userpushstore.clientsecret.FakePushClientSecret
|
||||
import io.element.android.tests.testutils.lambda.lambdaError
|
||||
import io.element.android.tests.testutils.lambda.lambdaRecorder
|
||||
import io.element.android.tests.testutils.lambda.value
|
||||
import kotlinx.coroutines.ExperimentalCoroutinesApi
|
||||
@@ -240,9 +241,9 @@ class DefaultPushHandlerTest {
|
||||
}
|
||||
|
||||
private fun createDefaultPushHandler(
|
||||
onNotifiableEventReceived: (NotifiableEvent) -> Unit = { TODO() },
|
||||
notifiableEventResult: (SessionId, RoomId, EventId) -> NotifiableEvent? = { _, _, _ -> TODO() },
|
||||
incrementPushCounterResult: () -> Unit = { TODO() },
|
||||
onNotifiableEventReceived: (NotifiableEvent) -> Unit = { lambdaError() },
|
||||
notifiableEventResult: (SessionId, RoomId, EventId) -> NotifiableEvent? = { _, _, _ -> lambdaError() },
|
||||
incrementPushCounterResult: () -> Unit = { lambdaError() },
|
||||
userPushStore: UserPushStore = FakeUserPushStore(),
|
||||
pushClientSecret: PushClientSecret = FakePushClientSecret(),
|
||||
buildMeta: BuildMeta = aBuildMeta(),
|
||||
|
||||
@@ -17,9 +17,10 @@
|
||||
package io.element.android.libraries.push.impl.test
|
||||
|
||||
import io.element.android.libraries.push.impl.pushgateway.PushGatewayNotifyRequest
|
||||
import io.element.android.tests.testutils.lambda.lambdaError
|
||||
|
||||
class FakePushGatewayNotifyRequest(
|
||||
private val executeResult: (PushGatewayNotifyRequest.Params) -> Unit = { TODO() }
|
||||
private val executeResult: (PushGatewayNotifyRequest.Params) -> Unit = { lambdaError() }
|
||||
) : PushGatewayNotifyRequest {
|
||||
override suspend fun execute(params: PushGatewayNotifyRequest.Params) {
|
||||
executeResult(params)
|
||||
|
||||
@@ -17,9 +17,10 @@
|
||||
package io.element.android.libraries.push.impl.test
|
||||
|
||||
import io.element.android.libraries.pushproviders.api.CurrentUserPushConfig
|
||||
import io.element.android.tests.testutils.lambda.lambdaError
|
||||
|
||||
class FakeTestPush(
|
||||
private val executeResult: (CurrentUserPushConfig) -> Unit = { TODO() }
|
||||
private val executeResult: (CurrentUserPushConfig) -> Unit = { lambdaError() }
|
||||
) : TestPush {
|
||||
override suspend fun execute(config: CurrentUserPushConfig) {
|
||||
executeResult(config)
|
||||
|
||||
@@ -18,10 +18,11 @@ package io.element.android.libraries.push.test
|
||||
|
||||
import io.element.android.libraries.matrix.api.MatrixClient
|
||||
import io.element.android.libraries.pushproviders.api.PusherSubscriber
|
||||
import io.element.android.tests.testutils.lambda.lambdaError
|
||||
|
||||
class FakePusherSubscriber(
|
||||
private val registerPusherResult: (MatrixClient, String, String) -> Result<Unit> = { _, _, _ -> TODO() },
|
||||
private val unregisterPusherResult: (MatrixClient, String, String) -> Result<Unit> = { _, _, _ -> TODO() },
|
||||
private val registerPusherResult: (MatrixClient, String, String) -> Result<Unit> = { _, _, _ -> lambdaError() },
|
||||
private val unregisterPusherResult: (MatrixClient, String, String) -> Result<Unit> = { _, _, _ -> lambdaError() },
|
||||
) : PusherSubscriber {
|
||||
override suspend fun registerPusher(matrixClient: MatrixClient, pushKey: String, gateway: String): Result<Unit> {
|
||||
return registerPusherResult(matrixClient, pushKey, gateway)
|
||||
|
||||
@@ -18,9 +18,10 @@ package io.element.android.libraries.push.test.test
|
||||
|
||||
import io.element.android.libraries.pushproviders.api.PushData
|
||||
import io.element.android.libraries.pushproviders.api.PushHandler
|
||||
import io.element.android.tests.testutils.lambda.lambdaError
|
||||
|
||||
class FakePushHandler(
|
||||
private val handleResult: (PushData) -> Unit = { TODO() }
|
||||
private val handleResult: (PushData) -> Unit = { lambdaError() }
|
||||
) : PushHandler {
|
||||
override suspend fun handle(pushData: PushData) {
|
||||
handleResult(pushData)
|
||||
|
||||
@@ -16,8 +16,10 @@
|
||||
|
||||
package io.element.android.libraries.pushproviders.firebase
|
||||
|
||||
import io.element.android.tests.testutils.lambda.lambdaError
|
||||
|
||||
class FakeFirebaseNewTokenHandler(
|
||||
private val handleResult: (String) -> Unit = { TODO() }
|
||||
private val handleResult: (String) -> Unit = { lambdaError() }
|
||||
) : FirebaseNewTokenHandler {
|
||||
override suspend fun handle(firebaseToken: String) {
|
||||
handleResult(firebaseToken)
|
||||
|
||||
@@ -24,4 +24,5 @@ android {
|
||||
dependencies {
|
||||
implementation(projects.libraries.matrix.api)
|
||||
implementation(projects.libraries.pushproviders.api)
|
||||
implementation(projects.tests.testutils)
|
||||
}
|
||||
|
||||
@@ -27,8 +27,8 @@ class FakePushProvider(
|
||||
private val isAvailable: Boolean = true,
|
||||
private val distributors: List<Distributor> = listOf(Distributor("aDistributorValue", "aDistributorName")),
|
||||
private val currentUserPushConfig: CurrentUserPushConfig? = null,
|
||||
private val registerWithResult: (MatrixClient, Distributor) -> Result<Unit> = { _, _ -> TODO() },
|
||||
private val unregisterWithResult: (MatrixClient) -> Result<Unit> = { TODO() },
|
||||
private val registerWithResult: (MatrixClient, Distributor) -> Result<Unit> = { _, _ -> lambdaError() },
|
||||
private val unregisterWithResult: (MatrixClient) -> Result<Unit> = { lambdaError() },
|
||||
) : PushProvider {
|
||||
override fun isAvailable(): Boolean = isAvailable
|
||||
|
||||
|
||||
@@ -17,9 +17,10 @@
|
||||
package io.element.android.libraries.pushproviders.unifiedpush
|
||||
|
||||
import io.element.android.libraries.pushproviders.api.Distributor
|
||||
import io.element.android.tests.testutils.lambda.lambdaError
|
||||
|
||||
class FakeRegisterUnifiedPushUseCase(
|
||||
private val result: (Distributor, String) -> Result<Unit> = { _, _ -> TODO("Not yet implemented") }
|
||||
private val result: (Distributor, String) -> Result<Unit> = { _, _ -> lambdaError() }
|
||||
) : RegisterUnifiedPushUseCase {
|
||||
override suspend fun execute(distributor: Distributor, clientSecret: String): Result<Unit> {
|
||||
return result(distributor, clientSecret)
|
||||
|
||||
@@ -16,8 +16,10 @@
|
||||
|
||||
package io.element.android.libraries.pushproviders.unifiedpush
|
||||
|
||||
import io.element.android.tests.testutils.lambda.lambdaError
|
||||
|
||||
class FakeUnifiedPushGatewayResolver(
|
||||
private val getGatewayResult: (String) -> String = { TODO() },
|
||||
private val getGatewayResult: (String) -> String = { lambdaError() },
|
||||
) : UnifiedPushGatewayResolver {
|
||||
override suspend fun getGateway(endpoint: String): String {
|
||||
return getGatewayResult(endpoint)
|
||||
|
||||
@@ -16,8 +16,10 @@
|
||||
|
||||
package io.element.android.libraries.pushproviders.unifiedpush
|
||||
|
||||
import io.element.android.tests.testutils.lambda.lambdaError
|
||||
|
||||
class FakeUnifiedPushNewGatewayHandler(
|
||||
private val handleResult: suspend (String, String, String) -> Result<Unit> = { _, _, _ -> TODO() },
|
||||
private val handleResult: suspend (String, String, String) -> Result<Unit> = { _, _, _ -> lambdaError() },
|
||||
) : UnifiedPushNewGatewayHandler {
|
||||
override suspend fun handle(endpoint: String, pushGateway: String, clientSecret: String): Result<Unit> {
|
||||
return handleResult(endpoint, pushGateway, clientSecret)
|
||||
|
||||
@@ -17,14 +17,15 @@
|
||||
package io.element.android.libraries.pushproviders.unifiedpush
|
||||
|
||||
import io.element.android.libraries.matrix.api.core.UserId
|
||||
import io.element.android.tests.testutils.lambda.lambdaError
|
||||
|
||||
class FakeUnifiedPushStore(
|
||||
private val getEndpointResult: (String) -> String? = { TODO() },
|
||||
private val storeUpEndpointResult: (String, String?) -> Unit = { _, _ -> TODO() },
|
||||
private val getPushGatewayResult: (String) -> String? = { TODO() },
|
||||
private val storePushGatewayResult: (String, String?) -> Unit = { _, _ -> TODO() },
|
||||
private val getDistributorValueResult: (UserId) -> String? = { TODO() },
|
||||
private val setDistributorValueResult: (UserId, String) -> Unit = { _, _ -> TODO() },
|
||||
private val getEndpointResult: (String) -> String? = { lambdaError() },
|
||||
private val storeUpEndpointResult: (String, String?) -> Unit = { _, _ -> lambdaError() },
|
||||
private val getPushGatewayResult: (String) -> String? = { lambdaError() },
|
||||
private val storePushGatewayResult: (String, String?) -> Unit = { _, _ -> lambdaError() },
|
||||
private val getDistributorValueResult: (UserId) -> String? = { lambdaError() },
|
||||
private val setDistributorValueResult: (UserId, String) -> Unit = { _, _ -> lambdaError() },
|
||||
) : UnifiedPushStore {
|
||||
override fun getEndpoint(clientSecret: String): String? {
|
||||
return getEndpointResult(clientSecret)
|
||||
|
||||
@@ -17,9 +17,10 @@
|
||||
package io.element.android.libraries.pushproviders.unifiedpush
|
||||
|
||||
import io.element.android.libraries.matrix.api.MatrixClient
|
||||
import io.element.android.tests.testutils.lambda.lambdaError
|
||||
|
||||
class FakeUnregisterUnifiedPushUseCase(
|
||||
private val result: (MatrixClient, String) -> Result<Unit> = { _, _ -> TODO("Not yet implemented") }
|
||||
private val result: (MatrixClient, String) -> Result<Unit> = { _, _ -> lambdaError() }
|
||||
) : UnregisterUnifiedPushUseCase {
|
||||
override suspend fun execute(matrixClient: MatrixClient, clientSecret: String): Result<Unit> {
|
||||
return result(matrixClient, clientSecret)
|
||||
|
||||
@@ -18,10 +18,11 @@ package io.element.android.libraries.pushstore.test.userpushstore.clientsecret
|
||||
|
||||
import io.element.android.libraries.matrix.api.core.SessionId
|
||||
import io.element.android.libraries.pushstore.api.clientsecret.PushClientSecret
|
||||
import io.element.android.tests.testutils.lambda.lambdaError
|
||||
|
||||
class FakePushClientSecret(
|
||||
private val getSecretForUserResult: (SessionId) -> String = { TODO() },
|
||||
private val getUserIdFromSecretResult: (String) -> SessionId? = { TODO() }
|
||||
private val getSecretForUserResult: (SessionId) -> String = { lambdaError() },
|
||||
private val getUserIdFromSecretResult: (String) -> SessionId? = { lambdaError() }
|
||||
) : PushClientSecret {
|
||||
override suspend fun getSecretForUser(userId: SessionId): String {
|
||||
return getSecretForUserResult(userId)
|
||||
|
||||
@@ -164,7 +164,7 @@ internal fun MentionSpanPreview() {
|
||||
eventId = null,
|
||||
viaParameters = persistentListOf(),
|
||||
)
|
||||
else -> TODO()
|
||||
else -> throw AssertionError("Unexpected value $uriString")
|
||||
}
|
||||
}
|
||||
},
|
||||
|
||||
@@ -16,26 +16,28 @@
|
||||
|
||||
package io.element.android.tests.testutils
|
||||
|
||||
import io.element.android.tests.testutils.lambda.lambdaError
|
||||
|
||||
class EnsureNeverCalled : () -> Unit {
|
||||
override fun invoke() {
|
||||
throw AssertionError("Should not be called")
|
||||
lambdaError()
|
||||
}
|
||||
}
|
||||
|
||||
class EnsureNeverCalledWithParam<T> : (T) -> Unit {
|
||||
override fun invoke(p1: T) {
|
||||
throw AssertionError("Should not be called and is called with $p1")
|
||||
lambdaError("Should not be called and is called with $p1")
|
||||
}
|
||||
}
|
||||
|
||||
class EnsureNeverCalledWithParamAndResult<T, R> : (T) -> R {
|
||||
override fun invoke(p1: T): R {
|
||||
throw AssertionError("Should not be called and is called with $p1")
|
||||
lambdaError("Should not be called and is called with $p1")
|
||||
}
|
||||
}
|
||||
|
||||
class EnsureNeverCalledWithTwoParams<T, U> : (T, U) -> Unit {
|
||||
override fun invoke(p1: T, p2: U) {
|
||||
throw AssertionError("Should not be called and is called with $p1 and $p2")
|
||||
lambdaError("Should not be called and is called with $p1 and $p2")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,23 @@
|
||||
/*
|
||||
* Copyright (c) 2024 New Vector Ltd
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package io.element.android.tests.testutils.lambda
|
||||
|
||||
fun lambdaError(
|
||||
message: String = "This lambda should never be called."
|
||||
): Nothing {
|
||||
throw AssertionError(message)
|
||||
}
|
||||
@@ -26,7 +26,7 @@ abstract class LambdaRecorder internal constructor(
|
||||
|
||||
internal fun onInvoke(vararg params: Any?) {
|
||||
if (assertNoInvocation) {
|
||||
throw AssertionError("This lambda should never be called.")
|
||||
lambdaError()
|
||||
}
|
||||
parametersSequence.add(params.toList())
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user