Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added the ability to use a custom property name mapper and property value parser #21

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
188 changes: 87 additions & 101 deletions SpecFlow.Assist.Dynamic/DynamicTableHelpers.cs

Large diffs are not rendered by default.

26 changes: 26 additions & 0 deletions SpecFlow.Assist.Dynamic/Options.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using SpecFlow.Assist.Dynamic.PropertyNameMapping;
using SpecFlow.Assist.Dynamic.PropertyValueParser;

namespace SpecFlow.Assist.Dynamic
{
public class Options
{
public Options()
{
this.DoTypeConversion = true;
this.PropertyNameMapper = new DefaultPropertyNameMapper();
this.PropertyValueParser = new DefaultPropertyValueParser();
}

/// <summary>
/// Gets or sets a value indicating whether types should be converted
/// according to conventions described in https://github.com/marcusoftnet/SpecFlow.Assist.Dynamic/wiki/Conversion-conventions#property-type-conversions
/// </summary>
/// <value><c>true</c> if types should be converted; otherwise, <c>false</c>.</value>
public bool DoTypeConversion { get; set; }

public IPropertyNameMapper PropertyNameMapper { get; set; }

public IPropertyValueParser PropertyValueParser { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
namespace SpecFlow.Assist.Dynamic.PropertyNameMapping
{
public class DefaultPropertyNameMapper : IPropertyNameMapper
{
public string Map(string header)
{
var words = header.Split(' ');
var propName = words[0]; // leave the first word as is, since it might be correct cased...

for (var i = 1; i < words.Length; i++)
{
var s = words[i];
if (s.Length > 0)
{
propName += s[0].ToString().ToUpperInvariant() +
s.Substring(1).ToLowerInvariant();
}
}

return propName;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace SpecFlow.Assist.Dynamic.PropertyNameMapping
{
public interface IPropertyNameMapper
{
string Map(string header);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using System;

namespace SpecFlow.Assist.Dynamic.PropertyValueParser
{
public class DefaultPropertyValueParser : IPropertyValueParser
{
public object Parse(string value)
{
int i;
if (int.TryParse(value, out i))
return i;

double db;
if (double.TryParse(value, out db))
{
decimal d;
if (decimal.TryParse(value, out d) && d.Equals((decimal)db))
{
return db;
}
return d;
}

bool b;
if (bool.TryParse(value, out b))
return b;

DateTime dt;
if (DateTime.TryParse(value, out dt))
return dt;

return value;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace SpecFlow.Assist.Dynamic.PropertyValueParser
{
public interface IPropertyValueParser
{
object Parse(string value);
}
}
5 changes: 5 additions & 0 deletions SpecFlow.Assist.Dynamic/SpecFlow.Assist.Dynamic.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,16 @@
<Compile Include="..\SolutionInfo.cs">
<Link>SolutionInfo.cs</Link>
</Compile>
<Compile Include="PropertyValueParser\DefaultPropertyValueParser.cs" />
<Compile Include="PropertyValueParser\IPropertyValueParser.cs" />
<Compile Include="Options.cs" />
<Compile Include="PropertyNameMapping\DefaultPropertyNameMapper.cs" />
<Compile Include="DynamicInstanceComparisonException.cs" />
<Compile Include="DynamicInstanceFromTableException.cs" />
<Compile Include="DynamicSetComparisonException.cs" />
<Compile Include="DynamicStepArgumentTransformations.cs" />
<Compile Include="DynamicTableHelpers.cs" />
<Compile Include="PropertyNameMapping\IPropertyNameMapper.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
Expand Down
12 changes: 12 additions & 0 deletions Specs/DynamicInstancesFromTable.feature
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,15 @@ Scenario: Create dynamic instance from table with Field and Values
And the Age property should equal 39
And the BirthDate property should equal 1972-10-09
And the LengthInMeters property should equal 1.96

Scenario: Create dynamic instance from table using a custom property name mapper
When I create a dynamic instance from this table using the "CustomPropertyNameMapper" property name mapper
| Customer ID |
| 123 |
Then the CustomerID property should equal '123'

Scenario: Create dynamic instance from table using a custom property value parser
When I create a dynamic instance from this table using the "CustomPropertyValueParser" property value parser
| Age |
| -1 |
Then the Age property should equal 42
12 changes: 12 additions & 0 deletions Specs/PropertyNameMapping/CustomPropertyNameMapper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using SpecFlow.Assist.Dynamic.PropertyNameMapping;

namespace Specs.PropertyNameMapping
{
public class CustomPropertyNameMapper : IPropertyNameMapper
{
public string Map(string header)
{
return header.Replace(" ", string.Empty);
}
}
}
12 changes: 12 additions & 0 deletions Specs/PropertyValueParser/CustomPropertyValueParser.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using SpecFlow.Assist.Dynamic.PropertyValueParser;

namespace Specs.PropertyValueParser
{
public class CustomPropertyValueParser : IPropertyValueParser
{
public object Parse(string value)
{
return 42;
}
}
}
2 changes: 2 additions & 0 deletions Specs/Specs.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@
<DesignTime>True</DesignTime>
<DependentUpon>CreateDynamicSetsAndInstancesWithStepArgumentTransformation.feature</DependentUpon>
</Compile>
<Compile Include="PropertyNameMapping\CustomPropertyNameMapper.cs" />
<Compile Include="DynamicInstancesFromTable.feature.cs">
<AutoGen>True</AutoGen>
<DesignTime>True</DesignTime>
Expand All @@ -87,6 +88,7 @@
<DependentUpon>DynamicSetFromTable.feature</DependentUpon>
</Compile>
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="PropertyValueParser\CustomPropertyValueParser.cs" />
<Compile Include="ReservedCharsInCSharp.feature.cs">
<AutoGen>True</AutoGen>
<DesignTime>True</DesignTime>
Expand Down
10 changes: 3 additions & 7 deletions Specs/Steps/DynamicInstanceComparisionSteps.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
using System;
using Should.Fluent;
using Should.Fluent;
using TechTalk.SpecFlow;
using TechTalk.SpecFlow.Assist;
using SpecFlow.Assist.Dynamic;

namespace Specs.Steps
{
[Binding]
public class DynamicInstanceComparisionSteps
{

private const string EXCEPTION_KEY = "ExceptionKey";

private static DynamicInstanceComparisonException GetInstanceComparisonException()
Expand Down Expand Up @@ -37,8 +36,6 @@ public void ComparingAgainstDynamicInstance(Table table)
}
}



[Then("no instance comparison exception should have been thrown")]
public void NoException()
{
Expand Down Expand Up @@ -90,13 +87,12 @@ public void WhenICompareItToThisTableUsingNoTypeConversion(Table table)
{
try
{
table.CompareToDynamicInstance((object)State.OriginalInstance, false);
table.CompareToDynamicInstance((object)State.OriginalInstance, new Options { DoTypeConversion = false });
}
catch (DynamicInstanceComparisonException ex)
{
ScenarioContext.Current.Add(EXCEPTION_KEY, ex);
}
}

}
}
Loading