Skip to content

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

Merged
rholshausen merged 1 commit intopact-foundation:masterfrom
MaxCampman:feat-allow-non-object-matchers-for-array-contains
Aug 6, 2024
Merged

feat: allow non-object matchers for array-contains#459
rholshausen merged 1 commit intopact-foundation:masterfrom
MaxCampman:feat-allow-non-object-matchers-for-array-contains

Conversation

@MaxCampman
Copy link
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
Contributor

Awesome, I'll merge it.

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