Skip to content

Commit

Permalink
Test and document handleError and handleErrorWith
Browse files Browse the repository at this point in the history
  • Loading branch information
makiftutuncu committed Feb 17, 2020
1 parent d5f1cb6 commit 4c1e759
Show file tree
Hide file tree
Showing 9 changed files with 308 additions and 40 deletions.
38 changes: 38 additions & 0 deletions docs/e-scala/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,44 @@ sb1.toString()

sb2.toString()

/*****************************/
/* Handling error of a Maybe */
/*****************************/

E("error-1").toMaybe[Int].handleError { case _ => 0 }

E("error-1").toMaybe[Int].handleError { case e if e.hasName => 0 }

E().toMaybe[Int].handleError { case e if e.hasName => 0 }

5.toMaybe.handleError { case _ => 0 }

5.toMaybe.handleError { case e if e.hasName => 0 }

/************************************************/
/* Handling error of a Maybe with another Maybe */
/************************************************/

E("error-1").toMaybe[Int].handleErrorWith { case _ => E().toMaybe[Int] }

E("error-1").toMaybe[Int].handleErrorWith { case _ => 5.toMaybe }

E("error-1").toMaybe[Int].handleErrorWith { case e if e.hasName => 0.toMaybe }

E("error-1").toMaybe[Int].handleErrorWith { case e if e.hasName => E().toMaybe[Int] }

E().toMaybe[Int].handleErrorWith { case e if e.hasName => 0.toMaybe }

E().toMaybe[Int].handleErrorWith { case e if e.hasName => E("error-1").toMaybe[Int] }

5.toMaybe.handleErrorWith { case _ => E().toMaybe[Int] }

5.toMaybe.handleErrorWith { case _ => 0.toMaybe }

5.toMaybe.handleErrorWith { case e if e.hasName => E().toMaybe[Int] }

5.toMaybe.handleErrorWith { case e if e.hasName => 0.toMaybe }

/************************************/
/* Constructing a Maybe from Option */
/************************************/
Expand Down
44 changes: 44 additions & 0 deletions e-java/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,50 @@ String forEach1 = sb1.toString();
String forEach2 = sb2.toString();
// "test"

/*****************************/
/* Handling error of a Maybe */
/*****************************/

Maybe<Integer> handled1 = new E("error").toMaybe().handleError(e -> 0);
// 0

Maybe<Integer> handled2 = new E("error").code(1).toMaybe().handleError(AbstracE::code);
// 1

Maybe<Integer> handled3 = Maybe.success(5).handleError(e -> 0);
// 5

Maybe<Integer> handled4 = Maybe.success(5).handleError(AbstractE::code);
// 5

/************************************************/
/* Handling error of a Maybe with another Maybe */
/************************************************/

Maybe<Integer> handledWith1 = new E("error-1").toMaybe().handleErrorWith(e -> new E().toMaybe());
// {}

Maybe<Integer> handledWith2 = new E("error-1").toMaybe().handleErrorWith(e -> Maybe.success(5));
// 5

Maybe<Integer> handledWith3 = new E("error-1").toMaybe().handleErrorWith(e -> e.code(1).toMaybe());
// {"name":"error-1","code":1}

Maybe<Integer> handledWith4 = new E("error-1").code(1).toMaybe().handleErrorWith(e -> Maybe.success(e.code()));
// 1

Maybe<Integer> handledWith5 = Maybe.success(5).handleErrorWith(e -> new E().toMaybe());
// 5

Maybe<Integer> handledWith5 = Maybe.success(5).handleErrorWith(e -> new E("error-1").toMaybe());
// 5

Maybe<Integer> handledWith5 = Maybe.success(5).handleErrorWith(e -> e.code(1).toMaybe());
// 5

Maybe<Integer> handledWith5 = Maybe.success(5).handleErrorWith(e -> Maybe.success(e.code()));
// 5

