2
// Copyright (c) 2007 James Newton-King
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
13
// The above copyright notice and this permission notice shall be
14
// included in all copies or substantial portions of the Software.
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.
27
using System.Collections;
28
using System.Collections.Generic;
29
using System.ComponentModel;
30
using Newtonsoft.Json.Linq;
32
using NUnit.Framework;
34
using Microsoft.VisualStudio.TestPlatform.UnitTestFramework;
35
using TestFixture = Microsoft.VisualStudio.TestPlatform.UnitTestFramework.TestClassAttribute;
36
using Test = Microsoft.VisualStudio.TestPlatform.UnitTestFramework.TestMethodAttribute;
40
namespace Newtonsoft.Json.Tests.Linq
43
public class JPropertyTests : TestFixtureBase
46
public void NullValue()
48
JProperty p = new JProperty("TestProperty", null);
49
Assert.IsNotNull(p.Value);
50
Assert.AreEqual(JTokenType.Null, p.Value.Type);
51
Assert.AreEqual(p, p.Value.Parent);
54
Assert.IsNotNull(p.Value);
55
Assert.AreEqual(JTokenType.Null, p.Value.Type);
56
Assert.AreEqual(p, p.Value.Parent);
59
#if !(SILVERLIGHT || NETFX_CORE || PORTABLE)
61
public void ListChanged()
63
JProperty p = new JProperty("TestProperty", null);
66
ListChangedType? listChangedType = null;
69
l.ListChanged += (sender, args) =>
71
listChangedType = args.ListChangedType;
72
index = args.NewIndex;
77
Assert.AreEqual(ListChangedType.ItemChanged, listChangedType.Value);
78
Assert.AreEqual(0, index.Value);
83
public void IListCount()
85
JProperty p = new JProperty("TestProperty", null);
88
Assert.AreEqual(1, l.Count);
92
public void IListClear()
94
JProperty p = new JProperty("TestProperty", null);
97
ExceptionAssert.Throws<JsonException>(
98
"Cannot add or remove items from Newtonsoft.Json.Linq.JProperty.",
106
public void IListAdd()
108
JProperty p = new JProperty("TestProperty", null);
111
ExceptionAssert.Throws<JsonException>(
112
"Newtonsoft.Json.Linq.JProperty cannot have multiple values.",
120
public void IListRemove()
122
JProperty p = new JProperty("TestProperty", null);
125
ExceptionAssert.Throws<JsonException>(
126
"Cannot add or remove items from Newtonsoft.Json.Linq.JProperty.",
136
JsonReader reader = new JsonTextReader(new StringReader("{'propertyname':['value1']}"));
139
Assert.AreEqual(JsonToken.StartObject, reader.TokenType);
142
JProperty property = JProperty.Load(reader);
143
Assert.AreEqual("propertyname", property.Name);
144
Assert.IsTrue(JToken.DeepEquals(JArray.Parse("['value1']"), property.Value));
146
Assert.AreEqual(JsonToken.EndObject, reader.TokenType);
148
reader = new JsonTextReader(new StringReader("{'propertyname':null}"));
151
Assert.AreEqual(JsonToken.StartObject, reader.TokenType);
154
property = JProperty.Load(reader);
155
Assert.AreEqual("propertyname", property.Name);
156
Assert.IsTrue(JToken.DeepEquals(new JValue(null, JTokenType.Null), property.Value));
158
Assert.AreEqual(JsonToken.EndObject, reader.TokenType);
162
public void MultiContentConstructor()
164
JProperty p = new JProperty("error", new List<string> {"one", "two"});
165
JArray a = (JArray) p.Value;
167
Assert.AreEqual(a.Count, 2);
168
Assert.AreEqual("one", (string) a[0]);
169
Assert.AreEqual("two", (string) a[1]);
173
public void IListGenericAdd()
175
IList<JToken> t = new JProperty("error", new List<string> {"one", "two"});
177
ExceptionAssert.Throws<JsonException>(
178
"Newtonsoft.Json.Linq.JProperty cannot have multiple values.",
b'\\ No newline at end of file'