1 // Copyright 2004-2008 Castle Project - http://www.castleproject.org/
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
7 // http://www.apache.org/licenses/LICENSE-2.0
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
15 namespace Castle
.Components
.Binder
.Tests
18 using System
.Collections
;
19 using System
.Collections
.Generic
;
20 using System
.Globalization
;
21 using System
.Threading
;
22 using NUnit
.Framework
;
25 public class ConverterTestCase
27 private bool convSucceed
;
28 private DefaultConverter converter
;
33 converter
= new DefaultConverter();
39 CultureInfo en
= CultureInfo
.CreateSpecificCulture("en");
41 Thread
.CurrentThread
.CurrentCulture
= en
;
42 Thread
.CurrentThread
.CurrentUICulture
= en
;
46 public void StringConvert()
48 Assert
.AreEqual("hello", Convert(typeof(string), "hello"));
49 Assert
.IsTrue(convSucceed
);
51 Assert
.AreEqual(null, Convert(typeof(string), null));
52 Assert
.IsFalse(convSucceed
);
54 Assert
.AreEqual("\n \t", Convert(typeof(string), " \n \t "));
55 Assert
.IsTrue(convSucceed
);
57 Assert
.AreEqual(null, Convert(typeof(string), ""));
58 Assert
.IsTrue(convSucceed
);
62 public void ArrayConvert()
64 Assert
.AreEqual(new int[] {1, 2, 3}
, Convert(typeof(int[]), "1,2,3"));
65 Assert
.IsTrue(convSucceed
);
67 Assert
.AreEqual(null, Convert(typeof(int[]), null));
68 Assert
.IsFalse(convSucceed
);
70 Assert
.AreEqual(new int[] {}, Convert(typeof(int[]), ""));
71 Assert
.IsTrue(convSucceed
);
75 public void EnumConvert()
77 Assert
.AreEqual(UriPartial
.Scheme
, Convert(typeof(UriPartial
), UriPartial
.Scheme
.ToString("D")));
78 Assert
.IsTrue(convSucceed
);
80 Assert
.AreEqual(UriPartial
.Authority
, Convert(typeof(UriPartial
), UriPartial
.Authority
.ToString("D")));
81 Assert
.IsTrue(convSucceed
);
83 Assert
.AreEqual(UriPartial
.Authority
, Convert(typeof(UriPartial
), "Authority"));
84 Assert
.IsTrue(convSucceed
);
86 Assert
.AreEqual(null, Convert(typeof(UriPartial
), null));
87 Assert
.IsFalse(convSucceed
);
89 Assert
.AreEqual(null, Convert(typeof(UriPartial
), " "));
90 Assert
.IsFalse(convSucceed
);
94 Convert(typeof(UriPartial
), "Invalid Value");
95 Assert
.Fail("EnumConvert should had throwed an exception");
97 catch(BindingException
)
99 Assert
.IsFalse(convSucceed
);
104 public void DecimalConvert()
106 Assert
.AreEqual((decimal) 12.22, Convert(typeof(decimal), "12.22"));
107 Assert
.IsTrue(convSucceed
);
109 Assert
.AreEqual((decimal) 3000, Convert(typeof(decimal), "3,000.00"));
110 Assert
.IsTrue(convSucceed
);
112 Assert
.AreEqual(null, Convert(typeof(decimal), null));
113 Assert
.IsFalse(convSucceed
);
115 Assert
.AreEqual(null, Convert(typeof(decimal), " "));
116 Assert
.IsTrue(convSucceed
);
120 Convert(typeof(decimal), "Invalid Value");
121 Assert
.Fail("DecimalConvert should had throwed an exception");
123 catch(BindingException
)
125 Assert
.IsFalse(convSucceed
);
130 public void GuidConvert()
132 Assert
.AreEqual(new Guid("6CDEF425-6EEA-42AC-A318-0772B55FF259"),
133 Convert(typeof(Guid
), "6CDEF425-6EEA-42AC-A318-0772B55FF259"));
134 Assert
.IsTrue(convSucceed
);
136 Assert
.AreEqual(null, Convert(typeof(Guid
), null));
137 Assert
.IsFalse(convSucceed
);
139 Assert
.AreEqual(null, Convert(typeof(Guid
), " "));
140 Assert
.IsTrue(convSucceed
);
144 Convert(typeof(Guid
), "Invalid Value");
145 Assert
.Fail("GuidConvert should had throwed an exception");
147 catch(BindingException
)
149 Assert
.IsFalse(convSucceed
);
154 public void DateTimeConvert()
156 Assert
.AreEqual(new DateTime(2005, 1, 31), Convert(typeof(DateTime
), "2005-01-31"));
157 Assert
.IsTrue(convSucceed
);
159 Convert(typeof(DateTime
), null);
160 Assert
.IsFalse(convSucceed
);
162 Convert(typeof(DateTime
), " ");
163 Assert
.IsFalse(convSucceed
);
167 public void InvalidDate1()
171 Convert(typeof(DateTime
), "Invalid Value");
172 Assert
.Fail("DateTimeConvert should had throwed an exception");
174 catch(BindingException
)
176 Assert
.IsFalse(convSucceed
);
181 public void InvalidDate2()
185 Convert(typeof(DateTime
), "2005-02-31");
186 Assert
.Fail("DateTimeConvert should had throwed an exception");
188 catch(BindingException
)
190 Assert
.IsFalse(convSucceed
);
195 public void Int32Convert()
197 Assert
.AreEqual(12, Convert(typeof(int), "12"));
198 Assert
.IsTrue(convSucceed
);
200 Assert
.AreEqual(null, Convert(typeof(int), ""));
201 Assert
.IsTrue(convSucceed
);
203 Assert
.AreEqual(null, Convert(typeof(int), null));
204 Assert
.IsFalse(convSucceed
);
208 public void BooleanConvert()
210 Assert
.AreEqual(false, Convert(typeof(bool), ""));
211 Assert
.IsFalse(convSucceed
);
213 Assert
.AreEqual(false, Convert(typeof(bool), "FalSE"));
214 Assert
.IsTrue(convSucceed
);
216 Assert
.AreEqual(true, Convert(typeof(bool), "1"));
217 Assert
.IsTrue(convSucceed
);
219 Assert
.AreEqual(false, Convert(typeof(bool), "0"));
220 Assert
.IsTrue(convSucceed
);
222 Assert
.AreEqual(true, Convert(typeof(bool), "true"));
223 Assert
.IsTrue(convSucceed
);
225 Assert
.AreEqual(true, Convert(typeof(bool), "on"));
226 Assert
.IsTrue(convSucceed
);
228 Assert
.AreEqual(null, Convert(typeof(bool), null));
229 Assert
.IsFalse(convSucceed
);
233 public void BooleanWithArrayAsSourceConvert()
235 Assert
.AreEqual(true, ConvertFromArray(typeof(bool), new string[] {"1", "0"}
));
236 Assert
.IsTrue(convSucceed
);
238 Assert
.AreEqual(false, ConvertFromArray(typeof(bool), new string[] {"0"}
));
239 Assert
.IsTrue(convSucceed
);
241 Assert
.AreEqual(false, ConvertFromArray(typeof(bool), new string[] {"0", "0"}
));
242 Assert
.IsTrue(convSucceed
);
246 public void PrimitiveConvert()
248 Assert
.AreEqual(12.01, Convert(typeof(float), "12.01"));
249 Assert
.IsTrue(convSucceed
);
251 Assert
.AreEqual(null, Convert(typeof(float), ""));
252 Assert
.IsTrue(convSucceed
);
254 Assert
.AreEqual(null, Convert(typeof(float), null));
255 Assert
.IsFalse(convSucceed
);
259 public void TypeConverterConvert()
261 Assert
.IsTrue(Convert(typeof(CustomType
), "validvalue").GetType() == typeof(CustomType
));
262 Assert
.IsTrue(convSucceed
);
266 Convert(typeof(CustomType
), "invalid value");
267 Assert
.Fail("TypeConverterConvert should had throwed an exception");
269 catch(BindingException
)
275 Convert(typeof(CustomType2
), "validvalue");
276 Assert
.Fail("TypeConverterConvert should had throwed an exception");
278 catch(BindingException
)
284 public void InstanceOfConvert()
286 ArrayList col
= new ArrayList();
287 Assert
.AreEqual(col
, converter
.Convert(typeof(ICollection
), col
, out convSucceed
));
288 Assert
.IsTrue(convSucceed
);
292 public void NullableEnumConversion()
294 UriPartial
? val
= (UriPartial
?) Convert(typeof(UriPartial
?), "Path");
295 Assert
.AreEqual(UriPartial
.Path
, val
);
296 Assert
.IsTrue(val
.HasValue
);
297 Assert
.IsTrue(convSucceed
);
299 val
= (UriPartial
?) Convert(typeof(UriPartial
?), "");
300 Assert
.IsFalse(val
.HasValue
);
301 Assert
.IsTrue(convSucceed
);
303 val
= (UriPartial
?) Convert(typeof(UriPartial
?), " ");
304 Assert
.IsFalse(val
.HasValue
);
305 Assert
.IsTrue(convSucceed
);
307 val
= (UriPartial
?) Convert(typeof(UriPartial
?), null);
308 Assert
.IsFalse(val
.HasValue
);
309 Assert
.IsFalse(convSucceed
);
313 public void NullableInt32Conversion()
315 int? val
= (int?) Convert(typeof(int?), "");
316 Assert
.IsFalse(val
.HasValue
);
317 Assert
.IsTrue(convSucceed
);
319 val
= (int?) Convert(typeof(int?), " ");
320 Assert
.IsFalse(val
.HasValue
);
321 Assert
.IsTrue(convSucceed
);
323 Assert
.AreEqual(null, Convert(typeof(int?), null));
324 Assert
.IsFalse(convSucceed
);
328 public void NullableDecimalConversion()
330 decimal? val
= (decimal?) Convert(typeof(decimal?), "12.22");
331 Assert
.AreEqual((decimal?) 12.22, val
);
332 Assert
.IsTrue(val
.HasValue
);
333 Assert
.IsTrue(convSucceed
);
335 val
= (decimal?) Convert(typeof(decimal?), "");
336 Assert
.IsFalse(val
.HasValue
);
337 Assert
.IsTrue(convSucceed
);
339 val
= (decimal?) Convert(typeof(decimal?), "3,000.00");
340 Assert
.AreEqual((decimal?) 3000, val
);
341 Assert
.IsTrue(val
.HasValue
);
342 Assert
.IsTrue(convSucceed
);
344 val
= (decimal?) Convert(typeof(decimal?), null);
345 Assert
.IsFalse(val
.HasValue
);
346 Assert
.IsFalse(convSucceed
);
348 val
= (decimal?) Convert(typeof(decimal?), " ");
349 Assert
.IsFalse(val
.HasValue
);
350 Assert
.IsTrue(convSucceed
);
354 Convert(typeof(decimal?), "Invalid Value");
355 Assert
.Fail("DecimalConvert should had throwed an exception");
357 catch(BindingException
)
359 Assert
.IsFalse(convSucceed
);
364 public void NullableDateTimeConversion()
366 Assert
.AreEqual(new DateTime
?(new DateTime(2005, 1, 31)), Convert(typeof(DateTime
?), "2005-01-31"));
367 Assert
.IsTrue(convSucceed
);
369 Convert(typeof(DateTime
?), null);
370 Assert
.IsFalse(convSucceed
);
372 Convert(typeof(DateTime
?), " ");
373 Assert
.IsTrue(convSucceed
);
377 public void NullableBooleanConversion()
379 Assert
.AreEqual(new bool?(), Convert(typeof(bool?), ""));
380 Assert
.IsTrue(convSucceed
);
382 Assert
.AreEqual(new bool?(), Convert(typeof(bool?), null));
383 Assert
.IsFalse(convSucceed
);
385 Assert
.AreEqual(new bool?(true), Convert(typeof(bool?), "1"));
386 Assert
.IsTrue(convSucceed
);
388 Assert
.AreEqual(new bool?(false), Convert(typeof(bool?), "0"));
389 Assert
.IsTrue(convSucceed
);
391 Assert
.AreEqual(new bool?(false), Convert(typeof(bool?), "0"));
392 Assert
.IsTrue(convSucceed
);
396 public void NullableBooleanWithArrayAsSourceConvert()
398 Assert
.AreEqual(new bool?(true), ConvertFromArray(typeof(bool?), new string[] {"1", "0"}
));
399 Assert
.IsTrue(convSucceed
);
401 Assert
.AreEqual(new bool?(false), ConvertFromArray(typeof(bool?), new string[] {"0"}
));
402 Assert
.IsTrue(convSucceed
);
404 Assert
.AreEqual(new bool?(false), ConvertFromArray(typeof(bool?), new string[] {"0", "0"}
));
405 Assert
.IsTrue(convSucceed
);
409 public void ListOfIntsConvert()
411 Type desiredType
= typeof(System
.Collections
.Generic
.List
<int>);
415 result
= Convert(desiredType
, "1,2,3") as List
<int>;
417 Assert
.IsNotNull(result
);
418 Assert
.AreEqual(3, result
.Count
);
419 Assert
.AreEqual(1, result
[0]);
420 Assert
.AreEqual(2, result
[1]);
421 Assert
.AreEqual(3, result
[2]);
422 Assert
.IsTrue(convSucceed
);
424 Assert
.AreEqual(null, Convert(desiredType
, null));
425 Assert
.IsFalse(convSucceed
);
427 result
= Convert(desiredType
, "") as List
<int>;
428 Assert
.IsNotNull(result
);
429 Assert
.AreEqual(0, result
.Count
);
430 Assert
.IsTrue(convSucceed
);
434 public void ListOfStringsConvert()
436 Type desiredType
= typeof(System
.Collections
.Generic
.List
<string>);
440 result
= Convert(desiredType
, "1,2,3") as List
<string>;
442 Assert
.IsNotNull(result
);
443 Assert
.AreEqual(3, result
.Count
);
444 Assert
.AreEqual("1", result
[0]);
445 Assert
.AreEqual("2", result
[1]);
446 Assert
.AreEqual("3", result
[2]);
447 Assert
.IsTrue(convSucceed
);
449 Assert
.AreEqual(null, Convert(desiredType
, null));
450 Assert
.IsFalse(convSucceed
);
452 result
= Convert(desiredType
, "") as List
<string>;
453 Assert
.IsNotNull(result
);
454 Assert
.AreEqual(0, result
.Count
);
455 Assert
.IsTrue(convSucceed
);
458 private object Convert(Type desiredType
, string input
)
460 return converter
.Convert(desiredType
, typeof(string), input
, out convSucceed
);
463 private object ConvertFromArray(Type desiredType
, string[] input
)
465 return converter
.Convert(desiredType
, typeof(string[]), input
, out convSucceed
);