[bcl] Remove NET_4_0 defines from class libs
[mono.git] / mcs / class / System / Test / System.ComponentModel / DateTimeOffsetConverterTests.cs
1 //
2 // DateTimeOffsetConverterTests.cs
3 //
4 // Author:
5 //      Carlos Alberto Cortez (calberto.cortez@gmail.com)
6 //
7 // Copyright (C) 2010 Novell, Inc (http://www.novell.com)
8 //
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
16 // 
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
19 // 
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 //
28
29 #if !MOBILE
30
31 using System;
32 using System.ComponentModel;
33 using System.ComponentModel.Design.Serialization;
34 using System.Globalization;
35 using NUnit.Framework;
36
37 namespace MonoTests.System.ComponentModel
38 {
39         [TestFixture]
40         public class DateTimeOffsetConverterTests
41         {
42                 DateTimeOffsetConverter converter;
43         
44                 [SetUp]
45                 public void SetUp ()
46                 {
47                         converter = new DateTimeOffsetConverter ();
48                 }
49
50                 [Test]
51                 public void CanConvertFrom ()
52                 {
53                         Assert.IsTrue (converter.CanConvertFrom (typeof (string)), "#A1");
54                         Assert.IsFalse (converter.CanConvertFrom (typeof (DateTime)), "#A2");
55                         Assert.IsFalse (converter.CanConvertFrom (typeof (DateTimeOffset)), "#A3");
56                         Assert.IsFalse (converter.CanConvertFrom (typeof (object)), "#A4");
57                         Assert.IsTrue (converter.CanConvertFrom (typeof (InstanceDescriptor)), "#A5");
58                 }
59
60                 [Test]
61                 public void CanConvertTo ()
62                 {
63                         Assert.IsTrue (converter.CanConvertTo (typeof (string)), "#A1");
64                         Assert.IsFalse (converter.CanConvertTo (typeof (object)), "#A2");
65                         Assert.IsFalse (converter.CanConvertTo (typeof (DateTime)), "#A3");
66                         Assert.IsFalse (converter.CanConvertTo (typeof (DateTimeOffset)), "#A4");
67                         Assert.IsTrue (converter.CanConvertTo (typeof (InstanceDescriptor)), "#A5");
68                 }
69
70                 [Test]
71                 public void ConvertFrom_String ()
72                 {
73                         DateTimeOffset dateOffset = DateTimeOffset.Now;
74                         DateTimeOffset newDateOffset = (DateTimeOffset) converter.ConvertFrom (null, CultureInfo.InvariantCulture, 
75                                         dateOffset.ToString (CultureInfo.InvariantCulture));
76
77                         Assert.AreEqual (dateOffset.Date, newDateOffset.Date, "#A1");
78                         Assert.AreEqual (dateOffset.Hour, newDateOffset.Hour, "#A2");
79                         Assert.AreEqual (dateOffset.Minute, newDateOffset.Minute, "#A3");
80                         Assert.AreEqual (dateOffset.Second, newDateOffset.Second, "#A4");
81                         Assert.AreEqual (dateOffset.Offset, newDateOffset.Offset, "#A5");
82
83                         newDateOffset = (DateTimeOffset) converter.ConvertFrom (null, CultureInfo.InvariantCulture, String.Empty);
84                         Assert.AreEqual (DateTimeOffset.MinValue, newDateOffset, "#B1");
85                 }
86
87                 [Test]
88                 [ExpectedException (typeof (NotSupportedException))]
89                 public void ConvertFrom_Object ()
90                 {
91                         converter.ConvertFrom (new object ());
92                 }
93
94                 [Test]
95                 [ExpectedException (typeof (NotSupportedException))]
96                 public void ConvertFrom_Int32 ()
97                 {
98                         converter.ConvertFrom (10);
99                 }
100
101                 [Test]
102                 public void ConvertTo_MinValue ()
103                 {
104                         Assert.AreEqual (String.Empty, converter.ConvertTo (null, 
105                                 CultureInfo.InvariantCulture, DateTimeOffset.MinValue, typeof (string)), "#A1");
106                         Assert.AreEqual (String.Empty, converter.ConvertTo (null, 
107                                 CultureInfo.CurrentCulture, DateTimeOffset.MinValue, typeof (string)), "#A2");
108                         Assert.AreEqual (String.Empty, converter.ConvertTo (DateTimeOffset.MinValue, 
109                                 typeof (string)), "#A3");
110                 }
111
112                 [Test]
113                 public void ConvertTo_MaxValue ()
114                 {
115                         Assert.AreEqual (DateTimeOffset.MaxValue.ToString (CultureInfo.InvariantCulture), 
116                                 converter.ConvertTo (null, CultureInfo.InvariantCulture, DateTimeOffset.MaxValue, 
117                                 typeof (string)), "#A1");
118                 }
119
120                 [Test]
121                 [ExpectedException (typeof (NotSupportedException))]
122                 public void ConvertTo_object ()
123                 {
124                         converter.ConvertTo (DateTimeOffset.Now, typeof (object));
125                 }
126
127                 [Test]
128                 [ExpectedException (typeof (NotSupportedException))]
129                 public void ConvertTo_int ()
130                 {
131                         converter.ConvertTo (DateTimeOffset.Now, typeof (int));
132                 }
133
134                 [Test]
135                 [ExpectedException (typeof (NotSupportedException))]
136                 public void ConvertTo_DateTime ()
137                 {
138                         converter.ConvertTo (DateTimeOffset.Now, typeof (DateTime));
139                 }
140
141                 [Test]
142                 [ExpectedException (typeof (NotSupportedException))]
143                 public void ConvertTo_DateTimeOffset ()
144                 {
145                         converter.ConvertTo (DateTimeOffset.Now, typeof (DateTimeOffset));
146                 }
147
148                 [Test]
149                 public void ConvertToString_MinValue ()
150                 {
151                         Assert.AreEqual (String.Empty, converter.ConvertToString (null, 
152                                 CultureInfo.InvariantCulture, DateTimeOffset.MinValue), "#A1");
153
154                         Assert.AreEqual (String.Empty, converter.ConvertToString (null, DateTimeOffset.MinValue), "#A2");
155                         Assert.AreEqual (String.Empty, converter.ConvertToString (null, 
156                                 CultureInfo.CurrentCulture, DateTimeOffset.MinValue), "#A3");
157                         Assert.AreEqual (String.Empty, converter.ConvertToString (DateTimeOffset.MinValue), "#A4");
158                 }
159
160                 [Test]
161                 public void ConvertToString_MaxValue ()
162                 {
163                         Assert.AreEqual (DateTimeOffset.MaxValue.ToString (CultureInfo.InvariantCulture), 
164                                 converter.ConvertToString (null, CultureInfo.InvariantCulture, DateTimeOffset.MaxValue), "#A1");
165                 }
166
167                 [Test]
168                 [SetCulture("en-US")]
169                 public void ConvertToString ()
170                 {
171                         CultureInfo ciUS = new CultureInfo("en-US");
172                         CultureInfo ciGB = new CultureInfo("en-GB");
173                         CultureInfo ciDE = new CultureInfo("de-DE");
174
175                         DateTimeOffset dateOffset = new DateTimeOffset (2008, 12, 31, 23, 59, 58, 5, new TimeSpan (3, 6, 0));
176                         DoTestToString ("12/31/2008 11:59 PM +03:06", dateOffset, ciUS);
177                         DoTestToString ("31/12/2008 23:59 +03:06", dateOffset, ciGB);
178                         DoTestToString ("31.12.2008 23:59 +03:06", dateOffset, ciDE);
179                         DoTestToString ("12/31/2008 23:59:58 +03:06", dateOffset, CultureInfo.InvariantCulture);
180                         Assert.AreEqual ("12/31/2008 23:59:58 +03:06", converter.ConvertToInvariantString (dateOffset), "Invariant");
181
182                         dateOffset = new DateTimeOffset (new DateTime (2008, 12, 31), new TimeSpan (0, 0, 0));
183                         DoTestToString ("12/31/2008 +00:00", dateOffset, ciUS);
184                         DoTestToString ("31/12/2008 +00:00", dateOffset, ciGB);
185                         DoTestToString ("31.12.2008 +00:00", dateOffset, ciDE);
186                         DoTestToString ("2008-12-31 +00:00", dateOffset, CultureInfo.InvariantCulture);
187                         Assert.AreEqual ("2008-12-31 +00:00", converter.ConvertToInvariantString (dateOffset), "Invariant");
188                 }
189
190                 void DoTestToString (string expected, DateTimeOffset value, CultureInfo ci)
191                 {
192                         String message = ci.Name;
193                         if (message == null || message.Length == 0)
194                                 message = "?Invariant";
195                         Assert.AreEqual (expected, converter.ConvertTo (null, ci, value, typeof (string)), message);
196                 }
197
198                 [Test]
199                 [ExpectedException (typeof (FormatException))]
200                 public void ConvertFrom_InvalidValue ()
201                 {
202                         converter.ConvertFrom ("*1");
203                 }
204
205                 [Test]
206                 [ExpectedException (typeof (FormatException))]
207                 public void ConvertFrom_InvalidValue_Invariant ()
208                 {
209                         converter.ConvertFrom (null, CultureInfo.InvariantCulture, "*1");
210                 }
211
212                 [Test]
213                 [ExpectedException (typeof (FormatException))]
214                 public void ConvertFromString_InvalidValue ()
215                 {
216                         converter.ConvertFromString ("*1");
217                 }
218
219                 [Test]
220                 [ExpectedException (typeof (FormatException))]
221                 public void ConvertFromString_InvalidValue_Invariant ()
222                 {
223                         converter.ConvertFromString (null, CultureInfo.InvariantCulture, "*1");
224                 }
225
226                 [Test]
227                 public void ConvertTo_InstanceDescriptor ()
228                 {
229                         DateTimeOffset dto = new DateTimeOffset (new DateTime (2010, 10, 11), new TimeSpan (3, 6, 0));
230                         InstanceDescriptor descriptor = (InstanceDescriptor) converter.ConvertTo (dto, typeof (InstanceDescriptor));
231
232                         Assert.AreEqual (".ctor", descriptor.MemberInfo.Name, "#A0");
233                         Assert.AreEqual (8, descriptor.Arguments.Count, "#A1");
234                         DateTimeOffset dto2 = (DateTimeOffset) descriptor.Invoke ();
235                         Assert.AreEqual (dto, dto2, "#A2");
236                 }
237         }
238 }
239
240 #endif
241