Skip to content

Commit

Permalink
Support enumerations in step binding arguments (Issue 28)
Browse files Browse the repository at this point in the history
  • Loading branch information
gasparnagy committed Nov 26, 2009
1 parent c35c81f commit 62a207f
Show file tree
Hide file tree
Showing 20 changed files with 47 additions and 30 deletions.
12 changes: 12 additions & 0 deletions Runtime/ObjectContainer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,18 @@ public static StepDefinitionSkeletonProvider StepDefinitionSkeletonProvider
}
#endregion

#region StepArgumentTypeConverter
private static IStepArgumentTypeConverter stepArgumentTypeConverter = null;

public static IStepArgumentTypeConverter StepArgumentTypeConverter
{
get
{
return GetOrCreate(ref stepArgumentTypeConverter, typeof(StepArgumentTypeConverter));
}
}
#endregion

#region unitTestRuntimeProvider
private static IUnitTestRuntimeProvider unitTestRuntimeProvider = null;

Expand Down
11 changes: 6 additions & 5 deletions Runtime/StepArgumentTypeConverter.cs
Original file line number Diff line number Diff line change
@@ -1,26 +1,27 @@
using System;
using System.Globalization;

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

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

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

return convertedArg;
Expand Down
6 changes: 3 additions & 3 deletions Runtime/TestRunner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public class TestRunner : ITestRunner
private readonly IUnitTestRuntimeProvider unitTestRuntimeProvider;
private readonly StepFormatter stepFormatter;
private readonly StepDefinitionSkeletonProvider stepDefinitionSkeletonProvider;
private readonly IStepArgumentTypeConverter stepArgumentTypeConverter = new StepArgumentTypeConverter();

public TestRunner()
{
Expand All @@ -27,6 +28,7 @@ public TestRunner()
unitTestRuntimeProvider = ObjectContainer.UnitTestRuntimeProvider;
stepFormatter = ObjectContainer.StepFormatter;
stepDefinitionSkeletonProvider = ObjectContainer.StepDefinitionSkeletonProvider;
stepArgumentTypeConverter = ObjectContainer.StepArgumentTypeConverter;
}

