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

feat: allow non-object matchers for array-contains #459

Conversation

MaxCampman
Copy link
Contributor

I have recently been writing a lot of pact tests and I noticed that the array-contains matcher requires each variant to be an object.

Reading the spec, it says "Checks if all the variants are present in an array".

Based on this, and the below implementation guides, it seems that non-object types should be supported too.

Having given the code base a cursory review, it seems that supporting this should be quite trivial.

Note: this is my first PR here so please let me know if there are steps I have missed

@rholshausen
Copy link
Contributor

While this is a trivial change, I'm unsure that it would work correctly. If the variant is an array (like [A, B]), I think that means that the source array must include a sub-array with the values A, B in that order.

Is this what you require?

@MaxCampman
Copy link
Contributor Author

While this is a trivial change, I'm unsure that it would work correctly. If the variant is an array (like [A, B]), I think that means that the source array must include a sub-array with the values A, B in that order.

Is this what you require?

This seems to be consistent with the rest of pact. If I wanted to check that a sub-array contained certain items in no particular order, I would be able to specify further "array-contains" matchers in a nested fashion, something like:

Match.ArrayContains([
    Match.ArrayContains(["A", "B"]),
    Match.ArrayContains(["C", "D"]),
])

As you say, if I specified just the arrays, they would need to match in the specified order:

Match.ArrayContains([
    new[] { "A", "B" },
    new[] { "C", "D" },
])

Not sure what kind of use case would require nested array-includes, but nonetheless I think this change makes it possible to specify both restrictions

Can see this as a test case I added to a fork of PactNet running on top of the changes in this PR

@rholshausen
Copy link
Contributor

Awesome, I'll merge it.

@rholshausen rholshausen merged commit af869d3 into pact-foundation:master Aug 6, 2024
36 of 37 checks passed
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

Successfully merging this pull request may close these issues.

2 participants