Example #1
0
        public void Compare_List_With_Null()
        {
            SutEngine.Configure <IListModel>()
            .For(x => x.Children, x => x.MatchUsing(y => y.Id));

            var oldModel = new IListModel()
            {
                Id       = 1,
                Children = null,
            };

            var newModel = new IListModel()
            {
                Id       = 1,
                Children = new List <GrandChildModel>()
                {
                    new GrandChildModel()
                    {
                        Id    = 100,
                        Name  = "Name",
                        Value = 25,
                    }
                }
            };

            var diff = SutEngine.Compare(oldModel, newModel);

            Assert.AreEqual(3, diff.Count());
        }
Example #2
0
        public void Compare_Object_With_Ignored_Properties()
        {
            SutEngine.Configure <HasIgnores>()
            .Ignore(x => x.IgnoreChild)
            .Ignore(x => x.IgnoreValue);

            var oldModel = new HasIgnores
            {
                Id          = 1,
                IgnoreValue = 5,
                IgnoreChild = new GrandChildModel
                {
                    Id    = 10,
                    Name  = "A",
                    Value = 100
                }
            };
            var newModel = new HasIgnores
            {
                Id          = 2,
                IgnoreValue = 55,
                IgnoreChild = new GrandChildModel
                {
                    Id    = 20,
                    Name  = "B",
                    Value = 200
                }
            };

            var changes = SutEngine.Compare(oldModel, newModel);

            Assert.AreEqual(changes.Count(), 1);
            Assert.IsNotNull(changes.Single(x => x.Name == "Id" && (long)x.OldValue == 1 && (long)x.NewValue == 2));
        }
Example #3
0
        public void Compare_Object_With_Dictionary_Property()
        {
            var comparer = SutEngine.Get <HasDictionary>();
            var oldModel = new HasDictionary
            {
                Names = new Dictionary <string, string>
                {
                    { "fr", "Salut" },
                    { "en", "Hi" }
                }
            };
            var newModel = new HasDictionary
            {
                Names = new Dictionary <string, string>
                {
                    { "en", "Hello" },
                    { "es", "Hola" }
                }
            };

            var changes = comparer(oldModel, newModel);

            Assert.AreEqual(changes.Count(), 3);
            Assert.IsNotNull(changes.Single(x => x.Name == "Names.fr" && (string)x.OldValue == "Salut"));
            Assert.IsNotNull(changes.Single(x => x.Name == "Names.es" && (string)x.NewValue == "Hola"));
            Assert.IsNotNull(changes.Single(x => x.Name == "Names.en" && (string)x.OldValue == "Hi" && (string)x.NewValue == "Hello"));
        }
Example #4
0
        public void Configure_A_Type_Ignoring_Public_Fields()
        {
            SutEngine.Configure <PublicFieldsModel>();

            var oldModel = new PublicFieldsModel()
            {
                Id     = 1,
                Check  = false,
                Name   = "Name",
                Values = new List <int>()
                {
                    1, 2, 3, 4
                },
                Ignored = 5,
            };

            var newModel = new PublicFieldsModel()
            {
                Id     = 1,
                Check  = true,
                Name   = "Name 2",
                Values = new List <int>()
                {
                    2, 3, 4, 5
                },
                Ignored = 6,
            };

            var diff = SutEngine.Compare(oldModel, newModel);

            Assert.AreEqual(0, diff.Count);
        }
        public void Configure_A_Type_Twice_Should_Throw()
        {
            SutEngine.Configure <SimpleModel>()
            .Ignore(x => x.Value);

            SutEngine.Configure <SimpleModel>();
        }
