2 // WriterTest.cs: Unit Tests for ResXResourceWriter.
5 // Robert Jordan <robertj@gmx.net>
9 using System.Collections;
10 using System.ComponentModel;
12 using System.Globalization;
14 using System.Resources;
16 using System.Windows.Forms;
18 using NUnit.Framework;
20 namespace MonoTests.System.Resources
23 public class WriterTest
30 fileName = Path.GetTempFileName ();
34 public void TearDown ()
36 File.Delete (fileName);
39 // This is totally out of place here, but since this test seem to be the last one run, and since nunit doesn't
40 // offer any global TearDown method, it was the option I found.
41 int c = Application.OpenForms.Count;
43 Console.WriteLine ("HEY!");
44 Console.WriteLine ("You created " + c.ToString () + " form(s) and you didn't dispose of them!");
45 Console.WriteLine ("Please modify your test to shut me up.");
51 public void TestWriter ()
53 ResXResourceWriter w = new ResXResourceWriter (fileName);
54 w.AddResource ("String", "hola");
55 w.AddResource ("String2", (object) "hello");
56 w.AddResource ("Int", 42);
57 w.AddResource ("Enum", PlatformID.Win32NT);
58 w.AddResource ("Convertible", new Point (43, 45));
59 w.AddResource ("Serializable", new ArrayList (new byte [] { 1, 2, 3, 4 }));
60 w.AddResource ("ByteArray", new byte [] { 12, 13, 14 });
61 w.AddResource ("ByteArray2", (object) new byte [] { 15, 16, 17 });
62 w.AddResource ("IntArray", new int [] { 1012, 1013, 1014 });
63 w.AddResource ("StringArray", new string [] { "hello", "world" });
64 w.AddResource ("Image", new Bitmap (1, 1));
65 w.AddResource ("StrType", new MyStrType ("hello"));
66 w.AddResource ("BinType", new MyBinType ("world"));
69 w.AddResource ("NonSerType", new MyNonSerializableType ());
71 } catch (InvalidOperationException) {
77 ResXResourceReader r = new ResXResourceReader (fileName);
78 Hashtable h = new Hashtable ();
79 foreach (DictionaryEntry e in r) {
80 h.Add (e.Key, e.Value);
84 Assert.AreEqual ("hola", (string) h ["String"], "#1");
85 Assert.AreEqual ("hello", (string) h ["String2"], "#2");
86 Assert.AreEqual (42, (int) h ["Int"], "#3");
87 Assert.AreEqual (PlatformID.Win32NT, (PlatformID) h ["Enum"], "#4");
88 Assert.AreEqual (43, ((Point) h ["Convertible"]).X, "#5");
89 Assert.AreEqual (2, (byte) ((ArrayList) h ["Serializable"]) [1], "#6");
90 Assert.AreEqual (13, ((byte []) h ["ByteArray"]) [1], "#7");
91 Assert.AreEqual (16, ((byte []) h ["ByteArray2"]) [1], "#8");
92 Assert.AreEqual (1013, ((int []) h ["IntArray"]) [1], "#9");
93 Assert.AreEqual ("world", ((string []) h ["StringArray"]) [1], "#10");
94 Assert.AreEqual (typeof (Bitmap), h ["Image"].GetType (), "#11");
95 Assert.AreEqual ("hello", ((MyStrType) h ["StrType"]).Value, "#12");
96 Assert.AreEqual ("world", ((MyBinType) h ["BinType"]).Value, "#13");
98 File.Delete (fileName);
103 [TypeConverter (typeof (MyStrTypeConverter))]
104 public class MyStrType
108 public MyStrType (string s)
114 public class MyStrTypeConverter : TypeConverter
116 public override bool CanConvertTo (ITypeDescriptorContext context, Type destinationType)
118 if (destinationType == typeof (string))
120 return base.CanConvertTo (context, destinationType);
123 public override bool CanConvertFrom (ITypeDescriptorContext context, Type sourceType)
125 if (sourceType == typeof (string))
127 return base.CanConvertFrom (context, sourceType);
130 public override object ConvertTo (ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
132 if (destinationType == typeof (string))
133 return ((MyStrType) value).Value;
134 return base.ConvertTo (context, culture, value, destinationType);
137 public override object ConvertFrom (ITypeDescriptorContext context, CultureInfo culture, object value)
139 if (value.GetType () == typeof (string))
140 return new MyStrType ((string) value);
141 return base.ConvertFrom (context, culture, value);
146 [TypeConverter (typeof (MyBinTypeConverter))]
147 public class MyBinType
151 public MyBinType (string s)
157 public class MyBinTypeConverter : TypeConverter
159 public override bool CanConvertTo (ITypeDescriptorContext context, Type destinationType)
161 if (destinationType == typeof (byte []))
163 return base.CanConvertTo (context, destinationType);
166 public override bool CanConvertFrom (ITypeDescriptorContext context, Type sourceType)
168 if (sourceType == typeof (byte []))
170 return base.CanConvertFrom (context, sourceType);
173 public override object ConvertTo (ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
175 if (destinationType == typeof (byte []))
176 return Encoding.Default.GetBytes (((MyBinType) value).Value);
177 return base.ConvertTo (context, culture, value, destinationType);
180 public override object ConvertFrom (ITypeDescriptorContext context, CultureInfo culture, object value)
182 if (value.GetType () == typeof (byte []))
183 return new MyBinType (Encoding.Default.GetString ((byte []) value));
184 return base.ConvertFrom (context, culture, value);
188 [TypeConverter (typeof (MyNonSerializableTypeConverter))]
189 public class MyNonSerializableType
191 public MyNonSerializableType ()
196 public class MyNonSerializableTypeConverter : TypeConverter
198 public override bool CanConvertTo (ITypeDescriptorContext context, Type destinationType)
200 if (destinationType == typeof (byte []))
202 return base.CanConvertTo (context, destinationType);
205 public override bool CanConvertFrom (ITypeDescriptorContext context, Type sourceType)
207 if (sourceType == typeof (byte []))
209 return base.CanConvertFrom (context, sourceType);
212 public override object ConvertTo (ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
214 if (destinationType == typeof (byte []))
215 return new byte [] { 0, 1, 2, 3 };
216 return base.ConvertTo (context, culture, value, destinationType);
219 public override object ConvertFrom (ITypeDescriptorContext context, CultureInfo culture, object value)
221 if (value.GetType () == typeof (byte []))
222 return new MyNonSerializableType ();
223 return base.ConvertFrom (context, culture, value);