Add a test case for DateTimeOffsetConverter converting to InstanceDescriptor.
[mono.git] / mcs / class / System / Test / System.ComponentModel / DateTimeConverterTests.cs
1 //
2 // System.ComponentModel.DateTimeConverter test cases
3 //
4 // Authors:
5 //      Gert Driesen (drieseng@users.sourceforge.net)
6 //
7 // (c) 2005 Novell, Inc. (http://www.ximian.com)
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 DateTimeConverterTests
21         {
22                 private DateTimeConverter converter;
23                 private string pattern;
24                 
25                 [SetUp]
26                 public void SetUp ()
27                 {
28                         converter = new DateTimeConverter ();
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 (DateTime)), "#2");
39                         Assert.IsFalse (converter.CanConvertFrom (typeof (object)), "#3");
40                         Assert.IsTrue (converter.CanConvertFrom (typeof (InstanceDescriptor)), "#4");
41                 }
42
43                 [Test]
44                 public void CanConvertTo ()
45                 {
46                         Assert.IsTrue (converter.CanConvertTo (typeof (string)), "#1");
47                         Assert.IsFalse (converter.CanConvertTo (typeof (object)), "#2");
48                         Assert.IsTrue (converter.CanConvertTo (typeof (InstanceDescriptor)), "#3");
49                 }
50
51                 [Test]
52                 public void ConvertFrom_String ()
53                 {
54                         DateTime date = DateTime.Now;
55                         DateTime newDate = (DateTime) converter.ConvertFrom (null, CultureInfo.InvariantCulture, 
56                                 date.ToString(CultureInfo.InvariantCulture));
57
58                         Assert.AreEqual (date.Year, newDate.Year, "#1");
59                         Assert.AreEqual (date.Month, newDate.Month, "#2");
60                         Assert.AreEqual (date.Day, newDate.Day, "#3");
61                         Assert.AreEqual (date.Hour, newDate.Hour, "#4");
62                         Assert.AreEqual (date.Minute, newDate.Minute, "#5");
63                         Assert.AreEqual (date.Second, newDate.Second, "#6");
64                         Assert.AreEqual (0, newDate.Millisecond, "#7");
65
66                         newDate = (DateTime) converter.ConvertFrom (null, CultureInfo.InvariantCulture, 
67                                                                     String.Empty);
68                         Assert.AreEqual (DateTime.MinValue, newDate, "#8");
69                 }
70
71                 [Test]
72                 [ExpectedException (typeof (NotSupportedException))]
73                 public void ConvertFrom_Object ()
74                 {
75                         converter.ConvertFrom (new object ());
76                 }
77
78                 [Test]
79                 [ExpectedException (typeof (NotSupportedException))]
80                 public void ConvertFrom_Int32 ()
81                 {
82                         converter.ConvertFrom (10);
83                 }
84
85                 [Test]
86                 public void ConvertTo_MinValue ()
87                 {
88                         Assert.AreEqual (string.Empty, converter.ConvertTo (null, 
89                                 CultureInfo.InvariantCulture, DateTime.MinValue, typeof (string)), "#1");
90                         Assert.AreEqual (string.Empty, converter.ConvertTo (null, 
91                                 CultureInfo.CurrentCulture, DateTime.MinValue, typeof (string)), "#2");
92                         Assert.AreEqual (string.Empty, converter.ConvertTo (DateTime.MinValue, 
93                                 typeof (string)), "#3");
94                 }
95
96                 [Test]
97                 public void ConvertTo_MaxValue ()
98                 {
99                         Assert.AreEqual (DateTime.MaxValue.ToString (CultureInfo.InvariantCulture), 
100                                 converter.ConvertTo (null, CultureInfo.InvariantCulture, DateTime.MaxValue, 
101                                 typeof (string)), "#1");
102
103                         // FIXME: We probably shouldn't be using CurrentCulture in these tests.
104                         if (CultureInfo.CurrentCulture == CultureInfo.InvariantCulture)
105                                 return;
106                         Assert.AreEqual (DateTime.MaxValue.ToString (pattern, 
107                                 CultureInfo.CurrentCulture), converter.ConvertTo (null, 
108                                 CultureInfo.CurrentCulture, DateTime.MaxValue, typeof (string)),
109                                 "#2");
110                         Assert.AreEqual (DateTime.MaxValue.ToString (pattern, 
111                                 CultureInfo.CurrentCulture), converter.ConvertTo (DateTime.MaxValue, 
112                                 typeof (string)), "#3");
113                 }
114
115                 [Test]
116                 public void ConvertToString_MinValue ()
117                 {
118                         Assert.AreEqual (string.Empty, converter.ConvertToString (null, 
119                                 CultureInfo.InvariantCulture, DateTime.MinValue), "#1");
120
121                         Assert.AreEqual (string.Empty, converter.ConvertToString (null, 
122                                 DateTime.MinValue), "#2");
123                         Assert.AreEqual (string.Empty, converter.ConvertToString (null, 
124                                 CultureInfo.CurrentCulture, DateTime.MinValue), "#3");
125                         Assert.AreEqual (string.Empty, converter.ConvertToString (DateTime.MinValue),
126                                 "#4");
127                 }
128
129                 [Test]
130                 public void ConvertToString_MaxValue ()
131                 {
132                         Assert.AreEqual (DateTime.MaxValue.ToString (CultureInfo.InvariantCulture), 
133                                 converter.ConvertToString (null, CultureInfo.InvariantCulture, 
134                                 DateTime.MaxValue), "#1");
135
136                         // FIXME: We probably shouldn't be using CurrentCulture in these tests.
137                         if (CultureInfo.CurrentCulture == CultureInfo.InvariantCulture)
138                                 return;
139                         Assert.AreEqual (DateTime.MaxValue.ToString (pattern, CultureInfo.CurrentCulture),
140                                 converter.ConvertToString (null, DateTime.MaxValue), "#2");
141                         Assert.AreEqual (DateTime.MaxValue.ToString (pattern, CultureInfo.CurrentCulture),
142                                 converter.ConvertToString (null, CultureInfo.CurrentCulture,
143                                 DateTime.MaxValue), "#3");
144                         Assert.AreEqual (DateTime.MaxValue.ToString (pattern, CultureInfo.CurrentCulture),
145                                 converter.ConvertToString (DateTime.MaxValue), "#4");
146                 }
147
148                 [Test]
149                 public void ConvertToString ()
150                 {
151                         CultureInfo culture = new MyCultureInfo ();
152                         DateTimeFormatInfo info = (DateTimeFormatInfo) culture.GetFormat (typeof (DateTimeFormatInfo));
153                         DateTime date = DateTime.Now;
154
155                         Assert.AreEqual (date.ToString (info.ShortDatePattern + " " + 
156                                 info.ShortTimePattern, culture), converter.ConvertToString (
157                                 null, culture, date));
158
159                         CultureInfo ciUS = new CultureInfo("en-US");
160                         CultureInfo ciGB = new CultureInfo("en-GB");
161                         CultureInfo ciDE = new CultureInfo("de-DE");
162                         //
163                         date = new DateTime(2008, 12, 31, 23, 59, 58, 5);
164                         DoTestToString("12/31/2008 11:59 PM", date, ciUS);
165                         DoTestToString("31/12/2008 23:59", date, ciGB);
166                         DoTestToString("31.12.2008 23:59", date, ciDE);
167                         DoTestToString("12/31/2008 23:59:58", date, CultureInfo.InvariantCulture);
168                         Assert.AreEqual("12/31/2008 23:59:58", converter.ConvertToInvariantString(date), "Invariant");
169                         //
170                         date = new DateTime(2008, 12, 31);
171                         DoTestToString("12/31/2008", date, ciUS);
172                         DoTestToString("31/12/2008", date, ciGB);
173                         DoTestToString("31.12.2008", date, ciDE);
174                         DoTestToString("2008-12-31", date, CultureInfo.InvariantCulture);
175                         Assert.AreEqual("2008-12-31", converter.ConvertToInvariantString(date), "Invariant");
176                 }
177                 private void DoTestToString(String expected, DateTime value, CultureInfo ci)
178                 {
179                         String message = ci.Name;
180                         if (message == null || message.Length == 0)
181                                 message = "?Invariant";
182                         Assert.AreEqual(expected, converter.ConvertTo(null, ci, value, typeof(String)), message);
183                 }
184
185                 [Test]
186                 public void ConvertFromString ()
187                 {
188                         CultureInfo culture = new MyCultureInfo ();
189                         DateTimeFormatInfo info = (DateTimeFormatInfo) culture.GetFormat (typeof (DateTimeFormatInfo));
190                         DateTime date = DateTime.Now;
191
192                         DateTime newDate = (DateTime) converter.ConvertFrom (null, culture, date.ToString("G", info));
193
194                         Assert.AreEqual (date.Year, newDate.Year, "#1");
195                         Assert.AreEqual (date.Month, newDate.Month, "#2");
196                         Assert.AreEqual (date.Day, newDate.Day, "#3");
197                         Assert.AreEqual (date.Hour, newDate.Hour, "#4");
198                         Assert.AreEqual (date.Minute, newDate.Minute, "#5");
199                         Assert.AreEqual (date.Second, newDate.Second, "#6");
200                         Assert.AreEqual (0, newDate.Millisecond, "#7");
201                 }
202
203                 [Test]
204                 [ExpectedException (typeof (FormatException))]
205                 public void ConvertFrom_InvalidValue ()
206                 {
207                         converter.ConvertFrom ("*1");
208                 }
209
210                 [Test]
211                 [ExpectedException (typeof (FormatException))]
212                 public void ConvertFrom_InvalidValue_Invariant ()
213                 {
214                         converter.ConvertFrom (null, CultureInfo.InvariantCulture, "*1");
215                 }
216
217                 [Test]
218                 [ExpectedException (typeof (FormatException))]
219                 public void ConvertFromString_InvalidValue ()
220                 {
221                         converter.ConvertFromString ("*1");
222                 }
223
224                 [Test]
225                 [ExpectedException (typeof (FormatException))]
226                 public void ConvertFromString_InvalidValue_Invariant ()
227                 {
228                         converter.ConvertFromString (null, CultureInfo.InvariantCulture, "*1");
229                 }
230
231                 [Serializable]
232                 private sealed class MyCultureInfo : CultureInfo
233                 {
234                         internal MyCultureInfo () : base ("en-US")
235                         {
236                         }
237
238                         public override object GetFormat (Type formatType)
239                         {
240                                 if (formatType == typeof (DateTimeFormatInfo)) {
241                                         DateTimeFormatInfo info = (DateTimeFormatInfo) ((DateTimeFormatInfo) base.GetFormat (formatType)).Clone ();
242                                         info.ShortDatePattern = "MM?dd?yyyy";
243                                         info.ShortTimePattern = "hh!mm";
244                                         return DateTimeFormatInfo.ReadOnly (info);
245                                 } else {
246                                         return base.GetFormat (formatType);
247                                 }
248                         }
249                 }
250         }
251 }