public void Should_Group_Parallel_Nodes()
            {
                var graph = new CakeGraph();

                graph.Connect("1", "2");
                graph.Connect("1", "3");
                graph.Connect("1", "4");
                graph.Connect("2", "5");
                graph.Connect("3", "5");
                graph.Connect("4", "5");

                var result = graph.TraverseAndGroup("5").ToArray();

                Assert.Equal(3, result.Length);

                Assert.Equal(1, result[0].Count());
                Assert.Equal("1", result[0].First());

                Assert.Equal(3, result[1].Count());
                Assert.Equal("2", result[1].First());
                Assert.Equal("3", result[1].Skip(1).First());
                Assert.Equal("4", result[1].Skip(2).First());

                Assert.Equal(1, result[2].Count());
                Assert.Equal("5", result[2].First());
            }
            public void Should_Not_Find_Non_Existing_Node_In_Graph()
            {
                // Given
                var graph = new CakeGraph();

                graph.Add("start");

                // When, Then
                Assert.False(graph.Exist("other"));
            }
            public void Should_Find_Node_In_Graph_Regardless_Of_Casing()
            {
                // Given
                var graph = new CakeGraph();

                graph.Add("start");

                // When, Then
                Assert.True(graph.Exist("START"));
            }
            public void Should_Find_Node_In_Graph()
            {
                // Given
                var graph = new CakeGraph();

                graph.Add("start");

                // When, Then
                Assert.True(graph.Exist("start"));
            }
            public void Should_Add_Node_To_Graph()
            {
                // Given
                var graph = new CakeGraph();

                // When
                graph.Add("start");

                // Then
                Assert.Equal(1, graph.Nodes.Count);
            }
            public void Should_Return_Empty_If_No_Nodes()
            {
                // Given
                var graph = new CakeGraph();

                // When
                var result = graph.Traverse("D").ToArray();

                // Then
                Assert.Equal(0, result.Length);
            }
            public void Should_Throw_If_Provided_Node_Is_Null()
            {
                // Given
                var graph = new CakeGraph();

                // When
                var result = Record.Exception(() => graph.Add(null));

                // Then
                Assert.IsArgumentNullException(result, "node");
            }
Exemple #8
0
        /// <summary>
        /// Throws an <see cref="CakeException"/> if the specified node doesn't exist.
        /// </summary>
        /// <param name="target">The target node.</param>
        /// <param name="graph">The node graph.</param>
        /// <exception cref="CakeException">An exception of this type is thrown if the node isn't found.</exception>
        protected static void ThrowIfTargetNotFound(string target, CakeGraph graph)
        {
            if (graph.Exist(target))
            {
                return;
            }

            const string format = "The target '{0}' was not found.";

            throw new CakeException(string.Format(CultureInfo.InvariantCulture, format, target));
        }
            public void Should_Return_Empty_If_Not_Found()
            {
                var graph = new CakeGraph();

                graph.Connect("A", "B");
                graph.Connect("B", "C");

                var result = graph.Traverse("E").ToArray();

                Assert.Equal(0, result.Length);
            }
Exemple #10
0
            public void Should_Throw_If_Provided_Node_Is_Null()
            {
                // Given
                var graph = new CakeGraph();

                // When
                var exception = Record.Exception(() => graph.Add(null));

                // Then
                Assert.IsType <ArgumentNullException>(exception);
                Assert.Equal("node", ((ArgumentNullException)exception).ParamName);
            }
Exemple #11
0
            public void Should_Throw_If_Edge_Is_Reflexive()
            {
                // Given
                var graph = new CakeGraph();

                // When
                var result = Record.Exception(() => graph.Connect("start", "start"));

                // Then
                Assert.IsType <CakeException>(result);
                Assert.Equal("Reflexive edges in graph are not allowed.", result.Message);
            }
Exemple #12
0
            public void Should_Create_Edge_Between_Connected_Nodes()
            {
                // Given
                var graph = new CakeGraph();

                // When
                graph.Connect("start", "end");

                // Then
                Assert.Equal("start", graph.Edges[0].Start);
                Assert.Equal("end", graph.Edges[0].End);
            }
Exemple #13
0
            public void Should_Not_Create_Edge_Between_Connected_Nodes_If_An_Edge_Already_Exist_Regardless_Of_Casing()
            {
                // Given
                var graph = new CakeGraph();

                graph.Connect("start", "end");

                // When
                graph.Connect("START", "END");

                // Then
                Assert.Equal(1, graph.Edges.Count);
            }
