private static NTreeNode GenerateTreeWithCycle()
        {
            NTreeNode tree = GenerateTree();

            IEnumerable <NTreeNode> flat = new[] { tree };

            for (int i = 0; i < 3; i++)
            {
                flat = flat.Concat(flat.SelectMany(n => n.Children));
            }

            flat = flat.Distinct();

            NTreeNode loopFrom = flat.SingleOrDefault(n => n.Label == "K");
            NTreeNode loopTo   = flat.SingleOrDefault(n => n.Label == "B");

            loopFrom.Children = new[] { loopTo };

            return(tree);
        }
        public void WhenTraversingLoopingTree_WithCycleHandlingTruncate_FindsCorrectPaths()
        {
            NTreeNode tree = GenerateTreeWithCycle();

            IEnumerable <IEnumerable <string> > paths = TreeTraverser
                                                        .GetTraversalPaths(tree, node => node.Children, CyclicGraphBehaviour.Truncate)
                                                        .Select(path => path.Select(node => node.Label).ToArray())
                                                        .ToArray();

            string[][] expected = new[]
            {
                new[] { "A", "B", "D", "H" },
                new[] { "A", "B", "D", "I" },
                new[] { "A", "B", "E", "J" },
                new[] { "A", "C", "F", "L" },
                new[] { "A", "C", "F", "M" },
                new[] { "A", "C", "G", "N" },
                new[] { "A", "C", "G", "O" },
                new[] { "A", "B", "E", "K", "B" }
            };

            Assert.Equal(expected, paths);
        }
        public void WhenTraversingSimpleTree_FindsCorrectPaths()
        {
            NTreeNode tree = GenerateTree();

            IEnumerable <IEnumerable <string> > paths = TreeTraverser
                                                        .GetTraversalPaths(tree, node => node.Children)
                                                        .Select(path => path.Select(node => node.Label).ToArray())
                                                        .ToArray();

            string[][] expected = new[]
            {
                new[] { "A", "B", "D", "H" },
                new[] { "A", "B", "D", "I" },
                new[] { "A", "B", "E", "J" },
                new[] { "A", "B", "E", "K" },
                new[] { "A", "C", "F", "L" },
                new[] { "A", "C", "F", "M" },
                new[] { "A", "C", "G", "N" },
                new[] { "A", "C", "G", "O" },
            };

            Assert.Equal(expected, paths);
        }
        public void WhenTraversingLoopingTree_WithCycleHandlingThrow_ThrowsCyclicGraphException()
        {
            NTreeNode tree = GenerateTreeWithCycle();

            Assert.Throws <CyclicGraphException>(() => TreeTraverser.GetTraversalPaths(tree, node => node.Children, CyclicGraphBehaviour.Throw).ToList());
        }