Skip to content

Commit

Permalink
Merge pull request #1191 from adam-knights/throws-use-params-overloads
Browse files Browse the repository at this point in the history
Add new Throws overloads that allow arguments to be passed to it
  • Loading branch information
stakx committed Jul 29, 2021
2 parents 9e148f6 + 9cc906a commit 0103409
Show file tree
Hide file tree
Showing 14 changed files with 1,217 additions and 44 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Expand Up @@ -10,6 +10,8 @@ The format is loosely based on [Keep a Changelog](http://keepachangelog.com/en/1
#### Added

* `SetupSet`, `VerifySet` methods for `mock.Protected().As<>()` (@tonyhallett, #1165)
* New `Throws` method overloads that allow specifying a function with or without paramaters, to provide an exception, for example `.Throws(() => new InvalidOperationException())`
and `Setup(x => x.GetFooAsync(It.IsAny<string>()).Result).Throws((string s) => new InvalidOperationException(s))`. (@adam-knights, #1191)

#### Fixed

Expand Down
25 changes: 25 additions & 0 deletions src/Moq/Behaviors/ThrowComputedException.cs
@@ -0,0 +1,25 @@
// Copyright (c) 2007, Clarius Consulting, Manas Technology Solutions, InSTEDD, and Contributors.
// All rights reserved. Licensed under the BSD 3-Clause License; see License.txt.

using System;
using System.Diagnostics;

namespace Moq.Behaviors
{
internal sealed class ThrowComputedException : Behavior
{
private readonly Func<IInvocation, Exception> exceptionFactory;

public ThrowComputedException(Func<IInvocation, Exception> exceptionFactory)
{
Debug.Assert(exceptionFactory != null);

this.exceptionFactory = exceptionFactory;
}

public override void Execute(Invocation invocation)
{
throw this.exceptionFactory.Invoke(invocation);
}
}
}
108 changes: 108 additions & 0 deletions src/Moq/Language/Flow/SetupPhrase.cs
Expand Up @@ -159,6 +159,114 @@ public IThrowsResult Throws<TException>() where TException : Exception, new()
return this;
}

public IThrowsResult Throws(Delegate exceptionFunction)
{
this.Setup.SetThrowComputedExceptionBehavior(exceptionFunction);
return this;
}

public IThrowsResult Throws<TException>(Func<TException> exceptionFunction) where TException : Exception
{
this.Setup.SetThrowComputedExceptionBehavior(exceptionFunction);
return this;
}

public IThrowsResult Throws<T, TException>(Func<T, TException> exceptionFunction) where TException : Exception
{
this.Setup.SetThrowComputedExceptionBehavior(exceptionFunction);
return this;
}

public IThrowsResult Throws<T1, T2, TException>(Func<T1, T2, TException> exceptionFunction) where TException : Exception
{
this.Setup.SetThrowComputedExceptionBehavior(exceptionFunction);
return this;
}

public IThrowsResult Throws<T1, T2, T3, TException>(Func<T1, T2, T3, TException> exceptionFunction) where TException : Exception
{
this.Setup.SetThrowComputedExceptionBehavior(exceptionFunction);
return this;
}

public IThrowsResult Throws<T1, T2, T3, T4, TException>(Func<T1, T2, T3, T4, TException> exceptionFunction) where TException : Exception
{
this.Setup.SetThrowComputedExceptionBehavior(exceptionFunction);
return this;
}

public IThrowsResult Throws<T1, T2, T3, T4, T5, TException>(Func<T1, T2, T3, T4, T5, TException> exceptionFunction) where TException : Exception
{
this.Setup.SetThrowComputedExceptionBehavior(exceptionFunction);
return this;
}

public IThrowsResult Throws<T1, T2, T3, T4, T5, T6, TException>(Func<T1, T2, T3, T4, T5, T6, TException> exceptionFunction) where TException : Exception
{
this.Setup.SetThrowComputedExceptionBehavior(exceptionFunction);
return this;
}

public IThrowsResult Throws<T1, T2, T3, T4, T5, T6, T7, TException>(Func<T1, T2, T3, T4, T5, T6, T7, TException> exceptionFunction) where TException : Exception
{
this.Setup.SetThrowComputedExceptionBehavior(exceptionFunction);
return this;
}

public IThrowsResult Throws<T1, T2, T3, T4, T5, T6, T7, T8, TException>(Func<T1, T2, T3, T4, T5, T6, T7, T8, TException> exceptionFunction) where TException : Exception
{
this.Setup.SetThrowComputedExceptionBehavior(exceptionFunction);
return this;
}

public IThrowsResult Throws<T1, T2, T3, T4, T5, T6, T7, T8, T9, TException>(Func<T1, T2, T3, T4, T5, T6, T7, T8, T9, TException> exceptionFunction) where TException : Exception
{
this.Setup.SetThrowComputedExceptionBehavior(exceptionFunction);
return this;
}

public IThrowsResult Throws<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, TException>(Func<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, TException> exceptionFunction) where TException : Exception
{
this.Setup.SetThrowComputedExceptionBehavior(exceptionFunction);
return this;
}

public IThrowsResult Throws<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, TException>(Func<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, TException> exceptionFunction) where TException : Exception
{
this.Setup.SetThrowComputedExceptionBehavior(exceptionFunction);
return this;
}

public IThrowsResult Throws<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, TException>(Func<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, TException> exceptionFunction) where TException : Exception
{
this.Setup.SetThrowComputedExceptionBehavior(exceptionFunction);
return this;
}

public IThrowsResult Throws<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, TException>(Func<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, TException> exceptionFunction) where TException : Exception
{
this.Setup.SetThrowComputedExceptionBehavior(exceptionFunction);
return this;
}

public IThrowsResult Throws<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, TException>(Func<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, TException> exceptionFunction) where TException : Exception
{
this.Setup.SetThrowComputedExceptionBehavior(exceptionFunction);
return this;
}

public IThrowsResult Throws<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, TException>(Func<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, TException> exceptionFunction) where TException : Exception
{
this.Setup.SetThrowComputedExceptionBehavior(exceptionFunction);
return this;
}

public IThrowsResult Throws<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, TException>(Func<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, TException> exceptionFunction) where TException : Exception
{
this.Setup.SetThrowComputedExceptionBehavior(exceptionFunction);
return this;
}

public void Verifiable()
{
this.setup.MarkAsVerifiable();
Expand Down
16 changes: 16 additions & 0 deletions src/Moq/Language/Flow/SetupSequencePhrase.cs
Expand Up @@ -37,6 +37,14 @@ public ISetupSequentialAction Throws(Exception exception)
return this;
}

public ISetupSequentialAction Throws<TException>(Func<TException> exceptionFunction) where TException : Exception
{
Guard.NotNull(exceptionFunction, nameof(exceptionFunction));

this.setup.AddBehavior(new ThrowComputedException(_ => exceptionFunction()));
return this;
}

public override string ToString()
{
return setup.Expression.ToStringFixed();
Expand Down Expand Up @@ -91,6 +99,14 @@ public ISetupSequentialResult<TResult> Throws<TException>()
where TException : Exception, new()
=> this.Throws(new TException());

public ISetupSequentialResult<TResult> Throws<TException>(Func<TException> exceptionFunction) where TException : Exception
{
Guard.NotNull(exceptionFunction, nameof(exceptionFunction));

this.setup.AddBehavior(new ThrowComputedException(_ => exceptionFunction()));
return this;
}

public override string ToString()
{
return setup.Expression.ToStringFixed();
Expand Down
15 changes: 15 additions & 0 deletions src/Moq/Language/ISetupSequentialAction.cs
Expand Up @@ -55,5 +55,20 @@ ISetupSequentialAction Throws<TException>()
/// </code>
/// </example>
ISetupSequentialAction Throws(Exception exception);

/// <summary>
/// Configures the next call in the sequence to throw a calculated exception.
/// </summary>
/// <example>
/// The following code configures the first call to <c>Execute()</c>
/// to do nothing, and the second call to throw a calculated exception.
/// <code>
/// mock.SetupSequence(m => m.Execute())
/// .Pass()
/// .Throws(() => new InvalidOperationException());
/// </code>
/// </example>
ISetupSequentialAction Throws<TException>(Func<TException> exceptionFunction)
where TException : Exception;
}
}
5 changes: 5 additions & 0 deletions src/Moq/Language/ISetupSequentialResult.cs
Expand Up @@ -36,6 +36,11 @@ public interface ISetupSequentialResult<TResult>
/// </summary>
ISetupSequentialResult<TResult> Throws<TException>() where TException : Exception, new();

/// <summary>
/// Uses delegate to throws an exception
/// </summary>
ISetupSequentialResult<TResult> Throws<TException>(Func<TException> exceptionFunction) where TException : Exception;

/// <summary>
/// Calls original method
/// </summary>
Expand Down

0 comments on commit 0103409

Please sign in to comment.