-
Notifications
You must be signed in to change notification settings - Fork 27
Conversion conventions
SpecFlow.Assist.Dynamic uses a few conventions, for conversion of Gherkin to code, that helps you keep your specification readable for non-technical team members and business users but still usable when translated to code:
When translating column names to properties spaces are removed and each new word is capitalised. The first word is left alone though. So for example:
-
Birth datetranslates toBirthDate -
Total scoreis translated toTotalScore -
ageis translated toage -
age averageis translated toageAverage
You can even use C#-reserved characters like #$() etc in your column names, if you really want to. SpecFlow.Assist.Dynamic will simply take those away. Any thing that is not a-z, A-Z, 1-9 or an underscore is stripped out.
Here's some specs that show you how this works:
Scenario: Using reserved C# characters in column names
When I create a dynamic instance from this table
| C$harp n@me (with strange chars) |
| A value |
Then the CharpNmeWithStrangeChars property should equal 'A value'
Scenario: Only alpha-numeric characters, plus underscore is allowed in variable names
When I create a dynamic instance from this table
| My_Nice_Variable | My $$ Variable (needs clean up) |
| A value | Another value |
Then the My_Nice_Variable property should equal 'A value'
And the MyVariableNeedsCleanUp property should equal 'Another value'
Scenario: Using only reserved C# characters in column names
When I create a dynamic instance from this table
| $@() |
| A value |
Then an exception with a nice error message about the property only containing reserved chars should be thrownThere's some logging that shows how the conversions are made:
- converted 'Birth date' to property 'BirthDate'
- converted 'C$harp n@me (with strange chars)' to property 'CharpNmeWithStrangeChars'
SpecFlow.Assist.Dynamic will try to do a basic value conversion too. It will try to convert by doing a TryParse in the following order:
- First try to convert to
DateTime - Then to a
double - And then try to convert to
bool - Finally try to convert to an
int. - If nothing works it falls back to the most normal case; a
string
There's an option to disable the type conversion of values for each of the main methods that SpecFlow.Assist.Dynamic is exposing:
public static ExpandoObject CreateDynamicInstance(this Table table, bool doTypeConversion = true){}
public static IEnumerable<dynamic> CreateDynamicSet(this Table table, bool doTypeConversion = true){}
public static void CompareToDynamicInstance(this Table table, dynamic instance, bool doTypeConversion = true){}
public static void CompareToDynamicSet(this Table table, IList<dynamic> set, bool doTypeConversion = true){}If the doTypeConversion is set to false only strings will be used. Here's an example feature showing this in action:
Scenario: There's ways to disable type conversion for instance creation
When I create a dynamic instance from this table using no type conversion
| Name | Age | Birth date | Length in meters |
| 012345 | 044 | 1972-13-09 | 1,96 |
Then the Name value should still be '012345'
And the Age value should still be '044'
And the birth date should stil be '1972-13-09'
And length in meter should still be '1,96'and the relevant step, in this scenario When could be implemented like this:
[Given(@"I create a dynamic instance from this table using no type conversion")]
public void WhenICreateADynamicInstanceFromThisTableUsingNoTypeConversion(Table table)
{
dynamic instance = table.CreateDynamicInstance(false);
}