Example #6
0
        public void Compare_Nested_Null_Object_Does_Not_Throw_NullReferenceException()
        {
            var oldModel = new NestedModel()
            {
                Id    = 10,
                Child = null
            };

            var newModel = new NestedModel()
            {
                Id    = 10,
                Child = new ChildModel()
                {
                    Id         = 100,
                    Name       = "Child",
                    GrandChild = new GrandChildModel()
                    {
                        Id    = 1000,
                        Name  = "GrandChild",
                        Value = 500,
                    }
                }
            };

            var changes = SutEngine.Compare(oldModel, newModel);

            Assert.AreEqual(changes.Count(), 5);
            Assert.IsNotNull(changes.Single(x => x.Name == "Child.Id"));
            Assert.IsNotNull(changes.Single(x => x.Name == "Child.GrandChild.Value"));
            Assert.IsNotNull(changes.Single(x => x.Name == "Child.GrandChild.Name"));
        }
Example #7
0
        public void Compare_Dictionary_With_Null()
        {
            SutEngine.Configure <IDictionaryModel>();

            var oldModel = new IDictionaryModel()
            {
                Id       = 1,
                Children = null,
            };

            var newModel = new IDictionaryModel()
            {
                Id       = 1,
                Children = new Dictionary <int, GrandChildModel>()
                {
                    { 100, new GrandChildModel()
                      {
                          Id    = 100,
                          Name  = "Name",
                          Value = 25,
                      } }
                }
            };

            var diff = SutEngine.Compare(oldModel, newModel);

            Assert.AreEqual(3, diff.Count());
        }
Example #8
0
        public void Benchmark_Nested_Model()
        {
            var oldModel = new NestedModel
            {
                Id    = 1,
                Child = new ChildModel
                {
                    Id         = 2,
                    Name       = "Child",
                    GrandChild = new GrandChildModel
                    {
                        Id    = 3,
                        Name  = "GrandChild",
                        Value = 100,
                    }
                }
            };
            var newModel = new NestedModel
            {
                Id    = 1,
                Child = new ChildModel
                {
                    Id         = 2,
                    Name       = "Child 2",
                    GrandChild = new GrandChildModel
                    {
                        Id    = 4,
                        Name  = "GrandChild 2",
                        Value = 200,
                    }
                }
            };

            var sw = new Stopwatch();

            sw.Start();
            var comparer = SutEngine.Get <NestedModel>();

            sw.Stop();
            var compilation = sw.ElapsedMilliseconds;

            sw.Reset();

            sw.Start();
            var numIterations = 100000;

            for (var i = 0; i < numIterations; i++)
            {
                var updates = comparer(oldModel, newModel);
            }
            sw.Stop();

            var benchmark = sw.ElapsedMilliseconds;

            Console.WriteLine($"Compilation took {compilation} ms.");
            Console.WriteLine($"{numIterations} iterations took {benchmark} ms.");
            Assert.IsTrue(benchmark < 500);
        }
        public void Configure_A_Property_Twice_Should_Throw()
        {
            SutEngine.Configure <SimpleModel>()
            .For(x => x.Value, x => x.Ignore())
            .For(x => x.Value, x => x.Ignore());

            SutEngine.Configure <NestedList>()
            .For(x => x.Children, x => x.MatchUsing(y => y.Id));
        }
Example #10
0
        public void Compare_List_As_Deep_Compare_With_Default()
        {
            SutEngine.Configure <NestedListWithDefault>()
            .For(x => x.Children, x => x.MatchUsing(y => y.Id, -1));

            var oldModel = new NestedListWithDefault()
            {
                Children = new List <GrandChildModel>()
                {
                    new GrandChildModel()
                    {
                        Id    = 100,
                        Name  = "Name 1",
                        Value = 100
                    },
                }
            };

            var newModel = new NestedListWithDefault()
            {
                Children = new List <GrandChildModel>()
                {
                    new GrandChildModel()
                    {
                        Id    = 100,
                        Name  = "Name 1",
                        Value = 100
                    },
                    new GrandChildModel()
                    {
                        Id    = -1,
                        Name  = "Name 2",
                        Value = 200
                    },
                    new GrandChildModel()
                    {
                        Id    = -1,
                        Name  = "Name 3",
                        Value = 300
                    },
                    new GrandChildModel()
                    {
                        Id    = -1,
                        Name  = "Name 4",
                        Value = 400
                    },
                }
            };

            var changes = SutEngine.Compare(oldModel, newModel);

            Assert.AreEqual(changes.Count(), 9);
            Assert.IsNotNull(changes.Count(x => x.Name == "Children.0.Id" && (long)x.NewValue == 0) == 3);
        }
