// Copyright © WireMock.Net
using System;
using System.Collections.Generic;
using System.Linq;
namespace WireMock.Matchers;
///
/// MatchScores
///
public static class MatchScores
{
///
/// The tolerance
///
public const double Tolerance = 0.000001;
///
/// The default mismatch score
///
public const double Mismatch = 0.0;
///
/// The default perfect match score
///
public const double Perfect = 1.0;
///
/// The almost perfect match score
///
public const double AlmostPerfect = 0.99;
///
/// Is the value a perfect match?
///
/// The value.
/// true/false
public static bool IsPerfect(double value)
{
return Math.Abs(value - Perfect) < Tolerance;
}
///
/// Convert a bool to the score.
///
/// if set to true [value].
/// score
public static double ToScore(bool value)
{
return value ? Perfect : Mismatch;
}
///
/// Calculates the score from multiple values.
///
/// The values.
/// The .
/// average score
public static double ToScore(IReadOnlyCollection values, MatchOperator matchOperator)
{
return ToScore(values.Select(ToScore).ToArray(), matchOperator);
}
///
/// Calculates the score from multiple values.
///
/// The values.
///
/// average score
public static double ToScore(IReadOnlyCollection values, MatchOperator matchOperator)
{
if (!values.Any())
{
return Mismatch;
}
return matchOperator switch
{
MatchOperator.Or => ToScore(values.Any(IsPerfect)),
MatchOperator.And => ToScore(values.All(IsPerfect)),
_ => values.Average()
};
}
}