Beispiel #1
0
        /// <summary>
        /// Retrieves the path of the target node if the given source node content holds a reference or a sequence of references, or the given source node path otherwise.
        /// </summary>
        /// <param name="sourceNode">The source node for which to retrieve the target node.</param>
        /// <param name="index">The index of the target node to retrieve, if the source node contains a sequence of references. <see cref="Index.Empty"/> otherwise.</param>
        /// <param name="sourceNodePath">The path to the given <paramref name="sourceNode"/>.</param>
        /// <returns>The path to the corresponding target node if available, or the path to source node itself if it does not contain any reference or if its content should not process references.</returns>
        /// <remarks>This method can return null if the target node is null.</remarks>
        protected static GraphNodePath GetTargetNodePath(IGraphNode sourceNode, Index index, GraphNodePath sourceNodePath)
        {
            if (sourceNode == null)
            {
                throw new ArgumentNullException(nameof(sourceNode));
            }
            if (sourceNodePath == null)
            {
                throw new ArgumentNullException(nameof(sourceNodePath));
            }

            var objectReference = sourceNode.Content.Reference as ObjectReference;

            if (objectReference != null)
            {
                return(sourceNodePath.PushTarget());
            }

            var referenceEnumerable = sourceNode.Content.Reference as ReferenceEnumerable;

            if (referenceEnumerable != null && !index.IsEmpty)
            {
                return(sourceNodePath.PushIndex(index));
            }

            return(sourceNodePath.Clone());
        }
Beispiel #2
0
 public void TestCloneNewRoot()
 {
     var obj1 = new Class { ClassMember = new Class(), ListMember = { new Class(), new Class(), new Class() } };
     var obj2 = new Class { ClassMember = new Class(), ListMember = { new Class(), new Class(), new Class() } };
     var nodeContainer = new NodeContainer();
     var newRoot = nodeContainer.GetOrCreateNode(obj2);
     var path1 = new GraphNodePath(nodeContainer.GetOrCreateNode(obj1));
     var clone = path1.Clone(newRoot);
     Assert.AreNotEqual(newRoot, path1.RootNode);
     Assert.AreEqual(newRoot, clone.RootNode);
     Assert.AreEqual(path1.IsValid, clone.IsValid);
     Assert.AreEqual(path1.IsEmpty, clone.IsEmpty);
     var path2 = path1.PushMember(nameof(Class.ClassMember)).PushTarget().PushMember(nameof(Class.IntMember));
     clone = path2.Clone(newRoot);
     Assert.AreNotEqual(newRoot, path2.RootNode);
     Assert.AreEqual(newRoot, clone.RootNode);
     Assert.AreEqual(path2.IsValid, clone.IsValid);
     Assert.AreEqual(path2.IsEmpty, clone.IsEmpty);
     var path3 = path1.PushMember(nameof(Class.ListMember)).PushIndex(new Index(1)).PushMember(nameof(Class.IntMember));
     clone = path3.Clone(newRoot);
     Assert.AreNotEqual(newRoot, path3.RootNode);
     Assert.AreEqual(newRoot, clone.RootNode);
     Assert.AreEqual(path3.IsValid, clone.IsValid);
     Assert.AreEqual(path3.IsEmpty, clone.IsEmpty);
 }
Beispiel #3
0
        public void TestCloneNewRoot()
        {
            var obj1 = new Class {
                ClassMember = new Class(), ListMember = { new Class(), new Class(), new Class() }
            };
            var obj2 = new Class {
                ClassMember = new Class(), ListMember = { new Class(), new Class(), new Class() }
            };
            var nodeContainer = new NodeContainer();
            var newRoot       = nodeContainer.GetOrCreateNode(obj2);
            var path1         = new GraphNodePath(nodeContainer.GetOrCreateNode(obj1));
            var clone         = path1.Clone(newRoot);

            Assert.AreNotEqual(newRoot, path1.RootNode);
            Assert.AreEqual(newRoot, clone.RootNode);
            Assert.AreEqual(path1.IsValid, clone.IsValid);
            Assert.AreEqual(path1.IsEmpty, clone.IsEmpty);
            var path2 = path1.PushMember(nameof(Class.ClassMember)).PushTarget().PushMember(nameof(Class.IntMember));

            clone = path2.Clone(newRoot);
            Assert.AreNotEqual(newRoot, path2.RootNode);
            Assert.AreEqual(newRoot, clone.RootNode);
            Assert.AreEqual(path2.IsValid, clone.IsValid);
            Assert.AreEqual(path2.IsEmpty, clone.IsEmpty);
            var path3 = path1.PushMember(nameof(Class.ListMember)).PushIndex(new Index(1)).PushMember(nameof(Class.IntMember));

            clone = path3.Clone(newRoot);
            Assert.AreNotEqual(newRoot, path3.RootNode);
            Assert.AreEqual(newRoot, clone.RootNode);
            Assert.AreEqual(path3.IsValid, clone.IsValid);
            Assert.AreEqual(path3.IsEmpty, clone.IsEmpty);
        }
