Skip to content

Commit

Permalink
Formula plugin coverage. (#353)
Browse files Browse the repository at this point in the history
  • Loading branch information
Laimiux authored Mar 15, 2024
1 parent d565baa commit cdea009
Showing 1 changed file with 78 additions and 0 deletions.
78 changes: 78 additions & 0 deletions formula/src/test/java/com/instacart/formula/FormulaPluginTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package com.instacart.formula

import com.google.common.truth.Truth.assertThat
import com.instacart.formula.internal.ClearPluginsRule
import com.instacart.formula.plugin.Dispatcher
import com.instacart.formula.plugin.Plugin
import com.instacart.formula.subjects.IncrementingDispatcher
import org.junit.Rule
import org.junit.Test

class FormulaPluginTest {

private class TestPlugin(
private val mainDispatcher: Dispatcher? = null,
private val backgroundDispatcher: Dispatcher? = null,
private val defaultDispatcher: Dispatcher? = null,
) : Plugin {
override fun mainThreadDispatcher(): Dispatcher? {
return mainDispatcher ?: super.mainThreadDispatcher()
}

override fun backgroundThreadDispatcher(): Dispatcher? {
return backgroundDispatcher ?: super.backgroundThreadDispatcher()
}

override fun defaultDispatcher(): Dispatcher? {
return defaultDispatcher ?: super.defaultDispatcher()
}
}

@get:Rule
val rule = ClearPluginsRule()

@Test fun `when no plugin is specified, default dispatcher is none`() {
assertThat(FormulaPlugins.defaultDispatcher()).isEqualTo(Dispatcher.None)
}

@Test fun `when plugin returns null, default dispatcher is none`() {
FormulaPlugins.setPlugin(TestPlugin())
assertThat(FormulaPlugins.defaultDispatcher()).isEqualTo(Dispatcher.None)
}

@Test fun `plugin can override default dispatcher`() {
val myDispatcher = IncrementingDispatcher()
FormulaPlugins.setPlugin(TestPlugin(defaultDispatcher = myDispatcher))
assertThat(FormulaPlugins.defaultDispatcher()).isEqualTo(myDispatcher)
}

@Test fun `when no plugin is specified, default background dispatcher is none`() {
assertThat(FormulaPlugins.backgroundThreadDispatcher()).isEqualTo(Dispatcher.None)
}

@Test fun `when plugin returns null, default background dispatcher is none`() {
FormulaPlugins.setPlugin(TestPlugin())
assertThat(FormulaPlugins.backgroundThreadDispatcher()).isEqualTo(Dispatcher.None)
}

@Test fun `plugin can override background dispatcher`() {
val myDispatcher = IncrementingDispatcher()
FormulaPlugins.setPlugin(TestPlugin(backgroundDispatcher = myDispatcher))
assertThat(FormulaPlugins.backgroundThreadDispatcher()).isEqualTo(myDispatcher)
}

@Test fun `when plugin is null, default main dispatcher is none`() {
assertThat(FormulaPlugins.mainThreadDispatcher()).isEqualTo(Dispatcher.None)
}

@Test fun `when plugin returns null, default main dispatcher is none`() {
FormulaPlugins.setPlugin(TestPlugin())
assertThat(FormulaPlugins.mainThreadDispatcher()).isEqualTo(Dispatcher.None)
}

@Test fun `plugin can override main dispatcher`() {
val myDispatcher = IncrementingDispatcher()
FormulaPlugins.setPlugin(TestPlugin(mainDispatcher = myDispatcher))
assertThat(FormulaPlugins.mainThreadDispatcher()).isEqualTo(myDispatcher)
}
}

0 comments on commit cdea009

Please sign in to comment.