Skip to content

Commit

Permalink
🚧Replace custom guards (#911)
Browse files Browse the repository at this point in the history
* Replace `EnsureThat.ParamIsNotNull()` with `ArgumentNullException.ThrowIfNull()`

* Replace `EnsureThat.EnsureThat.ParamIsNotNull()` with `ArgumentNullException.ThrowIfNullOrEmpty()`

* Delete `EnsureThat.cs` class file

* cleanup: remove unused usings

* chore: readd ensure that guards for pointers and adjust tests

* refactor: replace guard

* test: adjust tests for replaced guards

---------

Co-authored-by: Calvin Wilkinson <[email protected]>
Co-authored-by: Calvin Wilkinson <[email protected]>
  • Loading branch information
3 people authored Apr 12, 2024
1 parent 1bd6a3e commit 34ea7d0
Show file tree
Hide file tree
Showing 18 changed files with 30 additions and 124 deletions.
2 changes: 1 addition & 1 deletion Testing/VelaptorTests/Content/AudioPathResolverTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public void Ctor_WithNullDirectoryParam_ThrowsException()

// Assert
act.Should().Throw<ArgumentNullException>()
.WithMessage("The parameter must not be null. (Parameter 'directory')");
.WithMessage("Value cannot be null. (Parameter 'directory')");
}

[Fact]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public void Ctor_WithNullReactableFactoryParam_ThrowsException()
// Assert
act.Should()
.Throw<ArgumentNullException>()
.WithMessage("The parameter must not be null. (Parameter 'reactableFactory')");
.WithMessage("Value cannot be null. (Parameter 'reactableFactory')");
}
#endregion

Expand Down
8 changes: 4 additions & 4 deletions Testing/VelaptorTests/Content/Factories/FontFactoryTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public void Ctor_WithNullFontServiceParam_ThrowsException()
// Assert
act.Should()
.Throw<ArgumentNullException>()
.WithMessage("The parameter must not be null. (Parameter 'fontService')");
.WithMessage("Value cannot be null. (Parameter 'fontService')");
}

[Fact]
Expand All @@ -73,7 +73,7 @@ public void Ctor_WithNullFontStatsServiceParam_ThrowsException()
// Assert
act.Should()
.Throw<ArgumentNullException>()
.WithMessage("The parameter must not be null. (Parameter 'fontStatsService')");
.WithMessage("Value cannot be null. (Parameter 'fontStatsService')");
}

[Fact]
Expand All @@ -92,7 +92,7 @@ public void Ctor_WithNullFontAtlasServiceParam_ThrowsException()
// Assert
act.Should()
.Throw<ArgumentNullException>()
.WithMessage("The parameter must not be null. (Parameter 'fontAtlasService')");
.WithMessage("Value cannot be null. (Parameter 'fontAtlasService')");
}

[Fact]
Expand All @@ -111,7 +111,7 @@ public void Ctor_WithNullTextureCacheParam_ThrowsException()
// Assert
act.Should()
.Throw<ArgumentNullException>()
.WithMessage("The parameter must not be null. (Parameter 'textureCache')");
.WithMessage("Value cannot be null. (Parameter 'textureCache')");
}
#endregion

Expand Down
39 changes: 0 additions & 39 deletions Testing/VelaptorTests/Guards/EnsureThatTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ namespace VelaptorTests.Guards;

using System;
using FluentAssertions;
using Helpers;
using Velaptor.Guards;
using Xunit;

