示例#1
0
        public void TestAttributeMethods()
        {
            SimpleElement se = new SimpleElement();

            Assert.AreEqual(se.Attributes.Count, 0);

            IXmlValue attr1 = se.AddAttribute("attr1");

            attr1.SetString("value1");
            Assert.AreEqual(se.Attributes.Count, 1);

            Assert.IsNull(se.GetAttribute("attr2"));
            Assert.AreEqual(attr1, se.GetAttribute("attr1"));
            Exception e = null;

            try
            {
                se.GetAttribute(null);
            }
            catch (Exception ex)
            {
                e = ex;
            }
            Assert.IsNotNull(e);
            Assert.IsInstanceOf(typeof(ArgumentException), e);

            se.AddAttribute("attr2");
            Assert.AreEqual(se.Attributes.Count, 2);
            se.SetAttribute("attr2", null);
            Assert.AreEqual(se.Attributes.Count, 1);
            se.SetAttribute("attr2", new SimpleValue("value2"));
            Assert.AreEqual(se.Attributes.Count, 2);

            Assert.IsNull(se.GetAttribute("attr3"));
            IXmlValue attr3 = se.GetSafeAttribute("attr3");

            Assert.IsNotNull(attr3);
            Assert.IsFalse(attr3.IsMutable);
        }
示例#2
0
        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);
        }