root / trunk / Libraries / Json40r2 / Source / Src / Newtonsoft.Json.Tests / Linq / JPropertyTests.cs @ d78cbf09
History | View | Annotate | Download (3.7 kB)
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 |
JProperty p = new JProperty("TestProperty", null); |
19 |
Assert.IsNotNull(p.Value); |
20 |
Assert.AreEqual(JTokenType.Null, p.Value.Type); |
21 |
Assert.AreEqual(p, p.Value.Parent); |
22 |
|
23 |
p.Value = null; |
24 |
Assert.IsNotNull(p.Value); |
25 |
Assert.AreEqual(JTokenType.Null, p.Value.Type); |
26 |
Assert.AreEqual(p, p.Value.Parent); |
27 |
} |
28 |
|
29 |
#if !SILVERLIGHT |
30 |
[Test] |
31 |
public void ListChanged() |
32 |
{ |
33 |
JProperty p = new JProperty("TestProperty", null); |
34 |
IBindingList l = p; |
35 |
|
36 |
ListChangedType? listChangedType = null; |
37 |
int? index = null; |
38 |
|
39 |
l.ListChanged += (sender, args) => |
40 |
{ |
41 |
listChangedType = args.ListChangedType; |
42 |
index = args.NewIndex; |
43 |
}; |
44 |
|
45 |
p.Value = 1; |
46 |
|
47 |
Assert.AreEqual(ListChangedType.ItemChanged, listChangedType.Value); |
48 |
Assert.AreEqual(0, index.Value); |
49 |
} |
50 |
#endif |
51 |
|
52 |
[Test] |
53 |
public void IListCount() |
54 |
{ |
55 |
JProperty p = new JProperty("TestProperty", null); |
56 |
IList l = p; |
57 |
|
58 |
Assert.AreEqual(1, l.Count); |
59 |
} |
60 |
|
61 |
[Test] |
62 |
[ExpectedException(typeof(Exception), ExpectedMessage = "Cannot add or remove items from Newtonsoft.Json.Linq.JProperty.")] |
63 |
public void IListClear() |
64 |
{ |
65 |
JProperty p = new JProperty("TestProperty", null); |
66 |
IList l = p; |
67 |
|
68 |
l.Clear(); |
69 |
} |
70 |
|
71 |
[Test] |
72 |
[ExpectedException(typeof(Exception), ExpectedMessage = "Newtonsoft.Json.Linq.JProperty cannot have multiple values.")] |
73 |
public void IListAdd() |
74 |
{ |
75 |
JProperty p = new JProperty("TestProperty", null); |
76 |
IList l = p; |
77 |
|
78 |
l.Add(null); |
79 |
} |
80 |
|
81 |
[Test] |
82 |
[ExpectedException(typeof(Exception), ExpectedMessage = "Cannot add or remove items from Newtonsoft.Json.Linq.JProperty.")] |
83 |
public void IListRemove() |
84 |
{ |
85 |
JProperty p = new JProperty("TestProperty", null); |
86 |
IList l = p; |
87 |
|
88 |
l.Remove(p.Value); |
89 |
} |
90 |
|
91 |
[Test] |
92 |
public void Load() |
93 |
{ |
94 |
JsonReader reader = new JsonTextReader(new StringReader("{'propertyname':['value1']}")); |
95 |
reader.Read(); |
96 |
|
97 |
Assert.AreEqual(JsonToken.StartObject, reader.TokenType); |
98 |
reader.Read(); |
99 |
|
100 |
JProperty property = JProperty.Load(reader); |
101 |
Assert.AreEqual("propertyname", property.Name); |
102 |
Assert.IsTrue(JToken.DeepEquals(JArray.Parse("['value1']"), property.Value)); |
103 |
|
104 |
Assert.AreEqual(JsonToken.EndObject, reader.TokenType); |
105 |
|
106 |
reader = new JsonTextReader(new StringReader("{'propertyname':null}")); |
107 |
reader.Read(); |
108 |
|
109 |
Assert.AreEqual(JsonToken.StartObject, reader.TokenType); |
110 |
reader.Read(); |
111 |
|
112 |
property = JProperty.Load(reader); |
113 |
Assert.AreEqual("propertyname", property.Name); |
114 |
Assert.IsTrue(JToken.DeepEquals(new JValue(null, JTokenType.Null), property.Value)); |
115 |
|
116 |
Assert.AreEqual(JsonToken.EndObject, reader.TokenType); |
117 |
} |
118 |
|
119 |
[Test] |
120 |
public void MultiContentConstructor() |
121 |
{ |
122 |
JProperty p = new JProperty("error", new List<string> { "one", "two" }); |
123 |
JArray a = (JArray) p.Value; |
124 |
|
125 |
Assert.AreEqual(a.Count, 2); |
126 |
Assert.AreEqual("one", (string)a[0]); |
127 |
Assert.AreEqual("two", (string)a[1]); |
128 |
} |
129 |
|
130 |
[Test] |
131 |
[ExpectedException(typeof(Exception), ExpectedMessage = "Newtonsoft.Json.Linq.JProperty cannot have multiple values.")] |
132 |
public void IListGenericAdd() |
133 |
{ |
134 |
IList<JToken> t = new JProperty("error", new List<string> { "one", "two" }); |
135 |
t.Add(1); |
136 |
t.Add(2); |
137 |
} |
138 |
} |
139 |
} |