コード例 #1
0
        public void TestAdd()
        {
            HContainer container = new HElement("div");

            // Nothing appening
            container.Add(null);

            Assert.Equal(0, container.Nodes().Count());

            container.Add(123.45);
            String s = (123.45).ToString();

            Assert.Equal(1, container.Nodes().Count());
            Assert.Equal("<div>" + s + "</div>", container.ToString());

            container.Add(null);
            Assert.Equal(1, container.Nodes().Count());
            Assert.Equal("<div>" + s + "</div>", container.ToString());

            container.Add("Other Content");
            Assert.Equal(1, container.Nodes().Count());
            Assert.Equal("<div>" + s + "Other Content</div>", container.ToString());

            container.Add(new HElement("span", "SPAN"));
            Assert.Equal(2, container.Nodes().Count());
            Assert.Equal("<div>" + s + "Other Content<span>SPAN</span></div>", container.ToString());
        }
コード例 #2
0
ファイル: HElementTest.cs プロジェクト: ygrenier/HDocument
        public void TestAttributes()
        {
            // Attributes are empty
            var elm = new HElement("test");

            Assert.Equal(0, elm.Attributes().Count());
            Assert.Null(elm.Attribute(null));
            Assert.Null(elm.Attribute(""));
            Assert.Null(elm.Attribute(" "));
            Assert.Null(elm.Attribute("attr2"));

            // Adding two attributes
            elm.Add(
                new HAttribute("attr1", "val1"),
                new HAttribute("attr3", "val3")
                );
            Assert.Equal(2, elm.Attributes().Count());
            Assert.Equal(0, elm.Attributes("Attr2").Count());
            Assert.Null(elm.Attribute(null));
            Assert.Null(elm.Attribute(""));
            Assert.Null(elm.Attribute(" "));
            Assert.Null(elm.Attribute("attr2"));

            // Adding a third attribute
            elm.Add(new HAttribute("attr2", "val2"));

            Assert.Equal(3, elm.Attributes().Count());
            Assert.Equal(1, elm.Attributes("Attr2").Count());
            Assert.Null(elm.Attribute(null));
            Assert.Null(elm.Attribute(""));
            Assert.Null(elm.Attribute(" "));
            Assert.NotNull(elm.Attribute("attr2"));
            Assert.NotNull(elm.Attribute("Attr2"));

            // Can't adding an attribute with a name already defined
            var ioex = Assert.Throws <InvalidOperationException>(() => elm.Add(new HAttribute("attr2", "value")));

            Assert.Equal("The attribute 'attr2' already defined", ioex.Message);

            // Test adding an attribute already defined in another element
            var otherElement = new HElement("OtherTest");
            var a1           = elm.Attribute("Attr2");

            otherElement.Add(a1);
            var a2 = otherElement.Attribute("attr2");

            Assert.NotSame(a1, a2);
            Assert.Equal(a1.Name, a2.Name);
            Assert.Equal(a1.Value, a2.Value);
        }
コード例 #3
0
        public void TestClone()
        {
            var container = new HElement("parent");

            container.Add(new HElement("child1"));
            container.Add(new HElement("child2", "Content"));
            container.Add(new HElement("child3", new HText("Content 1"), new HText("Content 2")));

            // Adding this container itself to a clone
            container.Add(container);

            var nodes = container.Nodes().ToArray();

            Assert.Equal(4, nodes.Length);
        }
コード例 #4
0
        public void TestAddNode()
        {
            HContainer container = new HElement("parent");

            HNode node1 = new HElement("test");

            container.Add(node1);

            HNode node2 = new HElement("test2");

            container.Add(node2);

            Assert.Same(node1, container.FirstNode);
            Assert.Same(node2, container.LastNode);
        }
コード例 #5
0
ファイル: HElementTest.cs プロジェクト: ygrenier/HDocument
        public void TestClone()
        {
            var parent = new HElement("parent");

            var elm = new HElement("test", "content");

            elm.Add(
                new HAttribute("attr1", "val1"),
                new HAttribute("attr3", "val3")
                );
            Assert.Equal(1, elm.Nodes().Count());
            parent.Add(elm);

            var newElm = new HElement("test", elm);
            var c      = newElm.Elements().First();

            Assert.Equal(2, c.Attributes().Count());
            Assert.Equal(0, c.Attributes("Attr2").Count());
            Assert.Equal(1, c.Nodes().Count());

            var cloneElm = c.Clone();

            Assert.Equal(2, cloneElm.Attributes().Count());
            Assert.Equal(0, cloneElm.Attributes("Attr2").Count());
            Assert.Equal(1, cloneElm.Nodes().Count());
        }
