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 NUnit.Framework;
18 namespace MonoTests.System.Resources
21 public class WriterTest
23 string fileName = Path.GetTempFileName ();
26 public void TestWriter ()
28 ResXResourceWriter w = new ResXResourceWriter (fileName);
29 w.AddResource ("String", "hola");
30 w.AddResource ("String2", (object) "hello");
31 w.AddResource ("Int", 42);
32 w.AddResource ("Enum", PlatformID.Win32NT);
33 w.AddResource ("Convertible", new Point (43, 45));
34 w.AddResource ("Serializable", new ArrayList(new byte[] {1, 2, 3, 4}));
35 w.AddResource ("ByteArray", new byte[] {12, 13, 14});
36 w.AddResource ("ByteArray2", (object) new byte[] {15, 16, 17});
37 w.AddResource ("IntArray", new int[] {1012, 1013, 1014});
38 w.AddResource ("StringArray", new string[] {"hello", "world"});
39 w.AddResource ("Image", new Bitmap (1, 1));
40 w.AddResource ("StrType", new MyStrType ("hello"));
41 w.AddResource ("BinType", new MyBinType ("world"));
44 w.AddResource ("NonSerType", new MyNonSerializableType ());
46 } catch (InvalidOperationException) {
52 ResXResourceReader r = new ResXResourceReader (fileName);
53 Hashtable h = new Hashtable();
54 foreach (DictionaryEntry e in r) {
55 h.Add (e.Key, e.Value);
59 Assert.AreEqual ("hola", (string) h["String"], "#1");
60 Assert.AreEqual ("hello", (string) h["String2"], "#2");
61 Assert.AreEqual (42, (int) h["Int"], "#3");
62 Assert.AreEqual (PlatformID.Win32NT, (PlatformID) h["Enum"], "#4");
63 Assert.AreEqual (43, ((Point) h["Convertible"]).X, "#5");
64 Assert.AreEqual (2, (byte) ((ArrayList) h["Serializable"])[1], "#6");
65 Assert.AreEqual (13, ((byte[]) h["ByteArray"])[1], "#7");
66 Assert.AreEqual (16, ((byte[]) h["ByteArray2"])[1], "#8");
67 Assert.AreEqual (1013, ((int[]) h["IntArray"])[1], "#9");
68 Assert.AreEqual ("world", ((string[]) h["StringArray"])[1], "#10");
69 Assert.AreEqual (typeof (Bitmap), h["Image"].GetType (), "#11");
70 Assert.AreEqual ("hello", ((MyStrType) h["StrType"]).Value, "#12");
71 Assert.AreEqual ("world", ((MyBinType) h["BinType"]).Value, "#13");
73 File.Delete (fileName);
78 [TypeConverter (typeof (MyStrTypeConverter))]
79 public class MyStrType
83 public MyStrType (string s)
89 public class MyStrTypeConverter : TypeConverter
91 public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
93 if (destinationType == typeof (string))
95 return base.CanConvertTo (context, destinationType);
98 public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
100 if (sourceType == typeof (string))
102 return base.CanConvertFrom (context, sourceType);
105 public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
107 if (destinationType == typeof (string))
108 return ((MyStrType) value).Value;
109 return base.ConvertTo (context, culture, value, destinationType);
112 public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
114 if (value.GetType() == typeof (string))
115 return new MyStrType((string) value);
116 return base.ConvertFrom (context, culture, value);
123 [TypeConverter (typeof (MyBinTypeConverter))]
124 public class MyBinType
128 public MyBinType (string s)
134 public class MyBinTypeConverter : TypeConverter
136 public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
138 if (destinationType == typeof (byte[]))
140 return base.CanConvertTo (context, destinationType);
143 public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
145 if (sourceType == typeof (byte[]))
147 return base.CanConvertFrom (context, sourceType);
150 public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
152 if (destinationType == typeof (byte[]))
153 return Encoding.Default.GetBytes (((MyBinType) value).Value);
154 return base.ConvertTo (context, culture, value, destinationType);
157 public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
159 if (value.GetType() == typeof (byte[]))
160 return new MyBinType (Encoding.Default.GetString ((byte[]) value));
161 return base.ConvertFrom (context, culture, value);
167 [TypeConverter (typeof (MyNonSerializableTypeConverter))]
168 public class MyNonSerializableType
170 public MyNonSerializableType ()
175 public class MyNonSerializableTypeConverter : TypeConverter
177 public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
179 if (destinationType == typeof (byte[]))
181 return base.CanConvertTo (context, destinationType);
184 public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
186 if (sourceType == typeof (byte[]))
188 return base.CanConvertFrom (context, sourceType);
191 public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
193 if (destinationType == typeof (byte[]))
194 return new byte[] {0, 1, 2, 3};
195 return base.ConvertTo (context, culture, value, destinationType);
198 public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
200 if (value.GetType() == typeof (byte[]))
201 return new MyNonSerializableType ();
202 return base.ConvertFrom (context, culture, value);