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

Improve tests to make the migration to NSubstitute easier #501

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions Tests/Reqnroll.RuntimeTests/ArgumentHelpers.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using Moq;
using Reqnroll.Bindings.Reflection;

namespace Reqnroll.RuntimeTests;

internal static class ArgumentHelpers
{
public static IBindingType IsBindingType<T>()
{
return It.Is<IBindingType>(bt => bt.TypeEquals(typeof(T)));
}
}
15 changes: 10 additions & 5 deletions Tests/Reqnroll.RuntimeTests/StepArgumentTransformationTests.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
Expand Down Expand Up @@ -84,6 +85,7 @@ public class StepArgumentTransformationTests
private readonly Mock<IContextManager> contextManagerStub = new Mock<IContextManager>();
private readonly Mock<IAsyncBindingInvoker> methodBindingInvokerStub = new Mock<IAsyncBindingInvoker>();
private readonly List<IStepArgumentTransformationBinding> stepTransformations = new List<IStepArgumentTransformationBinding>();
private readonly CultureInfo _enUSCulture = new CultureInfo("en-US", false);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FYI same pattern as in StepArgumentTypeConverterTest.cs


public StepArgumentTransformationTests()
{
Expand Down Expand Up @@ -179,7 +181,8 @@ public async Task StepArgumentTypeConverterShouldUseUserConverterForConversion()

var stepArgumentTypeConverter = CreateStepArgumentTypeConverter();

var result = await stepArgumentTypeConverter.ConvertAsync("user xyz", typeof(User), new CultureInfo("en-US", false));
var runtimeBindingType = new RuntimeBindingType(typeof(User));
var result = await stepArgumentTypeConverter.ConvertAsync("user xyz", runtimeBindingType, _enUSCulture);
result.Should().Be(resultUser);
}

Expand All @@ -197,7 +200,8 @@ public async Task StepArgumentTypeConverterShouldUseAsyncUserConverterForConvers

var stepArgumentTypeConverter = CreateStepArgumentTypeConverter();

var result = await stepArgumentTypeConverter.ConvertAsync("user xyz", typeof(User), new CultureInfo("en-US", false));
var runtimeBindingType = new RuntimeBindingType(typeof(User));
var result = await stepArgumentTypeConverter.ConvertAsync("user xyz", runtimeBindingType, _enUSCulture);
result.Should().Be(resultUser);
}

Expand All @@ -215,7 +219,8 @@ public async Task StepArgumentTypeConverterShouldUseAsyncValueTaskUserConverterF

var stepArgumentTypeConverter = CreateStepArgumentTypeConverter();

var result = await stepArgumentTypeConverter.ConvertAsync("user xyz", typeof(User), new CultureInfo("en-US", false));
var typeToConvertTo = new RuntimeBindingType(typeof(User));
var result = await stepArgumentTypeConverter.ConvertAsync("user xyz", typeToConvertTo, _enUSCulture);
result.Should().Be(resultUser);
}

Expand All @@ -240,8 +245,8 @@ public async Task ShouldUseStepArgumentTransformationToConvertTable()

var stepArgumentTypeConverter = CreateStepArgumentTypeConverter();


var result = await stepArgumentTypeConverter.ConvertAsync(table, typeof(IEnumerable<User>), new CultureInfo("en-US", false));
var typeToConvertTo = new RuntimeBindingType(typeof(IEnumerable<User>));
var result = await stepArgumentTypeConverter.ConvertAsync(table, typeToConvertTo, _enUSCulture);

result.Should().NotBeNull();
result.Should().Be(resultUsers);
Expand Down
46 changes: 30 additions & 16 deletions Tests/Reqnroll.RuntimeTests/StepArgumentTypeConverterTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,28 +35,32 @@ public StepArgumentTypeConverterTests()
[Fact]
public async Task ShouldConvertStringToStringType()
{
var result = await _stepArgumentTypeConverter.ConvertAsync("testValue", typeof(string), _enUSCulture);
var typeToConvertTo = new RuntimeBindingType(typeof(string));
var result = await _stepArgumentTypeConverter.ConvertAsync("testValue", typeToConvertTo, _enUSCulture);
result.Should().Be("testValue");
}

