From 02735461418655027fbee790d282f43dba335aad Mon Sep 17 00:00:00 2001 From: Daniel Wertheim Date: Tue, 6 Jul 2021 22:06:11 +0200 Subject: [PATCH] Changes ExceptionFactory to Internals.DefaultExceptionFactory (#160) --- src/projects/EnsureThat/Ensure.cs | 2 +- src/projects/EnsureThat/ExceptionFactory.cs | 9 +++ src/projects/EnsureThat/IExceptionFactory.cs | 67 ----------------- .../Internals/DefaultExceptionFactory.cs | 72 +++++++++++++++++++ ...sts.cs => DefaultExceptionFactoryTests.cs} | 6 +- 5 files changed, 85 insertions(+), 71 deletions(-) create mode 100644 src/projects/EnsureThat/ExceptionFactory.cs create mode 100644 src/projects/EnsureThat/Internals/DefaultExceptionFactory.cs rename src/tests/UnitTests/{ExceptionFactoryTests.cs => DefaultExceptionFactoryTests.cs} (97%) diff --git a/src/projects/EnsureThat/Ensure.cs b/src/projects/EnsureThat/Ensure.cs index 30eaf1c..e4b7347 100644 --- a/src/projects/EnsureThat/Ensure.cs +++ b/src/projects/EnsureThat/Ensure.cs @@ -10,7 +10,7 @@ public static class Ensure /// /// Gets or Sets the Exception factory to use. /// - public static IExceptionFactory ExceptionFactory { get; set; } = new ExceptionFactory(); + public static IExceptionFactory ExceptionFactory { get; set; } = EnsureThat.ExceptionFactory.Default; /// /// Ensures for objects. diff --git a/src/projects/EnsureThat/ExceptionFactory.cs b/src/projects/EnsureThat/ExceptionFactory.cs new file mode 100644 index 0000000..f0e73cd --- /dev/null +++ b/src/projects/EnsureThat/ExceptionFactory.cs @@ -0,0 +1,9 @@ +using EnsureThat.Internals; + +namespace EnsureThat +{ + public static class ExceptionFactory + { + public static IExceptionFactory Default { get; } = new DefaultExceptionFactory(); + } +} diff --git a/src/projects/EnsureThat/IExceptionFactory.cs b/src/projects/EnsureThat/IExceptionFactory.cs index c08150c..9c59f56 100644 --- a/src/projects/EnsureThat/IExceptionFactory.cs +++ b/src/projects/EnsureThat/IExceptionFactory.cs @@ -1,6 +1,5 @@ using System; using JetBrains.Annotations; -using NotNullAttribute = System.Diagnostics.CodeAnalysis.NotNullAttribute; namespace EnsureThat { @@ -10,70 +9,4 @@ public interface IExceptionFactory Exception ArgumentNullException([NotNull] string defaultMessage, string paramName, OptsFn optsFn = null); Exception ArgumentOutOfRangeException([NotNull] string defaultMessage, string paramName, TValue value, OptsFn optsFn = null); } - - public sealed class ExceptionFactory : IExceptionFactory - { - [return: NotNull] - [Pure] - public Exception ArgumentException(string defaultMessage, string paramName, OptsFn optsFn = null) - { - if (optsFn != null) - { - var opts = optsFn(new EnsureOptions()); - - if (opts.CustomExceptionFactory != null) - return opts.CustomExceptionFactory(defaultMessage, paramName); - - if (opts.CustomException != null) - return opts.CustomException; - - if (opts.CustomMessage != null) - return new ArgumentException(opts.CustomMessage, paramName); - } - - return new ArgumentException(defaultMessage, paramName); - } - - [return: NotNull] - [Pure] - public Exception ArgumentNullException(string defaultMessage, string paramName, OptsFn optsFn = null) - { - if (optsFn != null) - { - var opts = optsFn(new EnsureOptions()); - - if (opts.CustomExceptionFactory != null) - return opts.CustomExceptionFactory(defaultMessage, paramName); - - if (opts.CustomException != null) - return opts.CustomException; - - if (opts.CustomMessage != null) - return new ArgumentNullException(paramName, opts.CustomMessage); - } - - return new ArgumentNullException(paramName, defaultMessage); - } - - [return: NotNull] - [Pure] - public Exception ArgumentOutOfRangeException(string defaultMessage, string paramName, TValue value, OptsFn optsFn = null) - { - if (optsFn != null) - { - var opts = optsFn(new EnsureOptions()); - - if (opts.CustomExceptionFactory != null) - return opts.CustomExceptionFactory(defaultMessage, paramName); - - if (opts.CustomException != null) - return opts.CustomException; - - if (opts.CustomMessage != null) - return new ArgumentOutOfRangeException(paramName, value, opts.CustomMessage); - } - - return new ArgumentOutOfRangeException(paramName, value, defaultMessage); - } - } } diff --git a/src/projects/EnsureThat/Internals/DefaultExceptionFactory.cs b/src/projects/EnsureThat/Internals/DefaultExceptionFactory.cs new file mode 100644 index 0000000..90a692e --- /dev/null +++ b/src/projects/EnsureThat/Internals/DefaultExceptionFactory.cs @@ -0,0 +1,72 @@ +using System; +using JetBrains.Annotations; +using NotNullAttribute = System.Diagnostics.CodeAnalysis.NotNullAttribute; + +namespace EnsureThat.Internals +{ + public sealed class DefaultExceptionFactory : IExceptionFactory + { + [return: NotNull] + [Pure] + public Exception ArgumentException(string defaultMessage, string paramName, OptsFn optsFn = null) + { + if (optsFn != null) + { + var opts = optsFn(new EnsureOptions()); + + if (opts.CustomExceptionFactory != null) + return opts.CustomExceptionFactory(defaultMessage, paramName); + + if (opts.CustomException != null) + return opts.CustomException; + + if (opts.CustomMessage != null) + return new ArgumentException(opts.CustomMessage, paramName); + } + + return new ArgumentException(defaultMessage, paramName); + } + + [return: NotNull] + [Pure] + public Exception ArgumentNullException(string defaultMessage, string paramName, OptsFn optsFn = null) + { + if (optsFn != null) + { + var opts = optsFn(new EnsureOptions()); + + if (opts.CustomExceptionFactory != null) + return opts.CustomExceptionFactory(defaultMessage, paramName); + + if (opts.CustomException != null) + return opts.CustomException; + + if (opts.CustomMessage != null) + return new ArgumentNullException(paramName, opts.CustomMessage); + } + + return new ArgumentNullException(paramName, defaultMessage); + } + + [return: NotNull] + [Pure] + public Exception ArgumentOutOfRangeException(string defaultMessage, string paramName, TValue value, OptsFn optsFn = null) + { + if (optsFn != null) + { + var opts = optsFn(new EnsureOptions()); + + if (opts.CustomExceptionFactory != null) + return opts.CustomExceptionFactory(defaultMessage, paramName); + + if (opts.CustomException != null) + return opts.CustomException; + + if (opts.CustomMessage != null) + return new ArgumentOutOfRangeException(paramName, value, opts.CustomMessage); + } + + return new ArgumentOutOfRangeException(paramName, value, defaultMessage); + } + } +} diff --git a/src/tests/UnitTests/ExceptionFactoryTests.cs b/src/tests/UnitTests/DefaultExceptionFactoryTests.cs similarity index 97% rename from src/tests/UnitTests/ExceptionFactoryTests.cs rename to src/tests/UnitTests/DefaultExceptionFactoryTests.cs index 236d358..ecbb395 100644 --- a/src/tests/UnitTests/ExceptionFactoryTests.cs +++ b/src/tests/UnitTests/DefaultExceptionFactoryTests.cs @@ -1,19 +1,19 @@ using System; using System.Collections.Generic; -using EnsureThat; +using EnsureThat.Internals; using FluentAssertions; using Xunit; namespace UnitTests { - public class ExceptionFactoryTests : UnitTestBase + public class DefaultExceptionFactoryTests : UnitTestBase { private const string DefaultMessage = "54a170d6997c400487c177768f72aa7a"; private const string CustomMessage = "3b3e6082d4c1482fbc775abc11a2e415"; private const string DummyValue = "61927e885db34e08b9a7211c7eb74c36"; private readonly Exception _exceptionFactoryException = new KeyNotFoundException(); private readonly Exception _customException = new("036859e6693848b199575feffe17bd46"); - private readonly ExceptionFactory _sut = new(); + private readonly DefaultExceptionFactory _sut = new(); [Fact] public void ArgumentException_UsesDefaults_WhenNoOptionsAreProvided()