Matcher for Option which

37 views
Skip to first unread message

Rhys Keepence

unread,
Oct 17, 2012, 1:00:42 PM10/17/12
to specs2...@googlegroups.com
Hi,

I'm looking to match an Option, such that if the expectable is Some[T], then go and match it against a Matcher[T].

My full assertion is basically:
    acknowledgement must beSomeMatching(\\("Status") \> "success").eventually(10, 1.second)

where acknowledgement is a method which returns an Option[Node], depending on whether a server has responded asynchronously yet.

I've come up with this matcher (beSomeMatching) which does the job, but the code smells a bit and I think I'm missing something

Is there a better way of doing it?
If not is there anything I can do to tidy up that matcher, particularly, so that it returns a full failure message 
(Ideally, I'd like "None is not Some with xml matching ...." - currently it just says "None is not Some")?

Cheers
Rhys

Daniel Sobral

unread,
Oct 17, 2012, 1:11:13 PM10/17/12
to specs2...@googlegroups.com
Any reason why beSome.like or beSome.which are not good enough?
--
Daniel C. Sobral

I travel to the future all the time.

Rhys Keepence

unread,
Oct 17, 2012, 4:23:40 PM10/17/12
to specs2...@googlegroups.com
Essentially I want to be able to compose matchers in a single assertion, more importantly because they describe failures.

For example when I run an example such as (using my custom matcher)

  val xml = Some(<name>sir robin</name>)
  xml must beSomeMatching(\\("name") \> "roger the shrubber")

I get a meaningful error: 
  '<name>sir robin</name>' doesn't contain node 'name' with text: roger the shrubber

On the other hand this example

  val maybeXml = Some(<name>sir robin</name>)
  maybeXml must beSome.which ( xml => (xml \\ "name").text == "roger the shrubber")

produces a mysterious and incorrect:
  'None' is not Some[T]

Cheers,
Rhys

Rhys Keepence

unread,
Oct 17, 2012, 4:58:27 PM10/17/12
to specs2...@googlegroups.com
Scratch that, I didn't know it existed but `like` is exactly what I was looking for.

Thanks again

Rhys

Daniel Sobral

unread,
Oct 17, 2012, 6:35:17 PM10/17/12
to specs2...@googlegroups.com
On Wed, Oct 17, 2012 at 5:23 PM, Rhys Keepence <rhyske...@gmail.com> wrote:
> Essentially I want to be able to compose matchers in a single assertion,
> more importantly because they describe failures.
>
> For example when I run an example such as (using my custom matcher)
>
> val xml = Some(<name>sir robin</name>)
> xml must beSomeMatching(\\("name") \> "roger the shrubber")
>
> I get a meaningful error:
> '<name>sir robin</name>' doesn't contain node 'name' with text: roger the
> shrubber
>
> On the other hand this example
>
> val maybeXml = Some(<name>sir robin</name>)
> maybeXml must beSome.which ( xml => (xml \\ "name").text == "roger the
> shrubber")

Note that the problem above is that your which is using == instead of
a matcher like the one you used in the previous example.
Reply all
Reply to author
Forward
0 new messages