mirror of
https://github.com/wiremock/WireMock.Net.git
synced 2026-02-23 02:34:55 +01: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.
|
||||
/// </summary>
|
||||
/// <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="func">The function to check if a string is a match.</param>
|
||||
public FuncMatcher(MatchBehaviour matchBehaviour, Func<string?, bool> func)
|
||||
public FuncMatcher(Func<string?, bool> func, MatchBehaviour matchBehaviour = MatchBehaviour.AcceptOnMatch)
|
||||
{
|
||||
_stringFunc = Guard.NotNull(func);
|
||||
MatchBehaviour = matchBehaviour;
|
||||
@@ -40,16 +32,8 @@ public class FuncMatcher : IFuncMatcher
|
||||
/// Initializes a new instance of the <see cref="FuncMatcher"/> class for byte array matching.
|
||||
/// </summary>
|
||||
/// <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="func">The function to check if a byte[] is a match.</param>
|
||||
public FuncMatcher(MatchBehaviour matchBehaviour, Func<byte[]?, bool> func)
|
||||
public FuncMatcher(Func<byte[]?, bool> func, MatchBehaviour matchBehaviour = MatchBehaviour.AcceptOnMatch)
|
||||
{
|
||||
_bytesFunc = Guard.NotNull(func);
|
||||
MatchBehaviour = matchBehaviour;
|
||||
@@ -62,7 +46,7 @@ public class FuncMatcher : IFuncMatcher
|
||||
{
|
||||
try
|
||||
{
|
||||
return CreateMatchResult(_stringFunc(stringValue));
|
||||
return MatchResult.From(Name, MatchBehaviour, _stringFunc(stringValue));
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
@@ -74,7 +58,7 @@ public class FuncMatcher : IFuncMatcher
|
||||
{
|
||||
try
|
||||
{
|
||||
return CreateMatchResult(_bytesFunc(bytesValue));
|
||||
return MatchResult.From(Name, MatchBehaviour, _bytesFunc(bytesValue));
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
@@ -85,11 +69,6 @@ public class FuncMatcher : IFuncMatcher
|
||||
return MatchResult.From(Name, MatchScores.Mismatch);
|
||||
}
|
||||
|
||||
private MatchResult CreateMatchResult(bool isMatch)
|
||||
{
|
||||
return MatchResult.From(Name, MatchBehaviourHelper.Convert(MatchBehaviour, MatchScores.ToScore(isMatch)));
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public string Name => nameof(FuncMatcher);
|
||||
|
||||
@@ -99,8 +78,8 @@ public class FuncMatcher : IFuncMatcher
|
||||
var funcType = _stringFunc != null ? "Func<string?, bool>" : "Func<byte[]?, bool>";
|
||||
return $"new {Name}" +
|
||||
$"(" +
|
||||
$"{MatchBehaviour.GetFullyQualifiedEnumValue()}, " +
|
||||
$"/* {funcType} function */" +
|
||||
$"/* {funcType} function */, " +
|
||||
$"{MatchBehaviour.GetFullyQualifiedEnumValue()}" +
|
||||
$")";
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,8 +1,5 @@
|
||||
// Copyright © WireMock.Net
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Stef.Validation;
|
||||
using WireMock.Extensions;
|
||||
using WireMock.Matchers.Request;
|
||||
@@ -40,6 +37,18 @@ public class MatchResult
|
||||
/// </summary>
|
||||
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>
|
||||
/// Create a MatchResult.
|
||||
/// </summary>
|
||||
|
||||
@@ -79,4 +79,4 @@ public interface IWebSocketContext
|
||||
/// Broadcast JSON message to all connections in this mapping
|
||||
/// </summary>
|
||||
Task BroadcastJsonAsync(object data, CancellationToken cancellationToken = default);
|
||||
}
|
||||
}
|
||||
@@ -33,4 +33,4 @@ public class WebSocketMessage
|
||||
/// Timestamp when the message was received
|
||||
/// </summary>
|
||||
public DateTime Timestamp { get; set; }
|
||||
}
|
||||
}
|
||||
@@ -1,9 +1,7 @@
|
||||
// Copyright © WireMock.Net
|
||||
|
||||
using System;
|
||||
using FluentAssertions;
|
||||
using WireMock.Matchers;
|
||||
using Xunit;
|
||||
|
||||
namespace WireMock.Net.Tests.Matchers;
|
||||
|
||||
@@ -232,7 +230,7 @@ public class FuncMatcherTests
|
||||
{
|
||||
// Arrange
|
||||
Func<string?, bool> func = s => s == "test";
|
||||
var matcher = new FuncMatcher(MatchBehaviour.RejectOnMatch, func);
|
||||
var matcher = new FuncMatcher(func, MatchBehaviour.RejectOnMatch);
|
||||
|
||||
// Act & Assert
|
||||
matcher.MatchBehaviour.Should().Be(MatchBehaviour.RejectOnMatch);
|
||||
@@ -243,7 +241,7 @@ public class FuncMatcherTests
|
||||
{
|
||||
// Arrange
|
||||
Func<byte[]?, bool> func = b => b != null;
|
||||
var matcher = new FuncMatcher(MatchBehaviour.RejectOnMatch, func);
|
||||
var matcher = new FuncMatcher(func, MatchBehaviour.RejectOnMatch);
|
||||
|
||||
// Act & Assert
|
||||
matcher.MatchBehaviour.Should().Be(MatchBehaviour.RejectOnMatch);
|
||||
@@ -260,9 +258,7 @@ public class FuncMatcherTests
|
||||
var code = matcher.GetCSharpCodeArguments();
|
||||
|
||||
// Assert
|
||||
code.Should().Contain("FuncMatcher");
|
||||
code.Should().Contain("Func<string?, bool>");
|
||||
code.Should().Contain("AcceptOnMatch");
|
||||
code.Should().Be("new FuncMatcher(/* Func<string?, bool> function */, WireMock.Matchers.MatchBehaviour.AcceptOnMatch)");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
@@ -276,9 +272,7 @@ public class FuncMatcherTests
|
||||
var code = matcher.GetCSharpCodeArguments();
|
||||
|
||||
// Assert
|
||||
code.Should().Contain("FuncMatcher");
|
||||
code.Should().Contain("Func<byte[]?, bool>");
|
||||
code.Should().Contain("AcceptOnMatch");
|
||||
code.Should().Be("new FuncMatcher(/* Func<byte[]?, bool> function */, WireMock.Matchers.MatchBehaviour.AcceptOnMatch)");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
@@ -286,7 +280,7 @@ public class FuncMatcherTests
|
||||
{
|
||||
// Arrange
|
||||
Func<string?, bool> func = s => s == "test";
|
||||
var matcher = new FuncMatcher(MatchBehaviour.RejectOnMatch, func);
|
||||
var matcher = new FuncMatcher(func, MatchBehaviour.RejectOnMatch);
|
||||
|
||||
// Act
|
||||
var result = matcher.IsMatch("test");
|
||||
@@ -300,7 +294,7 @@ public class FuncMatcherTests
|
||||
{
|
||||
// Arrange
|
||||
Func<string?, bool> func = s => s == "test";
|
||||
var matcher = new FuncMatcher(MatchBehaviour.RejectOnMatch, func);
|
||||
var matcher = new FuncMatcher(func, MatchBehaviour.RejectOnMatch);
|
||||
|
||||
// Act
|
||||
var result = matcher.IsMatch("other");
|
||||
@@ -314,7 +308,7 @@ public class FuncMatcherTests
|
||||
{
|
||||
// Arrange
|
||||
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
|
||||
var result = matcher.IsMatch(new byte[] { 1, 2, 3 });
|
||||
@@ -328,7 +322,7 @@ public class FuncMatcherTests
|
||||
{
|
||||
// Arrange
|
||||
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
|
||||
var result = matcher.IsMatch(new byte[0]);
|
||||
|
||||
Reference in New Issue
Block a user