コード例 #6
0
        public void TestElementsWithName()
        {
            HContainer container = new HElement("Test");

            HElement[] elements = container.Elements("Test").ToArray();
            Assert.Equal(0, elements.Length);

            container.Add("Content 1");
            container.Add(new HElement("test"));
            container.Add("Content 2");
            container.Add(new HElement("element"));
            container.Add("Content 3");

            elements = container.Elements("Test").ToArray();
            Assert.Equal(1, elements.Length);
        }
コード例 #7
0
        public void TestAddParentAsNode()
        {
            HContainer parent = new HElement("parent");
            HElement   child1 = new HElement("child1");

            parent.Add(child1);
            HElement child2 = new HElement("child2");

            child1.Add(child2);

            child2.Add(parent);

            Assert.IsType <HElement>(child2.FirstNode);
            HElement node = (HElement)child2.FirstNode;

            Assert.Equal("parent", node.Name);
            Assert.NotSame(parent, node);

            node = (HElement)node.FirstNode;
            Assert.Equal("child1", node.Name);
            Assert.NotSame(child1, node);

            node = (HElement)node.FirstNode;
            Assert.Equal("child2", node.Name);
            Assert.NotSame(child2, node);

            Assert.Null(node.FirstNode);
        }
コード例 #8
0
        public void TestAddAfter()
        {
            HElement parent = new HElement("parent");

            HNode n1 = new HElement("test-1");

            parent.Add(n1);
            Assert.Equal(1, parent.Nodes().Count());

            HNode n2 = new HElement("test-2");

            n1.AddAfter(n2);
            Assert.Equal(2, parent.Nodes().Count());
            n1.AddAfter("1");
            Assert.Equal(3, parent.Nodes().Count());
            n2.AddAfter("2");
            Assert.Equal(4, parent.Nodes().Count());

            var nodes = parent.Nodes().ToArray();

            Assert.Equal(4, nodes.Length);
            Assert.IsType <HElement>(nodes[0]);
            Assert.IsType <HText>(nodes[1]);
            Assert.IsType <HElement>(nodes[2]);
            Assert.IsType <HText>(nodes[3]);

            n1 = new HElement("test-3");
            var ioe = Assert.Throws <InvalidOperationException>(() => n1.AddAfter(null));

            Assert.Equal("No parent found.", ioe.Message);
        }
コード例 #9
0
        public void TestPreviousNode()
        {
            HNode n1 = new HText("test 1");

            Assert.Null(n1.PreviousNode);

            HElement parent = new HElement("parent");

            parent.Add(n1);
            Assert.Null(n1.PreviousNode);

            HNode n2 = new HText("test 2");

            parent.Add(n2);
            Assert.Null(n1.PreviousNode);
            Assert.Same(n1, n2.PreviousNode);
        }
コード例 #10
0
        public void TestNextNode()
        {
            HNode n1 = new HText("test 1");

            Assert.Null(n1.NextNode);

            HElement parent = new HElement("parent");

            parent.Add(n1);
            Assert.Null(n1.NextNode);

            HNode n2 = new HText("test 2");

            parent.Add(n2);
            Assert.Same(n2, n1.NextNode);
            Assert.Null(n2.NextNode);
        }
コード例 #11
0
ファイル: HElementTest.cs プロジェクト: ygrenier/HDocument
        public void TestHasAttributes()
        {
            var elm = new HElement("test");

            Assert.False(elm.HasAttributes);

            elm.Add(new HAttribute("attr1", "val1"));
            Assert.True(elm.HasAttributes);
        }
コード例 #12
0
ファイル: HElementTest.cs プロジェクト: ygrenier/HDocument
        public void TestCantAddDocumentTypeInElement()
        {
            var elm = new HElement("test");
            var dt  = new HDocumentType();

            var ex = Assert.Throws <ArgumentException>(() => elm.Add(dt));

            Assert.Equal("Can't add a document type in a element.", ex.Message);
        }
