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: add array contains matcher #518

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

MaxCampman
Copy link

What?

  • Add array-contains matcher (e.g. JS version docs).
  • I was able to use this matcher via a workaround but thought I should add it here to make it available to others.
  • This was added to the pact-reference project here: feat: allow non-object matchers for array-contains pact-reference#459
  • In order to incorporate this change, I have had to update the pact-ffi version to 0.4.23 (from 0.4.16)
  • This required some small tweaks to the integration tests and some tweaking of the script used to fetch the libraries (there were a few name changes for libraries).
  • Actual change is implemented as:
    • Addition of: src/PactNet.Abstractions/Matchers/ArrayContainsMatcher.cs
    • Addition to: src/PactNet.Abstractions/Matchers/Match.cs
    • Addition to: src/PactNet.Abstractions/Matchers/MatcherConverter.cs

NOTE:

  • I unfortunately do not have access to an Apple computer at the moment.
  • This likely means that the build script needs some further tweaking for the OSX-specific script (if [[ "$OSTYPE" == "darwin"* ]]; then)

Copy link
Contributor

@adamrodger adamrodger left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The FFI change should probably be split into a separate preceding change just because of the extra complexity around the breaking changes in the FFI packaging. I could see that causing issues so a dedicated change would be better.

In terms of the example, the pact spec describes this matcher as checking that all variants are contained within a collection, but this example doesn't really reflect that given the collection is only of one type and thus only one variant. The eachLike matcher is better in that scenario. I think the example should somehow contain different types (and if it's too difficult to clearly do that then perhaps the matcher doesn't make as much sense in a typed language Vs something like JavaScript).

I've not reviewed the integration tests as I'm on my phone and they're quite big, so there may be further comments there later.

Also check any line endings settings in your editor because a few places have weird extra blank lines and things, plus obviously the sln file shows a huge diff when there probably isn't one.

PactNet.sln Outdated
SolutionGuid = {C2CBC30C-92D4-4E3A-A5B8-1E5D4E938DFC}
EndGlobalSection
EndGlobal

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

issue: Something appears to have gone weird with this file, perhaps line endings? Iirc sln files must have CRLF line endings always, even on non-Windows systems

The gitignore file also may have this issue because the final line shows a change when there isn't one

