Beispiel #1
0
        public void chunk_should_have_correct_number_of_snippets()
        {
            var diff  = Differ.Compare("one", fileOneContent, "two", fileTwoContent);
            var chunk = diff.Chunks.First();

            Assert.AreEqual(5, chunk.Snippets.Count());
        }
        public void should_have_one_chunks()
        {
            var diff = Differ.Compare("one", fileOneContent, "two", fileTwoContent);

            Assert.AreEqual(1, diff.Chunks.Count);
            Assert.IsInstanceOf <SubtractionSnippet>(diff.Chunks.Single().Snippets.Single());
        }
        public void second_chunk_should_have_correct_snippets()
        {
            var diff     = Differ.Compare("one", fileOneContent, "two", fileTwoContent);
            var chunk    = diff.Chunks.ElementAt(1);
            var snippets = chunk.Snippets;

            var firstContext = snippets.ElementAtOrDefault(0) as ContextSnippet;

            Assert.IsNotNull(firstContext);
            Assert.AreEqual(3, firstContext.OriginalLines.Count());
            Assert.AreEqual("six", firstContext.OriginalLines.ElementAt(0).Value);
            Assert.AreEqual("seven", firstContext.OriginalLines.ElementAt(1).Value);
            Assert.AreEqual("eight", firstContext.OriginalLines.ElementAt(2).Value);
            Assert.AreEqual(0, firstContext.ModifiedLines.Count());

            var addition = snippets.ElementAtOrDefault(1) as AdditionSnippet;

            Assert.IsNotNull(addition);
            Assert.AreEqual(0, addition.OriginalLines.Count());
            Assert.AreEqual(1, addition.ModifiedLines.Count());
            Assert.AreEqual("eight.5", addition.ModifiedLines.ElementAt(0).Value);

            var secondContext = snippets.ElementAtOrDefault(2) as ContextSnippet;

            Assert.IsNotNull(secondContext);
            Assert.AreEqual(2, secondContext.OriginalLines.Count());
            Assert.AreEqual("nine", secondContext.OriginalLines.ElementAt(0).Value);
            Assert.AreEqual("ten", secondContext.OriginalLines.ElementAt(1).Value);
            Assert.AreEqual(0, secondContext.ModifiedLines.Count());
        }
Beispiel #4
0
        public void chunk_should_have_correct_old_range()
        {
            var diff  = Differ.Compare("one", fileOneContent, "two", fileTwoContent);
            var chunk = diff.Chunks.First();

            Assert.AreEqual(10, chunk.OriginalRange.StartLine);
            Assert.AreEqual(10, chunk.OriginalRange.LinesAffected);
        }
        public void second_chunk_should_have_correct_new_range()
        {
            var diff  = Differ.Compare("one", fileOneContent, "two", fileTwoContent);
            var chunk = diff.Chunks.ElementAt(1);

            Assert.AreEqual(7, chunk.NewRange.StartLine);
            Assert.AreEqual(6, chunk.NewRange.LinesAffected);
        }
        public void when_compared_ignore_bom_should_consider_bom_changes_as_insignificant()
        {
            var diff = Differ.Compare("one", fileOneContent, "two", fileTwoContent, new CompareOptions
            {
                BomMode = BomMode.Ignore
            });

            Assert.AreEqual(0, diff.Chunks.Count, "Should have no chunks (identical)");
        }
Beispiel #7
0
        public static void DifferSmall()
        {
            var differ = new Differ <C, C>(new[] { "a", "b" });
            var res    = differ.Compare(c1, c2);

            // compare different types!
            var differ2 = new Differ <C, D>(new[] { "a", "b" });

            res = differ2.Compare(c1, d1);
        }
        public void when_compared_default_should_consider_bom_changes_as_significant()
        {
            var diff = Differ.Compare("one", fileOneContent, "two", fileTwoContent);

            Assert.AreEqual(1, diff.Chunks.Count, "Should have only one chunk");

            var snippets = diff.Chunks.Single().Snippets;

            Assert.AreEqual(2, snippets.Count(), "Should have two snippets");
            Assert.IsInstanceOfType(typeof(ModificationSnippet), snippets.First());
            Assert.IsInstanceOfType(typeof(ContextSnippet), snippets.ElementAt(1));
        }