コード例 #13
0
        public void TestLastNode()
        {
            HContainer container = new HElement("Test");

            Assert.Null(container.LastNode);

            var node = new HText("Content");

            container.Add(node);
            Assert.Same(node, container.LastNode);

            container.Add(new HText("Other content"));
            Assert.NotSame(node, container.LastNode);

            // If content is an empty string, LastNode returns null
            container = new HElement("Test");
            container.Add("");
            Assert.Null(container.LastNode);
        }
コード例 #14
0
        public void TestElements()
        {
            HContainer container = new HElement("Test");

            Assert.False(container.HasElements);
            HElement[] elements = container.Elements().ToArray();
            Assert.Equal(0, elements.Length);

            container.Add("Content 1");
            Assert.False(container.HasElements);
            container.Add(new HElement("test"));
            Assert.True(container.HasElements);
            container.Add("Content 2");
            container.Add(new HElement("element"));
            container.Add("Content 3");

            elements = container.Elements().ToArray();
            Assert.Equal(2, elements.Length);
        }
コード例 #15
0
        public void TestAddString()
        {
            HContainer container = new HElement("test");

            container.Add("String 1");
            container.Add(" - String 2");

            Assert.IsType <HText>(container.FirstNode);
            Assert.Equal("String 1 - String 2", ((HText)container.FirstNode).Value);

            container.Add(" - String 3");

            Assert.IsType <HText>(container.FirstNode);
            Assert.Equal("String 1 - String 2 - String 3", ((HText)container.FirstNode).Value);

            container = new HElement("test");
            container.Add("Content");
            container.Add(new HText("Other content"));
            Assert.Equal(2, container.Nodes().Count());
        }
コード例 #16
0
        public void TestHasAnyDataElement()
        {
            HElement element = new HElement("div");

            Assert.Equal(false, element.HasData());

            element.Add(new HAttribute("data-value1", "value1"));
            Assert.Equal(true, element.HasData());

            element = null;

            Assert.Equal(false, element.HasData());
        }
コード例 #17
0
        public void TestNodes()
        {
            HContainer container = new HElement("Test");

            Assert.False(container.HasNodes);
            HNode[] nodes = container.Nodes().ToArray();
            Assert.Equal(0, nodes.Length);

            container.Add("Content 1");
            Assert.True(container.HasNodes);
            container.Add(new HElement("test"));
            container.Add("Content 2");
            container.Add(new HElement("element"));
            container.Add("Content 3");

            nodes = container.Nodes().ToArray();
            Assert.Equal(5, nodes.Length);

            Assert.IsType <HText>(nodes[0]);
            Assert.IsType <HElement>(nodes[1]);
            Assert.IsType <HText>(nodes[2]);
            Assert.IsType <HElement>(nodes[3]);
            Assert.IsType <HText>(nodes[4]);
        }
コード例 #18
0
        public void TestNextAttribute()
        {
            var attr = new HAttribute("name", "value");

            Assert.Null(attr.NextAttribute);

            var elm = new HElement("parent");

            elm.Add(attr);
            Assert.Null(attr.NextAttribute);

            var attr2 = new HAttribute("attr1", "val1");

            elm.Add(attr2);
            Assert.Same(attr2, attr.NextAttribute);
            Assert.Null(attr2.NextAttribute);

            var attr3 = new HAttribute("attr2", "val2");

            elm.Add(attr3);
            Assert.Same(attr2, attr.NextAttribute);
            Assert.Same(attr3, attr2.NextAttribute);
            Assert.Null(attr3.NextAttribute);
        }
コード例 #19
0
ファイル: HElementTest.cs プロジェクト: ygrenier/HDocument
        public void TestCreateFromOther()
        {
            var elm = new HElement("test", "content");

            elm.Add(
                new HAttribute("attr1", "val1"),
                new HAttribute("attr3", "val3")
                );
            Assert.Equal(1, elm.Nodes().Count());

            var newElm = new HElement(elm);

            Assert.Equal(2, elm.Attributes().Count());
            Assert.Equal(0, elm.Attributes("Attr2").Count());
            Assert.Equal(1, elm.Nodes().Count());
        }
コード例 #20
0
        public void TestAddNodeAlreadyAssigned()
        {
            HContainer container = new HElement("parent");
            HNode      node1     = new HElement("test");

            container.Add(node1);

            HContainer container2 = new HElement("OtherParent");

            container2.Add(node1);

            Assert.IsType <HElement>(container.FirstNode);
            Assert.IsType <HElement>(container2.FirstNode);

            Assert.Same(node1, container.FirstNode);
            Assert.NotSame(node1, container2.FirstNode);
        }
