Skip to content

Commit

Permalink
remove RhinoMocksToMoq dependency and inline the code from the repo c…
Browse files Browse the repository at this point in the history
…opying the LICENSE file, this avoids the strong name issues with the NuGet package.
  • Loading branch information
hahn-kev committed Jul 30, 2024
1 parent c02e0d1 commit d1437c4
Show file tree
Hide file tree
Showing 8 changed files with 246 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -319,10 +319,10 @@ public override void TestSetup()
m_para.Stub(p => p.Id).Return(paraId);
m_para.Contents = TsStringUtils.EmptyString(Cache.DefaultVernWs);

GetBtDelegate getBtDelegate = () =>
Func<ICmTranslation> getBtDelegate = () =>
m_para.TranslationsOC.FirstOrDefault(trans => trans.TypeRA != null &&
trans.TypeRA.Guid == CmPossibilityTags.kguidTranBackTranslation);
Mock.Get(m_para).Setup(p => p.GetBT()).Returns(getBtDelegate);
m_para.Stub(p => p.GetBT()).Do(getBtDelegate);
}
#endregion

Expand Down
78 changes: 78 additions & 0 deletions tests/SIL.LCModel.Tests/RhinoMocksToMoq/Expect.cs
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);
}
}
}
21 changes: 21 additions & 0 deletions tests/SIL.LCModel.Tests/RhinoMocksToMoq/LICENSE
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.
39 changes: 39 additions & 0 deletions tests/SIL.LCModel.Tests/RhinoMocksToMoq/MockExtensions.cs
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();
}
}
}
23 changes: 23 additions & 0 deletions tests/SIL.LCModel.Tests/RhinoMocksToMoq/MockRepository.cs
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;
}
}
}
61 changes: 61 additions & 0 deletions tests/SIL.LCModel.Tests/RhinoMocksToMoq/MoqAdapter.cs
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);
}
}
}
21 changes: 21 additions & 0 deletions tests/SIL.LCModel.Tests/RhinoMocksToMoq/MoqExtensions.cs
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());
}
}
}
2 changes: 1 addition & 1 deletion tests/SIL.LCModel.Tests/SIL.LCModel.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ This package provides unit tests for SIL.LCModel.</Description>
<PackageReference Include="GitVersion.MsBuild" Version="5.6.10" PrivateAssets="All" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.0.0" />
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.0.0" PrivateAssets="All" />
<PackageReference Include="Moq" Version="4.20.70" />
<PackageReference Include="NUnit" Version="3.13.3" />
<PackageReference Include="NUnit3TestAdapter" Version="4.3.2" />
<PackageReference Include="RhinoMocksToMoq" Version="0.2.0"/>
<PackageReference Include="SIL.ReleaseTasks" Version="2.5.0" PrivateAssets="All" />
</ItemGroup>

Expand Down

0 comments on commit d1437c4

Please sign in to comment.