-
Notifications
You must be signed in to change notification settings - Fork 750
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added AgumentTypeConverter to convert step definition arguments from …
…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
1 parent
56797c9
commit c35c81f
Showing
8 changed files
with
100 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} | ||
} |