コード例 #21
0
ファイル: HElementTest.cs プロジェクト: ygrenier/HDocument
        public void TestLastAttribute()
        {
            var elm = new HElement("test");

            Assert.Null(elm.LastAttribute);

            elm.Add(
                new HAttribute("attr1", "val1"),
                new HAttribute("attr2", "val2"),
                new HAttribute("attr3", "val3")
                );
            var la = elm.LastAttribute;

            Assert.NotNull(la);
            Assert.Equal("attr3", la.Name);
            Assert.Equal("val3", la.Value);
        }
コード例 #22
0
ファイル: HCDataTest.cs プロジェクト: ygrenier/HDocument
        public void TestClone()
        {
            HCData cdata = new HCData("Test");

            HContainer parent = new HElement("test");

            parent.Add(cdata);
            Assert.Same(cdata, parent.FirstNode);
            Assert.Same(cdata, parent.LastNode);

            HContainer otherParent = new HElement("test");

            // Do clone
            otherParent.Add(cdata);

            Assert.IsType <HCData>(otherParent.FirstNode);
            Assert.NotSame(otherParent.FirstNode, parent.FirstNode);
        }
コード例 #23
0
ファイル: HCommentTest.cs プロジェクト: ygrenier/HDocument
        public void TestClone()
        {
            HComment hComment = new HComment("Test");

            HContainer parent = new HElement("test");

            parent.Add(hComment);
            Assert.Same(hComment, parent.FirstNode);
            Assert.Same(hComment, parent.LastNode);

            HContainer otherParent = new HElement("test");

            // Do clone
            otherParent.Add(hComment);

            Assert.IsType <HComment>(otherParent.FirstNode);
            Assert.NotSame(otherParent.FirstNode, parent.FirstNode);
        }
コード例 #24
0
        public void TestAddEnumerable()
        {
            var container = new HElement("parent");
            var dt        = DateTime.Now;

            container.Add(new List <Object>(new object[] { new HElement("parent"), "String", 123, null, dt }));

            var node = container.FirstNode;

            Assert.IsType <HElement>(node);
            Assert.Equal("parent", ((HElement)node).Name);

            node = node.NextNode;
            Assert.IsType <HText>(node);
            Assert.Equal(String.Format("{0}{1}{2}", "String", 123, dt), ((HText)node).Value);

            node = node.NextNode;
            Assert.Null(node);
        }
コード例 #25
0
        public void TestAddArray()
        {
            // params
            HContainer container = new HElement("parent");

            HNode node1 = new HElement("test");
            HNode node2 = new HElement("test2");

            container.Add(node1, node2);

            Assert.Same(node1, container.FirstNode);
            Assert.Same(node2, container.LastNode);

            // direct array
            container = new HElement("parent");

            node1 = new HElement("test");
            node2 = new HElement("test2");

            container.Add(new HNode[] { node1, node2 });

            Assert.Same(node1, container.FirstNode);
            Assert.Same(node2, container.LastNode);

            // mixed content
            container = new HElement("parent");
            var dt = DateTime.Now;

            container.Add(new HElement("parent"), "String", 123, null, dt);

            var node = container.FirstNode;

            Assert.IsType <HElement>(node);
            Assert.Equal("parent", ((HElement)node).Name);

            node = node.NextNode;
            Assert.IsType <HText>(node);
            Assert.Equal(String.Format("{0}{1}{2}", "String", 123, dt), ((HText)node).Value);

            node = node.NextNode;
            Assert.Null(node);
        }
コード例 #26
0
        public void TestGetClasses()
        {
            Assert.Equal(new String[] { }, ClassAttributeExtensions.GetClasses(null));

            var element = new HElement("div");

            Assert.Equal(new String[] { }, element.GetClasses());

            var clsAttr = new HAttribute("class");

            element.Add(clsAttr);

            Assert.Equal(new String[] { }, element.GetClasses());

            clsAttr.Value = "    ";
            Assert.Equal(new String[] { }, element.GetClasses());

            clsAttr.Value = "  class1  ";
            Assert.Equal(new String[] { "class1" }, element.GetClasses());

            clsAttr.Value = "  class1 class-2 ";
            Assert.Equal(new String[] { "class1", "class-2" }, element.GetClasses());
        }