Expand All @@ -17,44 +16,6 @@ namespace VelaptorTests.Guards;
public class EnsureThatTests
{
#region Method Tests
[Fact]
public void ParamIsNotNull_WithNullValue_ThrowsException()
{
// Arrange
object? nullObj = null;

// Act & Assert
AssertExtensions.ThrowsWithMessage<ArgumentNullException>(() =>
{
EnsureThat.ParamIsNotNull(nullObj);
}, "The parameter must not be null. (Parameter 'nullObj')");
}

[Fact]
public void ParamIsNotNull_WithNonNullValue_DoesNotThrowException()
{
// Arrange
object nonNullObj = "non-null-obj";

// Act & Assert
AssertExtensions.DoesNotThrow<Exception>(() =>
{
EnsureThat.ParamIsNotNull(nonNullObj);
});
}

[Theory]
[InlineData("")]
[InlineData(null)]
public void StringParamIsNotNullOrEmpty_WhenInvoked_ThrowsException(string? value)
{
// Act & Assert
AssertExtensions.ThrowsWithMessage<ArgumentNullException>(() =>
{
EnsureThat.StringParamIsNotNullOrEmpty(value);
}, $"The string parameter must not be null or empty. (Parameter '{nameof(value)}')");
}

[Fact]
public void PointerIsNotNull_WithNonZeroIntPointer_DoesNotThrowException()
{
Expand Down
2 changes: 1 addition & 1 deletion Testing/VelaptorTests/Hardware/SystemDisplayTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public void Ctor_WithNullPlatformParam_ThrowsException()
AssertExtensions.ThrowsWithMessage<ArgumentNullException>(() =>
{
_ = new SystemDisplay(null);
}, "The parameter must not be null. (Parameter 'platform')");
}, "Value cannot be null. (Parameter 'platform')");
}
#endregion

Expand Down
2 changes: 1 addition & 1 deletion Testing/VelaptorTests/Input/KeyboardTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public void Ctor_WithNullKeyboardDataStoreParam_ThrowsException()
// Assert
act.Should()
.Throw<ArgumentNullException>()
.WithMessage("The parameter must not be null. (Parameter 'keyboardDataService')");
.WithMessage("Value cannot be null. (Parameter 'keyboardDataService')");
}
#endregion

Expand Down
2 changes: 1 addition & 1 deletion Testing/VelaptorTests/Input/MouseTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public void Ctor_WithNullReactableFactoryParam_ThrowsException()
// Assert
act.Should()
.Throw<ArgumentNullException>()
.WithMessage("The parameter must not be null. (Parameter 'reactableFactory')");
.WithMessage("Value cannot be null. (Parameter 'reactableFactory')");
}

[Fact]
Expand Down
4 changes: 2 additions & 2 deletions Testing/VelaptorTests/UI/WindowTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public void Ctor_WithNullWindowParam_ThrowsException()
// Assert
act.Should()
.Throw<ArgumentNullException>()
.WithMessage("The parameter must not be null. (Parameter 'window')");
.WithMessage("Value cannot be null. (Parameter 'window')");
}

[Fact]
Expand All @@ -71,7 +71,7 @@ public void Ctor_WithNullSceneManagerParam_ThrowsException()
// Assert
act.Should()
.Throw<ArgumentNullException>()
.WithMessage("The parameter must not be null. (Parameter 'sceneManager')");
.WithMessage("Value cannot be null. (Parameter 'sceneManager')");
}

[Fact]
Expand Down
3 changes: 1 addition & 2 deletions Velaptor/Content/AudioPathResolver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ namespace Velaptor.Content;
using System.IO;
using System.IO.Abstractions;
using System.Linq;
using Guards;

