Json.NET
Code Coverage Statistics for Source File

Newtonsoft.Json.Tests\Converters\JavaScriptDateTimeConverterTests.cs

Symbol Coverage: 96.67% (29 of 30)

Branch Coverage: 85.71% (6 of 7)

Cyclomatic Complexity Avg: 1.00 Max:1

Code Lines: 39


L V Source
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
 1
      JavaScriptDateTimeConverter converter = new JavaScriptDateTimeConverter();
42

  
43
 1
      DateTime d = new DateTime(2000, 12, 15, 22, 11, 3, 55, DateTimeKind.Utc);
44
      string result;
45

  
46
 1
      result = JsonConvert.SerializeObject(d, converter);
47
 1
      Assert.AreEqual("new Date(976918263055)", result);
48
 1
    }
49

  
50
#if !PocketPC && !NET20
51
    [Test]
52
    public void SerializeDateTimeOffset()
53
    {
54
 1
      JavaScriptDateTimeConverter converter = new JavaScriptDateTimeConverter();
55

  
56
 1
      DateTimeOffset now = new DateTimeOffset(2000, 12, 15, 22, 11, 3, 55, TimeSpan.Zero);
57
      string result;
58

  
59
 1
      result = JsonConvert.SerializeObject(now, converter);
60
 1
      Assert.AreEqual("new Date(976918263055)", result);
61
 1
    }
62

  
63
    [Test]
64
    public void SerializeNullableDateTimeClass()
65
    {
66
 1
      NullableDateTimeTestClass t = new NullableDateTimeTestClass()
67
 1
      {
68
 1
        DateTimeField = null,
69
 1
        DateTimeOffsetField = null
70
 1
      };
71

  
72
 1
      JavaScriptDateTimeConverter converter = new JavaScriptDateTimeConverter();
73

  
74
      string result;
75

  
76
 1
      result = JsonConvert.SerializeObject(t, converter);
77
 1
      Assert.AreEqual(@"{""PreField"":null,""DateTimeField"":null,""DateTimeOffsetField"":null,""PostField"":null}", result);
78

  
79
 1
      t = new NullableDateTimeTestClass()
80
 1
      {
81
 1
        DateTimeField = new DateTime(2000, 12, 15, 22, 11, 3, 55, DateTimeKind.Utc),
82
 1
        DateTimeOffsetField = new DateTimeOffset(2000, 12, 15, 22, 11, 3, 55, TimeSpan.Zero)
83
 1
      };
84

  
85
 1
      result = JsonConvert.SerializeObject(t, converter);
86
 1
      Assert.AreEqual(@"{""PreField"":null,""DateTimeField"":new Date(976918263055),""DateTimeOffsetField"":new Date(976918263055),""PostField"":null}", result);
87
 1
    }
88

  
89
    [Test]
90
    [ExpectedException(typeof(Exception), ExpectedMessage = "Cannot convert null value to System.DateTime.")]
91
    public void DeserializeNullToNonNullable()
92
    {
93
 1
      DateTimeTestClass c2 =
94
 1
       JsonConvert.DeserializeObject<DateTimeTestClass>(@"{""PreField"":""Pre"",""DateTimeField"":null,""DateTimeOffsetField"":null,""PostField"":""Post""}", new JavaScriptDateTimeConverter());
95
 0
    }
96

  
97
    [Test]
98
    public void DeserializeDateTimeOffset()
99
    {
100
 1
      JavaScriptDateTimeConverter converter = new JavaScriptDateTimeConverter();
101
 1
      DateTimeOffset start = new DateTimeOffset(2000, 12, 15, 22, 11, 3, 55, TimeSpan.Zero);
102

  
103
 1
      string json = JsonConvert.SerializeObject(start, converter);
104

  
105
 1
      DateTimeOffset result = JsonConvert.DeserializeObject<DateTimeOffset>(json, converter);
106
 1
      Assert.AreEqual(new DateTimeOffset(2000, 12, 15, 22, 11, 3, 55, TimeSpan.Zero), result);
107
 1
    }
108
#endif
109

  
110
    [Test]
111
    public void DeserializeDateTime()
112
    {
113
 1
      JavaScriptDateTimeConverter converter = new JavaScriptDateTimeConverter();
114

  
115
 1
      DateTime result = JsonConvert.DeserializeObject<DateTime>("new Date(976918263055)", converter);
116
 1
      Assert.AreEqual(new DateTime(2000, 12, 15, 22, 11, 3, 55, DateTimeKind.Utc), result);
117
 1
    }
118
  }
119
}