Skip to content

Conversion conventions

Stephen McCafferty edited this page Feb 16, 2017 · 5 revisions

SpecFlow.Assist.Dynamic uses a few conventions for converting Gherkin to code that help you keep your specification readable for non-technical team members and business users, but is still usable when translated to code:

Column names to properties

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 date translates to BirthDate
  • Total score is translated to TotalScore
  • age is translated to age
  • age average is translated to ageAverage

Reserved characters allowed

You can even use C#-reserved characters like #$() etc. in your column names, if you really want to. SpecFlow.Assist.Dynamic will simply remove these. Any characters that are not a-z, A-Z, 1-9 or an underscore are stripped out.

Here are 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 thrown

There is also a logging function that outputs information on what conversions were made:

 - converted  'Birth date' to property 'BirthDate'
 - converted  'C$harp n@me (with strange chars)' to property 'CharpNmeWithStrangeChars'

Property value type conversions

SpecFlow.Assist.Dynamic will also try to perform a basic value conversion. It will try to convert the value using TryParse in the following order:

  1. First try to convert to DateTime
  2. Then to a double
  3. And then try to convert to bool
  4. Finally try to convert to an int.
  5. If nothing works it falls back to the most normal case: a string

Disabling type conversion

You can disable the type conversion of values for each of the main methods that SpecFlow.Assist.Dynamic exposes:

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 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);  
}