// // System.ComponentModel.SingleConverter test cases // // Authors: // Gert Driesen (drieseng@users.sourceforge.net) // // (c) 2005 Novell, Inc. (http://www.ximian.com) // using System; using System.ComponentModel; using System.ComponentModel.Design.Serialization; using System.Globalization; using NUnit.Framework; namespace MonoTests.System.ComponentModel { [TestFixture] public class SingleConverterTests { private SingleConverter converter; [SetUp] public void SetUp () { converter = new SingleConverter (); } [Test] public void CanConvertFrom () { Assert.IsTrue (converter.CanConvertFrom (typeof (string)), "#1"); Assert.IsFalse (converter.CanConvertFrom (typeof (float)), "#2"); Assert.IsFalse (converter.CanConvertFrom (typeof (object)), "#3"); Assert.IsTrue (converter.CanConvertFrom (typeof (InstanceDescriptor)), "#4"); } [Test] public void CanConvertTo () { Assert.IsTrue (converter.CanConvertTo (typeof (string)), "#1"); Assert.IsFalse (converter.CanConvertTo (typeof (object)), "#2"); Assert.IsTrue (converter.CanConvertTo (typeof (int)), "#3"); } [Test] public void ConvertFrom_String () { Assert.AreEqual (10, converter.ConvertFrom (null, CultureInfo.InvariantCulture, "10"), "#1"); } [Test] [ExpectedException (typeof (NotSupportedException))] public void ConvertFrom_Object () { converter.ConvertFrom (new object ()); } [Test] [ExpectedException (typeof (NotSupportedException))] public void ConvertFrom_Int32 () { converter.ConvertFrom (int.MaxValue); } [Test] public void ConvertTo () { Assert.AreEqual (1.5f.ToString (CultureInfo.InvariantCulture), converter.ConvertTo (null, CultureInfo.InvariantCulture, 1.5f, typeof (string)), "#1"); Assert.AreEqual (1.5f.ToString (CultureInfo.CurrentCulture), converter.ConvertTo (null, CultureInfo.CurrentCulture, 1.5f, typeof (string)), "#2"); Assert.AreEqual (1.5f.ToString (CultureInfo.CurrentCulture), converter.ConvertTo (1.5f, typeof (string)), "#3"); } [Test] public void ConvertToString () { CultureInfo culture = new MyCultureInfo (); NumberFormatInfo numberFormatInfo = (NumberFormatInfo) culture.GetFormat (typeof (NumberFormatInfo)); Assert.AreEqual (numberFormatInfo.NegativeSign + "5", converter.ConvertToString (null, culture, (float) -5), "#1"); Assert.AreEqual (culture.NumberFormat.NegativeSign + "5", converter.ConvertToString (null, culture, (int) -5), "#2"); } [Test] public void ConvertFromString () { CultureInfo culture = new MyCultureInfo (); NumberFormatInfo numberFormatInfo = (NumberFormatInfo) culture.GetFormat (typeof (NumberFormatInfo)); Assert.AreEqual (-5, converter.ConvertFrom (null, culture, numberFormatInfo.NegativeSign + "5")); } [Test] public void ConvertFromString_Invalid1 () { try { converter.ConvertFromString (null, CultureInfo.InvariantCulture, "*1"); Assert.Fail ("#1"); } catch (AssertionException) { throw; } catch (Exception ex) { Assert.AreEqual (typeof (Exception), ex.GetType (), "#2"); Assert.IsNotNull (ex.InnerException, "#3"); Assert.AreEqual (typeof (FormatException), ex.InnerException.GetType (), "#4"); } } [Test] public void ConvertFromString_Invalid2 () { try { converter.ConvertFromString ("*1"); Assert.Fail ("#1"); } catch (AssertionException) { throw; } catch (Exception ex) { Assert.AreEqual (typeof (Exception), ex.GetType (), "#2"); Assert.IsNotNull (ex.InnerException, "#3"); Assert.AreEqual (typeof (FormatException), ex.InnerException.GetType (), "#4"); } } [Test] public void ConvertFrom_InvalidString1 () { try { converter.ConvertFrom (null, CultureInfo.InvariantCulture, "*1"); Assert.Fail ("#1"); } catch (AssertionException) { throw; } catch (Exception ex) { Assert.AreEqual (typeof (Exception), ex.GetType (), "#2"); Assert.IsNotNull (ex.InnerException, "#3"); Assert.AreEqual (typeof (FormatException), ex.InnerException.GetType (), "#4"); } } [Test] public void ConvertFrom_InvalidString2 () { try { converter.ConvertFrom ("*1"); Assert.Fail ("#1"); } catch (AssertionException) { throw; } catch (Exception ex) { Assert.AreEqual (typeof (Exception), ex.GetType (), "#2"); Assert.IsNotNull (ex.InnerException, "#3"); Assert.AreEqual (typeof (FormatException), ex.InnerException.GetType (), "#4"); } } [Test] public void ConvertFromString_Hex1 () { try { converter.ConvertFromString ("#10"); Assert.Fail ("#1"); } catch (AssertionException) { throw; } catch (Exception ex) { Assert.AreEqual (typeof (Exception), ex.GetType (), "#2"); Assert.IsNotNull (ex.InnerException, "#3"); Assert.AreEqual (typeof (FormatException), ex.InnerException.GetType (), "#4"); } } [Test] public void ConvertFromString_Hex2 () { try { converter.ConvertFromString ("0x10"); Assert.Fail ("#1"); } catch (AssertionException) { throw; } catch (Exception ex) { Assert.AreEqual (typeof (Exception), ex.GetType (), "#2"); Assert.IsNotNull (ex.InnerException, "#3"); Assert.AreEqual (typeof (FormatException), ex.InnerException.GetType (), "#4"); } } [Test] public void ConvertFrom_HexString1 () { try { converter.ConvertFrom ("#10"); Assert.Fail ("#1"); } catch (AssertionException) { throw; } catch (Exception ex) { Assert.AreEqual (typeof (Exception), ex.GetType (), "#2"); Assert.IsNotNull (ex.InnerException, "#3"); Assert.AreEqual (typeof (FormatException), ex.InnerException.GetType (), "#4"); } } [Test] public void ConvertFrom_HexString2 () { try { converter.ConvertFrom ("0x10"); Assert.Fail ("#1"); } catch (AssertionException) { throw; } catch (Exception ex) { Assert.AreEqual (typeof (Exception), ex.GetType (), "#2"); Assert.IsNotNull (ex.InnerException, "#3"); Assert.AreEqual (typeof (FormatException), ex.InnerException.GetType (), "#4"); } } [Serializable] private sealed class MyCultureInfo : CultureInfo { internal MyCultureInfo () : base ("en-US") { } public override object GetFormat (Type formatType) { if (formatType == typeof (NumberFormatInfo)) { NumberFormatInfo nfi = (NumberFormatInfo) ((NumberFormatInfo) base.GetFormat (formatType)).Clone (); nfi.NegativeSign = "myNegativeSign"; return NumberFormatInfo.ReadOnly (nfi); } else { return base.GetFormat (formatType); } } #if NET_2_0 // adding this override in 1.x shows different result in .NET (it is ignored). // Some compatibility kids might want to fix this issue. public override NumberFormatInfo NumberFormat { get { NumberFormatInfo nfi = (NumberFormatInfo) base.NumberFormat.Clone (); nfi.NegativeSign = "myNegativeSign"; return nfi; } set { throw new NotSupportedException (); } } #endif } } }