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

185
Visualizações
Cómo probar cualquiera de Arrow en estilo funcional

Me gustaría probar el resultado obtenido usando cualquiera. Supongamos que tengo un ejemplo simple sin Ambos

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

Y ahora he envuelto el resultado:

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

Supongo que se ve un poco feo, porque las últimas Either.Left no se ejecutarán si tenemos Cualquiera.Izquierda en lugar de Either.Right ¿Cómo puedo probar el resultado correctamente en estilo funcional?

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

0

kotlintest proporciona un kotest-assertions-arrow que se puede usar para probar los tipos de flecha.

Básicamente, expone los emparejadores para cualquiera y otros tipos de datos. Echa un vistazo a esto .

 @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

Las implementaciones de Either son clases de datos en ambos lados, por lo que puede hacer algo como:

 check(result == 4.right())

O puede usar algo similar con cualquier otra biblioteca de aserciones que use equals para afirmar la igualdad.

over 4 years ago · Santiago Trujillo Relatório

0

Puede crear funciones de extensión:

 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 }

Con ellos, tus pruebas podrían verse así:

 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