[Fact]
public async Task ShouldConvertStringToIntType()
{
var result = await _stepArgumentTypeConverter.ConvertAsync("10", typeof(int), _enUSCulture);
var typeToConvertTo = new RuntimeBindingType(typeof(int));
var result = await _stepArgumentTypeConverter.ConvertAsync("10", typeToConvertTo, _enUSCulture);
result.Should().Be(10);
}

[Fact]
public async Task ShouldConvertStringToDateType()
{
var result = await _stepArgumentTypeConverter.ConvertAsync("2009/10/06", typeof(DateTime), _enUSCulture);
var typeToConvertTo = new RuntimeBindingType(typeof(DateTime));
var result = await _stepArgumentTypeConverter.ConvertAsync("2009/10/06", typeToConvertTo, _enUSCulture);
result.Should().Be(new DateTime(2009, 10, 06));
}

[Fact]
public async Task ShouldConvertStringToFloatType()
{
var result = await _stepArgumentTypeConverter.ConvertAsync("10.01", typeof(float), _enUSCulture);
var typeToConvertTo = new RuntimeBindingType(typeof(float));
var result = await _stepArgumentTypeConverter.ConvertAsync("10.01", typeToConvertTo, _enUSCulture);
result.Should().Be(10.01f);
}

Expand All @@ -68,57 +72,65 @@ private enum TestEnumeration
[Fact]
public async Task ShouldConvertStringToEnumerationType()
{
var result = await _stepArgumentTypeConverter.ConvertAsync("Value1", typeof(TestEnumeration), _enUSCulture);
var typeToConvertTo = new RuntimeBindingType(typeof(TestEnumeration));
var result = await _stepArgumentTypeConverter.ConvertAsync("Value1", typeToConvertTo, _enUSCulture);
result.Should().Be(TestEnumeration.Value1);
}

[Fact]
public async Task ShouldConvertStringToEnumerationTypeWithDifferingCase()
{
var result = await _stepArgumentTypeConverter.ConvertAsync("vALUE1", typeof(TestEnumeration), _enUSCulture);
var typeToConvertTo = new RuntimeBindingType(typeof(TestEnumeration));
var result = await _stepArgumentTypeConverter.ConvertAsync("vALUE1", typeToConvertTo, _enUSCulture);
result.Should().Be(TestEnumeration.Value1);
}

[Fact]
public async Task ShouldConvertStringToEnumerationTypeWithWhitespace()
{
var result = await _stepArgumentTypeConverter.ConvertAsync("Value 1", typeof(TestEnumeration), _enUSCulture);
var typeToConvertTo = new RuntimeBindingType(typeof(TestEnumeration));
var result = await _stepArgumentTypeConverter.ConvertAsync("Value 1", typeToConvertTo, _enUSCulture);
result.Should().Be(TestEnumeration.Value1);
}

[Fact]
public async Task ShouldConvertGuidToGuidType()
{
var result = await _stepArgumentTypeConverter.ConvertAsync("{EF338B79-FD29-488F-8CA7-39C67C2B8874}", typeof (Guid), _enUSCulture);
var typeToConvertTo = new RuntimeBindingType(typeof (Guid));
var result = await _stepArgumentTypeConverter.ConvertAsync("{EF338B79-FD29-488F-8CA7-39C67C2B8874}", typeToConvertTo, _enUSCulture);
result.Should().Be(new Guid("{EF338B79-FD29-488F-8CA7-39C67C2B8874}"));
}

[Fact]
public async Task ShouldConvertNullableGuidToGuidType()
{
var result = await _stepArgumentTypeConverter.ConvertAsync("{1081CFD1-F31F-420F-9360-40590ABEF887}", typeof(Guid?), _enUSCulture);
var typeToConvertTo = new RuntimeBindingType(typeof(Guid?));
var result = await _stepArgumentTypeConverter.ConvertAsync("{1081CFD1-F31F-420F-9360-40590ABEF887}", typeToConvertTo, _enUSCulture);
result.Should().Be(new Guid("{1081CFD1-F31F-420F-9360-40590ABEF887}"));
}

