Json.NET
Code Coverage Statistics for Source File

Newtonsoft.Json.Tests\Linq\JValueTests.cs

Symbol Coverage: 89.47% (68 of 76)

Branch Coverage: 52.94% (9 of 17)

Cyclomatic Complexity Avg: 1.00 Max:1

Code Lines: 76


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 NUnit.Framework;
31
using Newtonsoft.Json.Linq;
32

  
33
namespace Newtonsoft.Json.Tests.Linq
34
{
35
  public class JValueTests : TestFixtureBase
36
  {
37
    [Test]
38
    public void ChangeValue()
39
    {
40
 1
      JValue v = new JValue(true);
41
 1
      Assert.AreEqual(true, v.Value);
42
 1
      Assert.AreEqual(JTokenType.Boolean, v.Type);
43

  
44
 1
      v.Value = "Pie";
45
 1
      Assert.AreEqual("Pie", v.Value);
46
 1
      Assert.AreEqual(JTokenType.String, v.Type);
47

  
48
 1
      v.Value = null;
49
 1
      Assert.AreEqual(null, v.Value);
50
 1
      Assert.AreEqual(JTokenType.Null, v.Type);
51

  
52
 1
      v.Value = (int?)null;
53
 1
      Assert.AreEqual(null, v.Value);
54
 1
      Assert.AreEqual(JTokenType.Null, v.Type);
55

  
56
 1
      v.Value = "Pie";
57
 1
      Assert.AreEqual("Pie", v.Value);
58
 1
      Assert.AreEqual(JTokenType.String, v.Type);
59

  
60
 1
      v.Value = DBNull.Value;
61
 1
      Assert.AreEqual(DBNull.Value, v.Value);
62
 1
      Assert.AreEqual(JTokenType.Null, v.Type);
63

  
64
 1
      byte[] data = new byte[0];
65
 1
      v.Value = data;
66

  
67
 1
      Assert.AreEqual(data, v.Value);
68
 1
      Assert.AreEqual(JTokenType.Bytes, v.Type);
69

  
70
 1
      v.Value = StringComparison.OrdinalIgnoreCase;
71
 1
      Assert.AreEqual(StringComparison.OrdinalIgnoreCase, v.Value);
72
 1
      Assert.AreEqual(JTokenType.Integer, v.Type);
73
 1
    }
74

  
75
    [Test]
76
    public void CreateComment()
77
    {
78
 1
      JValue commentValue = JValue.CreateComment(null);
79
 1
      Assert.AreEqual(null, commentValue.Value);
80
 1
      Assert.AreEqual(JTokenType.Comment, commentValue.Type);
81

  
82
 1
      commentValue.Value = "Comment";
83
 1
      Assert.AreEqual("Comment", commentValue.Value);
84
 1
      Assert.AreEqual(JTokenType.Comment, commentValue.Type);
85
 1
    }
86

  
87
    [Test]
88
    public void CreateString()
89
    {
90
 1
      JValue stringValue = JValue.CreateString(null);
91
 1
      Assert.AreEqual(null, stringValue.Value);
92
 1
      Assert.AreEqual(JTokenType.String, stringValue.Type);
93
 1
    }
94

  
95
    [Test]
96
    [ExpectedException(typeof(InvalidOperationException), ExpectedMessage = "Cannot access child value on Newtonsoft.Json.Linq.JValue.")]
97
    public void Last()
98
    {
99
 1
      JValue v = new JValue(true);
100
 1
      JToken last = v.Last;
101
 0
    }
102

  
103
    [Test]
104
    [ExpectedException(typeof(InvalidOperationException), ExpectedMessage = "Cannot access child value on Newtonsoft.Json.Linq.JValue.")]
105
    public void Children()
106
    {
107
 1
      JValue v = new JValue(true);
108
 1
      var c = v.Children();
109
 0
    }
110

  
111
    [Test]
112
    [ExpectedException(typeof(InvalidOperationException), ExpectedMessage = "Cannot access child value on Newtonsoft.Json.Linq.JValue.")]
113
    public void First()
114
    {
115
 1
      JValue v = new JValue(true);
116
 1
      JToken first = v.First;
117
 0
    }
118

  
119
    [Test]
120
    [ExpectedException(typeof(InvalidOperationException), ExpectedMessage = "Cannot access child value on Newtonsoft.Json.Linq.JValue.")]
121
    public void Item()
122
    {
123
 1
      JValue v = new JValue(true);
124
 1
      JToken first = v[0];
125
 0
    }
126

  
127
    [Test]
128
    [ExpectedException(typeof(InvalidOperationException), ExpectedMessage = "Cannot access child value on Newtonsoft.Json.Linq.JValue.")]
129
    public void Values()
130
    {
131
 1
      JValue v = new JValue(true);
132
 1
      v.Values<int>();
133
 0
    }
134

  
135
    [Test]
136
    [ExpectedException(typeof(InvalidOperationException), ExpectedMessage = "The parent is missing.")]
137
    public void RemoveParentNull()
138
    {
139
 1
      JValue v = new JValue(true);
140
 1
      v.Remove();
141
 0
    }
142

  
143
    [Test]
144
    public void Root()
145
    {
146
 1
      JValue v = new JValue(true);
147
 1
      Assert.AreEqual(v, v.Root);
148
 1
    }
149

  
150
    [Test]
151
    public void Previous()
152
    {
153
 1
      JValue v = new JValue(true);
154
 1
      Assert.IsNull(v.Previous);
155
 1
    }
156

  
157
    [Test]
158
    public void Next()
159
    {
160
 1
      JValue v = new JValue(true);
161
 1
      Assert.IsNull(v.Next);
162
 1
    }
163

  
164
    [Test]
165
    public void DeepEquals()
166
    {
167
 1
      Assert.IsTrue(JToken.DeepEquals(new JValue(5L), new JValue(5)));
168
 1
      Assert.IsFalse(JToken.DeepEquals(new JValue(5M), new JValue(5)));
169
 1
      Assert.IsTrue(JToken.DeepEquals(new JValue((ulong)long.MaxValue), new JValue(long.MaxValue)));
170
 1
    }
171

  
172
    [Test]
173
    public void HasValues()
174
    {
175
 1
      Assert.IsFalse((new JValue(5L)).HasValues);
176
 1
    }
177

  
178
    [Test]
179
    [ExpectedException(typeof(InvalidOperationException), ExpectedMessage = "Cannot set child value on Newtonsoft.Json.Linq.JValue.")]
180
    public void SetValue()
181
    {
182
 1
      JToken t = new JValue(5L);
183
 1
      t[0] = new JValue(3);
184
 0
    }
185

  
186
    [Test]
187
    [ExpectedException(typeof(ArgumentException), ExpectedMessage = "Can not convert Null to Int32.")]
188
    public void CastNullValueToNonNullable()
189
    {
190
 1
      JValue v = new JValue((object)null);
191
 1
      int i = (int) v;
192
 0
    }
193
  }
194
}