Пример #1
0
        public void DeepRetrieveFromObjectGraph()
        {
            // Arrange
            Node node = new Node
            {
                Number = 1,
                Parent = new Node
                {
                    Number = 2,
                    Parent = new Node
                    {
                        Number = 3
                    }
                }
            };

            // Act
            var third = node.Maybe()
                .Apply(n => n.Parent)
                .Apply(n => n.Parent)
                .Return();

            // Assert
            Assert.IsNotNull(third);
            Assert.AreEqual(3, third.Number);
        }
Пример #2
0
        public void PassingIf()
        {
            // Arrange
            Node node = new Node
            {
                Number = 1,
                Parent = new Node
                {
                    Number = 2,
                    Parent = new Node
                    {
                        Number = 3
                    }
                }
            };

            // Act
            var third = node.Maybe()
                .Apply(n => n.Parent)
                .If(n => n.Number == 2)
                .Apply(n => n.Parent)
                .Return(n => n.Number);

            // Assert
            Assert.AreEqual(3, third);
        }
Пример #3
0
        public void PassingDo()
        {
            // Arrange
            Node node = new Node
            {
                Number = 1,
                Parent = new Node
                {
                    Number = 2,
                    Parent = new Node
                    {
                        Number = 3
                    }
                }
            };

            int blah = 0;

            // Act
            node.Maybe()
                .Apply(n => n.Parent)
                .Do(n => blah = n.Number);

            // Assert
            Assert.AreEqual(2, blah);
        }
Пример #4
0
        public void PassingCast()
        {
            // Arrange
            Node node = new Node
            {
                 Number = 1,
                 Parent = new Node
                 {
                      Number = 2,
                      Parent = new Node
                      {
                            Number = 3
                      }
                 }
            };

            // Act
            var secondNum = node.Maybe()
                 .Apply(n => n.Parent)
                 .Cast<IDoStuff>()
                 .Return(n => n.Number);

            // Assert
            Assert.AreEqual(2, secondNum);
        }
Пример #5
0
        public void FailingCast()
        {
            // Arrange
            Node node = new Node
            {
                Number = 1,
                Parent = new Node
                {
                    Number = 2,
                    Parent = new Node
                    {
                        Number = 3
                    }
                }
            };

            // Act
            var third = node.Maybe()
                .Apply(n => n.Parent)
                .Cast<Blah>()
                .Apply(n => n.Parent)
                .Return(n => n.Number);

            // Assert
            Assert.AreEqual(0, third);
        }
Пример #6
0
        public void DeepRetrieveFromPartialObjectGraph()
        {
            // Arrange
            Node node = new Node
            {
                Number = 1,
                Parent = new Node
                {
                    Number = 2,
                }
            };

            // Act
            var third = node.Maybe()
                .Apply(n => n.Parent)
                .Apply(n => n.Parent)
                .Return();

            // Assert
            Assert.IsNull(third);
        }