Skip to content

Commit

Permalink
Add [NotNull] attribute (#100)
Browse files Browse the repository at this point in the history
* Add `[NotNull]`

* Update Nuspec
  • Loading branch information
brminnick authored Nov 23, 2021
1 parent 6dbb23e commit 902e0e9
Show file tree
Hide file tree
Showing 7 changed files with 169 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,14 @@
- Mark internal classes as abstract
- Update AssemblyFileVersion
- Add NuGet README
- Improve Nullablitiy
</PackageReleaseNotes>
<Version>6.0.3</Version>
<Version>6.0.4</Version>
<PackageReadmeFile>README.md</PackageReadmeFile>
<RepositoryUrl>https://github.com/brminnick/AsyncAwaitBestPractices</RepositoryUrl>
<Product>$(AssemblyName) ($(TargetFramework))</Product>
<AssemblyVersion>6.0.3</AssemblyVersion>
<AssemblyFileVersion>6.0.3</AssemblyFileVersion>
<AssemblyVersion>6.0.4</AssemblyVersion>
<AssemblyFileVersion>6.0.4</AssemblyFileVersion>
<PackageVersion>$(Version)$(VersionSuffix)</PackageVersion>
<Authors>Brandon Minnick, John Thiriet</Authors>
<Owners>Brandon Minnick</Owners>
Expand Down
2 changes: 1 addition & 1 deletion Src/AsyncAwaitBestPractices.MVVM/BaseCommand.shared.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public event EventHandler CanExecuteChanged
/// </summary>
/// <typeparam name="T"></typeparam>
/// <returns></returns>
protected static bool IsNullable<T>()
private protected static bool IsNullable<T>()
{
var type = typeof(T);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ class Tests_WeakEventManager_ActionT : BaseTest
{
readonly WeakEventManager<string> _actionEventManager = new();

EventHandler<string>? _action;

event Action<string> ActionEvent
{
add => _actionEventManager.AddEventHandler(value);
Expand Down Expand Up @@ -223,4 +225,43 @@ public void WeakEventManagerActionT_RemoveEventHandler_WhiteSpaceEventName()
//Assert
Assert.Throws<ArgumentNullException>(() => _actionEventManager.RemoveEventHandler(s => { var temp = s; }, " "), "Value cannot be null.\nParameter name: eventName");
}

#if NETCOREAPP3_1 || NET5_0 || NET6_0
[Test]
public void WeakEventManagerActionT_AddRemoveEventHandler_VerifyNotNullAttribute()
{
//Arrange
EventHandler<string> addEventResult, removeEventResult;
string actionName = nameof(_action);

//Act
assignEvent();

_actionEventManager.AddEventHandler(_action, actionName);
addEventResult = _action;

_action = null;
assignEvent();

_actionEventManager.RemoveEventHandler(_action, actionName);
removeEventResult = _action;

_action = null;

//Assert
Assert.IsNull(_action);
Assert.IsNotNull(addEventResult);
Assert.IsNotNull(removeEventResult);

void assignEvent()
{
_action = new EventHandler<string>(handleEvent);

void handleEvent(object? sender, string e)
{

}
}
}
#endif
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ class Tests_WeakEventManager_Delegate : BaseTest, INotifyPropertyChanged
{
readonly WeakEventManager _propertyChangedWeakEventManager = new();

PropertyChangedEventHandler? _nullablePropertyChangedEventHandler;

public event PropertyChangedEventHandler? PropertyChanged
{
add => _propertyChangedWeakEventManager.AddEventHandler(value);
Expand Down Expand Up @@ -304,4 +306,43 @@ static void sampleDelegate(object? sender, EventArgs e)

}
}

#if NETCOREAPP3_1 || NET5_0 || NET6_0
[Test]
public void WeakEventManagerDelegate_AddRemoveEventHandler_VerifyNotNullAttribute()
{
//Arrange
PropertyChangedEventHandler addEventHandlerResult, removeEventHandlerResult;
string eventHandlerName = nameof(_nullablePropertyChangedEventHandler);

//Act
assignEventHandler();

_propertyChangedWeakEventManager.AddEventHandler(_nullablePropertyChangedEventHandler, eventHandlerName);
addEventHandlerResult = _nullablePropertyChangedEventHandler;

_nullablePropertyChangedEventHandler = null;
assignEventHandler();

_propertyChangedWeakEventManager.RemoveEventHandler(_nullablePropertyChangedEventHandler, eventHandlerName);
removeEventHandlerResult = _nullablePropertyChangedEventHandler;

_nullablePropertyChangedEventHandler = null;

//Assert
Assert.IsNull(_nullablePropertyChangedEventHandler);
Assert.IsNotNull(addEventHandlerResult);
Assert.IsNotNull(removeEventHandlerResult);

void assignEventHandler()
{
_nullablePropertyChangedEventHandler = new PropertyChangedEventHandler(handlePropertyChanged);

void handlePropertyChanged(object? sender, PropertyChangedEventArgs e)
{

}
}
}
#endif
}
7 changes: 4 additions & 3 deletions Src/AsyncAwaitBestPractices/AsyncAwaitBestPractices.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,14 @@
- Mark internal classes as abstract
- Update AssemblyFileVersion
- Add NuGet README
- Improve Nullablitiy
</PackageReleaseNotes>
<Version>6.0.3</Version>
<Version>6.0.4</Version>
<PackageReadmeFile>README.md</PackageReadmeFile>
<RepositoryUrl>https://github.com/brminnick/AsyncAwaitBestPractices</RepositoryUrl>
<Product>$(AssemblyName) ($(TargetFramework))</Product>
<AssemblyVersion>6.0.3</AssemblyVersion>
<AssemblyFileVersion>6.0.3</AssemblyFileVersion>
<AssemblyVersion>6.0.4</AssemblyVersion>
<AssemblyFileVersion>6.0.4</AssemblyFileVersion>
<PackageVersion>$(Version)$(VersionSuffix)</PackageVersion>
<Authors>Brandon Minnick, John Thiriet</Authors>
<Owners>Brandon Minnick</Owners>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
using System;
using System.Runtime.CompilerServices;
#if NETSTANDARD2_1
using System.Diagnostics.CodeAnalysis;
#endif

namespace AsyncAwaitBestPractices;

Expand All @@ -10,28 +13,56 @@ public partial class WeakEventManager<TEventArgs>
/// </summary>
/// <param name="handler">Handler</param>
/// <param name="eventName">Event name</param>
public void AddEventHandler(EventHandler<TEventArgs>? handler, [CallerMemberName] string eventName = "") => AddEventHandler(in handler, in eventName);
#if NETSTANDARD2_1
public void AddEventHandler([NotNull] EventHandler<TEventArgs>? handler, [CallerMemberName, NotNull] string eventName = "")
#else
public void AddEventHandler(EventHandler<TEventArgs>? handler, [CallerMemberName] string eventName = "")
#endif
{
AddEventHandler(in handler, in eventName);
}

/// <summary>
/// Adds the event handler
/// </summary>
/// <param name="action">Handler</param>
/// <param name="eventName">Event name</param>
public void AddEventHandler(Action<TEventArgs>? action, [CallerMemberName] string eventName = "") => AddEventHandler(in action, in eventName);
#if NETSTANDARD2_1
public void AddEventHandler([NotNull] Action<TEventArgs>? action, [CallerMemberName, NotNull] string eventName = "")
#else
public void AddEventHandler(Action<TEventArgs>? action, [CallerMemberName] string eventName = "")
#endif
{
AddEventHandler(in action, in eventName);
}

/// <summary>
/// Removes the event handler
/// </summary>
/// <param name="handler">Handler</param>
/// <param name="eventName">Event name</param>
public void RemoveEventHandler(EventHandler<TEventArgs>? handler, [CallerMemberName] string eventName = "") => RemoveEventHandler(in handler, in eventName);
#if NETSTANDARD2_1
public void RemoveEventHandler([NotNull] EventHandler<TEventArgs>? handler, [CallerMemberName, NotNull] string eventName = "")
#else
public void RemoveEventHandler(EventHandler<TEventArgs>? handler, [CallerMemberName] string eventName = "")
#endif
{
RemoveEventHandler(in handler, in eventName);
}

/// <summary>
/// Removes the event handler
/// </summary>
/// <param name="action">Handler</param>
/// <param name="eventName">Event name</param>
public void RemoveEventHandler(Action<TEventArgs>? action, [CallerMemberName] string eventName = "") => RemoveEventHandler(in action, in eventName);
#if NETSTANDARD2_1
public void RemoveEventHandler([NotNull] Action<TEventArgs>? action, [CallerMemberName, NotNull] string eventName = "")
#else
public void RemoveEventHandler(Action<TEventArgs>? action, [CallerMemberName] string eventName = "")
#endif
{
RemoveEventHandler(in action, in eventName);
}

/// <summary>
/// Invokes the event EventHandler
Expand Down Expand Up @@ -59,14 +90,28 @@ public partial class WeakEventManager
/// </summary>
/// <param name="handler">Handler</param>
/// <param name="eventName">Event name</param>
public void AddEventHandler(Delegate? handler, [CallerMemberName] string eventName = "") => AddEventHandler(in handler, in eventName);
#if NETSTANDARD2_1
public void AddEventHandler([NotNull] Delegate? handler, [CallerMemberName, NotNull] string eventName = "")
#else
public void AddEventHandler(Delegate? handler, [CallerMemberName] string eventName = "")
#endif
{
AddEventHandler(in handler, in eventName);
}

/// <summary>
/// Removes the event handler.
/// </summary>
/// <param name="handler">Handler</param>
/// <param name="eventName">Event name</param>
public void RemoveEventHandler(Delegate? handler, [CallerMemberName] string eventName = "") => RemoveEventHandler(in handler, in eventName);
#if NETSTANDARD2_1
public void RemoveEventHandler([NotNull] Delegate? handler, [CallerMemberName, NotNull] string eventName = "")
#else
public void RemoveEventHandler(Delegate? handler, [CallerMemberName] string eventName = "")
#endif
{
RemoveEventHandler(in handler, in eventName);
}

/// <summary>
/// Invokes the event EventHandler
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
using System.Collections.Generic;
using System.Reflection;
using System.Runtime.CompilerServices;
#if NETSTANDARD2_1
using System.Diagnostics.CodeAnalysis;
#endif

using static System.String;

Expand All @@ -20,7 +23,11 @@ public partial class WeakEventManager<TEventArgs>
/// </summary>
/// <param name="handler">Handler</param>
/// <param name="eventName">Event name</param>
#if NETSTANDARD2_1
public void AddEventHandler([NotNull] in EventHandler<TEventArgs>? handler, [CallerMemberName, NotNull] in string eventName = "")
#else
public void AddEventHandler(in EventHandler<TEventArgs>? handler, [CallerMemberName] in string eventName = "")
#endif
{
if (IsNullOrWhiteSpace(eventName))
throw new ArgumentNullException(nameof(eventName));
Expand All @@ -36,7 +43,11 @@ public void AddEventHandler(in EventHandler<TEventArgs>? handler, [CallerMemberN
/// </summary>
/// <param name="action">Handler</param>
/// <param name="eventName">Event name</param>
#if NETSTANDARD2_1
public void AddEventHandler([NotNull] in Action<TEventArgs>? action, [CallerMemberName, NotNull] in string eventName = "")
#else
public void AddEventHandler(in Action<TEventArgs>? action, [CallerMemberName] in string eventName = "")
#endif
{
if (IsNullOrWhiteSpace(eventName))
throw new ArgumentNullException(nameof(eventName));
Expand All @@ -52,7 +63,11 @@ public void AddEventHandler(in Action<TEventArgs>? action, [CallerMemberName] in
/// </summary>
/// <param name="handler">Handler</param>
/// <param name="eventName">Event name</param>
#if NETSTANDARD2_1
public void RemoveEventHandler([NotNull] in EventHandler<TEventArgs>? handler, [CallerMemberName, NotNull] in string eventName = "")
#else
public void RemoveEventHandler(in EventHandler<TEventArgs>? handler, [CallerMemberName] in string eventName = "")
#endif
{
if (IsNullOrWhiteSpace(eventName))
throw new ArgumentNullException(nameof(eventName));
Expand All @@ -68,7 +83,11 @@ public void RemoveEventHandler(in EventHandler<TEventArgs>? handler, [CallerMemb
/// </summary>
/// <param name="action">Handler</param>
/// <param name="eventName">Event name</param>
#if NETSTANDARD2_1
public void RemoveEventHandler([NotNull] in Action<TEventArgs>? action, [CallerMemberName, NotNull] in string eventName = "")
#else
public void RemoveEventHandler(in Action<TEventArgs>? action, [CallerMemberName] in string eventName = "")
#endif
{
if (IsNullOrWhiteSpace(eventName))
throw new ArgumentNullException(nameof(eventName));
Expand Down Expand Up @@ -109,7 +128,11 @@ public partial class WeakEventManager
/// </summary>
/// <param name="handler">Handler</param>
/// <param name="eventName">Event name</param>
#if NETSTANDARD2_1
public void AddEventHandler([NotNull] in Delegate? handler, [CallerMemberName, NotNull] in string eventName = "")
#else
public void AddEventHandler(in Delegate? handler, [CallerMemberName] in string eventName = "")
#endif
{
if (IsNullOrWhiteSpace(eventName))
throw new ArgumentNullException(nameof(eventName));
Expand All @@ -125,7 +148,11 @@ public void AddEventHandler(in Delegate? handler, [CallerMemberName] in string e
/// </summary>
/// <param name="handler">Handler</param>
/// <param name="eventName">Event name</param>
#if NETSTANDARD2_1
public void RemoveEventHandler([NotNull] in Delegate? handler, [CallerMemberName, NotNull] in string eventName = "")
#else
public void RemoveEventHandler(in Delegate? handler, [CallerMemberName] in string eventName = "")
#endif
{
if (IsNullOrWhiteSpace(eventName))
throw new ArgumentNullException(nameof(eventName));
Expand Down

0 comments on commit 902e0e9

Please sign in to comment.