PageRenderTime 12ms CodeModel.GetById 1ms app.highlight 8ms RepoModel.GetById 1ms app.codeStats 0ms

/tests/src/test/scala/scalaz/MonoidTest.scala

http://github.com/scalaz/scalaz
Scala | 61 lines | 48 code | 13 blank | 0 comment | 6 complexity | 803dc24320e0fd586bd57b29fcf9b38f MD5 | raw file
 1package scalaz
 2
 3import std.AllInstances._
 4import org.scalacheck.Prop.forAll
 5
 6object MonoidTest extends SpecLite {
 7  "multiply" ! forAll{ (a: Int, b: Int) =>
 8    if(b <= 0) {
 9      Monoid[Int].multiply(a, b) must_=== 0
10    } else {
11      Monoid[Int].multiply(a, b) must_=== (a * b)
12    }
13  }
14
15  "endo multiply" in {
16    import syntax.monoid._
17
18    def inc(i: Int) = i + 1
19
20    val incTimesThree: Endo[Int] = Endo(inc).multiply(3)
21    incTimesThree(0) must_===(3)
22  }
23
24  "endo kleisli multiply" in {
25    import syntax.monoid._
26
27    val k = Kleisli { i: Int => if (i % 2 == 0) Some(i * 2) else None }
28
29    val kTimes3 = k.endo.multiply(3)
30    kTimes3.run(2) must_=== (Some(16))
31  }
32
33  "unfold" in {
34    val ss = std.stream.unfold(1) {
35      case x if x < 10 => Some((x.toString, x * 2))
36      case _           => None
37    }
38    ss.toList must_===(List("1", "2", "4", "8"))
39  }
40
41  "intercalate empty" in (
42    Foldable[List].intercalate(List[String](), "oops")
43    must_===("")
44  )
45
46  "intercalate" in {
47    val xs = List(Vector(Cord("this"), Cord("has")), Vector(),
48		  Vector(Cord("elements")), Vector(Cord("beneath")), Vector())
49    ((Foldable[List] compose Foldable[Vector]).intercalate(xs, Cord("!!")).toString
50     must_===(Cord("this!!has!!elements!!beneath").toString))
51  }
52
53  "invariant functor" in {
54    import InvariantFunctorTest._
55    import syntax.invariantFunctor._
56
57    val sg: Monoid[Num] = Monoid[Int].xmap[Num](Num.apply _, _.x)
58    sg.append(Num(1), Num(2)) must_===(Num(3))
59    sg.zero must_===(Num(0))
60  }
61}