/// <summary>
/// Resolves paths to audio content.
Expand All @@ -23,7 +22,7 @@ internal sealed class AudioPathResolver : ContentPathResolver
/// <param name="directory">Performs operations with directories.</param>
public AudioPathResolver(IDirectory directory)
{
EnsureThat.ParamIsNotNull(directory);
ArgumentNullException.ThrowIfNull(directory);
this.directory = directory;
ContentDirectoryName = "Audio";
}
Expand Down
3 changes: 1 addition & 2 deletions Velaptor/Content/Factories/AudioFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ namespace Velaptor.Content.Factories;
using System.Linq;
using Carbonate;
using Carbonate.OneWay;
using Guards;
using ReactableData;
using Velaptor.Factories;
using CASLAudio = CASL.Audio;
Expand All @@ -32,7 +31,7 @@ internal sealed class AudioFactory : IAudioFactory
/// <param name="reactableFactory">Creates reactables for sending and receiving notifications with or without data.</param>
public AudioFactory(IReactableFactory reactableFactory)
{
EnsureThat.ParamIsNotNull(reactableFactory);
ArgumentNullException.ThrowIfNull(reactableFactory);

this.disposeReactable = reactableFactory.CreateDisposeAudioReactable();

Expand Down
10 changes: 5 additions & 5 deletions Velaptor/Content/Factories/FontFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@

namespace Velaptor.Content.Factories;

using System;
using Caching;
using Fonts;
using Fonts.Services;
using Graphics;
using Guards;
using Services;

/// <summary>
Expand All @@ -34,10 +34,10 @@ public FontFactory(
IFontAtlasService fontAtlasService,
IItemCache<string, ITexture> textureCache)
{
EnsureThat.ParamIsNotNull(fontAtlasService);
EnsureThat.ParamIsNotNull(textureCache);
EnsureThat.ParamIsNotNull(fontService);
EnsureThat.ParamIsNotNull(fontStatsService);
ArgumentNullException.ThrowIfNull(fontAtlasService);
ArgumentNullException.ThrowIfNull(textureCache);
ArgumentNullException.ThrowIfNull(fontService);
ArgumentNullException.ThrowIfNull(fontStatsService);

this.fontAtlasService = fontAtlasService;
this.textureCache = textureCache;
Expand Down
52 changes: 2 additions & 50 deletions Velaptor/Guards/EnsureThat.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,61 +14,13 @@ namespace Velaptor.Guards;
/// </summary>
internal static class EnsureThat
{
/// <summary>
/// Throws an <see cref="ArgumentNullException"/> if the given <paramref name="value"/> is null.
/// </summary>
/// <param name="value">The value to check.</param>
/// <param name="paramName">The name of the parameter being checked.</param>
/// <typeparam name="T">The class restricted type of the value.</typeparam>
/// <remarks>
/// <para>
/// This method is intended to have the value <paramref name="paramName"/> to be the
/// name of the item that is null.
/// </para>
/// <para>
/// Example: A parameter being injected into a constructor.
/// </para>
/// </remarks>
/// <exception cref="ArgumentNullException">
/// Thrown if <paramref name="value"/> is null.
/// </exception>
public static void ParamIsNotNull<T>(
T? value,
[CallerArgumentExpression("value")] string paramName = "")
where T : class
{
if (value is null)
{
throw new ArgumentNullException(paramName, "The parameter must not be null.");
}
}

/// <summary>
/// Throws an <see cref="ArgumentNullException"/> if the given string <paramref name="value"/>
/// is null or empty.
/// </summary>
/// <param name="value">The string value to check.</param>
/// <param name="paramName">The name of the parameter being checked.</param>
/// <exception cref="ArgumentNullException">
/// Thrown if the <paramref name="value"/> is null or empty.
/// </exception>
public static void StringParamIsNotNullOrEmpty(
string value,
[CallerArgumentExpression("value")] string paramName = "")
{
if (string.IsNullOrEmpty(value))
{
throw new ArgumentNullException(paramName, "The string parameter must not be null or empty.");
}
}

/// <summary>
/// Throws a <see cref="NullReferenceException"/> if the given <paramref name="pointer"/> is a value of zero.
/// </summary>
/// <param name="pointer">The pointer to check.</param>
/// <param name="paramName">The name of the parameter being checked.</param>
/// <exception cref="NullReferenceException">
/// Thrown if the <paramref name="pointer"/> is a value of zero.
/// Thrown if <paramref name="pointer"/> is a value of zero.
/// </exception>
[SuppressMessage("csharpsquid|should not be thrown by user code.", "S112", Justification = "Intentional")]
public static void PointerIsNotNull(nint pointer, [CallerArgumentExpression("pointer")] string paramName = "")
Expand All @@ -91,7 +43,7 @@ public static void PointerIsNotNull(nint pointer, [CallerArgumentExpression("poi
/// <param name="pointer">The pointer to check.</param>
/// <param name="paramName">The name of the parameter being checked.</param>
/// <exception cref="NullReferenceException">
/// Thrown if the <paramref name="pointer"/> is a value of zero.
/// Thrown if <paramref name="pointer"/> is a value of zero.
/// </exception>
[SuppressMessage("csharpsquid|should not be thrown by user code.", "S112", Justification = "Intentional")]
public static void PointerIsNotNull(nuint pointer, [CallerArgumentExpression("pointer")] string paramName = "")
Expand Down
3 changes: 1 addition & 2 deletions Velaptor/Hardware/SystemDisplay.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ namespace Velaptor.Hardware;
using System.Diagnostics.CodeAnalysis;
using System.Numerics;
using System.Runtime.InteropServices;
using Guards;

/// <summary>
/// Holds information about a single display in the system.
Expand All @@ -32,7 +31,7 @@ public readonly record struct SystemDisplay
/// </exception>
internal SystemDisplay(IPlatform platform)
{
EnsureThat.ParamIsNotNull(platform);
ArgumentNullException.ThrowIfNull(platform);
this.platform = platform;
}

Expand Down
4 changes: 2 additions & 2 deletions Velaptor/Input/Keyboard.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#pragma warning disable SA1129 // Do not use default value type constructor
namespace Velaptor.Input;

using Guards;
using System;
using Services;

/// <summary>
Expand All @@ -21,7 +21,7 @@ internal sealed class Keyboard : IAppInput<KeyboardState>
/// <param name="keyboardDataService">Creates reactables for sending and receiving notifications with or without data.</param>
public Keyboard(IKeyboardDataService keyboardDataService)
{
EnsureThat.ParamIsNotNull(keyboardDataService);
ArgumentNullException.ThrowIfNull(keyboardDataService);

this.keyboardDataService = keyboardDataService;
}
Expand Down
3 changes: 1 addition & 2 deletions Velaptor/Input/Mouse.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ namespace Velaptor.Input;
using System.Drawing;
using Carbonate;
using Factories;
using Guards;
using ReactableData;

/// <summary>
Expand All @@ -32,7 +31,7 @@ internal sealed class Mouse : IAppInput<MouseState>
/// <param name="reactableFactory">Creates reactables for sending and receiving notifications with or without data.</param>
public Mouse(IReactableFactory reactableFactory)
{
EnsureThat.ParamIsNotNull(reactableFactory);
ArgumentNullException.ThrowIfNull(reactableFactory);

var mouseDataReactable = reactableFactory.CreateMouseReactable();

Expand Down
5 changes: 2 additions & 3 deletions Velaptor/UI/Button.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ namespace Velaptor.UI;
using Factories;
using Graphics;
using Graphics.Renderers;
using Guards;
using Input;

/// <summary>
Expand Down Expand Up @@ -56,7 +55,7 @@ public Button()
[SuppressMessage("ReSharper", "UnusedMember.Global", Justification = "Used by library users.")]
public Button(Label? label)
{
EnsureThat.ParamIsNotNull(label);
ArgumentNullException.ThrowIfNull(label);

this.rectRenderer = RendererFactory.CreateShapeRenderer();

Expand Down Expand Up @@ -164,7 +163,7 @@ internal Button(
IAppInput<MouseState> mouse)
: base(keyboard, mouse)
{
EnsureThat.ParamIsNotNull(controlFactory);
ArgumentNullException.ThrowIfNull(controlFactory);

this.controlFactory = controlFactory;
}
Expand Down
3 changes: 1 addition & 2 deletions Velaptor/UI/Label.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ namespace Velaptor.UI;
using ExtensionMethods;
using Factories;
using Graphics.Renderers;
using Guards;
using Input;

/// <summary>
Expand Down Expand Up @@ -80,7 +79,7 @@ internal Label(
IAppInput<MouseState> mouse)
: base(keyboard, mouse)
{
EnsureThat.ParamIsNotNull(fontLoader);
ArgumentNullException.ThrowIfNull(fontLoader);

Init(fontLoader, RendererFactory.CreateFontRenderer());
}
Expand Down
Loading

0 comments on commit 34ea7d0

Please sign in to comment.