Exemple #14
0
            public void Should_Throw_If_Encountering_Circular_Reference()
            {
                var graph = new CakeGraph();

                graph.Connect("A", "B");
                graph.Connect("B", "C");
                graph.Connect("C", "A");

                var result = Record.Exception(() => graph.Traverse("C"));

                Assert.IsType <CakeException>(result);
                Assert.Equal("Graph contains circular references.", result.Message);
            }
Exemple #15
0
            public void Should_Add_End_Node_If_Missing_To_Node_Collection()
            {
                // Given
                var graph = new CakeGraph();

                graph.Add("start");

                // When
                graph.Connect("start", "end");

                // Then
                Assert.Equal(2, graph.Nodes.Count);
            }
Exemple #16
0
            public void Should_Throw_If_Edge_Is_Unidirectional_Regardless_Of_Casing()
            {
                // Given
                var graph = new CakeGraph();

                graph.Connect("start", "end");

                // When
                var result = Record.Exception(() => graph.Connect("END", "START"));

                // Then
                Assert.IsType <CakeException>(result);
                Assert.Equal("Unidirectional edges in graph are not allowed.", result.Message);
            }
Exemple #17
0
            public void Should_Throw_If_Edge_Is_Unidirectional()
            {
                // Given
                var graph = new CakeGraph();

                graph.Connect("start", "end");

                // When
                var exception = Record.Exception(() => graph.Connect("end", "start"));

                // Then
                Assert.IsType <CakeException>(exception);
                Assert.Equal("Unidirectional edges in graph are not allowed.", exception.Message);
            }
Exemple #18
0
            public void Should_Throw_If_Edge_Is_Unidirectional_Regardless_Of_Casing()
            {
                // Given
                var graph = new CakeGraph();

                graph.Connect("start", "end");

                // When
                var result = Record.Exception(() => graph.Connect("END", "START"));

                // Then
                Assert.IsType <CakeException>(result);
                Assert.Equal($"Unidirectional edges in graph are not allowed.{Environment.NewLine}\"start\" and \"end\" cannot depend on each other.", result?.Message);
            }
Exemple #19
0
            public void Should_Throw_If_Node_Already_Is_Present_In_Graph()
            {
                // Given
                var graph = new CakeGraph();

                graph.Add("start");

                // When
                var result = Record.Exception(() => graph.Add("start"));

                // Then
                Assert.IsType <CakeException>(result);
                Assert.Equal("Node has already been added to graph.", result.Message);
            }
Exemple #20
0
            public void Should_Not_Group_NonParallel_Nodes()
            {
                var graph = new CakeGraph();

                graph.Connect("A", "B");
                graph.Connect("B", "C");

                var result = graph.Traverse("C").ToArray();

                Assert.Equal(3, result.Length);
                Assert.Equal("A", result[0]);
                Assert.Equal("B", result[1]);
                Assert.Equal("C", result[2]);
            }
Exemple #21
0
            public void Should_Return_Empty_Collection_Of_Nodes_If_Target_Was_Not_Found()
            {
                // Given
                var graph = new CakeGraph();

                graph.Connect("A", "B");
                graph.Connect("C", "D");
                graph.Connect("B", "C");

                // When
                var result = graph.Traverse("E").ToArray();

                // Then
                Assert.Equal(0, result.Length);
            }
Exemple #22
0
            public void Should_Traverse_Graph_In_Correct_Order_Regardless_Of_Casing_Of_Root()
            {
                // Given
                var graph = new CakeGraph();

                graph.Connect("A", "B");
                graph.Connect("C", "D");
                graph.Connect("B", "C");

                // When
                var result = graph.Traverse("d").ToArray();

                // Then
                Assert.Equal(4, result.Length);
                Assert.Equal("A", result[0]);
                Assert.Equal("B", result[1]);
                Assert.Equal("C", result[2]);
                Assert.Equal("d", result[3]);
            }
Exemple #23
0
            public void Should_Skip_Nodes_That_Are_Not_On_The_Way_To_The_Target()
            {
                // Given
                var graph = new CakeGraph();

                graph.Connect("A", "B");
                graph.Connect("B", "C");
                graph.Connect("B", "D");
                graph.Connect("D", "E");

                // When
                var result = graph.Traverse("E").ToArray();

                // Then
                Assert.Equal(4, result.Length);
                Assert.Equal("A", result[0]);
                Assert.Equal("B", result[1]);
                Assert.Equal("D", result[2]);
                Assert.Equal("E", result[3]);
            }
Exemple #24
0
 public ParallelGraphExtensionTests()
 {
     _sb    = new StringBuilder();
     _tasks = new List <CakeTask>(defineTasks());
     _graph = ParallelGraphBuilder.Build(_tasks);
 }