< Summary

Line coverage
95%
Covered lines: 139
Uncovered lines: 7
Coverable lines: 146
Total lines: 223
Line coverage: 95.2%
Branch coverage
88%
Covered branches: 104
Total branches: 118
Branch coverage: 88.1%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

File(s)

/home/runner/work/dotnet-snap/dotnet-snap/src/Dotnet.Installer.Core/Types/DotnetVersion.cs

#LineLine coverage
 1using System.Text;
 2using System.Text.Json.Serialization;
 3using Dotnet.Installer.Core.Converters;
 4
 5namespace Dotnet.Installer.Core.Types;
 6
 7[JsonConverter(typeof(DotnetVersionJsonConverter))]
 8public partial class DotnetVersion : IEquatable<DotnetVersion>, IComparable<DotnetVersion>
 9{
 10710    public DotnetVersion(int major, int minor, int patch, bool isPreview = false, bool isRc = false,
 10711        int? previewIdentifier = null, int? revision = null)
 10712    {
 10713        if (isPreview && isRc)
 114        {
 115            throw new ApplicationException("The .NET version can either be a preview, an RC, or none.");
 16        }
 17
 10618        if ((isPreview || isRc) && previewIdentifier is null)
 219        {
 220            throw new ApplicationException(
 221                "You must specify a Preview Identifier if version is either a preview of an RC.");
 22        }
 23
 10424        if (!isPreview && !isRc && previewIdentifier is not null)
 125        {
 126            throw new ApplicationException(
 127                "You can't specify a Preview Identifier if the version is neither a preview or an RC.");
 28        }
 29
 10330        if (revision.HasValue && revision.Value == 0)
 131        {
 132            throw new ApplicationException(
 133                "A revision can only contain a value starting with 1, null otherwise.");
 34        }
 35
 10236        Major = major;
 10237        Minor = minor;
 10238        Patch = patch;
 39
 10240        IsPreview = isPreview;
 10241        IsRc = isRc;
 42
 10243        PreviewIdentifier = previewIdentifier;
 44
 10245        Revision = revision;
 10246    }
 47
 12248    public int Major { get; }
 12249    public int Minor { get; }
 18350    public int Patch { get; }
 51
 20752    public bool IsPreview { get; private set; }
 19553    public bool IsRc { get; private set; }
 2254    public bool IsStable => !IsPreview && !IsRc;
 28055    public int? PreviewIdentifier { get; private set; } = null;
 56
 20157    public int? Revision { get; set; }
 58
 2659    public bool IsRuntime => Patch < 100;
 1860    public bool IsSdk => !IsRuntime;
 61
 1062    public int? FeatureBand => !IsSdk ? default(int?) : int.Parse($"{Patch.ToString()[..1]}00");
 63
 64    public static DotnetVersion Parse(string version)
 1765    {
 1766        int? revision = default;
 1767        var revisionSplit = version.Split('+');
 1768        if (revisionSplit.Length > 1)
 469        {
 470            revision = int.Parse(revisionSplit[1]);
 471        }
 72
 1773        var previewSplit = revisionSplit[0].Split('-');
 1774        var versionSections = previewSplit[0].Split('.');
 1775        var parsedVersion = new DotnetVersion
 1776        (
 1777            int.Parse(versionSections[0]),
 1778            int.Parse(versionSections[1]),
 1779            int.Parse(versionSections[2])
 1780        );
 81
 1482        if (previewSplit.Length > 1)
 783        {
 784            var previewVersionSections = previewSplit[1].Split('.');
 85
 786            if (string.Equals("preview", previewVersionSections[0]))
 487            {
 488                parsedVersion.IsPreview = true;
 489            }
 390            else if (string.Equals("rc", previewVersionSections[0]))
 391            {
 392                parsedVersion.IsRc = true;
 393            }
 94
 795            parsedVersion.PreviewIdentifier = int.Parse(previewVersionSections[1]);
 796        }
 97
 1498        parsedVersion.Revision = revision;
 99
 14100        return parsedVersion;
 14101    }
 102
 103    public override string ToString()
 9104    {
 9105        var versionBuilder = new StringBuilder();
 106
 9107        versionBuilder.Append(Major);
 9108        versionBuilder.Append('.');
 9109        versionBuilder.Append(Minor);
 9110        versionBuilder.Append('.');
 9111        versionBuilder.Append(Patch);
 112
 12113        if (IsPreview) versionBuilder.Append("-preview.");
 12114        if (IsRc) versionBuilder.Append("-rc.");
 115
 9116        versionBuilder.Append(PreviewIdentifier.ToString());
 117
 12118        if (Revision is not null) versionBuilder.Append('+');
 9119        versionBuilder.Append(Revision.ToString());
 120
 9121        return versionBuilder.ToString();
 9122    }
 123}

