Skip to content

Commit

Permalink
Added AgumentTypeConverter to convert step definition arguments from …
Browse files Browse the repository at this point in the history
…strings to enumerated types Refactored TestRunner to call ArgumentTypeConverter to convert step arguments Modified Parser and Runtime tests to explicitly set culture of en-US to prevent failing tests when machine's culture is not US by default

Signed-off-by: Gaspar Nagy <[email protected]>
  • Loading branch information
xerxesb authored and gasparnagy committed Nov 26, 2009
1 parent 56797c9 commit c35c81f
Show file tree
Hide file tree
Showing 8 changed files with 100 additions and 3 deletions.
29 changes: 29 additions & 0 deletions Runtime/StepArgumentTypeConverter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using System;

namespace TechTalk.SpecFlow
{
public interface IStepArgumentTypeConverter
{
object Convert(string value, Type typeToConvertTo);
}

public class StepStepArgumentTypeConverter : IStepArgumentTypeConverter
{
public object Convert(string value, Type typeToConvertTo)
{
object convertedArg;

var paramType = typeToConvertTo;
if (paramType.BaseType == typeof(Enum))
{
convertedArg = Enum.Parse(paramType, value, true);
}
else
{
convertedArg = System.Convert.ChangeType(value, paramType);
}

return convertedArg;
}
}
}
1 change: 1 addition & 0 deletions Runtime/TechTalk.SpecFlow.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
<Compile Include="..\VersionInfo.cs">
<Link>VersionInfo.cs</Link>
</Compile>
<Compile Include="StepArgumentTypeConverter.cs" />
<Compile Include="Attributes.cs" />
<Compile Include="ErrorHandling\BindingException.cs" />
<Compile Include="BindingMatch.cs" />
Expand Down
2 changes: 2 additions & 0 deletions Runtime/TestRunner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,7 @@ private TimeSpan InvokeAction(Delegate action, object[] arguments, MethodInfo me

private object[] GetExecuteArguments(BindingMatch match)
{
IStepArgumentTypeConverter typeConverter = new StepStepArgumentTypeConverter();
List<object> arguments = new List<object>();

var regexArgs = match.Match.Groups.Cast<Group>().Skip(1).Select(g => g.Value).ToArray();
Expand All @@ -391,6 +392,7 @@ private object[] GetExecuteArguments(BindingMatch match)

for (int argIndex = 0; argIndex < regexArgs.Length; argIndex++)
{
var convertedArg = typeConverter.Convert(regexArgs[argIndex], match.StepBinding.ParameterTypes[argIndex]);
object convertedArg = Convert.ChangeType(regexArgs[argIndex], match.StepBinding.ParameterTypes[argIndex],
FeatureContext.Current.FeatureInfo.CultureInfo);
arguments.Add(convertedArg);
Expand Down
2 changes: 1 addition & 1 deletion Tests/ParserTests/SuccessfulGenerationTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ public void CanGenerateMixedGWTFeature()
public void CanGenerateFromFile(string fileName)
{
Console.WriteLine(fileName);
SpecFlowLangParser parser = new SpecFlowLangParser(new CultureInfo("en"));
SpecFlowLangParser parser = new SpecFlowLangParser(new CultureInfo("en-US"));
using (var reader = new StreamReader(fileName))
{
Feature feature = parser.Parse(reader);
Expand Down
2 changes: 1 addition & 1 deletion Tests/ParserTests/SuccessfulParsingTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ public void CanParseMixedGWTFeature()
public void CanParseFile(string fileName)
{
Console.WriteLine(fileName);
SpecFlowLangParser parser = new SpecFlowLangParser(new CultureInfo("en"));
SpecFlowLangParser parser = new SpecFlowLangParser(new CultureInfo("en-US"));
using (var reader = new StreamReader(fileName))
{
Feature feature = parser.Parse(reader);
Expand Down
2 changes: 1 addition & 1 deletion Tests/RuntimeTests/ExecutionTestBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ public void CanExecuteMixedGWTFeature()
public void CanGenerateFromFile(string fileName)
{
Console.WriteLine(fileName);
SpecFlowLangParser parser = new SpecFlowLangParser(new CultureInfo("en"));
SpecFlowLangParser parser = new SpecFlowLangParser(new CultureInfo("en-US"));
using (var reader = new StreamReader(fileName))
{
Feature feature = parser.Parse(reader);
Expand Down
1 change: 1 addition & 0 deletions Tests/RuntimeTests/RuntimeTests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="StepArgumentTypeConverterTests.cs" />
<Compile Include="ConfigurationTest.cs" />
<Compile Include="ExecutionTestBase.cs" />
<Compile Include="NUnitTestExecutor.cs" />
Expand Down
64 changes: 64 additions & 0 deletions Tests/RuntimeTests/StepArgumentTypeConverterTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
using System;
using NUnit.Framework;

namespace TechTalk.SpecFlow.RuntimeTests
{
[TestFixture]
public class StepArgumentTypeConverterTests
{
private IStepArgumentTypeConverter _stepArgumentTypeConverter;

[SetUp]
public void SetUp()
{
_stepArgumentTypeConverter = new StepStepArgumentTypeConverter();
}

[Test]
public void ShouldConvertStringToStringType()
{
var result = _stepArgumentTypeConverter.Convert("testValue", typeof(string));
Assert.That(result, Is.EqualTo("testValue"));
}

[Test]
public void ShouldConvertStringToIntType()
{
var result = _stepArgumentTypeConverter.Convert("10", typeof(int));
Assert.That(result, Is.EqualTo(10));
}

[Test]
public void ShouldConvertStringToDateType()
{
var result = _stepArgumentTypeConverter.Convert("2009/10/06", typeof (DateTime));
Assert.That(result, Is.EqualTo(new DateTime(2009, 10, 06)));
}

[Test]
public void ShouldConvertStringToFloatType()
{
var result = _stepArgumentTypeConverter.Convert("10.01", typeof(float));
Assert.That(result, Is.EqualTo(10.01f));
}

private enum TestEnumeration
{
Value1
}

[Test]
public void ShouldConvertStringToEnumerationType()
{
var result = _stepArgumentTypeConverter.Convert("Value1", typeof(TestEnumeration));
Assert.That(result, Is.EqualTo(TestEnumeration.Value1));
}

[Test]
public void ShouldConvertStringToEnumerationTypeWithDifferingCase()
{
var result = _stepArgumentTypeConverter.Convert("vALUE1", typeof(TestEnumeration));
Assert.That(result, Is.EqualTo(TestEnumeration.Value1));
}
}
}

0 comments on commit c35c81f

Please sign in to comment.