Exemplo n.º 1
0
        public void Visit(DiffNode node, int param)
        {
            DrawLineBreak();

            string indent = BuildIndent(param);
            string action = ActionToString(node.DiffAction);

            _sb.AppendFormat("{0}<span{1}>&lt;{2}", indent, action, node.Raw.Name);

            if (node.DiffAction == null)
            {
                _sb.Append("</span>");
                foreach (DiffContent content in node.Content)
                {
                    content.Accept(this, param + 1);
                }
                DrawLineBreak();

                string closingTag = string.Format("{0}<span{1}>&lt;/{2}&gt;</span>", indent, action, node.Raw.Name);
                DrawLineBreak(openNew: param != 0, closingPrefix: closingTag);
                _closeLineWithoutPrefixMode = true;
            }
            else
            {
                AppendRawAttributesToSb(node, _sb);
                _sb.Append("/");
                _sb.Append("</span>");
            }
        }
Exemplo n.º 2
0
        public void Visit(DiffNode node, XdtContext param)
        {
            if (!node.IsChanged)
            {
                return;
            }

            XElement element = node.DiffAction == DiffAction.Added
                                ? new XElement(node.Raw)
                                : new XElement(node.Raw.Name, node.Raw.Attributes());

            var context = new XdtContext(element);

            if (node.DiffAction == null)
            {
                foreach (DiffContent item in node.Content)
                {
                    item.Accept(this, context);
                }
            }

            element.SetAttributeValue(XdtElement("Locator"), GetLocator(node, context));
            XElement transformationContext = context.XElement;

            foreach (string transformation in GetElementTransformations(node.DiffAction, context))
            {
                transformationContext = GetTransformationContext(transformationContext);
                transformationContext.SetAttributeValue(XdtElement("Transform"), transformation);
                param.XElement.Add(transformationContext);
            }
        }
        public void Visit(DiffNode node)
        {
            var right = Nodes.Pop();
            var left  = Nodes.Pop();

            Nodes.Push(new DiffNode(left, right));
        }
Exemplo n.º 4
0
        public void Xdt_ElementsHaveLocatorAndTransformWhenNeeded()
        {
            var destDoc = new XDocument(_simpleDoc);

            destDoc.Root.Element("appSettings").Elements().First().SetAttributeValue("value", "test");
            destDoc.Root.Element("appSettings").Add(new XElement("add", new XAttribute("key", "foo"), new XAttribute("value", "bar")));

            DiffNode output = _xmlDiff.Compare(_simpleDoc.Root, destDoc.Root);

            var visitor = new XdtVisitor();

            visitor.Visit(output);
            var result = XDocument.Parse(visitor.Result);

            // root element should have no locator or transformation
            Assert.IsNull(_FindXdtAttribute(result.Root));
            // same for appSettings
            Assert.IsNull(_FindXdtAttribute(result.Root.Element("appSettings")));
            // there should be two children - one updated, one inserted - the one that stays the same shouldn't appear in the transformation file
            var children = result.Root.Element("appSettings").Elements();

            Assert.AreEqual(children.Count(), 2);
            // there should be a Locator and a Transform on the changed setting
            Assert.AreEqual(_FindXdtAttribute(children.First(), "Locator").Value, "Match(key)");
            Assert.AreEqual(_FindXdtAttribute(children.First(), "Transform").Value, "SetAttributes(value)");
            // there should be a Transform on the new setting
            Assert.AreEqual(_FindXdtAttribute(children.Last(), "Transform").Value, "Insert");
            Assert.IsNull(_FindXdtAttribute(children.Last(), "Locator"));
        }
Exemplo n.º 5
0
        public void Compare_SingleText_NotAffectNestedElements()
        {
            var source =
                new XElement("level0",
                             new XElement("isProd", new XText("true"),
                                          new XElement("level2"),
                                          new XElement("level2", new XText("level2Text"),
                                                       new XElement("level3", new XText("level3Text"))),
                                          new XElement("level2")));

            var result =
                new XElement("level0",
                             new XElement("isProd", new XText("false"),
                                          new XElement("level2"),
                                          new XElement("level2", new XText("level2Text"),
                                                       new XElement("level3", new XText("level3Text"))),
                                          new XElement("level2")));

            DiffNode level0      = _xmlDiff.Compare(source, result);
            var      isProdLevel = level0.Childs.Single();

            Assert.AreEqual(2, isProdLevel.Values.Count());
            Assert.AreEqual(0, isProdLevel.Childs.Count());
            verifyText(DiffAction.Removed, isProdLevel.Values, "true");
            verifyText(DiffAction.Added, isProdLevel.Values, "false");
        }
