generated from kasthack-labs/dotnet-repo-template
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
NotEmptyTestBase.cs
233 lines (156 loc) · 8.62 KB
/
NotEmptyTestBase.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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
namespace kasthack.NotEmpty.Tests;
#pragma warning disable SA1125, RCS1020 // We need explicit nullables here.
#pragma warning disable SA1129 // Testing nullable construction.
#pragma warning disable CA1825 // We want zero-length arrays.
#pragma warning disable SA1124 // Feature.
using System;
using System.Collections.Generic;
using global::Xunit;
using kasthack.NotEmpty.Core;
using kasthack.NotEmpty.Tests.SampleModels;
public abstract class NotEmptyTestBase
{
private readonly Action<object?, AssertOptions?> action;
protected NotEmptyTestBase(Action<object?, AssertOptions?> action) => this.action = action;
#region Object handling
[Fact]
public void ValueTupleWorks() => this.Action((1, 1));
[Fact]
public void AnonymousObjectWorks() => this.Action(new { Value = 1 });
[Fact]
public void NullThrows() => Assert.ThrowsAny<Exception>(() => this.Action(null));
[Fact]
public void NotNullEmptyObjectWorks() => this.Action(new object());
[Fact]
public void NotNullObjectWithNotNullPropertyWorks() => this.Action(new { Value = new object() });
[Fact]
public void NotNullObjectWithNullPropertyThrows() => Assert.ThrowsAny<Exception>(() => this.Action(new { Value = (object?)null, }));
[Fact]
public void DefaulValueInValueTupleThrows() => Assert.ThrowsAny<Exception>(() => this.Action((1, 0)));
[Fact]
public void NestedObjectWithNullPropertyThrows() => Assert.ThrowsAny<Exception>(() => this.Action(new { Property = new { Value = 0, } }));
#region Depth
[Fact(Skip = "OOM")]
public void InfititeDepthThrowsWhenAllowed() => Assert.ThrowsAny<Exception>(() => this.Action(new InfiniteNestedStruct(), new AssertOptions { MaxDepth = null }));
[Fact]
public void InfiniteDepthDoesntThrowWithDefaultSettings() => this.Action(new InfiniteNestedStruct());
[Fact]
public void InvalidDepthThrows() => Assert.ThrowsAny<Exception>(() => this.Action(0, new AssertOptions { MaxDepth = -1 }));
[Fact]
public void DepthZeroWorks() => Assert.ThrowsAny<Exception>(() => this.Action(0, new AssertOptions { MaxDepth = 0 }));
#endregion
#region Computed property
[Fact]
public void SampleClassWithComputedPropertyWorks() => this.Action(new SampleClassWithComputedProperty(1));
[Fact]
public void SampleClassWithDefaultComputedPropertyThrows() => Assert.ThrowsAny<Exception>(() => this.Action(new SampleClassWithComputedProperty(0)));
[Fact]
public void ComputedPropertyIsIgnoredWhenConfigured() => this.Action(new SampleClassWithComputedProperty(0), new() { IgnoreComputedProperties = true });
#endregion
#endregion
#region Numbers
[Fact]
public void NotDefaultNullableDecimalWorks() => this.Action(new { Value = new Nullable<decimal>(1m) });
[Fact]
public void NotDefaultDecimalWorks() => this.Action(new { Value = 1m });
[Fact]
public void NotDefaultIntWorks() => this.Action(new { Value = 1 });
[Fact]
public void BoxedNotDefaultPrimitiveWorks() => this.Action(new { Value = (object)1 });
[Fact]
public void NotNullNullableWorks() => this.Action(new { Value = (int?)1 });
[Fact]
public void BoxedNotNullNullableWorks() => this.Action(new { Value = (object)new Nullable<int>(1) });
[Fact]
public void DefaultPrimitiveThrows() => Assert.ThrowsAny<Exception>(() => this.Action(new { Value = 0 }));
[Fact]
public void BoxedDefaultPrimitiveThrows() => Assert.ThrowsAny<Exception>(() => this.Action(new { Value = (object)0 }));
[Fact]
public void BoxedDefaultPrimitiveThrowsAsRoot() => Assert.ThrowsAny<Exception>(() => this.Action(0));
[Fact]
public void NullNullableThrows() => Assert.ThrowsAny<Exception>(() => this.Action(new { Value = new Nullable<int>(), }));
[Fact]
#pragma warning disable SA1119 // Statement should not use unnecessary parenthesis
public void BoxedNullNullableThrows() => Assert.ThrowsAny<Exception>(() => this.Action(new { Value = (object?)(new Nullable<int>())!, }));
#pragma warning restore SA1119 // Statement should not use unnecessary parenthesis
#endregion
#region Enums
// no false-positive
[Fact]
public void NotDefaultEnumWorks() => this.Action(new { Value = EnumWithDefaultValueDefined.No });
// no false-negative
[Fact]
public void DefaultEnumThrows() => Assert.ThrowsAny<Exception>(() => this.Action(new { Value = default(EnumWithDefaultValueDefined) }));
[Fact]
public void DefaultEnumDoesntThrowWhenAllowed() => this.Action(new { Value = default(EnumWithDefaultValueDefined) }, new AssertOptions { AllowDefinedDefaultEnumValues = true });
[Fact]
public void DefaultEnumThrowsWhenAllowedButNotDefined() => Assert.ThrowsAny<Exception>(() => this.Action(new { Value = default(EnumWithoutDefaultValue) }, new AssertOptions { AllowDefinedDefaultEnumValues = true }));
#endregion
#region Strings
[Fact]
public void NotEmptyStringWorks() => this.Action("test string");
[Fact]
public void EmptyStringThrows() => Assert.ThrowsAny<Exception>(() => this.Action(string.Empty));
[Fact]
public void EmptyStringDoesntThrowWhenAllowed() => this.Action(string.Empty, new AssertOptions { AllowEmptyStrings = true, });
#endregion
#region Collections
#region Emptinness
[Fact]
public void NotEmptyListWorks() => this.Action(new List<object> { new object() });
[Fact]
public void NotEmptyArrayWorks() => this.Action(new object[] { new object() });
[Fact]
public void EmptyArrayThrows() => Assert.ThrowsAny<Exception>(() => this.Action(new object[] { }));
[Fact]
public void EmptyListThrows() => Assert.ThrowsAny<Exception>(() => this.Action(new List<object>()));
[Fact]
public void EmptyArrayDoesntThrowWhenAllowed() => this.Action(new object[] { }, new AssertOptions { AllowEmptyCollections = true, });
#endregion
#region Dictionaries
[Fact]
public void EmptyDictionaryThrows() => Assert.ThrowsAny<Exception>(() => this.Action(new Dictionary<string, string>()));
[Fact]
public void NotEmptyDictionaryWorks() => this.Action(new Dictionary<string, string> { { "key", "value" } });
[Fact]
public void NotEmptyDictionaryWithBoxedKeysWorks() => this.Action(new Dictionary<int, string> { { 1, "value" } });
[Fact]
public void DictionaryWithNullValuesThrows() => Assert.ThrowsAny<Exception>(() => this.Action(new Dictionary<string, string>() { { "key", null! }, }));
[Fact]
public void EmptyDictionaryDoesntThrowWhenAllowed() => this.Action(new Dictionary<string, string>(), new AssertOptions { AllowEmptyCollections = true, });
#endregion
#region Default elements
[Fact]
public void ArrayWithDefaultPrimitiveThrows() => Assert.ThrowsAny<Exception>(() => this.Action(new object[] { 1, 0 }));
[Fact]
public void ArrayWithDefaultPrimitiveDoesntThrowWhenAllowed() => this.Action(new object[] { 1, 0 }, new AssertOptions { AllowZerosInNumberArrays = true });
[Fact]
public void AllowZerosInNumberArraysWorksCorrectlyForChildren() => Assert.ThrowsAny<Exception>(() => this.Action(new[] { new { Value = 0 } }, new AssertOptions { AllowZerosInNumberArrays = true }));
[Fact]
public void ArrayWithNullThrows() => Assert.ThrowsAny<Exception>(() => this.Action(new object?[] { new object(), null }));
#endregion
#endregion
#region DateTime
[Fact]
public void EmptyDateTimeThrows() => Assert.ThrowsAny<Exception>(() => this.Action(default(DateTime)));
[Fact]
public void NotEmptyDateTimeWithSomeZeroesWorks() => this.Action(new DateTime(2000, 1, 1, 0, 0, 0));
#endregion
#region Booleans
[Fact]
public void FalseThrows() => Assert.ThrowsAny<Exception>(() => this.Action(new { Value = false }));
[Fact]
public void TrueWorks() => this.Action(true);
[Fact]
public void FalseDoesntThrowWhenAllowed() => this.Action(new { Value = false }, new AssertOptions { AllowFalseBooleanProperties = true });
[Fact]
public void FalseThrowsWhenAllowedForDifferentKind() => Assert.ThrowsAny<Exception>(() => this.Action(false, new AssertOptions { AllowFalseBooleanProperties = true }));
[Fact]
public void FalseThrowsWhenAllowedForDifferentKindV2() => Assert.ThrowsAny<Exception>(() => this.Action(new[] { false }, new AssertOptions { AllowFalseBooleanProperties = true }));
#endregion
protected void Action(object? value, AssertOptions? options = null) => this.action(value, options);
}
#pragma warning restore SA1125, RCS1020 // We need explicit nullables here.
#pragma warning restore SA1129 // Testing nullable construction.
#pragma warning restore CA1825 // We want zero-length arrays.
#pragma warning restore SA1124 // Feature.