public void TestElementMethods() { //GetElement SimpleElement se = new SimpleElement(); Assert.IsNull(se.GetElement("child1")); IXmlElement child1 = se.AddElement("child1"); Assert.IsNotNull(se.GetElement("child1")); //FindElement Assert.IsNull(se.FindElement("/child2")); Assert.AreEqual(child1, se.FindElement("/child1")); //EnsureElement Assert.AreEqual(se.ElementList.Count, 1); Assert.IsNotNull(se.EnsureElement("/child2")); IXmlElement child2 = se.GetElement("child2"); Assert.IsTrue(child2.IsMutable); Assert.AreEqual(se.ElementList.Count, 2); //element is added //GetSafeElement Assert.AreEqual(se.ElementList.Count, 2); Assert.AreEqual(se, se.GetSafeElement("")); IXmlElement child3 = se.GetSafeElement("/child3"); Assert.IsNotNull(child3); Assert.IsFalse(child3.IsMutable); Assert.AreEqual(se.ElementList.Count, 2); //element is not added IXmlElement child4 = se.GetSafeElement("/child3/../child4"); Assert.IsNotNull(child4); Exception e = null; try { se.GetSafeElement(null); } catch (Exception ex) { e = ex; } Assert.IsNotNull(e); Assert.IsInstanceOf(typeof(ArgumentNullException), e); e = null; try { se.GetSafeElement(".."); } catch (Exception ex) { e = ex; } Assert.IsNotNull(e); Assert.IsInstanceOf(typeof(ArgumentException), e); }
public void TestProperties() { SimpleValue sv = new SimpleValue("value", false, false); Assert.IsFalse(sv.IsAttribute); Assert.IsTrue(sv.IsContent); Assert.IsFalse(sv.IsEmpty); Assert.IsNull(sv.Parent); sv.SetString(null); Assert.IsTrue(sv.IsEmpty); sv.SetString(""); Assert.IsTrue(sv.IsEmpty); sv.SetBinary(Binary.NO_BINARY); Assert.IsTrue(sv.IsEmpty); SimpleElement element = new SimpleElement(); sv.Parent = element; Assert.IsNotNull(sv.Parent); Exception e = null; try { sv.Parent = new SimpleElement(); //already set } catch (Exception ex) { e = ex; } Assert.IsNotNull(e); Assert.IsInstanceOf(typeof(InvalidOperationException), e); e = null; try { sv.Parent = null; //cannot be null } catch (Exception ex) { e = ex; } Assert.IsNotNull(e); Assert.IsInstanceOf(typeof(ArgumentException), e); e = null; IXmlValue immutable = element.GetSafeAttribute("attr"); Assert.IsNotNull(immutable); Assert.IsFalse(immutable.IsMutable); try { immutable.Parent = element; } catch (Exception ex) { e = ex; } Assert.IsNotNull(e); Assert.IsInstanceOf(typeof(InvalidOperationException), e); sv = new SimpleValue("value", false, false); Assert.IsTrue(sv.IsMutable); sv.Parent = element.GetSafeElement("test"); Assert.IsFalse(sv.IsMutable); }
public void TestProperties() { SimpleElement se = new SimpleElement("name", 5); Assert.IsNotNull(se); Assert.AreEqual(se.Value, 5); Assert.AreEqual(se.GetInt(), 5); Assert.IsFalse(se.IsAttribute); Assert.IsTrue(se.IsContent); Assert.IsFalse(se.IsEmpty); Assert.IsTrue(se.IsMutable); Assert.AreEqual(se.Name, "name"); Exception e = null; try { se.Name = "?name"; } catch (Exception ex) { e = ex; } Assert.IsNotNull(e); Assert.IsInstanceOf(typeof(ArgumentException), e); Assert.AreEqual(se.Name, "name"); se.Name = "newname"; Assert.AreEqual(se.Name, "newname"); e = null; try { se.GetSafeElement("el").Name = "elname"; } catch (Exception ex) { e = ex; } Assert.IsNotNull(e); Assert.IsInstanceOf(typeof(InvalidOperationException), e); Assert.IsNull(se.Parent); Assert.IsNotNull(se.Root); Assert.AreEqual(se, se.Root); Assert.AreEqual("/newname", se.AbsolutePath); SimpleElement root = new SimpleElement("root"); se.Parent = root; Assert.IsNotNull(se.Parent); Assert.AreEqual(root, se.Parent); Assert.IsNotNull(se.Root); Assert.AreEqual(root, se.Root); Assert.AreEqual("/root/newname", se.AbsolutePath); Assert.IsNull(se.Comment); e = null; try { se.Comment = "new comment --"; } catch (Exception ex) { e = ex; } Assert.IsNotNull(e); Assert.IsInstanceOf(typeof(ArgumentException), e); Assert.IsNull(se.Comment); se.Comment = "new comment"; Assert.AreEqual(se.Comment, "new comment"); }
public void TestElementList() { SimpleElement parent = new SimpleElement("parent", "value"); Assert.IsTrue(parent.IsMutable); //so, element list is mutable as well IList elements = parent.ElementList; Assert.IsNotNull(elements); Assert.AreEqual(elements.Count, 0); //adding something that is not IXmlElement will fail Exception e = null; try { elements.Add("something"); } catch (Exception ex) { e = ex; } Assert.IsNotNull(e); Assert.IsInstanceOf(typeof(InvalidCastException), e); Assert.AreEqual(elements.Count, 0); IXmlElement child0 = parent.AddElement("child0"); IXmlElement child1 = new SimpleElement("child1"); IXmlElement child2 = new SimpleElement("child2"); IXmlElement child3 = new SimpleElement("child3"); IXmlElement child4 = new SimpleElement("child5"); IXmlElement child5 = new SimpleElement("child6"); Assert.IsNotNull(child0); Assert.AreEqual(child0.Parent, parent); Assert.AreEqual(elements.Count, 1); Assert.AreEqual(elements[0], child0); child1.Comment = "comment"; child1.AddAttribute("attr").SetString("attrValue"); child1.Parent = child0; elements.Add(child1); Assert.AreEqual(elements.Count, 2); Assert.AreNotEqual(child1.Parent, ((SimpleElement)elements[1]).Parent); Assert.AreEqual(parent, ((SimpleElement)elements[1]).Parent); Assert.AreEqual(elements[1], child1); Assert.AreNotSame(elements[1], child1); Assert.IsInstanceOf(typeof(ArrayList), elements); ArrayList elementList = (ArrayList)elements; IList l = new ArrayList(); l.Add(child2); elementList.AddRange(l); Assert.AreEqual(elementList.Count, 3); Assert.AreEqual(child2.Parent, parent); Assert.AreEqual(elementList[2], child2); elementList.Insert(3, child3); Assert.AreEqual(elementList.Count, 4); Assert.AreEqual(child3.Parent, parent); Assert.AreEqual(elementList[3], child3); l = new ArrayList(); l.Add(child4); l.Add(child5); elementList.InsertRange(4, l); Assert.AreEqual(elementList.Count, 6); Assert.AreEqual(elementList[4], child4); Assert.AreEqual(elementList[5], child5); Assert.IsTrue(elementList.Contains(child5)); elementList.Remove(child5); Assert.AreEqual(elementList.Count, 5); elementList.RemoveRange(3, 2); Assert.AreEqual(elementList.Count, 3); Assert.IsFalse(elementList.Contains(child4)); Assert.IsFalse(elementList.Contains(child5)); elementList.SetRange(1, l); Assert.AreEqual(elementList.Count, 3); Assert.AreEqual(elementList[0], child0); Assert.AreEqual(elementList[1], child4); Assert.AreEqual(elementList[2], child5); elementList[0] = child3; Assert.AreEqual(elementList[0], child3); //3,4,5 elementList.Reverse(); Assert.AreEqual(elementList[2], child3); //5,4,3 Assert.AreEqual(elementList[0], child5); //5,4,3 elementList.Reverse(0, 2); Assert.AreEqual(elementList[2], child3); //4,5,3 Assert.AreEqual(elementList[0], child4); //4,5,3 elementList.Sort(new SimpleElementComparer()); Assert.AreEqual(elementList[0], child3); //3,4,5 Assert.AreEqual(elementList[2], child5); //3,4,5 elementList.Reverse(); elementList.Sort(0, 2, new SimpleElementComparer()); Assert.AreEqual(elementList[0], child4); //4,5,3 Assert.AreEqual(elementList[2], child3); //4,5,3 elementList.Clear(); Assert.AreEqual(elementList.Count, 0); e = null; try { IXmlElement immutable = parent.GetSafeElement("immutable"); immutable.AddElement("child"); } catch (Exception ex) { e = ex; } Assert.IsNotNull(e); Assert.IsInstanceOf(typeof(InvalidOperationException), e); SimpleElement parent2 = new SimpleElement(); parent.AddElement("child1"); Assert.IsFalse(parent.ElementList.Equals(parent2.ElementList)); Assert.IsFalse(parent.ElementList.Equals("test")); parent2.AddElement("child1"); Assert.IsTrue(parent.ElementList.Equals(parent2.ElementList)); parent.AddElement("child2"); parent2.AddElement("child2"); Assert.AreEqual(parent.ElementList, parent2.ElementList); parent2.GetElement("child2").AddAttribute("attr"); Assert.AreNotEqual(parent.ElementList, parent2.ElementList); }