Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add new Throws overloads that allow arguments to be passed to it #1191

Merged
merged 3 commits into from Jul 29, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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));
stakx marked this conversation as resolved.
Show resolved Hide resolved

this.setup.AddBehavior(new ThrowComputedException(_ => exceptionFunction()));
stakx marked this conversation as resolved.
Show resolved Hide resolved
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