Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

191
Visualizações
How to test Either from Arrow in functional style

I would like to test the obtained result using Either. Let's assume I have a simple example without Either

@Test
fun `test arithmetic`() {
    val simpleResult = 2 + 2
    Assertions.assertEquals(4, simpleResult)
}

And now i have wrapped result:

@Test
fun `test arithmetic with either`() {
    val result : Either<Nothing, Int> = (2 + 2).right()
    Assertions.assertTrue(result.isRight())
    result.map { Assertions.assertEquals(4, it) }
}

I suppose it looks a little bit ugly, because the last Assertions will not be executed if we got Either.Left instead of Either.Right How can I test the result properly in functional style?

over 4 years ago · Santiago Trujillo
3 Respostas
Responde à pergunta

0

kotlintest provides a kotest-assertions-arrow module which can be used to test Arrow types.

It basically expose matchers for Either and other data type. Take a look at this.

@Test
fun `test arithmetic with either`() {
    val result : Either<Nothing, Int> = (2 + 2).right()
    result.shouldBeRight(4)
}
over 4 years ago · Santiago Trujillo Relatório

0

The implementations of Either are data classes on both sides so you can do something like:

check(result == 4.right())

Or can use something similar with any other assertion library that uses equals to assert equality.

over 4 years ago · Santiago Trujillo Relatório

0

You can create extension functions:

fun <L, R> Either<L, R>.assertIsLeft(): L {
    return when (this) {
        is Either.Left -> value
        is Either.Right -> throw AssertionError("Expected Either.Left, but found Either.Right with value $value")
    }
}

fun <L, R> Either<L, R>.assertIsRight(): R {
    return when (this) {
        is Either.Right -> value
        is Either.Left -> throw AssertionError("Expected Either.Right, but found Either.Left with value $value")
    }
}

fun <T: Any> T.assertEqualsTo(expected: T): Boolean {
    return this == expected
}

With them you tests could look like these:

val resultRight : Either<Nothing, Int> = (2 + 2).right()
resultRight
    .assertIsRight()
    .assertEqualsTo(4)

val resultLeft: Either<RuntimeException, Nothing> = RuntimeException("Some exception cause").left()
resultLeft
    .assertIsLeft()
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda