示例#1
0
 static void AddResults <T>(List <DiffPair <T> > list, Slice <T> slice, DiffAction action)
 {
     for (int i = 0; i < slice.Length; i++)
     {
         list.Add(new DiffPair <T>(action, slice[i]));
     }
 }
示例#2
0
 private static Dictionary <IndexedName, RealNode> ParseChilds(XElement elem, DiffAction defaultAction)
 {
     return(elem.Elements()
            .Select(x => Parse(x, defaultAction))
            .GroupBy(x => x.Raw.Name)
            .Aggregate(Enumerable.Empty <KeyValuePair <IndexedName, RealNode> >(), ReduceGroup)
            .ToDictionary(x => x.Key, x => x.Value));
 }
 private static RealNode Parse(XElement elem, DiffAction defaultAction)
 {
     Dictionary<IndexedName, RealNode> childs = elem.HasElements
         ? ParseChilds(elem, defaultAction)
         : new Dictionary<IndexedName, RealNode>();
     Dictionary<XName, XAttribute> attributes = elem.Attributes().ToDictionary(x => x.Name, x => x);
     return new RealNode(defaultAction, elem, GetTextValue(elem), attributes, childs);
 }
 private static Dictionary<IndexedName, RealNode> ParseChilds(XElement elem, DiffAction defaultAction)
 {
     return elem.Elements()
             .Select(x => Parse(x, defaultAction))
             .GroupBy(x => x.Raw.Name)
             .Aggregate(Enumerable.Empty<KeyValuePair<IndexedName, RealNode>>(), ReduceGroup)
             .ToDictionary(x => x.Key, x => x.Value);
 }
示例#5
0
 public DiffOperation(DiffAction action, int startInOld, int endInOld, int startInNew, int endInNew)
 {
     this.Action     = action;
     this.StartInOld = startInOld;
     this.EndInOld   = endInOld;
     this.StartInNew = startInNew;
     this.EndInNew   = endInNew;
 }
示例#6
0
 public DiffOperation(DiffAction action, int startInOld, int endInOld, int startInNew, int endInNew)
 {
     Action     = action;
     StartInOld = startInOld;
     EndInOld   = endInOld;
     StartInNew = startInNew;
     EndInNew   = endInNew;
 }
示例#7
0
 public RealNode(DiffAction defaultAction, XElement raw, string value,
                 Dictionary <XName, XAttribute> attrs, Dictionary <IndexedName, RealNode> childs)
 {
     DefaultAction = defaultAction;
     Raw           = raw;
     Value         = value;
     Childs        = childs;
     Attributes    = attrs;
 }
示例#8
0
        private static RealNode Parse(XElement elem, DiffAction defaultAction)
        {
            Dictionary <IndexedName, RealNode> childs = elem.HasElements
                                ? ParseChilds(elem, defaultAction)
                                : new Dictionary <IndexedName, RealNode>();
            Dictionary <XName, XAttribute> attributes = elem.Attributes().ToDictionary(x => x.Name, x => x);

            return(new RealNode(defaultAction, elem, GetTextValue(elem), attributes, childs));
        }
示例#9
0
        public DiffValue(DiffAction action, string raw)
        {
            if (string.IsNullOrEmpty(raw))
            {
                throw new ArgumentNullException(nameof(raw));
            }

            Action = action;
            Raw    = raw;
        }
示例#10
0
 public void HandleAttr(XName attr, DiffAction diffAction)
 {
     if (diffAction == DiffAction.Added)
     {
         SetAttrs.Add(attr);
     }
     else if (diffAction == DiffAction.Removed)
     {
         RemoveAttrs.Add(attr);
     }
 }
示例#11
0
        private List <DiffOperation> Operations()
        {
            int startInOld             = 0;
            int startInNew             = 0;
            List <DiffOperation> list  = new List <DiffOperation>();
            List <DiffMatch>     list2 = MatchingBlocks();

            list2.Add(new DiffMatch(oldWords.Length, newWords.Length, 0));
            for (int i = 0; i < list2.Count; i++)
            {
                DiffMatch  match = list2[i];
                bool       flag  = startInOld == match.StartInOld;
                bool       flag2 = startInNew == match.StartInNew;
                DiffAction none  = DiffAction.none;
                if (!flag && !flag2)
                {
                    none = DiffAction.replace;
                }
                else if (flag && !flag2)
                {
                    none = DiffAction.insert;
                }
                else if (!flag)
                {
                    none = DiffAction.delete;
                }
                else
                {
                    none = DiffAction.none;
                }
                if (none != DiffAction.none)
                {
                    list.Add(new DiffOperation(none, startInOld, match.StartInOld, startInNew, match.StartInNew));
                }
                if (match.Size != 0)
                {
                    list.Add(new DiffOperation(DiffAction.equal, match.StartInOld, match.EndInOld, match.StartInNew, match.EndInNew));
                }
                startInOld = match.EndInOld;
                startInNew = match.EndInNew;
            }
            return(list);
        }
示例#12
0
        private void verifyAttribute(DiffAction action, IEnumerable <DiffAttribute> attrs, XAttribute expected)
        {
            var raws = attrs.Where(x => x.Action == action).Select(x => x.Raw);

            Assert.IsTrue(raws.Any(x => x.Name == expected.Name && x.Value == expected.Value));
        }
