public void CanConnect_Should_ReturnFalse_WhenDirectionsIncorrect()
        {
            var graph = new GraphModel(GraphTypeWithConnections);

            var node1    = new NodeModel(Substitute.For <INodeType>());
            var portIn1  = PortModel.Create <int>("In", PortDirection.Input);
            var portOut1 = PortModel.Create <int>("Out", PortDirection.Output);

            node1.AddPort(portIn1);
            node1.AddPort(portOut1);

            var node2    = new NodeModel(Substitute.For <INodeType>());
            var portIn2  = PortModel.Create <int>("In", PortDirection.Input);
            var portOut2 = PortModel.Create <int>("Out", PortDirection.Output);

            node2.AddPort(portIn2);
            node2.AddPort(portOut2);

            graph.AddNode(node1);
            graph.AddNode(node2);

            Assert.That(graph.CanConnect(portIn1, portIn2), Is.False);     // input input
            Assert.That(graph.CanConnect(portOut1, portOut2), Is.False);   // output output
            Assert.That(graph.CanConnect(portIn1, portOut2), Is.False);    // input output
        }
Ejemplo n.º 2
0
        public void CreateT_Should_Succeed_InputWithCapacity()
        {
            var inputPort = PortModel.Create <string>("In", PortDirection.Input, PortCapacity.Multiple);

            Assert.That(inputPort.Id, Is.EqualTo("In"));
            Assert.That(inputPort.Type, Is.EqualTo(typeof(string)));
            Assert.That(inputPort.Direction, Is.EqualTo(PortDirection.Input));
            Assert.That(inputPort.Capacity, Is.EqualTo(PortCapacity.Multiple));
        }
Ejemplo n.º 3
0
        public void CreateT_Should_Succeed_OutputWithoutCapacity()
        {
            var outputPort = PortModel.Create <string>("Out", PortDirection.Output);

            Assert.That(outputPort.Id, Is.EqualTo("Out"));
            Assert.That(outputPort.Type, Is.EqualTo(typeof(string)));
            Assert.That(outputPort.Direction, Is.EqualTo(PortDirection.Output));
            Assert.That(outputPort.Capacity, Is.EqualTo(PortCapacity.Multiple));
        }
Ejemplo n.º 4
0
        public void Properties_Should_Throw_OnSecondSet()
        {
            var port = PortModel.Create <string>("In", PortDirection.Input);

            port.Node = new NodeModel(Substitute.For <INodeType>());

            Assert.That(() => port.Node = new NodeModel(Substitute.For <INodeType>()), Throws.InvalidOperationException);
            Assert.That(() => port.Node = null, Throws.Nothing);
        }
        public void Connect_Should_Throw_WhenNullPorts()
        {
            var graph = new GraphModel(GraphTypeWithConnections);
            var port  = PortModel.Create <string>("", PortDirection.Input);

            Assert.That(() => graph.Connect(null, null), Throws.ArgumentNullException);
            Assert.That(() => graph.Connect(port, null), Throws.ArgumentNullException);
            Assert.That(() => graph.Connect(null, port), Throws.ArgumentNullException);
        }
Ejemplo n.º 6
0
        public void AddPort_Should_Throw_WhenIdIsOccupied()
        {
            var node  = new NodeModel(Substitute.For <INodeType>());
            var port1 = PortModel.Create <string>("Id", PortDirection.Input);
            var port2 = PortModel.Create <string>("Id", PortDirection.Input);

            node.AddPort(port1);

            Assert.That(() => node.AddPort(port2), Throws.ArgumentException);
        }
        public void CanConnect_Should_ReturnFalse_WhenSameNode()
        {
            var graph = new GraphModel(GraphTypeWithConnections);

            var node1 = new NodeModel(Substitute.For <INodeType>());
            var port1 = PortModel.Create <string>("1", PortDirection.Output);
            var port2 = PortModel.Create <string>("2", PortDirection.Input);

            node1.AddPort(port1);
            node1.AddPort(port2);

            graph.AddNode(node1);

            Assert.That(graph.CanConnect(port1, port2), Is.False);
        }
