• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

luttje / Key2Joy / 6749638574

03 Nov 2023 08:00PM UTC coverage: 44.916% (+1.1%) from 43.795%
6749638574

push

github

luttje
fix JS failing on callback/function parameter + add tests

802 of 2447 branches covered (0.0%)

Branch coverage included in aggregate %.

75 of 76 new or added lines in 4 files covered. (98.68%)

147 existing lines in 11 files now uncovered.

4048 of 8351 relevant lines covered (48.47%)

17051.46 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

11.03
/Core/Key2Joy.Core/Util/TypeExtensions.cs
1
using System;
2
using System.Collections.Generic;
3

4
namespace Key2Joy.Util;
5

6
public static class TypeExtensions
7
{
8
    /// <summary>
9
    /// Source: https://stackoverflow.com/a/457708
10
    /// </summary>
11
    /// <param name="generic"></param>
12
    /// <param name="toCheck"></param>
13
    /// <returns></returns>
14
    public static bool IsSubclassOfRawGeneric(this Type generic, Type toCheck)
15
    {
16
        while (toCheck != null && toCheck != typeof(object))
4✔
17
        {
18
            var cur = toCheck.IsGenericType ? toCheck.GetGenericTypeDefinition() : toCheck;
2!
19
            if (generic == cur)
2✔
20
            {
21
                return true;
1✔
22
            }
23
            toCheck = toCheck.BaseType;
1✔
24
        }
25
        return false;
2✔
26
    }
27

28
    public static bool IsList(this Type generic) => generic.IsGenericType && (generic.GetGenericTypeDefinition() == typeof(List<>));
8✔
29

30
    /// <summary>
31
    /// Gets the minimum value for a numeric type
32
    /// </summary>
33
    /// <param name="type"></param>
34
    /// <returns></returns>
35
    public static object GetNumericMinValue(this Type type)
36
    {
UNCOV
37
        type = Nullable.GetUnderlyingType(type) ?? type;
×
38

UNCOV
39
        if (type == typeof(int))
×
40
        {
UNCOV
41
            return int.MinValue;
×
42
        }
UNCOV
43
        else if (type == typeof(uint))
×
44
        {
UNCOV
45
            return uint.MinValue;
×
46
        }
UNCOV
47
        else if (type == typeof(long))
×
48
        {
UNCOV
49
            return long.MinValue;
×
50
        }
UNCOV
51
        else if (type == typeof(ulong))
×
52
        {
UNCOV
53
            return ulong.MinValue;
×
54
        }
UNCOV
55
        else if (type == typeof(short))
×
56
        {
UNCOV
57
            return short.MinValue;
×
58
        }
UNCOV
59
        else if (type == typeof(ushort))
×
60
        {
UNCOV
61
            return ushort.MinValue;
×
62
        }
UNCOV
63
        else if (type == typeof(byte))
×
64
        {
UNCOV
65
            return byte.MinValue;
×
66
        }
UNCOV
67
        else if (type == typeof(sbyte))
×
68
        {
UNCOV
69
            return sbyte.MinValue;
×
70
        }
UNCOV
71
        else if (type == typeof(float))
×
72
        {
73
            return float.MinValue;
×
74
        }
75
        else if (type == typeof(double))
×
76
        {
77
            return double.MinValue;
×
78
        }
79
        else if (type == typeof(decimal))
×
80
        {
81
            return decimal.MinValue;
×
82
        }
83
        else
84
        {
85
            throw new ArgumentException($"Type {type} is not a numeric type");
×
86
        }
87
    }
88

89
    /// <summary>
90
    /// Gets the maximum value for a numeric type
91
    /// </summary>
92
    /// <param name="type"></param>
93
    /// <returns></returns>
94
    public static object GetNumericMaxValue(this Type type)
95
    {
UNCOV
96
        type = Nullable.GetUnderlyingType(type) ?? type;
×
97

UNCOV
98
        if (type == typeof(int))
×
99
        {
UNCOV
100
            return int.MaxValue;
×
101
        }
UNCOV
102
        else if (type == typeof(uint))
×
103
        {
UNCOV
104
            return uint.MaxValue;
×
105
        }
UNCOV
106
        else if (type == typeof(long))
×
107
        {
UNCOV
108
            return long.MaxValue;
×
109
        }
UNCOV
110
        else if (type == typeof(ulong))
×
111
        {
UNCOV
112
            return ulong.MaxValue;
×
113
        }
UNCOV
114
        else if (type == typeof(short))
×
115
        {
UNCOV
116
            return short.MaxValue;
×
117
        }
UNCOV
118
        else if (type == typeof(ushort))
×
119
        {
UNCOV
120
            return ushort.MaxValue;
×
121
        }
UNCOV
122
        else if (type == typeof(byte))
×
123
        {
UNCOV
124
            return byte.MaxValue;
×
125
        }
UNCOV
126
        else if (type == typeof(sbyte))
×
127
        {
UNCOV
128
            return sbyte.MaxValue;
×
129
        }
UNCOV
130
        else if (type == typeof(float))
×
131
        {
132
            return float.MaxValue;
×
133
        }
134
        else if (type == typeof(double))
×
135
        {
136
            return double.MaxValue;
×
137
        }
138
        else if (type == typeof(decimal))
×
139
        {
140
            return decimal.MaxValue;
×
141
        }
142
        else
143
        {
144
            throw new ArgumentException($"Type {type} is not a numeric type");
×
145
        }
146
    }
147

148
    /// <summary>
149
    ///
150
    /// </summary>
151
    /// <param name="input"></param>
152
    /// <returns></returns>
153
    public static decimal ToDecimalSafe(object input)
154
    {
UNCOV
155
        if (input is decimal dec)
×
156
        {
UNCOV
157
            return dec;
×
158
        }
159

160
        if (input is double d)
×
161
        {
162
            if (d > (double)decimal.MaxValue)
×
163
            {
164
                return decimal.MaxValue;
×
165
            }
166
            else if (d < (double)decimal.MinValue)
×
167
            {
168
                return decimal.MinValue;
×
169
            }
170
            else
171
            {
172
                return (decimal)d;
×
173
            }
174
        }
175

176
        if (input is float f)
×
177
        {
UNCOV
178
            if (f > (float)decimal.MaxValue)
×
179
            {
180
                return decimal.MaxValue;
×
181
            }
UNCOV
182
            else if (f < (float)decimal.MinValue)
×
183
            {
UNCOV
184
                return decimal.MinValue;
×
185
            }
186
            else
187
            {
UNCOV
188
                return (decimal)f;
×
189
            }
190
        }
191

192
        try
193
        {
UNCOV
194
            return Convert.ToDecimal(input);
×
195
        }
196
        catch (OverflowException)
×
197
        {
198
            return decimal.MaxValue;
×
199
        }
200
    }
×
201
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc