Skip to content

Commit

Permalink
Test missing inspector cases. (#354)
Browse files Browse the repository at this point in the history
  • Loading branch information
Laimiux authored Mar 15, 2024
1 parent 1bb7f9b commit 9c79e02
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 0 deletions.
44 changes: 44 additions & 0 deletions formula/src/test/java/com/instacart/formula/FormulaRuntimeTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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<Int, Int>() {
val delegate = InputIdentityFormula<Int>()
override fun Snapshot<Int, Unit>.evaluate(): Evaluation<Int> {
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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,38 +8,52 @@ class TestInspector : Inspector {
val events = mutableListOf<String>()

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}")
}
}
Original file line number Diff line number Diff line change
@@ -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<Input : Any> : StatelessFormula<Input, Input>() {
override fun Snapshot<Input, Unit>.evaluate(): Evaluation<Input> {
return Evaluation(
output = input,
)
}
}

0 comments on commit 9c79e02

Please sign in to comment.