This repository was archived by the owner on Jun 21, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathGuard.cs
98 lines (91 loc) · 3.05 KB
/
Guard.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
using NullGuard;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.Linq;
namespace GitHub.Extensions
{
public static class Guard
{
public static void ArgumentNotNull([AllowNull]object value, string name)
{
if (value != null) return;
string message = String.Format(CultureInfo.InvariantCulture, "Failed Null Check on '{0}'", name);
#if DEBUG
if (!InUnitTestRunner())
{
Debug.Fail(message);
}
#endif
throw new ArgumentNullException(name, message);
}
public static void ArgumentNonNegative(int value, string name)
{
if (value > -1) return;
var message = String.Format(CultureInfo.InvariantCulture, "The value for '{0}' must be non-negative", name);
#if DEBUG
if (!InUnitTestRunner())
{
Debug.Fail(message);
}
#endif
throw new ArgumentException(message, name);
}
/// <summary>
/// Checks a string argument to ensure it isn't null or empty.
/// </summary>
/// <param name = "value">The argument value to check.</param>
/// <param name = "name">The name of the argument.</param>
public static void ArgumentNotEmptyString(string value, string name)
{
if (value.Length > 0) return;
string message = String.Format(CultureInfo.InvariantCulture, "The value for '{0}' must not be empty", name);
#if DEBUG
if (!InUnitTestRunner())
{
Debug.Fail(message);
}
#endif
throw new ArgumentException(message, name);
}
public static void ArgumentInRange(int value, int minValue, string name)
{
if (value >= minValue) return;
string message = String.Format(CultureInfo.InvariantCulture,
"The value '{0}' for '{1}' must be greater than or equal to '{2}'",
value,
name,
minValue);
#if DEBUG
if (!InUnitTestRunner())
{
Debug.Fail(message);
}
#endif
throw new ArgumentOutOfRangeException(name, message);
}
public static void ArgumentInRange(int value, int minValue, int maxValue, string name)
{
if (value >= minValue && value <= maxValue) return;
string message = String.Format(CultureInfo.InvariantCulture,
"The value '{0}' for '{1}' must be greater than or equal to '{2}' and less than or equal to '{3}'",
value,
name,
minValue,
maxValue);
#if DEBUG
if (!InUnitTestRunner())
{
Debug.Fail(message);
}
#endif
throw new ArgumentOutOfRangeException(name, message);
}
// Borrowed from Splat.
static bool InUnitTestRunner()
{
return Splat.ModeDetector.InUnitTestRunner();
}
}
}