Exemplo n.º 6
0
        private string GetLocator(DiffNode node, XdtContext context)
        {
            // todo: let's use an indexed locator always?
            // pros: less code/logic + it's hard to locate some changes <node attr="123"... to <node attr="newVal"...
            // cons: transformation becomes hard to read?
            if (node.DiffAction == DiffAction.Added)
            {
                return(null);
            }
            XElement rawElement = node.Raw;

            XElement[] prevSiblingsWithSameName = rawElement.ElementsBeforeSelf().Where(e => e.Name == rawElement.Name).ToArray();
            XElement[] nextSiblingsWithSameName = rawElement.ElementsAfterSelf().Where(e => e.Name == rawElement.Name).ToArray();
            XElement[] siblingsWithSameName     = prevSiblingsWithSameName.Concat(nextSiblingsWithSameName).ToArray();
            if (siblingsWithSameName.Length == 0)
            {
                return(null);
            }

            XName[] uniqueAttrNames = rawElement.Attributes()
                                      .Where(a => siblingsWithSameName.All(e => e.Attribute(a.Name)?.Value != a.Value))
                                      .Select(x => x.Name)
                                      .Except(context.SetAttrs.Concat(context.RemoveAttrs)).ToArray();
            return(uniqueAttrNames.Length == 0
                                ? "Condition([" + (prevSiblingsWithSameName.Length + 1) + "])"
                                : "Match(" + uniqueAttrNames[0] + ")");
        }
Exemplo n.º 7
0
        public void Compare_SingleAttributeDeepChange()
        {
            var source =
                new XElement("level0",
                             new XElement("level1",
                                          new XElement("level2"),
                                          new XElement("level2",
                                                       new XElement("changed", new XAttribute("isProd", false))),
                                          new XElement("level2")));

            var newElem = new XElement("changed", new XAttribute("isProd", true));
            var result  =
                new XElement("level0",
                             new XElement("level1",
                                          new XElement("level2"),
                                          new XElement("level2",
                                                       newElem),
                                          new XElement("level2")));

            DiffNode level0  = _xmlDiff.Compare(source, result);
            var      level1  = level0.Childs.Single();
            var      level2  = level1.Childs.Single();
            var      changed = level2.Childs.Single();

            Assert.AreEqual(newElem, changed.Raw);
            verifyAttribute(DiffAction.Removed, changed.Attributes, new XAttribute("isProd", false));
            verifyAttribute(DiffAction.Added, changed.Attributes, new XAttribute("isProd", true));
        }
        // Draw buttons to copy values from one SerializedProperty to another
        private void DrawCopyValueButtons(DiffNode node, Rect prop1Rect, Rect prop2Rect)
        {
            if (!node.prop1.serializedObject.targetObject || !node.prop2.serializedObject.targetObject)
            {
                return;
            }

            GetCopyValueButtonRects(ref prop1Rect, ref prop2Rect);

            if (GUI.Button(prop1Rect, COPY_TO_OBJ1_BUTTON))
            {
                object obj2Value = node.prop2.CopyValue();
                if (node.prop1.CanPasteValue(obj2Value, true))
                {
                    node.prop1.PasteValue(obj2Value, true);

                    RefreshDiff();
                    GUIUtility.ExitGUI();
                }
            }

            if (GUI.Button(prop2Rect, COPY_TO_OBJ2_BUTTON))
            {
                object obj1Value = node.prop1.CopyValue();
                if (node.prop2.CanPasteValue(obj1Value, true))
                {
                    node.prop2.PasteValue(obj1Value, true);

                    RefreshDiff();
                    GUIUtility.ExitGUI();
                }
            }
        }
