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 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 TestObjectMethods() { bool v1 = true; SimpleValue sv1 = new SimpleValue(v1); int v2 = 5; SimpleValue sv2 = new SimpleValue(v2); long v3 = 999L; SimpleValue sv3 = new SimpleValue(v3); double v4 = 5.257; SimpleValue sv4 = new SimpleValue(v4); decimal v5 = decimal.MinusOne; SimpleValue sv5 = new SimpleValue(v5); string v6 = "test"; SimpleValue sv6 = new SimpleValue(v6); Binary v7 = new Binary(new byte[] { 99, 50, 27 }); SimpleValue sv7 = new SimpleValue(v7); DateTime v8 = new DateTime(1976, 4, 19); SimpleValue sv8 = new SimpleValue(v8); //ToString Assert.AreEqual(v1.ToString(), sv1.ToString()); Assert.AreEqual(v2.ToString(), sv2.ToString()); Assert.AreEqual(v3.ToString(), sv3.ToString()); Assert.AreEqual(v4.ToString(), sv4.ToString()); Assert.AreEqual(v5.ToString(), sv5.ToString()); Assert.AreEqual(v6, sv6.ToString()); Assert.AreEqual(Convert.ToBase64String(v7.ToByteArray()), sv7.ToString()); Assert.AreEqual(v8.ToString(), sv8.ToString()); //Equals sv1.SetBoolean(false); sv6.SetString("False"); Assert.IsTrue(sv1.Equals(sv6)); Assert.IsFalse(sv1.Equals(sv7)); Assert.IsFalse(sv1.Equals("test")); //GetHashCode Assert.AreEqual(XmlHelper.HashValue(sv6), sv6.GetHashCode()); //Clone SimpleValue sv1c = (SimpleValue)sv1.Clone(); SimpleValue sv2c = (SimpleValue)sv2.Clone(); SimpleValue sv3c = (SimpleValue)sv3.Clone(); SimpleValue sv4c = (SimpleValue)sv4.Clone(); SimpleValue sv5c = (SimpleValue)sv5.Clone(); SimpleValue sv6c = (SimpleValue)sv6.Clone(); SimpleValue sv7c = (SimpleValue)sv7.Clone(); SimpleValue sv8c = (SimpleValue)sv8.Clone(); Assert.AreEqual(sv1c, sv1); Assert.AreNotSame(sv1c, sv1); Assert.AreEqual(sv2c, sv2); Assert.AreNotSame(sv2c, sv2); Assert.AreEqual(sv3c, sv3); Assert.AreNotSame(sv3c, sv3); Assert.AreEqual(sv4c, sv4); Assert.AreNotSame(sv4c, sv4); Assert.AreEqual(sv5c, sv5); Assert.AreNotSame(sv5c, sv5); Assert.AreEqual(sv6c, sv6); Assert.AreNotSame(sv6c, sv6); Assert.AreEqual(sv7c, sv7); Assert.AreNotSame(sv7c, sv7); Assert.AreEqual(sv8c, sv8); Assert.AreNotSame(sv8c, sv8); Assert.IsTrue(sv1c.IsMutable); Assert.IsNull(sv1c.Parent); sv6.SetString("newtest"); Assert.AreNotEqual(sv6c, sv6); }
public void TestValueWithException2() { SimpleValue sv = new SimpleValue("value", true, false); sv.SetString(null); }
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)); }