Skip to content

Commit 2c96e5d

Browse files
author
Marcus Hammarberg
committed
Added comparision to dynamic instance without value conversion
1 parent 8fb4858 commit 2c96e5d

File tree

5 files changed

+72
-13
lines changed

5 files changed

+72
-13
lines changed

SpecFlow.Assist.Dynamic/DynamicTableHelpers.cs

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -65,13 +65,14 @@ public static IEnumerable<dynamic> CreateDynamicSet(this Table table, bool doTyp
6565
/// </summary>
6666
/// <param name="table">the table to compare the instance against</param>
6767
/// <param name="instance">the instance to compare the table against</param>
68-
public static void CompareToDynamicInstance(this Table table, dynamic instance)
68+
/// <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>
69+
public static void CompareToDynamicInstance(this Table table, dynamic instance, bool doTypeConversion = true)
6970
{
7071
IList<string> propDiffs = GetPropertyDifferences(table, instance);
7172
if (propDiffs.Any())
7273
throw new DynamicInstanceComparisonException(propDiffs);
7374

74-
AssertValuesOfRowDifference(table.Rows[0], instance);
75+
AssertValuesOfRowDifference(table.Rows[0], instance, doTypeConversion);
7576
}
7677

7778
/// <summary>
@@ -80,7 +81,8 @@ public static void CompareToDynamicInstance(this Table table, dynamic instance)
8081
/// </summary>
8182
/// <param name="table">the table to compare the set against</param>
8283
/// <param name="set">the set to compare the table against</param>
83-
public static void CompareToDynamicSet(this Table table, IList<dynamic> set)
84+
/// <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>
85+
public static void CompareToDynamicSet(this Table table, IList<dynamic> set, bool doTypeConversion = true)
8486
{
8587
AssertEqualNumberOfRows(table, set);
8688

@@ -92,15 +94,15 @@ public static void CompareToDynamicSet(this Table table, IList<dynamic> set)
9294

9395
// Now we know that the table and the list has the same number of rows and properties
9496

95-
var valueDifference = GetSetValueDifferences(table, set);
97+
var valueDifference = GetSetValueDifferences(table, set, doTypeConversion);
9698

9799
if (valueDifference.Any())
98100
{
99101
throw new DynamicSetComparisonException(ERRORMESS_PROPERTY_DIFF_SET, valueDifference);
100102
}
101103
}
102104

103-
private static List<string> GetSetValueDifferences(Table table, IList<object> set)
105+
private static List<string> GetSetValueDifferences(Table table, IList<object> set, bool doTypeConversion = true)
104106
{
105107
var memberNames = Impromptu.GetMemberNames(set[0]);
106108
var valueDifference = new List<string>();
@@ -110,7 +112,7 @@ private static List<string> GetSetValueDifferences(Table table, IList<object> se
110112
foreach (var memberName in memberNames)
111113
{
112114
var currentHeader = string.Empty;
113-
var rowValue = GetRowValue(i, table, memberName, out currentHeader);
115+
var rowValue = GetRowValue(i, table, memberName, out currentHeader, doTypeConversion);
114116
var instanceValue = Impromptu.InvokeGet(set[i], memberName);
115117

116118
if (!instanceValue.Equals(rowValue))
@@ -129,7 +131,7 @@ private static List<string> GetSetValueDifferences(Table table, IList<object> se
129131
return valueDifference;
130132
}
131133

132-
private static object GetRowValue(int rowIndex, Table table, string memberName, out string currentHeader)
134+
private static object GetRowValue(int rowIndex, Table table, string memberName, out string currentHeader, bool doTypeConversion = true)
133135
{
134136
object rowValue = null;
135137
currentHeader = string.Empty;
@@ -138,21 +140,21 @@ private static object GetRowValue(int rowIndex, Table table, string memberName,
138140
if (CreatePropertyName(header) == memberName)
139141
{
140142
currentHeader = header;
141-
rowValue = CreateTypedValue(table.Rows[rowIndex][header]);
143+
rowValue = CreateTypedValue(table.Rows[rowIndex][header], doTypeConversion);
142144
break;
143145
}
144146
}
145147
return rowValue;
146148
}
147149

148-
private static void AssertValuesOfRowDifference(TableRow tableRow, dynamic instance)
150+
private static void AssertValuesOfRowDifference(TableRow tableRow, dynamic instance, bool doTypeConversion = true)
149151
{
150-
IList<string> valueDiffs = ValidateValuesOfRow(tableRow, instance);
152+
IList<string> valueDiffs = ValidateValuesOfRow(tableRow, instance, doTypeConversion);
151153
if (valueDiffs.Any())
152154
throw new DynamicInstanceComparisonException(valueDiffs);
153155
}
154156

155-
private static IList<string> GetPropertyDifferences(Table table, dynamic instance)
157+
private static IList<string> GetPropertyDifferences(Table table, dynamic instance, bool doTypeConversion = true)
156158
{
157159
var tableHeadersAsPropertyNames = table.Header.Select(CreatePropertyName);
158160
IEnumerable<string> instanceMembers = Impromptu.GetMemberNames(instance);
@@ -169,15 +171,15 @@ private static void AssertEqualNumberOfRows(Table table, IList<object> set)
169171
}
170172
}
171173

172-
private static IList<string> ValidateValuesOfRow(TableRow tableRow, dynamic instance)
174+
private static IList<string> ValidateValuesOfRow(TableRow tableRow, dynamic instance, bool doTypeConversion = true)
173175
{
174176
var valueDiffs = new List<string>();
175177

176178
foreach (var header in tableRow.Keys)
177179
{
178180
var propertyName = CreatePropertyName(header);
179181
var valueFromInstance = Impromptu.InvokeGet(instance, propertyName);
180-
var valueFromTable = CreateTypedValue(tableRow[header]);
182+
var valueFromTable = CreateTypedValue(tableRow[header], doTypeConversion);
181183

182184
if (!valueFromInstance.Equals(valueFromTable))
183185
{

Specs/Steps/DynamicInstanceComparisionSteps.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,5 +84,19 @@ public void ExceptionMessageValueOnProperty(string expectedPropertyName)
8484
{
8585
CheckForOneDifferenceContaingString(expectedPropertyName);
8686
}
87+
88+
[When(@"I compare it to this table using no type conversion")]
89+
public void WhenICompareItToThisTableUsingNoTypeConversion(Table table)
90+
{
91+
try
92+
{
93+
table.CompareToDynamicInstance((object)State.OriginalInstance, false);
94+
}
95+
catch (DynamicInstanceComparisonException ex)
96+
{
97+
ScenarioContext.Current.Add(EXCEPTION_KEY, ex);
98+
}
99+
}
100+
87101
}
88102
}

Specs/Steps/DynamicInstanceCreationSteps.cs

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

132+
[Given(@"I create a dynamic instance from this table using no type conversion")]
132133
[When(@"I create a dynamic instance from this table using no type conversion")]
133134
public void WhenICreateADynamicInstanceFromThisTableUsingNoTypeConversion(Table table)
134135
{

Specs/ValueConversions.feature

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,3 +64,12 @@ Scenario: There's ways to disable type conversion for set creation
6464
And the 1 item should still Name equal '012345'
6565
And the 1 item should still Age equal '044'
6666

67+
Scenario: There's ways to disable type conversion for matching a dynamic instance against a table
68+
Given I create a dynamic instance from this table using no type conversion
69+
| Name | Age |
70+
| 012345 | 039 |
71+
When I compare it to this table using no type conversion
72+
| Name | Age |
73+
| 012345 | 039 |
74+
Then no instance comparison exception should have been thrown
75+

Specs/ValueConversions.feature.cs

Lines changed: 33 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)