< Summary - Estud

Information
Class: Estud.Back.Extensions.EnumExtensions
Assembly: Back
File(s): /home/runner/work/syki/syki/Back/Extensions/EnumExtensions.cs
Tag: 114_29044117136
Line coverage
88%
Covered lines: 24
Uncovered lines: 3
Coverable lines: 27
Total lines: 87
Line coverage: 88.8%
Branch coverage
100%
Covered branches: 12
Total branches: 12
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
GetDescription(...)100%66100%
IsIn(...)100%44100%
ToEnum(...)100%210%
ToEnum(...)100%210%
IntToEnum(...)100%11100%
IsValid(...)100%11100%
ToInt(...)100%11100%
ToShort(...)100%210%
Is(...)100%11100%
DiffInMinutes(...)100%22100%

File(s)

/home/runner/work/syki/syki/Back/Extensions/EnumExtensions.cs

#LineLine coverage
 1namespace Estud.Back.Extensions;
 2
 3public static class EnumExtensions
 4{
 5    public static string GetDescription(this Enum value)
 6    {
 267        if (value == null)
 8        {
 29            return string.Empty;
 10        }
 11
 2412        var attribute = value.GetType()
 2413            .GetField(value.ToString())!
 2414            .GetCustomAttributes(typeof(DescriptionAttribute), inherit: false);
 15
 2416        if (attribute is DescriptionAttribute[] source && source.Length != 0)
 17        {
 2218            return source.First().Description;
 19        }
 20
 221        return value.ToString();
 22    }
 23
 24    public static bool IsIn(this Enum source, params Enum[] valuesToCheck)
 25    {
 2426        if (valuesToCheck == null || valuesToCheck.Length == 0)
 27        {
 428            return false;
 29        }
 30
 2031        return valuesToCheck.Contains(source);
 32    }
 33
 34    public static T ToEnum<T>(this string value)
 35    {
 036        return (T)Enum.Parse(typeof(T), value, true);
 37    }
 38
 39    public static T ToEnum<T>(this short value)
 40    {
 041        return (T)Enum.Parse(typeof(T), value.ToString(), true);
 42    }
 43
 44    public static T IntToEnum<T>(this int value)
 45    {
 1646        return (T)Enum.Parse(typeof(T), value.ToString(), true);
 47    }
 48
 49    public static bool IsValid(this Enum value)
 50    {
 5451        return Enum.IsDefined(value.GetType(), value);
 52    }
 53
 54    public static int ToInt<TEnum>(this TEnum enumValue) where TEnum : Enum
 55    {
 87056        return Convert.ToInt32(enumValue);
 57    }
 58
 59    public static short ToShort<TEnum>(this TEnum enumValue) where TEnum : Enum
 60    {
 061        return (short) enumValue.ToInt();
 62    }
 63
 64    public static bool Is(this DayOfWeek dayOfWeek, Day day)
 65    {
 120866        return day.ToString() == dayOfWeek.ToString();
 67    }
 68
 69    public static int DiffInMinutes(this Hour hourA, Hour hourB)
 70    {
 20071        if (hourA > hourB)
 72        {
 873            (hourA, hourB) = (hourB, hourA);
 74        }
 75
 20076        var hourAHour = int.Parse(hourA.ToString().Substring(1, 2));
 20077        var hourAMinute = int.Parse(hourA.ToString().Substring(4, 2));
 78
 20079        var hourBHour = int.Parse(hourB.ToString().Substring(1, 2));
 20080        var hourBMinute = int.Parse(hourB.ToString().Substring(4, 2));
 81
 20082        var hours = hourBHour - hourAHour;
 20083        var minutes = hourBMinute - hourAMinute;
 84
 20085        return hours * 60 + minutes;
 86    }
 87}