Skip to content

Commit ca87a81

Browse files
authored
Add [NotNull] attribute on the Should() method for object assertions (#2380)
* Add [NotNull] attribute on the Should() method of reference types Compelling example: ```csharp [Theory] [InlineData(true)] [InlineData(false)] public void Test1(bool currentUser) { IPrincipal principal = currentUser ? new WindowsPrincipal(WindowsIdentity.GetCurrent()) : new ClaimsPrincipal(); IIdentity? identity = principal.Identity; identity.Should().NotBeOfType<GenericIdentity>(); identity.IsAuthenticated.Should().BeFalse(); } ``` Fixes #1115 * Add entry in the release notes under improvements
1 parent db5d1c0 commit ca87a81

File tree

8 files changed

+163
-126
lines changed

8 files changed

+163
-126
lines changed

Src/FluentAssertions/AssertionExtensions.cs

Lines changed: 25 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using System;
1+
using System;
22
using System.Collections;
33
using System.Collections.Generic;
44
using System.Diagnostics;
@@ -18,6 +18,7 @@
1818
using FluentAssertions.Types;
1919
using FluentAssertions.Xml;
2020
using JetBrains.Annotations;
21+
using NotNullAttribute = System.Diagnostics.CodeAnalysis.NotNullAttribute;
2122
#if !NETSTANDARD2_0
2223
using FluentAssertions.Events;
2324
#endif
@@ -174,7 +175,7 @@ public static ExecutionTimeAssertions Should(this ExecutionTime executionTime)
174175
/// current <see cref="Assembly"/>.
175176
/// </summary>
176177
[Pure]
177-
public static AssemblyAssertions Should(this Assembly assembly)
178+
public static AssemblyAssertions Should([NotNull] this Assembly assembly)
178179
{
179180
return new AssemblyAssertions(assembly);
180181
}
@@ -184,7 +185,7 @@ public static AssemblyAssertions Should(this Assembly assembly)
184185
/// current <see cref="XElement"/>.
185186
/// </summary>
186187
[Pure]
187-
public static XDocumentAssertions Should(this XDocument actualValue)
188+
public static XDocumentAssertions Should([NotNull] this XDocument actualValue)
188189
{
189190
return new XDocumentAssertions(actualValue);
190191
}
@@ -194,7 +195,7 @@ public static XDocumentAssertions Should(this XDocument actualValue)
194195
/// current <see cref="XElement"/>.
195196
/// </summary>
196197
[Pure]
197-
public static XElementAssertions Should(this XElement actualValue)
198+
public static XElementAssertions Should([NotNull] this XElement actualValue)
198199
{
199200
return new XElementAssertions(actualValue);
200201
}
@@ -204,7 +205,7 @@ public static XElementAssertions Should(this XElement actualValue)
204205
/// current <see cref="XAttribute"/>.
205206
/// </summary>
206207
[Pure]
207-
public static XAttributeAssertions Should(this XAttribute actualValue)
208+
public static XAttributeAssertions Should([NotNull] this XAttribute actualValue)
208209
{
209210
return new XAttributeAssertions(actualValue);
210211
}
@@ -214,7 +215,7 @@ public static XAttributeAssertions Should(this XAttribute actualValue)
214215
/// current <see cref="Stream"/>.
215216
/// </summary>
216217
[Pure]
217-
public static StreamAssertions Should(this Stream actualValue)
218+
public static StreamAssertions Should([NotNull] this Stream actualValue)
218219
{
219220
return new StreamAssertions(actualValue);
220221
}
@@ -224,7 +225,7 @@ public static StreamAssertions Should(this Stream actualValue)
224225
/// current <see cref="BufferedStream"/>.
225226
/// </summary>
226227
[Pure]
227-
public static BufferedStreamAssertions Should(this BufferedStream actualValue)
228+
public static BufferedStreamAssertions Should([NotNull] this BufferedStream actualValue)
228229
{
229230
return new BufferedStreamAssertions(actualValue);
230231
}
@@ -281,7 +282,7 @@ private static void ForceEnumeration<T>(T subject, Func<T, IEnumerable> enumerab
281282
/// current <see cref="object"/>.
282283
/// </summary>
283284
[Pure]
284-
public static ObjectAssertions Should(this object actualValue)
285+
public static ObjectAssertions Should([NotNull] this object actualValue)
285286
{
286287
return new ObjectAssertions(actualValue);
287288
}
@@ -311,7 +312,7 @@ public static NullableBooleanAssertions Should(this bool? actualValue)
311312
/// current <see cref="HttpResponseMessage"/>.
312313
/// </summary>
313314
[Pure]
314-
public static HttpResponseMessageAssertions Should(this HttpResponseMessage actualValue)
315+
public static HttpResponseMessageAssertions Should([NotNull] this HttpResponseMessage actualValue)
315316
{
316317
return new HttpResponseMessageAssertions(actualValue);
317318
}
@@ -341,7 +342,7 @@ public static NullableGuidAssertions Should(this Guid? actualValue)
341342
/// current <see cref="IEnumerable{T}"/>.
342343
/// </summary>
343344
[Pure]
344-
public static GenericCollectionAssertions<T> Should<T>(this IEnumerable<T> actualValue)
345+
public static GenericCollectionAssertions<T> Should<T>([NotNull] this IEnumerable<T> actualValue)
345346
{
346347
return new GenericCollectionAssertions<T>(actualValue);
347348
}
@@ -351,7 +352,7 @@ public static GenericCollectionAssertions<T> Should<T>(this IEnumerable<T> actua
351352
/// current <see cref="IEnumerable{T}"/>.
352353
/// </summary>
353354
[Pure]
354-
public static StringCollectionAssertions Should(this IEnumerable<string> @this)
355+
public static StringCollectionAssertions Should([NotNull] this IEnumerable<string> @this)
355356
{
356357
return new StringCollectionAssertions(@this);
357358
}
@@ -362,7 +363,7 @@ public static StringCollectionAssertions Should(this IEnumerable<string> @this)
362363
/// </summary>
363364
[Pure]
364365
public static GenericDictionaryAssertions<IDictionary<TKey, TValue>, TKey, TValue> Should<TKey, TValue>(
365-
this IDictionary<TKey, TValue> actualValue)
366+
[NotNull] this IDictionary<TKey, TValue> actualValue)
366367
{
367368
return new GenericDictionaryAssertions<IDictionary<TKey, TValue>, TKey, TValue>(actualValue);
368369
}
@@ -373,7 +374,7 @@ public static GenericDictionaryAssertions<IDictionary<TKey, TValue>, TKey, TValu
373374
/// </summary>
374375
[Pure]
375376
public static GenericDictionaryAssertions<IEnumerable<KeyValuePair<TKey, TValue>>, TKey, TValue> Should<TKey, TValue>(
376-
this IEnumerable<KeyValuePair<TKey, TValue>> actualValue)
377+
[NotNull] this IEnumerable<KeyValuePair<TKey, TValue>> actualValue)
377378
{
378379
return new GenericDictionaryAssertions<IEnumerable<KeyValuePair<TKey, TValue>>, TKey, TValue>(actualValue);
379380
}
@@ -384,7 +385,7 @@ public static GenericDictionaryAssertions<IEnumerable<KeyValuePair<TKey, TValue>
384385
/// </summary>
385386
[Pure]
386387
public static GenericDictionaryAssertions<TCollection, TKey, TValue> Should<TCollection, TKey, TValue>(
387-
this TCollection actualValue)
388+
[NotNull] this TCollection actualValue)
388389
where TCollection : IEnumerable<KeyValuePair<TKey, TValue>>
389390
{
390391
return new GenericDictionaryAssertions<TCollection, TKey, TValue>(actualValue);
@@ -478,7 +479,7 @@ public static NullableTimeOnlyAssertions Should(this TimeOnly? actualValue)
478479
/// current <see cref="IComparable{T}"/>.
479480
/// </summary>
480481
[Pure]
481-
public static ComparableTypeAssertions<T> Should<T>(this IComparable<T> comparableValue)
482+
public static ComparableTypeAssertions<T> Should<T>([NotNull] this IComparable<T> comparableValue)
482483
{
483484
return new ComparableTypeAssertions<T>(comparableValue);
484485
}
@@ -708,7 +709,7 @@ public static NullableNumericAssertions<double> Should(this double? actualValue)
708709
/// current <see cref="string"/>.
709710
/// </summary>
710711
[Pure]
711-
public static StringAssertions Should(this string actualValue)
712+
public static StringAssertions Should([NotNull] this string actualValue)
712713
{
713714
return new StringAssertions(actualValue);
714715
}
@@ -738,7 +739,7 @@ public static NullableSimpleTimeSpanAssertions Should(this TimeSpan? actualValue
738739
/// current <see cref="System.Type"/>.
739740
/// </summary>
740741
[Pure]
741-
public static TypeAssertions Should(this Type subject)
742+
public static TypeAssertions Should([NotNull] this Type subject)
742743
{
743744
return new TypeAssertions(subject);
744745
}
@@ -762,7 +763,7 @@ public static TypeSelectorAssertions Should(this TypeSelector typeSelector)
762763
/// </summary>
763764
/// <seealso cref="TypeAssertions"/>
764765
[Pure]
765-
public static ConstructorInfoAssertions Should(this ConstructorInfo constructorInfo)
766+
public static ConstructorInfoAssertions Should([NotNull] this ConstructorInfo constructorInfo)
766767
{
767768
return new ConstructorInfoAssertions(constructorInfo);
768769
}
@@ -772,7 +773,7 @@ public static ConstructorInfoAssertions Should(this ConstructorInfo constructorI
772773
/// </summary>
773774
/// <seealso cref="TypeAssertions"/>
774775
[Pure]
775-
public static MethodInfoAssertions Should(this MethodInfo methodInfo)
776+
public static MethodInfoAssertions Should([NotNull] this MethodInfo methodInfo)
776777
{
777778
return new MethodInfoAssertions(methodInfo);
778779
}
@@ -797,7 +798,7 @@ public static MethodInfoSelectorAssertions Should(this MethodInfoSelector method
797798
/// </summary>
798799
/// <seealso cref="TypeAssertions"/>
799800
[Pure]
800-
public static PropertyInfoAssertions Should(this PropertyInfo propertyInfo)
801+
public static PropertyInfoAssertions Should([NotNull] this PropertyInfo propertyInfo)
801802
{
802803
return new PropertyInfoAssertions(propertyInfo);
803804
}
@@ -821,7 +822,7 @@ public static PropertyInfoSelectorAssertions Should(this PropertyInfoSelector pr
821822
/// current <see cref="System.Action"/>.
822823
/// </summary>
823824
[Pure]
824-
public static ActionAssertions Should(this Action action)
825+
public static ActionAssertions Should([NotNull] this Action action)
825826
{
826827
return new ActionAssertions(action, Extractor);
827828
}
@@ -831,7 +832,7 @@ public static ActionAssertions Should(this Action action)
831832
/// current <see cref="System.Func{Task}"/>.
832833
/// </summary>
833834
[Pure]
834-
public static NonGenericAsyncFunctionAssertions Should(this Func<Task> action)
835+
public static NonGenericAsyncFunctionAssertions Should([NotNull] this Func<Task> action)
835836
{
836837
return new NonGenericAsyncFunctionAssertions(action, Extractor);
837838
}
@@ -841,7 +842,7 @@ public static NonGenericAsyncFunctionAssertions Should(this Func<Task> action)
841842
/// current <see><cref>System.Func{Task{T}}</cref></see>.
842843
/// </summary>
843844
[Pure]
844-
public static GenericAsyncFunctionAssertions<T> Should<T>(this Func<Task<T>> action)
845+
public static GenericAsyncFunctionAssertions<T> Should<T>([NotNull] this Func<Task<T>> action)
845846
{
846847
return new GenericAsyncFunctionAssertions<T>(action, Extractor);
847848
}
@@ -851,7 +852,7 @@ public static GenericAsyncFunctionAssertions<T> Should<T>(this Func<Task<T>> act
851852
/// current <see cref="System.Func{T}"/>.
852853
/// </summary>
853854
[Pure]
854-
public static FunctionAssertions<T> Should<T>(this Func<T> func)
855+
public static FunctionAssertions<T> Should<T>([NotNull] this Func<T> func)
855856
{
856857
return new FunctionAssertions<T>(func, Extractor);
857858
}

Src/FluentAssertions/XmlAssertionExtensions.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System.Diagnostics;
2+
using System.Diagnostics.CodeAnalysis;
23
using System.Xml;
34
using FluentAssertions.Xml;
45

@@ -7,12 +8,12 @@ namespace FluentAssertions;
78
[DebuggerNonUserCode]
89
public static class XmlAssertionExtensions
910
{
10-
public static XmlNodeAssertions Should(this XmlNode actualValue)
11+
public static XmlNodeAssertions Should([NotNull] this XmlNode actualValue)
1112
{
1213
return new XmlNodeAssertions(actualValue);
1314
}
1415

15-
public static XmlElementAssertions Should(this XmlElement actualValue)
16+
public static XmlElementAssertions Should([NotNull] this XmlElement actualValue)
1617
{
1718
return new XmlElementAssertions(actualValue);
1819
}

0 commit comments

Comments
 (0)