Skip to content

Commit

Permalink
Added new rapid-test project to provide ScalaTest support for async s…
Browse files Browse the repository at this point in the history
…pecs
  • Loading branch information
darkfrog26 committed Dec 20, 2024
1 parent 84ce4fa commit b579984
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 2 deletions.
14 changes: 12 additions & 2 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ val developerURL: String = "https://matthicks.com"

name := projectName
ThisBuild / organization := org
ThisBuild / version := "0.3.1"
ThisBuild / version := "0.3.2-SNAPSHOT"
ThisBuild / scalaVersion := scala213
ThisBuild / crossScalaVersions := allScalaVersions
ThisBuild / scalacOptions ++= Seq("-unchecked", "-deprecation", "-feature")
Expand Down Expand Up @@ -54,7 +54,7 @@ val scalaJsMacrotaskVersion: String = "1.1.1"
val scalaTestVersion: String = "3.2.19"

lazy val root = project.in(file("."))
.aggregate(core.jvm, core.js, core.native, cats.jvm, cats.js)
.aggregate(core.jvm, core.js, core.native, test.jvm, test.js, test.native, cats.jvm, cats.js)
.settings(
name := projectName,
publish := {},
Expand All @@ -75,6 +75,16 @@ lazy val core = crossProject(JVMPlatform, JSPlatform, NativePlatform)
)
)

lazy val test = crossProject(JVMPlatform, JSPlatform, NativePlatform)
.crossType(CrossType.Full)
.dependsOn(core)
.settings(
name := s"$projectName-test",
libraryDependencies ++= Seq(
"org.scalatest" %%% "scalatest" % scalaTestVersion
)
)

lazy val cats = crossProject(JVMPlatform, JSPlatform)
.crossType(CrossType.Full)
.dependsOn(core)
Expand Down
6 changes: 6 additions & 0 deletions core/shared/src/main/scala/rapid/Task.scala
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,12 @@ trait Task[Return] extends Any {
* @return a new task that returns `Unit` after the existing task completes
*/
def unit: Task[Unit] = map(_ => ())

/**
* Provides convenience functionality to execute this Task as a scala.concurrent.Future.
*/
def toFuture(implicit ec: scala.concurrent.ExecutionContext): scala.concurrent.Future[Return] =
scala.concurrent.Future(this.sync())
}

object Task {
Expand Down
10 changes: 10 additions & 0 deletions test/shared/src/main/scala/rapid/AsyncTaskSpec.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package rapid

import scala.concurrent.Future

import scala.language.implicitConversions

trait AsyncTaskSpec {
implicit def task2Future[Return](task: Task[Return])
(implicit ec: scala.concurrent.ExecutionContext): Future[Return] = task.toFuture
}
54 changes: 54 additions & 0 deletions test/shared/src/test/scala/spec/BasicsAsyncSpec.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package spec

import org.scalatest.matchers.should.Matchers
import org.scalatest.wordspec.{AnyWordSpec, AsyncWordSpec}
import rapid._

class BasicsAsyncSpec extends AsyncWordSpec with AsyncTaskSpec with Matchers {
"Basics sync" should {
"handle a simple task" in {
val i = Task {
5 * 5
}
i.map { n =>
n should be(25)
}
}
"handle a simple task mapping" in {
val i = Task {
5 * 5
}
val s = i.map { v =>
s"Value: $v"
}
s.map { s =>
s should be("Value: 25")
}
}
"handle flat mapping" in {
val task = (1 to 10).foldLeft(Task(0))((t, i) => t.flatMap { total =>
Task(total + i)
})
task.map { result =>
result should be(55)
}
}
"throw an error and recover" in {
val result = Task[String](throw new RuntimeException("Die Die Die"))
.handleError { _ =>
Task.pure("Recovered")
}
result map { s =>
s should be("Recovered")
}
}
"process a list of tasks to a task with a list" in {
val list = List(
Task("One"), Task("Two"), Task("Three")
)
list.tasks.map { list =>
list should be(List("One", "Two", "Three"))
}
}
}
}

0 comments on commit b579984

Please sign in to comment.