This repository has been archived by the owner on Feb 16, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 22
/
DisjunctionMatchers.scala
121 lines (102 loc) · 3.96 KB
/
DisjunctionMatchers.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
package org.specs2.scalaz
import scalaz.{ -\/, \/, \/- }
import org.specs2.matcher._
import org.specs2.text.Quote._
import org.specs2.execute.{ Failure, Result }
trait DisjunctionMatchers { outer =>
def beRightDisjunction[T](t: => T) =
new Matcher[\/[_, T]] {
def apply[S <: \/[_, T]](value: Expectable[S]) = {
val expected = t
result(
value.value == \/.right(t),
value.description + " is \\/- with value" + q(expected),
value.description + " is not \\/- with value" + q(expected),
value
)
}
}
def beRightDisjunction[T] = new RightDisjunctionMatcher[T]
class RightDisjunctionMatcher[T] extends Matcher[\/[_, T]] {
def apply[S <: \/[_, T]](value: Expectable[S]) = {
result(
value.value.isRight,
value.description + " is \\/-",
value.description + " is not \\/-",
value
)
}
def like(f: PartialFunction[T, MatchResult[_]]) = this and partialMatcher(f)
private def partialMatcher(f: PartialFunction[T, MatchResult[_]]) = new Matcher[\/[_, T]] {
def apply[S <: \/[_, T]](value: Expectable[S]) = {
val res: Result = value.value match {
case \/-(t) if f.isDefinedAt(t) => f(t).toResult
case \/-(t) if !f.isDefinedAt(t) => Failure("function undefined")
case other => Failure("no match")
}
result(
res.isSuccess,
value.description + " is \\/-[T] and " + res.message,
value.description + " is \\/-[T] but " + res.message,
value
)
}
}
}
def rightDisjunction[T](t: => T) = beRightDisjunction(t)
def rightDisjunction[T] = beRightDisjunction
def beLeftDisjunction[T](t: => T) = new Matcher[\/[T, _]] {
def apply[S <: \/[T, _]](value: Expectable[S]) = {
val expected = t
result(
value.value == \/.left(t),
value.description + " is -\\/ with value" + q(expected),
value.description + " is not -\\/ with value" + q(expected),
value
)
}
}
def beLeftDisjunction[T] = new LeftDisjunctionMatcher[T]
class LeftDisjunctionMatcher[T] extends Matcher[\/[T, _]] {
def apply[S <: \/[T, _]](value: Expectable[S]) = {
result(
value.value.isLeft,
value.description + " is -\\/",
value.description + " is not -\\/",
value
)
}
def like(f: PartialFunction[T, MatchResult[_]]) = this and partialMatcher(f)
private def partialMatcher(f: PartialFunction[T, MatchResult[_]]) = new Matcher[\/[T, _]] {
def apply[S <: \/[T, _]](value: Expectable[S]) = {
val res: Result = value.value match {
case -\/(t) if f.isDefinedAt(t) => f(t).toResult
case -\/(t) if !f.isDefinedAt(t) => Failure("function undefined")
case other => Failure("no match")
}
result(
res.isSuccess,
value.description + " is -\\/[T] and " + res.message,
value.description + " is -\\/[T] but " + res.message,
value
)
}
}
}
def leftDisjunction[T](t: => T) = beLeftDisjunction(t)
def leftDisjunction[T] = beLeftDisjunction
implicit def toDisjunctionResultMatcher[F, S](result: MatchResult[\/[F, S]]) =
new DisjunctionResultMatcher(result)
class DisjunctionResultMatcher[F, S](result: MatchResult[\/[F, S]]) {
def leftDisjunction(f: => F) = result(outer beLeftDisjunction f)
def beLeftDisjunction(f: => F) = result(outer beLeftDisjunction f)
def rightDisjunction(s: => S) = result(outer beRightDisjunction s)
def beRightDisjunction(s: => S) = result(outer beRightDisjunction s)
def leftDisjunction = result(outer.beLeftDisjunction)
def beLeftDisjunction = result(outer.beLeftDisjunction)
def rightDisjunction = result(outer.beRightDisjunction)
def beRightDisjunction = result(outer.beRightDisjunction)
}
}
object DisjunctionMatchers extends DisjunctionMatchers
// vim: expandtab:ts=2:sw=2