5

How can I check if a Seq[String] is empty or not using specs2 in Scala ? I am using seq must be empty or seq.length must be greaterThan(0) but I end up always with type mismatch errors.

ret is Seq[String]

ret.length must be greaterThan(0)

[error] ApiTest.scala:99: type mismatch;
[error]  found   : Int
[error]  required: org.specs2.matcher.Matcher[String]
[error]         ret.length must be greaterThan(0)
iwein
  • 25,788
  • 10
  • 70
  • 111
mete
  • 589
  • 2
  • 6
  • 17
  • You might be more lucky if you posted the exact code and the corresponding error message as given by the compiler. – Samuel Tardieu Sep 20 '12 at 11:30
  • 1
    Yes please add a more complete example. I think that your case might be an instance of a "classical" type inference issue where you have consecutive matcher expressions separated by newlines, like "ret.length must be greaterThan(0) \n ret.lenght must beLowerThan(10)" (if that's the case I'll edit this comment as a proper answer) – Eric Sep 20 '12 at 23:56

2 Answers2

3

I think the type mismatch error is caused by another bit of code than that which you've posted.

Your example should just work with:

ret must not be empty

I've tried and confirmed to be working correctly:

 "Seq beEmpty test" should {
    "just work" in {
      Seq("foo") must not be empty
    }
  }

You could run into trouble if you use multiple assertions per test, for example the following doesn't compile:

"Seq beEmpty test" should {
  "just work" in {
    List() must be empty
    Seq("foo") must not be empty
  }
}

Which is unexpected, but easily fixed by helping the compiler:

"Seq beEmpty test" should {
  "just work" in {
    List() must beEmpty
    Seq("foo") must not beEmpty
  }
}
iwein
  • 25,788
  • 10
  • 70
  • 111
2

Try using the specs2 matcher have size. Since the size can't be negative, if it's not zero, it must be greater than zero. Therefore, we could use:

ret must not have size (0)