2 // DateTimeOffsetConverterTests.cs
5 // Carlos Alberto Cortez (calberto.cortez@gmail.com)
7 // Copyright (C) 2010 Novell, Inc (http://www.novell.com)
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:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
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.
29 #if NET_4_0 && !MOBILE
32 using System.ComponentModel;
33 using System.ComponentModel.Design.Serialization;
34 using System.Globalization;
35 using NUnit.Framework;
37 namespace MonoTests.System.ComponentModel
40 public class DateTimeOffsetConverterTests
42 DateTimeOffsetConverter converter;
47 converter = new DateTimeOffsetConverter ();
51 public void CanConvertFrom ()
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");
61 public void CanConvertTo ()
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");
71 public void ConvertFrom_String ()
73 DateTimeOffset dateOffset = DateTimeOffset.Now;
74 DateTimeOffset newDateOffset = (DateTimeOffset) converter.ConvertFrom (null, CultureInfo.InvariantCulture,
75 dateOffset.ToString (CultureInfo.InvariantCulture));
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");
83 newDateOffset = (DateTimeOffset) converter.ConvertFrom (null, CultureInfo.InvariantCulture, String.Empty);
84 Assert.AreEqual (DateTimeOffset.MinValue, newDateOffset, "#B1");
88 [ExpectedException (typeof (NotSupportedException))]
89 public void ConvertFrom_Object ()
91 converter.ConvertFrom (new object ());
95 [ExpectedException (typeof (NotSupportedException))]
96 public void ConvertFrom_Int32 ()
98 converter.ConvertFrom (10);
102 public void ConvertTo_MinValue ()
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");
113 public void ConvertTo_MaxValue ()
115 Assert.AreEqual (DateTimeOffset.MaxValue.ToString (CultureInfo.InvariantCulture),
116 converter.ConvertTo (null, CultureInfo.InvariantCulture, DateTimeOffset.MaxValue,
117 typeof (string)), "#A1");
121 [ExpectedException (typeof (NotSupportedException))]
122 public void ConvertTo_object ()
124 converter.ConvertTo (DateTimeOffset.Now, typeof (object));
128 [ExpectedException (typeof (NotSupportedException))]
129 public void ConvertTo_int ()
131 converter.ConvertTo (DateTimeOffset.Now, typeof (int));
135 [ExpectedException (typeof (NotSupportedException))]
136 public void ConvertTo_DateTime ()
138 converter.ConvertTo (DateTimeOffset.Now, typeof (DateTime));
142 [ExpectedException (typeof (NotSupportedException))]
143 public void ConvertTo_DateTimeOffset ()
145 converter.ConvertTo (DateTimeOffset.Now, typeof (DateTimeOffset));
149 public void ConvertToString_MinValue ()
151 Assert.AreEqual (String.Empty, converter.ConvertToString (null,
152 CultureInfo.InvariantCulture, DateTimeOffset.MinValue), "#A1");
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");
161 public void ConvertToString_MaxValue ()
163 Assert.AreEqual (DateTimeOffset.MaxValue.ToString (CultureInfo.InvariantCulture),
164 converter.ConvertToString (null, CultureInfo.InvariantCulture, DateTimeOffset.MaxValue), "#A1");
168 [SetCulture("en-US")]
169 public void ConvertToString ()
171 CultureInfo ciUS = new CultureInfo("en-US");
172 CultureInfo ciGB = new CultureInfo("en-GB");
173 CultureInfo ciDE = new CultureInfo("de-DE");
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");
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");
190 void DoTestToString (string expected, DateTimeOffset value, CultureInfo ci)
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);
199 [ExpectedException (typeof (FormatException))]
200 public void ConvertFrom_InvalidValue ()
202 converter.ConvertFrom ("*1");
206 [ExpectedException (typeof (FormatException))]
207 public void ConvertFrom_InvalidValue_Invariant ()
209 converter.ConvertFrom (null, CultureInfo.InvariantCulture, "*1");
213 [ExpectedException (typeof (FormatException))]
214 public void ConvertFromString_InvalidValue ()
216 converter.ConvertFromString ("*1");
220 [ExpectedException (typeof (FormatException))]
221 public void ConvertFromString_InvalidValue_Invariant ()
223 converter.ConvertFromString (null, CultureInfo.InvariantCulture, "*1");
227 public void ConvertTo_InstanceDescriptor ()
229 DateTimeOffset dto = new DateTimeOffset (new DateTime (2010, 10, 11), new TimeSpan (3, 6, 0));
230 InstanceDescriptor descriptor = (InstanceDescriptor) converter.ConvertTo (dto, typeof (InstanceDescriptor));
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");