Beispiel #4
0
        public void TestClone()
        {
            var obj = new Class {
                ClassMember = new Class(), ListMember = { new Class(), new Class(), new Class() }
            };
            var nodeContainer = new NodeContainer();
            var path1         = new GraphNodePath(nodeContainer.GetOrCreateNode(obj));
            var clone         = path1.Clone();

            AssertAreEqual(path1, clone);
            AssertAreEqual(path1.GetHashCode(), clone.GetHashCode());
            AssertAreEqual(path1.RootNode, clone.RootNode);
            AssertAreEqual(path1.IsValid, clone.IsValid);
            AssertAreEqual(path1.IsEmpty, clone.IsEmpty);
            AssertAreEqual(path1.GetNode(), clone.GetNode());
            var path2 = path1.PushMember(nameof(Class.ClassMember)).PushTarget().PushMember(nameof(Class.IntMember));

            clone = path2.Clone();
            AssertAreEqual(path2, clone);
            AssertAreEqual(path2.RootNode, clone.RootNode);
            AssertAreEqual(path2.IsValid, clone.IsValid);
            AssertAreEqual(path2.IsEmpty, clone.IsEmpty);
            AssertAreEqual(path2.GetNode(), clone.GetNode());
            var path3 = path1.PushMember(nameof(Class.ListMember)).PushIndex(new Index(1)).PushMember(nameof(Class.IntMember));

            clone = path3.Clone();
            AssertAreEqual(path3, clone);
            AssertAreEqual(path3.RootNode, clone.RootNode);
            AssertAreEqual(path3.IsValid, clone.IsValid);
            AssertAreEqual(path3.IsEmpty, clone.IsEmpty);
            AssertAreEqual(path3.GetNode(), clone.GetNode());
        }
 public void TestClone()
 {
     var obj = new Class { ClassMember = new Class(), ListMember = { new Class(), new Class(), new Class() } };
     var nodeContainer = new NodeContainer();
     var path1 = new GraphNodePath(nodeContainer.GetOrCreateNode(obj));
     var clone = path1.Clone();
     AssertAreEqual(path1, clone);
     AssertAreEqual(path1.GetHashCode(), clone.GetHashCode());
     AssertAreEqual(path1.RootNode, clone.RootNode);
     AssertAreEqual(path1.IsValid, clone.IsValid);
     AssertAreEqual(path1.IsEmpty, clone.IsEmpty);
     AssertAreEqual(path1.GetNode(), clone.GetNode());
     var path2 = path1.PushMember(nameof(Class.ClassMember)).PushTarget().PushMember(nameof(Class.IntMember));
     clone = path2.Clone();
     AssertAreEqual(path2, clone);
     AssertAreEqual(path2.RootNode, clone.RootNode);
     AssertAreEqual(path2.IsValid, clone.IsValid);
     AssertAreEqual(path2.IsEmpty, clone.IsEmpty);
     AssertAreEqual(path2.GetNode(), clone.GetNode());
     var path3 = path1.PushMember(nameof(Class.ListMember)).PushIndex(new Index(1)).PushMember(nameof(Class.IntMember));
     clone = path3.Clone();
     AssertAreEqual(path3, clone);
     AssertAreEqual(path3.RootNode, clone.RootNode);
     AssertAreEqual(path3.IsValid, clone.IsValid);
     AssertAreEqual(path3.IsEmpty, clone.IsEmpty);
     AssertAreEqual(path3.GetNode(), clone.GetNode());
 }
Beispiel #6
0
        public void TestSimpleObjectInitialPath()
        {
            var nodeContainer = new NodeContainer();
            var instance      = new SimpleClass {
                Member1 = 3, Member2 = new SimpleClass()
            };
            var rootNode  = nodeContainer.GetOrCreateNode(instance);
            var container = new SimpleClass {
                Member2 = instance
            };
            var containerNode = nodeContainer.GetOrCreateNode(container);
            var initialPath   = new GraphNodePath(containerNode);

            initialPath.PushMember(nameof(SimpleClass.Member2));
            initialPath.PushTarget();
            var visitor = new TestVisitor();

            visitor.Visit(rootNode, null, initialPath);
            var expectedNodes = new IGraphNode[]
            {
                rootNode,
                rootNode[nameof(SimpleClass.Member1)],
                rootNode[nameof(SimpleClass.Member2)],
                rootNode[nameof(SimpleClass.Member2)].Target,
                rootNode[nameof(SimpleClass.Member2)].Target[nameof(SimpleClass.Member1)],
                rootNode[nameof(SimpleClass.Member2)].Target[nameof(SimpleClass.Member2)],
            };
            var expectedPaths = new GraphNodePath[6];

            expectedPaths[0] = initialPath.Clone();
            expectedPaths[1] = initialPath.Clone();
            expectedPaths[1].PushMember(nameof(SimpleClass.Member1));
            expectedPaths[2] = initialPath.Clone();
            expectedPaths[2].PushMember(nameof(SimpleClass.Member2));
            expectedPaths[3] = expectedPaths[2].Clone();
            expectedPaths[3].PushTarget();
            expectedPaths[4] = expectedPaths[3].Clone();
            expectedPaths[4].PushMember(nameof(SimpleClass.Member1));
            expectedPaths[5] = expectedPaths[3].Clone();
            expectedPaths[5].PushMember(nameof(SimpleClass.Member2));
            VerifyNodesAndPath(expectedNodes, expectedPaths, visitor);
        }