From 9c79e02cc64fed64398acf39e0d2577ceef69088 Mon Sep 17 00:00:00 2001 From: Laimonas Turauskas Date: Fri, 15 Mar 2024 16:20:11 -0400 Subject: [PATCH] Test missing inspector cases. (#354) --- .../instacart/formula/FormulaRuntimeTest.kt | 44 +++++++++++++++++++ .../formula/internal/TestInspector.kt | 14 ++++++ .../formula/types/InputIdentityFormula.kt | 13 ++++++ 3 files changed, 71 insertions(+) create mode 100644 formula/src/test/java/com/instacart/formula/types/InputIdentityFormula.kt diff --git a/formula/src/test/java/com/instacart/formula/FormulaRuntimeTest.kt b/formula/src/test/java/com/instacart/formula/FormulaRuntimeTest.kt index 6bcea90d..4ff51fec 100644 --- a/formula/src/test/java/com/instacart/formula/FormulaRuntimeTest.kt +++ b/formula/src/test/java/com/instacart/formula/FormulaRuntimeTest.kt @@ -80,6 +80,7 @@ import com.instacart.formula.test.TestableRuntime import com.instacart.formula.types.ActionDelegateFormula import com.instacart.formula.types.IncrementActionFormula import com.instacart.formula.types.IncrementFormula +import com.instacart.formula.types.InputIdentityFormula import com.instacart.formula.types.OnDataActionFormula import com.instacart.formula.types.OnEventFormula import com.instacart.formula.types.OnInitActionFormula @@ -1379,6 +1380,49 @@ class FormulaRuntimeTest(val runtime: TestableRuntime, val name: String) { } } + @Test fun `input changed inspector event`() { + val localInspector = TestInspector() + val globalInspector = TestInspector() + FormulaPlugins.setPlugin(object : Plugin { + override fun inspector(type: KClass<*>): Inspector { + return globalInspector + } + }) + + val formula = object : StatelessFormula() { + val delegate = InputIdentityFormula() + override fun Snapshot.evaluate(): Evaluation { + return Evaluation( + output = context.child(delegate, input) + ) + } + } + val subject = runtime.test(formula, 0, localInspector) + subject.input(1) + + for (inspector in listOf(globalInspector, localInspector)) { + assertThat(inspector.events).containsExactly( + "formula-run-started", + "formula-started: null", + "evaluate-started: null", + "formula-started: com.instacart.formula.types.InputIdentityFormula", + "evaluate-started: com.instacart.formula.types.InputIdentityFormula", + "evaluate-finished: com.instacart.formula.types.InputIdentityFormula", + "evaluate-finished: null", + "formula-run-finished", + + "formula-run-started", + "evaluate-started: null", + "input-changed: null", + "evaluate-started: com.instacart.formula.types.InputIdentityFormula", + "input-changed: com.instacart.formula.types.InputIdentityFormula", + "evaluate-finished: com.instacart.formula.types.InputIdentityFormula", + "evaluate-finished: null", + "formula-run-finished" + ).inOrder() + } + } + @Test fun `only global inspector events`() { val globalInspector = TestInspector() diff --git a/formula/src/test/java/com/instacart/formula/internal/TestInspector.kt b/formula/src/test/java/com/instacart/formula/internal/TestInspector.kt index 71b94b79..c652df84 100644 --- a/formula/src/test/java/com/instacart/formula/internal/TestInspector.kt +++ b/formula/src/test/java/com/instacart/formula/internal/TestInspector.kt @@ -8,38 +8,52 @@ class TestInspector : Inspector { val events = mutableListOf() override fun onRunStarted(evaluate: Boolean) { + super.onRunStarted(evaluate) events.add("formula-run-started") } override fun onRunFinished() { + super.onRunFinished() events.add("formula-run-finished") } override fun onFormulaStarted(formulaType: KClass<*>) { + super.onFormulaStarted(formulaType) events.add("formula-started: ${formulaType.qualifiedName}") } override fun onEvaluateStarted(formulaType: KClass<*>, state: Any?) { + super.onEvaluateStarted(formulaType, state) events.add("evaluate-started: ${formulaType.qualifiedName}") } override fun onEvaluateFinished(formulaType: KClass<*>, output: Any?, evaluated: Boolean) { + super.onEvaluateFinished(formulaType, output, evaluated) events.add("evaluate-finished: ${formulaType.qualifiedName}") } override fun onActionStarted(formulaType: KClass<*>, action: DeferredAction<*>) { + super.onActionStarted(formulaType, action) events.add("action-started: ${formulaType.qualifiedName}") } override fun onActionFinished(formulaType: KClass<*>, action: DeferredAction<*>) { + super.onActionFinished(formulaType, action) events.add("action-finished: ${formulaType.qualifiedName}") } + override fun onInputChanged(formulaType: KClass<*>, prevInput: Any?, newInput: Any?) { + super.onInputChanged(formulaType, prevInput, newInput) + events.add("input-changed: ${formulaType.qualifiedName}") + } + override fun onStateChanged(formulaType: KClass<*>, event: Any?, old: Any?, new: Any?) { + super.onStateChanged(formulaType, event, old, new) events.add("state-changed: ${formulaType.qualifiedName}") } override fun onFormulaFinished(formulaType: KClass<*>) { + super.onFormulaFinished(formulaType) events.add("formula-finished: ${formulaType.qualifiedName}") } } \ No newline at end of file diff --git a/formula/src/test/java/com/instacart/formula/types/InputIdentityFormula.kt b/formula/src/test/java/com/instacart/formula/types/InputIdentityFormula.kt new file mode 100644 index 00000000..85efbad2 --- /dev/null +++ b/formula/src/test/java/com/instacart/formula/types/InputIdentityFormula.kt @@ -0,0 +1,13 @@ +package com.instacart.formula.types + +import com.instacart.formula.Evaluation +import com.instacart.formula.Snapshot +import com.instacart.formula.StatelessFormula + +class InputIdentityFormula : StatelessFormula() { + override fun Snapshot.evaluate(): Evaluation { + return Evaluation( + output = input, + ) + } +}