From c96d1ae0e6e83167b3dd9cdfae322ca43db99c8e Mon Sep 17 00:00:00 2001 From: Chalard Jean Date: Sun, 27 Apr 2025 11:01:10 +0900 Subject: [PATCH] Move failWithErrorReason to a common place MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This is a shared public utility. Its main utility is for methods that can fail, have an error message of their own, and also want to take an error message from a caller and want to combine them. Example useĀ : fun assertEquals(expected: T, actual: T, msg: String? = null) { if (expected != null) { failWithErrorReason(msg, "Expected $expected, got $actual") } } ...which allows someone to say assertEquals(a, b, "Checking a vs b") Test: TH Flag: EXEMPT test-only change Change-Id: Id3eaa8384b4177f38f13576d9b4d742c52027ae4 --- .../com/android/testutils/TestableNetworkCallback.kt | 8 +------- .../android/testutils/TestableNetworkOfferCallback.kt | 6 ------ .../hostdevice/com/android/testutils/MiscAsserts.kt | 10 +++++++++- 3 files changed, 10 insertions(+), 14 deletions(-) diff --git a/staticlibs/testutils/devicetests/com/android/testutils/TestableNetworkCallback.kt b/staticlibs/testutils/devicetests/com/android/testutils/TestableNetworkCallback.kt index d9c51e5201..c4e236e1d0 100644 --- a/staticlibs/testutils/devicetests/com/android/testutils/TestableNetworkCallback.kt +++ b/staticlibs/testutils/devicetests/com/android/testutils/TestableNetworkCallback.kt @@ -70,7 +70,7 @@ open class RecorderCallback private constructor( data class Reserved private constructor( override val network: Network, val caps: NetworkCapabilities - ): CallbackEntry() { + ) : CallbackEntry() { constructor(caps: NetworkCapabilities) : this(NULL_NETWORK, caps) } data class Available(override val network: Network) : CallbackEntry() @@ -399,12 +399,6 @@ open class TestableNetworkCallback private constructor( } } as T - // "Nothing" is the return type to declare a function never returns a value. - fun failWithErrorReason(errorMsg: String?, errorReason: String): Nothing { - val message = if (errorMsg != null) "$errorMsg : $errorReason" else errorReason - fail(message) - } - inline fun expect( network: HasNetwork, timeoutMs: Long = defaultTimeoutMs, diff --git a/staticlibs/testutils/devicetests/com/android/testutils/TestableNetworkOfferCallback.kt b/staticlibs/testutils/devicetests/com/android/testutils/TestableNetworkOfferCallback.kt index 8f9014c816..8228440f54 100644 --- a/staticlibs/testutils/devicetests/com/android/testutils/TestableNetworkOfferCallback.kt +++ b/staticlibs/testutils/devicetests/com/android/testutils/TestableNetworkOfferCallback.kt @@ -56,12 +56,6 @@ class TestableNetworkOfferCallback( history.add(CallbackEntry.Unneeded(request)) } - // TODO : move to some utility, this is copied from TestableNetworkCallback - fun failWithErrorReason(errorMsg: String?, errorReason: String): Nothing { - val message = if (errorMsg != null) "$errorMsg : $errorReason" else errorReason - fail(message) - } - inline fun expect( errorMsg: String? = null, predicate: (T) -> Boolean = { true } diff --git a/staticlibs/testutils/hostdevice/com/android/testutils/MiscAsserts.kt b/staticlibs/testutils/hostdevice/com/android/testutils/MiscAsserts.kt index 176546a4c0..de797b0ba4 100644 --- a/staticlibs/testutils/hostdevice/com/android/testutils/MiscAsserts.kt +++ b/staticlibs/testutils/hostdevice/com/android/testutils/MiscAsserts.kt @@ -142,4 +142,12 @@ fun assertEventuallyTrue( } Thread.sleep(pollIntervalMs) } -} \ No newline at end of file +} + +// "Nothing" is the return type to declare a function never returns a value. This is useful +// because the compiler will know the branch of the code calling this does not return, which +// lets check exhaustivity, infer return types, nullability and smart casts. +fun failWithErrorReason(errorMsg: String?, errorReason: String): Nothing { + val message = if (errorMsg != null) "$errorMsg : $errorReason" else errorReason + fail(message) +}