< Summary

Information
Class: Pozitron.QuerySpecification.SpecificationInMemoryEvaluator
Assembly: Pozitron.QuerySpecification
File(s): /home/runner/work/QuerySpecification/QuerySpecification/src/QuerySpecification/Evaluators/SpecificationInMemoryEvaluator.cs
Tag: 52_11740816328
Line coverage
100%
Covered lines: 32
Uncovered lines: 0
Coverable lines: 32
Total lines: 100
Line coverage: 100%
Branch coverage
100%
Covered branches: 14
Total branches: 14
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.cctor()100%11100%
get_Evaluators()100%11100%
.ctor()100%11100%
.ctor(...)100%11100%
Evaluate(...)100%88100%
Evaluate(...)100%66100%

File(s)

/home/runner/work/QuerySpecification/QuerySpecification/src/QuerySpecification/Evaluators/SpecificationInMemoryEvaluator.cs

#LineLine coverage
 1namespace Pozitron.QuerySpecification;
 2
 3/// <summary>
 4/// Evaluates specifications in memory.
 5/// </summary>
 6public class SpecificationInMemoryEvaluator
 7{
 8    /// <summary>
 9    /// Gets the default instance of the <see cref="SpecificationInMemoryEvaluator"/> class.
 10    /// </summary>
 111    public static SpecificationInMemoryEvaluator Default = new();
 12
 13    /// <summary>
 14    /// Gets the list of in-memory evaluators.
 15    /// </summary>
 1416    protected List<IInMemoryEvaluator> Evaluators { get; }
 17
 18    /// <summary>
 19    /// Initializes a new instance of the <see cref="SpecificationInMemoryEvaluator"/> class.
 20    /// </summary>
 221    public SpecificationInMemoryEvaluator()
 22    {
 223        Evaluators =
 224        [
 225            WhereEvaluator.Instance,
 226            OrderEvaluator.Instance,
 227            LikeMemoryEvaluator.Instance,
 228        ];
 229    }
 30
 31    /// <summary>
 32    /// Initializes a new instance of the <see cref="SpecificationInMemoryEvaluator"/> class with the specified evaluato
 33    /// </summary>
 34    /// <param name="evaluators">The in-memory evaluators to use.</param>
 135    public SpecificationInMemoryEvaluator(IEnumerable<IInMemoryEvaluator> evaluators)
 36    {
 137        Evaluators = evaluators.ToList();
 138    }
 39
 40    /// <summary>
 41    /// Evaluates the given specification on the provided enumerable source and returns the result.
 42    /// </summary>
 43    /// <typeparam name="T">The type of the entity.</typeparam>
 44    /// <typeparam name="TResult">The type of the result.</typeparam>
 45    /// <param name="source">The enumerable source.</param>
 46    /// <param name="specification">The specification to evaluate.</param>
 47    /// <param name="ignorePaging">Whether to ignore paging settings (Take/Skip) defined in the specification.</param>
 48    /// <returns>The evaluated enumerable result.</returns>
 49    public virtual IEnumerable<TResult> Evaluate<T, TResult>(
 50        IEnumerable<T> source,
 51        Specification<T, TResult> specification,
 52        bool ignorePaging = false)
 53    {
 1054        ArgumentNullException.ThrowIfNull(specification);
 55
 956        var selector = specification.Selector;
 957        var selectorMany = specification.SelectorMany;
 58
 959        if (selector is null && selectorMany is null)
 60        {
 161            throw new SelectorNotFoundException();
 62        }
 63
 864        source = Evaluate(source, (Specification<T>)specification, true);
 65
 866        var result = selector is not null
 867            ? source.Select(selector.Compile())
 868            : source.SelectMany(selectorMany!.Compile());
 69
 870        return ignorePaging
 871            ? result
 872            : result.ApplyPaging(specification);
 73    }
 74
 75    /// <summary>
 76    /// Evaluates the given specification on the provided enumerable source and returns the result.
 77    /// </summary>
 78    /// <typeparam name="T">The type of the entity.</typeparam>
 79    /// <param name="source">The enumerable source.</param>
 80    /// <param name="specification">The specification to evaluate.</param>
 81    /// <param name="ignorePaging">Whether to ignore paging settings (Take/Skip) defined in the specification.</param>
 82    /// <returns>The evaluated enumerable result.</returns>
 83    public virtual IEnumerable<T> Evaluate<T>(
 84        IEnumerable<T> source,
 85        Specification<T> specification,
 86        bool ignorePaging = false)
 87    {
 1588        ArgumentNullException.ThrowIfNull(specification);
 1689        if (specification.IsEmpty) return source;
 90
 9691        foreach (var evaluator in Evaluators)
 92        {
 3693            source = evaluator.Evaluate(source, specification);
 94        }
 95
 1296        return ignorePaging
 1297            ? source
 1298            : source.ApplyPaging(specification);
 99    }
 100}