Comments (2)
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.
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)
- parse error when using Json matchers HOT 4
- Questions about versions 4.14.0/4.14.1(-cross) and 4.15 HOT 8
- Not found: ... specs2-mock_3/4.15.0/specs2-mock_3-4.15.0.pom HOT 3
- `TimeoutException` causes `NullPointerException`, test to be skipped HOT 4
- Wrong execution order when using SpecificationWithJUnit with BeforeAfterAll on specs2 version 4.8.2 and up HOT 2
- Please tag 4.16.1 HOT 1
- Probably prefix removal for a dependency in the documentation notes. HOT 5
- Surefire report is not produced with test name when using JUnit5 and Specs2 HOT 5
- Scala 2.11 support possible? HOT 2
- 4.x series status? HOT 5
- Consider re-releasing 4.17.0 as 4.19.0 HOT 2
- v4.19 not published to Maven Central (?) HOT 3
- Compatibility error after updating from 4.19.0 to 4.19.1 HOT 7
- `Around` not working in specs 4 Scala 3 HOT 3
- specs2-mock depends on older mockito HOT 3
- Release 4.20.0 does not have tag or release notes HOT 1
- Release 4.20.0 not published for Scala 3 HOT 2
- specs2-mock does not support Scala 3 HOT 3
- Broken links on specs2 website HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from specs2.