-
-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
remove RhinoMocksToMoq dependency and inline the code from the repo c…
…opying the LICENSE file, this avoids the strong name issues with the NuGet package.
- Loading branch information
Showing
8 changed files
with
246 additions
and
3 deletions.
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
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,78 @@ | ||
using System; | ||
using System.Linq.Expressions; | ||
using Moq; | ||
|
||
namespace Rhino.Mocks | ||
{ | ||
public interface IExpect<T> where T : class | ||
{ | ||
void Throw(Exception exception); | ||
} | ||
|
||
public interface IExpect<T, TR> where T : class | ||
{ | ||
IExpect<T, TR> Do(Func<TR> action); | ||
|
||
IExpect<T, TR> Return(TR result); | ||
|
||
void ReturnInOrder(params TR[] results); | ||
|
||
void Throw(Exception exception); | ||
} | ||
|
||
public class Expect<T, TR> : IExpect<T, TR> where T : class | ||
{ | ||
private readonly MoqAdapter<T, TR> _moqAdapter; | ||
|
||
private bool _isResultAssigned; | ||
|
||
public Expect(Mock<T> mock, Expression<Func<T, TR>> expression) | ||
{ | ||
_moqAdapter = new MoqAdapter<T, TR>(mock, expression); | ||
} | ||
|
||
public IExpect<T, TR> Do(Func<TR> action) | ||
{ | ||
_moqAdapter.Setup(action); | ||
return this; | ||
} | ||
|
||
public IExpect<T, TR> Return(TR result) | ||
{ | ||
if (_isResultAssigned) | ||
{ | ||
throw new InvalidOperationException("Return should be setup only once"); | ||
} | ||
|
||
_isResultAssigned = true; | ||
|
||
_moqAdapter.Setup(result); | ||
return this; | ||
} | ||
|
||
public void Throw(Exception exception) | ||
{ | ||
_moqAdapter.Throws(exception); | ||
} | ||
|
||
public void ReturnInOrder(params TR[] results) | ||
{ | ||
_moqAdapter.SetupReturnInOrder(results); | ||
} | ||
} | ||
|
||
public class Expect<T> : IExpect<T> where T : class | ||
{ | ||
private readonly MoqAdapter<T> _mockAdapter; | ||
|
||
public Expect(Mock<T> mock, Expression<Action<T>> expression) | ||
{ | ||
_mockAdapter = new MoqAdapter<T>(mock, expression); | ||
} | ||
|
||
public void Throw(Exception exception) | ||
{ | ||
_mockAdapter.Throws(exception); | ||
} | ||
} | ||
} |
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,21 @@ | ||
MIT License | ||
|
||
Copyright (c) 2020 Minh Nguyen | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
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,39 @@ | ||
using System; | ||
using System.Linq.Expressions; | ||
using Moq; | ||
|
||
namespace Rhino.Mocks | ||
{ | ||
public static class MockExtensions | ||
{ | ||
public static IExpect<T, TR> Expect<T, TR>(this T obj, Expression<Func<T, TR>> expression) where T : class | ||
{ | ||
return Stub(obj, expression); | ||
} | ||
|
||
public static IExpect<T> Expect<T>(this T obj, Expression<Action<T>> expression) where T : class | ||
{ | ||
return Stub(obj, expression); | ||
} | ||
|
||
public static IExpect<T, TR> Stub<T, TR>(this T obj, Expression<Func<T, TR>> expression) where T : class | ||
{ | ||
return new Expect<T, TR>(Mock.Get(obj), expression); | ||
} | ||
|
||
public static IExpect<T> Stub<T>(this T obj, Expression<Action<T>> expression) where T : class | ||
{ | ||
return new Expect<T>(Mock.Get(obj), expression); | ||
} | ||
|
||
public static void AssertWasNotCalled<T>(this T obj, Expression<Action<T>> expression) where T : class | ||
{ | ||
Mock.Get(obj).Verify(expression, Times.Never); | ||
} | ||
|
||
public static void VerifyAllExpectations<T>(this T obj) where T : class | ||
{ | ||
Mock.Get(obj).Verify(); | ||
} | ||
} | ||
} |
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,23 @@ | ||
using Moq; | ||
|
||
namespace Rhino.Mocks | ||
{ | ||
public static class MockRepository | ||
{ | ||
public static T GenerateStub<T>() where T : class | ||
{ | ||
return GenerateMock<T>(); | ||
} | ||
|
||
public static T GenerateStrictMock<T>() where T : class | ||
{ | ||
return GenerateMock<T>(MockBehavior.Strict); | ||
} | ||
|
||
public static T GenerateMock<T>(MockBehavior behavior = MockBehavior.Default) where T : class | ||
{ | ||
var mock = new Mock<T>(behavior); | ||
return mock.Object; | ||
} | ||
} | ||
} |
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,61 @@ | ||
using System; | ||
using System.Linq.Expressions; | ||
using Moq; | ||
|
||
namespace Rhino.Mocks | ||
{ | ||
class MoqAdapter<T, TR> where T : class | ||
{ | ||
private readonly Mock<T> _mock; | ||
private readonly Expression<Func<T, TR>> _expression; | ||
|
||
public MoqAdapter(Mock<T> mock, Expression<Func<T, TR>> expression) | ||
{ | ||
_mock = mock; | ||
_expression = expression; | ||
} | ||
|
||
public void Setup(TR result) | ||
{ | ||
if (result != null) | ||
{ | ||
_mock.Setup(_expression).Returns(result); | ||
} | ||
} | ||
|
||
public void Setup(Func<TR> result) | ||
{ | ||
if (result != null) | ||
{ | ||
_mock.Setup(_expression).Returns(result); | ||
} | ||
} | ||
|
||
public void SetupReturnInOrder(params TR[] results) | ||
{ | ||
_mock.Setup(_expression).ReturnsInOrder(results); | ||
} | ||
|
||
public void Throws(Exception exception) | ||
{ | ||
_mock.Setup(_expression).Throws(exception); | ||
} | ||
} | ||
|
||
class MoqAdapter<T> where T : class | ||
{ | ||
private readonly Mock<T> _mock; | ||
private readonly Expression<Action<T>> _expression; | ||
|
||
public MoqAdapter(Mock<T> mock, Expression<Action<T>> expression) | ||
{ | ||
_mock = mock; | ||
_expression = expression; | ||
} | ||
|
||
public void Throws(Exception exception) | ||
{ | ||
_mock.Setup(_expression).Throws(exception); | ||
} | ||
} | ||
} |
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,21 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using Moq.Language.Flow; | ||
|
||
namespace Rhino.Mocks | ||
{ | ||
public static class MoqExtensions | ||
{ | ||
public static IReturnsResult<TMock> ReturnsInOrder<TMock, TResult>(this ISetup<TMock, TResult> setup, params Func<TResult>[] valueFunctions) where TMock : class | ||
{ | ||
var functionQueue = new Queue<Func<TResult>>(valueFunctions); | ||
return setup.Returns(() => functionQueue.Dequeue()()); | ||
} | ||
|
||
public static void ReturnsInOrder<TMock, TResult>(this ISetup<TMock, TResult> setup, TResult[] results) where TMock : class | ||
{ | ||
ReturnsInOrder(setup, results.Select(result => new Func<TResult>(() => result)).ToArray()); | ||
} | ||
} | ||
} |
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