Skip to content

Commit

Permalink
Added EnroTestAssertions and TestNavigationHandle.assertClosedWithRes…
Browse files Browse the repository at this point in the history
…ult which were missed (not added) in a previous commit
  • Loading branch information
isaac-udy committed Jun 21, 2024
1 parent f56bc1c commit d1f3685
Show file tree
Hide file tree
Showing 2 changed files with 127 additions and 0 deletions.
35 changes: 35 additions & 0 deletions enro-test/src/main/java/dev/enro/test/EnroTestAssertions.kt
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))
}
}
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"
}
}

0 comments on commit d1f3685

Please sign in to comment.