2 // System.ComponentModel.CultureInfoConverter test cases
5 // Gert Driesen (drieseng@users.sourceforge.net)
7 // (c) 2008 Gert Driesen
11 using System.ComponentModel;
12 using System.ComponentModel.Design.Serialization;
13 using System.Globalization;
15 using NUnit.Framework;
17 namespace MonoTests.System.ComponentModel
20 public class CultureInfoConverterTest
22 private CultureInfoConverter converter;
27 converter = new CultureInfoConverter ();
31 public void CanConvertFrom ()
33 Assert.IsTrue (converter.CanConvertFrom (typeof (string)), "#1");
34 Assert.IsFalse (converter.CanConvertFrom (typeof (CultureInfo)), "#2");
35 Assert.IsFalse (converter.CanConvertFrom (typeof (object)), "#3");
36 Assert.IsFalse (converter.CanConvertFrom (typeof (int)), "#4");
37 Assert.IsTrue (converter.CanConvertFrom (typeof (InstanceDescriptor)), "#5");
41 public void CanConvertTo ()
43 Assert.IsTrue (converter.CanConvertTo (typeof (string)), "#1");
44 Assert.IsFalse (converter.CanConvertTo (typeof (object)), "#2");
45 Assert.IsFalse (converter.CanConvertTo (typeof (CultureInfo)), "#3");
46 Assert.IsFalse (converter.CanConvertTo (typeof (int)), "#4");
47 Assert.IsTrue (converter.CanConvertTo (typeof (InstanceDescriptor)), "#5");
51 public void ConvertFrom_String ()
55 c = (CultureInfo) converter.ConvertFrom (null, CultureInfo.InvariantCulture,
57 Assert.AreEqual (CultureInfo.InvariantCulture, c, "#1");
59 c = (CultureInfo) converter.ConvertFrom (null, CultureInfo.InvariantCulture,
61 Assert.AreEqual (new CultureInfo ("nl-BE"), c, "#2");
63 c = (CultureInfo) converter.ConvertFrom (null, CultureInfo.InvariantCulture,
65 Assert.AreEqual (new CultureInfo ("nl"), c, "#3");
67 c = (CultureInfo) converter.ConvertFrom (null, CultureInfo.InvariantCulture,
69 Assert.AreEqual (new CultureInfo ("nl-BE"), c, "#4");
71 c = (CultureInfo) converter.ConvertFrom (null, CultureInfo.InvariantCulture,
73 Assert.AreEqual (new CultureInfo ("nl-BE"), c, "#5");
75 c = (CultureInfo) converter.ConvertFrom (null, CultureInfo.InvariantCulture,
77 Assert.AreEqual (CultureInfo.InvariantCulture, c, "#6");
80 c = (CultureInfo) converter.ConvertFrom (null, CultureInfo.InvariantCulture,
82 Assert.AreEqual (CultureInfo.InvariantCulture, c, "#6");
87 public void ConvertFrom_String_IncompleteName ()
90 converter.ConvertFrom (null, CultureInfo.InvariantCulture,
93 } catch (ArgumentException ex) {
94 // The nl-B culture cannot be converted to a
95 // CultureInfo object on this computer
96 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
97 Assert.IsNull (ex.InnerException, "#3");
98 Assert.IsNotNull (ex.Message, "#4");
99 Assert.IsTrue (ex.Message.IndexOf (typeof (CultureInfo).Name) != -1, "#5");
100 Assert.IsTrue (ex.Message.IndexOf ("nl-B") != -1, "#6");
101 Assert.IsNull (ex.ParamName, "#7");
106 public void ConvertFrom_String_InvalidCulture ()
110 converter.ConvertFrom (null, CultureInfo.InvariantCulture,
113 } catch (ArgumentException ex) {
114 // The (default) culture cannot be converted to
115 // a CultureInfo object on this computer
116 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#A2");
117 Assert.IsNull (ex.InnerException, "#A3");
118 Assert.IsNotNull (ex.Message, "#A4");
119 Assert.IsTrue (ex.Message.IndexOf (typeof (CultureInfo).Name) != -1, "#A5");
120 Assert.IsTrue (ex.Message.IndexOf ("(default)") != -1, "#A6");
121 Assert.IsNull (ex.ParamName, "#A7");
126 converter.ConvertFrom (null, CultureInfo.InvariantCulture,
129 } catch (ArgumentException ex) {
130 // The culture cannot be converted to
131 // a CultureInfo object on this computer
132 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#B2");
133 Assert.IsNull (ex.InnerException, "#B3");
134 Assert.IsNotNull (ex.Message, "#B4");
135 Assert.IsTrue (ex.Message.IndexOf (typeof (CultureInfo).Name) != -1, "#B5");
136 Assert.IsTrue (ex.Message.IndexOf (" ") != -1, "#B6");
137 Assert.IsNull (ex.ParamName, "#B7");
141 converter.ConvertFrom (null, CultureInfo.InvariantCulture,
144 } catch (ArgumentException ex) {
145 // The \r\n culture cannot be converted to
146 // a CultureInfo object on this computer
147 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#C2");
148 Assert.IsNull (ex.InnerException, "#C3");
149 Assert.IsNotNull (ex.Message, "#C4");
150 Assert.IsTrue (ex.Message.IndexOf (typeof (CultureInfo).Name) != -1, "#C5");
151 Assert.IsTrue (ex.Message.IndexOf ("\r\n") != -1, "#C6");
152 Assert.IsNull (ex.ParamName, "#C7");
157 public void ConvertFrom_Value_Null ()
160 converter.ConvertFrom (null, CultureInfo.InvariantCulture,
163 } catch (NotSupportedException ex) {
164 // CultureInfoConverter cannot convert from (null)
165 Assert.AreEqual (typeof (NotSupportedException), ex.GetType (), "#2");
166 Assert.IsNull (ex.InnerException, "#3");
167 Assert.IsNotNull (ex.Message, "#4");
168 Assert.IsTrue (ex.Message.IndexOf (typeof (CultureInfoConverter).Name) != -1, "#5");
169 Assert.IsTrue (ex.Message.IndexOf ("(null)") != -1, "#6");
174 public void ConvertToString ()
178 result = converter.ConvertToString (null, CultureInfo.InvariantCulture,
179 new MyCultureInfo ());
180 Assert.AreEqual ("display", result, "#1");
182 result = converter.ConvertToString (null, CultureInfo.InvariantCulture,
184 Assert.AreEqual ("(Default)", result, "#2");
186 result = converter.ConvertToString (null, CultureInfo.InvariantCulture,
187 CultureInfo.InvariantCulture);
188 Assert.AreEqual ("(Default)", result, "#3");
190 result = converter.ConvertToString (null, CultureInfo.InvariantCulture,
191 new CultureInfo ("nl-BE"));
192 Assert.AreEqual ("Dutch (Belgium)", result, "#4");
196 private sealed class MyCultureInfo : CultureInfo
198 internal MyCultureInfo () : base ("nl-BE")
202 public override string DisplayName {
203 get { return "display"; }
206 public override string EnglishName {
207 get { return "english"; }
213 public void GetCultureName ()
215 CustomCultureInfoConverter custom_converter = new CustomCultureInfoConverter ();
217 CultureInfo fr_culture = CultureInfo.GetCultureInfo ("fr-FR");
218 Assert.AreEqual (fr_culture.Name, custom_converter.GetCultureName (fr_culture), "#A1");
220 CultureInfo es_culture = CultureInfo.GetCultureInfo ("es-MX");
221 Assert.AreEqual (es_culture.Name, custom_converter.GetCultureName (es_culture), "#A2");
224 class CustomCultureInfoConverter : CultureInfoConverter
226 public new string GetCultureName (CultureInfo culture)
228 return base.GetCultureName (culture);