-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Updated enro-test to prefer "assert" methods in all cases, added func…
…tionality to deliver a result directly to a particular navigation key, and a few other minor documentation and function naming updates
- Loading branch information
Showing
22 changed files
with
662 additions
and
109 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
37 changes: 37 additions & 0 deletions
37
enro-test/src/main/java/dev/enro/test/NavigationInstruction.deliverResultForTest.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,37 @@ | ||
@file:Suppress("INVISIBLE_REFERENCE", "INVISIBLE_MEMBER") | ||
package dev.enro.test | ||
|
||
import dev.enro.core.NavigationInstruction | ||
import dev.enro.core.NavigationKey | ||
import dev.enro.core.result.EnroResult | ||
import dev.enro.core.result.internal.PendingResult | ||
|
||
/** | ||
* Given a NavigationInstruction.Open, this function will deliver a result to the instruction. This is useful for testing | ||
* the behavior of a screen/ViewModel that expects a result. | ||
*/ | ||
fun <T : Any> NavigationInstruction.Open<*>.deliverResultForTest(type: Class<T>, result: T) { | ||
val navigationController = EnroTest.getCurrentNavigationController() | ||
val resultId = internal.resultId!! | ||
|
||
val navigationKey = internal.resultKey ?: navigationKey | ||
|
||
val pendingResult = PendingResult.Result( | ||
resultChannelId = resultId, | ||
instruction = this, | ||
navigationKey = navigationKey as NavigationKey.WithResult<T>, | ||
resultType = type.kotlin, | ||
result = result | ||
) | ||
EnroResult | ||
.from(navigationController) | ||
.addPendingResult(pendingResult) | ||
} | ||
|
||
/** | ||
* Given a NavigationInstruction.Open, this function will deliver a result to the instruction. This is useful for testing | ||
* the behavior of a screen/ViewModel that expects a result. | ||
*/ | ||
inline fun <reified T : Any> NavigationInstruction.Open<*>.deliverResultForTest(result: T) { | ||
deliverResultForTest(T::class.java, result) | ||
} |
62 changes: 62 additions & 0 deletions
62
enro-test/src/main/java/dev/enro/test/NavigationKey.deliverResultForTest.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,62 @@ | ||
@file:Suppress("INVISIBLE_REFERENCE", "INVISIBLE_MEMBER") | ||
package dev.enro.test | ||
|
||
import dev.enro.core.NavigationInstruction | ||
import dev.enro.core.NavigationKey | ||
import dev.enro.core.result.EnroResult | ||
import dev.enro.core.result.internal.PendingResult | ||
|
||
/** | ||
* Given a NavigationKey.WithResult, this function will attempt to find the related NavigationInstruction that opened that | ||
* NavigationKey, and deliver a result to that instruction. This is useful for testing the behavior of a screen/ViewModel | ||
* that expects a result. | ||
*/ | ||
fun <T : Any> NavigationKey.WithResult<T>.deliverResultForTest( | ||
type: Class<T>, | ||
result: T, | ||
) { | ||
val exactInstruction = TestNavigationHandle.allInstructions | ||
.filterIsInstance<NavigationInstruction.Open<*>>() | ||
.firstOrNull { | ||
System.identityHashCode(it.navigationKey) == System.identityHashCode(this) | ||
} | ||
val fuzzyInstructions = TestNavigationHandle.allInstructions | ||
.filterIsInstance<NavigationInstruction.Open<*>>() | ||
.filter { | ||
it.navigationKey == this | ||
} | ||
if (fuzzyInstructions.isEmpty()) { | ||
throw EnroTestAssertionException("No instruction was found for NavigationKey $this") | ||
} | ||
val instruction = when { | ||
exactInstruction != null -> exactInstruction | ||
fuzzyInstructions.size == 1 -> fuzzyInstructions.first() | ||
else -> { | ||
throw EnroTestAssertionException("No instruction was found for NavigationKey $this") | ||
} | ||
} | ||
val navigationController = EnroTest.getCurrentNavigationController() | ||
val resultId = instruction.internal.resultId!! | ||
val navigationKey = instruction.internal.resultKey ?: instruction.navigationKey | ||
|
||
@Suppress("UNCHECKED_CAST") | ||
val pendingResult = PendingResult.Result( | ||
resultChannelId = resultId, | ||
instruction = instruction, | ||
navigationKey = navigationKey as NavigationKey.WithResult<T>, | ||
resultType = type.kotlin, | ||
result = result | ||
) | ||
EnroResult | ||
.from(navigationController) | ||
.addPendingResult(pendingResult) | ||
} | ||
|
||
/** | ||
* Given a NavigationKey.WithResult, this function will attempt to find the related NavigationInstruction that opened that | ||
* NavigationKey, and deliver a result to that instruction. This is useful for testing the behavior of a screen/ViewModel | ||
* that expects a result. | ||
*/ | ||
inline fun <reified T : Any> NavigationKey.WithResult<T>.deliverResultForTest(result: T) { | ||
deliverResultForTest(T::class.java, result) | ||
} |
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
Oops, something went wrong.