Skip to content

Commit 7ddcd39

Browse files
implement as possible error extension function for success or single error data structure
1 parent 44b3838 commit 7ddcd39

2 files changed

Lines changed: 34 additions & 1 deletion

File tree

library/src/commonMain/kotlin/com/benromdhane/omar/offroadsoft/monad/error/SuccessOrSingleError.kt

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -223,4 +223,11 @@ fun <SUCCESS : Any, ERROR : Any> SuccessOrSingleError<SUCCESS, ERROR>.flatMapSuc
223223
mapper
224224
) {
225225
Maybe.NotEmpty.of(it)
226-
}
226+
}
227+
228+
fun <SUCCESS : Any, ERROR : Any> SuccessOrSingleError<SUCCESS, ERROR>.asPossibleError() =
229+
this
230+
.fold(
231+
{ PossibleError.Success.of() },
232+
{ PossibleError.Error.of(it) },
233+
)

library/src/commonTest/kotlin/com/benromdhane/omar/offroadsoft/monad/error/SuccessOrSingleErrorTest.kt

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1455,4 +1455,30 @@ class SuccessOrSingleErrorTest {
14551455
assertTrue { result }
14561456
}
14571457
}
1458+
1459+
@Test
1460+
fun `as possible error must return success possible error if initial success or single error is success`() {
1461+
val result =
1462+
SuccessOrSingleError
1463+
.Success
1464+
.of<_, Throwable>(Uuid.random().toString())
1465+
.asPossibleError()
1466+
.error()
1467+
1468+
assertFalse { result }
1469+
}
1470+
1471+
@Test
1472+
fun `as possible error must return error possible error if initial success or single error is error`() {
1473+
val initialError = Exception(Uuid.random().toString())
1474+
val result =
1475+
SuccessOrSingleError
1476+
.Error
1477+
.of<String, _>(initialError)
1478+
.asPossibleError()
1479+
.toMaybeError()
1480+
.orNull()!!
1481+
1482+
assertEquals(initialError, result)
1483+
}
14581484
}

0 commit comments

Comments
 (0)