Skip to content

Commit 8fb4858

Browse files
author
Marcus Hammarberg
committed
Added overloaded methods for CreateInstance and CreateSet
1 parent e64e7b6 commit 8fb4858

File tree

5 files changed

+144
-8
lines changed

5 files changed

+144
-8
lines changed

SpecFlow.Assist.Dynamic/DynamicTableHelpers.cs

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,9 @@ public static class DynamicTableHelpers
2828
/// Create a dynamic object from the headers and values of the <paramref name="table"/>
2929
/// </summary>
3030
/// <param name="table">the table to create a dynamic object from</param>
31+
/// <param name="doTypeConversion">should types be converted according to conventions described in https://github.com/marcusoftnet/SpecFlow.Assist.Dynamic/wiki/Conversion-conventions#property-type-conversions</param>
3132
/// <returns>the created object</returns>
32-
public static ExpandoObject CreateDynamicInstance(this Table table)
33+
public static ExpandoObject CreateDynamicInstance(this Table table, bool doTypeConversion = true)
3334
{
3435
if (table.Header.Count == 2 && table.RowCount > 1)
3536
{
@@ -39,20 +40,23 @@ public static ExpandoObject CreateDynamicInstance(this Table table)
3940

4041
if (table.RowCount == 1)
4142
{
42-
return CreateDynamicInstance(table.Rows[0]);
43+
return CreateDynamicInstance(table.Rows[0], doTypeConversion);
4344
}
4445

4546
throw new DynamicInstanceFromTableException(ERRORMESS_INSTANCETABLE_FORMAT);
4647
}
48+
4749

4850
/// <summary>
4951
/// Creates a set of dynamic objects based of the <paramref name="table"/> headers and values
5052
/// </summary>
5153
/// <param name="table">the table to create a set of dynamics from</param>
54+
/// <param name="doTypeConversion">should types be converted according to conventions described in https://github.com/marcusoftnet/SpecFlow.Assist.Dynamic/wiki/Conversion-conventions#property-type-conversions</param>
5255
/// <returns>a set of dynamics</returns>
53-
public static IEnumerable<dynamic> CreateDynamicSet(this Table table)
56+
public static IEnumerable<dynamic> CreateDynamicSet(this Table table, bool doTypeConversion = true)
5457
{
55-
return table.Rows.Select(CreateDynamicInstance);
58+
return from r in table.Rows
59+
select CreateDynamicInstance(r, doTypeConversion);
5660
}
5761

5862
/// <summary>
@@ -212,24 +216,26 @@ private static Table CreateHorizontalTable(Table verticalFieldValueTable)
212216
return horizontalTable;
213217
}
214218

215-
private static ExpandoObject CreateDynamicInstance(TableRow tablerow)
219+
private static ExpandoObject CreateDynamicInstance(TableRow tablerow, bool doTypeConversion = true)
216220
{
217221
dynamic expando = new ExpandoObject();
218222
var dicExpando = expando as IDictionary<string, object>;
219223

220224
foreach (var header in tablerow.Keys)
221225
{
222226
var propName = CreatePropertyName(header);
223-
var propValue = CreateTypedValue(tablerow[header]);
227+
var propValue = CreateTypedValue(tablerow[header], doTypeConversion);
224228
dicExpando.Add(propName, propValue);
225229
}
226230

227231
return expando;
228232
}
229233

230-
private static object CreateTypedValue(string valueFromTable)
234+
private static object CreateTypedValue(string valueFromTable, bool doTypeConversion = true)
231235
{
232-
// TODO: More types here?
236+
if (!doTypeConversion)
237+
return valueFromTable;
238+
233239
int i;
234240
if (int.TryParse(valueFromTable, out i))
235241
return i;

Specs/Steps/DynamicInstanceCreationSteps.cs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,36 @@ public void ThenAnExceptionWithANiceErrorMessageAboutThePropertyOnlyContainingRe
129129
ex.Message.Should().Contain("only contains");
130130
}
131131

