Skip to content

Commit 2cc4ecf

Browse files
Merge pull request #19
success or single error extension functions
2 parents cd127b6 + 7ddcd39 commit 2cc4ecf

2 files changed

Lines changed: 219 additions & 1 deletion

File tree

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

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ sealed interface SuccessOrSingleError<SUCCESS : Any, ERROR : Any> {
2727
): SuccessOrSingleError<SUCCESS, ERROR>
2828

2929
fun toSuccess(alternativeSuccess: SUCCESS): SuccessOrSingleError<SUCCESS, ERROR>
30+
3031
fun toSuccess(alternativeSuccess: () -> SUCCESS): SuccessOrSingleError<SUCCESS, ERROR>
3132
fun toSuccess(alternativeSuccess: SUCCESS, condition: (ERROR) -> Boolean): SuccessOrSingleError<SUCCESS, ERROR>
3233
fun toSuccess(
@@ -206,4 +207,27 @@ fun <SUCCESS : Any, ERROR : Throwable> SuccessOrSingleError<SUCCESS, ERROR>.asTr
206207
this.fold(
207208
{ Try.seed(it) },
208209
{ Try.seed(it) }
209-
)
210+
)
211+
212+
fun <SUCCESS : Any, ERROR : Any> SuccessOrSingleError<SUCCESS, ERROR>.flatMapSuccessToPossibleError(mapper: (SUCCESS) -> PossibleError<ERROR>) =
213+
this
214+
.fold(
215+
mapper
216+
) {
217+
PossibleError.Error.of(it)
218+
}
219+
220+
fun <SUCCESS : Any, ERROR : Any> SuccessOrSingleError<SUCCESS, ERROR>.flatMapSuccessToMaybe(mapper: (SUCCESS) -> Maybe<ERROR>) =
221+
this
222+
.fold(
223+
mapper
224+
) {
225+
Maybe.NotEmpty.of(it)
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: 194 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.benromdhane.omar.offroadsoft.monad.error
22

33
import com.benromdhane.omar.offroadsoft.monad.Either
4+
import com.benromdhane.omar.offroadsoft.monad.Maybe
45
import io.kotest.assertions.assertSoftly
56
import kotlin.random.Random
67
import kotlin.test.Test
@@ -1287,4 +1288,197 @@ class SuccessOrSingleErrorTest {
12871288

12881289
assertEquals(initialValue, result)
12891290
}
1291+
1292+
@Test
1293+
fun `flat map success with possible error as mapper result must return possible error with initial error if success or single error was initiated as error and mapper result is error`() {
1294+
val initialError = Exception(Uuid.random().toString())
1295+
val newError = Exception(Uuid.random().toString())
1296+
var evaluated = false
1297+
val result =
1298+
SuccessOrSingleError
1299+
.Error
1300+
.of<String, _>(initialError)
1301+
.flatMapSuccessToPossibleError {
1302+
evaluated = true
1303+
PossibleError.Error.of(newError)
1304+
}
1305+
.toMaybeError()
1306+
.orNull()!!
1307+
1308+
assertSoftly {
1309+
assertFalse { evaluated }
1310+
assertEquals(initialError, result)
1311+
}
1312+
}
1313+
1314+
@Test
1315+
fun `flat map success with possible error as mapper result must return possible error with initial error if success or single error was initiated as error and mapper result is success`() {
1316+
val initialError = Exception(Uuid.random().toString())
1317+
var evaluated = false
1318+
val result =
1319+
SuccessOrSingleError
1320+
.Error
1321+
.of<String, _>(initialError)
1322+
.flatMapSuccessToPossibleError {
1323+
evaluated = true
1324+
PossibleError.Success.of()
1325+
}
1326+
.toMaybeError()
1327+
.orNull()!!
1328+
1329+
assertSoftly {
1330+
assertFalse { evaluated }
1331+
assertEquals(initialError, result)
1332+
}
1333+
}
1334+
1335+
@Test
1336+
fun `flat map success with possible error as mapper result must return possible error with mapper error if success or single error was initiated as success and mapper result is error`() {
1337+
val initialValue = Uuid.random().toString()
1338+
val error = Exception(Uuid.random().toString())
1339+
var evaluated = false
1340+
val result =
1341+
SuccessOrSingleError
1342+
.Success
1343+
.of<_, Throwable>(initialValue)
1344+
.flatMapSuccessToPossibleError {
1345+
evaluated = true
1346+
PossibleError.Error.of(error)
1347+
}
1348+
.toMaybeError()
1349+
.orNull()!!
1350+
1351+
assertSoftly {
1352+
assertTrue { evaluated }
1353+
assertEquals(error, result)
1354+
}
1355+
}
1356+
1357+
@Test
1358+
fun `flat map success with possible error as mapper result must return possible error success if success or single error was initiated as success and mapper result is success`() {
1359+
val initialValue = Uuid.random().toString()
1360+
var evaluated = false
1361+
val result =
1362+
SuccessOrSingleError
1363+
.Success
1364+
.of<_, Throwable>(initialValue)
1365+
.flatMapSuccessToPossibleError {
1366+
evaluated = true
1367+
PossibleError.Success.of()
1368+
}
1369+
.error()
1370+
1371+
assertSoftly {
1372+
assertTrue { evaluated }
1373+
assertFalse { result }
1374+
}
1375+
}
1376+
1377+
@Test
1378+
fun `flat map success with maybe error as mapper result must return maybe with initial error if success or single error was initiated as error and mapper result is not empty`() {
1379+
val initialError = Exception(Uuid.random().toString())
1380+
val newError = Exception(Uuid.random().toString())
1381+
var evaluated = false
1382+
val result =
1383+
SuccessOrSingleError
1384+
.Error
1385+
.of<String, _>(initialError)
1386+
.flatMapSuccessToMaybe {
1387+
evaluated = true
1388+
Maybe.NotEmpty.of(newError)
1389+
}
1390+
.orNull()!!
1391+
1392+
assertSoftly {
1393+
assertFalse { evaluated }
1394+
assertEquals(initialError, result)
1395+
}
1396+
}
1397+
1398+
@Test
1399+
fun `flat map success with maybe error as mapper result must return maybe with initial error if success or single error was initiated as error and mapper result is empty`() {
1400+
val initialError = Exception(Uuid.random().toString())
1401+
var evaluated = false
1402+
val result =
1403+
SuccessOrSingleError
1404+
.Error
1405+
.of<String, _>(initialError)
1406+
.flatMapSuccessToMaybe {
1407+
evaluated = true
1408+
Maybe.Empty.of()
1409+
}
1410+
.orNull()!!
1411+
1412+
assertSoftly {
1413+
assertFalse { evaluated }
1414+
assertEquals(initialError, result)
1415+
}
1416+
}
1417+
1418+
@Test
1419+
fun `flat map success with maybe error as mapper result must return maybe with mapper error if success or single error was initiated as success and mapper result is not empty`() {
1420+
val initialValue = Uuid.random().toString()
1421+
val error = Exception(Uuid.random().toString())
1422+
var evaluated = false
1423+
val result =
1424+
SuccessOrSingleError
1425+
.Success
1426+
.of<_, Throwable>(initialValue)
1427+
.flatMapSuccessToMaybe {
1428+
evaluated = true
1429+
Maybe.NotEmpty.of(error)
1430+
}
1431+
.orNull()!!
1432+
1433+
assertSoftly {
1434+
assertTrue { evaluated }
1435+
assertEquals(error, result)
1436+
}
1437+
}
1438+
1439+
@Test
1440+
fun `flat map success with maybe error as mapper result must return empty maybe if success or single error was initiated as success and mapper result is empty`() {
1441+
val initialValue = Uuid.random().toString()
1442+
var evaluated = false
1443+
val result =
1444+
SuccessOrSingleError
1445+
.Success
1446+
.of<_, Throwable>(initialValue)
1447+
.flatMapSuccessToMaybe {
1448+
evaluated = true
1449+
Maybe.Empty.of()
1450+
}
1451+
.empty()
1452+
1453+
assertSoftly {
1454+
assertTrue { evaluated }
1455+
assertTrue { result }
1456+
}
1457+
}
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+
}
12901484
}

0 commit comments

Comments
 (0)