~ubuntu-branches/ubuntu/trusty/monodevelop/trusty-proposed

« back to all changes in this revision

Viewing changes to external/Newtonsoft.Json/Src/Newtonsoft.Json.Tests/Linq/JPropertyTests.cs

  • Committer: Package Import Robot
  • Author(s): Jo Shields
  • Date: 2013-05-12 09:46:03 UTC
  • mto: This revision was merged to the branch mainline in revision 29.
  • Revision ID: package-import@ubuntu.com-20130512094603-mad323bzcxvmcam0
Tags: upstream-4.0.5+dfsg
Import upstream version 4.0.5+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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;
 
28
using System.Collections.Generic;
 
29
using System.ComponentModel;
 
30
using Newtonsoft.Json.Linq;
 
31
#if !NETFX_CORE
 
32
using NUnit.Framework;
 
33
#else
 
34
using Microsoft.VisualStudio.TestPlatform.UnitTestFramework;
 
35
using TestFixture = Microsoft.VisualStudio.TestPlatform.UnitTestFramework.TestClassAttribute;
 
36
using Test = Microsoft.VisualStudio.TestPlatform.UnitTestFramework.TestMethodAttribute;
 
37
#endif
 
38
using System.IO;
 
39
 
 
40
namespace Newtonsoft.Json.Tests.Linq
 
41
{
 
42
  [TestFixture]
 
43
  public class JPropertyTests : TestFixtureBase
 
44
  {
 
45
    [Test]
 
46
    public void NullValue()
 
47
    {
 
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);
 
52
 
 
53
      p.Value = null;
 
54
      Assert.IsNotNull(p.Value);
 
55
      Assert.AreEqual(JTokenType.Null, p.Value.Type);
 
56
      Assert.AreEqual(p, p.Value.Parent);
 
57
    }
 
58
 
 
59
#if !(SILVERLIGHT || NETFX_CORE || PORTABLE)
 
60
    [Test]
 
61
    public void ListChanged()
 
62
    {
 
63
      JProperty p = new JProperty("TestProperty", null);
 
64
      IBindingList l = p;
 
65
 
 
66
      ListChangedType? listChangedType = null;
 
67
      int? index = null;
 
68
 
 
69
      l.ListChanged += (sender, args) =>
 
70
        {
 
71
          listChangedType = args.ListChangedType;
 
72
          index = args.NewIndex;
 
73
        };
 
74
 
 
75
      p.Value = 1;
 
76
 
 
77
      Assert.AreEqual(ListChangedType.ItemChanged, listChangedType.Value);
 
78
      Assert.AreEqual(0, index.Value);
 
79
    }
 
80
#endif
 
81
 
 
82
    [Test]
 
83
    public void IListCount()
 
84
    {
 
85
      JProperty p = new JProperty("TestProperty", null);
 
86
      IList l = p;
 
87
 
 
88
      Assert.AreEqual(1, l.Count);
 
89
    }
 
90
 
 
91
    [Test]
 
92
    public void IListClear()
 
93
    {
 
94
      JProperty p = new JProperty("TestProperty", null);
 
95
      IList l = p;
 
96
 
 
97
      ExceptionAssert.Throws<JsonException>(
 
98
        "Cannot add or remove items from Newtonsoft.Json.Linq.JProperty.",
 
99
        () =>
 
100
        {
 
101
          l.Clear();
 
102
        });
 
103
    }
 
104
 
 
105
    [Test]
 
106
    public void IListAdd()
 
107
    {
 
108
      JProperty p = new JProperty("TestProperty", null);
 
109
      IList l = p;
 
110
 
 
111
      ExceptionAssert.Throws<JsonException>(
 
112
        "Newtonsoft.Json.Linq.JProperty cannot have multiple values.",
 
113
        () =>
 
114
          {
 
115
            l.Add(null);
 
116
          });
 
117
    }
 
118
 
 
119
    [Test]
 
120
    public void IListRemove()
 
121
    {
 
122
      JProperty p = new JProperty("TestProperty", null);
 
123
      IList l = p;
 
124
 
 
125
      ExceptionAssert.Throws<JsonException>(
 
126
        "Cannot add or remove items from Newtonsoft.Json.Linq.JProperty.",
 
127
        () =>
 
128
          {
 
129
            l.Remove(p.Value);
 
130
          });
 
131
    }
 
132
 
 
133
    [Test]
 
134
    public void Load()
 
135
    {
 
136
      JsonReader reader = new JsonTextReader(new StringReader("{'propertyname':['value1']}"));
 
137
      reader.Read();
 
138
 
 
139
      Assert.AreEqual(JsonToken.StartObject, reader.TokenType);
 
140
      reader.Read();
 
141
 
 
142
      JProperty property = JProperty.Load(reader);
 
143
      Assert.AreEqual("propertyname", property.Name);
 
144
      Assert.IsTrue(JToken.DeepEquals(JArray.Parse("['value1']"), property.Value));
 
145
 
 
146
      Assert.AreEqual(JsonToken.EndObject, reader.TokenType);
 
147
 
 
148
      reader = new JsonTextReader(new StringReader("{'propertyname':null}"));
 
149
      reader.Read();
 
150
 
 
151
      Assert.AreEqual(JsonToken.StartObject, reader.TokenType);
 
152
      reader.Read();
 
153
 
 
154
      property = JProperty.Load(reader);
 
155
      Assert.AreEqual("propertyname", property.Name);
 
156
      Assert.IsTrue(JToken.DeepEquals(new JValue(null, JTokenType.Null), property.Value));
 
157
 
 
158
      Assert.AreEqual(JsonToken.EndObject, reader.TokenType);
 
159
    }
 
160
 
 
161
    [Test]
 
162
    public void MultiContentConstructor()
 
163
    {
 
164
      JProperty p = new JProperty("error", new List<string> {"one", "two"});
 
165
      JArray a = (JArray) p.Value;
 
166
 
 
167
      Assert.AreEqual(a.Count, 2);
 
168
      Assert.AreEqual("one", (string) a[0]);
 
169
      Assert.AreEqual("two", (string) a[1]);
 
170
    }
 
171
 
 
172
    [Test]
 
173
    public void IListGenericAdd()
 
174
    {
 
175
      IList<JToken> t = new JProperty("error", new List<string> {"one", "two"});
 
176
 
 
177
      ExceptionAssert.Throws<JsonException>(
 
178
        "Newtonsoft.Json.Linq.JProperty cannot have multiple values.",
 
179
        () =>
 
180
          {
 
181
            t.Add(1);
 
182
          });
 
183
    }
 
184
  }
 
185
}
 
 
b'\\ No newline at end of file'