Exemplo n.º 9
0
 public void CreateNodes()
 {
     if (ynUseGos == true)
     {
         if (parentTmpGo != null)
         {
             DestroyImmediate(parentTmpGo);
         }
         parentTmpGo = new GameObject("parentTmpGos");
     }
     for (int layer = 0; layer < layerNodeCounts.Count; layer++)
     {
         List <DiffNode> nodeLayer = new List <DiffNode>();
         nodeLayers.Add(nodeLayer);
         int numNodes = layerNodeCounts[layer];
         for (int n = 0; n < numNodes; n++)
         {
             DiffNode node = new DiffNode(layer, this);
             if (layer > 0)
             {
                 DiffNode nodeChild = nodeLayers[layer - 1][n];
                 nodeChild.nodeFrom = node;
                 nodeChild.CreateLink();
                 //
                 DiffNode nodeChild2 = nodeLayers[layer - 1][n + 1];
                 nodeChild2.nodeFrom = node;
                 nodeChild2.CreateLink();
             }
         }
     }
 }
Exemplo n.º 10
0
        public void Xdt_ChildrenOfInsertedElementsHaveNoXdtAttributes()
        {
            var destDoc = new XDocument(_simpleDoc);

            destDoc.Root.Add(new XElement("test",
                                          new XElement("example", new XAttribute("value", "1")),
                                          new XElement("example", new XAttribute("value", "2")),
                                          new XElement("another",
                                                       new XElement("example")
                                                       )
                                          ));

            DiffNode output = _xmlDiff.Compare(_simpleDoc.Root, destDoc.Root);

            var visitor = new XdtVisitor();

            visitor.Visit(output);
            var result = XDocument.Parse(visitor.Result);

            // there should be a Transform on the new element
            Assert.AreEqual(_FindXdtAttribute(result.Root.Element("test"), "Transform").Value, "Insert");
            // there should be no XDT attributes on the children of the new element
            Assert.IsTrue(result.Root.Element("test").HasElements);
            Assert.IsFalse(result.Root.Element("test").Elements().Any(e => _FindXdtAttribute(e) != null));
        }
Exemplo n.º 11
0
        public void Compare_SingleTextDeepChange()
        {
            var source =
                new XElement("level0",
                             new XElement("level1",
                                          new XElement("level2"),
                                          new XElement("level2",
                                                       new XElement("isProd", new XText("true"), new XElement("someelem"), new XText("!Bang"))),
                                          new XElement("level2")));

            var result =
                new XElement("level0",
                             new XElement("level1",
                                          new XElement("level2"),
                                          new XElement("level2",
                                                       new XElement("isProd", new XText("false"), new XElement("someelem"), new XText("!Bang"))),
                                          new XElement("level2")));

            DiffNode level0  = _xmlDiff.Compare(source, result);
            var      level1  = level0.Childs.Single();
            var      level2  = level1.Childs.Single();
            var      changed = level2.Childs.Single();

            verifyText(DiffAction.Removed, changed.Values, "true!Bang");
            verifyText(DiffAction.Added, changed.Values, "false!Bang");
        }
Exemplo n.º 12
0
        private RdlSyntaxNode ComposeEqualityOperators()
        {
            var node = ComposeArithmeticOperators(Precendence.Level1);

            while (IsEqualityOperator(Current))
            {
                switch (Current.TokenType)
                {
                case StatementType.GreaterEqual:
                    Consume(StatementType.GreaterEqual);
                    node = new GreaterEqualNode(node, ComposeEqualityOperators());
                    break;

                case StatementType.Greater:
                    Consume(StatementType.Greater);
                    node = new GreaterNode(node, ComposeEqualityOperators());
                    break;

                case StatementType.LessEqual:
                    Consume(StatementType.LessEqual);
                    node = new LessEqualNode(node, ComposeEqualityOperators());
                    break;

                case StatementType.Less:
                    Consume(StatementType.Less);
                    node = new LessNode(node, ComposeEqualityOperators());
                    break;

                case StatementType.Equality:
                    Consume(StatementType.Equality);
                    node = new EqualityNode(node, ComposeEqualityOperators());
                    break;

                case StatementType.Diff:
                    Consume(StatementType.Diff);
                    node = new DiffNode(node, ComposeEqualityOperators());
                    break;

                case StatementType.Between:
                    node = new BetweenNode(ConsumeAndGetToken(), node,
                                           ComposeAndSkip(f => f.ComposeArithmeticOperators(Precendence.Level1), StatementType.And),
                                           ComposeArithmeticOperators(Precendence.Level1));
                    break;

                case StatementType.Not:
                    Consume(StatementType.Not);
                    node = new NotNode(Current, node);
                    break;

                default:
                    throw new NotSupportedException();
                }
            }

            return(node);
        }
