2
2
using System . Collections . Generic ;
3
3
using System . Diagnostics ;
4
4
using System . Linq ;
5
+ // ReSharper disable PossibleMultipleEnumeration
6
+ // ReSharper disable MemberCanBePrivate.Global
7
+ // ReSharper disable UnusedMember.Global
8
+ // ReSharper disable ParameterOnlyUsedForPreconditionCheck.Global
9
+ // ReSharper disable UnusedMethodReturnValue.Global
5
10
6
11
namespace KY . Core
7
12
{
8
13
public static class AssertionExtensions
9
14
{
15
+ private const string DefaultMessage = "[Message will be replaced in method body]" ;
16
+
10
17
[ DebuggerHidden ]
11
- public static double AssertIsPositive ( this double self , string argumentName = "" )
18
+ public static double AssertIsPositive ( this double self , string argumentName = "" , string message = "Has to be positive" )
12
19
{
13
20
if ( self <= 0 )
14
- throw new ArgumentOutOfRangeException ( argumentName , self , "Has to be positive!" ) ;
21
+ {
22
+ throw new ArgumentOutOfRangeException ( argumentName , self , message ) ;
23
+ }
15
24
16
25
return self ;
17
26
}
18
27
19
28
[ DebuggerHidden ]
20
- public static double AssertHasValueAndIsPositive ( this double ? self , string argumentName = "" )
29
+ public static double AssertIsPositive ( this double ? self , string argumentName = "" , string message = "Has to be positive ")
21
30
{
22
- return self . AssertIsNotNull ( argumentName ) . AssertIsPositive ( argumentName ) ;
31
+ return self . AssertIsNotNull ( argumentName , message ) . AssertIsPositive ( argumentName , message ) ;
23
32
}
24
33
25
34
[ DebuggerHidden ]
26
- public static double ? AssertIsNullOrPositive ( this double ? self , string argumentName = "" )
35
+ public static double ? AssertIsNullOrPositive ( this double ? self , string argumentName = "" , string message = "Has to be null or positive" )
27
36
{
28
- if ( self . HasValue )
29
- self . Value . AssertIsPositive ( argumentName ) ;
30
-
37
+ self ? . AssertIsPositive ( argumentName , message ) ;
31
38
return self ;
32
39
}
33
40
34
41
[ DebuggerHidden ]
35
- public static int AssertIsNotNegative ( this int self , string argumentName = "" )
42
+ public static int AssertIsNotNegative ( this int self , string argumentName = "" , string message = "Has to be positive or 0" )
36
43
{
37
44
if ( self < 0 )
38
- throw new ArgumentOutOfRangeException ( argumentName , self , "Has to be positive or 0!" ) ;
39
-
45
+ {
46
+ throw new ArgumentOutOfRangeException ( argumentName , self , message ) ;
47
+ }
40
48
return self ;
41
49
}
42
50
43
51
[ DebuggerHidden ]
44
- public static int AssertIsPositive ( this int self , string argumentName = "" )
52
+ public static int AssertIsPositive ( this int self , string argumentName = "" , string message = "Has to be positive" )
45
53
{
46
54
if ( self <= 0 )
47
- throw new ArgumentOutOfRangeException ( argumentName , self , "Has to be positive!" ) ;
48
-
55
+ {
56
+ throw new ArgumentOutOfRangeException ( argumentName , self , message ) ;
57
+ }
49
58
return self ;
50
59
}
51
60
52
61
[ DebuggerHidden ]
53
- public static long AssertIsNotNegative ( this long self , string argumentName = "" )
62
+ public static long AssertIsNotNegative ( this long self , string argumentName = "" , string message = "Has to be positive or 0" )
54
63
{
55
64
if ( self < 0 )
56
- throw new ArgumentOutOfRangeException ( argumentName , self , "Has to be positive or 0!" ) ;
57
-
65
+ {
66
+ throw new ArgumentOutOfRangeException ( argumentName , self , message ) ;
67
+ }
58
68
return self ;
59
69
}
60
70
61
71
[ DebuggerHidden ]
62
- public static long AssertIsPositive ( this long self , string argumentName = "" )
72
+ public static long AssertIsPositive ( this long self , string argumentName = "" , string message = "Has to be positive" )
63
73
{
64
74
if ( self <= 0 )
65
- throw new ArgumentOutOfRangeException ( argumentName , self , "Has to be positive!" ) ;
66
-
75
+ {
76
+ throw new ArgumentOutOfRangeException ( argumentName , self , message ) ;
77
+ }
67
78
return self ;
68
79
}
69
80
70
81
[ DebuggerHidden ]
71
- public static long AssertHasValueAndIsPositive ( this long ? self , string argumentName = "" )
82
+ public static long AssertIsPositive ( this long ? self , string argumentName = "" , string message = "Has to be positive ")
72
83
{
73
- return self . AssertIsNotNull ( argumentName ) . AssertIsPositive ( argumentName ) ;
84
+ return self . AssertIsNotNull ( argumentName , message ) . AssertIsPositive ( argumentName , message ) ;
74
85
}
75
86
76
87
[ DebuggerHidden ]
77
- public static long ? AssertIsNullOrPositive ( this long ? self , string argumentName = "" )
88
+ public static long ? AssertIsNullOrPositive ( this long ? self , string argumentName = "" , string message = "Has to be null or positive" )
78
89
{
79
- if ( self . HasValue )
80
- self . Value . AssertIsPositive ( argumentName ) ;
81
-
90
+ self ? . AssertIsPositive ( argumentName , message ) ;
82
91
return self ;
83
92
}
84
93
85
94
[ DebuggerHidden ]
86
- public static string AssertIsNotLongerThan ( this string reference , int maxLength , string parameterName = null )
95
+ public static string AssertIsNotLongerThan ( this string reference , int maxLength , string parameterName = null , string message = DefaultMessage )
87
96
{
88
97
if ( reference . Length > maxLength )
89
- throw new ArgumentException ( parameterName , string . Format ( "String is too long. Max: {0} / Acctual: {1}" , maxLength , reference . Length ) ) ;
98
+ {
99
+ throw new ArgumentException ( parameterName , message == DefaultMessage ? $ "String is too long. Max: { maxLength } / Actual: { reference . Length } " : message ) ;
100
+ }
90
101
return reference ;
91
102
}
92
103
93
104
[ DebuggerHidden ]
94
- public static string AssertIsNotNullOrEmpty ( this string reference , string parameterName = null )
105
+ public static string AssertIsNotNullOrEmpty ( this string reference , string parameterName = null , string message = "Cannot be null or empty" )
95
106
{
96
- reference . AssertIsNotNull ( parameterName ) ;
97
107
if ( string . IsNullOrEmpty ( reference ) )
98
- throw new ArgumentOutOfRangeException ( parameterName , "Cannot be empty." ) ;
108
+ {
109
+ throw new ArgumentOutOfRangeException ( parameterName , message ) ;
110
+ }
99
111
return reference ;
100
112
}
101
113
102
114
[ DebuggerHidden ]
103
- public static T AssertIsNotNull < T > ( this T reference , string parameterName = null )
115
+ public static T AssertIsNotNull < T > ( this T reference , string parameterName = null , string message = "Cannot be null" )
104
116
where T : class
105
117
{
106
118
if ( reference == null )
107
- throw new ArgumentNullException ( parameterName ?? typeof ( T ) . FullName , "Cannot be null" ) ;
119
+ {
120
+ throw new ArgumentNullException ( parameterName ?? typeof ( T ) . FullName , message ) ;
121
+ }
108
122
return reference ;
109
123
}
110
124
111
125
[ DebuggerHidden ]
112
- public static T AssertIsNotNull < T > ( this T ? reference , string parameterName = null )
126
+ public static T AssertIsNotNull < T > ( this T ? reference , string parameterName = null , string message = "Cannot be null" )
113
127
where T : struct
114
128
{
115
129
if ( reference == null )
116
- throw new ArgumentNullException ( parameterName ?? typeof ( T ) . FullName , "Cannot be null" ) ;
130
+ {
131
+ throw new ArgumentNullException ( parameterName ?? typeof ( T ) . FullName , message ) ;
132
+ }
117
133
return reference . Value ;
118
134
}
119
135
120
136
[ DebuggerHidden ]
121
- public static IEnumerable < long > AssertAllElementsArePositive ( this IEnumerable < long > self , string parameterName = null )
137
+ public static IEnumerable < long > AssertAllElementsArePositive ( this IEnumerable < long > self , string parameterName = null , string message = "All Elements have to be positive" )
122
138
{
123
139
if ( self . Any ( l => l <= 0 ) )
124
- throw new ArgumentOutOfRangeException ( parameterName , self , "All Elements have to be positive!" ) ;
125
-
140
+ {
141
+ throw new ArgumentOutOfRangeException ( parameterName , self , message ) ;
142
+ }
126
143
return self ;
127
144
}
128
145
129
146
[ DebuggerHidden ]
130
- public static IEnumerable < T > AssertContains < T > ( this IEnumerable < T > collection , T item , string argumentName = "" )
147
+ public static IEnumerable < T > AssertContains < T > ( this IEnumerable < T > collection , T item , string argumentName = "" , string message = DefaultMessage )
131
148
{
132
149
collection . AssertIsNotNull ( argumentName ) ;
133
- if ( ! collection . Contains ( item ) )
134
- throw new InvalidOperationException ( argumentName + " collection have to contains child" ) ;
150
+ if ( collection . Contains ( item ) )
151
+ {
152
+ throw new InvalidOperationException ( message == DefaultMessage ? argumentName + " collection have to contains child" : message ) ;
153
+ }
135
154
return collection ;
136
155
}
137
156
138
157
[ DebuggerHidden ]
139
- public static IEnumerable < T > AssertIsNotNullOrEmpty < T > ( this IEnumerable < T > collection , string argumentName = "" )
158
+ public static IEnumerable < T > AssertIsNotNullOrEmpty < T > ( this IEnumerable < T > collection , string argumentName = "" , string message = DefaultMessage )
140
159
{
141
160
collection . AssertIsNotNull ( argumentName ) ;
142
- if ( ! collection . Any ( ) )
143
- throw new InvalidOperationException ( argumentName + " collection have to contain any child" ) ;
161
+ if ( collection . Any ( ) )
162
+ {
163
+ throw new InvalidOperationException ( message == DefaultMessage ? argumentName + " collection have to contain any child" : message ) ;
164
+ }
144
165
return collection ;
145
166
}
146
167
147
168
[ DebuggerHidden ]
148
- public static T AssertIs < T > ( this T self , T expected , string argumentName = "" )
169
+ public static T AssertIs < T > ( this T self , T expected , string argumentName = "" , string message = DefaultMessage )
149
170
{
150
171
if ( Equals ( self , expected ) )
172
+ {
151
173
return self ;
152
-
153
- throw new ArgumentOutOfRangeException ( argumentName , self , "Has to be " + expected ) ;
174
+ }
175
+ throw new ArgumentOutOfRangeException ( argumentName , self , message == DefaultMessage ? "Has to be " + expected : message ) ;
154
176
}
155
177
156
178
[ DebuggerHidden ]
157
- public static T AssertIsNot < T > ( this T self , T notExpected , string argumentName = "" )
179
+ public static T AssertIsNot < T > ( this T self , T notExpected , string argumentName = "" , string message = DefaultMessage )
158
180
{
159
- if ( ! Equals ( self , notExpected ) )
181
+ if ( Equals ( self , notExpected ) )
182
+ {
160
183
return self ;
184
+ }
161
185
162
- throw new ArgumentOutOfRangeException ( argumentName , self , "Mustn't be " + notExpected ) ;
186
+ throw new ArgumentOutOfRangeException ( argumentName , self , message == DefaultMessage ? "Mustn't be " + notExpected : message ) ;
163
187
}
164
188
}
165
189
}
0 commit comments