// Copyright © WireMock.Net
using System;
using System.Collections.Generic;
using System.Linq;
using Stef.Validation;
using WireMock.Extensions;
namespace WireMock.Matchers;
///
/// The MatchResult which contains the score (value between 0.0 - 1.0 of the similarity) and an optional error message.
///
public class MatchResult
{
///
/// A value between 0.0 - 1.0 of the similarity.
///
public double Score { get; set; }
///
/// The exception message) in case the matching fails.
/// [Optional]
///
public Exception? Exception { get; set; }
///
/// The name or description of the matcher.
///
public required string Name { get; set; }
///
/// The sub MatchResults in case of multiple matchers.
///
public MatchResult[]? MatchResults { get; set; }
///
/// Is the value a perfect match?
///
public bool IsPerfect() => MatchScores.IsPerfect(Score);
///
/// Create a MatchResult.
///
/// The name or description of the matcher.
/// A value between 0.0 - 1.0 of the similarity.
/// The exception in case the matching fails. [Optional]
public static MatchResult From(string name, double score = 0, Exception? exception = null)
{
return new MatchResult
{
Name = name,
Score = score,
Exception = exception
};
}
///
/// Create a MatchResult from exception.
///
/// The name or description of the matcher.
/// The exception in case the matching fails.
/// MatchResult
public static MatchResult From(string name, Exception exception)
{
return From(name, MatchScores.Mismatch, exception);
}
///
/// Create a MatchResult from multiple MatchResults.
///
/// The name or description of the matcher.
/// A list of MatchResults.
/// The MatchOperator
/// MatchResult
public static MatchResult From(string name, IReadOnlyList matchResults, MatchOperator matchOperator)
{
Guard.NotNullOrEmpty(matchResults);
if (matchResults.Count == 1)
{
return matchResults[0];
}
return new MatchResult
{
Name = name,
MatchResults = matchResults.ToArray(),
Score = MatchScores.ToScore(matchResults.Select(r => r.Score).ToArray(), matchOperator),
Exception = matchResults.Select(m => m.Exception).OfType().ToArray().ToException()
};
}
///
/// Expand to Tuple
///
/// Tuple : Score and Exception
public (double Score, Exception? Exception) Expand()
{
return (Score, Exception);
}
}