[Fact]
public async Task ShouldConvertNullableGuidWithEmptyValueToNull()
{
var result = await _stepArgumentTypeConverter.ConvertAsync("", typeof(Guid?), _enUSCulture);
var typeToConvertTo = new RuntimeBindingType(typeof(Guid?));
var result = await _stepArgumentTypeConverter.ConvertAsync("", typeToConvertTo, _enUSCulture);
result.Should().BeNull();
}

[Fact]
public async Task ShouldConvertLooseGuids()
{
var result = await _stepArgumentTypeConverter.ConvertAsync("1", typeof (Guid), _enUSCulture);
var typeToConvertTo = new RuntimeBindingType(typeof (Guid));
var result = await _stepArgumentTypeConverter.ConvertAsync("1", typeToConvertTo, _enUSCulture);
result.Should().Be(new Guid("10000000-0000-0000-0000-000000000000"));
}

[Fact]
public async Task ShouldUseATypeConverterWhenAvailable()
{
var originalValue = new DateTimeOffset(2019, 7, 29, 0, 0, 0, TimeSpan.Zero);
var result = await _stepArgumentTypeConverter.ConvertAsync(originalValue, typeof (TestClass), _enUSCulture);
var typeToConvertTo = new RuntimeBindingType(typeof (TestClass));
var result = await _stepArgumentTypeConverter.ConvertAsync(originalValue, typeToConvertTo, _enUSCulture);
result.Should().BeOfType(typeof(TestClass));
result.As<TestClass>().Time.Should().Be(originalValue);
}
Expand All @@ -129,8 +141,9 @@ public async Task ShouldTraceWarningIfMultipleTransformationsFound()
var method = typeof(TestClass).GetMethod(nameof(TestClass.StringToIntConverter));
_stepTransformations.Add(new StepArgumentTransformationBinding(@"\d+", new RuntimeBindingMethod(method)));
_stepTransformations.Add(new StepArgumentTransformationBinding(@".*", new RuntimeBindingMethod(method)));

await _stepArgumentTypeConverter.ConvertAsync("1", typeof(int), _enUSCulture);

var typeToConvertTo = new RuntimeBindingType(typeof(int));
await _stepArgumentTypeConverter.ConvertAsync("1", typeToConvertTo, _enUSCulture);

_testTracer.Verify(c => c.TraceWarning(It.IsAny<string>()), Times.Once);
}
Expand All @@ -142,8 +155,9 @@ public async Task ShouldNotTraceWarningIfTransformationsHaveDifferentOrder()

_stepTransformations.Add(new StepArgumentTransformationBinding(@"\d+", new RuntimeBindingMethod(method)));
_stepTransformations.Add(new StepArgumentTransformationBinding(@".*", new RuntimeBindingMethod(method), order: 10));

await _stepArgumentTypeConverter.ConvertAsync("1", typeof(int), _enUSCulture);

var typeToConvertTo = new RuntimeBindingType(typeof(int));
await _stepArgumentTypeConverter.ConvertAsync("1", typeToConvertTo, _enUSCulture);

_testTracer.Verify(c => c.TraceWarning(It.IsAny<string>()), Times.Never);
}
Expand Down
79 changes: 13 additions & 66 deletions Tests/Reqnroll.RuntimeTests/StepExecutionTestsWithConversions.cs
Original file line number Diff line number Diff line change
@@ -1,36 +1,12 @@
using System;
using System.Globalization;
using System.Linq.Expressions;
using System.Threading.Tasks;
using FluentAssertions;
using Xunit;
using Moq;
using Reqnroll.Bindings;
using Reqnroll.Bindings.Reflection;

namespace Reqnroll.RuntimeTests
{
internal static class LegacyStepArgumentTypeConverterExtensions
{
public static async Task<object> ConvertAsync(this IStepArgumentTypeConverter converter, object value, Type typeToConvertTo, CultureInfo cultureInfo)
{
return await converter.ConvertAsync(value, new RuntimeBindingType(typeToConvertTo), cultureInfo);
}

public static Expression<Func<IStepArgumentTypeConverter, bool>> GetCanConvertMethodFilter(object argument, Type type)
{
return c => c.CanConvert(argument, It.Is<IBindingType>(bt => bt.TypeEquals(type)), It.IsAny<CultureInfo>());
}

public static Expression<Func<IStepArgumentTypeConverter, Task<object>>> GetConvertAsyncMethodFilter(object argument, Type type)
{
return c => c.ConvertAsync(It.Is<object>(s => s.Equals(argument)), It.Is<IBindingType>(bt => bt.TypeEquals(type)), It.IsAny<CultureInfo>());
//Arg<string>.Is.Equal(argument),
//Arg<IBindingType>.Matches(bt => bt.TypeEquals(type)),
//Arg<CultureInfo>.Is.Anything);
}
}

[Binding]
public class StepExecutionTestsBindingsForArgumentConvert
{
Expand Down Expand Up @@ -59,18 +35,14 @@ public virtual void DoubleArgWithTable(double param, Table table)
}
}


