All files
[pithos-ms-client] / trunk / Libraries / Json40r2 / Source / Src / Newtonsoft.Json.Tests / Converters / JavaScriptDateTimeConverterTests.cs
1 #region License
2 // Copyright (c) 2007 James Newton-King
3 //
4 // Permission is hereby granted, free of charge, to any person
5 // obtaining a copy of this software and associated documentation
6 // files (the "Software"), to deal in the Software without
7 // restriction, including without limitation the rights to use,
8 // copy, modify, merge, publish, distribute, sublicense, and/or sell
9 // copies of the Software, and to permit persons to whom the
10 // Software is furnished to do so, subject to the following
11 // conditions:
12 //
13 // The above copyright notice and this permission notice shall be
14 // included in all copies or substantial portions of the Software.
15 //
16 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
18 // OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
20 // HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
21 // WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23 // OTHER DEALINGS IN THE SOFTWARE.
24 #endregion
25
26 using System;
27 using System.Collections.Generic;
28 using System.Linq;
29 using System.Text;
30 using Newtonsoft.Json.Tests.TestObjects;
31 using NUnit.Framework;
32 using Newtonsoft.Json.Converters;
33
34 namespace Newtonsoft.Json.Tests.Converters
35 {
36   public class JavaScriptDateTimeConverterTests : TestFixtureBase
37   {
38     [Test]
39     public void SerializeDateTime()
40     {
41       JavaScriptDateTimeConverter converter = new JavaScriptDateTimeConverter();
42
43       DateTime d = new DateTime(2000, 12, 15, 22, 11, 3, 55, DateTimeKind.Utc);
44       string result;
45
46       result = JsonConvert.SerializeObject(d, converter);
47       Assert.AreEqual("new Date(976918263055)", result);
48     }
49
50 #if !PocketPC && !NET20
51     [Test]
52     public void SerializeDateTimeOffset()
53     {
54       JavaScriptDateTimeConverter converter = new JavaScriptDateTimeConverter();
55
56       DateTimeOffset now = new DateTimeOffset(2000, 12, 15, 22, 11, 3, 55, TimeSpan.Zero);
57       string result;
58
59       result = JsonConvert.SerializeObject(now, converter);
60       Assert.AreEqual("new Date(976918263055)", result);
61     }
62
63     [Test]
64     public void sdfs()
65     {
66       int i = Convert.ToInt32("+1");
67       Console.WriteLine(i);
68     }
69
70     [Test]
71     public void SerializeNullableDateTimeClass()
72     {
73       NullableDateTimeTestClass t = new NullableDateTimeTestClass()
74       {
75         DateTimeField = null,
76         DateTimeOffsetField = null
77       };
78
79       JavaScriptDateTimeConverter converter = new JavaScriptDateTimeConverter();
80
81       string result;
82
83       result = JsonConvert.SerializeObject(t, converter);
84       Assert.AreEqual(@"{""PreField"":null,""DateTimeField"":null,""DateTimeOffsetField"":null,""PostField"":null}", result);
85
86       t = new NullableDateTimeTestClass()
87       {
88         DateTimeField = new DateTime(2000, 12, 15, 22, 11, 3, 55, DateTimeKind.Utc),
89         DateTimeOffsetField = new DateTimeOffset(2000, 12, 15, 22, 11, 3, 55, TimeSpan.Zero)
90       };
91
92       result = JsonConvert.SerializeObject(t, converter);
93       Assert.AreEqual(@"{""PreField"":null,""DateTimeField"":new Date(976918263055),""DateTimeOffsetField"":new Date(976918263055),""PostField"":null}", result);
94     }
95
96     [Test]
97     [ExpectedException(typeof(Exception), ExpectedMessage = "Cannot convert null value to System.DateTime.")]
98     public void DeserializeNullToNonNullable()
99     {
100       DateTimeTestClass c2 =
101        JsonConvert.DeserializeObject<DateTimeTestClass>(@"{""PreField"":""Pre"",""DateTimeField"":null,""DateTimeOffsetField"":null,""PostField"":""Post""}", new JavaScriptDateTimeConverter());
102     }
103
104     [Test]
105     public void DeserializeDateTimeOffset()
106     {
107       JavaScriptDateTimeConverter converter = new JavaScriptDateTimeConverter();
108       DateTimeOffset start = new DateTimeOffset(2000, 12, 15, 22, 11, 3, 55, TimeSpan.Zero);
109
110       string json = JsonConvert.SerializeObject(start, converter);
111
112       DateTimeOffset result = JsonConvert.DeserializeObject<DateTimeOffset>(json, converter);
113       Assert.AreEqual(new DateTimeOffset(2000, 12, 15, 22, 11, 3, 55, TimeSpan.Zero), result);
114     }
115 #endif
116
117     [Test]
118     public void DeserializeDateTime()
119     {
120       JavaScriptDateTimeConverter converter = new JavaScriptDateTimeConverter();
121
122       DateTime result = JsonConvert.DeserializeObject<DateTime>("new Date(976918263055)", converter);
123       Assert.AreEqual(new DateTime(2000, 12, 15, 22, 11, 3, 55, DateTimeKind.Utc), result);
124     }
125   }
126 }