Skip to content

Commit 0c9c09b

Browse files
committed
Close #521 - Add support for Scala Native
1 parent 2be3a0e commit 0c9c09b

47 files changed

Lines changed: 9617 additions & 108 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

build.sbt

Lines changed: 200 additions & 84 deletions
Large diffs are not rendered by default.

modules/effectie-cats-effect2/shared/src/test/scala-2/effectie/syntax/fxIdSpec.scala

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,13 @@ import effectie.core.{Fx, FxCtor}
66
import effectie.syntax.fx._
77
import effectie.testing.tools.{dropResult, expectThrowable}
88
import effectie.testing.types.SomeThrowableError
9-
import extras.concurrent.testing.types.ErrorLogger
109
import hedgehog._
1110
import hedgehog.runner._
1211

1312
/** @author Kevin Lee
1413
* @since 2021-05-16
1514
*/
1615
object fxIdSpec extends Properties {
17-
implicit val errorLogger: ErrorLogger[Throwable] = ErrorLogger.printlnDefaultErrorLogger
1816

1917
override def tests: List[Test] = List(
2018
property("test fx.{effectOf, pureOf, unitOf} for Id", IdSpec.testAll),

modules/effectie-cats-effect2/shared/src/test/scala-3/effectie/syntax/fxIdSpec.scala

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,13 @@ import effectie.core.{Fx, FxCtor}
66
import effectie.syntax.fx.*
77
import effectie.testing.tools.{dropResult, expectThrowable}
88
import effectie.testing.types.SomeThrowableError
9-
import extras.concurrent.testing.types.ErrorLogger
109
import hedgehog.*
1110
import hedgehog.runner.*
1211

1312
/** @author Kevin Lee
1413
* @since 2021-05-16
1514
*/
1615
object fxIdSpec extends Properties {
17-
implicit val errorLogger: ErrorLogger[Throwable] = ErrorLogger.printlnDefaultErrorLogger
1816

1917
override def tests: List[Test] = List(
2018
property("test fx.{effectOf, pureOf, unitOf} for Id", IdSpec.testAll),

modules/effectie-cats-effect2/shared/src/test/scala/effectie/instances/ce2/canHandleErrorIdSpec.scala

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import effectie.core._
88
import effectie.syntax.error._
99
import effectie.syntax.fx._
1010
import effectie.testing.types.SomeError
11-
import extras.concurrent.testing.types.ErrorLogger
1211
import hedgehog._
1312
import hedgehog.runner._
1413

@@ -19,8 +18,6 @@ import scala.util.control.ControlThrowable
1918
*/
2019
object canHandleErrorIdSpec extends Properties {
2120

22-
implicit val errorLogger: ErrorLogger[Throwable] = ErrorLogger.printlnDefaultErrorLogger
23-
2421
override def tests: List[Test] = List(
2522
example(
2623
"test CanHandleError[Id].handleNonFatalWith should handle NonFatal",

modules/effectie-cats-effect2/shared/src/test/scala/effectie/instances/ce2/canRecoverIdSpec.scala

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import effectie.core._
88
import effectie.syntax.error._
99
import effectie.syntax.fx._
1010
import effectie.testing.types.SomeError
11-
import extras.concurrent.testing.types.ErrorLogger
1211
import hedgehog._
1312
import hedgehog.runner._
1413

@@ -19,8 +18,6 @@ import scala.util.control.{ControlThrowable, NonFatal}
1918
*/
2019
object canRecoverIdSpec extends Properties {
2120

22-
implicit val errorLogger: ErrorLogger[Throwable] = ErrorLogger.printlnDefaultErrorLogger
23-
2421
override def tests: List[Test] = List(
2522
/* Id */
2623
example(
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package effectie.instances.id
2+
3+
import cats.Id
4+
import effectie.core.FromFuture
5+
import effectie.core.FromFuture.FromFutureToIdTimeout
6+
7+
import scala.concurrent.{Await, Future}
8+
9+
/** @author Kevin Lee
10+
* @since 2020-09-22
11+
*/
12+
object fromFuture {
13+
14+
@SuppressWarnings(Array("org.wartremover.warts.ImplicitParameter"))
15+
implicit def fromFutureToId(implicit timeout: FromFutureToIdTimeout): FromFuture[Id] =
16+
new FromFuture[Id] {
17+
override def toEffect[A](future: => Future[A]): Id[A] =
18+
Await.result[A](future, timeout.fromFutureToIdTimeout)
19+
}
20+
21+
}
Lines changed: 222 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,222 @@
1+
package effectie.instances.future
2+
3+
import cats.data.EitherT
4+
import cats.syntax.all._
5+
import effectie.core._
6+
import effectie.syntax.all._
7+
import effectie.testing.types._
8+
import extras.concurrent.testing.ConcurrentSupport
9+
import extras.concurrent.testing.types.{ErrorLogger, WaitFor}
10+
import hedgehog._
11+
import hedgehog.runner._
12+
13+
/** @author Kevin Lee
14+
* @since 2020-07-31
15+
*/
16+
object canCatchFutureSpec extends Properties {
17+
18+
private implicit val errorLogger: ErrorLogger[Throwable] = ErrorLogger.printlnDefaultErrorLogger
19+
20+
override def tests: List[Test] = futureSpecs
21+
22+
/* Future */
23+
val futureSpecs = List(
24+
example(
25+
"test CanCatch[Future]catchNonFatalEitherT should catch NonFatal",
26+
FutureSpec.testCanCatch_Future_catchNonFatalEitherTShouldCatchNonFatal,
27+
),
28+
example(
29+
"test CanCatch[Future]catchNonFatalEitherT should return the successful result",
30+
FutureSpec.testCanCatch_Future_catchNonFatalEitherTShouldReturnSuccessfulResult,
31+
),
32+
example(
33+
"test CanCatch[Future]catchNonFatalEitherT should return the failed result",
34+
FutureSpec.testCanCatch_Future_catchNonFatalEitherTShouldReturnFailedResult,
35+
),
36+
)
37+
38+
@SuppressWarnings(Array("org.wartremover.warts.Throw"))
39+
def throwThrowable[A](throwable: => Throwable): A =
40+
throw throwable // scalafix:ok DisableSyntax.throw
41+
42+
def run[F[*]: FxCtor, A](a: => A): F[A] =
43+
FxCtor[F].effectOf(a)
44+
45+
object FutureSpec {
46+
import effectie.instances.future.canCatch._
47+
import effectie.instances.future.fxCtor._
48+
49+
import java.util.concurrent.{ExecutorService, Executors}
50+
import scala.concurrent.duration._
51+
import scala.concurrent.{ExecutionContext, Future}
52+
53+
val waitFor = WaitFor(1.second)
54+
55+
// def testCanCatch_Future_catchNonFatalThrowableShouldCatchNonFatal: Result = {
56+
//
57+
// implicit val executorService: ExecutorService = Executors.newFixedThreadPool(1)
58+
// implicit val ec: ExecutionContext =
59+
// ConcurrentSupport.newExecutionContext(executorService, ErrorLogger.printlnExecutionContextErrorLogger)
60+
//
61+
// val expectedExpcetion = new RuntimeException("Something's wrong")
62+
// val fa = run[Future, Int](throwThrowable[Int](expectedExpcetion))
63+
// val expected = expectedExpcetion.asLeft[Int]
64+
// val actual = ConcurrentSupport.futureToValueAndTerminate(
65+
// executorService,
66+
// waitFor
67+
// )(CanCatch[Future].catchNonFatalThrowable(fa))
68+
//
69+
// actual ==== expected
70+
// }
71+
//
72+
// def testCanCatch_Future_catchNonFatalShouldCatchNonFatal: Result = {
73+
//
74+
// implicit val executorService: ExecutorService = Executors.newFixedThreadPool(1)
75+
// implicit val ec: ExecutionContext =
76+
// ConcurrentSupport.newExecutionContext(executorService, ErrorLogger.printlnExecutionContextErrorLogger)
77+
//
78+
// val expectedExpcetion = new RuntimeException("Something's wrong")
79+
// val fa = run[Future, Int](throwThrowable[Int](expectedExpcetion))
80+
// val expected = SomeError.someThrowable(expectedExpcetion).asLeft[Int]
81+
// val actual = ConcurrentSupport.futureToValueAndTerminate(
82+
// executorService,
83+
// waitFor
84+
// )(CanCatch[Future].catchNonFatal(fa)(SomeError.someThrowable))
85+
//
86+
// actual ==== expected
87+
// }
88+
//
89+
// def testCanCatch_Future_catchNonFatalThrowableShouldReturnSuccessfulResult: Result = {
90+
//
91+
// implicit val executorService: ExecutorService = Executors.newFixedThreadPool(1)
92+
// implicit val ec: ExecutionContext =
93+
// ConcurrentSupport.newExecutionContext(executorService, ErrorLogger.printlnExecutionContextErrorLogger)
94+
//
95+
// val fa = run[Future, Int](1)
96+
// val expected = 1.asRight[Throwable]
97+
// val actual = ConcurrentSupport.futureToValueAndTerminate(
98+
// executorService,
99+
// waitFor
100+
// )(CanCatch[Future].catchNonFatalThrowable(fa))
101+
//
102+
// actual ==== expected
103+
// }
104+
//
105+
// def testCanCatch_Future_catchNonFatalShouldReturnSuccessfulResult: Result = {
106+
//
107+
// implicit val executorService: ExecutorService = Executors.newFixedThreadPool(1)
108+
// implicit val ec: ExecutionContext =
109+
// ConcurrentSupport.newExecutionContext(executorService, ErrorLogger.printlnExecutionContextErrorLogger)
110+
//
111+
// val fa = run[Future, Int](1)
112+
// val expected = 1.asRight[SomeError]
113+
// val actual = ConcurrentSupport.futureToValueAndTerminate(
114+
// executorService,
115+
// waitFor
116+
// )(CanCatch[Future].catchNonFatal(fa)(SomeError.someThrowable))
117+
//
118+
// actual ==== expected
119+
// }
120+
//
121+
// def testCanCatch_Future_catchNonFatalEitherShouldCatchNonFatal: Result = {
122+
//
123+
// implicit val executorService: ExecutorService = Executors.newFixedThreadPool(1)
124+
// implicit val ec: ExecutionContext =
125+
// ConcurrentSupport.newExecutionContext(executorService, ErrorLogger.printlnExecutionContextErrorLogger)
126+
//
127+
// val expectedExpcetion = new RuntimeException("Something's wrong")
128+
// val fa = run[Future, Either[SomeError, Int]](throwThrowable[Either[SomeError, Int]](expectedExpcetion))
129+
// val expected = SomeError.someThrowable(expectedExpcetion).asLeft[Int]
130+
// val actual = ConcurrentSupport.futureToValueAndTerminate(
131+
// executorService,
132+
// waitFor
133+
// )(CanCatch[Future].catchNonFatalEither(fa)(SomeError.someThrowable))
134+
//
135+
// actual ==== expected
136+
// }
137+
//
138+
// def testCanCatch_Future_catchNonFatalEitherShouldReturnSuccessfulResult: Result = {
139+
//
140+
// implicit val executorService: ExecutorService = Executors.newFixedThreadPool(1)
141+
// implicit val ec: ExecutionContext =
142+
// ConcurrentSupport.newExecutionContext(executorService, ErrorLogger.printlnExecutionContextErrorLogger)
143+
//
144+
// val fa = run[Future, Either[SomeError, Int]](1.asRight[SomeError])
145+
// val expected = 1.asRight[SomeError]
146+
// val actual = ConcurrentSupport.futureToValueAndTerminate(
147+
// executorService,
148+
// waitFor
149+
// )(CanCatch[Future].catchNonFatalEither(fa)(SomeError.someThrowable))
150+
//
151+
// actual ==== expected
152+
// }
153+
//
154+
// def testCanCatch_Future_catchNonFatalEitherShouldReturnFailedResult: Result = {
155+
//
156+
// implicit val executorService: ExecutorService = Executors.newFixedThreadPool(1)
157+
// implicit val ec: ExecutionContext =
158+
// ConcurrentSupport.newExecutionContext(executorService, ErrorLogger.printlnExecutionContextErrorLogger)
159+
//
160+
// val expectedFailure = SomeError.message("Failed")
161+
// val fa = run[Future, Either[SomeError, Int]](expectedFailure.asLeft[Int])
162+
// val expected = expectedFailure.asLeft[Int]
163+
// val actual = ConcurrentSupport.futureToValueAndTerminate(
164+
// executorService,
165+
// waitFor
166+
// )(CanCatch[Future].catchNonFatalEither(fa)(SomeError.someThrowable))
167+
//
168+
// actual ==== expected
169+
// }
170+
171+
def testCanCatch_Future_catchNonFatalEitherTShouldCatchNonFatal: Result = {
172+
173+
implicit val executorService: ExecutorService = Executors.newFixedThreadPool(1)
174+
implicit val ec: ExecutionContext =
175+
ConcurrentSupport.newExecutionContext(executorService, ErrorLogger.printlnExecutionContextErrorLogger)
176+
177+
val expectedExpcetion = new RuntimeException("Something's wrong")
178+
val fa = EitherT(run[Future, Either[SomeError, Int]](throwThrowable[Either[SomeError, Int]](expectedExpcetion)))
179+
val expected = SomeError.someThrowable(expectedExpcetion).asLeft[Int]
180+
val actual = ConcurrentSupport.futureToValueAndTerminate(
181+
executorService,
182+
waitFor,
183+
)(CanCatch[Future].catchNonFatalEitherT(fa)(SomeError.someThrowable).value)
184+
185+
actual ==== expected
186+
}
187+
188+
def testCanCatch_Future_catchNonFatalEitherTShouldReturnSuccessfulResult: Result = {
189+
190+
implicit val executorService: ExecutorService = Executors.newFixedThreadPool(1)
191+
implicit val ec: ExecutionContext =
192+
ConcurrentSupport.newExecutionContext(executorService, ErrorLogger.printlnExecutionContextErrorLogger)
193+
194+
val fa = EitherT(run[Future, Either[SomeError, Int]](1.asRight[SomeError]))
195+
val expected = 1.asRight[SomeError]
196+
val actual = ConcurrentSupport.futureToValueAndTerminate(
197+
executorService,
198+
waitFor,
199+
)(CanCatch[Future].catchNonFatalEitherT(fa)(SomeError.someThrowable).value)
200+
201+
actual ==== expected
202+
}
203+
204+
def testCanCatch_Future_catchNonFatalEitherTShouldReturnFailedResult: Result = {
205+
206+
implicit val executorService: ExecutorService = Executors.newFixedThreadPool(1)
207+
implicit val ec: ExecutionContext =
208+
ConcurrentSupport.newExecutionContext(executorService, ErrorLogger.printlnExecutionContextErrorLogger)
209+
210+
val expectedFailure = SomeError.message("Failed")
211+
val fa = EitherT(run[Future, Either[SomeError, Int]](expectedFailure.asLeft[Int]))
212+
val expected = expectedFailure.asLeft[Int]
213+
val actual = ConcurrentSupport.futureToValueAndTerminate(
214+
executorService,
215+
waitFor,
216+
)(CanCatch[Future].catchNonFatalEitherT(fa)(SomeError.someThrowable).value)
217+
218+
actual ==== expected
219+
}
220+
}
221+
222+
}

0 commit comments

Comments
 (0)