From 2f4408a4f7e97300d1c7dfe7914aa22acd74e4ed Mon Sep 17 00:00:00 2001 From: icnocop Date: Thu, 1 Nov 2018 17:02:06 -0700 Subject: [PATCH] Added the ability to use a custom property name mapper and property value parser Fixes #19 and #5 --- .../DynamicTableHelpers.cs | 188 ++++++++---------- SpecFlow.Assist.Dynamic/Options.cs | 26 +++ .../DefaultPropertyNameMapper.cs | 23 +++ .../IPropertyNameMapper.cs | 7 + .../DefaultPropertyValueParser.cs | 35 ++++ .../IPropertyValueParser.cs | 7 + .../SpecFlow.Assist.Dynamic.csproj | 5 + Specs/DynamicInstancesFromTable.feature | 12 ++ .../CustomPropertyNameMapper.cs | 12 ++ .../CustomPropertyValueParser.cs | 12 ++ Specs/Specs.csproj | 2 + .../Steps/DynamicInstanceComparisionSteps.cs | 10 +- Specs/Steps/DynamicInstanceCreationSteps.cs | 73 +++++-- Specs/Steps/DynamicSetComparisonSteps.cs | 10 +- Specs/Steps/DynamicSetCreationSteps.cs | 12 +- 15 files changed, 294 insertions(+), 140 deletions(-) create mode 100644 SpecFlow.Assist.Dynamic/Options.cs create mode 100644 SpecFlow.Assist.Dynamic/PropertyNameMapping/DefaultPropertyNameMapper.cs create mode 100644 SpecFlow.Assist.Dynamic/PropertyNameMapping/IPropertyNameMapper.cs create mode 100644 SpecFlow.Assist.Dynamic/PropertyValueParser/DefaultPropertyValueParser.cs create mode 100644 SpecFlow.Assist.Dynamic/PropertyValueParser/IPropertyValueParser.cs create mode 100644 Specs/PropertyNameMapping/CustomPropertyNameMapper.cs create mode 100644 Specs/PropertyValueParser/CustomPropertyValueParser.cs diff --git a/SpecFlow.Assist.Dynamic/DynamicTableHelpers.cs b/SpecFlow.Assist.Dynamic/DynamicTableHelpers.cs index 2ffca45..fa4f836 100644 --- a/SpecFlow.Assist.Dynamic/DynamicTableHelpers.cs +++ b/SpecFlow.Assist.Dynamic/DynamicTableHelpers.cs @@ -4,7 +4,7 @@ using System.Linq; using System.Text.RegularExpressions; using ImpromptuInterface; -using ImpromptuInterface.InvokeExt; +using SpecFlow.Assist.Dynamic; namespace TechTalk.SpecFlow.Assist { @@ -24,68 +24,87 @@ public static class DynamicTableHelpers "A difference was found on row '{0}' for column '{1}' (property '{2}').\n\tInstance:\t'{3}'(type: {4}).\n\tTable:\t\t'{5}'(type: {6})"; /// - /// Create a dynamic object from the headers and values of the + /// Create a dynamic object from the headers and values of the /// /// the table to create a dynamic object from - /// should types be converted according to conventions described in https://github.com/marcusoftnet/SpecFlow.Assist.Dynamic/wiki/Conversion-conventions#property-type-conversions + /// The options. /// the created object - public static ExpandoObject CreateDynamicInstance(this Table table, bool doTypeConversion = true) + public static ExpandoObject CreateDynamicInstance(this Table table, Options options = null) { + if (options == null) + { + options = new Options(); + } + if (table.Header.Count == 2 && table.RowCount > 1) { var horizontalTable = CreateHorizontalTable(table); - return CreateDynamicInstance(horizontalTable.Rows[0], doTypeConversion); + return CreateDynamicInstance(horizontalTable.Rows[0], options); } if (table.RowCount == 1) { - return CreateDynamicInstance(table.Rows[0], doTypeConversion); + return CreateDynamicInstance(table.Rows[0], options); } throw new DynamicInstanceFromTableException(ERRORMESS_INSTANCETABLE_FORMAT); } - /// - /// Creates a set of dynamic objects based of the headers and values + /// Creates a set of dynamic objects based of the headers and values /// /// the table to create a set of dynamics from - /// should types be converted according to conventions described in https://github.com/marcusoftnet/SpecFlow.Assist.Dynamic/wiki/Conversion-conventions#property-type-conversions + /// The options. /// a set of dynamics - public static IEnumerable CreateDynamicSet(this Table table, bool doTypeConversion = true) + public static IEnumerable CreateDynamicSet(this Table table, Options options = null) { + if (options == null) + { + options = new Options(); + } + return from r in table.Rows - select CreateDynamicInstance(r, doTypeConversion); + select CreateDynamicInstance(r, options); } /// - /// Validates if a dynamic instance matches the + /// Validates if a dynamic instance matches the /// Throws descriptive exception if not /// /// the table to compare the instance against /// the instance to compare the table against - /// should types be converted according to conventions described in https://github.com/marcusoftnet/SpecFlow.Assist.Dynamic/wiki/Conversion-conventions#property-type-conversions - public static void CompareToDynamicInstance(this Table table, dynamic instance, bool doTypeConversion = true) + /// The options. + public static void CompareToDynamicInstance(this Table table, dynamic instance, Options options = null) { - IList propDiffs = GetPropertyDifferences(table, instance); + if (options == null) + { + options = new Options(); + } + + IList propDiffs = GetPropertyDifferences(table, instance, options); if (propDiffs.Any()) throw new DynamicInstanceComparisonException(propDiffs); - AssertValuesOfRowDifference(table.Rows[0], instance, doTypeConversion); + AssertValuesOfRowDifference(table.Rows[0], instance, options); } /// - /// Validates that the dynamic set matches the + /// Validates that the dynamic set matches the /// Throws descriptive exception if not /// /// the table to compare the set against /// the set to compare the table against - /// should types be converted according to conventions described in https://github.com/marcusoftnet/SpecFlow.Assist.Dynamic/wiki/Conversion-conventions#property-type-conversions - public static void CompareToDynamicSet(this Table table, IList set, bool doTypeConversion = true) + /// The options. + public static void CompareToDynamicSet(this Table table, IList set, Options options = null) { + if (options == null) + { + options = new Options(); + } + AssertEqualNumberOfRows(table, set); - IList propDiffs = GetPropertyDifferences(table, set[0]); + IList propDiffs = GetPropertyDifferences(table, set[0], options); if (propDiffs.Any()) { throw new DynamicSetComparisonException(ERRORMESS_PROPERTY_DIFF_SET, propDiffs); @@ -93,7 +112,7 @@ public static void CompareToDynamicSet(this Table table, IList set, boo // Now we know that the table and the list has the same number of rows and properties - var valueDifference = GetSetValueDifferences(table, set, doTypeConversion); + var valueDifference = GetSetValueDifferences(table, set, options); if (valueDifference.Any()) { @@ -101,7 +120,7 @@ public static void CompareToDynamicSet(this Table table, IList set, boo } } - private static List GetSetValueDifferences(Table table, IList set, bool doTypeConversion = true) + private static List GetSetValueDifferences(Table table, IList set, Options options) { var memberNames = Impromptu.GetMemberNames(set[0]); var valueDifference = new List(); @@ -110,56 +129,64 @@ private static List GetSetValueDifferences(Table table, IList se { foreach (var memberName in memberNames) { - var currentHeader = string.Empty; - var rowValue = GetRowValue(i, table, memberName, out currentHeader, doTypeConversion); - var rowType = rowValue.GetType().Name; - var instanceValue = Impromptu.InvokeGet(set[i], memberName); - var instanceType = instanceValue.GetType().Name; - - if (!instanceValue.Equals(rowValue)) - { - var difference = string.Format(ERRORMESS_SET_VALUES_DIFFERS, - i + 1, - currentHeader, - memberName, - instanceValue, - instanceType, - rowValue, - rowType); - - valueDifference.Add(difference); - } + var currentHeader = string.Empty; + var rowValue = GetRowValue(i, table, memberName, out currentHeader, options); + + if ((rowValue.GetType() == typeof(string)) && ((string)rowValue == "NULL")) + { + rowValue = null; + } + + var instanceValue = Impromptu.InvokeGet(set[i], memberName); + + if (((instanceValue != null) || (rowValue != null)) + && !instanceValue.Equals((dynamic)rowValue)) + { + var rowType = rowValue.GetType().Name; + var instanceType = instanceValue.GetType().Name; + + var difference = string.Format(ERRORMESS_SET_VALUES_DIFFERS, + i + 1, + currentHeader, + memberName, + instanceValue, + instanceType, + rowValue, + rowType); + + valueDifference.Add(difference); + } } } return valueDifference; } - private static object GetRowValue(int rowIndex, Table table, string memberName, out string currentHeader, bool doTypeConversion = true) + private static object GetRowValue(int rowIndex, Table table, string memberName, out string currentHeader, Options options) { object rowValue = null; currentHeader = string.Empty; foreach (var header in table.Header) { - if (CreatePropertyName(header) == memberName) + if (CreatePropertyName(header, options) == memberName) { currentHeader = header; - rowValue = CreateTypedValue(table.Rows[rowIndex][header], doTypeConversion); + rowValue = CreateTypedValue(table.Rows[rowIndex][header], options); break; } } return rowValue; } - private static void AssertValuesOfRowDifference(TableRow tableRow, dynamic instance, bool doTypeConversion = true) + private static void AssertValuesOfRowDifference(TableRow tableRow, dynamic instance, Options options) { - IList valueDiffs = ValidateValuesOfRow(tableRow, instance, doTypeConversion); + IList valueDiffs = ValidateValuesOfRow(tableRow, instance, options); if (valueDiffs.Any()) throw new DynamicInstanceComparisonException(valueDiffs); } - private static IList GetPropertyDifferences(Table table, dynamic instance, bool doTypeConversion = true) + private static IList GetPropertyDifferences(Table table, dynamic instance, Options options) { - var tableHeadersAsPropertyNames = table.Header.Select(CreatePropertyName); + var tableHeadersAsPropertyNames = table.Header.Select(x => CreatePropertyName(x, options)); IEnumerable instanceMembers = Impromptu.GetMemberNames(instance); return GetPropertyNameDifferences(tableHeadersAsPropertyNames, instanceMembers); @@ -174,16 +201,16 @@ private static void AssertEqualNumberOfRows(Table table, IList set) } } - private static IList ValidateValuesOfRow(TableRow tableRow, dynamic instance, bool doTypeConversion = true) + private static IList ValidateValuesOfRow(TableRow tableRow, dynamic instance, Options options) { var valueDiffs = new List(); foreach (var header in tableRow.Keys) { - var propertyName = CreatePropertyName(header); + var propertyName = CreatePropertyName(header, options); var valueFromInstance = Impromptu.InvokeGet(instance, propertyName); var typeFromInstance = valueFromInstance.GetType().Name; - var valueFromTable = CreateTypedValue(tableRow[header], doTypeConversion); + var valueFromTable = CreateTypedValue(tableRow[header], options); var typeFromTable = valueFromTable.GetType().Name; if (!valueFromInstance.Equals(valueFromTable)) @@ -223,82 +250,41 @@ private static Table CreateHorizontalTable(Table verticalFieldValueTable) return horizontalTable; } - private static ExpandoObject CreateDynamicInstance(TableRow tablerow, bool doTypeConversion = true) + private static ExpandoObject CreateDynamicInstance(TableRow tablerow, Options options) { dynamic expando = new ExpandoObject(); var dicExpando = expando as IDictionary; foreach (var header in tablerow.Keys) { - var propName = CreatePropertyName(header); - var propValue = CreateTypedValue(tablerow[header], doTypeConversion); + var propName = CreatePropertyName(header, options); + var propValue = CreateTypedValue(tablerow[header], options); dicExpando.Add(propName, propValue); } return expando; } - private static object CreateTypedValue(string valueFromTable, bool doTypeConversion = true) + private static object CreateTypedValue(string valueFromTable, Options options) { - if (!doTypeConversion) + if (!options.DoTypeConversion) return valueFromTable; - int i; - if (int.TryParse(valueFromTable, out i)) - return i; - - double db; - if (Double.TryParse(valueFromTable, out db)) - { - decimal d; - if (Decimal.TryParse(valueFromTable, out d) && d.Equals((decimal)db)) - { - return db; - } - return d; - } - - bool b; - if (Boolean.TryParse(valueFromTable, out b)) - return b; - - DateTime dt; - if (DateTime.TryParse(valueFromTable, out dt)) - return dt; - - return valueFromTable; + return options.PropertyValueParser.Parse(valueFromTable); } - private static string CreatePropertyName(string header) + private static string CreatePropertyName(string header, Options options) { var cleanedHeader = RemoveReservedChars(header); - var propName = FixCasing(cleanedHeader); + var propName = options.PropertyNameMapper.Map(cleanedHeader); // Throw if no chars in string if (propName.Length != 0) return propName; - + var mess = string.Format("Property '{0}' only contains reserved C# characters", header); throw new DynamicInstanceFromTableException(mess); } - private static string FixCasing(string header) - { - var arr = header.Split(' '); - var propName = arr[0]; // leave the first element as is, since it might be correct cased... - - for (var i = 1; i < arr.Length; i++) - { - var s = arr[i]; - if (s.Length > 0) - { - propName += s[0].ToString().ToUpperInvariant() + - s.Substring(1).ToLowerInvariant(); - } - } - - return propName; - } - private static string RemoveReservedChars(string orgPropertyName) { const string pattern = @"[^\w\s]"; diff --git a/SpecFlow.Assist.Dynamic/Options.cs b/SpecFlow.Assist.Dynamic/Options.cs new file mode 100644 index 0000000..76b8b8a --- /dev/null +++ b/SpecFlow.Assist.Dynamic/Options.cs @@ -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(); + } + + /// + /// 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 + /// + /// true if types should be converted; otherwise, false. + public bool DoTypeConversion { get; set; } + + public IPropertyNameMapper PropertyNameMapper { get; set; } + + public IPropertyValueParser PropertyValueParser { get; set; } + } +} diff --git a/SpecFlow.Assist.Dynamic/PropertyNameMapping/DefaultPropertyNameMapper.cs b/SpecFlow.Assist.Dynamic/PropertyNameMapping/DefaultPropertyNameMapper.cs new file mode 100644 index 0000000..7af1590 --- /dev/null +++ b/SpecFlow.Assist.Dynamic/PropertyNameMapping/DefaultPropertyNameMapper.cs @@ -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; + } + } +} diff --git a/SpecFlow.Assist.Dynamic/PropertyNameMapping/IPropertyNameMapper.cs b/SpecFlow.Assist.Dynamic/PropertyNameMapping/IPropertyNameMapper.cs new file mode 100644 index 0000000..4b0b09e --- /dev/null +++ b/SpecFlow.Assist.Dynamic/PropertyNameMapping/IPropertyNameMapper.cs @@ -0,0 +1,7 @@ +namespace SpecFlow.Assist.Dynamic.PropertyNameMapping +{ + public interface IPropertyNameMapper + { + string Map(string header); + } +} \ No newline at end of file diff --git a/SpecFlow.Assist.Dynamic/PropertyValueParser/DefaultPropertyValueParser.cs b/SpecFlow.Assist.Dynamic/PropertyValueParser/DefaultPropertyValueParser.cs new file mode 100644 index 0000000..4b96d91 --- /dev/null +++ b/SpecFlow.Assist.Dynamic/PropertyValueParser/DefaultPropertyValueParser.cs @@ -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; + } + } +} diff --git a/SpecFlow.Assist.Dynamic/PropertyValueParser/IPropertyValueParser.cs b/SpecFlow.Assist.Dynamic/PropertyValueParser/IPropertyValueParser.cs new file mode 100644 index 0000000..4782d8c --- /dev/null +++ b/SpecFlow.Assist.Dynamic/PropertyValueParser/IPropertyValueParser.cs @@ -0,0 +1,7 @@ +namespace SpecFlow.Assist.Dynamic.PropertyValueParser +{ + public interface IPropertyValueParser + { + object Parse(string value); + } +} \ No newline at end of file diff --git a/SpecFlow.Assist.Dynamic/SpecFlow.Assist.Dynamic.csproj b/SpecFlow.Assist.Dynamic/SpecFlow.Assist.Dynamic.csproj index 0d23d06..a43f10c 100644 --- a/SpecFlow.Assist.Dynamic/SpecFlow.Assist.Dynamic.csproj +++ b/SpecFlow.Assist.Dynamic/SpecFlow.Assist.Dynamic.csproj @@ -52,11 +52,16 @@ SolutionInfo.cs + + + + + diff --git a/Specs/DynamicInstancesFromTable.feature b/Specs/DynamicInstancesFromTable.feature index c25951b..76da746 100644 --- a/Specs/DynamicInstancesFromTable.feature +++ b/Specs/DynamicInstancesFromTable.feature @@ -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 \ No newline at end of file diff --git a/Specs/PropertyNameMapping/CustomPropertyNameMapper.cs b/Specs/PropertyNameMapping/CustomPropertyNameMapper.cs new file mode 100644 index 0000000..2f89af9 --- /dev/null +++ b/Specs/PropertyNameMapping/CustomPropertyNameMapper.cs @@ -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); + } + } +} diff --git a/Specs/PropertyValueParser/CustomPropertyValueParser.cs b/Specs/PropertyValueParser/CustomPropertyValueParser.cs new file mode 100644 index 0000000..26350c9 --- /dev/null +++ b/Specs/PropertyValueParser/CustomPropertyValueParser.cs @@ -0,0 +1,12 @@ +using SpecFlow.Assist.Dynamic.PropertyValueParser; + +namespace Specs.PropertyValueParser +{ + public class CustomPropertyValueParser : IPropertyValueParser + { + public object Parse(string value) + { + return 42; + } + } +} diff --git a/Specs/Specs.csproj b/Specs/Specs.csproj index 0f92e37..50fd80f 100644 --- a/Specs/Specs.csproj +++ b/Specs/Specs.csproj @@ -76,6 +76,7 @@ True CreateDynamicSetsAndInstancesWithStepArgumentTransformation.feature + True True @@ -87,6 +88,7 @@ DynamicSetFromTable.feature + True True diff --git a/Specs/Steps/DynamicInstanceComparisionSteps.cs b/Specs/Steps/DynamicInstanceComparisionSteps.cs index 0f89da9..1c7fb11 100644 --- a/Specs/Steps/DynamicInstanceComparisionSteps.cs +++ b/Specs/Steps/DynamicInstanceComparisionSteps.cs @@ -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() @@ -37,8 +36,6 @@ public void ComparingAgainstDynamicInstance(Table table) } } - - [Then("no instance comparison exception should have been thrown")] public void NoException() { @@ -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); } } - } } diff --git a/Specs/Steps/DynamicInstanceCreationSteps.cs b/Specs/Steps/DynamicInstanceCreationSteps.cs index ae47d5f..08a60af 100644 --- a/Specs/Steps/DynamicInstanceCreationSteps.cs +++ b/Specs/Steps/DynamicInstanceCreationSteps.cs @@ -1,15 +1,19 @@ using System; -using System.Collections.Generic; using Should.Fluent; using TechTalk.SpecFlow; using TechTalk.SpecFlow.Assist; +using SpecFlow.Assist.Dynamic; +using SpecFlow.Assist.Dynamic.PropertyNameMapping; +using SpecFlow.Assist.Dynamic.PropertyValueParser; +using System.Reflection; +using System.Linq; +using NUnit.Framework; namespace Specs.Steps { [Binding] public class DynamicInstanceCreationSteps { - [Given(@"I create a dynamic instance from this table")] [When(@"I create a dynamic instance from this table")] public void CreateDynamicInstanceFromTable(Table table) @@ -17,6 +21,44 @@ public void CreateDynamicInstanceFromTable(Table table) State.OriginalInstance = table.CreateDynamicInstance(); } + [When(@"I create a dynamic instance from this table using the ""(.*)"" property name mapper")] + public void WhenICreateADynamicInstanceFromThisTableUsingThePropertyNameMapper(string propertyNameMapper, Table table) + { + Type customPropertyNameMapperType = Assembly.GetExecutingAssembly().GetTypes() + .SingleOrDefault(x => x.GetInterfaces().Contains(typeof(IPropertyNameMapper)) + && x.Name.EndsWith(propertyNameMapper)); + + Assert.IsNotNull(customPropertyNameMapperType); + + IPropertyNameMapper customPropertyNameMapper = Activator.CreateInstance(customPropertyNameMapperType) as IPropertyNameMapper; + + Options options = new Options + { + PropertyNameMapper = customPropertyNameMapper + }; + + State.OriginalInstance = table.CreateDynamicInstance(options); + } + + [When(@"I create a dynamic instance from this table using the ""(.*)"" property value parser")] + public void WhenICreateADynamicInstanceFromThisTableUsingThePropertyValueParser(string propertyValueParser, Table table) + { + Type customPropertyValueParserType = Assembly.GetExecutingAssembly().GetTypes() + .SingleOrDefault(x => x.GetInterfaces().Contains(typeof(IPropertyValueParser)) + && x.Name.EndsWith(propertyValueParser)); + + Assert.IsNotNull(customPropertyValueParserType); + + IPropertyValueParser customPropertyNameMapper = Activator.CreateInstance(customPropertyValueParserType) as IPropertyValueParser; + + Options options = new Options + { + PropertyValueParser = customPropertyNameMapper + }; + + State.OriginalInstance = table.CreateDynamicInstance(options); + } + [Then(@"the Name property should equal '(.*)'")] public void NameShouldBe(string expectedValue) { @@ -32,7 +74,7 @@ public void AgeShouldBe(int expectedAge) [Then(@"the age property should equal (\d+)")] public void LowerCaseAgeShouldBe(int expectedAge) { - ((int) State.OriginalInstance.age).Should().Equal(expectedAge); + ((int)State.OriginalInstance.age).Should().Equal(expectedAge); } [Then(@"the BirthDate property should equal (.*)")] @@ -41,36 +83,35 @@ public void BirthDateShouldBe(string expectedDate) ((DateTime)State.OriginalInstance.BirthDate).Should().Equal(DateTime.Parse(expectedDate)); } - [Then] public void ThenTheLengthInMetersPropertyShouldEqual_P0(double expectedLenghtInMeters) { CheckLengthInMeters(expectedLenghtInMeters); - } - [Then(@"the LengthInMeters property should equal '(\d+\.\d+)'")] public void LengthInMeterShouldBe(double expectedLenghtInMeters) { CheckLengthInMeters(expectedLenghtInMeters); } - [Then(@"the MolecularWeight property should equal '(\d+\.\d+)'")] public void MolecularWeightShouldBe(decimal expectedMolecularWeight) { CheckMolecularWeight(expectedMolecularWeight); } - + [Then(@"the CustomerID property should equal '(.*)'")] + public void ThenTheCustomerIDPropertyShouldEqual(int expectedValue) + { + ((int)State.OriginalInstance.CustomerID).Should().Equal(expectedValue); + } private static void CheckLengthInMeters(double expectedLenghtInMeters) { - ((double) State.OriginalInstance.LengthInMeters).Should().Equal(expectedLenghtInMeters); + ((double)State.OriginalInstance.LengthInMeters).Should().Equal(expectedLenghtInMeters); } - private static void CheckMolecularWeight(decimal expectedMolecularWeight) { ((decimal)State.OriginalInstance.MolecularWeight).Should().Equal(expectedMolecularWeight); @@ -79,27 +120,25 @@ private static void CheckMolecularWeight(decimal expectedMolecularWeight) [Then(@"the SATScore should be (\d+)")] public void SATTest(int expectedScore) { - ((int) State.OriginalInstance.SATScore).Should().Equal(expectedScore); + ((int)State.OriginalInstance.SATScore).Should().Equal(expectedScore); } [Then(@"the IsDeveloper property should equal '(.*)'")] public void ThenTheIsDeveloperPropertyShouldEqualTrueAndBeOfTypeBool(bool expectedValue) { - ((bool) State.OriginalInstance.IsDeveloper).Should().Equal(expectedValue); + ((bool)State.OriginalInstance.IsDeveloper).Should().Equal(expectedValue); } [Then(@"the CharpNmeWithStrangeChars property should equal '(.*)'")] public void ThenTheCharpNmeWithStrangeCharsPropertyShouldEqual(string expectedValue) { ((string)State.OriginalInstance.CharpNmeWithStrangeChars).Should().Equal(expectedValue); - } [Then(@"the My_Nice_Variable property should equal '(.*)'")] public void ThenTheMy_Nice_VariablePropertyShouldEqual(string expectedValue) { ((string)State.OriginalInstance.My_Nice_Variable).Should().Equal(expectedValue); - } [Then(@"the MyVariableNeedsCleanUp property should equal '(.*)'")] @@ -113,7 +152,7 @@ public void OnlyReservedChars(Table table) { try { - State.OriginalInstance = table.CreateDynamicInstance(); + State.OriginalInstance = table.CreateDynamicInstance(); } catch (DynamicInstanceFromTableException ex) { @@ -133,7 +172,7 @@ public void ThenAnExceptionWithANiceErrorMessageAboutThePropertyOnlyContainingRe [When(@"I create a dynamic instance from this table using no type conversion")] public void WhenICreateADynamicInstanceFromThisTableUsingNoTypeConversion(Table table) { - State.OriginalInstance = table.CreateDynamicInstance(false); + State.OriginalInstance = table.CreateDynamicInstance(new Options { DoTypeConversion = false }); } [Then(@"the Name value should still be '(.*)'")] @@ -152,7 +191,6 @@ public void ThenTheAgeValueShouldStillBe(string expectedValue) public void ThenTheBirthDateShouldStilBe(string expectedValue) { ((string)State.OriginalInstance.BirthDate).Should().Equal(expectedValue); - } [Then(@"length in meter should still be '(.*)'")] @@ -160,6 +198,5 @@ public void ThenLengthInMeterShouldStillBe(string expectedValue) { ((string)State.OriginalInstance.LengthInMeters).Should().Equal(expectedValue); } - } } diff --git a/Specs/Steps/DynamicSetComparisonSteps.cs b/Specs/Steps/DynamicSetComparisonSteps.cs index 3f85313..03ea80f 100644 --- a/Specs/Steps/DynamicSetComparisonSteps.cs +++ b/Specs/Steps/DynamicSetComparisonSteps.cs @@ -1,4 +1,5 @@ using Should.Fluent; +using SpecFlow.Assist.Dynamic; using TechTalk.SpecFlow; using TechTalk.SpecFlow.Assist; @@ -27,7 +28,7 @@ public void CompareSetToInstance(Table table) { table.CompareToDynamicSet(State.OriginalSet); } - catch (DynamicSetComparisonException ex) + catch (DynamicSetComparisonException ex) { ScenarioContext.Current.Add(EXCEPTION_KEY, ex); } @@ -38,7 +39,7 @@ public void CompareSetToInstanceNoConversion(Table table) { try { - table.CompareToDynamicSet(State.OriginalSet, false); + table.CompareToDynamicSet(State.OriginalSet, new Options { DoTypeConversion = false }); } catch (DynamicSetComparisonException ex) { @@ -66,7 +67,6 @@ public void SetComparisionExceptionWithNumberOfDifferences(int expectedNumberOfD GetSetComparisonException().Differences.Count.Should().Equal(expectedNumberOfDifference); } - [Then(@"the error message for different rows should expect (.*) for table and (.*) for instance")] public void ShouldDifferInRowCount(string tableRowCountString, string instanceRowCountString) { @@ -91,13 +91,11 @@ public void DifferenceOnFieldOfInstance(string expectedFieldToDiffer) public void DifferenceOnValue(int differenceNumber, int rowNumber, string expectedProperty, string instanceValue, string tableRowValue) { var exception = GetSetComparisonException(); - var difference = exception.Differences[differenceNumber -1]; + var difference = exception.Differences[differenceNumber - 1]; difference.Contains("'" + rowNumber + "'"); difference.Contains("'" + expectedProperty + "'"); difference.Contains("'" + instanceValue + "'"); difference.Contains("'" + tableRowValue + "'"); } - } - } diff --git a/Specs/Steps/DynamicSetCreationSteps.cs b/Specs/Steps/DynamicSetCreationSteps.cs index d9a067c..3431168 100644 --- a/Specs/Steps/DynamicSetCreationSteps.cs +++ b/Specs/Steps/DynamicSetCreationSteps.cs @@ -1,10 +1,10 @@ using System; -using System.Collections.Generic; using System.Linq; using NUnit.Framework; using Should.Fluent; using TechTalk.SpecFlow; using TechTalk.SpecFlow.Assist; +using SpecFlow.Assist.Dynamic; namespace Specs.Steps { @@ -15,22 +15,20 @@ private static dynamic GetItem(int itemNumber) { return State.OriginalSet[itemNumber - 1]; } - [Given(@"I create a set of dynamic instances from this table")] [When(@"I create a set of dynamic instances from this table")] public void WithMethodBInding(Table table) { - State.OriginalSet = table.CreateDynamicSet().ToList(); + State.OriginalSet = table.CreateDynamicSet().ToList(); } [Given(@"I create a set of dynamic instances from this table using no type conversion")] public void WithMethodBIndingNoTypeConversion(Table table) { - State.OriginalSet = table.CreateDynamicSet(false).ToList(); + State.OriginalSet = table.CreateDynamicSet(new Options { DoTypeConversion = false }).ToList(); } - [Then(@"I should have a list of (\d+) dynamic objects")] public void ShouldContain(int expectedNumberOfItems) { @@ -61,7 +59,6 @@ public void ThenTheItemShouldStillAgeEqual(int itemNumber, string expectedAge) Assert.AreEqual(expectedAge, GetItem(itemNumber).Age); } - [Then(@"the (\d+) item should have Name equal to '(.*)'")] public void ItemInSetShouldHaveExpectedName(int itemNumber, string expectedName) { @@ -77,8 +74,7 @@ public void ItemInSetShouldHaveExpectedLenghtInMeters(int itemNumber, double exp [When(@"I create a set of dynamic instances from this table using no type conversion")] public void WhenICreateASetOfDynamicInstancesFromThisTableUsingNoTypeConversion(Table table) { - State.OriginalSet = table.CreateDynamicSet(false).ToList(); + State.OriginalSet = table.CreateDynamicSet(new Options { DoTypeConversion = false }).ToList(); } - } }