Merge branch 'master' of github.com:mono/mono
[mono.git] / mcs / class / System / Test / System.ComponentModel / CharConverterTest.cs
1 //
2 // System.ComponentModel.CharConverter test cases
3 //
4 // Authors:
5 //      Gert Driesen (drieseng@users.sourceforge.net)
6 //
7 // (c) 2008 Gert Driesen
8 //
9
10 using System;
11 using System.ComponentModel;
12 using System.ComponentModel.Design.Serialization;
13 using System.Globalization;
14
15 using NUnit.Framework;
16
17 namespace MonoTests.System.ComponentModel
18 {
19         [TestFixture]
20         public class CharConverterTest
21         {
22                 private CharConverter converter;
23                 private string pattern;
24                 
25                 [SetUp]
26                 public void SetUp ()
27                 {
28                         converter = new CharConverter ();
29
30                         DateTimeFormatInfo info = CultureInfo.CurrentCulture.DateTimeFormat;
31                         pattern = info.ShortDatePattern + " " + info.ShortTimePattern;
32                 }
33
34                 [Test]
35                 public void CanConvertFrom ()
36                 {
37                         Assert.IsTrue (converter.CanConvertFrom (typeof (string)), "#1");
38                         Assert.IsFalse (converter.CanConvertFrom (typeof (char)), "#2");
39                         Assert.IsFalse (converter.CanConvertFrom (typeof (object)), "#3");
40                         Assert.IsFalse (converter.CanConvertFrom (typeof (int)), "#4");
41                         Assert.IsFalse (converter.CanConvertFrom (typeof (char [])), "#5");
42                         Assert.IsTrue (converter.CanConvertFrom (typeof (InstanceDescriptor)), "#6");
43                 }
44
45                 [Test]
46                 public void CanConvertTo ()
47                 {
48                         Assert.IsTrue (converter.CanConvertTo (typeof (string)), "#1");
49                         Assert.IsFalse (converter.CanConvertTo (typeof (char)), "#2");
50                         Assert.IsFalse (converter.CanConvertTo (typeof (object)), "#3");
51                         Assert.IsFalse (converter.CanConvertTo (typeof (int)), "#4");
52                         Assert.IsFalse (converter.CanConvertTo (typeof (char [])), "#5");
53                         Assert.IsFalse (converter.CanConvertTo (typeof (InstanceDescriptor)), "#6");
54                 }
55
56                 [Test]
57                 public void ConvertFrom_String ()
58                 {
59                         char c;
60
61                         c = (char) converter.ConvertFrom (null, CultureInfo.InvariantCulture,
62                                 String.Empty);
63                         Assert.AreEqual ('\0', c, "#1");
64
65                         c = (char) converter.ConvertFrom (null, CultureInfo.InvariantCulture,
66                                 "e");
67                         Assert.AreEqual ('e', c, "#2");
68
69                         c = (char) converter.ConvertFrom (null, CultureInfo.InvariantCulture,
70                                 "\t f\r\n ");
71                         Assert.AreEqual ('f', c, "#3");
72                 }
73
74                 [Test]
75                 public void ConvertFrom_String_Invalid ()
76                 {
77                         try {
78                                 converter.ConvertFrom (null, CultureInfo.InvariantCulture,
79                                         "ef");
80                                 Assert.Fail ("#A1");
81                         } catch (FormatException ex) {
82                                 // ef is not a valid value for Char
83                                 Assert.AreEqual (typeof (FormatException), ex.GetType (), "#A2");
84                                 Assert.IsNull (ex.InnerException, "#A3");
85                                 Assert.IsNotNull (ex.Message, "#A4");
86                                 Assert.IsTrue (ex.Message.IndexOf (typeof (char).Name) != -1, "#A5");
87                                 Assert.IsTrue (ex.Message.IndexOf ("ef") != -1, "#A6");
88                         }
89
90                         try {
91                                 converter.ConvertFrom (null, CultureInfo.InvariantCulture,
92                                         "\ref \n");
93                                 Assert.Fail ("#B1");
94                         } catch (FormatException ex) {
95                                 // \ref\n is not a valid value for Char
96                                 Assert.AreEqual (typeof (FormatException), ex.GetType (), "#B2");
97                                 Assert.IsNull (ex.InnerException, "#B3");
98                                 Assert.IsNotNull (ex.Message, "#B4");
99                                 Assert.IsTrue (ex.Message.IndexOf (typeof (char).Name) != -1, "#B5");
100                                 Assert.IsTrue (ex.Message.IndexOf ("ef") != -1, "#B6");
101                         }
102                 }
103
104                 [Test]
105                 public void ConvertFrom_Value_Null ()
106                 {
107                         try {
108                                 converter.ConvertFrom (null, CultureInfo.InvariantCulture,
109                                         (string) null);
110                                 Assert.Fail ("#1");
111                         } catch (NotSupportedException ex) {
112                                 // CharConverter cannot convert from (null)
113                                 Assert.AreEqual (typeof (NotSupportedException), ex.GetType (), "#2");
114                                 Assert.IsNull (ex.InnerException, "#3");
115                                 Assert.IsNotNull (ex.Message, "#4");
116                                 Assert.IsTrue (ex.Message.IndexOf (typeof (CharConverter).Name) != -1, "#5");
117                                 Assert.IsTrue (ex.Message.IndexOf ("(null)") != -1, "#6");
118                         }
119                 }
120
121                 [Test]
122                 public void ConvertToString ()
123                 {
124                         string result;
125
126                         result = converter.ConvertToString (null, CultureInfo.InvariantCulture,
127                                 ' ');
128                         Assert.AreEqual (" ", result, "#1");
129
130                         result = converter.ConvertToString (null, CultureInfo.InvariantCulture,
131                                 '\0');
132                         Assert.AreEqual (string.Empty, result, "#2");
133
134                         result = converter.ConvertToString (null, CultureInfo.InvariantCulture,
135                                 'f');
136                         Assert.AreEqual ("f", result, "#3");
137
138                         result = converter.ConvertToString (null, CultureInfo.InvariantCulture,
139                                 null);
140                         Assert.AreEqual (string.Empty, result, "#4");
141
142                         result = converter.ConvertToString (null, CultureInfo.InvariantCulture,
143                                 new char [] { 'a', 'f' });
144                         Assert.AreEqual ("System.Char[]", result, "#5");
145                 }
146         }
147 }