-Added WriteValue(object) to JsonWriter
-Added JTokenEqualityComparer -Added additional validation to new IDictionary/ICollection methods on JObject/JArray -Changed JObject so that properties must have a unique name -Changed remaining IConvertible calls to use ToXXX methods instead of casting -Fixed many bugs in new IDictionary/ICollection methods on JObject/JArray -Fixed AddFirst when no existing children
This commit is contained in:
@@ -113,5 +113,65 @@ now brown cow?", '"', true);
|
||||
string json = JavaScriptConvert.ToString(StringComparison.CurrentCultureIgnoreCase);
|
||||
Assert.AreEqual("1", json);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ObjectToString()
|
||||
{
|
||||
object value;
|
||||
|
||||
value = 1;
|
||||
Assert.AreEqual("1", JavaScriptConvert.ToString(value));
|
||||
|
||||
value = 1.1;
|
||||
Assert.AreEqual("1.1", JavaScriptConvert.ToString(value));
|
||||
|
||||
value = 1.1m;
|
||||
Assert.AreEqual("1.1", JavaScriptConvert.ToString(value));
|
||||
|
||||
value = (float)1.1;
|
||||
Assert.AreEqual("1.1", JavaScriptConvert.ToString(value));
|
||||
|
||||
value = (short)1;
|
||||
Assert.AreEqual("1", JavaScriptConvert.ToString(value));
|
||||
|
||||
value = (long)1;
|
||||
Assert.AreEqual("1", JavaScriptConvert.ToString(value));
|
||||
|
||||
value = (byte)1;
|
||||
Assert.AreEqual("1", JavaScriptConvert.ToString(value));
|
||||
|
||||
value = (uint)1;
|
||||
Assert.AreEqual("1", JavaScriptConvert.ToString(value));
|
||||
|
||||
value = (ushort)1;
|
||||
Assert.AreEqual("1", JavaScriptConvert.ToString(value));
|
||||
|
||||
value = (sbyte)1;
|
||||
Assert.AreEqual("1", JavaScriptConvert.ToString(value));
|
||||
|
||||
value = (ulong)1;
|
||||
Assert.AreEqual("1", JavaScriptConvert.ToString(value));
|
||||
|
||||
value = new DateTime(JavaScriptConvert.InitialJavaScriptDateTicks, DateTimeKind.Utc);
|
||||
Assert.AreEqual(@"""\/Date(0)\/""", JavaScriptConvert.ToString(value));
|
||||
|
||||
value = new DateTimeOffset(JavaScriptConvert.InitialJavaScriptDateTicks, TimeSpan.Zero);
|
||||
Assert.AreEqual(@"""\/Date(0+0000)\/""", JavaScriptConvert.ToString(value));
|
||||
|
||||
value = null;
|
||||
Assert.AreEqual("null", JavaScriptConvert.ToString(value));
|
||||
|
||||
value = DBNull.Value;
|
||||
Assert.AreEqual("null", JavaScriptConvert.ToString(value));
|
||||
|
||||
value = "I am a string";
|
||||
Assert.AreEqual(@"""I am a string""", JavaScriptConvert.ToString(value));
|
||||
|
||||
value = true;
|
||||
Assert.AreEqual("true", JavaScriptConvert.ToString(value));
|
||||
|
||||
value = 'c';
|
||||
Assert.AreEqual(@"""c""", JavaScriptConvert.ToString(value));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,294 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using NUnit.Framework;
|
||||
using Newtonsoft.Json.Linq;
|
||||
|
||||
namespace Newtonsoft.Json.Tests.Linq
|
||||
{
|
||||
public class JArrayTests : TestFixtureBase
|
||||
{
|
||||
[Test]
|
||||
public void Clear()
|
||||
{
|
||||
JArray a = new JArray { 1 };
|
||||
Assert.AreEqual(1, a.Count);
|
||||
|
||||
a.Clear();
|
||||
Assert.AreEqual(0, a.Count);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Contains()
|
||||
{
|
||||
JValue v = new JValue(1);
|
||||
|
||||
JArray a = new JArray { v };
|
||||
|
||||
Assert.AreEqual(false, a.Contains(new JValue(2)));
|
||||
Assert.AreEqual(false, a.Contains(new JValue(1)));
|
||||
Assert.AreEqual(false, a.Contains(null));
|
||||
Assert.AreEqual(true, a.Contains(v));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void GenericCollectionCopyTo()
|
||||
{
|
||||
JArray j = new JArray();
|
||||
j.Add(new JValue(1));
|
||||
j.Add(new JValue(2));
|
||||
j.Add(new JValue(3));
|
||||
Assert.AreEqual(3, j.Count);
|
||||
|
||||
JToken[] a = new JToken[5];
|
||||
|
||||
((ICollection<JToken>)j).CopyTo(a, 1);
|
||||
|
||||
Assert.AreEqual(null, a[0]);
|
||||
|
||||
Assert.AreEqual(1, (int)a[1]);
|
||||
|
||||
Assert.AreEqual(2, (int)a[2]);
|
||||
|
||||
Assert.AreEqual(3, (int)a[3]);
|
||||
|
||||
Assert.AreEqual(null, a[4]);
|
||||
|
||||
}
|
||||
|
||||
[Test]
|
||||
[ExpectedException(typeof(ArgumentNullException), ExpectedMessage = @"Value cannot be null.
|
||||
Parameter name: array")]
|
||||
public void GenericCollectionCopyToNullArrayShouldThrow()
|
||||
{
|
||||
JArray j = new JArray();
|
||||
((ICollection<JToken>)j).CopyTo(null, 0);
|
||||
}
|
||||
|
||||
[Test]
|
||||
[ExpectedException(typeof(ArgumentOutOfRangeException), ExpectedMessage = @"arrayIndex is less than 0.
|
||||
Parameter name: arrayIndex")]
|
||||
public void GenericCollectionCopyToNegativeArrayIndexShouldThrow()
|
||||
{
|
||||
JArray j = new JArray();
|
||||
((ICollection<JToken>)j).CopyTo(new JToken[1], -1);
|
||||
}
|
||||
|
||||
[Test]
|
||||
[ExpectedException(typeof(ArgumentException), ExpectedMessage = @"arrayIndex is equal to or greater than the length of array.")]
|
||||
public void GenericCollectionCopyToArrayIndexEqualGreaterToArrayLengthShouldThrow()
|
||||
{
|
||||
JArray j = new JArray();
|
||||
((ICollection<JToken>)j).CopyTo(new JToken[1], 1);
|
||||
}
|
||||
|
||||
[Test]
|
||||
[ExpectedException(typeof(ArgumentException), ExpectedMessage = @"The number of elements in the source JObject is greater than the available space from arrayIndex to the end of the destination array.")]
|
||||
public void GenericCollectionCopyToInsufficientArrayCapacity()
|
||||
{
|
||||
JArray j = new JArray();
|
||||
j.Add(new JValue(1));
|
||||
j.Add(new JValue(2));
|
||||
j.Add(new JValue(3));
|
||||
|
||||
((ICollection<JToken>)j).CopyTo(new JToken[3], 1);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Remove()
|
||||
{
|
||||
JValue v = new JValue(1);
|
||||
JArray j = new JArray();
|
||||
j.Add(v);
|
||||
|
||||
Assert.AreEqual(1, j.Count);
|
||||
|
||||
Assert.AreEqual(false, j.Remove(new JValue(1)));
|
||||
Assert.AreEqual(false, j.Remove(null));
|
||||
Assert.AreEqual(true, j.Remove(v));
|
||||
Assert.AreEqual(false, j.Remove(v));
|
||||
|
||||
Assert.AreEqual(0, j.Count);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void IndexOf()
|
||||
{
|
||||
JValue v1 = new JValue(1);
|
||||
JValue v2 = new JValue(1);
|
||||
JValue v3 = new JValue(1);
|
||||
|
||||
JArray j = new JArray();
|
||||
|
||||
j.Add(v1);
|
||||
Assert.AreEqual(0, j.IndexOf(v1));
|
||||
|
||||
j.Add(v2);
|
||||
Assert.AreEqual(0, j.IndexOf(v1));
|
||||
Assert.AreEqual(1, j.IndexOf(v2));
|
||||
|
||||
j.AddFirst(v3);
|
||||
Assert.AreEqual(1, j.IndexOf(v1));
|
||||
Assert.AreEqual(2, j.IndexOf(v2));
|
||||
Assert.AreEqual(0, j.IndexOf(v3));
|
||||
|
||||
v3.Remove();
|
||||
Assert.AreEqual(0, j.IndexOf(v1));
|
||||
Assert.AreEqual(1, j.IndexOf(v2));
|
||||
Assert.AreEqual(-1, j.IndexOf(v3));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void RemoveAt()
|
||||
{
|
||||
JValue v1 = new JValue(1);
|
||||
JValue v2 = new JValue(1);
|
||||
JValue v3 = new JValue(1);
|
||||
|
||||
JArray j = new JArray();
|
||||
|
||||
j.Add(v1);
|
||||
j.Add(v2);
|
||||
j.Add(v3);
|
||||
|
||||
Assert.AreEqual(true, j.Contains(v1));
|
||||
j.RemoveAt(0);
|
||||
Assert.AreEqual(false, j.Contains(v1));
|
||||
|
||||
Assert.AreEqual(true, j.Contains(v3));
|
||||
j.RemoveAt(1);
|
||||
Assert.AreEqual(false, j.Contains(v3));
|
||||
|
||||
Assert.AreEqual(1, j.Count);
|
||||
}
|
||||
|
||||
[Test]
|
||||
[ExpectedException(typeof(ArgumentOutOfRangeException), ExpectedMessage = @"index is equal to or greater than Count.
|
||||
Parameter name: index")]
|
||||
public void RemoveAtOutOfRangeIndexShouldError()
|
||||
{
|
||||
JArray j = new JArray();
|
||||
j.RemoveAt(0);
|
||||
}
|
||||
|
||||
[Test]
|
||||
[ExpectedException(typeof(ArgumentOutOfRangeException), ExpectedMessage = @"index is less than 0.
|
||||
Parameter name: index")]
|
||||
public void RemoveAtNegativeIndexShouldError()
|
||||
{
|
||||
JArray j = new JArray();
|
||||
j.RemoveAt(-1);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Insert()
|
||||
{
|
||||
JValue v1 = new JValue(1);
|
||||
JValue v2 = new JValue(2);
|
||||
JValue v3 = new JValue(3);
|
||||
JValue v4 = new JValue(4);
|
||||
|
||||
JArray j = new JArray();
|
||||
|
||||
j.Add(v1);
|
||||
j.Add(v2);
|
||||
j.Add(v3);
|
||||
j.Insert(1, v4);
|
||||
|
||||
Assert.AreEqual(0, j.IndexOf(v1));
|
||||
Assert.AreEqual(1, j.IndexOf(v4));
|
||||
Assert.AreEqual(2, j.IndexOf(v2));
|
||||
Assert.AreEqual(3, j.IndexOf(v3));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void AddFirstAddedTokenShouldBeFirst()
|
||||
{
|
||||
JValue v1 = new JValue(1);
|
||||
JValue v2 = new JValue(2);
|
||||
JValue v3 = new JValue(3);
|
||||
|
||||
JArray j = new JArray();
|
||||
Assert.AreEqual(null, j.First);
|
||||
Assert.AreEqual(null, j.Last);
|
||||
|
||||
j.AddFirst(v1);
|
||||
Assert.AreEqual(v1, j.First);
|
||||
Assert.AreEqual(v1, j.Last);
|
||||
|
||||
j.AddFirst(v2);
|
||||
Assert.AreEqual(v2, j.First);
|
||||
Assert.AreEqual(v1, j.Last);
|
||||
|
||||
j.AddFirst(v3);
|
||||
Assert.AreEqual(v3, j.First);
|
||||
Assert.AreEqual(v1, j.Last);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void InsertShouldInsertAtZeroIndex()
|
||||
{
|
||||
JValue v1 = new JValue(1);
|
||||
JValue v2 = new JValue(2);
|
||||
|
||||
JArray j = new JArray();
|
||||
|
||||
j.Insert(0, v1);
|
||||
Assert.AreEqual(0, j.IndexOf(v1));
|
||||
|
||||
j.Insert(0, v2);
|
||||
Assert.AreEqual(1, j.IndexOf(v1));
|
||||
Assert.AreEqual(0, j.IndexOf(v2));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void InsertNull()
|
||||
{
|
||||
JArray j = new JArray();
|
||||
j.Insert(0, null);
|
||||
|
||||
Assert.AreEqual(null, ((JValue)j[0]).Value);
|
||||
}
|
||||
|
||||
[Test]
|
||||
[ExpectedException(typeof(ArgumentOutOfRangeException), ExpectedMessage = @"Specified argument was out of the range of valid values.
|
||||
Parameter name: index")]
|
||||
public void InsertNegativeIndexShouldThrow()
|
||||
{
|
||||
JArray j = new JArray();
|
||||
j.Insert(-1, new JValue(1));
|
||||
}
|
||||
|
||||
[Test]
|
||||
[ExpectedException(typeof(ArgumentOutOfRangeException), ExpectedMessage = @"Specified argument was out of the range of valid values.
|
||||
Parameter name: index")]
|
||||
public void InsertOutOfRangeIndexShouldThrow()
|
||||
{
|
||||
JArray j = new JArray();
|
||||
j.Insert(2, new JValue(1));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Item()
|
||||
{
|
||||
JValue v1 = new JValue(1);
|
||||
JValue v2 = new JValue(2);
|
||||
JValue v3 = new JValue(3);
|
||||
JValue v4 = new JValue(4);
|
||||
|
||||
JArray j = new JArray();
|
||||
|
||||
j.Add(v1);
|
||||
j.Add(v2);
|
||||
j.Add(v3);
|
||||
|
||||
j[1] = v4;
|
||||
|
||||
Assert.AreEqual(null, v2.Parent);
|
||||
Assert.AreEqual(-1, j.IndexOf(v2));
|
||||
Assert.AreEqual(j, v4.Parent);
|
||||
Assert.AreEqual(1, j.IndexOf(v4));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,233 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using NUnit.Framework;
|
||||
using Newtonsoft.Json.Linq;
|
||||
|
||||
namespace Newtonsoft.Json.Tests.Linq
|
||||
{
|
||||
public class JObjectTests : TestFixtureBase
|
||||
{
|
||||
[Test]
|
||||
public void TryGetValue()
|
||||
{
|
||||
JObject o = new JObject();
|
||||
o.Add("PropertyNameValue", new JValue(1));
|
||||
Assert.AreEqual(1, o.Children().Count());
|
||||
|
||||
JToken t;
|
||||
Assert.AreEqual(false, o.TryGetValue("sdf", out t));
|
||||
Assert.AreEqual(null, t);
|
||||
|
||||
Assert.AreEqual(false, o.TryGetValue(null, out t));
|
||||
Assert.AreEqual(null, t);
|
||||
|
||||
Assert.AreEqual(true, o.TryGetValue("PropertyNameValue", out t));
|
||||
Assert.AreEqual(true, JToken.DeepEquals(new JValue(1), t));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void DictionaryItemShouldSet()
|
||||
{
|
||||
JObject o = new JObject();
|
||||
o["PropertyNameValue"] = new JValue(1);
|
||||
Assert.AreEqual(1, o.Children().Count());
|
||||
|
||||
JToken t;
|
||||
Assert.AreEqual(true, o.TryGetValue("PropertyNameValue", out t));
|
||||
Assert.AreEqual(true, JToken.DeepEquals(new JValue(1), t));
|
||||
|
||||
o["PropertyNameValue"] = new JValue(2);
|
||||
Assert.AreEqual(1, o.Children().Count());
|
||||
|
||||
Assert.AreEqual(true, o.TryGetValue("PropertyNameValue", out t));
|
||||
Assert.AreEqual(true, JToken.DeepEquals(new JValue(2), t));
|
||||
|
||||
o["PropertyNameValue"] = null;
|
||||
Assert.AreEqual(1, o.Children().Count());
|
||||
|
||||
Assert.AreEqual(true, o.TryGetValue("PropertyNameValue", out t));
|
||||
Assert.AreEqual(true, JToken.DeepEquals(new JValue((object)null), t));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Remove()
|
||||
{
|
||||
JObject o = new JObject();
|
||||
o.Add("PropertyNameValue", new JValue(1));
|
||||
Assert.AreEqual(1, o.Children().Count());
|
||||
|
||||
Assert.AreEqual(false, o.Remove("sdf"));
|
||||
Assert.AreEqual(false, o.Remove(null));
|
||||
Assert.AreEqual(true, o.Remove("PropertyNameValue"));
|
||||
|
||||
Assert.AreEqual(0, o.Children().Count());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void GenericCollectionRemove()
|
||||
{
|
||||
JValue v = new JValue(1);
|
||||
JObject o = new JObject();
|
||||
o.Add("PropertyNameValue", v);
|
||||
Assert.AreEqual(1, o.Children().Count());
|
||||
|
||||
Assert.AreEqual(false, ((ICollection<KeyValuePair<string, JToken>>)o).Remove(new KeyValuePair<string, JToken>("PropertyNameValue1", new JValue(1))));
|
||||
Assert.AreEqual(false, ((ICollection<KeyValuePair<string, JToken>>)o).Remove(new KeyValuePair<string, JToken>("PropertyNameValue", new JValue(2))));
|
||||
Assert.AreEqual(false, ((ICollection<KeyValuePair<string, JToken>>)o).Remove(new KeyValuePair<string, JToken>("PropertyNameValue", new JValue(1))));
|
||||
Assert.AreEqual(true, ((ICollection<KeyValuePair<string, JToken>>)o).Remove(new KeyValuePair<string, JToken>("PropertyNameValue", v)));
|
||||
|
||||
Assert.AreEqual(0, o.Children().Count());
|
||||
}
|
||||
|
||||
[Test]
|
||||
[ExpectedException(typeof(Exception), ExpectedMessage = "Can not add property PropertyNameValue to Newtonsoft.Json.Linq.JObject. Property with the same name already exists on object.")]
|
||||
public void DuplicatePropertyNameShouldThrow()
|
||||
{
|
||||
JObject o = new JObject();
|
||||
o.Add("PropertyNameValue", null);
|
||||
o.Add("PropertyNameValue", null);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void GenericDictionaryAdd()
|
||||
{
|
||||
JObject o = new JObject();
|
||||
|
||||
o.Add("PropertyNameValue", new JValue(1));
|
||||
Assert.AreEqual(1, (int)o["PropertyNameValue"]);
|
||||
|
||||
o.Add("PropertyNameValue1", null);
|
||||
Assert.AreEqual(null, ((JValue)o["PropertyNameValue1"]).Value);
|
||||
|
||||
Assert.AreEqual(2, o.Children().Count());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void GenericCollectionAdd()
|
||||
{
|
||||
JObject o = new JObject();
|
||||
((ICollection<KeyValuePair<string,JToken>>)o).Add(new KeyValuePair<string,JToken>("PropertyNameValue", new JValue(1)));
|
||||
|
||||
Assert.AreEqual(1, (int)o["PropertyNameValue"]);
|
||||
Assert.AreEqual(1, o.Children().Count());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void GenericCollectionClear()
|
||||
{
|
||||
JObject o = new JObject();
|
||||
o.Add("PropertyNameValue", new JValue(1));
|
||||
Assert.AreEqual(1, o.Children().Count());
|
||||
|
||||
JProperty p = (JProperty)o.Children().ElementAt(0);
|
||||
|
||||
((ICollection<KeyValuePair<string, JToken>>)o).Clear();
|
||||
Assert.AreEqual(0, o.Children().Count());
|
||||
|
||||
Assert.AreEqual(null, p.Parent);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void GenericCollectionContains()
|
||||
{
|
||||
JValue v = new JValue(1);
|
||||
JObject o = new JObject();
|
||||
o.Add("PropertyNameValue", v);
|
||||
Assert.AreEqual(1, o.Children().Count());
|
||||
|
||||
bool contains = ((ICollection<KeyValuePair<string, JToken>>)o).Contains(new KeyValuePair<string, JToken>("PropertyNameValue", new JValue(1)));
|
||||
Assert.AreEqual(false, contains);
|
||||
|
||||
contains = ((ICollection<KeyValuePair<string, JToken>>)o).Contains(new KeyValuePair<string, JToken>("PropertyNameValue", v));
|
||||
Assert.AreEqual(true, contains);
|
||||
|
||||
contains = ((ICollection<KeyValuePair<string, JToken>>)o).Contains(new KeyValuePair<string, JToken>("PropertyNameValue", new JValue(2)));
|
||||
Assert.AreEqual(false, contains);
|
||||
|
||||
contains = ((ICollection<KeyValuePair<string, JToken>>)o).Contains(new KeyValuePair<string, JToken>("PropertyNameValue1", new JValue(1)));
|
||||
Assert.AreEqual(false, contains);
|
||||
|
||||
contains = ((ICollection<KeyValuePair<string, JToken>>)o).Contains(default(KeyValuePair<string, JToken>));
|
||||
Assert.AreEqual(false, contains);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void GenericDictionaryContains()
|
||||
{
|
||||
JObject o = new JObject();
|
||||
o.Add("PropertyNameValue", new JValue(1));
|
||||
Assert.AreEqual(1, o.Children().Count());
|
||||
|
||||
bool contains = ((IDictionary<string, JToken>)o).ContainsKey("PropertyNameValue");
|
||||
Assert.AreEqual(true, contains);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void GenericCollectionCopyTo()
|
||||
{
|
||||
JObject o = new JObject();
|
||||
o.Add("PropertyNameValue", new JValue(1));
|
||||
o.Add("PropertyNameValue2", new JValue(2));
|
||||
o.Add("PropertyNameValue3", new JValue(3));
|
||||
Assert.AreEqual(3, o.Children().Count());
|
||||
|
||||
KeyValuePair<string, JToken>[] a = new KeyValuePair<string,JToken>[5];
|
||||
|
||||
((ICollection<KeyValuePair<string, JToken>>)o).CopyTo(a, 1);
|
||||
|
||||
Assert.AreEqual(default(KeyValuePair<string,JToken>), a[0]);
|
||||
|
||||
Assert.AreEqual("PropertyNameValue", a[1].Key);
|
||||
Assert.AreEqual(1, (int)a[1].Value);
|
||||
|
||||
Assert.AreEqual("PropertyNameValue2", a[2].Key);
|
||||
Assert.AreEqual(2, (int)a[2].Value);
|
||||
|
||||
Assert.AreEqual("PropertyNameValue3", a[3].Key);
|
||||
Assert.AreEqual(3, (int)a[3].Value);
|
||||
|
||||
Assert.AreEqual(default(KeyValuePair<string, JToken>), a[4]);
|
||||
|
||||
}
|
||||
|
||||
[Test]
|
||||
[ExpectedException(typeof(ArgumentNullException), ExpectedMessage = @"Value cannot be null.
|
||||
Parameter name: array")]
|
||||
public void GenericCollectionCopyToNullArrayShouldThrow()
|
||||
{
|
||||
JObject o = new JObject();
|
||||
((ICollection<KeyValuePair<string, JToken>>)o).CopyTo(null, 0);
|
||||
}
|
||||
|
||||
[Test]
|
||||
[ExpectedException(typeof(ArgumentOutOfRangeException), ExpectedMessage = @"arrayIndex is less than 0.
|
||||
Parameter name: arrayIndex")]
|
||||
public void GenericCollectionCopyToNegativeArrayIndexShouldThrow()
|
||||
{
|
||||
JObject o = new JObject();
|
||||
((ICollection<KeyValuePair<string, JToken>>)o).CopyTo(new KeyValuePair<string, JToken>[1], -1);
|
||||
}
|
||||
|
||||
[Test]
|
||||
[ExpectedException(typeof(ArgumentException), ExpectedMessage = @"arrayIndex is equal to or greater than the length of array.")]
|
||||
public void GenericCollectionCopyToArrayIndexEqualGreaterToArrayLengthShouldThrow()
|
||||
{
|
||||
JObject o = new JObject();
|
||||
((ICollection<KeyValuePair<string, JToken>>)o).CopyTo(new KeyValuePair<string, JToken>[1], 1);
|
||||
}
|
||||
|
||||
[Test]
|
||||
[ExpectedException(typeof(ArgumentException), ExpectedMessage = @"The number of elements in the source JObject is greater than the available space from arrayIndex to the end of the destination array.")]
|
||||
public void GenericCollectionCopyToInsufficientArrayCapacity()
|
||||
{
|
||||
JObject o = new JObject();
|
||||
o.Add("PropertyNameValue", new JValue(1));
|
||||
o.Add("PropertyNameValue2", new JValue(2));
|
||||
o.Add("PropertyNameValue3", new JValue(3));
|
||||
|
||||
((ICollection<KeyValuePair<string, JToken>>)o).CopyTo(new KeyValuePair<string, JToken>[3], 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,60 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using NUnit.Framework;
|
||||
using Newtonsoft.Json.Linq;
|
||||
|
||||
namespace Newtonsoft.Json.Tests.Linq
|
||||
{
|
||||
public class JTokenEqualityComparerTests : TestFixtureBase
|
||||
{
|
||||
[Test]
|
||||
public void JValueDictionary()
|
||||
{
|
||||
Dictionary<JToken, int> dic = new Dictionary<JToken, int>(JToken.EqualityComparer);
|
||||
JValue v11 = new JValue(1);
|
||||
JValue v12 = new JValue(1);
|
||||
|
||||
dic[v11] = 1;
|
||||
dic[v12] += 1;
|
||||
Assert.AreEqual(2, dic[v11]);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void JArrayDictionary()
|
||||
{
|
||||
Dictionary<JToken, int> dic = new Dictionary<JToken, int>(JToken.EqualityComparer);
|
||||
JArray v11 = new JArray();
|
||||
JArray v12 = new JArray();
|
||||
|
||||
dic[v11] = 1;
|
||||
dic[v12] += 1;
|
||||
Assert.AreEqual(2, dic[v11]);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void JObjectDictionary()
|
||||
{
|
||||
Dictionary<JToken, int> dic = new Dictionary<JToken, int>(JToken.EqualityComparer);
|
||||
JObject v11 = new JObject() { { "Test", new JValue(1) }, { "Test1", new JValue(1) } };
|
||||
JObject v12 = new JObject() { { "Test", new JValue(1) }, { "Test1", new JValue(1) } };
|
||||
|
||||
dic[v11] = 1;
|
||||
dic[v12] += 1;
|
||||
Assert.AreEqual(2, dic[v11]);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void JConstructorDictionary()
|
||||
{
|
||||
Dictionary<JToken, int> dic = new Dictionary<JToken, int>(JToken.EqualityComparer);
|
||||
JConstructor v11 = new JConstructor("ConstructorValue");
|
||||
JConstructor v12 = new JConstructor("ConstructorValue");
|
||||
|
||||
dic[v11] = 1;
|
||||
dic[v12] += 1;
|
||||
Assert.AreEqual(2, dic[v11]);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -419,7 +419,7 @@ namespace Newtonsoft.Json.Tests.Linq
|
||||
}
|
||||
|
||||
[Test]
|
||||
[ExpectedException(typeof(Exception), ExpectedMessage = "Can not add Newtonsoft.Json.Linq.JProperty to Newtonsoft.Json.Linq.JArray")]
|
||||
[ExpectedException(typeof(Exception), ExpectedMessage = "Can not add Newtonsoft.Json.Linq.JProperty to Newtonsoft.Json.Linq.JArray.")]
|
||||
public void AddPropertyToArray()
|
||||
{
|
||||
JArray a = new JArray();
|
||||
@@ -427,7 +427,7 @@ namespace Newtonsoft.Json.Tests.Linq
|
||||
}
|
||||
|
||||
[Test]
|
||||
[ExpectedException(typeof(Exception), ExpectedMessage = "Can not add Newtonsoft.Json.Linq.JValue to Newtonsoft.Json.Linq.JObject")]
|
||||
[ExpectedException(typeof(Exception), ExpectedMessage = "Can not add Newtonsoft.Json.Linq.JValue to Newtonsoft.Json.Linq.JObject.")]
|
||||
public void AddValueToObject()
|
||||
{
|
||||
JObject o = new JObject();
|
||||
|
||||
@@ -536,7 +536,7 @@ keyword such as type of business.""
|
||||
[ExpectedException(typeof(ArgumentException), ExpectedMessage = @"Accessed JConstructor values with invalid key value: ""purple"". Argument position index expected.")]
|
||||
public void JConstructorStringIndex()
|
||||
{
|
||||
JConstructor c = new JConstructor();
|
||||
JConstructor c = new JConstructor("ConstructorValue");
|
||||
Assert.AreEqual(null, c["purple"]);
|
||||
}
|
||||
|
||||
|
||||
@@ -78,6 +78,9 @@
|
||||
<Compile Include="JsonTextWriterTest.cs" />
|
||||
<Compile Include="JsonTokenReaderTest.cs" />
|
||||
<Compile Include="JsonTokenWriterTest.cs" />
|
||||
<Compile Include="Linq\JArrayTests.cs" />
|
||||
<Compile Include="Linq\JObjectTests.cs" />
|
||||
<Compile Include="Linq\JTokenEqualityComparerTests.cs" />
|
||||
<Compile Include="Linq\JTokenTests.cs" />
|
||||
<Compile Include="Linq\JValueTests.cs" />
|
||||
<Compile Include="Linq\LinqToJsonTest.cs" />
|
||||
|
||||
Reference in New Issue
Block a user