-
-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathDebugGuardTests.cs
More file actions
234 lines (204 loc) · 7.99 KB
/
Copy pathDebugGuardTests.cs
File metadata and controls
234 lines (204 loc) · 7.99 KB
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
// Copyright (c) Six Labors and contributors.
// Licensed under the Apache License, Version 2.0.
// tell this file to enable debug conditional method calls, i.e. all the debug guard calls
#define DEBUG
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using Xunit;
namespace SixLabors
{
public class DebugGuardTests
{
private class Foo
{
}
[Fact]
public void AllStaticMethodsOnOnDebugGuardHaveDEBUGConditional()
{
IEnumerable<MethodInfo> methods = typeof(DebugGuard).GetTypeInfo().GetMethods()
.Where(x => x.IsStatic);
foreach (MethodInfo m in methods)
{
IEnumerable<ConditionalAttribute> attribs = m.GetCustomAttributes<ConditionalAttribute>();
Assert.True(attribs.Select(x => x.ConditionString).Contains("DEBUG"), $"Method '{m.Name}' does not have [Conditional(\"DEBUG\")] set.");
}
}
[Fact]
public void NotNull_WhenNull_Throws()
{
Foo foo = null;
Assert.Throws<ArgumentNullException>(() => Guard.NotNull(foo, nameof(foo)));
}
[Fact]
public void NotNull_WhenNotNull()
{
var foo = new Foo();
Guard.NotNull(foo, nameof(foo));
}
[Theory]
[InlineData(null, true)]
[InlineData("", true)]
[InlineData(" ", true)]
[InlineData("$", false)]
[InlineData("lol", false)]
public void NotNullOrWhiteSpace(string str, bool shouldThrow)
{
if (shouldThrow)
{
Assert.ThrowsAny<ArgumentException>(() => Guard.NotNullOrWhiteSpace(str, nameof(str)));
}
else
{
Guard.NotNullOrWhiteSpace(str, nameof(str));
}
}
[Theory]
[InlineData(true)]
[InlineData(false)]
public void IsTrue(bool value)
{
if (!value)
{
Assert.Throws<ArgumentException>(() => Guard.IsTrue(value, nameof(value), "Boo!"));
}
else
{
Guard.IsTrue(value, nameof(value), "Boo.");
}
}
[Theory]
[InlineData(true)]
[InlineData(false)]
public void IsFalse(bool value)
{
if (value)
{
Assert.Throws<ArgumentException>(() => Guard.IsFalse(value, nameof(value), "Boo!"));
}
else
{
Guard.IsFalse(value, nameof(value), "Boo.");
}
}
public static readonly TheoryData<int, int, bool> SizeCheckData = new TheoryData<int, int, bool>
{
{ 0, 0, false },
{ 1, 1, false },
{ 1, 0, false },
{ 13, 13, false },
{ 20, 13, false },
{ 12, 13, true },
{ 0, 1, true },
};
[Theory]
[MemberData(nameof(SizeCheckData))]
public void MustBeSizedAtLeast(int length, int minLength, bool shouldThrow)
{
int[] data = new int[length];
if (shouldThrow)
{
Assert.Throws<ArgumentException>(() => Guard.MustBeSizedAtLeast((Span<int>)data, minLength, nameof(data)));
Assert.Throws<ArgumentException>(() => Guard.MustBeSizedAtLeast((ReadOnlySpan<int>)data, minLength, nameof(data)));
}
else
{
Guard.MustBeSizedAtLeast((Span<int>)data, minLength, nameof(data));
Guard.MustBeSizedAtLeast((ReadOnlySpan<int>)data, minLength, nameof(data));
}
}
[Theory]
[MemberData(nameof(SizeCheckData))]
public void DestinationShouldNotBeTooShort(int destLength, int sourceLength, bool shouldThrow)
{
int[] dest = new int[destLength];
int[] source = new int[sourceLength];
if (shouldThrow)
{
Assert.Throws<ArgumentException>(() => Guard.DestinationShouldNotBeTooShort((Span<int>)source, (Span<int>)dest, nameof(dest)));
Assert.Throws<ArgumentException>(() => Guard.DestinationShouldNotBeTooShort((ReadOnlySpan<int>)source, (Span<int>)dest, nameof(dest)));
}
else
{
Guard.DestinationShouldNotBeTooShort((Span<int>)source, (Span<int>)dest, nameof(dest));
Guard.DestinationShouldNotBeTooShort((ReadOnlySpan<int>)source, (Span<int>)dest, nameof(dest));
}
}
[Fact]
public void MustBeLessThan_IsLess_ThrowsNoException()
{
DebugGuard.MustBeLessThan(0, 1, "myParamName");
}
[Theory]
[InlineData(2, 1)]
[InlineData(1, 1)]
public void MustBeLessThan_IsGreaterOrEqual_ThrowsNoException(int value, int max)
{
ArgumentOutOfRangeException exception = Assert.Throws<ArgumentOutOfRangeException>(
() => DebugGuard.MustBeLessThan(value, max, "myParamName"));
Assert.Equal("myParamName", exception.ParamName);
Assert.Contains($"Value {value} must be less than {max}.", exception.Message);
}
[Theory]
[InlineData(0, 1)]
[InlineData(1, 1)]
public void MustBeLessThanOrEqualTo_IsLessOrEqual_ThrowsNoException(int value, int max)
{
DebugGuard.MustBeLessThanOrEqualTo(value, max, "myParamName");
}
[Fact]
public void MustBeLessThanOrEqualTo_IsGreater_ThrowsNoException()
{
ArgumentOutOfRangeException exception = Assert.Throws<ArgumentOutOfRangeException>(() => DebugGuard.MustBeLessThanOrEqualTo(2, 1, "myParamName"));
Assert.Equal("myParamName", exception.ParamName);
Assert.Contains($"Value 2 must be less than or equal to 1.", exception.Message);
}
[Fact]
public void MustBeGreaterThan_IsGreater_ThrowsNoException()
{
DebugGuard.MustBeGreaterThan(2, 1, "myParamName");
}
[Theory]
[InlineData(1, 2)]
[InlineData(1, 1)]
public void MustBeGreaterThan_IsLessOrEqual_ThrowsNoException(int value, int min)
{
ArgumentOutOfRangeException exception = Assert.Throws<ArgumentOutOfRangeException>(
() => DebugGuard.MustBeGreaterThan(value, min, "myParamName"));
Assert.Equal("myParamName", exception.ParamName);
Assert.Contains($"Value {value} must be greater than {min}.", exception.Message);
}
[Theory]
[InlineData(2, 1)]
[InlineData(1, 1)]
public void MustBeGreaterThanOrEqualTo_IsGreaterOrEqual_ThrowsNoException(int value, int min)
{
DebugGuard.MustBeGreaterThanOrEqualTo(value, min, "myParamName");
}
[Fact]
public void MustBeGreaterThanOrEqualTo_IsLess_ThrowsNoException()
{
ArgumentOutOfRangeException exception = Assert.Throws<ArgumentOutOfRangeException>(
() => DebugGuard.MustBeGreaterThanOrEqualTo(1, 2, "myParamName"));
Assert.Equal("myParamName", exception.ParamName);
Assert.Contains($"Value 1 must be greater than or equal to 2.", exception.Message);
}
[Theory]
[InlineData(new int[] { 1, 2 }, 1)]
[InlineData(new int[] { 1, 2 }, 2)]
public void MustBeSizedAtLeast_Array_LengthIsGreaterOrEqual_ThrowsNoException(int[] value, int minLength)
{
DebugGuard.MustBeSizedAtLeast<int>(value, minLength, "myParamName");
}
[Fact]
public void MustBeSizedAtLeast_Array_LengthIsLess_ThrowsException()
{
ArgumentException exception = Assert.Throws<ArgumentException>(
() => DebugGuard.MustBeSizedAtLeast<int>(new int[] { 1, 2 }, 3, "myParamName"));
Assert.Equal("myParamName", exception.ParamName);
Assert.Contains($"The size must be at least 3.", exception.Message);
}
}
}