-
-
Notifications
You must be signed in to change notification settings - Fork 799
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 support for void sequences (2nd iteration) #463
Changes from 11 commits
a57fb7d
96b3be9
d6d83aa
fd0cbda
4624db8
cd1a4cb
08b74f3
dbcf02f
4e7d84b
45f337c
3d8c53a
6f9eed0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
//Copyright (c) 2007. Clarius Consulting, Manas Technology Solutions, InSTEDD | ||
//https://github.com/moq/moq4 | ||
//All rights reserved. | ||
|
||
//Redistribution and use in source and binary forms, | ||
//with or without modification, are permitted provided | ||
//that the following conditions are met: | ||
|
||
// * Redistributions of source code must retain the | ||
// above copyright notice, this list of conditions and | ||
// the following disclaimer. | ||
|
||
// * Redistributions in binary form must reproduce | ||
// the above copyright notice, this list of conditions | ||
// and the following disclaimer in the documentation | ||
// and/or other materials provided with the distribution. | ||
|
||
// * Neither the name of Clarius Consulting, Manas Technology Solutions or InSTEDD nor the | ||
// names of its contributors may be used to endorse | ||
// or promote products derived from this software | ||
// without specific prior written permission. | ||
|
||
//THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND | ||
//CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, | ||
//INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF | ||
//MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | ||
//DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR | ||
//CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | ||
//SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, | ||
//BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR | ||
//SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | ||
//INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, | ||
//WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING | ||
//NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | ||
//OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
//SUCH DAMAGE. | ||
|
||
//[This is the BSD license, see | ||
// http://www.opensource.org/licenses/bsd-license.php] | ||
|
||
using System; | ||
|
||
namespace Moq.Language | ||
{ | ||
/// <summary> | ||
/// Defines the <c>Pass</c> and <c>Throws</c> verbs for sequence setups | ||
/// on <c>void</c> methods. | ||
/// </summary> | ||
public interface ISetupSequentialAction | ||
{ | ||
/// <summary> | ||
/// Configures the next call in the sequence to do nothing. | ||
/// </summary> | ||
/// <example> | ||
/// The following code configures the first call to <c>Exuecute()</c> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Typo: There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Note that as there are other typos in Moq's code base, I'm OK with letting this stand, then do a focused typo hunt across the whole solution sometime in the future. (But of course you can fix it now if you want.) |
||
/// to do nothing, and the second call to throw an exception. | ||
/// <code> | ||
/// mock.SetupSequence(m => m.Execute()) | ||
/// .Pass() | ||
/// .Throws<InvalidOperationException>(); | ||
/// </code> | ||
/// </example> | ||
ISetupSequentialAction Pass(); | ||
|
||
/// <summary> | ||
/// Configures the next call in the sequence to throw an exception. | ||
/// </summary> | ||
/// <example> | ||
/// The following code configures the first call to <c>Exuecute()</c> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Typo: |
||
/// to do nothing, and the second call to throw an exception. | ||
/// <code> | ||
/// mock.SetupSequence(m => m.Execute()) | ||
/// .Pass() | ||
/// .Throws<InvalidOperationException>(); | ||
/// </code> | ||
/// </example> | ||
ISetupSequentialAction Throws<TException>() | ||
where TException : Exception, new(); | ||
|
||
/// <summary> | ||
/// Configures the next call in the sequence to throw an exception. | ||
/// </summary> | ||
/// <example> | ||
/// The following code configures the first call to <c>Exuecute()</c> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Typo: |
||
/// to do nothing, and the second call to throw an exception. | ||
/// <code> | ||
/// mock.SetupSequence(m => m.Execute()) | ||
/// .Pass() | ||
/// .Throws(new InvalidOperationException()); | ||
/// </code> | ||
/// </example> | ||
ISetupSequentialAction Throws(Exception exception); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
//Copyright (c) 2007. Clarius Consulting, Manas Technology Solutions, InSTEDD | ||
//https://github.com/moq/moq4 | ||
//All rights reserved. | ||
|
||
//Redistribution and use in source and binary forms, | ||
//with or without modification, are permitted provided | ||
//that the following conditions are met: | ||
|
||
// * Redistributions of source code must retain the | ||
// above copyright notice, this list of conditions and | ||
// the following disclaimer. | ||
|
||
// * Redistributions in binary form must reproduce | ||
// the above copyright notice, this list of conditions | ||
// and the following disclaimer in the documentation | ||
// and/or other materials provided with the distribution. | ||
|
||
// * Neither the name of Clarius Consulting, Manas Technology Solutions or InSTEDD nor the | ||
// names of its contributors may be used to endorse | ||
// or promote products derived from this software | ||
// without specific prior written permission. | ||
|
||
//THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND | ||
//CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, | ||
//INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF | ||
//MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | ||
//DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR | ||
//CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | ||
//SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, | ||
//BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR | ||
//SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | ||
//INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, | ||
//WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING | ||
//NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | ||
//OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
//SUCH DAMAGE. | ||
|
||
//[This is the BSD license, see | ||
// http://www.opensource.org/licenses/bsd-license.php] | ||
|
||
using System; | ||
using System.Linq.Expressions; | ||
using Moq.Language; | ||
using Moq.Language.Flow; | ||
|
||
namespace Moq | ||
{ | ||
internal sealed class SetupSequentialActionContext<TMock> : ISetupSequentialAction | ||
where TMock : class | ||
{ | ||
private int currentStep; | ||
private int expectationsCount; | ||
private Mock<TMock> mock; | ||
private Expression<Action<TMock>> expression; | ||
private readonly Action callbackAction; | ||
|
||
public SetupSequentialActionContext( | ||
Mock<TMock> mock, | ||
Expression<Action<TMock>> expression) | ||
{ | ||
this.mock = mock; | ||
this.expression = expression; | ||
this.callbackAction = () => currentStep++; | ||
} | ||
|
||
public ISetupSequentialAction Pass() | ||
{ | ||
var setup = this.GetSetup(); | ||
setup.Callback(DoNothing); | ||
this.EndSetup(setup); | ||
return this; | ||
} | ||
|
||
private static void DoNothing() { } | ||
|
||
public ISetupSequentialAction Throws<TException>() where TException : Exception, new() | ||
{ | ||
var setup = this.GetSetup(); | ||
setup.Throws<TException>(); | ||
this.EndSetup(setup); | ||
return this; | ||
} | ||
|
||
public ISetupSequentialAction Throws(Exception exception) | ||
{ | ||
var setup = this.GetSetup(); | ||
setup.Throws(exception); | ||
this.EndSetup(setup); | ||
return this; | ||
} | ||
|
||
private void EndSetup(ICallback callback) | ||
{ | ||
callback.Callback(callbackAction); | ||
} | ||
|
||
private ISetup<TMock> GetSetup() | ||
{ | ||
var expectationStep = this.expectationsCount; | ||
this.expectationsCount++; | ||
|
||
return this.mock | ||
.When(() => currentStep == expectationStep) | ||
.Setup(expression); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
//Copyright (c) 2007. Clarius Consulting, Manas Technology Solutions, InSTEDD | ||
//https://github.com/moq/moq4 | ||
//All rights reserved. | ||
|
||
//Redistribution and use in source and binary forms, | ||
//with or without modification, are permitted provided | ||
//that the following conditions are met: | ||
|
||
// * Redistributions of source code must retain the | ||
// above copyright notice, this list of conditions and | ||
// the following disclaimer. | ||
|
||
// * Redistributions in binary form must reproduce | ||
// the above copyright notice, this list of conditions | ||
// and the following disclaimer in the documentation | ||
// and/or other materials provided with the distribution. | ||
|
||
// * Neither the name of Clarius Consulting, Manas Technology Solutions or InSTEDD nor the | ||
// names of its contributors may be used to endorse | ||
// or promote products derived from this software | ||
// without specific prior written permission. | ||
|
||
//THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND | ||
//CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, | ||
//INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF | ||
//MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | ||
//DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR | ||
//CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | ||
//SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, | ||
//BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR | ||
//SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | ||
//INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, | ||
//WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING | ||
//NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | ||
//OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
//SUCH DAMAGE. | ||
|
||
//[This is the BSD license, see | ||
// http://www.opensource.org/licenses/bsd-license.php] | ||
|
||
using System; | ||
using Xunit; | ||
|
||
namespace Moq.Tests | ||
{ | ||
public class SequentialActionExtensionsFixture | ||
{ | ||
[Fact] | ||
public void PerformSequence() | ||
{ | ||
var mock = new Mock<IFoo>(); | ||
|
||
mock.SetupSequence(m => m.Do()) | ||
.Pass() | ||
.Throws<InvalidOperationException>() | ||
.Throws(new ArgumentException()); | ||
|
||
mock.Object.Do(); | ||
Assert.Throws<InvalidOperationException>(() => mock.Object.Do()); | ||
Assert.Throws<ArgumentException>(() => mock.Object.Do()); | ||
} | ||
|
||
[Fact] | ||
public void PerformSequenceWithThrowFirst() | ||
{ | ||
var mock = new Mock<IFoo>(); | ||
|
||
mock.SetupSequence(m => m.Do()) | ||
.Throws<InvalidOperationException>() | ||
.Pass() | ||
.Throws(new ArgumentException()); | ||
|
||
Assert.Throws<InvalidOperationException>(() => mock.Object.Do()); | ||
mock.Object.Do(); | ||
Assert.Throws<ArgumentException>(() => mock.Object.Do()); | ||
} | ||
|
||
public interface IFoo | ||
{ | ||
void Do(); | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
#451
isn't strictly wrong, as it still gives people a link to the GitHub issues and discussions, but I guess#463
would be more accurate now. :-)