Skip to content

Commit 475ca13

Browse files
authored
Add unit tests for new nullable attributes (#225)
1 parent 0302a9b commit 475ca13

8 files changed

+400
-9
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,282 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
using System;
5+
using System.Diagnostics.CodeAnalysis;
6+
using nanoFramework.TestFramework;
7+
8+
#nullable enable
9+
// ReSharper disable RedundantNullableFlowAttribute
10+
namespace NFUnitTestAttributes.Diagnostics.CodeAnalysis
11+
{
12+
[TestClass]
13+
public class NullableAttributesTests
14+
{
15+
private static void AssertDoesNotThrow(Action unitTest)
16+
{
17+
Exception? exception = null;
18+
19+
try
20+
{
21+
unitTest();
22+
}
23+
catch (Exception ex)
24+
{
25+
exception = ex;
26+
}
27+
28+
Assert.IsNull(exception, $"Expected no exception but got: {exception}");
29+
}
30+
31+
[TestMethod]
32+
public void Validate_AllowNullAttribute()
33+
{
34+
AssertDoesNotThrow(() =>
35+
{
36+
var sut = new AllowNullAttributeTestClass();
37+
sut.Method(null);
38+
});
39+
}
40+
41+
[TestMethod]
42+
public void Validate_DisallowNullAttribute()
43+
{
44+
AssertDoesNotThrow(() =>
45+
{
46+
var sut = new DisallowNullAttributeTestClass();
47+
sut.Method(null!);
48+
});
49+
}
50+
51+
[TestMethod]
52+
public void Validate_DoesNotReturnAttribute()
53+
{
54+
var sut = new DoesNotReturnAttributeTestClass();
55+
56+
Assert.ThrowsException(typeof(NotImplementedException), () => sut.Method());
57+
}
58+
59+
[TestMethod]
60+
public void Validate_DoesNotReturnIfAttribute()
61+
{
62+
var sut = new DoesNotReturnIfAttributeTestClass();
63+
64+
Assert.ThrowsException(typeof(NotImplementedException), () => sut.Method(true));
65+
}
66+
67+
[TestMethod]
68+
public void Validate_MaybeNullAttribute()
69+
{
70+
AssertDoesNotThrow(() =>
71+
{
72+
var sut = new MaybeNullAttributeTestClass();
73+
sut.Method(null!);
74+
});
75+
}
76+
77+
[TestMethod]
78+
public void Validate_MaybeNullWhenAttribute()
79+
{
80+
AssertDoesNotThrow(() =>
81+
{
82+
var sut = new MaybeNullWhenAttributeTestClass();
83+
sut.Method(null!);
84+
});
85+
}
86+
87+
[TestMethod]
88+
public void Validate_MemberNotNullAttribute()
89+
{
90+
AssertDoesNotThrow(() =>
91+
{
92+
var sut = new MemberNotNullAttributeTestClass();
93+
sut.Method(null!);
94+
});
95+
}
96+
97+
[TestMethod]
98+
public void Validate_MemberNotNullWhenAttribute()
99+
{
100+
AssertDoesNotThrow(() =>
101+
{
102+
var sut = new MemberNotNullWhenAttributeTestClass();
103+
sut.Method();
104+
});
105+
}
106+
107+
[TestMethod]
108+
public void Validate_NotNullAttribute()
109+
{
110+
AssertDoesNotThrow(() =>
111+
{
112+
var sut = new NotNullAttributeTestClass();
113+
sut.Method(null!);
114+
});
115+
}
116+
117+
[TestMethod]
118+
public void Validate_NotNullIfNotNullAttribute()
119+
{
120+
AssertDoesNotThrow(() =>
121+
{
122+
var sut = new NotNullIfNotNullAttributeTestClass();
123+
sut.Method(null!);
124+
});
125+
}
126+
127+
[TestMethod]
128+
public void Validate_NotNullWhenAttribute()
129+
{
130+
AssertDoesNotThrow(() =>
131+
{
132+
var sut = new NotNullWhenAttributeTestClass();
133+
sut.Method(null!);
134+
});
135+
}
136+
}
137+
138+
#region Test classes
139+
public class AllowNullAttributeTestClass
140+
{
141+
[AllowNull]
142+
public string? Field;
143+
144+
[AllowNull]
145+
public string Property { get; set; }
146+
147+
public void Method([AllowNull] string? value)
148+
{
149+
// Do nothing
150+
}
151+
}
152+
153+
public class DisallowNullAttributeTestClass
154+
{
155+
[DisallowNull]
156+
public string? Field = null!;
157+
158+
[DisallowNull]
159+
public string? Property { get; set; } = null!;
160+
161+
public void Method([DisallowNull] string value)
162+
{
163+
// Do nothing
164+
}
165+
}
166+
167+
public class DoesNotReturnAttributeTestClass
168+
{
169+
[DoesNotReturn]
170+
public void Method()
171+
{
172+
throw new NotImplementedException();
173+
}
174+
}
175+
176+
public class DoesNotReturnIfAttributeTestClass
177+
{
178+
public void Method([DoesNotReturnIf(true)] bool value)
179+
{
180+
if (value)
181+
{
182+
throw new NotImplementedException();
183+
}
184+
}
185+
}
186+
187+
public class MaybeNullAttributeTestClass
188+
{
189+
[MaybeNull]
190+
public string? Field = null!;
191+
192+
[MaybeNull]
193+
public string? Property { get; set; } = null!;
194+
195+
[MaybeNull]
196+
public string? this[string? value] => Field;
197+
198+
public string Method([MaybeNull] string value)
199+
{
200+
return null!;
201+
}
202+
}
203+
204+
public class MaybeNullWhenAttributeTestClass
205+
{
206+
public bool Method([MaybeNullWhen(true)] string value)
207+
{
208+
return true;
209+
}
210+
}
211+
212+
public class MemberNotNullAttributeTestClass
213+
{
214+
public string? Field = null!;
215+
216+
[MemberNotNull(nameof(Field))]
217+
public string Property { get; set; } = null!;
218+
219+
[MemberNotNull(nameof(Field))]
220+
public void Method(string value)
221+
{
222+
Field = value;
223+
}
224+
}
225+
226+
public class MemberNotNullWhenAttributeTestClass
227+
{
228+
public string? Field = null!;
229+
230+
[MemberNotNullWhen(true, nameof(Field))]
231+
public bool Property { get; set; } = true;
232+
233+
[MemberNotNullWhen(true, nameof(Field))]
234+
public bool Method()
235+
{
236+
Field = string.Empty;
237+
return true;
238+
}
239+
}
240+
241+
public class NotNullAttributeTestClass
242+
{
243+
[NotNull]
244+
public string? Field = null!;
245+
246+
[NotNull]
247+
public string? Property { get; set; } = null!;
248+
249+
[NotNull]
250+
public string this[string? value] => value!;
251+
252+
public string Method([NotNull] string value)
253+
{
254+
return null!;
255+
}
256+
}
257+
258+
public class NotNullIfNotNullAttributeTestClass
259+
{
260+
public string? Field = null!;
261+
262+
[NotNullIfNotNull(nameof(Field))]
263+
public string? Property { get; set; } = null!;
264+
265+
[NotNullIfNotNull(nameof(Field))]
266+
public string this[string? value] => value!;
267+
268+
public string Method([NotNullIfNotNull(nameof(Field))] string value)
269+
{
270+
return null!;
271+
}
272+
}
273+
274+
public class NotNullWhenAttributeTestClass
275+
{
276+
public bool Method([NotNullWhen(true)] string value)
277+
{
278+
return true;
279+
}
280+
}
281+
#endregion
282+
}

Tests/NFUnitTestAttributes/NFUnitTestAttributes.nfproj

+4-3
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,14 @@
2121
<IsTestProject>true</IsTestProject>
2222
<TestProjectType>UnitTest</TestProjectType>
2323
<TargetFrameworkVersion>v1.0</TargetFrameworkVersion>
24-
<LangVersion>default</LangVersion>
24+
<LangVersion>default</LangVersion>
2525
</PropertyGroup>
2626
<Import Project="$(NanoFrameworkProjectSystemPath)NFProjectSystem.props" Condition="Exists('$(NanoFrameworkProjectSystemPath)NFProjectSystem.props')" />
2727
<ItemGroup>
28-
<Compile Include="CallerArgumentExpressionAttributeTests.cs" />
29-
<Compile Include="CallerMemberNameAttributeTests.cs" />
3028
<Compile Include="ConstructorTests.cs" />
29+
<Compile Include="Diagnostics\CodeAnalysis\NullableAttributesTests.cs" />
30+
<Compile Include="Runtime\CompilerServices\CallerArgumentExpressionAttributeTests.cs" />
31+
<Compile Include="Runtime\CompilerServices\CallerMemberNameAttributeTests.cs" />
3132
<Compile Include="UnitTestAttributesTest1.cs" />
3233
<Compile Include="Properties\AssemblyInfo.cs" />
3334
</ItemGroup>

Tests/NFUnitTestAttributes/CallerArgumentExpressionAttributeTests.cs renamed to Tests/NFUnitTestAttributes/Runtime/CompilerServices/CallerArgumentExpressionAttributeTests.cs

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
using System.Runtime.CompilerServices;
55
using nanoFramework.TestFramework;
66

7-
namespace NFUnitTestAttributes
7+
#nullable enable
8+
namespace NFUnitTestAttributes.Runtime.CompilerServices
89
{
910
[TestClass]
1011
public class CallerArgumentExpressionAttributeTests

Tests/NFUnitTestAttributes/CallerMemberNameAttributeTests.cs renamed to Tests/NFUnitTestAttributes/Runtime/CompilerServices/CallerMemberNameAttributeTests.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
using System.Runtime.CompilerServices;
55
using nanoFramework.TestFramework;
66

7-
namespace NFUnitTestAttributes
7+
namespace NFUnitTestAttributes.Runtime.CompilerServices
88
{
99
[TestClass]
1010
public class CallerMemberNameAttributeTests
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
using System;
5+
using nanoFramework.TestFramework;
6+
7+
#nullable enable
8+
namespace NFUnitTestException
9+
{
10+
[TestClass]
11+
public class ArgumentExceptionTests
12+
{
13+
[TestMethod]
14+
public void ctor_sets_correct_message_with_parameter()
15+
{
16+
const string parameter = nameof(ctor_sets_correct_message_with_parameter);
17+
const string expected = $"Value does not fall within the expected range. (Parameter '{parameter}')";
18+
19+
var ex1 = new ArgumentException(null, parameter);
20+
var ex2 = new ArgumentException(null, parameter, (Exception?)null);
21+
22+
Assert.AreEqual(expected, ex1.Message);
23+
Assert.AreEqual(expected, ex2.Message);
24+
}
25+
26+
[TestMethod]
27+
public void ctor_sets_correct_message_without_parameter()
28+
{
29+
const string expected = "Value does not fall within the expected range.";
30+
31+
var ex1 = new ArgumentException();
32+
var ex2 = new ArgumentException(null);
33+
var ex3 = new ArgumentException(null, (Exception?)null);
34+
var ex4 = new ArgumentException(null, (string?)null);
35+
var ex5 = new ArgumentException(null, (string?)null, (Exception?)null);
36+
37+
Assert.AreEqual(expected, ex1.Message);
38+
Assert.AreEqual(expected, ex2.Message);
39+
Assert.AreEqual(expected, ex3.Message);
40+
Assert.AreEqual(expected, ex4.Message);
41+
Assert.AreEqual(expected, ex5.Message);
42+
}
43+
44+
[TestMethod]
45+
public void ThrowIfNullOrEmpty_throws_ArgumentException_if_empty_string()
46+
{
47+
Assert.ThrowsException(typeof(ArgumentException), () => ArgumentException.ThrowIfNullOrEmpty(string.Empty));
48+
}
49+
50+
[TestMethod]
51+
public void ThrowIfNullOrEmpty_throws_ArgumentNullException_if_null()
52+
{
53+
Assert.ThrowsException(typeof(ArgumentNullException), () => ArgumentException.ThrowIfNullOrEmpty(null));
54+
}
55+
}
56+
}

0 commit comments

Comments
 (0)