Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove inline functions from RxAction and add tests #359

Merged
merged 1 commit into from
Mar 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ import com.google.common.truth.Truth.assertThat
import com.instacart.formula.android.FragmentFlowState
import com.instacart.formula.android.FragmentKey
import com.instacart.formula.android.BackCallback
import com.instacart.formula.android.FragmentEnvironment
import com.instacart.formula.rxjava3.toObservable
import com.instacart.formula.test.TestKey
import com.instacart.formula.test.TestKeyWithId
import com.instacart.formula.test.TestFragmentActivity
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,10 @@ interface RxAction<Event : Any> : Action<Event> {
* }
* ```
*/
inline fun <Event : Any> fromObservable(
crossinline create: () -> Observable<Event>
fun <Event : Any> fromObservable(
create: () -> Observable<Event>
): Action<Event> {
return object : RxAction<Event> {

override fun observable(): Observable<Event> {
return create()
}

override fun key(): Any = Unit
}
return fromObservable(Unit, create)
}

/**
Expand All @@ -45,18 +38,11 @@ interface RxAction<Event : Any> : Action<Event> {
*
* @param key Used to distinguish this [Action] from other actions.
*/
inline fun <Event : Any> fromObservable(
fun <Event : Any> fromObservable(
key: Any?,
crossinline create: () -> Observable<Event>
create: () -> Observable<Event>
): Action<Event> {
return object : RxAction<Event> {

override fun observable(): Observable<Event> {
return create()
}

override fun key(): Any? = key
}
return RxActionImpl(key, create)
}
}

Expand All @@ -67,3 +53,16 @@ interface RxAction<Event : Any> : Action<Event> {
return Cancelable(disposable::dispose)
}
}

private data class RxActionImpl<Event : Any>(
private val key: Any?,
private val factory: () -> Observable<Event>
): RxAction<Event> {
override fun observable(): Observable<Event> {
return factory()
}

override fun key(): Any? {
return key
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.instacart.formula.rxjava3

import com.instacart.formula.RuntimeConfig
import com.instacart.formula.test.CountingInspector
import com.instacart.formula.types.InputIdentityFormula
import io.reactivex.rxjava3.core.Observable
import org.junit.Test

class RxJavaRuntimeTest {

@Test fun `toObservable with unit input and no config`() {
val formula = InputIdentityFormula<Unit>()
formula.toObservable().test().assertValues(Unit)
}

@Test fun `toObservable with unit input and config`() {
val inspector = CountingInspector()
val config = RuntimeConfig(inspector = inspector)
val formula = InputIdentityFormula<Unit>()
formula.toObservable(config).test().assertValues(Unit)
inspector.assertEvaluationCount(1)
}

@Test fun `toObservable with integer input and no config`() {
val formula = InputIdentityFormula<Int>()
formula.toObservable(0).test().assertValues(0)
}

@Test fun `toObservable with observable input and no config`() {
val formula = InputIdentityFormula<Int>()
formula.toObservable(Observable.just(0, 1, 2)).test().assertValues(0, 1, 2)
}
}
Loading