diff --git a/Runtime/StepArgumentTypeConverter.cs b/Runtime/StepArgumentTypeConverter.cs new file mode 100644 index 000000000..4b567ede8 --- /dev/null +++ b/Runtime/StepArgumentTypeConverter.cs @@ -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; + } + } +} \ No newline at end of file diff --git a/Runtime/TechTalk.SpecFlow.csproj b/Runtime/TechTalk.SpecFlow.csproj index a0c67bc38..abffc489a 100644 --- a/Runtime/TechTalk.SpecFlow.csproj +++ b/Runtime/TechTalk.SpecFlow.csproj @@ -59,6 +59,7 @@ VersionInfo.cs + diff --git a/Runtime/TestRunner.cs b/Runtime/TestRunner.cs index 80e2e3d02..d925aee55 100644 --- a/Runtime/TestRunner.cs +++ b/Runtime/TestRunner.cs @@ -382,6 +382,7 @@ private TimeSpan InvokeAction(Delegate action, object[] arguments, MethodInfo me private object[] GetExecuteArguments(BindingMatch match) { + IStepArgumentTypeConverter typeConverter = new StepStepArgumentTypeConverter(); List arguments = new List(); var regexArgs = match.Match.Groups.Cast().Skip(1).Select(g => g.Value).ToArray(); @@ -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); diff --git a/Tests/ParserTests/SuccessfulGenerationTest.cs b/Tests/ParserTests/SuccessfulGenerationTest.cs index 0fdadb661..5355b007b 100644 --- a/Tests/ParserTests/SuccessfulGenerationTest.cs +++ b/Tests/ParserTests/SuccessfulGenerationTest.cs @@ -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); diff --git a/Tests/ParserTests/SuccessfulParsingTest.cs b/Tests/ParserTests/SuccessfulParsingTest.cs index d0b0f64b7..52c99ccfc 100644 --- a/Tests/ParserTests/SuccessfulParsingTest.cs +++ b/Tests/ParserTests/SuccessfulParsingTest.cs @@ -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); diff --git a/Tests/RuntimeTests/ExecutionTestBase.cs b/Tests/RuntimeTests/ExecutionTestBase.cs index f86220a18..2b7e0c595 100644 --- a/Tests/RuntimeTests/ExecutionTestBase.cs +++ b/Tests/RuntimeTests/ExecutionTestBase.cs @@ -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); diff --git a/Tests/RuntimeTests/RuntimeTests.csproj b/Tests/RuntimeTests/RuntimeTests.csproj index 5c9158ec6..b631c251d 100644 --- a/Tests/RuntimeTests/RuntimeTests.csproj +++ b/Tests/RuntimeTests/RuntimeTests.csproj @@ -64,6 +64,7 @@ + diff --git a/Tests/RuntimeTests/StepArgumentTypeConverterTests.cs b/Tests/RuntimeTests/StepArgumentTypeConverterTests.cs new file mode 100644 index 000000000..99c1c1975 --- /dev/null +++ b/Tests/RuntimeTests/StepArgumentTypeConverterTests.cs @@ -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)); + } + } +} \ No newline at end of file