Exemplo n.º 13
0
        public void ToString_ReturnStringRepresentation()
        {
            var    child    = new DiffNode(DiffAction.Added, new XElement("child"));
            var    diffAttr = new DiffNode(new XElement("root"), new DiffContent[] { child });
            string result   = diffAttr.ToString();
            string expected = "= Element \"root\"\r\n" +
                              "...+ Element \"child\"\r\n";

            Assert.AreEqual(expected, result);
        }
Exemplo n.º 14
0
        public void IsChangedProperty_ShouldReturnTrueIfAnyOfChildsIsChanged()
        {
            var childs = new List <DiffContent>
            {
                new DiffNode(new XElement("child"), null),
                new DiffValue(DiffAction.Added, "value")
            };
            var diffNode = new DiffNode(new XElement("some"), childs);

            Assert.IsTrue(diffNode.IsChanged);
        }
Exemplo n.º 15
0
        private static string CellValueToString(object value)
        {
            if (value == null)
            {
                return(string.Empty);
            }

            // TODO: only allow reflection for all info?
            bool unused;

            return(DiffNode.ObjectToString(true, value, out unused));
        }
Exemplo n.º 16
0
        public void Compare_DifferentRootElements()
        {
            var sourceAttr = new XAttribute("value", 1);
            var source     = new XElement("root", sourceAttr);
            var resultAttr = new XAttribute("value", 2);
            var result     = new XElement("root", resultAttr);

            DiffNode output = _xmlDiff.Compare(source, result);

            Assert.AreEqual(result, output.Raw);
            verifyAttribute(DiffAction.Removed, output.Attributes, sourceAttr);
            verifyAttribute(DiffAction.Added, output.Attributes, resultAttr);
        }
Exemplo n.º 17
0
 private void AppendRawAttributesToSb(DiffNode node, StringBuilder sb)
 {
     if (node.Raw.HasAttributes)
     {
         foreach (XAttribute attr in node.Raw.Attributes().Take(MaxAttributesPreviewCount))
         {
             sb.AppendFormat(" \"{0}\"=\"{1}\"", attr.Name, attr.Value);
         }
         if (node.Raw.Attributes().Count() > MaxAttributesPreviewCount)
         {
             sb.AppendFormat("...");
         }
     }
 }
Exemplo n.º 18
0
        public void Xdt_TestChangedValue()
        {
            var destDoc = new XDocument(_simpleDoc);

            destDoc.Root.Element("appSettings").Elements().First().Value = "SomeVal";
            DiffNode output = _xmlDiff.Compare(_simpleDoc.Root, destDoc.Root);

            var visitor = new XdtVisitor();

            visitor.Visit(output);
            var result = XDocument.Parse(visitor.Result);

            Assert.AreEqual("Replace", _FindXdtAttribute(result.Root.Element("appSettings").Elements().First(), "Transform").Value);
            Assert.AreEqual(1, result.Root.Element("appSettings").Elements().Count());
            Assert.AreEqual("SomeVal", result.Root.Element("appSettings").Elements().First().Value);
        }
Exemplo n.º 19
0
        public void Xdt_SameDocument_ProducesBlankXdt()
        {
            DiffNode output = _xmlDiff.Compare(_simpleDoc.Root, new XDocument(_simpleDoc).Root);

            var visitor = new XdtVisitor();

            visitor.Visit(output);
            var result = XDocument.Parse(visitor.Result);

            // document should have a root element with the same name as in the original document
            Assert.AreEqual(result.Root.Name, _simpleDoc.Root.Name);
            // that root element should have the xdt prefix namespace
            Assert.AreEqual(result.Root.GetPrefixOfNamespace(XdtVisitor.XdtNamespaceUri), "xdt");
            // that root element should be empty, as there are no differences
            Assert.IsFalse(result.Root.HasElements);
        }
