-
-
Notifications
You must be signed in to change notification settings - Fork 213
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
Stef negate matcher #134
Merged
Merged
Stef negate matcher #134
Changes from 4 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
0fb4b62
RejectOnMatch (wip)
StefH 8959e55
RejectOnMatch (wip)
StefH e7319a2
RejectOnMatch (wip)
StefH ca5056a
RejectOnMatch (wip)
StefH 9d6df1c
docs: improve sample app matcher to show use of reject on match
alastairtree d3640d0
Reworked code review comments
StefH File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
https://github.com/GitTools/GitReleaseNotes | ||
|
||
GitReleaseNotes.exe . /OutputFile CHANGELOG.md /Version 1.0.3.16 | ||
GitReleaseNotes.exe . /OutputFile CHANGELOG.md /Version 1.0.3.17 | ||
|
||
GitReleaseNotes.exe . /OutputFile CHANGELOG.md /allTags |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,54 +14,69 @@ public class JsonPathMatcher : IStringMatcher, IObjectMatcher | |
{ | ||
private readonly string[] _patterns; | ||
|
||
/// <inheritdoc cref="IMatcher.MatchBehaviour"/> | ||
public MatchBehaviour MatchBehaviour { get; } | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="JsonPathMatcher"/> class. | ||
/// </summary> | ||
/// <param name="patterns">The patterns.</param> | ||
public JsonPathMatcher([NotNull] params string[] patterns) | ||
public JsonPathMatcher([NotNull] params string[] patterns) : this(MatchBehaviour.AcceptOnMatch, patterns) | ||
{ | ||
} | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="JsonPathMatcher"/> class. | ||
/// </summary> | ||
/// <param name="matchBehaviour">The match behaviour.</param> | ||
/// <param name="patterns">The patterns.</param> | ||
public JsonPathMatcher(MatchBehaviour matchBehaviour, [NotNull] params string[] patterns) | ||
{ | ||
Check.NotNull(patterns, nameof(patterns)); | ||
|
||
MatchBehaviour = matchBehaviour; | ||
_patterns = patterns; | ||
} | ||
|
||
/// <inheritdoc cref="IStringMatcher.IsMatch"/> | ||
public double IsMatch(string input) | ||
{ | ||
if (input == null) | ||
double match = MatchScores.Mismatch; | ||
if (input != null) | ||
{ | ||
return MatchScores.Mismatch; | ||
try | ||
{ | ||
var jtoken = JToken.Parse(input); | ||
match = IsMatch(jtoken); | ||
} | ||
catch (Exception) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is it possible to catch the exact exception? Wouldn't want to inadvertently catch a SystemOutOfMemoryException for example. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good point, I'll fix this. |
||
{ | ||
// just ignore exception | ||
} | ||
} | ||
|
||
try | ||
{ | ||
var jtoken = JToken.Parse(input); | ||
return IsMatch(jtoken); | ||
} | ||
catch (Exception) | ||
{ | ||
return MatchScores.Mismatch; | ||
} | ||
return MatchBehaviourHelper.Convert(MatchBehaviour, match); | ||
} | ||
|
||
/// <inheritdoc cref="IObjectMatcher.IsMatch"/> | ||
public double IsMatch(object input) | ||
{ | ||
if (input == null) | ||
double match = MatchScores.Mismatch; | ||
if (input != null) | ||
{ | ||
return MatchScores.Mismatch; | ||
try | ||
{ | ||
// Check if JToken or object | ||
JToken jtoken = input is JToken token ? token : JObject.FromObject(input); | ||
match = IsMatch(jtoken); | ||
} | ||
catch (Exception) | ||
{ | ||
// just ignore exception | ||
} | ||
} | ||
|
||
try | ||
{ | ||
// Check if JToken or object | ||
JToken jtoken = input is JToken token ? token : JObject.FromObject(input); | ||
return IsMatch(jtoken); | ||
} | ||
catch (Exception) | ||
{ | ||
return MatchScores.Mismatch; | ||
} | ||
return MatchBehaviourHelper.Convert(MatchBehaviour, match); | ||
} | ||
|
||
/// <inheritdoc cref="IStringMatcher.GetPatterns"/> | ||
|
@@ -70,11 +85,8 @@ public string[] GetPatterns() | |
return _patterns; | ||
} | ||
|
||
/// <inheritdoc cref="IMatcher.GetName"/> | ||
public string GetName() | ||
{ | ||
return "JsonPathMatcher"; | ||
} | ||
/// <inheritdoc cref="IMatcher.Name"/> | ||
public string Name => "JsonPathMatcher"; | ||
|
||
private double IsMatch(JToken jtoken) | ||
{ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
namespace WireMock.Matchers | ||
{ | ||
/// <summary> | ||
/// MatchBehaviour | ||
/// </summary> | ||
public enum MatchBehaviour | ||
{ | ||
/// <summary> | ||
/// Accept on match (default) | ||
/// </summary> | ||
AcceptOnMatch, | ||
|
||
/// <summary> | ||
/// Reject on match | ||
/// </summary> | ||
RejectOnMatch | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
| ||
namespace WireMock.Matchers | ||
{ | ||
internal static class MatchBehaviourHelper | ||
{ | ||
/// <summary> | ||
/// Converts the specified match behaviour and match value to a new match value. | ||
/// | ||
/// if AcceptOnMatch --> return match (default) | ||
/// if RejectOnMatch and match = 0.0 --> return 1.0 | ||
/// if RejectOnMatch and match = 0.? --> return 0.0 | ||
/// if RejectOnMatch and match = 1.0 --> return 0.0 | ||
/// </summary> | ||
/// | ||
/// <param name="matchBehaviour">The match behaviour.</param> | ||
/// <param name="match">The match.</param> | ||
/// <returns>match value</returns> | ||
internal static double Convert(MatchBehaviour matchBehaviour, double match) | ||
{ | ||
if (matchBehaviour == MatchBehaviour.AcceptOnMatch) | ||
{ | ||
return match; | ||
} | ||
|
||
return match <= MatchScores.Tolerance ? MatchScores.Perfect : MatchScores.Mismatch; | ||
} | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shall we make this example more realistic - like the absense of an API key header returns 401 unauthorised?