/home/runner/work/dotnet-snap/dotnet-snap/src/Dotnet.Installer.Core/Types/DotnetVersion.Operators.cs

#LineLine coverage
 1namespace Dotnet.Installer.Core.Types;
 2
 3public partial class DotnetVersion
 4{
 5    public int CompareTo(DotnetVersion? other)
 266    {
 7        // Keep null entries last
 308        if (other is null) return -1;
 9
 2210        if (other.Major != Major) return Major - other.Major;
 2211        if (other.Minor != Minor) return Minor - other.Minor;
 3612        if (other.Patch != Patch) return Patch - other.Patch;
 13
 14        // Version is the same, but preview information might be different
 815        if ((other.IsPreview && IsPreview) || (other.IsRc && IsRc))
 316            return PreviewIdentifier!.Value - other.PreviewIdentifier!.Value;
 17
 518        if (other.IsPreview && IsRc) return 1;
 519        if (other.IsRc && IsPreview) return -1;
 20
 721        if (IsStable && !other.IsStable) return 1;
 322        if (!IsStable && other.IsStable) return -1;
 23
 24        // It will come down to revisions then
 325        if (Revision.HasValue && !other.Revision.HasValue) return 1;
 326        else if (!Revision.HasValue && other.Revision.HasValue) return -1;
 327        else if (!Revision.HasValue && !other.Revision.HasValue) return 0;
 328        else return Revision!.Value - other.Revision!.Value;
 2629    }
 30
 031    public static bool operator <(DotnetVersion lhs, DotnetVersion rhs) => lhs.CompareTo(rhs) < 0;
 032    public static bool operator >(DotnetVersion lhs, DotnetVersion rhs) => lhs.CompareTo(rhs) > 0;
 033    public static bool operator <=(DotnetVersion lhs, DotnetVersion rhs) => lhs.CompareTo(rhs) <= 0;
 034    public static bool operator >=(DotnetVersion lhs, DotnetVersion rhs) => lhs.CompareTo(rhs) >= 0;
 35
 036    public override bool Equals(object? obj) => Equals(obj as DotnetVersion);
 37
 38    public bool Equals(DotnetVersion? other)
 1939    {
 2240        if (other is null) return false;
 41
 1742        if (ReferenceEquals(this, other)) return true;
 43
 1544        if (GetType() != other.GetType()) return false;
 45
 1546        return (Major == other.Major) && (Minor == other.Minor) && (Patch == other.Patch) &&
 1547               (IsPreview == other.IsPreview) && (IsRc == other.IsRc) && (PreviewIdentifier == other.PreviewIdentifier) 
 1548               (Revision == other.Revision);
 1949    }
 50
 51    public bool Equals(DotnetVersion? other, DotnetVersionComparison comparisonType)
 052        => Equals(this, other, comparisonType);
 53
 54    public static bool Equals(DotnetVersion? rhs, DotnetVersion? lhs,
 55        DotnetVersionComparison comparisonType = DotnetVersionComparison.Default)
 756    {
 1057        if (rhs is null || lhs is null) return rhs == lhs;
 58
 459        return comparisonType switch
 460        {
 461            DotnetVersionComparison.IgnoreRevision =>
 262                (lhs.Major == rhs.Major) &&
 263                (lhs.Minor == rhs.Minor) &&
 264                (lhs.Patch == rhs.Patch) &&
 265                (lhs.IsPreview == rhs.IsPreview) &&
 266                (lhs.IsRc == rhs.IsRc) &&
 267                (lhs.PreviewIdentifier == rhs.PreviewIdentifier),
 468
 269            _ => rhs.Equals(lhs),
 470        };
 771    }
 72
 73    public override int GetHashCode()
 1874    {
 75        unchecked // Overflow is fine, just wrap
 1876        {
 1877            int hash = 17;
 1878            hash = hash * 23 + Major.GetHashCode();
 1879            hash = hash * 23 + Minor.GetHashCode();
 1880            hash = hash * 23 + Patch.GetHashCode();
 1881            hash = hash * 23 + IsPreview.GetHashCode();
 1882            hash = hash * 23 + IsRc.GetHashCode();
 1883            hash = hash * 23 + (PreviewIdentifier?.GetHashCode() ?? 0);
 1884            hash = hash * 23 + (Revision?.GetHashCode() ?? 0);
 1885            return hash;
 86        }
 1887    }
 88
 89    public static bool operator ==(DotnetVersion? lhs, DotnetVersion? rhs)
 1390    {
 1391        if (lhs is null)
 392        {
 393            return rhs is null;
 94        }
 95
 1096        return lhs.Equals(rhs);
 1397    }
 98
 099    public static bool operator !=(DotnetVersion lhs, DotnetVersion rhs) => !(lhs == rhs);
 100}