Exemplo n.º 1
0
        /// <summary>
        /// Add a breadcrumb to an existing breadcrumb
        /// </summary>
        /// <param name="config">Comparison configuration</param>
        /// <param name="existing">The existing breadcrumb</param>
        /// <param name="name">The field or property name</param>
        /// <param name="extra">Extra information to append after the name</param>
        /// <param name="index">The index if it is an array, list, row etc.</param>
        /// <returns>The new breadcrumb</returns>
        protected string AddBreadCrumb(ComparisonConfig config, string existing, string name, string extra, string index)
        {
            bool useIndex = !String.IsNullOrEmpty(index);
            bool useName = name.Length > 0;
            StringBuilder sb = new StringBuilder();

            sb.Append(existing);

            if (useName)
            {
                sb.AppendFormat(".");
                sb.Append(name);
            }

            sb.Append(extra);

            if (useIndex)
            {
                // ReSharper disable RedundantAssignment
                int result = -1;
                // ReSharper restore RedundantAssignment
                sb.AppendFormat(Int32.TryParse(index, out result) ? "[{0}]" : "[\"{0}\"]", index);
            }

            if (config.ShowBreadcrumb)
                Debug.WriteLine(sb.ToString());

            return sb.ToString();
        }
 /// <summary>
 /// Add a breadcrumb to an existing breadcrumb
 /// </summary>
 /// <param name="config">The comparison configuration</param>
 /// <param name="existing">The existing breadcrumb</param>
 /// <param name="name">The property or field name</param>
 /// <param name="extra">Extra information to output after the name</param>
 /// <param name="index">The index for an array, list, or row</param>
 /// <returns>The new breadcrumb</returns>
 protected string AddBreadCrumb(ComparisonConfig config, string existing, string name, string extra, int index)
 {
     return AddBreadCrumb(config, existing, name, extra, index >= 0 ? index.ToString(CultureInfo.InvariantCulture) : null);
 }
 /// <summary>
 /// Add a breadcrumb to an existing breadcrumb
 /// </summary>
 /// <param name="config">Comparison configuration</param>
 /// <param name="existing">The existing breadcrumb</param>
 /// <param name="name">The field or property name</param>
 /// <returns>The new breadcrumb</returns>
 protected string AddBreadCrumb(ComparisonConfig config, string existing, string name)
 {
     return AddBreadCrumb(config, existing, name, string.Empty, null);
 }
        private bool IsValidIndexer(ComparisonConfig config, PropertyInfo info, string breadCrumb)
        {
            ParameterInfo[] indexers = info.GetIndexParameters();

            if (indexers.Length == 0)
            {
                return false;
            }

            if (config.SkipInvalidIndexers)
                return false;

            if (indexers.Length > 1)
            {
                if (config.SkipInvalidIndexers)
                    return false;

                throw new Exception("Cannot compare objects with more than one indexer for object " + breadCrumb);
            }

            if (indexers[0].ParameterType != typeof(Int32))
            {
                if (config.SkipInvalidIndexers)
                    return false;

                throw new Exception("Cannot compare objects with a non integer indexer for object " + breadCrumb);
            }

#if !NEWPCL
            var type = info.ReflectedType;
#else
            var type = info.DeclaringType;
#endif
            if (type == null)
            {
                if (config.SkipInvalidIndexers)
                    return false;

                throw new Exception("Cannot compare objects with a null indexer for object " + breadCrumb);
            }

            if (type.GetProperty("Count") == null
                || type.GetProperty("Count").PropertyType != typeof(Int32))
            {
                if (config.SkipInvalidIndexers)
                    return false;

                throw new Exception("Indexer must have a corresponding Count property that is an integer for object " + breadCrumb);
            }

            return true;
        }
