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()); }
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); }
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); }
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); }
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()); }
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); }
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); }
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); }
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); }
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); }
public void TestHasAttributes() { var elm = new HElement("test"); Assert.False(elm.HasAttributes); elm.Add(new HAttribute("attr1", "val1")); Assert.True(elm.HasAttributes); }
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); }
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); }
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); }
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()); }
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()); }
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]); }
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); }
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()); }
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); }
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); }
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); }
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); }
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); }
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); }
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()); }