Exemplo n.º 20
0
        public void Xdt_TestSetAndRemoveAttributes()
        {
            var destDoc = new XDocument(_simpleDoc);

            destDoc.Root.Element("appSettings").Elements().First().SetAttributeValue("value", "baz");
            destDoc.Root.Element("appSettings").Elements().First().Attributes("key").Remove();
            DiffNode output = _xmlDiff.Compare(_simpleDoc.Root, destDoc.Root);

            var visitor = new XdtVisitor();

            visitor.Visit(output);
            var result = XDocument.Parse(visitor.Result);

            Assert.AreEqual(2, result.Root.Element("appSettings").Elements().Count());
            Assert.AreEqual("SetAttributes(value)", _FindXdtAttribute(result.Root.Element("appSettings").Elements().First(), "Transform").Value);
            Assert.AreEqual("RemoveAttributes(key)", _FindXdtAttribute(result.Root.Element("appSettings").Elements().Last(), "Transform").Value);
        }
Exemplo n.º 21
0
        // refactor later
        public void Visit(DiffNode node)
        {
            // TODO: should we reset internal variables / state so that the same visitor instance can be used for multiple diffs?
            XdtContext initial = Initial;

            Visit(node, initial);
            XElement res = initial.XElement.Nodes().OfType <XElement>().FirstOrDefault();

            if (res != null)
            {
                res.Add(new XAttribute(XNamespace.Xmlns + "xdt", XdtNamespaceUri));
            }
            else
            {
                res = new XElement(node.Raw.Name, new XAttribute(XNamespace.Xmlns + "xdt", XdtNamespaceUri));
            }
            _xdt.Add(res);
        }
Exemplo n.º 22
0
 public void Visit(DiffNode node, int level)
 {
     if (node.IsChanged)
     {
         string indent = BuildIndent(level);
         string action = ActionToString(node.DiffAction);
         _sb.AppendFormat("{0}{1} Element \"{2}\"", indent, action, node.Raw.Name);
         AppendRawAttributesToSb(node, _sb);
         _sb.Append("\r\n");
         if (node.DiffAction == null)
         {
             foreach (DiffContent content in node.Content)
             {
                 content.Accept(this, level + 1);
             }
         }
     }
 }
Exemplo n.º 23
0
    public void LoadInputLayerWithData()
    {
        List <DiffNode> nodeLayer = nodeLayers[0];
        int             numNodes  = nodeLayer.Count;

        if (numNodes == data.Length)
        {
            for (int n = 0; n < numNodes; n++)
            {
                DiffNode node  = nodeLayer[n];
                float    value = data[n];
                node.UpdateValue(value);
            }
            imageValues.Apply();
        }
        else
        {
            Debug.Log("LoadInput: data:" + data.Length + " / input:" + numNodes + " count mismatch!\n");
        }
    }
Exemplo n.º 24
0
        public void Xdt_TestRemoveUniqueElement()
        {
            var destDoc = new XDocument(_simpleDoc);

            destDoc.Root.Element("appSettings").Remove();

            DiffNode output = _xmlDiff.Compare(_simpleDoc.Root, destDoc.Root);

            var visitor = new XdtVisitor();

            visitor.Visit(output);
            var result = XDocument.Parse(visitor.Result);

            // root element should have no locator or transformation
            Assert.IsNull(_FindXdtAttribute(result.Root));
            // removed element should have a Transform but as it is unique, no Locator
            Assert.AreEqual(_FindXdtAttribute(result.Root.Element("appSettings"), "Transform").Value, "Remove");
            Assert.IsNull(_FindXdtAttribute(result.Root.Element("appSettings"), "Locator"));
            // as it is being removed, it should have no children in the xdt
            Assert.IsFalse(result.Root.Element("appSettings").HasElements);
        }
