Skip to content

Commit

Permalink
tidy up, formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
dkandalov committed Oct 6, 2023
1 parent 86b5d55 commit c5bd3eb
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -60,14 +60,14 @@ infix fun <E> Result<*, E>.shouldBeFailure(expected: E) =

private fun <T, E> Result<T, E>.matchesValue(value: Result<T, E>): MatcherResult =
MatcherResult(
value == this,
{ "$value should be $this" },
{ "$value should not be $this" },
passed = value == this,
failureMessageFn = { "$value should be $this" },
negatedFailureMessageFn = { "$value should not be $this" },
)

private fun <T, E, C : Result<T, E>> Result<T, E>.matchesType(resultClass: KClass<C>): MatcherResult =
MatcherResult(
resultClass.isInstance(this),
{ "$this should be ${resultClass.simpleName}" },
{ "$this should not be ${resultClass.simpleName}" },
passed = resultClass.isInstance(this),
failureMessageFn = { "$this should be ${resultClass.simpleName}" },
negatedFailureMessageFn = { "$this should not be ${resultClass.simpleName}" },
)
Original file line number Diff line number Diff line change
Expand Up @@ -12,62 +12,57 @@ import org.junit.jupiter.api.assertDoesNotThrow
import org.junit.jupiter.api.assertThrows

class MatchersTest {

@Test
fun `should correctly assert when Success`() {
val actualValue = "Successful"
val actualResult = Success(actualValue)
val value = "Successful"
val success = Success(value)

assertDoesNotThrow { actualResult.shouldBeSuccess() }
assertDoesNotThrow { actualResult.shouldBeSuccess() shouldBe actualValue }
assertDoesNotThrow { actualResult.shouldBeSuccess { assertEquals(actualValue, it) } }
assertDoesNotThrow { actualResult.shouldBeSuccess(actualValue) }
assertDoesNotThrow { success.shouldBeSuccess() }
assertDoesNotThrow { success.shouldBeSuccess() shouldBe value }
assertDoesNotThrow { success.shouldBeSuccess { assertEquals(value, it) } }
assertDoesNotThrow { success.shouldBeSuccess(value) }
}

@Test
fun `should correctly assert when Failure`() {
val actualValue = "Failed"
val actualResult = Failure(actualValue)
val reason = "Failed"
val failure = Failure(reason)

assertDoesNotThrow { actualResult.shouldBeFailure() }
assertDoesNotThrow { actualResult.shouldBeFailure() shouldBe actualValue }
assertDoesNotThrow { actualResult.shouldBeFailure { assertEquals(actualValue, it) } }
assertDoesNotThrow { actualResult.shouldBeFailure(actualValue) }
assertDoesNotThrow { failure.shouldBeFailure() }
assertDoesNotThrow { failure.shouldBeFailure() shouldBe reason }
assertDoesNotThrow { failure.shouldBeFailure { assertEquals(reason, it) } }
assertDoesNotThrow { failure.shouldBeFailure(reason) }
}

@Test
fun `should correctly assert when Success but expecting Failure`() {
val actualValue = "Test successful"
val actualResult = Success(actualValue)
val value = "Test successful"
val success = Success(value)

throwsAssertionError("Success(value=Test successful) should be Failure") {
actualResult.shouldBeFailure()
success.shouldBeFailure()
}

throwsAssertionError("Success(value=Test successful) should be Failure") {
actualResult.shouldBeFailure { }
success.shouldBeFailure { }
}

throwsAssertionError("Success(value=Test successful) should be Failure(reason=Test successful)") {
actualResult.shouldBeFailure(actualValue)
success.shouldBeFailure(value)
}
}

@Test
fun `should correctly assert when Failure but expecting Success`() {
val actualValue = "Test failed"
val actualResult = Failure(actualValue)
val reason = "Test failed"
val failure = Failure(reason)

throwsAssertionError("Failure(reason=Test failed) should be Success") {
actualResult.shouldBeSuccess()
failure.shouldBeSuccess()
}

throwsAssertionError("Failure(reason=Test failed) should be Success") {
actualResult.shouldBeSuccess { }
failure.shouldBeSuccess { }
}

throwsAssertionError("Failure(reason=Test failed) should be Success(value=Test failed)") {
actualResult.shouldBeSuccess(actualValue)
failure.shouldBeSuccess(reason)
}
}

Expand All @@ -76,7 +71,6 @@ class MatchersTest {
throwsAssertionError("Failure(reason=Actual value) should be Failure(reason=Expected value)") {
Failure("Actual value").shouldBeFailure("Expected value")
}

throwsAssertionError("Success(value=Actual value) should be Success(value=Expected value)") {
Success("Actual value").shouldBeSuccess("Expected value")
}
Expand All @@ -86,5 +80,4 @@ class MatchersTest {
assertThrows<AssertionError> { block() }.also {
assertThat(it.message, present(equalTo(message)))
}

}

0 comments on commit c5bd3eb

Please sign in to comment.