Ejemplo n.º 8
0
        public void AddPort_Should_Succeed()
        {
            var node = new NodeModel(Substitute.For <INodeType>());

            var port = PortModel.Create <string>("Id", PortDirection.Input);

            NodeModel.NodePortsChangedArgs args = default;
            node.PortsChanged += e => { args = e; };
            node.AddPort(port);

            Assert.That(node.Ports, Does.Contain(port));
            Assert.That(port.Node, Is.EqualTo(node));
            Assert.That(args.Node, Is.EqualTo(node));
            Assert.That(args.Port, Is.EqualTo(port));
            Assert.That(args.IsAdded, Is.True);
        }
        public void CanConnect_Should_ReturnFalse_WhenPortsNotInGraph()
        {
            var graph = new GraphModel(GraphTypeWithConnections);

            var nodeType = Substitute.For <INodeType>();
            var node1    = new NodeModel(nodeType);
            var node2    = new NodeModel(nodeType);

            var port1 = PortModel.Create <string>("Out", PortDirection.Output);
            var port2 = PortModel.Create <string>("In", PortDirection.Input);

            Assert.That(graph.CanConnect(port1, port2), Is.False);

            node1.AddPort(port1);
            node2.AddPort(port2);

            Assert.That(graph.CanConnect(port1, port2), Is.False);
        }
Ejemplo n.º 10
0
        public void Connect_Should_Throw_WhenCanNotConnect()
        {
            var graph = new GraphModel(GraphTypeWithConnections);

            var nodeType = Substitute.For <INodeType>();
            var node1    = new NodeModel(nodeType);
            var node2    = new NodeModel(nodeType);

            var port1 = PortModel.Create <string>("Out", PortDirection.Input);
            var port2 = PortModel.Create <string>("In", PortDirection.Input);

            node1.AddPort(port1);
            node2.AddPort(port2);

            graph.AddNode(node1);
            graph.AddNode(node2);

            Assert.That(() => graph.Connect(port1, port2), Throws.InvalidOperationException);
        }
Ejemplo n.º 11
0
        public void Connect_Should_Remove_OtherConnections()
        {
            var graph = new GraphModel(GraphTypeWithConnections);

            var nodeType = Substitute.For <INodeType>();
            var node1    = new NodeModel(nodeType);
            var node2    = new NodeModel(nodeType);
            var node3    = new NodeModel(nodeType);
            var node4    = new NodeModel(nodeType);

            var port1 = PortModel.Create <string>("Out", PortDirection.Output, PortCapacity.Single);
            var port2 = PortModel.Create <string>("In", PortDirection.Input, PortCapacity.Single);
            var port3 = PortModel.Create <string>("Out", PortDirection.Output, PortCapacity.Single);
            var port4 = PortModel.Create <string>("In", PortDirection.Input, PortCapacity.Single);

            node1.AddPort(port1);
            node2.AddPort(port2);
            node3.AddPort(port3);
            node4.AddPort(port4);

            graph.AddNode(node1);
            graph.AddNode(node2);
            graph.AddNode(node3);
            graph.AddNode(node4);

            var connection1 = graph.Connect(port1, port2);

            IReadOnlyList <ConnectionModel> removedConnections = null;

            graph.GraphChanged += (an, rn, ac, rc) => { removedConnections = rc; };

            // ports may accept only one connection, that's why connection1 will be removed
            var connection2 = graph.Connect(port3, port2);

            Assert.That(graph.Connections, Does.Not.Contains(connection1));
            Assert.That(removedConnections, Is.EquivalentTo(new[] { connection1 }));

            // here, connection2 will be removed
            var connection3 = graph.Connect(port3, port4);

            Assert.That(graph.Connections, Does.Not.Contains(connection2));
            Assert.That(removedConnections, Is.EquivalentTo(new[] { connection2 }));
        }