Example #11
0
        public void Compare_List_As_Deep_Compare()
        {
            SutEngine.Configure <NestedList>()
            .For(x => x.Children, x => x.MatchUsing(y => y.Id));

            var oldModel = new NestedList()
            {
                Children = new List <GrandChildModel>()
                {
                    new GrandChildModel()
                    {
                        Id    = 100,
                        Name  = "Name 1",
                        Value = 100
                    },
                    new GrandChildModel()
                    {
                        Id    = 200,
                        Name  = "Name 2",
                        Value = 200
                    },
                }
            };

            var newModel = new NestedList()
            {
                Children = new List <GrandChildModel>()
                {
                    new GrandChildModel()
                    {
                        Id    = 100,
                        Name  = "Name 1 - Changed",
                        Value = 150
                    },
                    new GrandChildModel()
                    {
                        Id    = 300,
                        Name  = "Name 3",
                        Value = 300
                    },
                    new GrandChildModel()
                    {
                        Id    = 400,
                        Name  = "Name 4",
                        Value = 200
                    }
                }
            };

            var changes = SutEngine.Compare(oldModel, newModel);

            Assert.AreEqual(changes.Count(), 11);
            Assert.IsNotNull(changes.Single(x => x.Name == "Children.100.Name"));
        }
Example #12
0
        public void Configure_IDictionary()
        {
            SutEngine.Configure <IDictionaryModel>();

            var oldModel = new IDictionaryModel()
            {
                Id       = 1,
                Children = new Dictionary <int, GrandChildModel>
                {
                    { 100,
                      new GrandChildModel()
                      {
                          Id    = 100,
                          Name  = "Name 1",
                          Value = 100
                      } },
                    { 200,
                      new GrandChildModel()
                      {
                          Id    = 200,
                          Name  = "Name 2",
                          Value = 200
                      } }
                },
            };

            var newModel = new IDictionaryModel()
            {
                Id       = 1,
                Children = new Dictionary <int, GrandChildModel>
                {
                    { 100,
                      new GrandChildModel()
                      {
                          Id    = 100,
                          Name  = "Name 1",
                          Value = 100
                      } },
                    { 300,
                      new GrandChildModel()
                      {
                          Id    = 300,
                          Name  = "Name 3",
                          Value = 300
                      } }
                },
            };

            var diff = SutEngine.Compare(oldModel, newModel);

            Assert.AreEqual(6, diff.Count);
        }
Example #13
0
        public void Benchmark_Simple_Model()
        {
            var oldModel = new SimpleModel
            {
                Id       = 1,
                Check    = true,
                Name     = "Name",
                Value    = 1.23m,
                Date     = new DateTime(2015, 01, 01, 12, 50, 30),
                Time     = new TimeSpan(5, 4, 3),
                State    = State.Inactive,
                Nullable = null,
            };

            var newModel = new SimpleModel
            {
                Id       = 1,
                Check    = false,
                Name     = "Name?",
                Value    = 10.23m,
                Date     = new DateTime(2015, 01, 02, 12, 50, 30),
                Time     = new TimeSpan(5, 4, 1),
                State    = State.Active,
                Nullable = true,
            };

            var sw = new Stopwatch();

            sw.Start();
            var comparer = SutEngine.Get <SimpleModel>();

            sw.Stop();
            var compilation = sw.ElapsedMilliseconds;

            sw.Reset();

            sw.Start();
            var numIterations = 100000;

            for (var i = 0; i < numIterations; i++)
            {
                var updates = comparer(oldModel, newModel);
            }
            sw.Stop();

            var benchmark = sw.ElapsedMilliseconds;

            Console.WriteLine($"Compilation took {compilation} ms.");
            Console.WriteLine($"{numIterations} iterations took {benchmark} ms.");
            Assert.IsTrue(benchmark < 500);
        }
