mirror of
https://github.com/wiremock/WireMock.Net.git
synced 2026-07-18 11:01:23 +02:00
func
This commit is contained in:
@@ -21,16 +21,8 @@ public class FuncMatcher : IFuncMatcher
|
|||||||
/// Initializes a new instance of the <see cref="FuncMatcher"/> class for string matching.
|
/// Initializes a new instance of the <see cref="FuncMatcher"/> class for string matching.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="func">The function to check if a string is a match.</param>
|
/// <param name="func">The function to check if a string is a match.</param>
|
||||||
public FuncMatcher(Func<string?, bool> func) : this(MatchBehaviour.AcceptOnMatch, func)
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Initializes a new instance of the <see cref="FuncMatcher"/> class for string matching.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="matchBehaviour">The match behaviour.</param>
|
/// <param name="matchBehaviour">The match behaviour.</param>
|
||||||
/// <param name="func">The function to check if a string is a match.</param>
|
public FuncMatcher(Func<string?, bool> func, MatchBehaviour matchBehaviour = MatchBehaviour.AcceptOnMatch)
|
||||||
public FuncMatcher(MatchBehaviour matchBehaviour, Func<string?, bool> func)
|
|
||||||
{
|
{
|
||||||
_stringFunc = Guard.NotNull(func);
|
_stringFunc = Guard.NotNull(func);
|
||||||
MatchBehaviour = matchBehaviour;
|
MatchBehaviour = matchBehaviour;
|
||||||
@@ -40,16 +32,8 @@ public class FuncMatcher : IFuncMatcher
|
|||||||
/// Initializes a new instance of the <see cref="FuncMatcher"/> class for byte array matching.
|
/// Initializes a new instance of the <see cref="FuncMatcher"/> class for byte array matching.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="func">The function to check if a byte[] is a match.</param>
|
/// <param name="func">The function to check if a byte[] is a match.</param>
|
||||||
public FuncMatcher(Func<byte[]?, bool> func) : this(MatchBehaviour.AcceptOnMatch, func)
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Initializes a new instance of the <see cref="FuncMatcher"/> class for byte array matching.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="matchBehaviour">The match behaviour.</param>
|
/// <param name="matchBehaviour">The match behaviour.</param>
|
||||||
/// <param name="func">The function to check if a byte[] is a match.</param>
|
public FuncMatcher(Func<byte[]?, bool> func, MatchBehaviour matchBehaviour = MatchBehaviour.AcceptOnMatch)
|
||||||
public FuncMatcher(MatchBehaviour matchBehaviour, Func<byte[]?, bool> func)
|
|
||||||
{
|
{
|
||||||
_bytesFunc = Guard.NotNull(func);
|
_bytesFunc = Guard.NotNull(func);
|
||||||
MatchBehaviour = matchBehaviour;
|
MatchBehaviour = matchBehaviour;
|
||||||
@@ -62,7 +46,7 @@ public class FuncMatcher : IFuncMatcher
|
|||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
return CreateMatchResult(_stringFunc(stringValue));
|
return MatchResult.From(Name, MatchBehaviour, _stringFunc(stringValue));
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
@@ -74,7 +58,7 @@ public class FuncMatcher : IFuncMatcher
|
|||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
return CreateMatchResult(_bytesFunc(bytesValue));
|
return MatchResult.From(Name, MatchBehaviour, _bytesFunc(bytesValue));
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
@@ -85,11 +69,6 @@ public class FuncMatcher : IFuncMatcher
|
|||||||
return MatchResult.From(Name, MatchScores.Mismatch);
|
return MatchResult.From(Name, MatchScores.Mismatch);
|
||||||
}
|
}
|
||||||
|
|
||||||
private MatchResult CreateMatchResult(bool isMatch)
|
|
||||||
{
|
|
||||||
return MatchResult.From(Name, MatchBehaviourHelper.Convert(MatchBehaviour, MatchScores.ToScore(isMatch)));
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
public string Name => nameof(FuncMatcher);
|
public string Name => nameof(FuncMatcher);
|
||||||
|
|
||||||
@@ -99,8 +78,8 @@ public class FuncMatcher : IFuncMatcher
|
|||||||
var funcType = _stringFunc != null ? "Func<string?, bool>" : "Func<byte[]?, bool>";
|
var funcType = _stringFunc != null ? "Func<string?, bool>" : "Func<byte[]?, bool>";
|
||||||
return $"new {Name}" +
|
return $"new {Name}" +
|
||||||
$"(" +
|
$"(" +
|
||||||
$"{MatchBehaviour.GetFullyQualifiedEnumValue()}, " +
|
$"/* {funcType} function */, " +
|
||||||
$"/* {funcType} function */" +
|
$"{MatchBehaviour.GetFullyQualifiedEnumValue()}" +
|
||||||
$")";
|
$")";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,8 +1,5 @@
|
|||||||
// Copyright © WireMock.Net
|
// Copyright © WireMock.Net
|
||||||
|
|
||||||
using System;
|
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.Linq;
|
|
||||||
using Stef.Validation;
|
using Stef.Validation;
|
||||||
using WireMock.Extensions;
|
using WireMock.Extensions;
|
||||||
using WireMock.Matchers.Request;
|
using WireMock.Matchers.Request;
|
||||||
@@ -40,6 +37,18 @@ public class MatchResult
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public bool IsPerfect() => MatchScores.IsPerfect(Score);
|
public bool IsPerfect() => MatchScores.IsPerfect(Score);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Create a MatchResult.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="name">The name or description of the matcher.</param>
|
||||||
|
/// <param name="matchBehaviour">The match behaviour.</param>
|
||||||
|
/// <param name="isMatch">Is this a match?</param>
|
||||||
|
/// <param name="exception">The exception in case the matching fails. [Optional]</param>
|
||||||
|
public static MatchResult From(string name, MatchBehaviour matchBehaviour, bool isMatch, Exception? exception = null)
|
||||||
|
{
|
||||||
|
return From(name, MatchBehaviourHelper.Convert(matchBehaviour, MatchScores.ToScore(isMatch)), exception);
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Create a MatchResult.
|
/// Create a MatchResult.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|||||||
@@ -1,9 +1,7 @@
|
|||||||
// Copyright © WireMock.Net
|
// Copyright © WireMock.Net
|
||||||
|
|
||||||
using System;
|
|
||||||
using FluentAssertions;
|
using FluentAssertions;
|
||||||
using WireMock.Matchers;
|
using WireMock.Matchers;
|
||||||
using Xunit;
|
|
||||||
|
|
||||||
namespace WireMock.Net.Tests.Matchers;
|
namespace WireMock.Net.Tests.Matchers;
|
||||||
|
|
||||||
@@ -232,7 +230,7 @@ public class FuncMatcherTests
|
|||||||
{
|
{
|
||||||
// Arrange
|
// Arrange
|
||||||
Func<string?, bool> func = s => s == "test";
|
Func<string?, bool> func = s => s == "test";
|
||||||
var matcher = new FuncMatcher(MatchBehaviour.RejectOnMatch, func);
|
var matcher = new FuncMatcher(func, MatchBehaviour.RejectOnMatch);
|
||||||
|
|
||||||
// Act & Assert
|
// Act & Assert
|
||||||
matcher.MatchBehaviour.Should().Be(MatchBehaviour.RejectOnMatch);
|
matcher.MatchBehaviour.Should().Be(MatchBehaviour.RejectOnMatch);
|
||||||
@@ -243,7 +241,7 @@ public class FuncMatcherTests
|
|||||||
{
|
{
|
||||||
// Arrange
|
// Arrange
|
||||||
Func<byte[]?, bool> func = b => b != null;
|
Func<byte[]?, bool> func = b => b != null;
|
||||||
var matcher = new FuncMatcher(MatchBehaviour.RejectOnMatch, func);
|
var matcher = new FuncMatcher(func, MatchBehaviour.RejectOnMatch);
|
||||||
|
|
||||||
// Act & Assert
|
// Act & Assert
|
||||||
matcher.MatchBehaviour.Should().Be(MatchBehaviour.RejectOnMatch);
|
matcher.MatchBehaviour.Should().Be(MatchBehaviour.RejectOnMatch);
|
||||||
@@ -260,9 +258,7 @@ public class FuncMatcherTests
|
|||||||
var code = matcher.GetCSharpCodeArguments();
|
var code = matcher.GetCSharpCodeArguments();
|
||||||
|
|
||||||
// Assert
|
// Assert
|
||||||
code.Should().Contain("FuncMatcher");
|
code.Should().Be("new FuncMatcher(/* Func<string?, bool> function */, WireMock.Matchers.MatchBehaviour.AcceptOnMatch)");
|
||||||
code.Should().Contain("Func<string?, bool>");
|
|
||||||
code.Should().Contain("AcceptOnMatch");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
@@ -276,9 +272,7 @@ public class FuncMatcherTests
|
|||||||
var code = matcher.GetCSharpCodeArguments();
|
var code = matcher.GetCSharpCodeArguments();
|
||||||
|
|
||||||
// Assert
|
// Assert
|
||||||
code.Should().Contain("FuncMatcher");
|
code.Should().Be("new FuncMatcher(/* Func<byte[]?, bool> function */, WireMock.Matchers.MatchBehaviour.AcceptOnMatch)");
|
||||||
code.Should().Contain("Func<byte[]?, bool>");
|
|
||||||
code.Should().Contain("AcceptOnMatch");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
@@ -286,7 +280,7 @@ public class FuncMatcherTests
|
|||||||
{
|
{
|
||||||
// Arrange
|
// Arrange
|
||||||
Func<string?, bool> func = s => s == "test";
|
Func<string?, bool> func = s => s == "test";
|
||||||
var matcher = new FuncMatcher(MatchBehaviour.RejectOnMatch, func);
|
var matcher = new FuncMatcher(func, MatchBehaviour.RejectOnMatch);
|
||||||
|
|
||||||
// Act
|
// Act
|
||||||
var result = matcher.IsMatch("test");
|
var result = matcher.IsMatch("test");
|
||||||
@@ -300,7 +294,7 @@ public class FuncMatcherTests
|
|||||||
{
|
{
|
||||||
// Arrange
|
// Arrange
|
||||||
Func<string?, bool> func = s => s == "test";
|
Func<string?, bool> func = s => s == "test";
|
||||||
var matcher = new FuncMatcher(MatchBehaviour.RejectOnMatch, func);
|
var matcher = new FuncMatcher(func, MatchBehaviour.RejectOnMatch);
|
||||||
|
|
||||||
// Act
|
// Act
|
||||||
var result = matcher.IsMatch("other");
|
var result = matcher.IsMatch("other");
|
||||||
@@ -314,7 +308,7 @@ public class FuncMatcherTests
|
|||||||
{
|
{
|
||||||
// Arrange
|
// Arrange
|
||||||
Func<byte[]?, bool> func = b => b != null && b.Length > 0;
|
Func<byte[]?, bool> func = b => b != null && b.Length > 0;
|
||||||
var matcher = new FuncMatcher(MatchBehaviour.RejectOnMatch, func);
|
var matcher = new FuncMatcher(func, MatchBehaviour.RejectOnMatch);
|
||||||
|
|
||||||
// Act
|
// Act
|
||||||
var result = matcher.IsMatch(new byte[] { 1, 2, 3 });
|
var result = matcher.IsMatch(new byte[] { 1, 2, 3 });
|
||||||
@@ -328,7 +322,7 @@ public class FuncMatcherTests
|
|||||||
{
|
{
|
||||||
// Arrange
|
// Arrange
|
||||||
Func<byte[]?, bool> func = b => b != null && b.Length > 0;
|
Func<byte[]?, bool> func = b => b != null && b.Length > 0;
|
||||||
var matcher = new FuncMatcher(MatchBehaviour.RejectOnMatch, func);
|
var matcher = new FuncMatcher(func, MatchBehaviour.RejectOnMatch);
|
||||||
|
|
||||||
// Act
|
// Act
|
||||||
var result = matcher.IsMatch(new byte[0]);
|
var result = matcher.IsMatch(new byte[0]);
|
||||||
|
|||||||
Reference in New Issue
Block a user