Ejemplo n.º 12
0
        public void Disconnect_Should_Batch_WithConnect()
        {
            var graph = new GraphModel(Substitute.For <IGraphType>());

            var node1 = new NodeModel(Substitute.For <INodeType>());
            var node2 = new NodeModel(Substitute.For <INodeType>());

            var port1 = PortModel.Create <string>("Out1", PortDirection.Output);
            var port2 = PortModel.Create <string>("Out2", PortDirection.Output);
            var port3 = PortModel.Create <string>("In1", PortDirection.Input);
            var port4 = PortModel.Create <string>("In2", PortDirection.Input);

            node1.AddPort(port1);
            node1.AddPort(port2);
            node2.AddPort(port3);
            node2.AddPort(port4);

            graph.AddNode(node1);
            graph.AddNode(node2);

            var connection1 = graph.Connect(port1, port3);
            var connection2 = graph.Connect(port2, port4);

            IReadOnlyList <ConnectionModel> removedConnections = null;

            graph.GraphChanged += (an, rn, ac, rc) => { removedConnections = rc; };

            using (graph.BeginChangeSet())
            {
                graph.Disconnect(connection1);
                graph.Disconnect(connection2);

                var connection3 = graph.Connect(port1, port3);

                Assert.That(removedConnections, Is.Null);
                Assert.That(connection3, Is.SameAs(connection1));
            }

            Assert.That(removedConnections, Is.Not.Null);
            Assert.That(removedConnections, Is.EquivalentTo(new[] { connection2 }));
        }
Ejemplo n.º 13
0
        public void Connect_Should_Succeed()
        {
            var graph = new GraphModel(GraphTypeWithConnections);

            var nodeType = Substitute.For <INodeType>();
            var node1    = new NodeModel(nodeType);
            var node2    = new NodeModel(nodeType);

            var port1 = PortModel.Create <string>("Out", PortDirection.Output);
            var port2 = PortModel.Create <string>("In", PortDirection.Input);

            node1.AddPort(port1);
            node2.AddPort(port2);

            graph.AddNode(node1);
            graph.AddNode(node2);

            IReadOnlyList <ConnectionModel> list = null;

            graph.GraphChanged += (an, rn, ac, rc) => { list = ac; };

            var connection = graph.Connect(port1, port2);

            Assert.That(connection, Is.Not.Null);

            Assert.That(graph.Connections, Does.Contain(connection));

            Assert.That(port1.Connections, Has.Count.EqualTo(1));
            Assert.That(port2.Connections, Has.Count.EqualTo(1));

            Assert.That(port1.Connections, Is.EquivalentTo(port2.Connections));

            Assert.That(connection.From, Is.EqualTo(port1));
            Assert.That(connection.To, Is.EqualTo(port2));

            Assert.That(list, Is.Not.Null);
            Assert.That(list.Count, Is.EqualTo(1));
            Assert.That(list, Does.Contain(connection));
        }
Ejemplo n.º 14
0
        public void RemoveNode_Should_Remove_Connections()
        {
            var graph = new GraphModel(GraphTypeWithConnections);

            var node1 = new NodeModel(Substitute.For <INodeType>());
            var node2 = new NodeModel(Substitute.For <INodeType>());
            var node3 = new NodeModel(Substitute.For <INodeType>());

            graph.AddNode(node1);
            graph.AddNode(node2);
            graph.AddNode(node3);

            var port1 = PortModel.Create <string>("Out", PortDirection.Output);
            var port2 = PortModel.Create <string>("In", PortDirection.Input);
            var port3 = PortModel.Create <string>("Out", PortDirection.Output);
            var port4 = PortModel.Create <string>("In", PortDirection.Input);

            node1.AddPort(port1);
            node2.AddPort(port2);
            node2.AddPort(port3);
            node3.AddPort(port4);

            var connection1 = graph.Connect(port1, port2);
            var connection2 = graph.Connect(port3, port4);

            IReadOnlyList <ConnectionModel> removedConnections = null;

            graph.GraphChanged += (an, rn, ac, rc) => removedConnections = rc;

            graph.RemoveNode(node2);

            Assert.That(graph.Connections, Does.Not.Contains(connection1));
            Assert.That(graph.Connections, Does.Not.Contains(connection2));

            Assert.That(removedConnections, Is.EquivalentTo(new[] { connection1, connection2 }));
        }
Ejemplo n.º 15
0
 public void CreateT_Should_Throw_WhenNullId()
 {
     Assert.That(() => PortModel.Create <string>(null, PortDirection.Input), Throws.ArgumentNullException);
 }
Ejemplo n.º 16
0
        public void RemovePort_Should_Throw_WhenPortNotFound()
        {
            var node = new NodeModel(Substitute.For <INodeType>());

            Assert.That(() => node.RemovePort(PortModel.Create <string>("Id", PortDirection.Input)), Throws.ArgumentException);
        }