示例#13
0
 public DiffOperation(DiffAction action, int startInOld, int endInOld, int startInNew, int endInNew)
 {
     this.Action = action;
     this.StartInOld = startInOld;
     this.EndInOld = endInOld;
     this.StartInNew = startInNew;
     this.EndInNew = endInNew;
 }
 private static void RunDiffTool(string createdFilePath, string referenceFilePath)
 {
     DiffAction?.ExecuteDiffAction(createdFilePath, referenceFilePath);
 }
示例#15
0
        private void ComputeChangeRecords()
        {
            while (true)
            {
                // first check end-of-lists termination cases...
                if (_newNodes.Count == 0)
                {
                    // remaining old nodes are deleted
                    if (_oldNodes.Count > 0)
                    {
                        RecordDeleteOld(_oldNodes.Count);
                    }
                    break;
                }
                else if (_oldNodes.Count == 0)
                {
                    // remaining nodes were inserted
                    if (_newNodes.Count > 0)
                    {
                        RecordInsertNew(_newNodes.Count);
                    }
                    break;
                }
                else
                {
                    DiffAction action = GetNextAction();
                    switch (action.Operation)
                    {
                    case DiffOp.SkipBoth:
                        RemoveFirst(_oldNodes, action.Count);
                        RemoveFirst(_newNodes, action.Count);
                        break;

                    case DiffOp.ReduceOld:
                        ReplaceFirstWithChildren(_oldNodes);
                        break;

                    case DiffOp.ReduceNew:
                        ReplaceFirstWithChildren(_newNodes);
                        break;

                    case DiffOp.ReduceBoth:
                        ReplaceFirstWithChildren(_oldNodes);
                        ReplaceFirstWithChildren(_newNodes);
                        break;

                    case DiffOp.InsertNew:
                        RecordInsertNew(action.Count);
                        break;

                    case DiffOp.DeleteOld:
                        RecordDeleteOld(action.Count);
                        break;

                    case DiffOp.ReplaceOldWithNew:
                        RecordReplaceOldWithNew(action.Count, action.Count);
                        break;
                    }
                }
            }
        }
示例#16
0
        public void IsChangedProperty_ShouldBeTrueIfDiffActionSpecified(DiffAction action)
        {
            var diffNode = new DiffNode(action, new XElement("some"));

            Assert.IsTrue(diffNode.IsChanged);
        }
示例#17
0
 public DiffAttribute(DiffAction action, XAttribute raw)
 {
     Action = action;
     Raw    = raw ?? throw new ArgumentNullException(nameof(raw));
 }
示例#18
0
        private List <DiffOperation> Operations()
        {
            int positionInOld = 0, positionInNew = 0;
            List <DiffOperation> operations = new List <DiffOperation>();

            var matches = this.MatchingBlocks();

            matches.Add(new DiffMatch(this.oldWords.Length, this.newWords.Length, 0));

            for (int i = 0; i < matches.Count; i++)
            {
                var match = matches[i];

                bool matchStartsAtCurrentPositionInOld = (positionInOld == match.StartInOld);
                bool matchStartsAtCurrentPositionInNew = (positionInNew == match.StartInNew);

                DiffAction action = DiffAction.none;

                if (matchStartsAtCurrentPositionInOld == false &&
                    matchStartsAtCurrentPositionInNew == false)
                {
                    action = DiffAction.replace;
                }
                else if (matchStartsAtCurrentPositionInOld == true &&
                         matchStartsAtCurrentPositionInNew == false)
                {
                    action = DiffAction.insert;
                }
                else if (matchStartsAtCurrentPositionInOld == false &&
                         matchStartsAtCurrentPositionInNew == true)
                {
                    action = DiffAction.delete;
                }
                else // This occurs if the first few words are the same in both versions
                {
                    action = DiffAction.none;
                }

                if (action != DiffAction.none)
                {
                    operations.Add(
                        new DiffOperation(action,
                                          positionInOld,
                                          match.StartInOld,
                                          positionInNew,
                                          match.StartInNew));
                }

                if (match.Size != 0)
                {
                    operations.Add(new DiffOperation(
                                       DiffAction.equal,
                                       match.StartInOld,
                                       match.EndInOld,
                                       match.StartInNew,
                                       match.EndInNew));
                }

                positionInOld = match.EndInOld;
                positionInNew = match.EndInNew;
            }

            return(operations);
        }
示例#19
0
 public DiffPair(DiffAction action, T value)
 {
     this.Action = action;
     this.Value  = value;
 }
示例#20
0
        public void IsChangedProperty_ShouldAlwaysBeTrue(DiffAction action)
        {
            var diffVal = new DiffValue(action, Raw);

            Assert.IsTrue(diffVal.IsChanged);
        }
示例#21
0
 public DiffPair(DiffAction action, T value)
 {
     Action = action;
     Value  = value;
 }
示例#22
0
 public DiffNode(DiffAction action, XElement raw)
     : this(raw, null)
 {
     DiffAction = action;
 }
示例#23
0
        private void verifyText(DiffAction action, IEnumerable <DiffValue> texts, string expected)
        {
            var raws = texts.Where(x => x.Action == action).Select(x => x.Raw);

            Assert.IsTrue(raws.Any(x => x == expected));
        }
示例#24
0
 public Pair(T source, DiffAction sourceAction, T result, DiffAction resultAction)
     : this(source, result)
 {
     SourceAction = sourceAction;
     ResultAction = resultAction;
 }
示例#25
0
 private static void _AddResults <T>(List <DiffPair <T> > list, Slice <T> slice, DiffAction action)
 {
     list.AddRange(slice.Select(t => new DiffPair <T>(action, t)));
 }