< Summary - Code Coverage

Information
Class: Plainquire.Sort.PropertySort
Assembly: Plainquire.Sort
File(s): /home/runner/work/plainquire/plainquire/Plainquire.Sort/Plainquire.Sort/Sorts/PropertySort.cs
Tag: 64_13932151703
Line coverage
100%
Covered lines: 23
Uncovered lines: 0
Coverable lines: 23
Total lines: 110
Line coverage: 100%
Branch coverage
93%
Covered branches: 15
Total branches: 16
Branch coverage: 93.7%
Method coverage
100%
Covered methods: 5
Total methods: 5
Method coverage: 100%

Metrics

MethodBranch coverage Cyclomatic complexity NPath complexity Sequence coverage
.ctor(...)50%22100%
Create(...)100%11100%
Create(...)100%22100%
ToString()100%66100%
ParseSortSyntax(...)100%66100%

File(s)

/home/runner/work/plainquire/plainquire/Plainquire.Sort/Plainquire.Sort/Sorts/PropertySort.cs

#LineLine coverage
 1using Plainquire.Filter.Abstractions;
 2using Plainquire.Sort.Abstractions;
 3using System;
 4using System.Text.Json.Serialization;
 5using System.Text.RegularExpressions;
 6
 7namespace Plainquire.Sort;
 8
 9/// <summary>
 10/// A sort order for a property.
 11/// </summary>
 12public class PropertySort
 13{
 14    private readonly SortConfiguration? _configuration;
 15
 16    /// <summary>
 17    /// Path used when no member is specified in a member access expression (<c>person => person</c> instead of <c>perso
 18    /// </summary>
 19    public const string PATH_TO_SELF = "$SELF$";
 20
 21    /// <summary>
 22    /// The path to the property.
 23    /// </summary>
 24    public string PropertyPath { get; }
 25
 26    /// <summary>
 27    /// The sort direction.
 28    /// </summary>
 29    public SortDirection Direction { get; }
 30
 31    /// <summary>
 32    /// The position of the sort order.
 33    /// </summary>
 34    public int Position { get; }
 35
 36    /// <summary>
 37    /// Creates a new instance of <see cref="PropertySort"/>.
 38    /// </summary>
 39    /// <param name="propertyPath">Path to the property to be sorted by.</param>
 40    /// <param name="direction">Sort direction to use.</param>
 41    /// <param name="position">Position in the final sorting sequence.</param>
 42    /// <param name="configuration">The configuration to use.</param>
 43    /// <exception cref="ArgumentException"></exception>
 44    [JsonConstructor]
 45    internal PropertySort(string propertyPath, SortDirection direction, int position, SortConfiguration? configuration)
 46    {
 71147        PropertyPath = propertyPath ?? throw new ArgumentNullException(nameof(propertyPath));
 48        Direction = direction;
 49        Position = position;
 71150        _configuration = configuration;
 71151    }
 52
 53    /// <summary>
 54    /// Creates a new instance of <see cref="PropertySort"/>.
 55    /// </summary>
 56    /// <param name="propertyPath">Path to the property to be sorted by.</param>
 57    /// <param name="direction">Sort direction to use.</param>
 58    /// <param name="position">Position in the final sorting sequence.</param>
 59    /// <param name="configuration">The configuration to use.</param>
 60    /// <exception cref="ArgumentNullException"></exception>
 61    public static PropertySort Create(string propertyPath, SortDirection direction, int? position = null, SortConfigurat
 46862        => new(propertyPath, direction, position ?? 0, configuration);
 63
 64    /// <summary>
 65    /// Creates a new instance of <see cref="PropertySort"/>.
 66    /// </summary>
 67    /// <param name="sortSyntax">The sort order syntax.</param>
 68    /// <param name="position">Position in the final sorting sequence.</param>
 69    /// <param name="configuration">The configuration to use.</param>
 70    /// <returns></returns>
 71    public static PropertySort Create(string sortSyntax, int? position = null, SortConfiguration? configuration = null)
 72    {
 30973        if (string.IsNullOrEmpty(sortSyntax))
 274            throw new ArgumentException("Value cannot be null or empty.", nameof(sortSyntax));
 75
 30776        var (propertyPath, sortDirection) = ParseSortSyntax(sortSyntax, configuration);
 30777        return Create(propertyPath, sortDirection, position, configuration);
 78    }
 79
 80    /// <inheritdoc />
 81    public override string ToString()
 82    {
 483        var configuration = _configuration ?? SortConfiguration.Default ?? new SortConfiguration();
 84
 485        var directionPostfix = Direction == SortDirection.Ascending
 486            ? configuration.PrimaryAscendingPostfix
 487            : configuration.PrimaryDescendingPostfix;
 88
 489        return $"{PropertyPath}{directionPostfix}";
 90    }
 91
 92    private static (string PropertyPath, SortDirection Direction) ParseSortSyntax(string sortSyntax, SortConfiguration? 
 93    {
 30794        configuration ??= SortConfiguration.Default ?? new SortConfiguration();
 95
 30796        var sortSyntaxPattern = $"^(?<prefix>{configuration.SortDirectionPrefixPattern})(?<propertyPath>.*?)(?<postfix>{
 30797        var match = Regex.Match(sortSyntax, sortSyntaxPattern, RegexOptions.IgnoreCase, RegexDefaults.Timeout);
 98
 30799        var hasDescendingPrefix = configuration.DescendingPrefixes.Contains(match.Groups["prefix"].Value);
 307100        var hasDescendingPostfix = configuration.DescendingPostfixes.Contains(match.Groups["postfix"].Value);
 101
 307102        var sortDirection = hasDescendingPrefix || hasDescendingPostfix
 307103            ? SortDirection.Descending
 307104            : SortDirection.Ascending;
 105
 307106        var propertyPath = match.Groups["propertyPath"].Value;
 107
 307108        return (propertyPath, sortDirection);
 109    }
 110}