< Summary

Line coverage
96%
Covered lines: 141
Uncovered lines: 5
Coverable lines: 146
Total lines: 223
Line coverage: 96.5%
Branch coverage
91%
Covered branches: 108
Total branches: 118
Branch coverage: 91.5%
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{
 14210    public DotnetVersion(int major, int minor, int patch, bool isPreview = false, bool isRc = false,
 14211        int? previewIdentifier = null, int? revision = null)
 14212    {
 14213        if (isPreview && isRc)
 114        {
 115            throw new ApplicationException("The .NET version can either be a preview, an RC, or none.");
 16        }
 17
 14118        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
 13924        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
 13830        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
 13736        Major = major;
 13737        Minor = minor;
 13738        Patch = patch;
 39
 13740        IsPreview = isPreview;
 13741        IsRc = isRc;
 42
 13743        PreviewIdentifier = previewIdentifier;
 44
 13745        Revision = revision;
 13746    }
 47
 14648    public int Major { get; }
 14649    public int Minor { get; }
 26050    public int Patch { get; }
 51
 27852    public bool IsPreview { get; private set; }
 26653    public bool IsRc { get; private set; }
 4054    public bool IsStable => !IsPreview && !IsRc;
 35655    public int? PreviewIdentifier { get; private set; } = null;
 56
 28057    public int? Revision { get; set; }
 58
 5959    public bool IsRuntime => Patch < 100;
 4160    public bool IsSdk => !IsRuntime;
 61
 2662    public int? FeatureBand => !IsSdk ? default(int?) : int.Parse($"{Patch.ToString()[..1]}00");
 63
 64    public static DotnetVersion Parse(string version)
 2365    {
 2366        int? revision = default;
 2367        var revisionSplit = version.Split('+');
 2368        if (revisionSplit.Length > 1)
 469        {
 470            revision = int.Parse(revisionSplit[1]);
 471        }
 72
 2373        var previewSplit = revisionSplit[0].Split('-');
 2374        var versionSections = previewSplit[0].Split('.');
 2375        var parsedVersion = new DotnetVersion
 2376        (
 2377            int.Parse(versionSections[0]),
 2378            int.Parse(versionSections[1]),
 2379            int.Parse(versionSections[2])
 2380        );
 81
 1982        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
 1998        parsedVersion.Revision = revision;
 99
 19100        return parsedVersion;
 19101    }
 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)
 346    {
 7        // Keep null entries last
 388        if (other is null) return -1;
 9
 3010        if (other.Major != Major) return Major - other.Major;
 3011        if (other.Minor != Minor) return Minor - other.Minor;
 4612        if (other.Patch != Patch) return Patch - other.Patch;
 13
 14        // Version is the same, but preview information might be different
 1415        if ((other.IsPreview && IsPreview) || (other.IsRc && IsRc))
 316            return PreviewIdentifier!.Value - other.PreviewIdentifier!.Value;
 17
 1118        if (other.IsPreview && IsRc) return 1;
 1119        if (other.IsRc && IsPreview) return -1;
 20
 1321        if (IsStable && !other.IsStable) return 1;
 922        if (!IsStable && other.IsStable) return -1;
 23
 24        // It will come down to revisions then
 925        if (Revision.HasValue && !other.Revision.HasValue) return 1;
 926        else if (!Revision.HasValue && other.Revision.HasValue) return -1;
 1527        else if (!Revision.HasValue && !other.Revision.HasValue) return 0;
 328        else return Revision!.Value - other.Revision!.Value;
 3429    }
 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;
 833    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
 636    public override bool Equals(object? obj) => Equals(obj as DotnetVersion);
 37
 38    public bool Equals(DotnetVersion? other)
 2939    {
 3240        if (other is null) return false;
 41
 3342        if (ReferenceEquals(this, other)) return true;
 43
 1944        if (GetType() != other.GetType()) return false;
 45
 1946        return (Major == other.Major) && (Minor == other.Minor) && (Patch == other.Patch) &&
 1947               (IsPreview == other.IsPreview) && (IsRc == other.IsRc) && (PreviewIdentifier == other.PreviewIdentifier) 
 1948               (Revision == other.Revision);
 2949    }
 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}