Skip to content

Instantly share code, notes, and snippets.

@tomaszperek
Created October 10, 2015 11:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tomaszperek/7580af3074ae7f763304 to your computer and use it in GitHub Desktop.
Save tomaszperek/7580af3074ae7f763304 to your computer and use it in GitHub Desktop.
ScalaTest specification illustrating how zip function works
class ZipSpec extends FlatSpec with Matchers with ScalaFutures {
import shapeless._
import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.Future._
"zip" should "provide one future from args of futures" in {
val result = zip(successful(1), successful(true), successful("string"), successful(1.0))
val (a, b, c, d) = result.futureValue
(a, b, c, d) should equal((1, true, "string", 1.0))
}
it should "work well with failures" in {
val exception: Exception = new scala.Exception("booo")
val result = zip(successful(1), successful(true), failed(exception), successful(1.0))
whenReady(result.failed) { ex ⇒
ex should equal(exception)
}
}
it should "allow you to go crazy" in {
val (a, b, c, d, e, f, g, h, i, j, k, l, m, n) = zip(
successful(1),
successful(2),
successful(false),
successful(true),
successful("someString"),
successful(BigDecimal(0.0)),
successful(1.1),
successful('andSymbolsToo),
successful("future havoc"),
successful(10),
successful(11),
successful(12),
successful(13),
successful("enough already")
).futureValue
a should equal(1)
b should equal(2)
c should equal(false)
d should equal(true)
e should equal("someString")
f should equal(BigDecimal(0.0))
g should equal(1.1)
h should equal('andSymbolsToo)
i should equal("future havoc")
j should equal(10)
k should equal(11)
l should equal(12)
m should equal(13)
n should equal("enough already")
}
it should "not compile if hlist has non-future element" in {
"""val result = zip(successful(1), successful(true), successful("Some string"))""" should compile
"""val result = zip(successful(1), successful(true), "Some string")""" shouldNot compile
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment