Freewind
Freewind

Reputation: 198238

Is there a better way to check if a number should be inside a range, in scalatest?

Say I wanna check if the result (an integer) should >=4 and <=15, I can write such assertion in a ScalaTest test:

assert(num >= 4)
assert(num <= 15)

It works but can be improved in my opinion. Is there any better way to check it in Scalatest?

Upvotes: 3

Views: 4224

Answers (3)

manub
manub

Reputation: 4100

Something like this will work (using Matchers):

val beWithin4and5 = be >= 4 and be <= 5

value should beWithin4And5

Upvotes: 5

LMeyer
LMeyer

Reputation: 2631

You can use a matcher. Checking a range for the numbers you gave don't make much sense but this does for example :

numberExample should be (6 +- 1)

Here the test will fail if numberExample is lower than 5 or greater than 7.

If what you wanted is either 4 or 5 you can do this :

numberExample should (equal 4 or 5)

Upvotes: 2

Dima
Dima

Reputation: 40500

This maybe: assert(4 to 5 contains res.toInt)?

Upvotes: 3

Related Questions