Пример #1
0
        /// <summary>
        /// Text diffs the first line
        /// </summary>
        /// <param name="result"></param>
        /// <param name="firstText"></param>
        /// <param name="secondText"></param>
        /// <param name="firstBasePos">The starting position for the first text relative to the entire request/response text</param>
        /// <param name="secondBasePos">The starting position for the second text relative to the entire request/response text</param>
        private RequestsDifferResult DiffFirstLine(RequestsDifferResult result, string firstText, string secondText, int firstBasePos, int secondBasePos)
        {
            //extract the request lines and diff them
            int    nlIndex   = firstText.IndexOf('\n');
            string firstLine = String.Empty;

            if (nlIndex > -1)
            {
                firstLine = firstText.Substring(0, nlIndex);
            }

            nlIndex = secondText.IndexOf('\n');

            string secondLine = String.Empty;

            if (nlIndex > -1)
            {
                secondLine = secondText.Substring(0, nlIndex);
            }

            //do the diff
            WordsDiffer lineDiffer = new WordsDiffer();

            lineDiffer.AddTask(firstLine, firstBasePos);
            lineDiffer.AddTask(secondLine, secondBasePos);
            lineDiffer.DoDiff();

            result = AppendDiffs(result, lineDiffer);

            return(result);
        }
Пример #2
0
        public void WordsDiff()
        {
            string text1 = "first second;third1";
            string text2 = "second;first fourth-third";

            WordsDiffer differ = new WordsDiffer();

            DiffResult             result      = differ.DoDiff(text1, text2);
            IDiffObjectsCollection firstDiffs  = result.DifferencesForFirst;
            IDiffObjectsCollection secondDiffs = result.DifferencesForSecond;

            Assert.IsTrue(firstDiffs[0].ValueEquals(new WordDiffObject(0, 0, "first ")));
            Assert.IsTrue(firstDiffs[1].ValueEquals(new LetterDiffObject(0, 0, "1")));
            Assert.IsTrue(secondDiffs[0].ValueEquals(new WordDiffObject(0, 0, "first ")));
            Assert.IsTrue(secondDiffs[1].ValueEquals(new WordDiffObject(0, 0, "fourth-")));

            //test a sorted diff
            differ.Properties.Sorted = true;
            result = differ.DoDiff(text1, text2);

            firstDiffs  = result.DifferencesForFirst;
            secondDiffs = result.DifferencesForSecond;

            Assert.IsTrue(firstDiffs[0].ValueEquals(new LetterDiffObject(0, 0, "1")));
            Assert.IsTrue(secondDiffs[0].ValueEquals(new WordDiffObject(0, 0, "fourth-")));
        }
Пример #3
0
        /// <summary>
        /// Check if the values match and outputs a set of granular differences
        /// </summary>
        /// <param name="diffObject"></param>
        /// <param name="diffsForSelf"></param>
        /// <param name="diffsForArgument"></param>
        /// <returns></returns>
        public bool ValueMatches(IDiffObject diffObject,
                                 out IDiffObjectsCollection diffsForSelf,
                                 out IDiffObjectsCollection diffsForArgument,
                                 out IDiffObjectsCollection commonElements)
        {
            HeaderDiffObject otherObject = diffObject as HeaderDiffObject;

            diffsForSelf     = null;
            diffsForArgument = null;
            commonElements   = null;

            bool matches = false;

            if (otherObject != null)
            {
                matches = _headerLineHash == otherObject.HeaderLineHash;
                //if the values are different but the headers are the same
                if (!matches && String.Compare(_name, otherObject.Name, true) == 0)
                {
                    WordsDiffer valuesDiffer = new WordsDiffer();
                    valuesDiffer.AddTask(_values);
                    valuesDiffer.AddTask(otherObject.Values);
                    valuesDiffer.Properties.Sorted = true;
                    valuesDiffer.Properties.CaseInSensitiveSort = true;
                    double diffRatio = valuesDiffer.DoDiff(0, 1);

                    diffsForSelf     = valuesDiffer.GetResultingDifferences(0);
                    diffsForArgument = valuesDiffer.GetResultingDifferences(1);
                    commonElements   = valuesDiffer.GetResultingCommonElements(0);
                    matches          = diffRatio >= _valuesMinSimilarity;
                }
            }
            return(matches);
        }
Пример #4
0
        //[TestMethod]
        public void LargeDiffTask()
        {
            //generate two large chars strings
            string first  = Utils.RandomString(10010, 12000);
            string second = Utils.RandomString(9000, 12000);

            DateTime start = DateTime.Now;

            LettersDiffer lettersDiffer = new LettersDiffer();

            DiffResult res = lettersDiffer.DoDiff(first, second);

            DateTime end = DateTime.Now;

            TimeSpan delta = end.Subtract(start);

            Assert.IsTrue(delta.Minutes == 0 && delta.Seconds < 20,
                          String.Format("Diff time was {0}", delta));

            //generate two large word strings
            first  = GenerateWordsSequence(900);
            second = GenerateWordsSequence(1020);

            WordsDiffer wordsDiffer = new WordsDiffer();

            start = DateTime.Now;

            res = wordsDiffer.DoDiff(first, second);

            end = DateTime.Now;

            delta = end.Subtract(start);

            Assert.IsTrue(delta.Minutes == 0 && delta.Seconds < 20,
                          String.Format("Diff time was {0}", delta));
        }
Пример #5
0
        /// <summary>
        /// Diffs the POST data
        /// </summary>
        /// <param name="firstRequest"></param>
        /// <param name="secondRequest"></param>
        /// <returns></returns>
        private RequestsDifferResult DiffPostData(RequestsDifferResult result, string firstRequest, string secondRequest)
        {
            int firstPostStart = firstRequest.IndexOf("\r\n\r\n");

            if (firstPostStart == -1)
            {
                //try without \r
                firstPostStart = firstRequest.IndexOf("\n\n");
                if (firstPostStart != -1)
                {
                    firstPostStart += 2;
                }
            }
            else
            {
                firstPostStart += 4;
            }

            int secondPostStart = secondRequest.IndexOf("\r\n\r\n");

            if (secondPostStart == -1)
            {
                //try without \r
                secondPostStart = secondRequest.IndexOf("\n\n");
                if (secondPostStart != -1)
                {
                    secondPostStart += 2;
                }
            }
            else
            {
                secondPostStart += 4;
            }

            string firstPost  = String.Empty;
            string secondPost = String.Empty;

            //construct the post data
            if (firstPostStart > -1)
            {
                firstPost = firstRequest.Substring(firstPostStart, firstRequest.Length - firstPostStart);
            }

            if (secondPostStart > -1)
            {
                secondPost = secondRequest.Substring(secondPostStart, secondRequest.Length - secondPostStart);
            }

            //create a word differ
            WordsDiffer wordsDiffer = new WordsDiffer();

            wordsDiffer.Properties.Sorted = true;
            wordsDiffer.Properties.CaseInSensitiveSort = true;

            wordsDiffer.AddTask(firstPost, firstPostStart);
            wordsDiffer.AddTask(secondPost, secondPostStart);

            wordsDiffer.DoDiff();

            result = AppendDiffs(result, wordsDiffer);

            return(result);
        }