예제 #1
0
        public void Container_RemoveContent_ExistingElement_Works(Fb2Container node)
        {
            node.Should().NotBeNull();

            var firstAllowedNode = Fb2NodeFactory.GetNodeByName(node.AllowedElements.First());
            var lastAllowedNode  = Fb2NodeFactory.GetNodeByName(node.AllowedElements.Last());

            var nodesEquals = firstAllowedNode.Equals(lastAllowedNode);

            node.AddContent(firstAllowedNode, lastAllowedNode);
            node.Content.Should().NotBeEmpty().And.Subject.Should().HaveCount(2);

            firstAllowedNode.Parent.Should().NotBeNull();
            firstAllowedNode.Parent.Should().Be(node);

            lastAllowedNode.Parent.Should().NotBeNull();
            lastAllowedNode.Parent.Should().Be(node);

            node.RemoveContent(firstAllowedNode); // Fb2Node
            firstAllowedNode.Parent.Should().BeNull();

            node.Content.Should().NotBeEmpty().And.Subject.Should().HaveCount(1);
            node.Content.Should().Contain(lastAllowedNode);

            // empty nodes (without sub tree) of same type are, basically, equal lol
            // so if you add 2 empty "bold" elements to "paragraph"
            // and then use RemoveContent(firstAllowedNode), and check Contains - it will return true
            if (!nodesEquals) // when container can have only one type child
            {
                node.Content.Contains(firstAllowedNode).Should().BeFalse();
            }

            ClearContainerContent(node);

            lastAllowedNode.Parent.Should().BeNull();

            node.AddContent(firstAllowedNode, lastAllowedNode);
            node.Content.Should().NotBeEmpty().And.Subject.Should().HaveCount(2);

            firstAllowedNode.Parent.Should().NotBeNull();
            firstAllowedNode.Parent.Should().Be(node);

            lastAllowedNode.Parent.Should().NotBeNull();
            lastAllowedNode.Parent.Should().Be(node);

            node.RemoveContent(new List <Fb2Node> {
                firstAllowedNode, lastAllowedNode
            });                                                                          // IEnumerable<Fb2Node>

            node.Content.Should().BeEmpty();

            node.AddContent(firstAllowedNode, lastAllowedNode);
            node.Content.Should().NotBeEmpty().And.Subject.Should().HaveCount(2);

            node.RemoveContent(n => n.Name.Equals(firstAllowedNode.Name)); // Func<Fb2Node, bool> predicate

            if (nodesEquals)
            {
                node.Content.Should().BeEmpty();
            }
            else
            {
                node.Content.Should().NotBeEmpty().And.Subject.Should().HaveCount(1);
                node.Content.Should().Contain(lastAllowedNode);
                node.Content.Should().NotContain(firstAllowedNode);
            }

            ClearContainerContent(node);
        }