Skip to content

Commit

Permalink
a 'MatcherFactory1' design
Browse files Browse the repository at this point in the history
- this fails because the TC parameter should be a type member to avoid
  scala/bug#5075 but it is not
  • Loading branch information
S11001001 committed May 28, 2019
1 parent 4a31fe9 commit be16753
Showing 1 changed file with 11 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// SPDX-License-Identifier: Apache-2.0

import org.scalatest.Matchers
import org.scalatest.matchers.{MatchResult, Matcher}
import org.scalatest.matchers.{MatchResult, Matcher, MatcherFactory1}
import scalaz.Equal

/** Provides the `equalz` [[Matcher]].
Expand All @@ -27,27 +27,19 @@ import scalaz.Equal
trait Equalz extends Matchers {
import Equalz.{EqualzInvocation, Lub}

private[this] final def equalzImpl[A](expected: A)(implicit A: Equal[A]): Matcher[A] =
actual =>
MatchResult(
A.equal(expected, actual),
s"$actual did not equal $expected",
s"$actual equalled $expected"
)

def equalz[Ac](ac: Ac): EqualzInvocation[Ac] = new EqualzInvocation(ac)

implicit final class AnyShouldzWrapper[Ex](private val expected: Ex) {
def should[Ac, T](actual: EqualzInvocation[Ac])(implicit lub: Lub[Ex, Ac, T], eq: Equal[T]) =
(lub.left(expected): AnyShouldWrapper[T]) should equalzImpl(lub.right(actual.actual))
def shouldNot[Ac, T](actual: EqualzInvocation[Ac])(implicit lub: Lub[Ex, Ac, T], eq: Equal[T]) =
(lub.left(expected): AnyShouldWrapper[T]) shouldNot equalzImpl(lub.right(actual.actual))
}
final def equalz[Ex](expected: Ex): MatcherFactory1[Ex, Equal] =
new MatcherFactory1[Ex, Equal] {
override def matcher[T <: Ex](implicit ev: Equal[T]): Matcher[T] =
actual =>
MatchResult(
ev.equal(expected, actual),
s"$actual did not equal $expected",
s"$actual equalled $expected"
)
}
}

object Equalz extends Equalz {
final class EqualzInvocation[+Ac](private[Equalz] val actual: Ac) extends AnyVal

sealed abstract class Lub[-A, -B, C] {
def left(l: A): C
def right(r: B): C
Expand Down

0 comments on commit be16753

Please sign in to comment.