-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Increase test module coverage. (#387)
* Increase test module coverage. * Make multipleValueStream() private.
- Loading branch information
Showing
17 changed files
with
289 additions
and
77 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
75 changes: 75 additions & 0 deletions
75
formula-test/src/test/java/com/instacart/formula/test/CountingInspectorTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
package com.instacart.formula.test | ||
|
||
import com.google.common.truth.Truth | ||
import com.instacart.formula.Evaluation | ||
import com.instacart.formula.Snapshot | ||
import com.instacart.formula.StatelessFormula | ||
import org.junit.Test | ||
|
||
class CountingInspectorTest { | ||
|
||
@Test | ||
fun `assertEvaluationCount throws exception when count does not match`() { | ||
val inspector = CountingInspector() | ||
inspector.assertEvaluationCount(0) | ||
|
||
val result = runCatching { inspector.assertEvaluationCount(5) } | ||
Truth.assertThat(result.exceptionOrNull()).hasMessageThat().contains( | ||
"Evaluation count does not match - count: 0, expected: 5" | ||
) | ||
} | ||
|
||
@Test | ||
fun `assertEvaluationCount with types throws exception when count does not match`() { | ||
val inspector = CountingInspector() | ||
inspector.assertEvaluationCount(MyFormula::class, 0) | ||
inspector.onEvaluateFinished(MyFormula::class, null, true) | ||
inspector.assertEvaluationCount(MyFormula::class, 1) | ||
|
||
val result = runCatching { inspector.assertEvaluationCount(MyFormula::class, 5) } | ||
Truth.assertThat(result.exceptionOrNull()).hasMessageThat().contains( | ||
"Evaluation count does not match - count: 1, expected: 5" | ||
) | ||
} | ||
|
||
@Test | ||
fun `assertRunCount throws exception when count does not match`() { | ||
val inspector = CountingInspector() | ||
inspector.assertRunCount(0) | ||
|
||
val result = runCatching { inspector.assertRunCount(5) } | ||
Truth.assertThat(result.exceptionOrNull()).hasMessageThat().contains( | ||
"Run count does not match - count: 0, expected: 5" | ||
) | ||
} | ||
|
||
@Test | ||
fun `assertActionsStarted throws exception when count does not match`() { | ||
val inspector = CountingInspector() | ||
inspector.assertActionsStarted(0) | ||
|
||
val result = runCatching { inspector.assertActionsStarted(5) } | ||
Truth.assertThat(result.exceptionOrNull()).hasMessageThat().contains( | ||
"Actions started count does not match - count: 0, expected: 5" | ||
) | ||
} | ||
|
||
@Test | ||
fun `assertStateTransitions throws exception when count does not match`() { | ||
val inspector = CountingInspector() | ||
inspector.assertStateTransitions(MyFormula::class, 0) | ||
inspector.onStateChanged(MyFormula::class, null, null, null) | ||
inspector.assertStateTransitions(MyFormula::class, 1) | ||
|
||
val result = runCatching { inspector.assertStateTransitions(MyFormula::class, 5) } | ||
Truth.assertThat(result.exceptionOrNull()).hasMessageThat().contains( | ||
"State transition count does not match - count: 1, expected: 5" | ||
) | ||
} | ||
|
||
class MyFormula : StatelessFormula<Unit, Unit>() { | ||
override fun Snapshot<Unit, Unit>.evaluate(): Evaluation<Unit> { | ||
return Evaluation(Unit) | ||
} | ||
} | ||
} |
61 changes: 61 additions & 0 deletions
61
formula-test/src/test/java/com/instacart/formula/test/TestActionObserverTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package com.instacart.formula.test | ||
|
||
import com.google.common.truth.Truth | ||
import com.google.common.truth.Truth.assertThat | ||
import com.instacart.formula.Action | ||
import com.instacart.formula.Cancelable | ||
import org.junit.Test | ||
import java.lang.IllegalStateException | ||
|
||
class TestActionObserverTest { | ||
|
||
@Test fun `assert values success`() { | ||
multipleValueStream().test().assertValues(1, 2) | ||
} | ||
|
||
@Test fun `assert value fails due to different size`() { | ||
val result = runCatching { multipleValueStream().test().assertValues(1) } | ||
assertThat(result.exceptionOrNull()).isInstanceOf(AssertionError::class.java) | ||
} | ||
|
||
@Test fun `assert value fails due to different value`() { | ||
val result = runCatching { multipleValueStream().test().assertValues(1, 5) } | ||
assertThat(result.exceptionOrNull()).isInstanceOf(AssertionError::class.java) | ||
} | ||
|
||
@Test fun values() { | ||
val values = multipleValueStream().test().values() | ||
assertThat(values).hasSize(2) | ||
} | ||
|
||
@Test fun `cancel throws exception if action does not provide cancelable`() { | ||
val result = kotlin.runCatching { multipleValueStream().test().cancel() } | ||
assertThat(result.exceptionOrNull()).hasMessageThat().contains( | ||
"Action did not return a cancelable." | ||
) | ||
} | ||
|
||
@Test fun `cancel invokes cancelable`() { | ||
var cancelableCalled = 0 | ||
val action = object : Action<String> { | ||
override fun start(send: (String) -> Unit): Cancelable { | ||
return Cancelable { cancelableCalled += 1 } | ||
} | ||
|
||
override fun key(): Any? = null | ||
} | ||
|
||
action.test().cancel() | ||
assertThat(cancelableCalled).isEqualTo(1) | ||
} | ||
|
||
private fun multipleValueStream() = object : Action<Int> { | ||
override fun start(send: (Int) -> Unit): Cancelable? { | ||
send(1) | ||
send(2) | ||
return null | ||
} | ||
|
||
override fun key(): Any = Unit | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
formula-test/src/test/java/com/instacart/formula/test/TestCallbackTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package com.instacart.formula.test | ||
|
||
import com.google.common.truth.Truth | ||
import org.junit.Test | ||
|
||
class TestCallbackTest { | ||
|
||
@Test | ||
fun `assertTimesCalled throws an exception when count does not match`() { | ||
val callback = TestCallback() | ||
callback.assertTimesCalled(0) | ||
callback.invoke() | ||
callback.assertTimesCalled(1) | ||
|
||
val result = kotlin.runCatching { callback.assertTimesCalled(5) } | ||
Truth.assertThat(result.exceptionOrNull()).hasMessageThat().contains( | ||
"Expected: 5, was: 1" | ||
) | ||
} | ||
} |
58 changes: 58 additions & 0 deletions
58
formula-test/src/test/java/com/instacart/formula/test/TestFormulaObserverTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package com.instacart.formula.test | ||
|
||
import com.google.common.truth.Truth | ||
import com.instacart.formula.Evaluation | ||
import com.instacart.formula.Snapshot | ||
import com.instacart.formula.StatelessFormula | ||
import org.junit.Test | ||
|
||
class TestFormulaObserverTest { | ||
|
||
@Test fun `assertOutput passes if count matches`() { | ||
val formula = object : StatelessFormula<Int, Int>() { | ||
override fun Snapshot<Int, Unit>.evaluate(): Evaluation<Int> { | ||
return Evaluation(input) | ||
} | ||
} | ||
|
||
val observer = formula.test() | ||
observer.input(1) | ||
observer.assertOutputCount(1) | ||
|
||
observer.input(10) | ||
observer.assertOutputCount(2) | ||
} | ||
|
||
@Test fun `assertOutput throws exception if count does not match`() { | ||
val formula = object : StatelessFormula<Int, Int>() { | ||
override fun Snapshot<Int, Unit>.evaluate(): Evaluation<Int> { | ||
return Evaluation(input) | ||
} | ||
} | ||
|
||
val observer = formula.test() | ||
observer.input(1) | ||
val result = kotlin.runCatching { | ||
observer.assertOutputCount(5) | ||
} | ||
|
||
Truth.assertThat(result.exceptionOrNull()).hasMessageThat().contains( | ||
"Expected: 5, was: 1" | ||
) | ||
} | ||
|
||
@Test fun `output throws error if formula is not running`() { | ||
val formula = object : StatelessFormula<Int, Int>() { | ||
override fun Snapshot<Int, Unit>.evaluate(): Evaluation<Int> { | ||
return Evaluation(input) | ||
} | ||
} | ||
|
||
val result = runCatching { | ||
formula.test().output { } | ||
} | ||
Truth.assertThat(result.exceptionOrNull()).hasMessageThat().contains( | ||
"Formula is not running. Call [TestFormulaObserver.input] to start it." | ||
) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
formula-test/src/test/java/com/instacart/formula/test/TestListenerTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package com.instacart.formula.test | ||
|
||
import com.google.common.truth.Truth | ||
import org.junit.Test | ||
|
||
class TestListenerTest { | ||
|
||
@Test | ||
fun `assertTimesCalled throws an exception when count does not match`() { | ||
val listener = TestListener<String>() | ||
listener.assertTimesCalled(0) | ||
listener.invoke("value") | ||
listener.assertTimesCalled(1) | ||
listener.invoke("second") | ||
listener.assertTimesCalled(2) | ||
|
||
val result = runCatching { listener.assertTimesCalled(5) } | ||
Truth.assertThat(result.exceptionOrNull()).hasMessageThat().contains( | ||
"Expected: 5, was: 2" | ||
) | ||
} | ||
|
||
@Test | ||
fun values() { | ||
val listener = TestListener<String>() | ||
listener.invoke("value") | ||
listener.invoke("second") | ||
listener.invoke("third") | ||
|
||
Truth.assertThat(listener.values()).containsExactly( | ||
"value", "second", "third" | ||
).inOrder() | ||
} | ||
} |
Oops, something went wrong.