Exemplo n.º 5
0
 /// <summary>
 /// Add a breadcrumb to an existing breadcrumb
 /// </summary>
 /// <param name="config">Comparison configuration</param>
 /// <param name="existing">The existing breadcrumb</param>
 /// <param name="name">The field or property name</param>
 /// <returns>The new breadcrumb</returns>
 protected string AddBreadCrumb(ComparisonConfig config, string existing, string name)
 {
     return(AddBreadCrumb(config, existing, name, string.Empty, null));
 }
Exemplo n.º 6
0
        public void ClassTypeInListIgnorePositive()
        {
            var comparisonConfig = new ComparisonConfig()
            {
                MaxDifferences  = int.MaxValue,
                MembersToIgnore = new List <string>()
                {
                    "Type"
                },
                ClassTypesToIgnore = new List <Type>()
                {
                    typeof(Officer)
                },
                CollectionMatchingSpec = new Dictionary <System.Type, IEnumerable <string> >()
                {
                    {
                        typeof(Officer),
                        new List <string>()
                        {
                            "ID"
                        }
                    },
                    {
                        typeof(Person),
                        new List <string>()
                        {
                            "ID"
                        }
                    }
                },
                TreatStringEmptyAndNullTheSame = true,
                IgnoreCollectionOrder          = true
            };

            _compare.Config = comparisonConfig;

            var list1 = new List <object>();
            var list2 = new List <object>();

            Officer p1 = new Officer();

            p1.ID   = 2;
            p1.Name = "Greg";
            p1.Type = Deck.AstroPhysics;

            Officer p2 = new Officer();

            p2.ID   = 1;
            p2.Name = "Leyla";
            p2.Type = Deck.Engineering;

            Person p3 = new Person();

            p3.ID          = 3;
            p3.Name        = "Henk";
            p3.DateCreated = DateTime.Now;

            Person p4 = new Person();

            p4.ID          = 3;
            p4.Name        = "Henk";
            p4.DateCreated = p3.DateCreated;

            list1.Add(p1);
            list1.Add(p3);
            list2.Add(p2);
            list2.Add(p4);

            var result = _compare.Compare(list1, list2);

            Assert.IsTrue(result.AreEqual, result.DifferencesString);
        }
Exemplo n.º 7
0
        public void CollectionMatchingSpecBasedOnInterfaceTest()
        {
            IEntity entityToModify = new Entity {
                Id = 101, Description = "Desription 2"
            };
            IEntity entity1 = new Entity
            {
                Id       = 1,
                Children = new List <IEntity>
                {
                    new Entity
                    {
                        Id       = 10,
                        Children = new List <IEntity>
                        {
                            new Entity {
                                Id = 100, Description = "Description 1"
                            }
                        }
                    },
                    new Entity
                    {
                        Id       = 11,
                        Children = new List <IEntity>
                        {
                            entityToModify,
                            new Entity {
                                Id = 102, Description = "Description 3"
                            }
                        }
                    }
                }
            };
            IEntity newlyAddedEntity = new Entity {
                Id = 103, Description = "Description 4"
            };
            IEntity modifiedEntity = new Entity {
                Id = 101, Description = "Desription 5"
            };
            IEntity entity2 = new Entity
            {
                Id       = 1,
                Children = new List <IEntity>
                {
                    new Entity
                    {
                        Id       = 10,
                        Children = new List <IEntity>
                        {
                            new Entity {
                                Id = 100, Description = "Description 1"
                            },
                            newlyAddedEntity
                        }
                    },
                    new Entity
                    {
                        Id       = 11,
                        Children = new List <IEntity>
                        {
                            new Entity {
                                Id = 102, Description = "Description 3"
                            },
                            modifiedEntity
                        }
                    }
                }
            };

            ComparisonConfig config = new ComparisonConfig
            {
                IgnoreCollectionOrder  = true,
                MaxDifferences         = int.MaxValue,
                CollectionMatchingSpec = new Dictionary <Type, IEnumerable <string> > {
                    { typeof(IEntity), new string[] { "Id" } }
                }
            };

            _compare.Config = config;

            var result = _compare.Compare(entity1, entity2);

            Assert.AreEqual(result.Differences.Count, 3);
            Assert.AreEqual(result.Differences[0].PropertyName, "Children[Id:10].Children");
            Assert.AreEqual(result.Differences[1].PropertyName, "Children[Id:10].Children[Id:103]");
            Assert.AreEqual(result.Differences[2].PropertyName, "Children[Id:11].Children[Id:101].Description");
        }
