public void ConvertibleConversionStrategyTest() { var strategy = new ConvertibleConversionStrategy(); Assert.IsFalse(strategy.CanConvert(typeof(object), typeof(object))); Assert.IsTrue(strategy.CanConvert(typeof(int), typeof(string))); Assert.IsTrue(strategy.CanConvert(typeof(string), typeof(int))); Assert.IsTrue(strategy.CanConvert(typeof(int?), typeof(string))); Assert.IsTrue(strategy.CanConvert(typeof(string), typeof(int?))); var intVal = (int)strategy.Convert(typeof(string), typeof(int), "5", Thread.CurrentThread.CurrentCulture); Assert.AreEqual(5, intVal); var strVal = strategy.Convert(typeof(int), typeof(string), 5, Thread.CurrentThread.CurrentCulture) as string; Assert.AreEqual("5", strVal); int?nullIntVal = null; nullIntVal = (int?)strategy.Convert(typeof(string), typeof(int?), "5", Thread.CurrentThread.CurrentCulture); Assert.AreEqual(5, nullIntVal); strVal = strategy.Convert(typeof(int), typeof(string), nullIntVal, Thread.CurrentThread.CurrentCulture) as string; Assert.AreEqual("5", strVal); }
public void ConvertibleConversionStrategyTest() { var strategy = new ConvertibleConversionStrategy(); object newValue; Assert.IsFalse(strategy.TryConvert(new object(), typeof(object), out newValue)); Assert.IsTrue(strategy.TryConvert(1, typeof(string), out newValue)); Assert.AreEqual("1", newValue); Assert.IsTrue(strategy.TryConvert("2", typeof(int), out newValue)); Assert.AreEqual(2, newValue); Assert.IsTrue(strategy.TryConvert(new int?(3), typeof(string), out newValue)); Assert.AreEqual("3", newValue); Assert.IsTrue(strategy.TryConvert("4", typeof(int?), out newValue)); Assert.AreEqual(4, newValue); Assert.IsFalse(strategy.TryConvert("Hello", typeof(int), out newValue)); }