Exemplo n.º 25
0
 private void AppendRawAttributesToSb(DiffNode node, StringBuilder sb)
 {
     if (node.Raw.HasAttributes)
     {
         IEnumerator <XAttribute> enumerator = node.Raw.Attributes().GetEnumerator();
         int left = MaxAttributesPreviewCount;
         while (enumerator.MoveNext() && left > 0)
         {
             XAttribute attr = enumerator.Current;
             sb.AppendFormat("<span>\"{0}\"=\"{1}\"</span>", attr.Name, System.Security.SecurityElement.Escape(attr.Value));
             left--;
         }
         if (enumerator.MoveNext())
         {
             sb.Append("<span>...</span>");
         }
     }
     else
     {
         //sb.Append("<span>...</span>");
     }
 }
Exemplo n.º 26
0
        public void Xdt_TestRemoveAndSetAttribute()
        {
            var destDoc = new XDocument(_simpleDoc);

            destDoc.Root.Element("appSettings").Elements().First().Attributes("value").Remove();
            destDoc.Root.Element("appSettings").Elements().First().SetAttributeValue("key", "test_key");

            DiffNode output = _xmlDiff.Compare(_simpleDoc.Root, destDoc.Root);

            var visitor = new XdtVisitor();

            visitor.Visit(output);
            var result = XDocument.Parse(visitor.Result);

            // set and remove isn't possible in one go, so should be two instances with the same locator
            Assert.AreEqual(_FindXdtAttribute(result.Root.Element("appSettings").Elements().First(), "Transform").Value, "SetAttributes(key)");
            var second = result.Root.Element("appSettings").Elements().Skip(1).First();

            Assert.AreEqual(result.Root.Element("appSettings").Elements().First().Name, second.Name);
            Assert.AreEqual(_FindXdtAttribute(second, "Locator").Value, _FindXdtAttribute(result.Root.Element("appSettings").Elements().First(), "Locator").Value);
            Assert.AreEqual(_FindXdtAttribute(second, "Transform").Value, "RemoveAttributes(value)");
        }
Exemplo n.º 27
0
 public void FeedForward()
 {
     for (int layer = 1; layer < layerNodeCounts.Count; layer++)
     {
         List <DiffNode> nodeLayer = nodeLayers[layer];
         int             numNodes  = nodeLayer.Count;
         for (int n = 0; n < numNodes; n++)
         {
             DiffNode node   = nodeLayer[n];
             DiffNode nodeA  = nodeLayers[layer - 1][n];
             float    valueA = nodeA.value;
             DiffNode nodeB  = nodeLayers[layer - 1][n + 1];
             float    valueB = nodeB.value;
             float    value  = 0;
             if (IsDifferent(valueA, valueB) == true)
             {
                 value = 1;
             }
             node.UpdateValue(value);
         }
     }
     imageValues.Apply();
 }
Exemplo n.º 28
0
        public void Xdt_TestNonUniqueElementNoUnchangedAttribute()
        {
            var sourceDoc = new XDocument(_simpleDoc);

            sourceDoc.Root.Element("appSettings").AddFirst(new XElement("test"));
            var destDoc = new XDocument(sourceDoc);

            destDoc.Root.Element("appSettings").Elements("add").First().SetAttributeValue("value", "test value");
            destDoc.Root.Element("appSettings").Elements("add").First().SetAttributeValue("key", "test_key");

            DiffNode output = _xmlDiff.Compare(sourceDoc.Root, destDoc.Root);

            var visitor = new XdtVisitor();

            visitor.Visit(output);
            var result = XDocument.Parse(visitor.Result);

            // root element should have no locator or transformation
            Assert.IsNull(_FindXdtAttribute(result.Root));
            // changed element should be matched by a condition
            Assert.AreEqual(_FindXdtAttribute(result.Root.Element("appSettings").Elements("add").First(), "Transform").Value, "SetAttributes(key,value)");
            Assert.AreEqual(_FindXdtAttribute(result.Root.Element("appSettings").Elements("add").First(), "Locator").Value, "Condition([1])");
        }
 public void Visit(DiffNode node)
 {
     node.Left.Accept(this);
     node.Right.Accept(this);
     node.Accept(_visitor);
 }
Exemplo n.º 30
0
        public void IsChangedProperty_ShouldBeFalseIfNoDiffActionAndChildsSpecified()
        {
            var diffNode = new DiffNode(new XElement("some"), null);

            Assert.IsFalse(diffNode.IsChanged);
        }