Skip to content

Commit

Permalink
Fixes #157 so that exception factory honors options in all paths (#158)
Browse files Browse the repository at this point in the history
Resulted in move of interface (which was wrongly placed from the beginning)
  • Loading branch information
danielwertheim authored Jul 6, 2021
1 parent edf29f1 commit 08d860c
Show file tree
Hide file tree
Showing 3 changed files with 160 additions and 15 deletions.
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
using System;
using JetBrains.Annotations;

using NotNullAttribute = System.Diagnostics.CodeAnalysis.NotNullAttribute;

namespace EnsureThat.Internals
namespace EnsureThat
{
internal sealed class ExceptionFactory : IExceptionFactory
public interface IExceptionFactory
{
Exception ArgumentException([NotNull] string defaultMessage, string paramName, OptsFn optsFn = null);
Exception ArgumentNullException([NotNull] string defaultMessage, string paramName, OptsFn optsFn = null);
Exception ArgumentOutOfRangeException<TValue>([NotNull] string defaultMessage, string paramName, TValue value, OptsFn optsFn = null);
}

public sealed class ExceptionFactory : IExceptionFactory
{
[return: NotNull]
[Pure]
Expand All @@ -15,6 +21,9 @@ public Exception ArgumentException(string defaultMessage, string paramName, Opts
{
var opts = optsFn(new EnsureOptions());

if (opts.CustomExceptionFactory != null)
return opts.CustomExceptionFactory(defaultMessage, paramName);

if (opts.CustomException != null)
return opts.CustomException;

Expand Down Expand Up @@ -54,6 +63,9 @@ public Exception ArgumentOutOfRangeException<TValue>(string defaultMessage, stri
{
var opts = optsFn(new EnsureOptions());

if (opts.CustomExceptionFactory != null)
return opts.CustomExceptionFactory(defaultMessage, paramName);

if (opts.CustomException != null)
return opts.CustomException;

Expand Down
12 changes: 0 additions & 12 deletions src/projects/EnsureThat/Internals/IExceptionFactory.cs

This file was deleted.

145 changes: 145 additions & 0 deletions src/tests/UnitTests/ExceptionFactoryTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
using System;
using System.Collections.Generic;
using EnsureThat;
using FluentAssertions;
using Xunit;

namespace UnitTests
{
public class ExceptionFactoryTests : 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();

[Fact]
public void ArgumentException_UsesDefaults_WhenNoOptionsAreProvided()
=> _sut.ArgumentException(
DefaultMessage,
ParamName)
.Should()
.BeEquivalentTo(new ArgumentException(DefaultMessage, ParamName));

[Fact]
public void ArgumentException_UsesExceptionFactory_WhenOptionsAlsoContainsCustomExceptionAndCustomMessage()
=> _sut.ArgumentException(
DefaultMessage,
ParamName,
o => o
.WithMessage(CustomMessage)
.WithException(_customException)
.WithExceptionFactory((_, __) => _exceptionFactoryException))
.Should()
.Be(_exceptionFactoryException);

[Fact]
public void ArgumentException_UsesCustomException_WhenOptionsAlsoContainsCustomMessage()
=> _sut.ArgumentException(
DefaultMessage,
ParamName,
o => o
.WithMessage(CustomMessage)
.WithException(_customException))
.Should()
.Be(_customException);

[Fact]
public void ArgumentException_UsesCustomMessage_WhenOptionsHasNoOtherConfig()
=> _sut.ArgumentException(
DefaultMessage,
ParamName,
o => o
.WithMessage(CustomMessage))
.Should()
.BeEquivalentTo(new ArgumentException(CustomMessage, ParamName));

[Fact]
public void ArgumentNullException_UsesDefaults_WhenNoOptionsAreProvided()
=> _sut.ArgumentNullException(
DefaultMessage,
ParamName)
.Should()
.BeEquivalentTo(new ArgumentNullException(ParamName, DefaultMessage));

[Fact]
public void ArgumentNullException_UsesExceptionFactory_WhenOptionsAlsoContainsCustomExceptionAndCustomMessage()
=> _sut.ArgumentNullException(
DefaultMessage,
ParamName,
o => o
.WithMessage(CustomMessage)
.WithException(_customException)
.WithExceptionFactory((_, __) => _exceptionFactoryException))
.Should()
.Be(_exceptionFactoryException);

[Fact]
public void ArgumentNullException_UsesCustomException_WhenOptionsAlsoContainsCustomMessage()
=> _sut.ArgumentNullException(
DefaultMessage,
ParamName,
o => o
.WithMessage(CustomMessage)
.WithException(_customException))
.Should()
.Be(_customException);

[Fact]
public void ArgumentNullException_UsesCustomMessage_WhenOptionsHasNoOtherConfig()
=> _sut.ArgumentNullException(
DefaultMessage,
ParamName,
o => o
.WithMessage(CustomMessage))
.Should()
.BeEquivalentTo(new ArgumentNullException(ParamName, CustomMessage));

[Fact]
public void ArgumentOutOfRangeException_UsesDefaults_WhenNoOptionsAreProvided()
=> _sut.ArgumentOutOfRangeException(
DefaultMessage,
ParamName,
DummyValue)
.Should()
.BeEquivalentTo(new ArgumentOutOfRangeException(ParamName, DummyValue, DefaultMessage));

[Fact]
public void ArgumentOutOfRangeException_UsesExceptionFactory_WhenOptionsAlsoContainsCustomExceptionAndCustomMessage()
=> _sut.ArgumentOutOfRangeException(
DefaultMessage,
ParamName,
DummyValue,
o => o
.WithMessage(CustomMessage)
.WithException(_customException)
.WithExceptionFactory((_, __) => _exceptionFactoryException))
.Should()
.Be(_exceptionFactoryException);

[Fact]
public void ArgumentOutOfRangeException_UsesCustomException_WhenOptionsAlsoContainsCustomMessage()
=> _sut.ArgumentOutOfRangeException(
DefaultMessage,
ParamName,
DummyValue,
o => o
.WithMessage(CustomMessage)
.WithException(_customException))
.Should()
.Be(_customException);

[Fact]
public void ArgumentOutOfRangeException_UsesCustomMessage_WhenOptionsHasNoOtherConfig()
=> _sut.ArgumentOutOfRangeException(
DefaultMessage,
ParamName,
DummyValue,
o => o
.WithMessage(CustomMessage))
.Should()
.BeEquivalentTo(new ArgumentOutOfRangeException(ParamName, DummyValue, CustomMessage));
}
}

0 comments on commit 08d860c

Please sign in to comment.