< Summary - ZhEaIsNsAaBn.Libraries coverage (affected: 23 packages)

Information
Class: ZhEaIsNsAaBn.DbSeeder.Design.SeedKeyComparer
Assembly: ZhEaIsNsAaBn.DbSeeder.Design
File(s): /home/runner/work/ZhEaIsNsAaBn.Libraries.V1/ZhEaIsNsAaBn.Libraries.V1/src/DataBasePackages/ZhEaIsNsAaBn.DbSeeder.Design/Files/SeedKeyComparer.cs
Line coverage
92%
Covered lines: 12
Uncovered lines: 1
Coverable lines: 13
Total lines: 45
Line coverage: 92.3%
Branch coverage
50%
Covered branches: 9
Total branches: 18
Branch coverage: 50%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.cctor()100%11100%
Key(...)100%11100%
Compare(...)50%8883.33%
ComparePart(...)50%1010100%

File(s)

/home/runner/work/ZhEaIsNsAaBn.Libraries.V1/ZhEaIsNsAaBn.Libraries.V1/src/DataBasePackages/ZhEaIsNsAaBn.DbSeeder.Design/Files/SeedKeyComparer.cs

#LineLine coverage
 1namespace ZhEaIsNsAaBn.DbSeeder.Design;
 2
 3/// <summary>
 4/// Orders row keys (canonical JSON values joined by <see cref="Separator"/>) part by part: <c>null</c> first, numbers b
 5/// (<c>2</c> before <c>10</c>), everything else by ordinal text. Works the same on keys from the database and from file
 6/// </summary>
 7internal sealed class SeedKeyComparer : IComparer<string>
 8{
 9    public const char Separator = '\u001F';
 10
 311    public static readonly SeedKeyComparer Instance = new();
 12
 13
 220814    public static string Key( IEnumerable<string> jsonParts ) => string.Join( Separator, jsonParts );
 15
 16    public int Compare( string? x, string? y )
 17    {
 303018        var left  = ( x ?? string.Empty ).Split( Separator );
 303019        var right = ( y ?? string.Empty ).Split( Separator );
 20
 606021        for ( var i = 0; i < Math.Min( left.Length, right.Length ); i++ )
 22        {
 303023            var part = ComparePart( left[ i ], right[ i ] );
 24
 606025            if ( part != 0 ) { return part; }
 26        }
 27
 28
 029        return left.Length.CompareTo( right.Length );
 30    }
 31
 32
 33    private static int ComparePart( string left, string right )
 34    {
 303035        if ( left == right ) { return 0; }
 36
 303037        if ( left == "null" || right == "null" ) { return left == "null" ? -1 : 1; }
 38
 39
 303040        var numbers = decimal.TryParse( left, NumberStyles.Float, CultureInfo.InvariantCulture, out var a )
 303041                    & decimal.TryParse( right, NumberStyles.Float, CultureInfo.InvariantCulture, out var b );
 42
 303043        return numbers ? a.CompareTo( b ) : string.CompareOrdinal( left, right );
 44    }
 45}