"combine": "AND",
"matchers": [
{
"match": "type"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

issue: This matching rule looks wrong, should it be a date matcher instead?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is from this line:

Date = Match.Type(expected1.Date.ToString("O"))

So is indeed meant to be type

@@ -88,6 +208,126 @@
},
"type": "Synchronous/HTTP"
},
{
"description": "a request for multiple orders by id",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

issue: This interaction appears to be in here twice. I was hoping the updated FFI would fix this issue, but perhaps it doesn't

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is my fault, can see the description here is different to the one above.
Will amend

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed

@@ -0,0 +1,17 @@
using System.Text.Json.Serialization;

namespace PactNet.Matchers;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

style: The project uses namespace blocks instead of file scoped namespaces

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Noted, will amend


namespace PactNet.Matchers;

public class ArrayContainsMatcher : IMatcher
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

issue: The public API should be documented

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Noted, will amend

@@ -168,5 +168,10 @@ public static IMatcher Include(string example)
{
return new IncludeMatcher(example);
}

public static IMatcher ArrayContains(dynamic[] variations)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

issue: The public API should be documented

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Noted, will amend

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This one still needs documenting

var actual = JsonSerializer.Serialize(matcher);

// Assert
actual.Should().Be(@"{""pact:matcher:type"":""array-contains"",""variants"":[""Thing1"",""Thing2""]}");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion: You can use new string literals to format the JSON nicely here

Copy link
Author

@MaxCampman MaxCampman Sep 7, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, quite the oversight from me

Nevermind: ArrayContainsMatcherTests.cs(27, 17): [CS8370] Feature 'raw string literals' is not available in C# 7.3. Please use language version 11.0 or greater.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, yes my bad. These have to support .Net Framework also

Copy link
Author

@MaxCampman MaxCampman left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for taking the time to go through this, especially on a weekend.

I will open a new MR to just update the FFI, and will come back to this one to make the requested changes

"combine": "AND",
"matchers": [
{
"match": "type"
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is from this line:

Date = Match.Type(expected1.Date.ToString("O"))

So is indeed meant to be type

@@ -88,6 +208,126 @@
},
"type": "Synchronous/HTTP"
},
{
"description": "a request for multiple orders by id",
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is my fault, can see the description here is different to the one above.
Will amend

@@ -0,0 +1,17 @@
using System.Text.Json.Serialization;

namespace PactNet.Matchers;
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Noted, will amend


namespace PactNet.Matchers;

public class ArrayContainsMatcher : IMatcher
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Noted, will amend

@@ -168,5 +168,10 @@ public static IMatcher Include(string example)
{
return new IncludeMatcher(example);
}

public static IMatcher ArrayContains(dynamic[] variations)
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Noted, will amend

var actual = JsonSerializer.Serialize(matcher);

// Assert
actual.Should().Be(@"{""pact:matcher:type"":""array-contains"",""variants"":[""Thing1"",""Thing2""]}");
Copy link
Author

@MaxCampman MaxCampman Sep 7, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, quite the oversight from me

Nevermind: ArrayContainsMatcherTests.cs(27, 17): [CS8370] Feature 'raw string literals' is not available in C# 7.3. Please use language version 11.0 or greater.

@adamrodger
Copy link
Contributor

@MaxCampman if you rebase onto latest then the FFI upgrade should already be there 👍

@MaxCampman MaxCampman force-pushed the feat-add-array-contains-matcher branch 3 times, most recently from 1915b96 to ff6f37c Compare September 8, 2024 15:18
@MaxCampman MaxCampman force-pushed the feat-add-array-contains-matcher branch from ff6f37c to b03a9ea Compare September 8, 2024 15:19
.WithStatus(HttpStatusCode.OK)
.WithJsonBody(Match.ArrayContains(new dynamic[]
{
new
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

issue: I think for this example to 'come to life' then we'd really need it to match multiple different possible variants

Perhaps if there were different order types or something so that they could have different fields on them, then the matching rules could reflect those variants. Like a fulfilled order has an extra date on it for when it was completed, or something like that.

@@ -52,6 +54,15 @@ private async Task EnsureEventExistsAsync(IDictionary<string, object> parameters
await this.orders.InsertAsync(new OrderDto(id.GetInt32(), OrderStatus.Fulfilling, DateTimeOffset.Now));
}

private async Task EnsureEventsExistAsync(IDictionary<string, object> parameters)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

docs: Needs doc string like other methods

@@ -46,6 +47,29 @@ public async Task<IActionResult> GetByIdAsync(int id)
}
}

[HttpGet("many/{ids}", Name = "getMany")]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

docs: Needs doc string like other methods


namespace PactNet.Matchers
{
public class ArrayContainsMatcher : IMatcher
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

docs: Needs doc string like other matchers

@@ -168,5 +168,10 @@ public static IMatcher Include(string example)
{
return new IncludeMatcher(example);
}

public static IMatcher ArrayContains(dynamic[] variations)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This one still needs documenting

var actual = JsonSerializer.Serialize(matcher);

// Assert
actual.Should().Be(@"{""pact:matcher:type"":""array-contains"",""variants"":[""Thing1"",""Thing2""]}");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, yes my bad. These have to support .Net Framework also

.WithStatus(HttpStatusCode.OK)
.WithJsonBody(Match.ArrayContains([
Match.Regex("Thing 1", "Thing [0-9]+"),
Match.Regex("Thing X", "Thing [A-Z]+"),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

comment: This is a good test because it shows using different variants, which we should replicate in the example usage

@adamrodger adamrodger added this to the 5.1.0 milestone Oct 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