-
-
Notifications
You must be signed in to change notification settings - Fork 799
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
305 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,139 @@ | ||
//Copyright (c) 2007. Clarius Consulting, Manas Technology Solutions, InSTEDD | ||
//http://code.google.com/p/moq/ | ||
//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.Collections.Generic; | ||
using System.Linq; | ||
using System.Linq.Expressions; | ||
|
||
namespace Moq | ||
{ | ||
/// <summary> | ||
/// Captures mock parameters. | ||
/// </summary> | ||
/// <example> | ||
/// Arrange code: | ||
/// <code> | ||
/// var capture = new MockCapture{string}(); | ||
/// mock.Setup(x => x.DoSomething(capture.CaptureAny())); | ||
/// </code> | ||
/// Assert code: | ||
/// <code> | ||
/// Assert.Equal("Hello!", capture.Last); | ||
/// </code> | ||
/// </example> | ||
public class MockCapture<T> | ||
{ | ||
private readonly List<T> capturedValues = new List<T>(); | ||
private Action<T> callback; | ||
private Func<T, T> copier; | ||
|
||
/// <summary> | ||
/// Initializes an instance of the capture. | ||
/// </summary> | ||
public MockCapture() | ||
{ | ||
this.All = this.capturedValues.AsReadOnly(); | ||
} | ||
|
||
/// <summary> | ||
/// Gets all the captured parameter values. | ||
/// </summary> | ||
public IReadOnlyList<T> All { get; private set; } | ||
|
||
/// <summary> | ||
/// Gets the last captured parameter value. | ||
/// </summary> | ||
public T Last { get { return this.All.Last(); } } | ||
|
||
/// <summary> | ||
/// Gets the single captured parameter value. | ||
/// </summary> | ||
public T Single { get { return this.All.Single(); } } | ||
|
||
/// <summary> | ||
/// Gets the parameter to use in the setup expression. | ||
/// </summary> | ||
/// <returns>The setup expression parameter</returns> | ||
public T CaptureAny() | ||
{ | ||
return Match.Create(value => this.CaptureParameter(value), () => It.IsAny<T>()); | ||
} | ||
|
||
/// <summary> | ||
/// Gets the parameter to use in the setup expression. | ||
/// </summary> | ||
/// <returns>The setup expression parameter</returns> | ||
public T Capture(Expression<Func<T, bool>> match) | ||
{ | ||
var matchDelegate = match.Compile(); | ||
return Match.Create(value => matchDelegate.Invoke(value) && this.CaptureParameter(value), () => It.Is<T>(match)); | ||
} | ||
|
||
/// <summary> | ||
/// Copy captured parameters with the specified delegate. | ||
/// </summary> | ||
/// <param name="copier">The delegate used to clone parameters</param> | ||
public void CopyOnCapture(Func<T, T> copier) | ||
{ | ||
this.copier = copier; | ||
} | ||
|
||
/// <summary> | ||
/// Provide an action to run on captured parameters. | ||
/// </summary> | ||
/// <paramref name="callback">an action to run on the captured parameters</paramref> | ||
public void RunOnCapture(Action<T> callback) | ||
{ | ||
this.callback = callback; | ||
} | ||
|
||
private bool CaptureParameter(T item) | ||
{ | ||
var capturedValue = this.copier != null ? this.copier.Invoke(item) : item; | ||
this.capturedValues.Add(capturedValue); | ||
|
||
if (this.callback != null) | ||
this.callback.Invoke(item); | ||
|
||
return true; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,163 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using Xunit; | ||
|
||
namespace Moq.Tests | ||
{ | ||
public class MockCaptureFixture | ||
{ | ||
[Fact] | ||
public void CanCaptureParameter() | ||
{ | ||
var capture = new MockCapture<string>(); | ||
var mock = new Mock<IFoo>(); | ||
mock.Setup(x => x.DoSomething(capture.CaptureAny())); | ||
|
||
mock.Object.DoSomething("Hello!"); | ||
|
||
var expectedValues = new List<string> { "Hello!" }.AsReadOnly(); | ||
Assert.Equal(expectedValues, capture.All); | ||
} | ||
|
||
[Fact] | ||
public void CanCaptureMultipleParameters() | ||
{ | ||
var capture = new MockCapture<string>(); | ||
var mock = new Mock<IFoo>(); | ||
mock.Setup(x => x.DoSomething(capture.CaptureAny())); | ||
|
||
mock.Object.DoSomething("Hello!"); | ||
mock.Object.DoSomething("World!"); | ||
|
||
var expectedValues = new List<string> { "Hello!", "World!" }.AsReadOnly(); | ||
Assert.Equal(expectedValues, capture.All); | ||
} | ||
|
||
[Fact] | ||
public void CanCaptureSpecificParameter() | ||
{ | ||
var capture = new MockCapture<string>(); | ||
var mock = new Mock<IFoo>(); | ||
mock.Setup(x => x.DoSomething(capture.Capture(p => p.StartsWith("W")))); | ||
|
||
mock.Object.DoSomething("Hello!"); | ||
mock.Object.DoSomething("World!"); | ||
|
||
var expectedValues = new List<string> { "World!" }.AsReadOnly(); | ||
Assert.Equal(expectedValues, capture.All); | ||
} | ||
|
||
[Fact] | ||
public void CanCaptureLastParameter() | ||
{ | ||
var capture = new MockCapture<string>(); | ||
var mock = new Mock<IFoo>(); | ||
mock.Setup(x => x.DoSomething(capture.CaptureAny())); | ||
|
||
mock.Object.DoSomething("Hello!"); | ||
mock.Object.DoSomething("World!"); | ||
|
||
Assert.Equal("World!", capture.Last); | ||
} | ||
|
||
[Fact] | ||
public void CanCaptureSingleParameter() | ||
{ | ||
var capture = new MockCapture<string>(); | ||
var mock = new Mock<IFoo>(); | ||
mock.Setup(x => x.DoSomething(capture.CaptureAny())); | ||
|
||
mock.Object.DoSomething("Hello!"); | ||
|
||
Assert.Equal("Hello!", capture.Single); | ||
} | ||
|
||
[Fact] | ||
public void ThrowsIfSingleIsCalledWithNoParameter() | ||
{ | ||
var capture = new MockCapture<string>(); | ||
var mock = new Mock<IFoo>(); | ||
mock.Setup(x => x.DoSomething(capture.CaptureAny())); | ||
|
||
Assert.Throws<InvalidOperationException>(() => capture.Single); | ||
} | ||
|
||
[Fact] | ||
public void ThrowsIfSingleIsCalledWithManyParameter() | ||
{ | ||
var capture = new MockCapture<string>(); | ||
var mock = new Mock<IFoo>(); | ||
mock.Setup(x => x.DoSomething(capture.CaptureAny())); | ||
|
||
mock.Object.DoSomething("Hello!"); | ||
mock.Object.DoSomething("World!"); | ||
|
||
Assert.Throws<InvalidOperationException>(() => capture.Single); | ||
} | ||
|
||
[Fact] | ||
public void CanRunCallbackOnParameter() | ||
{ | ||
var capture = new MockCapture<FooParam>(); | ||
capture.RunOnCapture(x => x.Value = 42); | ||
|
||
var mock = new Mock<IFoo>(); | ||
mock.Setup(x => x.DoSomething(capture.CaptureAny())); | ||
|
||
var param = new FooParam(0); | ||
mock.Object.DoSomething(param); | ||
|
||
Assert.Equal(42, param.Value); | ||
} | ||
|
||
[Fact] | ||
public void CanCopyParameter() | ||
{ | ||
var capture = new MockCapture<FooParam>(); | ||
capture.CopyOnCapture(x => new FooParam(x.Value)); | ||
|
||
var mock = new Mock<IFoo>(); | ||
mock.Setup(x => x.DoSomething(capture.CaptureAny())); | ||
|
||
var param = new FooParam(41); | ||
mock.Object.DoSomething(param); | ||
param.Value = 42; | ||
mock.Object.DoSomething(param); | ||
param.Value = 0; | ||
|
||
var expectedValues = new List<FooParam> { new FooParam(41), new FooParam(42) }.AsReadOnly(); | ||
Assert.Equal(expectedValues, capture.All); | ||
} | ||
|
||
public interface IFoo | ||
{ | ||
void DoSomething(string item); | ||
void DoSomething(FooParam item); | ||
} | ||
|
||
public class FooParam : IEquatable<FooParam> | ||
{ | ||
public FooParam(int value) | ||
{ | ||
Value = value; | ||
} | ||
|
||
public int Value { get; set; } | ||
|
||
public bool Equals(FooParam other) | ||
{ | ||
return other != null && Value == other.Value; | ||
} | ||
|
||
public override bool Equals(object obj) | ||
{ | ||
return Equals(obj as FooParam); | ||
} | ||
|
||
public override int GetHashCode() | ||
{ | ||
return Value; | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters