Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Json matcher: not /#(i) / ("a" -> "b") does not work #279

Closed
trobert opened this issue Jul 9, 2014 · 4 comments
Closed

Json matcher: not /#(i) / ("a" -> "b") does not work #279

trobert opened this issue Jul 9, 2014 · 4 comments

Comments

@trobert
Copy link

trobert commented Jul 9, 2014

The following test pass successfully but should fail:

  val array = """ [ { "a" : "b" } ] """
  array must /#(0) / ("a" -> "b")         // Success
  array must not /#(0) / ("a" -> "b")     // Should fail
  array must not (/#(0) / ("a" -> "b"))   // Should fail
  array must /#(0).not / ("a" -> "b")     // Should fail

Looking at the code, I see that the JsonSelector object is loss when you apply "not", because a new JsonPairMatcher object is created, loosing the overriding of the navigate method.

Moreover my initial need was to check that an array only contains one element. I didn't found any way to do it. Any suggestion ? I wanted something like array must not /#(1) / (".*".r -> ".*".r)

@etorreborre
Copy link
Owner

Sorry I didn't notice the notification earlier. I'm going to have a look at that this week-end. In the meantime you can create a not condition by writing:

 (array must /#(0) / ("a" -> "b")).not

I'll think about your "1 element check" and come back to you.

@etorreborre
Copy link
Owner

This is a first attempt at adding ways to check sizes. What do you think?

@trobert
Copy link
Author

trobert commented Jul 11, 2014

Your proposal fits my need. Thanks !

Some additional thoughts:
I was thinking about a haveSize(m: Matcher[Int]) matcher, to be able to write more complex check on the size, like for expample, array must withSize(be_<=(2))
However I realised that is even not possible on plain Traversable. So maybe I am going too far. On the other, while you can write a check on the size of a plain traversable, you can't do it directly on the size of a json array.

etorreborre added a commit that referenced this issue Jul 18, 2014
And added a way to use matchers to check the last selected Json value
@etorreborre
Copy link
Owner

Sorry it took me a lot more time to fix than I thought, I had to do a rewrite... That made me realize that the whole JsonMatchers API is not ideal and should be probably abandoned in favor of some Argonaut matchers.

For the size example, it's done here, and you can use any Matcher[JsonType] where JsonType is either an array or a map.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants