-
Notifications
You must be signed in to change notification settings - Fork 315
/
Ensure.cs
213 lines (197 loc) · 9.95 KB
/
Ensure.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
using System;
using System.Collections.Generic;
using System.Linq;
namespace SaasKit.Multitenancy
{
using System.Diagnostics;
/// <summary>
/// Helper class that will throw exceptions when conditions are not satisfied.
/// </summary>
[DebuggerStepThrough]
public static class Ensure
{
/// <summary>
/// Ensures that the given expression is true
/// </summary>
/// <exception cref="System.Exception">Exception thrown if false condition</exception>
/// <param name="condition">Condition to test/ensure</param>
/// <param name="message">Message for the exception</param>
/// <exception cref="System.Exception">Thrown when <paramref name="condition"/> is false</exception>
public static void That(bool condition, string message = "")
{
That<Exception>(condition, message);
}
/// <summary>
/// Ensures that the given expression is true
/// </summary>
/// <typeparam name="TException">Type of exception to throw</typeparam>
/// <param name="condition">Condition to test/ensure</param>
/// <param name="message">Message for the exception</param>
/// <exception cref="TException">Thrown when <paramref name="condition"/> is false</exception>
/// <remarks><see cref="TException"/> must have a constructor that takes a single string</remarks>
public static void That<TException>(bool condition, string message = "") where TException : Exception
{
if (!condition)
{
throw (TException)Activator.CreateInstance(typeof(TException), message);
}
}
/// <summary>
/// Ensures given condition is false
/// </summary>
/// <typeparam name="TException">Type of exception to throw</typeparam>
/// <param name="condition">Condition to test</param>
/// <param name="message">Message for the exception</param>
/// <exception cref="TException">Thrown when <paramref name="condition"/> is true</exception>
/// <remarks><see cref="TException"/> must have a constructor that takes a single string</remarks>
public static void Not<TException>(bool condition, string message = "") where TException : Exception
{
That<TException>(!condition, message);
}
/// <summary>
/// Ensures given condition is false
/// </summary>
/// <param name="condition">Condition to test</param>
/// <param name="message">Message for the exception</param>
/// <exception cref="System.Exception">Thrown when <paramref name="condition"/> is true</exception>
public static void Not(bool condition, string message = "")
{
Not<Exception>(condition, message);
}
/// <summary>
/// Ensures given object is not null
/// </summary>
/// <param name="value">Value of the object to test for null reference</param>
/// <param name="message">Message for the Null Reference Exception</param>
/// <exception cref="System.NullReferenceException">Thrown when <paramref name="value"/> is null</exception>
public static void NotNull(object value, string message = "")
{
That<NullReferenceException>(value != null, message);
}
/// <summary>
/// Ensures given string is not null or empty
/// </summary>
/// <param name="value">String value to compare</param>
/// <param name="message">Message of the exception if value is null or empty</param>
/// <exception cref="System.Exception">string value is null or empty</exception>
public static void NotNullOrEmpty(string value, string message = "String cannot be null or empty")
{
That(!string.IsNullOrEmpty(value), message);
}
/// <summary>
/// Ensures given objects are equal
/// </summary>
/// <typeparam name="T">Type of objects to compare for equality</typeparam>
/// <param name="left">First Value to Compare</param>
/// <param name="right">Second Value to Compare</param>
/// <param name="message">Message of the exception when values equal</param>
/// <exception cref="System.Exception">Exception is thrown when <paramref cref="left"/> not equal to <paramref cref="right"/></exception>
/// <remarks>Null values will cause an exception to be thrown</remarks>
public static void Equal<T>(T left, T right, string message = "Values must be equal")
{
That(left != null && right != null && left.Equals(right), message);
}
/// <summary>
/// Ensures given objects are not equal
/// </summary>
/// <typeparam name="T">Type of objects to compare for equality</typeparam>
/// <param name="left">First Value to Compare</param>
/// <param name="right">Second Value to Compare</param>
/// <param name="message">Message of the exception when values equal</param>
/// <exception cref="System.Exception">Thrown when <paramref cref="left"/> equal to <paramref cref="right"/></exception>
/// <remarks>Null values will cause an exception to be thrown</remarks>
public static void NotEqual<T>(T left, T right, string message = "Values must not be equal")
{
That(left != null && right != null && !left.Equals(right), message);
}
/// <summary>
/// Ensures given collection contains a value that satisfied a predicate
/// </summary>
/// <typeparam name="T">Collection type</typeparam>
/// <param name="collection">Collection to test</param>
/// <param name="predicate">Predicate where one value in the collection must satisfy</param>
/// <param name="message">Message of the exception if value not found</param>
/// <exception cref="System.Exception">
/// Thrown if collection is null, empty or doesn't contain a value that satisfies <paramref cref="predicate"/>
/// </exception>
public static void Contains<T>(IEnumerable<T> collection, Func<T, bool> predicate, string message = "")
{
That(collection != null && collection.Any(predicate), message);
}
/// <summary>
/// Ensures ALL items in the given collection satisfy a predicate
/// </summary>
/// <typeparam name="T">Collection type</typeparam>
/// <param name="collection">Collection to test</param>
/// <param name="predicate">Predicate that ALL values in the collection must satisfy</param>
/// <param name="message">Message of the exception if not all values are valid</param>
/// <exception cref="System.Exception">
/// Thrown if collection is null, empty or not all values satisfies <paramref cref="predicate"/>
/// </exception>
public static void Items<T>(IEnumerable<T> collection, Func<T, bool> predicate, string message = "")
{
That(collection != null && !collection.Any(x => !predicate(x)), message);
}
/// <summary>
/// Argument-specific ensure methods
/// </summary>
public static class Argument
{
/// <summary>
/// Ensures given condition is true
/// </summary>
/// <param name="condition">Condition to test</param>
/// <param name="message">Message of the exception if condition fails</param>
/// <exception cref="System.ArgumentException">
/// Thrown if <paramref cref="condition"/> is false
/// </exception>
public static void Is(bool condition, string message = "")
{
That<ArgumentException>(condition, message);
}
/// <summary>
/// Ensures given condition is false
/// </summary>
/// <param name="condition">Condition to test</param>
/// <param name="message">Message of the exception if condition is true</param>
/// <exception cref="System.ArgumentException">
/// Thrown if <paramref cref="condition"/> is true
/// </exception>
public static void IsNot(bool condition, string message = "")
{
Is(!condition, message);
}
/// <summary>
/// Ensures given value is not null
/// </summary>
/// <param name="value">Value to test for null</param>
/// <param name="paramName">Name of the parameter in the method</param>
/// <exception cref="System.ArgumentNullException">
/// Thrown if <paramref cref="value" /> is null
/// </exception>
public static void NotNull(object value, string paramName = "")
{
That<ArgumentNullException>(value != null, paramName);
}
/// <summary>
/// Ensures the given string value is not null or empty
/// </summary>
/// <param name="value">Value to test for null or empty</param>
/// <param name="paramName">Name of the parameter in the method</param>
/// <exception cref="System.ArgumentException">
/// Thrown if <paramref cref="value"/> is null or empty string
/// </exception>
public static void NotNullOrEmpty(string value, string paramName = "")
{
if (value == null)
{
throw new ArgumentNullException(paramName, "String value cannot be null");
}
if (string.Empty.Equals(value))
{
throw new ArgumentException("String value cannot be empty", paramName);
}
}
}
}
}