Json.NET
Code Coverage Statistics for Source File

Newtonsoft.Json.Tests\Linq\JPropertyTests.cs

Symbol Coverage: 92.06% (58 of 63)

Branch Coverage: 60.00% (6 of 10)

Cyclomatic Complexity Avg: 1.00 Max:1

Code Lines: 65


L V Source
1
using System;
2
using System.Collections;
3
using System.Collections.Generic;
4
using System.ComponentModel;
5
using System.Linq;
6
using System.Text;
7
using Newtonsoft.Json.Linq;
8
using NUnit.Framework;
9
using System.IO;
10

  
11
namespace Newtonsoft.Json.Tests.Linq
12
{
13
  public class JPropertyTests : TestFixtureBase
14
  {
15
    [Test]
16
    public void NullValue()
17
    {
18
 1
      JProperty p = new JProperty("TestProperty", null);
19
 1
      Assert.IsNotNull(p.Value);
20
 1
      Assert.AreEqual(JTokenType.Null, p.Value.Type);
21
 1
      Assert.AreEqual(p, p.Value.Parent);
22

  
23
 1
      p.Value = null;
24
 1
      Assert.IsNotNull(p.Value);
25
 1
      Assert.AreEqual(JTokenType.Null, p.Value.Type);
26
 1
      Assert.AreEqual(p, p.Value.Parent);
27
 1
    }
28

  
29
#if !SILVERLIGHT
30
    [Test]
31
    public void ListChanged()
32
    {
33
 1
      JProperty p = new JProperty("TestProperty", null);
34
 1
      IBindingList l = p;
35

  
36
 1
      ListChangedType? listChangedType = null;
37
 1
      int? index = null;
38

  
39
 1
      l.ListChanged += (sender, args) =>
40
 1
      {
41
 1
        listChangedType = args.ListChangedType;
42
 1
        index = args.NewIndex;
43
 1
      };
44

  
45
 1
      p.Value = 1;
46

  
47
 1
      Assert.AreEqual(ListChangedType.ItemChanged, listChangedType.Value);
48
 1
      Assert.AreEqual(0, index.Value); 
49
 1
    }
50
#endif
51

  
52
    [Test]
53
    public void IListCount()
54
    {
55
 1
      JProperty p = new JProperty("TestProperty", null);
56
 1
      IList l = p;
57

  
58
 1
      Assert.AreEqual(1, l.Count);
59
 1
    }
60

  
61
    [Test]
62
    [ExpectedException(typeof(Exception), ExpectedMessage = "Cannot add or remove items from Newtonsoft.Json.Linq.JProperty.")]
63
    public void IListClear()
64
    {
65
 1
      JProperty p = new JProperty("TestProperty", null);
66
 1
      IList l = p;
67

  
68
 1
      l.Clear();
69
 0
    }
70

  
71
    [Test]
72
    [ExpectedException(typeof(Exception), ExpectedMessage = "Newtonsoft.Json.Linq.JProperty cannot have multiple values.")]
73
    public void IListAdd()
74
    {
75
 1
      JProperty p = new JProperty("TestProperty", null);
76
 1
      IList l = p;
77

  
78
 1
      l.Add(null);
79
 0
    }
80

  
81
    [Test]
82
    [ExpectedException(typeof(Exception), ExpectedMessage = "Cannot add or remove items from Newtonsoft.Json.Linq.JProperty.")]
83
    public void IListRemove()
84
    {
85
 1
      JProperty p = new JProperty("TestProperty", null);
86
 1
      IList l = p;
87

  
88
 1
      l.Remove(p.Value);
89
 0
    }
90

  
91
    [Test]
92
    public void Load()
93
    {
94
 1
      JsonReader reader = new JsonTextReader(new StringReader("{'propertyname':['value1']}"));
95
 1
      reader.Read();
96

  
97
 1
      Assert.AreEqual(JsonToken.StartObject, reader.TokenType);
98
 1
      reader.Read();
99

  
100
 1
      JProperty property = JProperty.Load(reader);
101
 1
      Assert.AreEqual("propertyname", property.Name);
102
 1
      Assert.IsTrue(JToken.DeepEquals(JArray.Parse("['value1']"), property.Value));
103

  
104
 1
      Assert.AreEqual(JsonToken.EndObject, reader.TokenType);
105

  
106
 1
      reader = new JsonTextReader(new StringReader("{'propertyname':null}"));
107
 1
      reader.Read();
108

  
109
 1
      Assert.AreEqual(JsonToken.StartObject, reader.TokenType);
110
 1
      reader.Read();
111

  
112
 1
      property = JProperty.Load(reader);
113
 1
      Assert.AreEqual("propertyname", property.Name);
114
 1
      Assert.IsTrue(JToken.DeepEquals(new JValue(null, JTokenType.Null), property.Value));
115

  
116
 1
      Assert.AreEqual(JsonToken.EndObject, reader.TokenType);
117
 1
    }
118

  
119
    [Test]
120
    public void MultiContentConstructor()
121
    {
122
 1
      JProperty p = new JProperty("error", new List<string> { "one", "two" });
123
 1
      JArray a = (JArray) p.Value;
124

  
125
 1
      Assert.AreEqual(a.Count, 2);
126
 1
      Assert.AreEqual("one", (string)a[0]);
127
 1
      Assert.AreEqual("two", (string)a[1]);
128
 1
    }
129

  
130
    [Test]
131
    [ExpectedException(typeof(Exception), ExpectedMessage = "Newtonsoft.Json.Linq.JProperty cannot have multiple values.")]
132
    public void IListGenericAdd()
133
    {
134
 1
      IList<JToken> t = new JProperty("error", new List<string> { "one", "two" });
135
 1
      t.Add(1);
136
 0
      t.Add(2);
137
 0
    }
138
  }
139
}