Example #14
0
        public void Configure_Inherited_IEnumerable()
        {
            SutEngine.Configure <InheritedIEnumerableModel>()
            .For(x => x.Children, x => x.MatchUsing(y => y.Id));

            var oldModel = new InheritedIEnumerableModel()
            {
                Id       = 1,
                Children = new IEnumerableCollectionClass
                {
                    new GrandChildModel()
                    {
                        Id    = 100,
                        Name  = "Name 1",
                        Value = 100
                    },
                    new GrandChildModel()
                    {
                        Id    = 200,
                        Name  = "Name 2",
                        Value = 200
                    }
                },
            };

            var newModel = new InheritedIEnumerableModel()
            {
                Id       = 1,
                Children = new IEnumerableCollectionClass
                {
                    new GrandChildModel()
                    {
                        Id    = 100,
                        Name  = "Name 1",
                        Value = 100
                    },
                    new GrandChildModel()
                    {
                        Id    = 300,
                        Name  = "Name 3",
                        Value = 300
                    }
                },
            };

            var diff = SutEngine.Compare(oldModel, newModel);

            Assert.AreEqual(6, diff.Count);
        }
Example #15
0
        public void Configure_Array()
        {
            SutEngine.Configure <ArrayModel>()
            .For(x => x.ArrayChildren, x => x.MatchUsing(y => y.Id));

            var oldModel = new ArrayModel()
            {
                Id            = 1,
                ArrayChildren = new[]
                {
                    new GrandChildModel()
                    {
                        Id    = 100,
                        Name  = "Name 1",
                        Value = 100
                    },
                    new GrandChildModel()
                    {
                        Id    = 200,
                        Name  = "Name 2",
                        Value = 200
                    }
                },
            };

            var newModel = new ArrayModel()
            {
                Id            = 1,
                ArrayChildren = new[]
                {
                    new GrandChildModel()
                    {
                        Id    = 100,
                        Name  = "Name 1",
                        Value = 100
                    },
                    new GrandChildModel()
                    {
                        Id    = 300,
                        Name  = "Name 3",
                        Value = 300
                    }
                },
            };

            var diff = SutEngine.Compare(oldModel, newModel);

            Assert.AreEqual(6, diff.Count);
        }
Example #16
0
        public void Compile_Async()
        {
            Assert.IsFalse(SutEngine.IsTypeConfigured(typeof(SimpleModel)));

            SutEngine.Configure <SimpleModel>()
            .Compile.Async();

            Assert.IsTrue(SutEngine.IsTypeConfigured(typeof(SimpleModel)));
            Assert.IsFalse(SutEngine.IsTypeCompiled(typeof(SimpleModel)));

            var comparer = SutEngine.Get <SimpleModel>();

            Assert.IsTrue(SutEngine.IsTypeConfigured(typeof(SimpleModel)));
            Assert.IsTrue(SutEngine.IsTypeCompiled(typeof(SimpleModel)));
        }
