< Summary

Information
Class: Pozitron.QuerySpecification.SpecificationMemoryEvaluator
Assembly: Pozitron.QuerySpecification
File(s): /home/runner/work/QuerySpecification/QuerySpecification/src/QuerySpecification/Evaluators/SpecificationMemoryEvaluator.cs
Tag: 67_15587897385
Line coverage
100%
Covered lines: 34
Uncovered lines: 0
Coverable lines: 34
Total lines: 102
Line coverage: 100%
Branch coverage
100%
Covered branches: 16
Total branches: 16
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Cyclomatic complexity NPath complexity Sequence coverage
.cctor()100%11100%
.ctor()50%22100%
.ctor(...)100%11100%
Evaluate(...)100%88100%
Evaluate(...)100%66100%

File(s)

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

#LineLine coverage
 1namespace Pozitron.QuerySpecification;
 2
 3/// <summary>
 4/// Evaluates specifications in memory.
 5/// </summary>
 6public class SpecificationMemoryEvaluator
 7{
 8    /// <summary>
 9    /// Gets the default instance of the <see cref="SpecificationMemoryEvaluator"/> class.
 10    /// </summary>
 211    public static SpecificationMemoryEvaluator Default = new();
 12
 13    /// <summary>
 14    /// Gets the list of in-memory evaluators.
 15    /// </summary>
 1416    protected List<IMemoryEvaluator> Evaluators { get; }
 17
 18    /// <summary>
 19    /// Initializes a new instance of the <see cref="SpecificationMemoryEvaluator"/> class.
 20    /// </summary>
 521    public SpecificationMemoryEvaluator()
 22    {
 523        Evaluators = TypeDiscovery.IsAutoDiscoveryEnabled
 524            ? TypeDiscovery.GetMemoryEvaluators()
 525            :
 526            [
 527                WhereEvaluator.Instance,
 528                LikeMemoryEvaluator.Instance,
 529                OrderEvaluator.Instance,
 530            ];
 531    }
 32
 33    /// <summary>
 34    /// Initializes a new instance of the <see cref="SpecificationMemoryEvaluator"/> class with the specified evaluators
 35    /// </summary>
 36    /// <param name="evaluators">The in-memory evaluators to use.</param>
 137    public SpecificationMemoryEvaluator(IEnumerable<IMemoryEvaluator> evaluators)
 38    {
 139        Evaluators = evaluators.ToList();
 140    }
 41
 42    /// <summary>
 43    /// Evaluates the given specification on the provided enumerable source and returns the result.
 44    /// </summary>
 45    /// <typeparam name="T">The type of the entity.</typeparam>
 46    /// <typeparam name="TResult">The type of the result.</typeparam>
 47    /// <param name="source">The enumerable source.</param>
 48    /// <param name="specification">The specification to evaluate.</param>
 49    /// <param name="ignorePaging">Whether to ignore paging settings (Take/Skip) defined in the specification.</param>
 50    /// <returns>The evaluated enumerable result.</returns>
 51    public virtual IEnumerable<TResult> Evaluate<T, TResult>(
 52        IEnumerable<T> source,
 53        Specification<T, TResult> specification,
 54        bool ignorePaging = false)
 55    {
 1056        ArgumentNullException.ThrowIfNull(specification);
 57
 958        var selector = specification.Selector;
 959        var selectorMany = specification.SelectorMany;
 60
 961        if (selector is null && selectorMany is null)
 62        {
 163            throw new SelectorNotFoundException();
 64        }
 65
 866        source = Evaluate(source, (Specification<T>)specification, true);
 67
 868        var result = selector is not null
 869            ? source.Select(selector.Compile())
 870            : source.SelectMany(selectorMany!.Compile());
 71
 872        return ignorePaging
 873            ? result
 874            : result.ApplyPaging(specification);
 75    }
 76
 77    /// <summary>
 78    /// Evaluates the given specification on the provided enumerable source and returns the result.
 79    /// </summary>
 80    /// <typeparam name="T">The type of the entity.</typeparam>
 81    /// <param name="source">The enumerable source.</param>
 82    /// <param name="specification">The specification to evaluate.</param>
 83    /// <param name="ignorePaging">Whether to ignore paging settings (Take/Skip) defined in the specification.</param>
 84    /// <returns>The evaluated enumerable result.</returns>
 85    public virtual IEnumerable<T> Evaluate<T>(
 86        IEnumerable<T> source,
 87        Specification<T> specification,
 88        bool ignorePaging = false)
 89    {
 1590        ArgumentNullException.ThrowIfNull(specification);
 1691        if (specification.IsEmpty) return source;
 92
 9693        foreach (var evaluator in Evaluators)
 94        {
 3695            source = evaluator.Evaluate(source, specification);
 96        }
 97
 1298        return ignorePaging
 1299            ? source
 12100            : source.ApplyPaging(specification);
 101    }
 102}