Beispiel #9
0
        public void chunk_should_have_correct_snippets()
        {
            var diff     = Differ.Compare("one", fileOneContent, "two", fileTwoContent);
            var chunk    = diff.Chunks.First();
            var snippets = chunk.Snippets;

            var firstContext = snippets.ElementAtOrDefault(0) as ContextSnippet;

            Assert.IsNotNull(firstContext);
            Assert.AreEqual(3, firstContext.OriginalLines.Count());
            Assert.AreEqual("    {", firstContext.OriginalLines.ElementAt(0).Value);
            Assert.AreEqual("        Establish context = () =>", firstContext.OriginalLines.ElementAt(1).Value);
            Assert.AreEqual("        {", firstContext.OriginalLines.ElementAt(2).Value);
            Assert.AreEqual(0, firstContext.ModifiedLines.Count());

            var firstAddition = snippets.ElementAtOrDefault(1) as AdditionSnippet;

            Assert.IsNotNull(firstAddition);
            Assert.AreEqual(0, firstAddition.OriginalLines.Count());
            Assert.AreEqual(3, firstAddition.ModifiedLines.Count());
            Assert.AreEqual("            var despatcher = new DefaultDiagnosticMessageDespatcher();", firstAddition.ModifiedLines.ElementAt(0).Value);
            Assert.AreEqual("            despatcher.RegisterListener(new StubListener(x => results = x));", firstAddition.ModifiedLines.ElementAt(1).Value);
            Assert.AreEqual("", firstAddition.ModifiedLines.ElementAt(2).Value);

            var secondContext = snippets.ElementAtOrDefault(2) as ContextSnippet;

            Assert.IsNotNull(secondContext);
            Assert.AreEqual(1, secondContext.OriginalLines.Count());
            Assert.AreEqual("            model = new FluentNHibernate.PersistenceModel();", secondContext.OriginalLines.ElementAt(0).Value);
            Assert.AreEqual(0, secondContext.ModifiedLines.Count());

            var modification = snippets.ElementAtOrDefault(3) as ModificationSnippet;

            Assert.IsNotNull(modification);
            Assert.AreEqual(3, modification.OriginalLines.Count());
            Assert.AreEqual("            model.Diagnostics", modification.OriginalLines.ElementAt(0).Value);
            Assert.AreEqual("                .Enable()", modification.OriginalLines.ElementAt(1).Value);
            Assert.AreEqual("                .RegisterListener(new StubListener(x => results = x));", modification.OriginalLines.ElementAt(2).Value);
            Assert.AreEqual(1, modification.ModifiedLines.Count());
            Assert.AreEqual("            model.SetLogger(new DefaultDiagnosticLogger(despatcher));", modification.ModifiedLines.ElementAt(0).Value);

            var thirdContext = snippets.ElementAtOrDefault(4) as ContextSnippet;

            Assert.IsNotNull(thirdContext);
            Assert.AreEqual(3, thirdContext.OriginalLines.Count());
            Assert.AreEqual("        };", thirdContext.OriginalLines.ElementAt(0).Value);
            Assert.AreEqual("", thirdContext.OriginalLines.ElementAt(1).Value);
            Assert.AreEqual("        Because of = () =>", thirdContext.OriginalLines.ElementAt(2).Value);
            Assert.AreEqual(0, thirdContext.ModifiedLines.Count());
        }
        public void first_chunk_should_have_correct_snippets()
        {
            var diff     = Differ.Compare("one", fileOneContent, "two", fileTwoContent);
            var chunk    = diff.Chunks.First();
            var snippets = chunk.Snippets;

            var addition = snippets.ElementAtOrDefault(0) as AdditionSnippet;

            Assert.IsNotNull(addition);
            Assert.AreEqual(0, addition.OriginalLines.Count());
            Assert.AreEqual(1, addition.ModifiedLines.Count());
            Assert.AreEqual("zero", addition.ModifiedLines.First().Value);

            var context = snippets.ElementAtOrDefault(1) as ContextSnippet;

            Assert.IsNotNull(context);
            Assert.AreEqual(3, context.OriginalLines.Count());
            Assert.AreEqual("one", context.OriginalLines.ElementAt(0).Value);
            Assert.AreEqual("two", context.OriginalLines.ElementAt(1).Value);
            Assert.AreEqual("three", context.OriginalLines.ElementAt(2).Value);
            Assert.AreEqual(0, context.ModifiedLines.Count());
        }
        public void chunk_should_have_correct_snippets()
        {
            var diff     = Differ.Compare("one", fileOneContent, "two", fileTwoContent);
            var chunk    = diff.Chunks.First();
            var snippets = chunk.Snippets;

            var firstAddition = snippets.ElementAtOrDefault(0) as AdditionSnippet;

            Assert.IsNotNull(firstAddition);
            Assert.AreEqual(0, firstAddition.OriginalLines.Count());
            Assert.AreEqual(1, firstAddition.ModifiedLines.Count());
            Assert.AreEqual("zero", firstAddition.ModifiedLines.First().Value);

            var firstContext = snippets.ElementAtOrDefault(1) as ContextSnippet;

            Assert.IsNotNull(firstContext);
            Assert.AreEqual(3, firstContext.OriginalLines.Count());
            Assert.AreEqual("one", firstContext.OriginalLines.ElementAt(0).Value);
            Assert.AreEqual("two", firstContext.OriginalLines.ElementAt(1).Value);
            Assert.AreEqual("three", firstContext.OriginalLines.ElementAt(2).Value);
            Assert.AreEqual(0, firstContext.ModifiedLines.Count());

            var secondAddition = snippets.ElementAtOrDefault(2) as AdditionSnippet;

            Assert.IsNotNull(secondAddition);
            Assert.AreEqual(0, secondAddition.OriginalLines.Count());
            Assert.AreEqual(1, secondAddition.ModifiedLines.Count());
            Assert.AreEqual("three.5", secondAddition.ModifiedLines.First().Value);

            var secondContext = snippets.ElementAtOrDefault(3) as ContextSnippet;

            Assert.IsNotNull(secondContext);
            Assert.AreEqual(3, secondContext.OriginalLines.Count());
            Assert.AreEqual("four", secondContext.OriginalLines.ElementAt(0).Value);
            Assert.AreEqual("five", secondContext.OriginalLines.ElementAt(1).Value);
            Assert.AreEqual("six", secondContext.OriginalLines.ElementAt(2).Value);
            Assert.AreEqual(0, secondContext.ModifiedLines.Count());
        }