Example #17
0
        public void Compare_Dictionary_With_Deep_Compare()
        {
            SutEngine.Configure <ObjectDictionary>();

            var oldModel = new ObjectDictionary()
            {
                Nested = new Dictionary <int, GrandChildModel>()
                {
                    { 1, new GrandChildModel()
                      {
                          Id    = 100,
                          Name  = "Name 1",
                          Value = 100
                      } },
                    { 2, new GrandChildModel()
                      {
                          Id    = 200,
                          Name  = "Name 2",
                          Value = 200
                      } },
                }
            };

            var newModel = new ObjectDictionary()
            {
                Nested = new Dictionary <int, GrandChildModel>()
                {
                    { 1, new GrandChildModel()
                      {
                          Id    = 100,
                          Name  = "Name 1 - Changed",
                          Value = 150
                      } },
                    { 3, new GrandChildModel()
                      {
                          Id    = 300,
                          Name  = "Name 3",
                          Value = 300
                      } },
                }
            };

            var changes = SutEngine.Compare(oldModel, newModel);

            Assert.AreEqual(changes.Count(), 8);
            Assert.IsNotNull(changes.Single(x => x.Name == "Nested.1.Value"));
        }
Example #18
0
        public void Compile_A_Type_With_All_Properties_Ignored()
        {
            SutEngine.Configure <HasIgnores>()
            .Ignore(x => x.Id)
            .Ignore(x => x.IgnoreChild)
            .Ignore(x => x.IgnoreValue);
            var diff = SutEngine.Compare(new HasIgnores()
            {
                Id = 1, IgnoreValue = 100
            },
                                         new HasIgnores()
            {
                Id = 2, IgnoreValue = 200
            });

            Assert.AreEqual(0, diff.Count);
        }
Example #19
0
        public void Compare_Object_Against_Null()
        {
            var model = new SimpleModel
            {
                Id    = 1,
                Check = true,
                Name  = "Name",
                Value = 1.23m,
                Date  = new DateTime(2015, 01, 01, 12, 50, 30),
                Time  = new TimeSpan(5, 4, 3)
            };

            var nullToModel = SutEngine.Compare(null, model);
            var modelToNull = SutEngine.Compare(model, null);

            Assert.IsTrue(nullToModel.All(x =>
                                          modelToNull.Single(y => x.Name == y.Name && object.Equals(y.OldValue, x.NewValue)) != null));
        }
Example #20
0
        public void Compare_Nestted_Objects()
        {
            var comparer = SutEngine.Get <NestedModel>();
            var oldModel = new NestedModel
            {
                Id    = 1,
                Child = new ChildModel
                {
                    Id         = 2,
                    Name       = "Child",
                    GrandChild = new GrandChildModel
                    {
                        Id    = 3,
                        Name  = "GrandChild",
                        Value = 100,
                    }
                }
            };
            var newModel = new NestedModel
            {
                Id    = 1,
                Child = new ChildModel
                {
                    Id         = 2,
                    Name       = "Child 2",
                    GrandChild = new GrandChildModel
                    {
                        Id    = 4,
                        Name  = "GrandChild 2",
                        Value = 200,
                    }
                }
            };

            var changes = comparer(oldModel, newModel);

            Assert.AreEqual(changes.Count(), 4);
            Assert.IsNotNull(changes.Single(x => x.Name == "Child.Name" && (string)x.OldValue == "Child" && (string)x.NewValue == "Child 2"));
            Assert.IsNotNull(changes.Single(x => x.Name == "Child.GrandChild.Name" && (string)x.OldValue == "GrandChild" && (string)x.NewValue == "GrandChild 2"));
            Assert.IsNotNull(changes.Single(x => x.Name == "Child.GrandChild.Id" && (long)x.OldValue == 3 && (long)x.NewValue == 4));
            Assert.IsNotNull(changes.Single(x => x.Name == "Child.GrandChild.Value" && (int)x.OldValue == 100 && (int)x.NewValue == 200));
        }
Example #21
0
        public void Compare_Object_With_List_Property()
        {
            var comparer = SutEngine.Get <HasList>();
            var oldModel = new HasList
            {
                Ids = new List <int> {
                    1, 2, 3, 4, 5
                }
            };
            var newModel = new HasList
            {
                Ids = new List <int> {
                    1, 3, 4, 5, 6
                }
            };
            var changes = comparer(oldModel, newModel);

            Assert.AreEqual(changes.Count(), 2);
            Assert.IsNotNull(changes.Single(x => x.Name == "Ids" && x.OldValue is int && (int)x.OldValue == 2));
            Assert.IsNotNull(changes.Single(x => x.Name == "Ids" && x.NewValue is int && (int)x.NewValue == 6));
        }
