Exemplo n.º 1
0
        public void TestDefaultValueAccessors()
        {
            SimpleValue sv = new SimpleValue();

            Assert.IsNull(sv.Value);
            Assert.AreEqual(true, sv.GetBoolean(true));
            Assert.AreEqual(5, sv.GetInt(5));
            Assert.AreEqual(long.MaxValue, sv.GetLong(long.MaxValue));
            Assert.AreEqual("test", sv.GetString("test"));
            Assert.AreEqual(double.MaxValue, sv.GetDouble(double.MaxValue));
            Assert.AreEqual(decimal.MinValue, sv.GetDecimal(decimal.MinValue));
            Assert.AreEqual(Binary.NO_BINARY, sv.GetBinary(Binary.NO_BINARY));
            DateTime dt = DateTime.Now;

            Assert.AreEqual(dt, sv.GetDateTime(dt));

            sv.SetBoolean(false);
            Assert.AreNotEqual(true, sv.GetBoolean(true));
            sv.SetInt(100);
            Assert.AreNotEqual(5, sv.GetInt(5));
            sv.SetLong(100L);
            Assert.AreNotEqual((object)long.MaxValue, (object)sv.GetLong(long.MaxValue));
            sv.SetString("testing");
            Assert.AreNotEqual("test", sv.GetString("test"));
            sv.SetDouble(100D);
            Assert.AreNotEqual(double.MaxValue, sv.GetDouble(double.MaxValue));
            sv.SetDecimal(decimal.MinusOne);
            Assert.AreNotEqual(decimal.MinValue, sv.GetDecimal(decimal.MinValue));
            sv.SetBinary(new Binary(new byte[] { 3, 4, 5 }));
            Assert.AreNotEqual(Binary.NO_BINARY, sv.GetBinary(Binary.NO_BINARY));
            sv.SetDateTime(DateTime.Today);
            Assert.AreNotEqual(dt, sv.GetDateTime(dt));
        }
        public void TestObjectHelpers()
        {
            Exception e = null;

            //EqualsValue
            SimpleValue sv1 = new SimpleValue("");
            SimpleValue sv2 = new SimpleValue(Binary.NO_BINARY);

            Assert.IsTrue(XmlHelper.EqualsValue(sv1, sv2)); //both are empty
            sv1.SetString("True");
            Assert.IsFalse(XmlHelper.EqualsValue(sv1, sv2));
            sv2.SetBoolean(true);
            Assert.IsTrue(XmlHelper.EqualsValue(sv1, sv2));
            try
            {
                XmlHelper.EqualsValue(null, sv2);
            }
            catch (Exception ex)
            {
                e = ex;
            }
            Assert.IsNotNull(e);
            Assert.IsInstanceOf(typeof(ArgumentException), e);
            sv2.SetString(null);
            try
            {
                XmlHelper.EqualsValue(sv1, sv2);
            }
            catch (Exception ex)
            {
                e = ex;
            }
            Assert.IsNotNull(e);
            Assert.IsInstanceOf(typeof(ArgumentException), e);
            sv2.SetBoolean(true);

            //EqualsElement
            SimpleElement se1 = new SimpleElement();
            SimpleElement se2 = new SimpleElement("name", "value");

            se2.Comment = "comment";
            Assert.IsFalse(XmlHelper.EqualsElement(se1, se2));
            se1.Name = se2.Name;
            Assert.IsFalse(XmlHelper.EqualsElement(se1, se2));
            se1.SetString(se2.GetString());
            Assert.IsFalse(XmlHelper.EqualsElement(se1, se2));
            se1.Comment = se2.Comment;
            Assert.IsTrue(XmlHelper.EqualsElement(se1, se2));
            se1.AddElement("el1");
            Assert.IsFalse(XmlHelper.EqualsElement(se1, se2));
            se2.AddElement("el1");
            Assert.IsTrue(XmlHelper.EqualsElement(se1, se2));
            se1.AddAttribute("attr1");
            Assert.IsFalse(XmlHelper.EqualsElement(se1, se2));
            se2.AddAttribute("attr1");
            Assert.IsTrue(XmlHelper.EqualsElement(se1, se2));
            try
            {
                XmlHelper.EqualsElement(null, se2);
            }
            catch (Exception ex)
            {
                e = ex;
            }
            Assert.IsNotNull(e);
            Assert.IsInstanceOf(typeof(ArgumentException), e);

            //HashValue
            SimpleValue sv = new SimpleValue();

            Assert.IsNull(sv.Value);
            Assert.AreEqual(0, XmlHelper.HashValue(sv));
            sv.SetString("test");
            Assert.AreEqual("test".GetHashCode(), XmlHelper.HashValue(sv));
            sv.SetDecimal(decimal.MinusOne);
            Assert.AreEqual(XmlHelper.Convert(decimal.MinusOne, XmlValueType.String).GetHashCode(),
                            XmlHelper.HashValue(sv));
            Assert.AreEqual(XmlHelper.HashValue(sv1), XmlHelper.HashValue(sv2));

            //HashElement
            Assert.AreEqual(XmlHelper.HashElement(se1), XmlHelper.HashElement(se2));
            IXmlElement el1 = se1.GetElement("el1");

            el1.SetString("new value");
            Assert.AreNotEqual(XmlHelper.HashElement(se1), XmlHelper.HashElement(se2));
        }