Code Monkey home page Code Monkey logo

Comments (2)

etorreborre avatar etorreborre commented on June 3, 2024

I actually don't know how to do it differently with that kind of DSL. The second matcher, that is chained with and, would have to know that the first one was an exception matcher and the result of the previous match would need to remember which exception was thrown.

I would say that in that case you are better off catching the exception and then checking it by yourself.

If you need some encapsulation in a Matcher you can write the following

import org.specs2.matcher._
import scala.reflect.ClassTag

class MySpec extends mutable.Specification {
  def throwIt(): Unit = {
    println("OH NO")
    throw new IllegalArgumentException("foo")
  }

  "not double-invoke throwable" >> {
    throwIt() must throwAnException[IllegalArgumentException];
  }

  def throwAnException[E <: Throwable](implicit c: ClassTag[E]): Matcher[Any] =
    new Matcher[Any] {
      def apply[S <: Any](s: Expectable[S]) = {
        // catch the exception if necessary
        val exception: Option[Throwable] = try { s.evaluate; None } catch { case e: Throwable => Some (e) };
        
        // re-throw the exception locally and use normal matchers
        // in this case the exception is thrown twice but it's not a big deal
        lazy val e = exception.foreach(e => throw e)
        val result: MatchResult[Unit] = e must (throwA[Throwable] and throwAn[E](c))

        // a bit of massaging is necessary to return a typed result
        result.asInstanceOf[MatchResult[Nothing]].setExpectable(s)
      }
    }

}

from specs2.

viliusl avatar viliusl commented on June 3, 2024

Thanks. I guess we can just call Throwables special and leave it at that:)

I mean from dsl side it's not ideal as and behaves differently for returned and thrown things - but thanks for sample code, will manage:)

from specs2.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.