132+
[When(@"I create a dynamic instance from this table using no type conversion")]
133+
public void WhenICreateADynamicInstanceFromThisTableUsingNoTypeConversion(Table table)
134+
{
135+
State.OriginalInstance = table.CreateDynamicInstance(false);
136+
}
137+
138+
[Then(@"the Name value should still be '(.*)'")]
139+
public void ThenTheNameValueShouldStillBe(string expectedValue)
140+
{
141+
((string)State.OriginalInstance.Name).Should().Equal(expectedValue);
142+
}
143+
144+
[Then(@"the Age value should still be '(.*)'")]
145+
public void ThenTheAgeValueShouldStillBe(string expectedValue)
146+
{
147+
((string)State.OriginalInstance.Age).Should().Equal(expectedValue);
148+
}
149+
150+
[Then(@"the birth date should stil be '(.*)'")]
151+
public void ThenTheBirthDateShouldStilBe(string expectedValue)
152+
{
153+
((string)State.OriginalInstance.BirthDate).Should().Equal(expectedValue);
154+
155+
}
156+
157+
[Then(@"length in meter should still be '(.*)'")]
158+
public void ThenLengthInMeterShouldStillBe(string expectedValue)
159+
{
160+
((string)State.OriginalInstance.LengthInMeters).Should().Equal(expectedValue);
161+
}
132162

133163
}
134164
}

Specs/Steps/DynamicSetCreationSteps.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,19 @@ public void ItemInSetShouldHaveExpectedAge(int itemNumber, int expectedAge)
4343
Assert.AreEqual(expectedAge, GetItem(itemNumber).Age);
4444
}
4545

46+
[Then(@"the (.*) item should still Name equal '(.*)'")]
47+
public void ThenTheItemShouldStillNameEqual(int itemNumber, string expectedName)
48+
{
49+
Assert.AreEqual(expectedName, GetItem(itemNumber).Name);
50+
}
51+
52+
[Then(@"the (.*) item should still Age equal '(.*)'")]
53+
public void ThenTheItemShouldStillAgeEqual(int itemNumber, string expectedAge)
54+
{
55+
Assert.AreEqual(expectedAge, GetItem(itemNumber).Age);
56+
}
57+
58+
4659
[Then(@"the (\d+) item should have Name equal to '(.*)'")]
4760
public void ItemInSetShouldHaveExpectedName(int itemNumber, string expectedName)
4861
{
@@ -54,5 +67,12 @@ public void ItemInSetShouldHaveExpectedLenghtInMeters(int itemNumber, double exp
5467
{
5568
Assert.AreEqual(expectedLengthInMetersItem, GetItem(itemNumber).LengthInMeters);
5669
}
70+
71+
[When(@"I create a set of dynamic instances from this table using no type conversion")]
72+
public void WhenICreateASetOfDynamicInstancesFromThisTableUsingNoTypeConversion(Table table)
73+
{
74+
State.OriginalSet = table.CreateDynamicSet(false).ToList();
75+
}
76+
5777
}
5878
}

Specs/ValueConversions.feature

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,3 +45,22 @@ Scenario: A strange double should not be translated into a date
4545
| Length in meters |
4646
| 4.567 |
4747
Then the LengthInMeters property should equal '4.567'
48+
49+
Scenario: There's ways to disable type conversion for instance creation
50+
When I create a dynamic instance from this table using no type conversion
51+
| Name | Age | Birth date | Length in meters |
52+
| 012345 | 044 | 1972-13-09 | 1,96 |
53+
Then the Name value should still be '012345'
54+
And the Age value should still be '044'
55+
And the birth date should stil be '1972-13-09'
56+
And length in meter should still be '1,96'
57+
58+
Scenario: There's ways to disable type conversion for set creation
59+
When I create a set of dynamic instances from this table using no type conversion
60+
| Name | Age |
61+
| 012345 | 044 |
62+
| Arvid | 1 |
63+
Then I should have a list of 2 dynamic objects
64+
And the 1 item should still Name equal '012345'
65+
And the 1 item should still Age equal '044'
66+

Specs/ValueConversions.feature.cs

Lines changed: 61 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)