EECOLOR / little-spec

MIT License
6 stars 2 forks source link

Custom matchers #4

Open EECOLOR opened 9 years ago

EECOLOR commented 9 years ago

Should we collect them in a separate library?

  def endWith(expected: String) =
    new Assertion[String] {
      def assert(s: => String) =
        if (s.endsWith(expected)) Right(success)
        else Left("The given string did not end with " + expected)
    }
Hayena commented 9 years ago
  private val beRight =
    new Assertion[Either[_, _]] {
      def assert(s: => Either[_, _]) =
        if (s.isRight) Success(success)
        else Failure("Expected Right")
    }

  private val beLeft: Assertion[Either[Any, _]] = beLeft(None)
  private def beLeft[T](expected: => T): Assertion[Either[T, _]] = beLeft(Some(expected))

  private def beLeft[T](expected: => Option[T]) =
    new Assertion[Either[T, _]] {
      def assert(s: => Either[T, _]) =
        s match {
          case Left(value) => Success(expected map (value is _) getOrElse success)
          case _ => Failure("Expected Left")
        }
    }