All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.specs2.matcher.Hamcrest.scala Maven / Gradle / Ivy

package org.specs2
package matcher

import org.hamcrest._
import MatchersImplicits._

/**
 * This trait adds implicit conversions to be able to use [Hamcrest matchers](http://code.google.com/p/hamcrest) as specs2 matchers.
 */
trait Hamcrest {

  /** convert a Hamcrest matcher to a specs2 matcher */
  implicit def asSpecs2Matcher[T](hamcrest: org.hamcrest.Matcher[T]): matcher.Matcher[T] =
    (t: T) => (hamcrest.matches(t), createKoMessageFromHamcrest(t, hamcrest))

  /**
   * @return a string showing the matched value and the failure message from the Hamcrest matcher
   */
  def createKoMessageFromHamcrest[T](t: =>T, hamcrest: org.hamcrest.Matcher[T]): String = {
    val description = new StringDescription

    description
       .appendText("\nExpected: ")
       .appendDescriptionOf(hamcrest)
       .appendText("\n     but: ")
    hamcrest.describeMismatch(t, description)
    description.toString
  }

}

object Hamcrest extends Hamcrest




© 2015 - 2024 Weber Informatics LLC | Privacy Policy