diff --git a/formula/src/main/java/com/instacart/formula/internal/ListInspector.kt b/formula/src/main/java/com/instacart/formula/internal/ListInspector.kt index 026e94f3..cedc9ae6 100644 --- a/formula/src/main/java/com/instacart/formula/internal/ListInspector.kt +++ b/formula/src/main/java/com/instacart/formula/internal/ListInspector.kt @@ -6,64 +6,50 @@ import kotlin.reflect.KClass internal class ListInspector( private val inspectors: List, -): Inspector { +) : Inspector { override fun onFormulaStarted(formulaType: KClass<*>) { - for (inspector in inspectors) { - inspector.onFormulaStarted(formulaType) - } + forEachInspector { onFormulaStarted(formulaType) } } override fun onFormulaFinished(formulaType: KClass<*>) { - for (inspector in inspectors) { - inspector.onFormulaFinished(formulaType) - } + forEachInspector { onFormulaFinished(formulaType) } } override fun onEvaluateStarted(formulaType: KClass<*>, state: Any?) { - for (inspector in inspectors) { - inspector.onEvaluateStarted(formulaType, state) - } + forEachInspector { onEvaluateStarted(formulaType, state) } } override fun onInputChanged(formulaType: KClass<*>, prevInput: Any?, newInput: Any?) { - for (inspector in inspectors) { - inspector.onInputChanged(formulaType, prevInput, newInput) - } + forEachInspector { onInputChanged(formulaType, prevInput, newInput) } } override fun onEvaluateFinished(formulaType: KClass<*>, output: Any?, evaluated: Boolean) { - for (inspector in inspectors) { - inspector.onEvaluateFinished(formulaType, output, evaluated) - } + forEachInspector { onEvaluateFinished(formulaType, output, evaluated) } } override fun onActionStarted(formulaType: KClass<*>, action: DeferredAction<*>) { - for (inspector in inspectors) { - inspector.onActionStarted(formulaType, action) - } + forEachInspector { onActionStarted(formulaType, action) } } override fun onActionFinished(formulaType: KClass<*>, action: DeferredAction<*>) { - for (inspector in inspectors) { - inspector.onActionFinished(formulaType, action) - } + forEachInspector { onActionFinished(formulaType, action) } } override fun onStateChanged(formulaType: KClass<*>, old: Any?, new: Any?) { - for (inspector in inspectors) { - inspector.onStateChanged(formulaType, old, new) - } + forEachInspector { onStateChanged(formulaType, old, new) } } override fun onRunStarted(evaluate: Boolean) { - for (inspector in inspectors) { - inspector.onRunStarted(evaluate) - } + forEachInspector { onRunStarted(evaluate) } } override fun onRunFinished() { + forEachInspector { onRunFinished() } + } + + private inline fun forEachInspector(callback: Inspector.() -> Unit) { for (inspector in inspectors) { - inspector.onRunFinished() + inspector.callback() } } } \ No newline at end of file diff --git a/formula/src/test/java/com/instacart/formula/FormulaRuntimeTest.kt b/formula/src/test/java/com/instacart/formula/FormulaRuntimeTest.kt index cb814e90..d8f52823 100644 --- a/formula/src/test/java/com/instacart/formula/FormulaRuntimeTest.kt +++ b/formula/src/test/java/com/instacart/formula/FormulaRuntimeTest.kt @@ -1291,4 +1291,20 @@ class FormulaRuntimeTest(val runtime: TestableRuntime, val name: String) { ).inOrder() } } + + @Test + fun `only global inspector events`() { + val globalInspector = TestInspector() + FormulaPlugins.setPlugin(object : Plugin { + override fun inspector(type: KClass<*>): Inspector { + return globalInspector + } + }) + + val formula = StartStopFormula(runtime) + val subject = runtime.test(formula, Unit) + subject.dispose() + + assertThat(globalInspector.events).isNotEmpty() + } }