public void TestObjectMethods() { //Equals SimpleElement se1 = new SimpleElement("name"); SimpleElement se2 = new SimpleElement("name", "value"); SimpleValue sv = new SimpleValue("value"); Assert.IsFalse(se1.Equals(sv)); Assert.IsFalse(se1.Equals(se2)); se2.SetString(se1.GetString()); Assert.IsTrue(se1.Equals(se2)); se1.AddElement("child1"); se1.AddElement("child2"); Assert.IsFalse(se1.Equals(se2)); se2.AddElement("child1"); se2.AddElement("child2"); Assert.IsTrue(se1.Equals(se2)); se1.AddAttribute("attr1"); Assert.IsFalse(se1.Equals(se2)); se2.AddAttribute("attr1"); Assert.IsTrue(se1.Equals(se2)); //GetHashCode Assert.AreEqual(XmlHelper.HashElement(se1), se1.GetHashCode()); //Clone SimpleElement se1c = (SimpleElement)se1.Clone(); Assert.AreEqual(se1c, se1); Assert.AreNotSame(se1, se1c); se1.Comment = "comment"; Assert.AreNotEqual(se1c, se1); }
public void TestAttributes() { SimpleElement se = new SimpleElement(); IDictionary attrs = se.Attributes; Assert.IsNotNull(attrs); Assert.AreEqual(attrs.Count, 0); Assert.IsFalse(attrs.IsReadOnly); Assert.IsFalse(attrs.IsFixedSize); Assert.IsNotNull(attrs.SyncRoot); Assert.IsFalse(attrs.IsSynchronized); SimpleValue sv = new SimpleValue("value"); Exception e = null; try { attrs.Add(3, sv); } catch (Exception ex) { e = ex; } Assert.IsNotNull(e); Assert.IsInstanceOf(typeof(ArgumentException), e); Assert.AreEqual(attrs.Count, 0); e = null; try { attrs.Add("?key1", sv); } catch (Exception ex) { e = ex; } Assert.IsNotNull(e); Assert.IsInstanceOf(typeof(ArgumentException), e); Assert.AreEqual(attrs.Count, 0); e = null; try { attrs.Add("key1", 3); } catch (Exception ex) { e = ex; } Assert.IsNotNull(e); Assert.IsInstanceOf(typeof(ArgumentException), e); Assert.AreEqual(attrs.Count, 0); Assert.IsNull(sv.Parent); Assert.IsFalse(sv.IsAttribute); attrs.Add("key1", sv); Assert.AreEqual(attrs.Count, 1); Assert.IsTrue(attrs.Contains("key1")); Assert.AreEqual(attrs.Keys.Count, 1); Assert.AreEqual(attrs.Values.Count, 1); IXmlValue v = (IXmlValue)attrs["key1"]; Assert.AreEqual(se, v.Parent); Assert.IsTrue(v.IsAttribute); SimpleElement se2 = (SimpleElement)se.Clone(); Assert.IsTrue(se.Attributes.Equals(se2.Attributes)); se.Attributes.Add("key2", v.Clone()); se2.Attributes.Add("key2", new SimpleValue(1000, true)); Assert.IsFalse(se.Attributes.Equals(se2.Attributes)); se.Attributes.Add("key3", v.Clone()); Assert.IsFalse(se.Attributes.Equals(se2.Attributes)); Assert.IsFalse(se.Attributes.Equals(se2)); Assert.AreEqual(attrs.Count, 3); Assert.IsInstanceOf(typeof(ICloneable), attrs); ICloneable cloneableAttrs = attrs as ICloneable; Assert.IsNotNull(cloneableAttrs); IDictionary clones = (IDictionary)cloneableAttrs.Clone(); Assert.AreEqual(clones.Count, 3); Assert.AreEqual(clones, attrs); attrs.Clear(); Assert.AreEqual(attrs.Count, 0); }