Merge pull request #1695 from gregoryyoung/master
[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                 [SetCulture("en-GB")]
150                 public void ConvertToString ()
151                 {
152                         CultureInfo culture = new MyCultureInfo ();
153                         DateTimeFormatInfo info = (DateTimeFormatInfo) culture.GetFormat (typeof (DateTimeFormatInfo));
154                         DateTime date = DateTime.Now;
155
156                         Assert.AreEqual (date.ToString (info.ShortDatePattern + " " + 
157                                 info.ShortTimePattern, culture), converter.ConvertToString (
158                                 null, culture, date));
159
160                         CultureInfo ciUS = new CultureInfo("en-US");
161                         CultureInfo ciGB = new CultureInfo("en-GB");
162                         CultureInfo ciDE = new CultureInfo("de-DE");
163                         //
164                         date = new DateTime(2008, 12, 31, 23, 59, 58, 5);
165                         DoTestToString("12/31/2008 11:59 pm", date, ciUS);
166                         DoTestToString("31/12/2008 23:59", date, ciGB);
167                         DoTestToString("31.12.2008 23:59", date, ciDE);
168                         DoTestToString("12/31/2008 23:59:58", date, CultureInfo.InvariantCulture);
169                         Assert.AreEqual("12/31/2008 23:59:58", converter.ConvertToInvariantString(date), "Invariant");
170                         //
171                         date = new DateTime(2008, 12, 31);
172                         DoTestToString("12/31/2008", date, ciUS);
173                         DoTestToString("31/12/2008", date, ciGB);
174                         DoTestToString("31.12.2008", date, ciDE);
175                         DoTestToString("2008-12-31", date, CultureInfo.InvariantCulture);
176                         Assert.AreEqual("2008-12-31", converter.ConvertToInvariantString(date), "Invariant");
177                 }
178                 private void DoTestToString(String expected, DateTime value, CultureInfo ci)
179                 {
180                         String message = ci.Name;
181                         if (message == null || message.Length == 0)
182                                 message = "?Invariant";
183                         Assert.AreEqual(expected, converter.ConvertTo(null, ci, value, typeof(String)), message);
184                 }
185
186                 [Test]
187                 public void ConvertFromString ()
188                 {
189                         CultureInfo culture = new MyCultureInfo ();
190                         DateTimeFormatInfo info = (DateTimeFormatInfo) culture.GetFormat (typeof (DateTimeFormatInfo));
191                         DateTime date = DateTime.Now;
192
193                         try {
194                                 converter.ConvertFrom (null, culture, date.ToString("G", info));
195                         } catch (FormatException) {
196                         }
197                 }
198
199                 [Test]
200                 [ExpectedException (typeof (FormatException))]
201                 public void ConvertFrom_InvalidValue ()
202                 {
203                         converter.ConvertFrom ("*1");
204                 }
205
206                 [Test]
207                 [ExpectedException (typeof (FormatException))]
208                 public void ConvertFrom_InvalidValue_Invariant ()
209                 {
210                         converter.ConvertFrom (null, CultureInfo.InvariantCulture, "*1");
211                 }
212
213                 [Test]
214                 [ExpectedException (typeof (FormatException))]
215                 public void ConvertFromString_InvalidValue ()
216                 {
217                         converter.ConvertFromString ("*1");
218                 }
219
220                 [Test]
221                 [ExpectedException (typeof (FormatException))]
222                 public void ConvertFromString_InvalidValue_Invariant ()
223                 {
224                         converter.ConvertFromString (null, CultureInfo.InvariantCulture, "*1");
225                 }
226
227                 [Serializable]
228                 private sealed class MyCultureInfo : CultureInfo
229                 {
230                         internal MyCultureInfo () : base ("en-US")
231                         {
232                         }
233
234                         public override object GetFormat (Type formatType)
235                         {
236                                 if (formatType == typeof (DateTimeFormatInfo)) {
237                                         DateTimeFormatInfo info = (DateTimeFormatInfo) ((DateTimeFormatInfo) base.GetFormat (formatType)).Clone ();
238                                         info.ShortDatePattern = "MM?dd?yyyy";
239                                         info.ShortTimePattern = "hh!mm";
240                                         return DateTimeFormatInfo.ReadOnly (info);
241                                 } else {
242                                         return base.GetFormat (formatType);
243                                 }
244                         }
245                 }
246         }
247 }