public class StepExecutionTestsWithConversions : StepExecutionTestsBase
{
[Fact]
public async Task ShouldCallBindingWithSimpleConvertParam()
{
var (testRunner, bindingMock) = GetTestRunnerFor<StepExecutionTestsBindings>();

//bindingInstance.Expect(b => b.BindingWithSimpleConvertParam(1.23));

//MockRepository.ReplayAll();

await testRunner.GivenAsync("sample step with simple convert param: 1.23");

GetLastTestStatus().Should().Be(ScenarioExecutionStatus.OK);
Expand All @@ -80,9 +52,7 @@ public async Task ShouldCallBindingWithSimpleConvertParam()
[Fact]
public async Task ShouldRaiseErrorIfSimpleConvertParamFails()
{
var (testRunner, bindingMock) = GetTestRunnerFor<StepExecutionTestsBindings>();

//MockRepository.ReplayAll();
var (testRunner, _) = GetTestRunnerFor<StepExecutionTestsBindings>();

await testRunner.GivenAsync("sample step with simple convert param: not-a-double");

Expand All @@ -92,45 +62,31 @@ public async Task ShouldRaiseErrorIfSimpleConvertParamFails()
[Fact]
public async Task ShouldCallTheOnlyThatCanConvert()
{
StepArgumentTypeConverterStub.Setup(LegacyStepArgumentTypeConverterExtensions.GetCanConvertMethodFilter("argument", typeof(double))).Returns(true);
//StepArgumentTypeConverterStub.Setup(c => c.CanConvert(It.IsAny<object>(), It.IsAny<IBindingType>(), It.IsAny<CultureInfo>())).Returns(false);
StepArgumentTypeConverterStub.Setup(LegacyStepArgumentTypeConverterExtensions.GetConvertAsyncMethodFilter("argument", typeof(double))).ReturnsAsync(1.23);
StepArgumentTypeConverterStub.Setup(c => c.CanConvert("argument", ArgumentHelpers.IsBindingType<double>(), It.IsAny<CultureInfo>())).Returns(true);
StepArgumentTypeConverterStub.Setup(c => c.ConvertAsync("argument", ArgumentHelpers.IsBindingType<double>(), It.IsAny<CultureInfo>())).ReturnsAsync(1.23);

var (testRunner, bindingMock) = GetTestRunnerWithConverterStub<StepExecutionTestsBindingsForArgumentConvert>();




//bindingInstance.Expect(b => b.DoubleArg(1.23));

//MockRepository.ReplayAll();

await testRunner.GivenAsync("sample step for argument convert: argument");


GetLastTestStatus().Should().Be(ScenarioExecutionStatus.OK, ContextManagerStub.ScenarioContext.TestError?.ToString());
bindingMock.Verify(x => x.DoubleArg(1.23));
//StepArgumentTypeConverterStub.Verify(c => c.Convert("argument", It.Is<IBindingType>(bt => bt.TypeEquals(typeof(double))), It.IsAny<CultureInfo>())).; LegacyStepArgumentTypeConverterExtensions.GetConvertMethodFilter("argument", typeof(double))).Return(1.23);
}



[Fact]
public async Task ShouldRaiseAmbiguousIfMultipleCanConvert()
{
StepArgumentTypeConverterStub.Setup(LegacyStepArgumentTypeConverterExtensions.GetCanConvertMethodFilter("argument", typeof(double))).Returns(true);
StepArgumentTypeConverterStub.Setup(LegacyStepArgumentTypeConverterExtensions.GetCanConvertMethodFilter("argument", typeof(int))).Returns(true);
StepArgumentTypeConverterStub.Setup(c => c.CanConvert("argument", ArgumentHelpers.IsBindingType<double>(), It.IsAny<CultureInfo>())).Returns(true);
StepArgumentTypeConverterStub.Setup(c => c.CanConvert("argument", ArgumentHelpers.IsBindingType<int>(), It.IsAny<CultureInfo>())).Returns(true);
StepArgumentTypeConverterStub.Setup(c => c.CanConvert(It.IsAny<object>(), It.IsAny<IBindingType>(), It.IsAny<CultureInfo>())).Returns(false);

var (testRunner, bindingMock) = GetTestRunnerWithConverterStub<StepExecutionTestsBindingsForArgumentConvert>();

// return false unless its a Double or an Int

//MockRepository.ReplayAll();
var (testRunner, _) = GetTestRunnerWithConverterStub<StepExecutionTestsBindingsForArgumentConvert>();

await testRunner.GivenAsync("sample step for argument convert: argument");


GetLastTestStatus().Should().Be(ScenarioExecutionStatus.BindingError, ContextManagerStub.ScenarioContext.TestError?.ToString());
}

Expand All @@ -139,38 +95,29 @@ public async Task ShouldCallTheOnlyThatCanConvertWithTable()
{
Table table = new Table("h1");

// return false unless its a Double or table->table
StepArgumentTypeConverterStub.Setup(LegacyStepArgumentTypeConverterExtensions.GetCanConvertMethodFilter("argument", typeof(double))).Returns(true);
//StepArgumentTypeConverterStub.Setup(c => c.CanConvert(It.IsAny<object>(), It.IsAny<IBindingType>(), It.IsAny<CultureInfo>())).Returns(false);
// return false unless it's a Double or table->table
StepArgumentTypeConverterStub.Setup(c => c.CanConvert("argument", ArgumentHelpers.IsBindingType<double>(), It.IsAny<CultureInfo>())).Returns(true);

StepArgumentTypeConverterStub.Setup(LegacyStepArgumentTypeConverterExtensions.GetConvertAsyncMethodFilter(table, typeof(Table))).ReturnsAsync(table);
StepArgumentTypeConverterStub.Setup(LegacyStepArgumentTypeConverterExtensions.GetConvertAsyncMethodFilter("argument", typeof(double))).ReturnsAsync(1.23);
StepArgumentTypeConverterStub.Setup(c => c.ConvertAsync(It.Is<object>(s => s.Equals(table)), ArgumentHelpers.IsBindingType<Table>(), It.IsAny<CultureInfo>())).ReturnsAsync(table);
StepArgumentTypeConverterStub.Setup(c => c.ConvertAsync("argument", ArgumentHelpers.IsBindingType<double>(), It.IsAny<CultureInfo>())).ReturnsAsync(1.23);


var (testRunner, bindingMock) = GetTestRunnerWithConverterStub<StepExecutionTestsBindingsForArgumentConvert>();


//bindingInstance.Expect(b => b.DoubleArgWithTable(1.23, table));

//MockRepository.ReplayAll();

await testRunner.GivenAsync("sample step for argument convert with table: argument", null, table);


GetLastTestStatus().Should().Be(ScenarioExecutionStatus.OK, ContextManagerStub.ScenarioContext.TestError?.ToString());
bindingMock.Verify(x => x.DoubleArgWithTable(1.23, table));
}

[Fact]
public async Task ShouldRaiseParamErrorIfNoneCanConvert()
{
var (testRunner, bindingMock) = GetTestRunnerFor<StepExecutionTestsBindingsForArgumentConvert>();
var (testRunner, _) = GetTestRunnerFor<StepExecutionTestsBindingsForArgumentConvert>();

// none can convert
StepArgumentTypeConverterStub.Setup(c => c.CanConvert(It.IsAny<object>(), It.IsAny<IBindingType>(), It.IsAny<CultureInfo>())).Returns(false);

// MockRepository.ReplayAll();

await testRunner.GivenAsync("sample step for argument convert: argument");

GetLastTestStatus().Should().Be(ScenarioExecutionStatus.BindingError, ContextManagerStub.ScenarioContext.TestError?.ToString());
Expand Down
Loading