Example #22
0
        public void Compare_Simple_Model()
        {
            var comparer = SutEngine.Get <SimpleModel>();
            var oldModel = new SimpleModel
            {
                Id       = 1,
                Check    = true,
                Name     = "Name",
                Value    = 1.23m,
                Date     = new DateTime(2015, 01, 01, 12, 50, 30),
                Time     = new TimeSpan(5, 4, 3),
                State    = State.Inactive,
                Nullable = null,
            };

            var newModel = new SimpleModel
            {
                Id       = 1,
                Check    = false,
                Name     = "Name?",
                Value    = 10.23m,
                Date     = new DateTime(2015, 01, 02, 12, 50, 30),
                Time     = new TimeSpan(5, 4, 1),
                State    = State.Active,
                Nullable = true,
            };

            var changes = comparer(oldModel, newModel);

            Assert.AreEqual(changes.Count(), 7);
            Assert.IsNotNull(changes.Single(x => x.Name == "Check" && (bool)x.OldValue == true && (bool)x.NewValue == false));
            Assert.IsNotNull(changes.Single(x => x.Name == "Name" && (string)x.OldValue == "Name" && (string)x.NewValue == "Name?"));
            Assert.IsNotNull(changes.Single(x => x.Name == "Value" && (decimal)x.OldValue == 1.23m && (decimal)x.NewValue == 10.23m));
            Assert.IsNotNull(changes.Single(x => x.Name == "Date" && (DateTime)x.OldValue != (DateTime)x.NewValue));
            Assert.IsNotNull(changes.Single(x => x.Name == "Time" && (TimeSpan)x.OldValue != (TimeSpan)x.NewValue));
            Assert.IsNotNull(changes.Single(x => x.Name == "State" && (State)x.OldValue != (State)x.NewValue));
            Assert.IsNotNull(changes.Single(x => x.Name == "Nullable" && (bool?)x.OldValue == null && (bool?)x.NewValue == true));
        }
Example #23
0
 public void Compile_A_Type_With_A_Struct_Member()
 {
     SutEngine.Configure <StructModel>()
     .Compile.Now();
 }
Example #24
0
 public void Configure_Not_A_Member_Should_Throw()
 {
     SutEngine.Configure <SimpleModel>()
     .For(x => "a string", x => x.Ignore());
 }
Example #25
0
 public void Configure_Enumerable_Twice_Should_Throw()
 {
     SutEngine.Configure <ArrayModel>()
     .For(x => x.ArrayChildren, x => x.MatchUsing(y => y.Id))
     .For(x => x.ArrayChildren, x => x.MatchUsing(y => y.Id));
 }
Example #26
0
 public void Configure_A_Member_That_Is_Not_A_Field_Or_Property_Should_Throw()
 {
     SutEngine.Configure <SimpleModel>()
     .For(x => x.GetHashCode(), x => x.Ignore());
 }
Example #27
0
 public void Compile_A_Type_With_Circular_References()
 {
     SutEngine.Configure <ParentCirularRef>()
     .Compile.Now();
 }
Example #28
0
        public void Compile_A_Type_With_No_Public_Properties_To_Compare()
        {
            var diff = SutEngine.Compare(new NoPublicProperty(1), new NoPublicProperty(2));

            Assert.AreEqual(0, diff.Count);
        }
Example #29
0
        public void Compare_Null_With_Null()
        {
            var changes = SutEngine.Compare <NestedModel>(null, null);

            Assert.IsFalse(changes.Any());
        }
Example #30
0
 public void Compile_A_Child_Type()
 {
     SutEngine.Configure <Child>()
     .Compile.Now();
 }