-
Notifications
You must be signed in to change notification settings - Fork 38
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
Custom matcher which allows nil values #7
Conversation
This is a great idea! Thanks for the pull request. I'll keep this open for now, sleep on it and thinking a little bit about the API in the next few days. |
That's cool. If you can think of a clean way to get this behavior without changing the existing code, I'd be glad to hear it! |
Hey @tdumitrescu, just to let you know I haven't forgotten about this :) I just haven't made up my mind in terms of the APIs yet. Re "If you can think of a clean way to get this behavior without changing the existing code" - Since this your code is here for everyone to see and it's simple enough for anyone to replicate that, I'm in no hurry to pull this in. I just want to give a little more thoughts on how the "shortcut"/helper methods should work/be called :) |
Thanks for the update. Yeah, I'm not wedded to this particular solution, as long as the functionality's there. Another way I was thinking about is a method named something like #or_else so that you could write |
Lots of interesting ideas here. I haven't really been using json_expression the way you describe (make a pattern that's generic enough for all possible responses and that match that against each response), so it's interesting to think about how I could make it work better for that use case. (In case you are wondering, I usually write specialized patterns specific each test case, so usually I know exactly what to expect in that particular case, hence I haven't been needing these kind of functionality for myself.) On 2012-10-11, at 3:19 PM, tdumitrescu wrote:
|
Interesting - the usage you describe (a specific pattern for each test case) doesn't seem to need any flexible matchers at all (e.g., why write 'Fixnum' when you're really testing for '5'?). We've found that a powerful usage for our needs is when you specify a single pattern near the top of the spec, and then make sure that API actions in different test cases all produce responses which match it. That way, the spec file serves as explicit (and executable) documentation for the API's response format, which client authors can look at, rather than maintaining a separate set of docs outside the repo detailing the API format. |
I actually never needed to use all the Fixnum etc myself :P all i needed was unordered comparison for arrays and to ensure no unexpected keys in the json. all of those flexibility is more or less a by-product of using === instead of == for equality comparison. So in hindsight I am glad I made that decision, as that clearly enabled some use cases I didn't account for :) I think your use case is pretty important, so I definitely want to provide the first class support it deserves. Since I'm currently not using the gem this way, by all means let me know if you run into other rough edges and we can figure out how to best deal with those. One of those rough edges I imagine would be that you might want to capture a value and match it with a matcher at the same time. Sent from my phone On 2012-10-11, at 9:36 PM, tdumitrescu notifications@github.com wrote:
|
Not sure if this is related, but how do I express an array with zero or more elements of a certain set of elements? I cannot seem to find any tests or references to such an expression? So, warns: [] would be valid, or warns: [{message: 'Hello'}, {message: 'Goodbye'}] would also be valid. |
@martinstreicher Not sure if I understand you correctly - did you mean you want... A) warns zero or more * arbitrary* elements -or- B) either warns contains exactly [{message: 'Hello'}, {message: 'Goodbye'}](nothing more, nothing less); or warn is [] If it's A, then {warns:[].ignore_extra_values!} should work. If it's B, then you probably need to do something like { warns: ->(w){ ... } } and implement your own logic inside the Proc (for now) |
@tdumitrescu Sorry for the hold up - I haven't forgotten about this, just that I've been off API work for a while so I wasn't able to spend as much time on this. I am leaning towards a more generic implementation that allows arbitrary chaining, such as: pattern = {
a: m( [1, 2, 3] ).or( [] ).or( nil ),
b: m( /\A[a-z]+\z/i ).and( /hello/i )
} What do you think? |
Of course, there could be some optional syntactic sugar for these (probably off by default... overriding `` seems a little too crazy) pattern = {
a: `[1, 2, 3]` | `[]` | `nil`,
b: `/\A[a-z]+\z/i` & `/hello/i`,
c: `String` & !(`/hello/i`)
} need to play around with this and see if there are any precedence issues though |
i definitely like those chaining examples with an i'm interested in seeing how you would implement the 'sugary' version...overriding one of ruby's basic operators to return a custom object? madness! |
I want warn [{error: 'message'}*] where * is the regex 0 or more of this specific element. What does the proc receive? On Jan 18, 2013, at 11:49 AM, Godfrey Chan wrote:
|
Hi Martin, I don't have any super clever ways to do this yet, but this should work. { The proc receives the object being matched. On Friday, January 18, 2013, Martin Streicher wrote:
|
OK -- will try it out. Perhaps what is needed is a .zero_or_more operator. I tried .forgiving!, but it seems best applied to non-empty collections. On Jan 18, 2013, at 4:27 PM, Godfrey Chan wrote:
|
Yeah -- I did this bit of trickery.. warns: ->(w){ On Jan 18, 2013, at 4:27 PM, Godfrey Chan notifications@github.com wrote:
|
@tdumitrescu @martinstreicher While I (slowly) work on the next version I discovered json schema and a ruby implementation, which seems to be a wider effort on solving and standardizing some of the same problems here, what's your thoughts on those projects? I am aware the goals are not 100% aligned but there's substantial overlap. I'm pondering how not to duplicate the effort and specialize in solving the unique problems here. (I also found http://apiary.io/, which seems really cool.) /cc @mathew-bb @pda @iangreenleaf |
I looked at json-schema… it looks interesting, but lacks the brevity of json_expressions. Perhaps json-schema could be a backend for json_expressions, providing correctness/robustness behind the nice brief API that json_expressions provides. I'd be lying if I said I had thought this idea through, though :) I'm violently opposed to monkey-patching for |
Closed in favour of #18 |
We just started playing with json_expressions for testing API controllers at Lumos Labs, and it's awesome for using specs to document response formats concisely. One pattern we've needed repeatedly is a value which can be either nil or a specific type/set of values; for instance, a foreign key which can either be a Fixnum or nil. So I added a #nil_or matcher which always matches on nil, and otherwise delegates matching to the given pattern/value. So nil_or(Fixnum) will succeed with both 5 and nil, but not 'meow'. Thought it would be helpful for other people too.