Exemplo n.º 1
0
        public void StringTest()
        {
            string testStr = "123";

            Assert.AreEqual(123, Convert.ToSByte(testStr));
            Assert.AreEqual(123, Convert.ToInt16(testStr));
            Assert.AreEqual(123, Convert.ToInt32(testStr));
            Assert.AreEqual(123, Convert.ToInt64(testStr));

            Assert.AreEqual(123, Convert.ToByte(testStr));
            Assert.AreEqual(123, Convert.ToUInt16(testStr));
            Assert.AreEqual(123, Convert.ToUInt32(testStr));
            Assert.AreEqual(123, Convert.ToUInt64(testStr));

            Assert.AreEqual(123.0m, Convert.ToDecimal(testStr));
            Assert.AreEqual(123.0f, Convert.ToSingle(testStr));
            Assert.AreEqual(123.0, Convert.ToDouble(testStr));

            string zeroStr = "0";

            Assert.AreEqual(0, Convert.ToSByte((string)null));
            Assert.AreEqual(0, Convert.ToSByte(zeroStr));
            Assert.AreEqual(0, Convert.ToInt16(zeroStr));
            Assert.AreEqual(0, Convert.ToInt32(zeroStr));
            Assert.AreEqual(0, Convert.ToInt64(zeroStr));

            Assert.AreEqual(0, Convert.ToByte(zeroStr));
            Assert.AreEqual(0, Convert.ToUInt16(zeroStr));
            Assert.AreEqual(0, Convert.ToUInt32(zeroStr));
            Assert.AreEqual(0, Convert.ToUInt64(zeroStr));

            Assert.AreEqual(0.0m, Convert.ToDecimal(zeroStr));
            Assert.AreEqual(0.0f, Convert.ToSingle(zeroStr));
            Assert.AreEqual(0.0, Convert.ToDouble(zeroStr));

            Assert.IsTrue(Convert.ToBoolean("True"));
            Assert.IsTrue(Convert.ToBoolean("true"));

            Assert.IsFalse(Convert.ToBoolean("false"));
            Assert.IsFalse(Convert.ToBoolean("FALSE"));
            Assert.IsFalse(Convert.ToBoolean((string)null));

            Assert.AreEqual('T', Convert.ToChar("T"));
            Assert.AreEqual(0, Convert.ToChar((string)null));

            Assert.AreEqual(DateTime.Today, Convert.ToDateTime(DateTime.Today.ToString()));
            Assert.AreEqual(DateTime.MinValue, Convert.ToDateTime((string)null));

            Assert.AreEqual(TimeSpan.FromDays(123.0), Convert.ToTimeSpan("123"));
            Assert.AreEqual(TimeSpan.MinValue, Convert.ToTimeSpan((string)null));

            Assert.AreEqual(typeof(int).GUID, Convert.ToGuid(typeof(int).GUID.ToString()));
            Assert.AreEqual(Guid.Empty, Convert.ToGuid((string)null));

            Assert.AreEqual(typeof(int), Convert.ToType("System.Int32, mscorlib"));
            Assert.IsNull(Convert.ToType((string)null));
        }