From cdea009af1591d93a4473fbf53110a3d4c2111c5 Mon Sep 17 00:00:00 2001 From: Laimonas Turauskas Date: Fri, 15 Mar 2024 16:28:40 -0400 Subject: [PATCH] Formula plugin coverage. (#353) --- .../instacart/formula/FormulaPluginTest.kt | 78 +++++++++++++++++++ 1 file changed, 78 insertions(+) create mode 100644 formula/src/test/java/com/instacart/formula/FormulaPluginTest.kt diff --git a/formula/src/test/java/com/instacart/formula/FormulaPluginTest.kt b/formula/src/test/java/com/instacart/formula/FormulaPluginTest.kt new file mode 100644 index 00000000..247be6e1 --- /dev/null +++ b/formula/src/test/java/com/instacart/formula/FormulaPluginTest.kt @@ -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) + } +} \ No newline at end of file