コード例 #1
0
        public void Transform_TwoChildWithNull_ReturnNoChildNode()
        {
            var Transformr = _container.Resolve <TypeNodeTransformer>();
            var testData   = new TwoChildrenNode("root",
                                                 null,
                                                 null);
            var result = Transformr.Transform(testData);

            Assert.IsTrue(result != null && result.GetType().Name == "NoChildrenNode");
        }
コード例 #2
0
        public void Describe_TwoChildWithTwoLeaf_TwoChildWithTwoLeaf()
        {
            var describer = _container.Resolve <TextNodeDescriber>();
            var testData  = new TwoChildrenNode("root",
                                                new NoChildrenNode("leaf1"),
                                                new NoChildrenNode("leaf2"));
            var result = describer.Describe(testData);

            Assert.IsTrue(result != null && result.Contains("root") && result.Contains("leaf1") && result.Contains("leaf2"));
        }
コード例 #3
0
        public void DescribeTwoChildrenNodeTest()
        {
            var describer = NodeDescriberFactory.GetDescriber();

            var node1 = new TwoChildrenNode("node1", null, null);
            var node2 = new TwoChildrenNode("node2", new NoChildrenNode("node2.1"), null);
            var node3 = new TwoChildrenNode("node3", null, new NoChildrenNode("node3.2"));
            var node4 = new TwoChildrenNode("node4", new NoChildrenNode("node4.1"), new NoChildrenNode("node4.2"));

            Assert.AreEqual("new TwoChildrenNode(\"node1\")", describer.Describe(node1));
            Assert.AreEqual("new TwoChildrenNode(\"node2\",\n    new NoChildrenNode(\"node2.1\"))", describer.Describe(node2));
            Assert.AreEqual("new TwoChildrenNode(\"node3\",\n    new NoChildrenNode(\"node3.2\"))", describer.Describe(node3));
            Assert.AreEqual("new TwoChildrenNode(\"node4\",\n    new NoChildrenNode(\"node4.1\"),\n    new NoChildrenNode(\"node4.2\"))", describer.Describe(node4));
        }
コード例 #4
0
        public void Describe_Given_TwoChildrenNode_Returns_TwoChildrenNodeDescription()
        {
            //arrange
            var expected = "new TwoChildrenNode(\"root\",\n"
                           + Indentation + "new NoChildrenNode(\"leaf1\"),\n"
                           + Indentation + "new NoChildrenNode(\"leaf2\"))";
            var testData = new TwoChildrenNode("root",
                                               new NoChildrenNode("leaf1"),
                                               new NoChildrenNode("leaf2"));
            //act
            var actual = _nodeDescriber.Describe(testData);

            //assert
            Assert.AreEqual(expected, actual);
        }
コード例 #5
0
ファイル: NodeTransformer.cs プロジェクト: cjjjcj4/Node
 private Node TransformTwoChildrenNode(TwoChildrenNode node)
 {
     Node firstChild = node.FirstChild;
     Node secondChild = node.SecondChild;
     if (firstChild == null && secondChild == null)
     {
         return new NoChildrenNode(node.Name);
     } else if (firstChild == null && secondChild != null)
     {
         return new SingleChildNode(node.Name, Transform(secondChild));
     } else if(firstChild != null && secondChild == null)
     {
         return new SingleChildNode(node.Name, Transform(firstChild));
     }
     return node;
 }
コード例 #6
0
        /// <summary>
        /// Method which transforms the tree of nodes into a matching tree that uses the correct node types
        /// </summary>
        /// <param name="node">Source node</param>
        /// <returns>Transformed node</returns>
        public Node Transform(Node node)
        {
            if (node == null)
            {
                return(null);
            }
            if (!(node is ManyChildrenNode))
            {
                throw new Exception("Node must be of ManyChildrenNode type");
            }

            Node             result     = null;
            ManyChildrenNode sourceNode = (ManyChildrenNode)node;

            Node [] transformedChild = this.TransforChildren(sourceNode);

            switch (transformedChild.Length)
            {
            case 0:
                result = new NoChildrenNode(sourceNode.Name);
                break;

            case 1:
                result = new SingleChildNode(
                    sourceNode.Name,
                    transformedChild[0]
                    );
                break;

            case 2:
                result = new TwoChildrenNode(
                    sourceNode.Name,
                    transformedChild[0],
                    transformedChild[1]
                    );
                break;

            default:
                throw new Exception("Unknown transformation");
            }

            return(result);
        }
コード例 #7
0
 protected bool Equals(TwoChildrenNode other)
 {
     return(Equals(FirstChild, other.FirstChild) && Equals(SecondChild, other.SecondChild));
 }
コード例 #8
0
        public Node CreateTransformedNode(Node originNode)
        {
            var myList   = new List <Node>();
            var nodeType = originNode.GetType();
            //Check the real type of node
            var properties  = nodeType.GetProperties(BindingFlags.Public | BindingFlags.Instance);
            var isManyChild = false;
            var nodeCount   = 0;
            var nodeName    = string.Empty;

            foreach (var propertyInfo in properties)
            {
                var propertyValue = propertyInfo.GetValue(originNode, null);
                if (propertyValue != null)
                {
                    if (!(propertyValue is ValueType) && !(propertyValue is string))
                    {
                        var nodeClassType = typeof(Node);
                        //if node has IEnumerable type
                        isManyChild = typeof(IEnumerable).IsAssignableFrom(propertyInfo.PropertyType);
                        if (isManyChild)
                        {
                            foreach (var item in propertyValue as IEnumerable <Node> )
                            {
                                if (item != null && nodeClassType.IsAssignableFrom(item.GetType()))
                                {
                                    nodeCount++;
                                    myList.Add(item);
                                }
                            }
                        }

                        if (nodeClassType.IsAssignableFrom(propertyInfo.PropertyType))
                        {
                            nodeCount++;
                            myList.Add(propertyValue as Node);
                        }
                    }
                    else
                    {
                        nodeName = propertyValue.ToString();
                    }
                }
            }



            switch (nodeCount)
            {
            case 0:
                originNode = new NoChildrenNode(nodeName);
                break;

            case 1:
                originNode = new SingleChildNode(nodeName, CreateTransformedNode(myList[0]));
                break;

            case 2:
                originNode = new TwoChildrenNode(nodeName, CreateTransformedNode(myList[0]), CreateTransformedNode(myList[1]));
                break;

            default:

                break;
            }

            return(originNode);
        }