-
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.
Added EnroTestAssertions and TestNavigationHandle.assertClosedWithRes…
…ult which were missed (not added) in a previous commit
- Loading branch information
Showing
2 changed files
with
127 additions
and
0 deletions.
There are no files selected for viewing
35 changes: 35 additions & 0 deletions
35
enro-test/src/main/java/dev/enro/test/EnroTestAssertions.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,35 @@ | ||
package dev.enro.test | ||
|
||
class EnroTestAssertionException(message: String) : AssertionError(message) | ||
|
||
@PublishedApi | ||
internal fun enroAssertionError(message: String): Nothing { | ||
throw EnroTestAssertionException(message) | ||
} | ||
|
||
data class EnroAssertionContext( | ||
val expected: Any?, | ||
val actual: Any?, | ||
) | ||
|
||
@PublishedApi | ||
internal fun Any?.shouldBeEqualTo(expected: Any?, message: EnroAssertionContext.() -> String) { | ||
if (this != expected) { | ||
val assertionContext = EnroAssertionContext( | ||
expected = expected, | ||
actual = this | ||
) | ||
throw EnroTestAssertionException(message(assertionContext)) | ||
} | ||
} | ||
|
||
@PublishedApi | ||
internal fun Any?.shouldNotBeEqualTo(expected: Any?, message: EnroAssertionContext.() -> String) { | ||
if (this == expected) { | ||
val assertionContext = EnroAssertionContext( | ||
expected = expected, | ||
actual = this | ||
) | ||
throw EnroTestAssertionException(message(assertionContext)) | ||
} | ||
} |
92 changes: 92 additions & 0 deletions
92
enro-test/src/main/java/dev/enro/test/TestNavigationHandle.assertClosedWithResult.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,92 @@ | ||
package dev.enro.test | ||
|
||
import dev.enro.core.NavigationInstruction | ||
import dev.enro.core.NavigationKey | ||
import kotlin.reflect.KClass | ||
|
||
/** | ||
* Asserts that the NavigationHandle has executed a Close.WithResult instruction, and that the result matches the provided predicate | ||
*/ | ||
fun <T : Any> TestNavigationHandle<out NavigationKey.WithResult<out T>>.assertClosedWithResult( | ||
type: KClass<T>, | ||
predicate: (T) -> Boolean = { true }, | ||
) { | ||
val instruction = instructions.filterIsInstance<NavigationInstruction.Close.WithResult>() | ||
.lastOrNull() | ||
|
||
instruction.shouldNotBeEqualTo(null) { | ||
"NavigationHandle was expected to have executed a Close.WithResult instruction, but no Close.WithResult instruction was found" | ||
} | ||
requireNotNull(instruction) | ||
|
||
val result = instruction.result | ||
val isAssignable = type.isInstance(result) | ||
isAssignable.shouldBeEqualTo(true) { | ||
"NavigationHandle's Close.WithResult was expected to be assignable to ${type}, but was of type ${instruction.result::class}" | ||
} | ||
@Suppress("UNCHECKED_CAST") | ||
result as T | ||
|
||
predicate(result).shouldBeEqualTo(true) { | ||
"NavigationHandle's Close.WithResult did not match the provided predicate\n\tResult: $result" | ||
} | ||
} | ||
|
||
/** | ||
* Asserts that the NavigationHandle has executed a Close.WithResult instruction, and that the result matches the provided predicate | ||
*/ | ||
inline fun <reified T : Any> TestNavigationHandle<out NavigationKey.WithResult<out T>>.assertClosedWithResult( | ||
predicate: (T) -> Boolean = { true }, | ||
) { | ||
val instruction = instructions.filterIsInstance<NavigationInstruction.Close.WithResult>() | ||
.lastOrNull() | ||
|
||
instruction.shouldNotBeEqualTo(null) { | ||
"NavigationHandle was expected to have executed a Close.WithResult instruction, but no Close.WithResult instruction was found" | ||
} | ||
requireNotNull(instruction) | ||
|
||
val result = instruction.result | ||
val isAssignable = T::class.isInstance(result) | ||
isAssignable.shouldBeEqualTo(true) { | ||
"NavigationHandle's Close.WithResult was expected to be assignable to ${T::class}, but was of type ${instruction.result::class}" | ||
} | ||
@Suppress("UNCHECKED_CAST") | ||
result as T | ||
|
||
predicate(result).shouldBeEqualTo(true) { | ||
"NavigationHandle's Close.WithResult did not match the provided predicate\n\tResult: $result" | ||
} | ||
} | ||
|
||
/** | ||
* Asserts that the NavigationHandle has executed a Close.WithResult instruction, and that the result is equal to [expected] | ||
*/ | ||
fun <T : Any> TestNavigationHandle<out NavigationKey.WithResult<out T>>.assertClosedWithResult( | ||
expected: T, | ||
) { | ||
val instruction = instructions.filterIsInstance<NavigationInstruction.Close.WithResult>() | ||
.lastOrNull() | ||
|
||
instruction.shouldNotBeEqualTo(null) { | ||
"NavigationHandle was expected to have executed a Close.WithResult instruction, but no Close.WithResult instruction was found" | ||
} | ||
requireNotNull(instruction) | ||
|
||
val result = instruction.result | ||
result.shouldBeEqualTo(expected) { | ||
"NavigationHandle's Close.WithResult was expected to be $expected, but was $result" | ||
} | ||
} | ||
|
||
/** | ||
* Asserts that the NavigationHandle has not executed a Close.WithResult instruction | ||
*/ | ||
fun TestNavigationHandle<out NavigationKey.WithResult<*>>.assertNotClosedWithResult() { | ||
val instruction = instructions.filterIsInstance<NavigationInstruction.Close.WithResult>() | ||
.lastOrNull() | ||
|
||
instruction.shouldBeEqualTo(null) { | ||
"NavigationHandle should not have executed a Close.WithResult instruction, but a Close.WithResult instruction was found" | ||
} | ||
} |