Exemplo n.º 8
0
 /// <summary>
 /// Add a breadcrumb to an existing breadcrumb
 /// </summary>
 /// <param name="config">The comparison configuration</param>
 /// <param name="existing">The existing breadcrumb</param>
 /// <param name="name">The property or field name</param>
 /// <param name="extra">Extra information to output after the name</param>
 /// <param name="index">The index for an array, list, or row</param>
 /// <returns>The new breadcrumb</returns>
 protected string AddBreadCrumb(ComparisonConfig config, string existing, string name, string extra, int index)
 {
     return(AddBreadCrumb(config, existing, name, extra, index >= 0 ? index.ToString(CultureInfo.InvariantCulture) : null));
 }
Exemplo n.º 9
0
        public static ComparisonResult CommonCompareResult <T>(T expectation, T result, ComparisonConfig comparisonConfig = null)
        {
            var compare = comparisonConfig == null ? new CompareLogic() : new CompareLogic(comparisonConfig);
            ComparisonResult comparisonResult = compare.Compare(expectation, result);

            return(comparisonResult);
        }
Exemplo n.º 10
0
 public static bool CommonCompare <T>(T expectation, T result, ComparisonConfig comparisonConfig = null)
 {
     return(CommonCompareResult(expectation, result, comparisonConfig).AreEqual);
 }
Exemplo n.º 11
0
        static void Main(string[] args)
        {
            Pupil pashka = new Pupil
            {
                Name = "Pashka",
                Age  = 15
            };

            Pupil jeka = new Pupil
            {
                Name = "Jeka",
                Age  = 18
            };

            Pupil sashka = new Pupil
            {
                Name = "Sashka",
                Age  = 21
            };

            Pupil stasik = new Pupil
            {
                Name = "Stasik",
                Age  = 35
            };

            School school96 = new School
            {
                Name  = "96",
                Pupil = new List <Pupil>()
                {
                    pashka
                }
            };

            School school143 = new School
            {
                Name  = "143",
                Pupil = new List <Pupil>()
                {
                    pashka, jeka, sashka
                },
                Room = new Room {
                    Color = "red", Table = new Table {
                        Form = "circle"
                    }
                }
            };

            School school54 = new School
            {
                Name  = "54",
                Pupil = new List <Pupil>
                {
                    jeka, pashka
                },
                Room = new Room {
                    Color = "green", Table = new Table {
                        Form = "rectangle"
                    }
                }
            };

            ReadOnlySchool school104 = new ReadOnlySchool
            {
                Name  = "104",
                pupil = new List <Pupil>
                {
                    jeka, pashka
                }
            };

            ComparisonConfig config = new ComparisonConfig();

            config.MaxDifferences  = int.MaxValue;
            config.CompareChildren = true;
            //config.IgnoreCollectionOrder = true;
            config.CustomComparers.Add(new CustomListComparer(RootComparerFactory.GetRootComparer()));
            config.CustomComparers.Add(new CustomExpandoObjectComparer(RootComparerFactory.GetRootComparer()));
            config.CustomComparers.Add(new NullToObjectComparer(RootComparerFactory.GetRootComparer()));
            ////config.CustomComparers = new List<BaseTypeComparer>();
            ////config.CustomComparers.Add(new ExpandoObjectComparer(RootComparerFactory.GetRootComparer()));
            ////config.CustomComparers.Add(new FirstNullClassComparer(RootComparerFactory.GetRootComparer()));
            ////config.CustomComparers.Add(new CollectionComparer(RootComparerFactory.GetRootComparer()));

            CompareLogic compareLogic = new CompareLogic(config);
            //ComparisonResult result = compareLogic.Compare(school143, school54);

            //var diffsResult = Scc.Portal.Orchard.AuditTrails.DiffsBuilder.DiffsBuilder.BuildDiffs(school143, school54);

            dynamic a = new ExpandoObject();
            dynamic b = new ExpandoObject();

            a.Table = "Table";
            b.Table = "Chair";
            b.Wall  = "Rectangle";
            b.Pupil = new HashSet <Pupil>
            {
                jeka, pashka
            };

            ComparisonResult expandoResult = compareLogic.Compare(a, b);

            var diffsResult = Scc.Portal.Orchard.AuditTrails.DiffsBuilder.DiffsBuilder.BuildDiffs(a, b);

            var yes = true;
        }