Beispiel #12
0
        public static void Benchmark()
        {
            var c1 = new C()
            {
                a = 666, b = 12, date = DateTime.Now, mynullable = DateTime.Now
            };
            var d1 = new D()
            {
                a = 666, b = 8, c = 9, date = DateTime.Now, mynullable = DateTime.Now
            };

            var fields = new[] { "a", "b" };

            WriteLine("Stats (microseconds) per iteration");

            RepeatBench("new Differ()", 1000, () =>
            {
                var dd = new Differ <C, D>(fields);
            });

            var d = new Differ <C, D>(fields);

            RepeatBench("Compare small objects", 1000000, () =>
            {
                var res = d.Compare(c1, d1);
            });
            RepeatBench("new FieldExtract()", 1000, () =>
            {
                var dd = new FieldExtract <C, int>(fields);
            });
            var extractor = new FieldExtract <C, int>(fields);

            RepeatBench("Extract integers", 1000000, () =>
            {
                var ints = extractor.Extract(c1);
            });

            // big data
            var big1 = new BigDto();
            var big2 = new BigDto();

            var bigprops     = ReflectionHelper.CollectProps <BigDto>();
            var bigpropnames = bigprops.SelectMany(p => p.Item2).ToArray();

            RepeatBench("new Differ() for big class", 100, () =>
            {
                var dd = new Differ <BigDto, BigDto>(bigpropnames);
            });

            var bigd = new Differ <BigDto, BigDto>(bigpropnames);

            RepeatBench("Compare large objects", 10000, () => { bigd.Compare(big1, big2); });

            var types = ReflectionHelper.CollectProps <BigDto>();
            var e4    = ReflectionHelper.GetExtractorFor <BigDto, int>(types);
            var e5    = ReflectionHelper.GetExtractorFor <BigDto, string>(types);
            var e6    = ReflectionHelper.GetExtractorFor <BigDto, decimal>(types);


            RepeatBench("Extract fields from large object", 10000, () =>
            {
                var r1 = e4.Extract(big1);
                var r2 = e5.Extract(big1);
                var r3 = e6.Extract(big1);
            });


            RepeatBench("Extract fields from large object, convert to dict, naive", 10000, () =>
            {
                var pd = e4.ResultsAsDict(e4.Extract(big1).Select(i => i.ToString()).ToList())
                         .Union(e5.ResultsAsDict(e5.Extract(big1).Select(e => e.ToString()).ToList()))
                         .Union(e6.ResultsAsDict(e6.Extract(big1).Select(e => e.ToString()).ToList()));
            });

            var boxedExtract = new FieldExtract <BigDto, object>(bigpropnames);

            RepeatBench("Extract fields, boxed", 100000, () =>
            {
                var r1 = boxedExtract.Extract(big1);
            });

            RepeatBench("Extract fields from large dto. string -> object dict", 10000, () =>
            {
                var r1 = boxedExtract.Extract(big1);
                var r2 = boxedExtract.ResultsAsZip(r1);
            });


            var propertyInfos = typeof(BigDto).GetProperties();

            RepeatBench("Extract fields with reflection", 10000, () =>
            {
                foreach (var p in propertyInfos)
                {
                    var val = p.GetValue(big1);
                }
            });

            RepeatBench("Extract fields with reflection, convert to string dict", 10000, () =>
            {
                var resdict = new Dictionary <string, string>();
                foreach (var p in propertyInfos)
                {
                    var val         = p.GetValue(big1);
                    var s           = val.ToString();
                    resdict[p.Name] = s;
                }
            });


            var copier = new FieldCopier <BigDto, BigDto>(bigpropnames);

            RepeatBench("Copy big object", 100000, () => { copier.Copy(big1, big2); });
        }
Beispiel #13
0
        public void should_have_one_chunk()
        {
            var diff = Differ.Compare("one", fileOneContent, "two", fileTwoContent);

            Assert.AreEqual(1, diff.Chunks.Count);
        }