// Copyright © WireMock.Net
using System;
using System.Linq;
using AnyOfTypes;
using Stef.Validation;
using WireMock.Extensions;
using WireMock.Models;
using WireMock.Util;
namespace WireMock.Matchers;
///
/// ExactMatcher
///
/// and
public class ExactMatcher : IStringMatcher, IIgnoreCaseMatcher
{
private readonly AnyOf[] _values;
///
public MatchBehaviour MatchBehaviour { get; }
///
/// Initializes a new instance of the class.
///
/// The match behaviour.
/// The string value.
public ExactMatcher(MatchBehaviour matchBehaviour, string value) : this(matchBehaviour, true, MatchOperator.Or, new AnyOf(value))
{
}
///
/// Initializes a new instance of the class.
///
/// The values.
public ExactMatcher(params AnyOf[] values) : this(MatchBehaviour.AcceptOnMatch, false, MatchOperator.Or, values)
{
}
///
/// Initializes a new instance of the class.
///
/// Ignore the case from the pattern(s).
/// The values.
public ExactMatcher(bool ignoreCase, params AnyOf[] values) : this(MatchBehaviour.AcceptOnMatch, ignoreCase, MatchOperator.Or, values)
{
}
///
/// Initializes a new instance of the class.
///
/// The match behaviour.
/// Ignore the case from the pattern(s).
/// The to use. (default = "Or")
/// The values.
public ExactMatcher(
MatchBehaviour matchBehaviour,
bool ignoreCase = false,
MatchOperator matchOperator = MatchOperator.Or,
params AnyOf[] values)
{
_values = Guard.NotNull(values);
MatchBehaviour = matchBehaviour;
IgnoreCase = ignoreCase;
MatchOperator = matchOperator;
}
///
public MatchResult IsMatch(string? input)
{
Func equals = IgnoreCase
? pattern => string.Equals(pattern, input, StringComparison.OrdinalIgnoreCase)
: pattern => pattern == input;
var score = MatchScores.ToScore(_values.Select(v => equals(v)).ToArray(), MatchOperator);
return new MatchResult(MatchBehaviourHelper.Convert(MatchBehaviour, score));
}
///
public AnyOf[] GetPatterns()
{
return _values;
}
///
public MatchOperator MatchOperator { get; }
///
public string Name => nameof(ExactMatcher);
///
public bool IgnoreCase { get; }
///
public string GetCSharpCodeArguments()
{
return $"new {Name}" +
$"(" +
$"{MatchBehaviour.GetFullyQualifiedEnumValue()}, " +
$"{CSharpFormatter.ToCSharpBooleanLiteral(IgnoreCase)}, " +
$"{MatchOperator.GetFullyQualifiedEnumValue()}, " +
$"{MappingConverterUtils.ToCSharpCodeArguments(_values)}" +
$")";
}
}