MutableStubKt

Added in 1.0.0-alpha01

public final class MutableStubKt


Summary

Public methods

static final @NonNull MutableStub<Object, @NonNull R>
<R extends Object> MutableStub(@NonNull Throwable defaultError)

Creates a new MutableStub that throws an exception if the queue is empty.

static final @NonNull MutableStub<Object, @NonNull R>
<R extends Object> MutableStub(R defaultResponse)

Creates a new MutableStub with a default return value used when the queue is empty.

static final @NonNull Stub<@NonNull T, @NonNull R>
<T extends Object, R extends Object> buildStub(
    @ExtensionFunctionType @NonNull Function1<@NonNull MutableStub<@NonNull T, @NonNull R>, Unit> builder
)

Builder to create MutableStubs.

static final void
<R extends Object> enqueue(
    @NonNull MutableStub<@NonNull ?, @NonNull R> receiver,
    @NonNull Iterable<@NonNull R> values
)

Adds a new item to the MutableStub's queue.

static final void
<R extends Object> enqueue(
    @NonNull MutableStub<@NonNull ?, @NonNull R> receiver,
    @NonNull R values
)

Adds a new item to the MutableStub's queue.

static final void
<R extends Object> plusAssign(
    @NonNull MutableStub<@NonNull ?, @NonNull R> receiver,
    @NonNull R value
)

Adds a new item to the MutableStub's queue.

Public methods

public static final @NonNull MutableStub<Object, @NonNull R> <R extends Object> MutableStub(@NonNull Throwable defaultError)

Creates a new MutableStub that throws an exception if the queue is empty.

Consider passing a default handler instead, so the exception is not created until necessary:

MutableStub { throw Exception() }
Parameters
@NonNull Throwable defaultError

The exception to throw if the queue is empty.

public static final @NonNull MutableStub<Object, @NonNull R> <R extends Object> MutableStub(R defaultResponse)

Creates a new MutableStub with a default return value used when the queue is empty.

Parameters
R defaultResponse

The default return value

public static final @NonNull Stub<@NonNull T, @NonNull R> <T extends Object, R extends Object> buildStub(
    @ExtensionFunctionType @NonNull Function1<@NonNull MutableStub<@NonNull T, @NonNull R>, Unit> builder
)

Builder to create MutableStubs.

import androidx.health.connect.client.testing.stubs.MutableStub
import androidx.health.connect.client.testing.stubs.buildStub
import androidx.health.connect.client.testing.stubs.enqueue

val builderMutableStub =
    buildStub<Int, String> {
        enqueue("first")
        defaultHandler = { "Default" }
    }
builderMutableStub.next(request = 0) // Returns "first"
builderMutableStub.next(request = 0) // Returns "Default"
public static final void <R extends Object> enqueue(
    @NonNull MutableStub<@NonNull ?, @NonNull R> receiver,
    @NonNull Iterable<@NonNull R> values
)

Adds a new item to the MutableStub's queue.

import androidx.health.connect.client.testing.stubs.MutableStub
import androidx.health.connect.client.testing.stubs.buildStub
import androidx.health.connect.client.testing.stubs.enqueue

val builderMutableStub =
    buildStub<Int, String> {
        enqueue("first")
        defaultHandler = { "Default" }
    }
builderMutableStub.next(request = 0) // Returns "first"
builderMutableStub.next(request = 0) // Returns "Default"
import androidx.health.connect.client.testing.stubs.MutableStub
import androidx.health.connect.client.testing.stubs.enqueue

val mutableStub = MutableStub<Int, String>(defaultHandler = { it.toString() })
mutableStub.enqueue("zero")
mutableStub += "one" // += is equivalent to enqueue()
mutableStub.next(request = 42) // Returns "zero"
mutableStub.next(request = 42) // Returns "one"
mutableStub.next(request = 42) // Returns "42"

// A new response can be enqueued at any time
mutableStub.enqueue("new")
// The default handler can also change at any time
mutableStub.defaultHandler = { (it + 1).toString() }
mutableStub.next(request = 42) // Returns "new"
mutableStub.next(request = 42) // Returns "43"
public static final void <R extends Object> enqueue(
    @NonNull MutableStub<@NonNull ?, @NonNull R> receiver,
    @NonNull R values
)

Adds a new item to the MutableStub's queue.

import androidx.health.connect.client.testing.stubs.MutableStub
import androidx.health.connect.client.testing.stubs.buildStub
import androidx.health.connect.client.testing.stubs.enqueue

val builderMutableStub =
    buildStub<Int, String> {
        enqueue("first")
        defaultHandler = { "Default" }
    }
builderMutableStub.next(request = 0) // Returns "first"
builderMutableStub.next(request = 0) // Returns "Default"
import androidx.health.connect.client.testing.stubs.MutableStub
import androidx.health.connect.client.testing.stubs.enqueue

val mutableStub = MutableStub<Int, String>(defaultHandler = { it.toString() })
mutableStub.enqueue("zero")
mutableStub += "one" // += is equivalent to enqueue()
mutableStub.next(request = 42) // Returns "zero"
mutableStub.next(request = 42) // Returns "one"
mutableStub.next(request = 42) // Returns "42"

// A new response can be enqueued at any time
mutableStub.enqueue("new")
// The default handler can also change at any time
mutableStub.defaultHandler = { (it + 1).toString() }
mutableStub.next(request = 42) // Returns "new"
mutableStub.next(request = 42) // Returns "43"
public static final void <R extends Object> plusAssign(
    @NonNull MutableStub<@NonNull ?, @NonNull R> receiver,
    @NonNull R value
)

Adds a new item to the MutableStub's queue.

import androidx.health.connect.client.testing.stubs.MutableStub
import androidx.health.connect.client.testing.stubs.enqueue

val mutableStub = MutableStub<Int, String>(defaultHandler = { it.toString() })
mutableStub.enqueue("zero")
mutableStub += "one" // += is equivalent to enqueue()
mutableStub.next(request = 42) // Returns "zero"
mutableStub.next(request = 42) // Returns "one"
mutableStub.next(request = 42) // Returns "42"

// A new response can be enqueued at any time
mutableStub.enqueue("new")
// The default handler can also change at any time
mutableStub.defaultHandler = { (it + 1).toString() }
mutableStub.next(request = 42) // Returns "new"
mutableStub.next(request = 42) // Returns "43"