Exemplo n.º 12
0
        protected static void TestDataStore <TKey, TValue>(
            IDataStore <TKey, TValue> dataStore,
            TKey testKey,
            TValue testValueA,
            TValue testValueB)
            where TKey : ITestKey
        {
            var compareConfig = new ComparisonConfig
            {
                MembersToIgnore = new List <string> {
                    "Id"
                }
            };

            Action <object, object> assertAreEqual = (a, b) =>
            {
                if (a.GetType() == typeof(string))
                {
                    Assert.Equal(a, b);
                }
                else
                {
                    Assert.Equal(
                        StripUnderscoreProperties(JObject.FromObject(a)).ToString(),
                        StripUnderscoreProperties(JObject.FromObject(b)).ToString());
                }
            };

            var preExistsResult = dataStore.Exists(testKey).Result;

            Assert.False(preExistsResult);

            var preGetResult = dataStore.Get(testKey).Result;

            Assert.Null(preGetResult);

            var preListResult = dataStore
                                .ListValues(k => k.AccountId == testKey.AccountId).Result
                                .ToList();

            Assert.False(preListResult.Any());

            var preUpdateResult = dataStore.Update(testKey, testValueA).Result;

            Assert.False(preUpdateResult);

            var createResult = dataStore.Create(testKey, testValueA).Result;

            Assert.True(createResult);

            var firstExistsResult = dataStore.Exists(testKey).Result;

            Assert.True(firstExistsResult);

            var firstGetResult = dataStore.Get(testKey).Result;

            assertAreEqual(testValueA, firstGetResult);

            var firstListResult = dataStore
                                  .ListValues(k => k.AccountId == testKey.AccountId).Result
                                  .ToList();

            assertAreEqual(testValueA, firstListResult.Single());

            var postCreateResult = dataStore.Create(testKey, testValueA).Result;

            Assert.False(postCreateResult);

            var updateResult = dataStore.Update(testKey, testValueB).Result;

            Assert.True(updateResult);

            var secondExistsResult = dataStore.Exists(testKey).Result;

            Assert.True(secondExistsResult);

            var secondGetResult = dataStore.Get(testKey).Result;

            assertAreEqual(testValueB, secondGetResult);

            dataStore.Upsert(testKey, testValueA).Wait();

            var thirdGetResult = dataStore.Get(testKey).Result;

            assertAreEqual(testValueA, thirdGetResult);

            var firstDeleteResult = dataStore.Delete(testKey).Result;

            Assert.True(firstDeleteResult);

            var postExistsResult = dataStore.Exists(testKey).Result;

            Assert.False(postExistsResult);

            var postGetResult = dataStore.Get(testKey).Result;

            Assert.Null(postGetResult);

            var postListResult = dataStore
                                 .ListValues(k => k.AccountId == testKey.AccountId).Result
                                 .ToList();

            Assert.False(postListResult.Any());

            var secondDeleteResult = dataStore.Delete(testKey).Result;

            Assert.False(secondDeleteResult);
        }
Exemplo n.º 13
0
 public ComparisonResult(ComparisonConfig config) : base(config)
 {
 }