/mcs/class/System/Test/System.ComponentModel/DateTimeOffsetConverterTests.cs

https://github.com/QuickJack/mono · C# · 241 lines · 182 code · 32 blank · 27 comment · 5 complexity · 29c0762f27c635c59f5d68d55e82d0b9 MD5 · raw file

  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. #if NET_4_0 && !MOBILE
  29. using System;
  30. using System.ComponentModel;
  31. using System.ComponentModel.Design.Serialization;
  32. using System.Globalization;
  33. using NUnit.Framework;
  34. namespace MonoTests.System.ComponentModel
  35. {
  36. [TestFixture]
  37. public class DateTimeOffsetConverterTests
  38. {
  39. DateTimeOffsetConverter converter;
  40. [SetUp]
  41. public void SetUp ()
  42. {
  43. converter = new DateTimeOffsetConverter ();
  44. }
  45. [Test]
  46. public void CanConvertFrom ()
  47. {
  48. Assert.IsTrue (converter.CanConvertFrom (typeof (string)), "#A1");
  49. Assert.IsFalse (converter.CanConvertFrom (typeof (DateTime)), "#A2");
  50. Assert.IsFalse (converter.CanConvertFrom (typeof (DateTimeOffset)), "#A3");
  51. Assert.IsFalse (converter.CanConvertFrom (typeof (object)), "#A4");
  52. Assert.IsTrue (converter.CanConvertFrom (typeof (InstanceDescriptor)), "#A5");
  53. }
  54. [Test]
  55. public void CanConvertTo ()
  56. {
  57. Assert.IsTrue (converter.CanConvertTo (typeof (string)), "#A1");
  58. Assert.IsFalse (converter.CanConvertTo (typeof (object)), "#A2");
  59. Assert.IsFalse (converter.CanConvertTo (typeof (DateTime)), "#A3");
  60. Assert.IsFalse (converter.CanConvertTo (typeof (DateTimeOffset)), "#A4");
  61. Assert.IsTrue (converter.CanConvertTo (typeof (InstanceDescriptor)), "#A5");
  62. }
  63. [Test]
  64. public void ConvertFrom_String ()
  65. {
  66. DateTimeOffset dateOffset = DateTimeOffset.Now;
  67. DateTimeOffset newDateOffset = (DateTimeOffset) converter.ConvertFrom (null, CultureInfo.InvariantCulture,
  68. dateOffset.ToString (CultureInfo.InvariantCulture));
  69. Assert.AreEqual (dateOffset.Date, newDateOffset.Date, "#A1");
  70. Assert.AreEqual (dateOffset.Hour, newDateOffset.Hour, "#A2");
  71. Assert.AreEqual (dateOffset.Minute, newDateOffset.Minute, "#A3");
  72. Assert.AreEqual (dateOffset.Second, newDateOffset.Second, "#A4");
  73. Assert.AreEqual (dateOffset.Offset, newDateOffset.Offset, "#A5");
  74. newDateOffset = (DateTimeOffset) converter.ConvertFrom (null, CultureInfo.InvariantCulture, String.Empty);
  75. Assert.AreEqual (DateTimeOffset.MinValue, newDateOffset, "#B1");
  76. }
  77. [Test]
  78. [ExpectedException (typeof (NotSupportedException))]
  79. public void ConvertFrom_Object ()
  80. {
  81. converter.ConvertFrom (new object ());
  82. }
  83. [Test]
  84. [ExpectedException (typeof (NotSupportedException))]
  85. public void ConvertFrom_Int32 ()
  86. {
  87. converter.ConvertFrom (10);
  88. }
  89. [Test]
  90. public void ConvertTo_MinValue ()
  91. {
  92. Assert.AreEqual (String.Empty, converter.ConvertTo (null,
  93. CultureInfo.InvariantCulture, DateTimeOffset.MinValue, typeof (string)), "#A1");
  94. Assert.AreEqual (String.Empty, converter.ConvertTo (null,
  95. CultureInfo.CurrentCulture, DateTimeOffset.MinValue, typeof (string)), "#A2");
  96. Assert.AreEqual (String.Empty, converter.ConvertTo (DateTimeOffset.MinValue,
  97. typeof (string)), "#A3");
  98. }
  99. [Test]
  100. public void ConvertTo_MaxValue ()
  101. {
  102. Assert.AreEqual (DateTimeOffset.MaxValue.ToString (CultureInfo.InvariantCulture),
  103. converter.ConvertTo (null, CultureInfo.InvariantCulture, DateTimeOffset.MaxValue,
  104. typeof (string)), "#A1");
  105. }
  106. [Test]
  107. [ExpectedException (typeof (NotSupportedException))]
  108. public void ConvertTo_object ()
  109. {
  110. converter.ConvertTo (DateTimeOffset.Now, typeof (object));
  111. }
  112. [Test]
  113. [ExpectedException (typeof (NotSupportedException))]
  114. public void ConvertTo_int ()
  115. {
  116. converter.ConvertTo (DateTimeOffset.Now, typeof (int));
  117. }
  118. [Test]
  119. [ExpectedException (typeof (NotSupportedException))]
  120. public void ConvertTo_DateTime ()
  121. {
  122. converter.ConvertTo (DateTimeOffset.Now, typeof (DateTime));
  123. }
  124. [Test]
  125. [ExpectedException (typeof (NotSupportedException))]
  126. public void ConvertTo_DateTimeOffset ()
  127. {
  128. converter.ConvertTo (DateTimeOffset.Now, typeof (DateTimeOffset));
  129. }
  130. [Test]
  131. public void ConvertToString_MinValue ()
  132. {
  133. Assert.AreEqual (String.Empty, converter.ConvertToString (null,
  134. CultureInfo.InvariantCulture, DateTimeOffset.MinValue), "#A1");
  135. Assert.AreEqual (String.Empty, converter.ConvertToString (null, DateTimeOffset.MinValue), "#A2");
  136. Assert.AreEqual (String.Empty, converter.ConvertToString (null,
  137. CultureInfo.CurrentCulture, DateTimeOffset.MinValue), "#A3");
  138. Assert.AreEqual (String.Empty, converter.ConvertToString (DateTimeOffset.MinValue), "#A4");
  139. }
  140. [Test]
  141. public void ConvertToString_MaxValue ()
  142. {
  143. Assert.AreEqual (DateTimeOffset.MaxValue.ToString (CultureInfo.InvariantCulture),
  144. converter.ConvertToString (null, CultureInfo.InvariantCulture, DateTimeOffset.MaxValue), "#A1");
  145. }
  146. [Test]
  147. [SetCulture("en-US")]
  148. public void ConvertToString ()
  149. {
  150. CultureInfo ciUS = new CultureInfo("en-US");
  151. CultureInfo ciGB = new CultureInfo("en-GB");
  152. CultureInfo ciDE = new CultureInfo("de-DE");
  153. DateTimeOffset dateOffset = new DateTimeOffset (2008, 12, 31, 23, 59, 58, 5, new TimeSpan (3, 6, 0));
  154. DoTestToString ("12/31/2008 11:59 PM +03:06", dateOffset, ciUS);
  155. DoTestToString ("31/12/2008 23:59 +03:06", dateOffset, ciGB);
  156. DoTestToString ("31.12.2008 23:59 +03:06", dateOffset, ciDE);
  157. DoTestToString ("12/31/2008 23:59:58 +03:06", dateOffset, CultureInfo.InvariantCulture);
  158. Assert.AreEqual ("12/31/2008 23:59:58 +03:06", converter.ConvertToInvariantString (dateOffset), "Invariant");
  159. dateOffset = new DateTimeOffset (new DateTime (2008, 12, 31), new TimeSpan (0, 0, 0));
  160. DoTestToString ("12/31/2008 +00:00", dateOffset, ciUS);
  161. DoTestToString ("31/12/2008 +00:00", dateOffset, ciGB);
  162. DoTestToString ("31.12.2008 +00:00", dateOffset, ciDE);
  163. DoTestToString ("2008-12-31 +00:00", dateOffset, CultureInfo.InvariantCulture);
  164. Assert.AreEqual ("2008-12-31 +00:00", converter.ConvertToInvariantString (dateOffset), "Invariant");
  165. }
  166. void DoTestToString (string expected, DateTimeOffset value, CultureInfo ci)
  167. {
  168. String message = ci.Name;
  169. if (message == null || message.Length == 0)
  170. message = "?Invariant";
  171. Assert.AreEqual (expected, converter.ConvertTo (null, ci, value, typeof (string)), message);
  172. }
  173. [Test]
  174. [ExpectedException (typeof (FormatException))]
  175. public void ConvertFrom_InvalidValue ()
  176. {
  177. converter.ConvertFrom ("*1");
  178. }
  179. [Test]
  180. [ExpectedException (typeof (FormatException))]
  181. public void ConvertFrom_InvalidValue_Invariant ()
  182. {
  183. converter.ConvertFrom (null, CultureInfo.InvariantCulture, "*1");
  184. }
  185. [Test]
  186. [ExpectedException (typeof (FormatException))]
  187. public void ConvertFromString_InvalidValue ()
  188. {
  189. converter.ConvertFromString ("*1");
  190. }
  191. [Test]
  192. [ExpectedException (typeof (FormatException))]
  193. public void ConvertFromString_InvalidValue_Invariant ()
  194. {
  195. converter.ConvertFromString (null, CultureInfo.InvariantCulture, "*1");
  196. }
  197. [Test]
  198. public void ConvertTo_InstanceDescriptor ()
  199. {
  200. DateTimeOffset dto = new DateTimeOffset (new DateTime (2010, 10, 11), new TimeSpan (3, 6, 0));
  201. InstanceDescriptor descriptor = (InstanceDescriptor) converter.ConvertTo (dto, typeof (InstanceDescriptor));
  202. Assert.AreEqual (".ctor", descriptor.MemberInfo.Name, "#A0");
  203. Assert.AreEqual (8, descriptor.Arguments.Count, "#A1");
  204. DateTimeOffset dto2 = (DateTimeOffset) descriptor.Invoke ();
  205. Assert.AreEqual (dto, dto2, "#A2");
  206. }
  207. }
  208. }
  209. #endif