public virtual void InitializeTestRunner(Assembly[] bindingAssemblies)
Expand Down Expand Up @@ -382,7 +384,6 @@ 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 @@ -392,8 +393,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],
var convertedArg = stepArgumentTypeConverter.Convert(regexArgs[argIndex], match.StepBinding.ParameterTypes[argIndex],
FeatureContext.Current.FeatureInfo.CultureInfo);
arguments.Add(convertedArg);
}
Expand Down
2 changes: 1 addition & 1 deletion Tests/ParserTests/TestFiles/background.feature.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<Feature xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Language>en</Language>
<Language>en-US</Language>
<Background>
<Title />
<Steps>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<Feature xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Language>en</Language>
<Language>en-US</Language>
<Background>
<Title>these preconditions should be applied always</Title>
<Steps>
Expand Down
2 changes: 1 addition & 1 deletion Tests/ParserTests/TestFiles/but.feature.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<Feature xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Language>en</Language>
<Language>en-US</Language>
<Scenarios>
<Scenario>
<Title>a simple scenario</Title>
Expand Down
2 changes: 1 addition & 1 deletion Tests/ParserTests/TestFiles/comments.feature.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<Feature xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Language>en</Language>
<Language>en-US</Language>
<Scenarios>
<Scenario>
<Title>a simple scenario</Title>
Expand Down
2 changes: 1 addition & 1 deletion Tests/ParserTests/TestFiles/featureheader.feature.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<Feature xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Language>en</Language>
<Language>en-US</Language>
<Scenarios>
<Scenario>
<Title>a simple scenario</Title>
Expand Down
2 changes: 1 addition & 1 deletion Tests/ParserTests/TestFiles/full.feature.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<Feature xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Language>en</Language>
<Language>en-US</Language>
<Scenarios>
<Scenario>
<Title>a simple scenario</Title>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<Feature xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Language>en</Language>
<Language>en-US</Language>
<Scenarios>
<Scenario>
<Title>a simple scenario with duplicated keywords</Title>
Expand Down
2 changes: 1 addition & 1 deletion Tests/ParserTests/TestFiles/mixedgivenwhenthen.feature.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<Feature xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Language>en</Language>
<Language>en-US</Language>
<Scenarios>
<Scenario>
<Title>a simple scenario</Title>
Expand Down
2 changes: 1 addition & 1 deletion Tests/ParserTests/TestFiles/multilineargument.feature.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<Feature xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Language>en</Language>
<Language>en-US</Language>
<Scenarios>
<Scenario>
<Title>a scenario with a multiline argument</Title>
Expand Down
2 changes: 1 addition & 1 deletion Tests/ParserTests/TestFiles/multilinetitle.feature.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<Feature xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Language>en</Language>
<Language>en-US</Language>
<Background>
<Title>some background
with multi-line title</Title>
Expand Down
2 changes: 1 addition & 1 deletion Tests/ParserTests/TestFiles/scenariooutline.feature.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<Feature xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Language>en</Language>
<Language>en-US</Language>
<Scenarios>
<Scenario xsi:type="ScenarioOutline">
<Title>a simple scenario outline</Title>
Expand Down
2 changes: 1 addition & 1 deletion Tests/ParserTests/TestFiles/simple.feature.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<Feature xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Language>en</Language>
<Language>en-US</Language>
<Scenarios>
<Scenario>
<Title>a simple scenario</Title>
Expand Down
2 changes: 1 addition & 1 deletion Tests/ParserTests/TestFiles/tableargument.feature.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<Feature xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Language>en</Language>
<Language>en-US</Language>
<Scenarios>
<Scenario>
<Title>a scenario with a table argument</Title>
Expand Down
2 changes: 1 addition & 1 deletion Tests/ParserTests/TestFiles/tags.feature.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<Feature xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Language>en</Language>
<Language>en-US</Language>
<Tags>
<Tag>
<Name>ftag1</Name>
Expand Down
2 changes: 1 addition & 1 deletion Tests/ParserTests/TestFiles/whitespaces.feature.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<Feature xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Language>en</Language>
<Language>en-US</Language>
<Scenarios>
<Scenario>
<Title>a simple scenario</Title>
Expand Down
17 changes: 10 additions & 7 deletions Tests/RuntimeTests/StepArgumentTypeConverterTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Globalization;
using NUnit.Framework;

namespace TechTalk.SpecFlow.RuntimeTests
Expand All @@ -7,38 +8,40 @@ namespace TechTalk.SpecFlow.RuntimeTests
public class StepArgumentTypeConverterTests
{
private IStepArgumentTypeConverter _stepArgumentTypeConverter;
private CultureInfo _enUSCulture;

[SetUp]
public void SetUp()
{
_stepArgumentTypeConverter = new StepStepArgumentTypeConverter();
_stepArgumentTypeConverter = new StepArgumentTypeConverter();
_enUSCulture = new CultureInfo("en-US");
}

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

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

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

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

Expand All @@ -50,14 +53,14 @@ private enum TestEnumeration
[Test]
public void ShouldConvertStringToEnumerationType()
{
var result = _stepArgumentTypeConverter.Convert("Value1", typeof(TestEnumeration));
var result = _stepArgumentTypeConverter.Convert("Value1", typeof(TestEnumeration), _enUSCulture);
Assert.That(result, Is.EqualTo(TestEnumeration.Value1));
}

[Test]
public void ShouldConvertStringToEnumerationTypeWithDifferingCase()
{
var result = _stepArgumentTypeConverter.Convert("vALUE1", typeof(TestEnumeration));
var result = _stepArgumentTypeConverter.Convert("vALUE1", typeof(TestEnumeration), _enUSCulture);
Assert.That(result, Is.EqualTo(TestEnumeration.Value1));
}
}
Expand Down
1 change: 1 addition & 0 deletions changelog.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ New features:
+ Allow transformation of feature files from command-line and MsBuild (Issue 3)
+ Merge all command-line tool (generation, reports) to a single executable: specflow.exe
+ Support for Dutch language
+ Support enumerations in step binding arguments (Issue 28)

Fixed issues:
+ MsTest does not refresh tests automatically (Issue 25)
Expand Down

0 comments on commit 62a207f

Please sign in to comment.