Skip to content

Commit

Permalink
Add test.
Browse files Browse the repository at this point in the history
  • Loading branch information
Laimiux committed Nov 28, 2023
1 parent c970555 commit fd57c06
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 23 deletions.
59 changes: 36 additions & 23 deletions formula/src/test/java/com/instacart/formula/FormulaRuntimeTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.instacart.formula
import com.google.common.truth.Truth
import com.google.common.truth.Truth.assertThat
import com.instacart.formula.actions.EmptyAction
import com.instacart.formula.internal.ClearPluginsRule
import com.instacart.formula.internal.TestInspector
import com.instacart.formula.internal.Try
import com.instacart.formula.rxjava3.RxAction
Expand Down Expand Up @@ -79,6 +80,7 @@ import org.junit.rules.RuleChain
import org.junit.rules.TestName
import org.junit.runner.RunWith
import org.junit.runners.Parameterized
import kotlin.reflect.KClass

@RunWith(Parameterized::class)
class FormulaRuntimeTest(val runtime: TestableRuntime, val name: String) {
Expand All @@ -92,7 +94,10 @@ class FormulaRuntimeTest(val runtime: TestableRuntime, val name: String) {
}

@get:Rule
val rule = RuleChain.outerRule(TestName()).around(runtime.rule)
val rule = RuleChain
.outerRule(TestName())
.around(ClearPluginsRule())
.around(runtime.rule)

@Test fun `state change triggers an evaluation`() {
val formula = EventCallbackFormula()
Expand Down Expand Up @@ -1249,33 +1254,41 @@ class FormulaRuntimeTest(val runtime: TestableRuntime, val name: String) {

@Test
fun `inspector events`() {
val globalInspector = TestInspector()
FormulaPlugins.setPlugin(object : Plugin {
override fun inspector(type: KClass<*>): Inspector {
return globalInspector
}
})

val formula = StartStopFormula(runtime)
val inspector = TestInspector()
val subject = runtime.test(formula, Unit, inspector)
val localInspector = TestInspector()
val subject = runtime.test(formula, Unit, localInspector)
subject.output { startListening() }
subject.output { stopListening() }
subject.dispose()

assertThat(inspector.events).containsExactly(
"formula-run-started",
"formula-started: com.instacart.formula.subjects.StartStopFormula",
"evaluate-started: com.instacart.formula.subjects.StartStopFormula",
"evaluate-finished: com.instacart.formula.subjects.StartStopFormula",
"formula-run-finished",
"state-changed: com.instacart.formula.subjects.StartStopFormula",
"formula-run-started",
"evaluate-started: com.instacart.formula.subjects.StartStopFormula",
"evaluate-finished: com.instacart.formula.subjects.StartStopFormula",
"action-started: com.instacart.formula.subjects.StartStopFormula",
"formula-run-finished",
"state-changed: com.instacart.formula.subjects.StartStopFormula",
"formula-run-started",
"evaluate-started: com.instacart.formula.subjects.StartStopFormula",
"evaluate-finished: com.instacart.formula.subjects.StartStopFormula",
"action-finished: com.instacart.formula.subjects.StartStopFormula",
"formula-run-finished",
"formula-finished: com.instacart.formula.subjects.StartStopFormula"
).inOrder()
for (inspector in listOf(globalInspector, localInspector)) {
assertThat(inspector.events).containsExactly(
"formula-run-started",
"formula-started: com.instacart.formula.subjects.StartStopFormula",
"evaluate-started: com.instacart.formula.subjects.StartStopFormula",
"evaluate-finished: com.instacart.formula.subjects.StartStopFormula",
"formula-run-finished",
"state-changed: com.instacart.formula.subjects.StartStopFormula",
"formula-run-started",
"evaluate-started: com.instacart.formula.subjects.StartStopFormula",
"evaluate-finished: com.instacart.formula.subjects.StartStopFormula",
"action-started: com.instacart.formula.subjects.StartStopFormula",
"formula-run-finished",
"state-changed: com.instacart.formula.subjects.StartStopFormula",
"formula-run-started",
"evaluate-started: com.instacart.formula.subjects.StartStopFormula",
"evaluate-finished: com.instacart.formula.subjects.StartStopFormula",
"action-finished: com.instacart.formula.subjects.StartStopFormula",
"formula-run-finished",
"formula-finished: com.instacart.formula.subjects.StartStopFormula"
).inOrder()
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.instacart.formula.internal

import com.instacart.formula.FormulaPlugins
import org.junit.rules.TestRule
import org.junit.runner.Description
import org.junit.runners.model.Statement

class ClearPluginsRule : TestRule {
override fun apply(base: Statement, description: Description): Statement {
return object : Statement() {
override fun evaluate() {
FormulaPlugins.setPlugin(null)
try {
base.evaluate()
} finally {
FormulaPlugins.setPlugin(null)
}
}
}
}
}

0 comments on commit fd57c06

Please sign in to comment.