-
Notifications
You must be signed in to change notification settings - Fork 17
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
Order independent flag for arrays #8
Comments
This is something I agree would be really great to have. I have thought about it before but there is one issue I don't know how to resolve: You probably don't want something that makes all array comparisons order independent. That would most likely lead to subtle false positives in your tests. I think it would be nicer to specifically mark which arrays should be compared order independently. However since we currently do all the comparisons directly on The only solution I see to that is creating our own JSON type with two variants for arrays: One where order matters, and one where it doesn't. If we did that we would probably also want our own Do you have any other ideas? |
Hello, This would be extremely helpful, even without all the setup needed to build the whole things. If it is a comparison mode, then someone opting in is expected to know what they are doing by reading the documentation. I have exactly this kind of issue here, where my tests sudenly stopped working because the system has decided to return items on a first level array in a different order. I suggest the following: with a new comparison mode, at first all arrays would be compared independently of the order (opt in for everything). If someone needs to take care of some of the arrays (e.e if these arrays need to be compared taking sorting in consideration) then a second call to assert_json_eq with the default mode (sorting matters) can be called passing only the sub arrays from both sides to be compared. What I think could be a good addition is passing a json path (or a serde_json pointer) together with the two values to restrict comparison, so that sub objects can be selected in place. Still, with an clear opt-in by means of a new comparison mode, I think that the implementation for all in adds enough value forusers. |
I don't have time to work in this at the moment but PRs are much appreciated! |
Hello, Please see if you agree on the proposal. If so, I might try to come with a solution. ProposalCreate an assert_json_contains!(container: Value, contained: Value) macro. DescriptionThis macro will verify if container contains contained. A container is considered to contain a contained if the following set of rules can be applied recursively:
A value is considered to be contained if:
Examplesnull
numeric
string
Object
arrays
With all of that, a strict equality but not considering orders in lists might be obtained if obj1 contains obj2 and obj2 contains obj1. If we do that we do not break the current API and give a good way for people trying to match service responses which are not expected to return a determined order on itens list to proceed. What do you think? |
Yes I think that makes sense and would be fairly straight forward to implement, its essentially how it works today except that arrays care about ordering. The hard part, like mentioned above, is mixing array dependent ordering with independent. What if for parts of your JSON you care about ordering and other parts you don't? I've found that to be fairly common in practice. |
Yup this is why creating another method would solve the problem. From a philosophical point of view, strict equality should take order in consideration, because JSON specification enforses order on array, which it does not on fields in objects. However, if you think about it, [1, 2, 3] contains [2, 3, 1] if 1 2 and 3 are contained. By offering a way of "pure" contains, we cover yet another scenarios. What if you have mixed requirements? Then you will need to perform a contains operation to make sure that objects match and then a comparison of the strict ordering sub objects by equality or diuff. I will try nto implement it and submit a pr. |
See pull request #27 |
Have a flag to indicate that the order of items in arrays do not matter.
The text was updated successfully, but these errors were encountered: