コード例 #1
0
        private static PropertyInfo GetThePropertyOnThisObject(object @object, string propertyName)
        {
            var type = @object.GetType();

            return(type.GetProperties()
                   .FirstOrDefault(x => TEHelpers.IsPropertyMatchingToColumnName(x, propertyName)));
        }
コード例 #2
0
        /// <summary>
        /// Indicates whether the table is equivalent to the specified instance by comparing the values of all
        /// columns against the properties of the instance.  Will return false after finding the first difference.
        /// </summary>
        public static bool IsEquivalentToInstance <T>(this Table table, T instance)
        {
            AssertThatTheInstanceExists(instance);

            var instanceTable = TEHelpers.GetTheProperInstanceTable(table, typeof(T));

            return(HasDifference(instanceTable, instance) == false);
        }
コード例 #3
0
        public static T CreateInstance <T>(this Table table, InstanceCreationOptions creationOptions)
        {
            var instanceTable = TEHelpers.GetTheProperInstanceTable(table, typeof(T));

            return(TEHelpers.ThisTypeHasADefaultConstructor <T>()
                       ? TEHelpers.CreateTheInstanceWithTheDefaultConstructor <T>(instanceTable, creationOptions)
                       : TEHelpers.CreateTheInstanceWithTheValuesFromTheTable <T>(instanceTable, creationOptions));
        }
コード例 #4
0
        public static void CompareToInstance <T>(this Table table, T instance)
        {
            AssertThatTheInstanceExists(instance);

            var instanceTable = TEHelpers.GetTheProperInstanceTable(table, typeof(T));

            var differences = FindAnyDifferences(instanceTable, instance);

            if (ThereAreAnyDifferences(differences))
            {
                ThrowAnExceptionThatDescribesThoseDifferences(differences);
            }
        }
コード例 #5
0
        public static T FindInSet <T>(this Table table, IEnumerable <T> set)
        {
            var instanceTable = TEHelpers.GetTheProperInstanceTable(table, typeof(T));

            var matches = set.Where(instance => InstanceMatchesTable(instance, instanceTable)).ToArray();

            if (matches.Length > 1)
            {
                throw new ComparisonException("Multiple instances match the table");
            }

            return(matches.FirstOrDefault());
        }
コード例 #6
0
ファイル: SetComparer.cs プロジェクト: zhengbingjie/SpecFlow
        private void AssertThatAllColumnsInTheTableMatchToPropertiesOnTheType()
        {
            var normalizedPropertyNames = new HashSet <string>(from property in typeof(T).GetProperties()
                                                               select TEHelpers.NormalizePropertyNameToMatchAgainstAColumnName(property.Name));
            var normalizedColumnNames = new HashSet <string>(from columnHeader in table.Header
                                                             select TEHelpers.NormalizePropertyNameToMatchAgainstAColumnName(TEHelpers.RemoveAllCharactersThatAreNotValidInAPropertyName(columnHeader)));

            var propertiesThatDoNotExist = normalizedColumnNames.Except(normalizedPropertyNames, StringComparer.OrdinalIgnoreCase).ToArray();

            if (propertiesThatDoNotExist.Any())
            {
                throw new ComparisonException($@"The following fields do not exist:{Environment.NewLine}{string.Join(Environment.NewLine, propertiesThatDoNotExist)}");
            }
        }
コード例 #7
0
 private static bool ThePropertyDoesNotExist <T>(T instance, TableRow row)
 {
     return(instance.GetType().GetProperties()
            .Any(property => TEHelpers.IsMemberMatchingToColumnName(property, row.Id())) == false);
 }
コード例 #8
0
        public static void FillInstance(this Table table, object instance, InstanceCreationOptions creationOptions)
        {
            var instanceTable = TEHelpers.GetTheProperInstanceTable(table, instance.GetType());

            TEHelpers.LoadInstanceWithKeyValuePairs(instanceTable, instance, creationOptions);
        }
コード例 #9
0
        private void AssertThatAllColumnsInTheTableMatchToPropertiesOnTheType()
        {
            var propertiesThatDoNotExist = from columnHeader in table.Header
                                           where (typeof(T).GetProperties().Any(property => TEHelpers.IsPropertyMatchingToColumnName(property, columnHeader)) == false)
                                           select columnHeader;

            if (propertiesThatDoNotExist.Any())
            {
                throw new ComparisonException(
                          propertiesThatDoNotExist.Aggregate(@"The following fields do not exist:",
                                                             (running, next) => running + string.Format("{0}{1}", Environment.NewLine, next)));
            }
        }
コード例 #10
0
        public static IEnumerable <T> FindAllInSet <T>(this Table table, IEnumerable <T> set)
        {
            var instanceTable = TEHelpers.GetTheProperInstanceTable(table, typeof(T));

            return(set.Where(instance => InstanceMatchesTable(instance, instanceTable)).ToArray());
        }
コード例 #11
0
        public static void FillInstance <T>(this Table table, T instance)
        {
            var instanceTable = TEHelpers.GetTheProperInstanceTable <T>(table);

            TEHelpers.LoadInstanceWithKeyValuePairs(instanceTable, instance);
        }
コード例 #12
0
 public static T CreateInstance <T>(this Table table)
 {
     return(TEHelpers.ThisTypeHasADefaultConstructor <T>()
                ? TEHelpers.CreateTheInstanceWithTheDefaultConstructor <T>(table)
                : TEHelpers.CreateTheInstanceWithTheValuesFromTheTable <T>(table));
 }
コード例 #13
0
 public static void FillInstance <T>(this Table table, T instance)
 {
     TEHelpers.LoadInstanceWithKeyValuePairs(table, instance);
 }