```

## Encoder
Expand Down
27 changes: 27 additions & 0 deletions e-java/src/test/java/e/java/MaybeTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,33 @@ public class MaybeTest {
assertEquals("test", sb2.toString());
}

@Test void testHandlingWithAnotherMaybe() {
Maybe<Integer> maybe1 = new E("error-1").code(1).toMaybe();
Maybe<Integer> maybe2 = new E().toMaybe();
Maybe<Integer> maybe3 = Maybe.success(5);

assertEquals(maybe2, maybe1.handleErrorWith(e -> maybe2));
assertEquals(maybe3, maybe1.handleErrorWith(e -> maybe3));
assertEquals(new E("error-1").code(2).toMaybe(), maybe1.handleErrorWith(e -> e.code(2).toMaybe()));
assertEquals(Maybe.success(1), maybe1.handleErrorWith(e -> Maybe.success(e.code())));

assertEquals(maybe3, maybe3.handleErrorWith(e -> maybe2));
assertEquals(maybe3, maybe3.handleErrorWith(e -> maybe1));
assertEquals(maybe3, maybe3.handleErrorWith(e -> e.code(1).toMaybe()));
assertEquals(maybe3, maybe3.handleErrorWith(e -> Maybe.success(e.code())));
}

@Test void testHandling() {
Maybe<Integer> maybe1 = new E("error").code(1).toMaybe();
Maybe<Integer> maybe2 = Maybe.success(5);

assertEquals(Maybe.success(0), maybe1.handleError(e -> 0));
assertEquals(Maybe.success(1), maybe1.handleError(AbstractE::code));

assertEquals(maybe2, maybe2.handleError(e -> 0));
assertEquals(maybe2, maybe2.handleError(AbstractE::code));
}

@Test void testEquality() {
Maybe<String> maybe1 = Maybe.failure(new E("test-1"));
Maybe<String> maybe2 = Maybe.failure(new E("test-1"));
Expand Down
44 changes: 44 additions & 0 deletions e-kotlin/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,50 @@ sb1.toString()
sb2.toString()
// "test"

/*****************************/
/* Handling error of a Maybe */
/*****************************/

E("error").code(1).toMaybe<Int>().handleError { 0 }
// 0

E("error").code(1).toMaybe<Int>().handleError { e -> e.code() }
// 1

5.toMaybe().handleError { 0 }
// 5

5.toMaybe().handleError { e -> e.code() }
// 5

/************************************************/
/* Handling error of a Maybe with another Maybe */
/************************************************/

E("error-1").code(1).toMaybe<Int>().handleErrorWith { E().toMaybe<Int>() }
// {}

E("error-1").code(1).toMaybe<Int>().handleErrorWith { 5.toMaybe() }
// 5

E("error-1").code(1).toMaybe<Int>().handleErrorWith { e -> e.code(2).toMaybe() }
// {"name":"error-1","code":1}

E("error-1").code(1).toMaybe<Int>().handleErrorWith { e -> e.code().toMaybe() }
// 1

5.toMaybe().handleErrorWith { E().toMaybe<Int>() }
// 5

5.toMaybe().handleErrorWith { E("error-1").code(1).toMaybe<Int>() }
// 5

5.toMaybe().handleErrorWith { e -> e.code(1).toMaybe() }
// 5

5.toMaybe().handleErrorWith { e -> e.code().toMaybe() }
// 5

/****************************************/
/* Constructing a Maybe from a nullable */
/****************************************/
Expand Down
27 changes: 27 additions & 0 deletions e-kotlin/src/test/kotlin/e/kotlin/MaybeTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,33 @@ object MaybeTest {
assertEquals("test", sb2.toString())
}

@Test fun `test handling error of Maybe with another Maybe`() {
val maybe1 = E("error-1").code(1).toMaybe<Int>()
val maybe2 = E().toMaybe<Int>()
val maybe3 = 5.toMaybe()

assertEquals(maybe2, maybe1.handleErrorWith { maybe2 })
assertEquals(maybe3, maybe1.handleErrorWith { maybe3 })
assertEquals(E("error-1").code(2).toMaybe<Int>(), maybe1.handleErrorWith { e -> e.code(2).toMaybe() })
assertEquals(1.toMaybe(), maybe1.handleErrorWith { e -> e.code().toMaybe() })

assertEquals(maybe3, maybe3.handleErrorWith { maybe2 })
assertEquals(maybe3, maybe3.handleErrorWith { maybe1 })
assertEquals(maybe3, maybe3.handleErrorWith { e -> e.code(1).toMaybe() })
assertEquals(maybe3, maybe3.handleErrorWith { e -> e.code().toMaybe() })
}

@Test fun `test handling error of Maybe`() {
val maybe1 = E("error").code(1).toMaybe<Int>()
val maybe2 = 5.toMaybe()

assertEquals(0.toMaybe(), maybe1.handleError { 0 })
assertEquals(1.toMaybe(), maybe1.handleError { e -> e.code() })

assertEquals(maybe2, maybe2.handleError { 0 })
assertEquals(maybe2, maybe2.handleError { e -> e.code() })
}

@Test fun `test equality`() {
val e1 = E("test-name")
val e2 = E(message = "Test Message")
Expand Down
Loading

0 comments on commit 4c1e759

Please sign in to comment.