Imported Upstream version 3.6.0

Former-commit-id: da6be194a6b1221998fc28233f2503bd61dd9d14
This commit is contained in:
Jo Shields
2014-08-13 10:39:27 +01:00
commit a575963da9
50588 changed files with 8155799 additions and 0 deletions

View File

@@ -0,0 +1,75 @@
#region License
// Copyright (c) 2007 James Newton-King
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
#endregion
#if !PocketPC && !SILVERLIGHT && !NETFX_CORE
using NUnit.Framework;
using System.Web.UI;
using Newtonsoft.Json.Linq;
namespace Newtonsoft.Json.Tests.Linq.ComponentModel
{
[TestFixture]
public class BindingTests : TestFixtureBase
{
[Test]
public void DataBinderEval()
{
JObject o = new JObject(
new JProperty("First", "String!"),
new JProperty("Second", 12345.6789m),
new JProperty("Third", new JArray(
1,
2,
3,
4,
5,
new JObject(
new JProperty("Fourth", "String!"),
new JProperty("Fifth", new JObject(
new JProperty("Sixth", "String!")))))));
object value;
value = (string)DataBinder.Eval(o, "First.Value");
Assert.AreEqual(value, (string)o["First"]);
value = DataBinder.Eval(o, "Second.Value");
Assert.AreEqual(value, (decimal)o["Second"]);
value = DataBinder.Eval(o, "Third");
Assert.AreEqual(value, o["Third"]);
value = DataBinder.Eval(o, "Third[0].Value");
Assert.AreEqual((int)value, (int)o["Third"][0]);
value = DataBinder.Eval(o, "Third[5].Fourth.Value");
Assert.AreEqual(value, (string)o["Third"][5]["Fourth"]);
value = DataBinder.Eval(o, "Third[5].Fifth.Sixth.Value");
Assert.AreEqual(value, (string)o["Third"][5]["Fifth"]["Sixth"]);
}
}
}
#endif

View File

@@ -0,0 +1,87 @@
#region License
// Copyright (c) 2007 James Newton-King
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
#endregion
#if !SILVERLIGHT && !NETFX_CORE
using NUnit.Framework;
using Newtonsoft.Json.Linq;
namespace Newtonsoft.Json.Tests.Linq.ComponentModel
{
[TestFixture]
public class JPropertyDescriptorTests : TestFixtureBase
{
[Test]
public void GetValue()
{
JObject o = JObject.Parse("{prop1:'12345!',prop2:[1,'two','III']}");
JPropertyDescriptor prop1 = new JPropertyDescriptor("prop1", typeof(string));
JPropertyDescriptor prop2 = new JPropertyDescriptor("prop2", typeof(JArray));
Assert.AreEqual("12345!", ((JValue) prop1.GetValue(o)).Value);
Assert.AreEqual(o["prop2"], prop2.GetValue(o));
}
[Test]
public void SetValue()
{
JObject o = JObject.Parse("{prop1:'12345!'}");
JPropertyDescriptor propertyDescriptor1 = new JPropertyDescriptor("prop1", typeof(string));
propertyDescriptor1.SetValue(o, "54321!");
Assert.AreEqual("54321!", (string)o["prop1"]);
}
[Test]
public void ResetValue()
{
JObject o = JObject.Parse("{prop1:'12345!'}");
JPropertyDescriptor propertyDescriptor1 = new JPropertyDescriptor("prop1", typeof(string));
propertyDescriptor1.ResetValue(o);
Assert.AreEqual("12345!", (string)o["prop1"]);
}
[Test]
public void IsReadOnly()
{
JPropertyDescriptor propertyDescriptor1 = new JPropertyDescriptor("prop1", typeof(string));
Assert.AreEqual(false, propertyDescriptor1.IsReadOnly);
}
[Test]
public void PropertyType()
{
JPropertyDescriptor propertyDescriptor1 = new JPropertyDescriptor("prop1", typeof(string));
Assert.AreEqual(typeof(string), propertyDescriptor1.PropertyType);
}
}
}
#endif

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,82 @@
using System;
using System.Collections.Generic;
using Newtonsoft.Json.Linq;
#if !NETFX_CORE
using NUnit.Framework;
#else
using Microsoft.VisualStudio.TestTools.UnitTesting;
using TestFixture = Microsoft.VisualStudio.TestTools.UnitTesting.TestClassAttribute;
using Test = Microsoft.VisualStudio.TestTools.UnitTesting.TestMethodAttribute;
#endif
using System.IO;
#if NET20
using Newtonsoft.Json.Utilities.LinqBridge;
#else
using System.Linq;
#endif
namespace Newtonsoft.Json.Tests.Linq
{
[TestFixture]
public class JConstructorTests : TestFixtureBase
{
[Test]
public void Load()
{
JsonReader reader = new JsonTextReader(new StringReader("new Date(123)"));
reader.Read();
JConstructor constructor = JConstructor.Load(reader);
Assert.AreEqual("Date", constructor.Name);
Assert.IsTrue(JToken.DeepEquals(new JValue(123), constructor.Values().ElementAt(0)));
}
[Test]
public void CreateWithMultiValue()
{
JConstructor constructor = new JConstructor("Test", new List<int> { 1, 2, 3 });
Assert.AreEqual("Test", constructor.Name);
Assert.AreEqual(3, constructor.Children().Count());
Assert.AreEqual(1, (int)constructor.Children().ElementAt(0));
Assert.AreEqual(2, (int)constructor.Children().ElementAt(1));
Assert.AreEqual(3, (int)constructor.Children().ElementAt(2));
}
[Test]
public void Iterate()
{
JConstructor c = new JConstructor("MrConstructor", 1, 2, 3, 4, 5);
int i = 1;
foreach (JToken token in c)
{
Assert.AreEqual(i, (int)token);
i++;
}
}
[Test]
public void SetValueWithInvalidIndex()
{
ExceptionAssert.Throws<ArgumentException>(@"Set JConstructor values with invalid key value: ""badvalue"". Argument position index expected.",
() =>
{
JConstructor c = new JConstructor();
c["badvalue"] = new JValue(3);
});
}
[Test]
public void SetValue()
{
object key = 0;
JConstructor c = new JConstructor();
c.Name = "con";
c.Add(null);
c[key] = new JValue(3);
Assert.AreEqual(3, (int)c[key]);
}
}
}

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,396 @@
using System;
using System.Collections.Generic;
#if !NETFX_CORE
using NUnit.Framework;
#else
using Microsoft.VisualStudio.TestTools.UnitTesting;
using TestFixture = Microsoft.VisualStudio.TestTools.UnitTesting.TestClassAttribute;
using Test = Microsoft.VisualStudio.TestTools.UnitTesting.TestMethodAttribute;
#endif
using Newtonsoft.Json.Linq;
#if NET20
using Newtonsoft.Json.Utilities.LinqBridge;
#else
using System.Linq;
#endif
namespace Newtonsoft.Json.Tests.Linq
{
[TestFixture]
public class JPathTests : TestFixtureBase
{
[Test]
public void SingleProperty()
{
JPath path = new JPath("Blah");
Assert.AreEqual(1, path.Parts.Count);
Assert.AreEqual("Blah", path.Parts[0]);
}
[Test]
public void TwoProperties()
{
JPath path = new JPath("Blah.Two");
Assert.AreEqual(2, path.Parts.Count);
Assert.AreEqual("Blah", path.Parts[0]);
Assert.AreEqual("Two", path.Parts[1]);
}
[Test]
public void SinglePropertyAndIndexer()
{
JPath path = new JPath("Blah[0]");
Assert.AreEqual(2, path.Parts.Count);
Assert.AreEqual("Blah", path.Parts[0]);
Assert.AreEqual(0, path.Parts[1]);
}
[Test]
public void MultiplePropertiesAndIndexers()
{
JPath path = new JPath("Blah[0].Two.Three[1].Four");
Assert.AreEqual(6, path.Parts.Count);
Assert.AreEqual("Blah", path.Parts[0]);
Assert.AreEqual(0, path.Parts[1]);
Assert.AreEqual("Two", path.Parts[2]);
Assert.AreEqual("Three", path.Parts[3]);
Assert.AreEqual(1, path.Parts[4]);
Assert.AreEqual("Four", path.Parts[5]);
}
[Test]
[ExpectedException(typeof(Exception)
#if !NETFX_CORE
, ExpectedMessage = @"Unexpected character while parsing path indexer: ["
#endif
)]
public void BadCharactersInIndexer()
{
new JPath("Blah[[0]].Two.Three[1].Four");
}
[Test]
[ExpectedException(typeof(Exception)
#if !NETFX_CORE
, ExpectedMessage = @"Path ended with open indexer. Expected ]"
#endif
)]
public void UnclosedIndexer()
{
new JPath("Blah[0");
}
[Test]
public void AdditionalDots()
{
JPath path = new JPath(".Blah..[0]..Two.Three....[1].Four.");
Assert.AreEqual(6, path.Parts.Count);
Assert.AreEqual("Blah", path.Parts[0]);
Assert.AreEqual(0, path.Parts[1]);
Assert.AreEqual("Two", path.Parts[2]);
Assert.AreEqual("Three", path.Parts[3]);
Assert.AreEqual(1, path.Parts[4]);
Assert.AreEqual("Four", path.Parts[5]);
}
[Test]
public void IndexerOnly()
{
JPath path = new JPath("[111119990]");
Assert.AreEqual(1, path.Parts.Count);
Assert.AreEqual(111119990, path.Parts[0]);
}
[Test]
[ExpectedException(typeof(Exception)
#if !NETFX_CORE
, ExpectedMessage = "Empty path indexer."
#endif
)]
public void EmptyIndexer()
{
new JPath("[]");
}
[Test]
[ExpectedException(typeof(Exception)
#if !NETFX_CORE
, ExpectedMessage = "Unexpected character while parsing path: ]"
#endif
)]
public void IndexerCloseInProperty()
{
new JPath("]");
}
[Test]
public void AdjacentIndexers()
{
JPath path = new JPath("[1][0][0][" + int.MaxValue + "]");
Assert.AreEqual(4, path.Parts.Count);
Assert.AreEqual(1, path.Parts[0]);
Assert.AreEqual(0, path.Parts[1]);
Assert.AreEqual(0, path.Parts[2]);
Assert.AreEqual(int.MaxValue, path.Parts[3]);
}
[Test]
[ExpectedException(typeof(Exception)
#if !NETFX_CORE
, ExpectedMessage = "Unexpected character following indexer: B"
#endif
)]
public void MissingDotAfterIndexer()
{
new JPath("[1]Blah");
}
[Test]
public void EvaluateSingleProperty()
{
JObject o = new JObject(
new JProperty("Blah", 1));
JToken t = o.SelectToken("Blah");
Assert.IsNotNull(t);
Assert.AreEqual(JTokenType.Integer, t.Type);
Assert.AreEqual(1, (int)t);
}
[Test]
public void EvaluateMissingProperty()
{
JObject o = new JObject(
new JProperty("Blah", 1));
JToken t = o.SelectToken("Missing[1]");
Assert.IsNull(t);
}
[Test]
public void EvaluateIndexerOnObject()
{
JObject o = new JObject(
new JProperty("Blah", 1));
JToken t = o.SelectToken("[1]");
Assert.IsNull(t);
}
[Test]
[ExpectedException(typeof(Exception)
#if !NETFX_CORE
, ExpectedMessage = @"Index 1 not valid on JObject."
#endif
)]
public void EvaluateIndexerOnObjectWithError()
{
JObject o = new JObject(
new JProperty("Blah", 1));
o.SelectToken("[1]", true);
}
[Test]
public void EvaluatePropertyOnArray()
{
JArray a = new JArray(1, 2, 3, 4, 5);
JToken t = a.SelectToken("BlahBlah");
Assert.IsNull(t);
}
[Test]
[ExpectedException(typeof(Exception)
#if !NETFX_CORE
, ExpectedMessage = @"Property 'BlahBlah' not valid on JArray."
#endif
)]
public void EvaluatePropertyOnArrayWithError()
{
JArray a = new JArray(1, 2, 3, 4, 5);
a.SelectToken("BlahBlah", true);
}
[Test]
[ExpectedException(typeof(IndexOutOfRangeException)
#if !NETFX_CORE
, ExpectedMessage = @"Index 1 outside the bounds of JConstructor."
#endif
)]
public void EvaluateConstructorOutOfBoundsIndxerWithError()
{
JConstructor c = new JConstructor("Blah");
c.SelectToken("[1]", true);
}
[Test]
[ExpectedException(typeof(Exception)
#if !NETFX_CORE
, ExpectedMessage = "Property 'Missing' does not exist on JObject."
#endif
)]
public void EvaluateMissingPropertyWithError()
{
JObject o = new JObject(
new JProperty("Blah", 1));
o.SelectToken("Missing", true);
}
[Test]
public void EvaluateOutOfBoundsIndxer()
{
JArray a = new JArray(1, 2, 3, 4, 5);
JToken t = a.SelectToken("[1000].Ha");
Assert.IsNull(t);
}
[Test]
[ExpectedException(typeof(IndexOutOfRangeException)
#if !NETFX_CORE
, ExpectedMessage = "Index 1000 outside the bounds of JArray."
#endif
)]
public void EvaluateArrayOutOfBoundsIndxerWithError()
{
JArray a = new JArray(1, 2, 3, 4, 5);
a.SelectToken("[1000].Ha", true);
}
[Test]
public void EvaluateArray()
{
JArray a = new JArray(1, 2, 3, 4);
JToken t = a.SelectToken("[1]");
Assert.IsNotNull(t);
Assert.AreEqual(JTokenType.Integer, t.Type);
Assert.AreEqual(2, (int)t);
}
[Test]
public void EvaluateSinglePropertyReturningArray()
{
JObject o = new JObject(
new JProperty("Blah", new [] { 1, 2, 3 }));
JToken t = o.SelectToken("Blah");
Assert.IsNotNull(t);
Assert.AreEqual(JTokenType.Array, t.Type);
t = o.SelectToken("Blah[2]");
Assert.AreEqual(JTokenType.Integer, t.Type);
Assert.AreEqual(3, (int)t);
}
[Test]
public void EvaluateLastSingleCharacterProperty()
{
JObject o2 = JObject.Parse("{'People':[{'N':'Jeff'}]}");
string a2 = (string)o2.SelectToken("People[0].N");
Assert.AreEqual("Jeff", a2);
}
[Test]
public void PathWithConstructor()
{
JArray a = JArray.Parse(@"[
{
""Property1"": [
1,
[
[
[]
]
]
]
},
{
""Property2"": new Constructor1(
null,
[
1
]
)
}
]");
JValue v = (JValue)a.SelectToken("[1].Property2[1][0]");
Assert.AreEqual(1L, v.Value);
}
[Test]
public void Example()
{
JObject o = JObject.Parse(@"{
""Stores"": [
""Lambton Quay"",
""Willis Street""
],
""Manufacturers"": [
{
""Name"": ""Acme Co"",
""Products"": [
{
""Name"": ""Anvil"",
""Price"": 50
}
]
},
{
""Name"": ""Contoso"",
""Products"": [
{
""Name"": ""Elbow Grease"",
""Price"": 99.95
},
{
""Name"": ""Headlight Fluid"",
""Price"": 4
}
]
}
]
}");
string name = (string)o.SelectToken("Manufacturers[0].Name");
// Acme Co
decimal productPrice = (decimal)o.SelectToken("Manufacturers[0].Products[0].Price");
// 50
string productName = (string)o.SelectToken("Manufacturers[1].Products[0].Name");
// Elbow Grease
Assert.AreEqual("Acme Co", name);
Assert.AreEqual(50m, productPrice);
Assert.AreEqual("Elbow Grease", productName);
IList<string> storeNames = o.SelectToken("Stores").Select(s => (string)s).ToList();
// Lambton Quay
// Willis Street
IList<string> firstProductNames = o["Manufacturers"].Select(m => (string)m.SelectToken("Products[1].Name")).ToList();
// null
// Headlight Fluid
decimal totalPrice = o["Manufacturers"].Sum(m => (decimal)m.SelectToken("Products[0].Price"));
// 149.95
Assert.AreEqual(2, storeNames.Count);
Assert.AreEqual("Lambton Quay", storeNames[0]);
Assert.AreEqual("Willis Street", storeNames[1]);
Assert.AreEqual(2, firstProductNames.Count);
Assert.AreEqual(null, firstProductNames[0]);
Assert.AreEqual("Headlight Fluid", firstProductNames[1]);
Assert.AreEqual(149.95m, totalPrice);
}
}
}

View File

@@ -0,0 +1,160 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using Newtonsoft.Json.Linq;
#if !NETFX_CORE
using NUnit.Framework;
#else
using Microsoft.VisualStudio.TestTools.UnitTesting;
using TestFixture = Microsoft.VisualStudio.TestTools.UnitTesting.TestClassAttribute;
using Test = Microsoft.VisualStudio.TestTools.UnitTesting.TestMethodAttribute;
#endif
using System.IO;
namespace Newtonsoft.Json.Tests.Linq
{
[TestFixture]
public class JPropertyTests : TestFixtureBase
{
[Test]
public void NullValue()
{
JProperty p = new JProperty("TestProperty", null);
Assert.IsNotNull(p.Value);
Assert.AreEqual(JTokenType.Null, p.Value.Type);
Assert.AreEqual(p, p.Value.Parent);
p.Value = null;
Assert.IsNotNull(p.Value);
Assert.AreEqual(JTokenType.Null, p.Value.Type);
Assert.AreEqual(p, p.Value.Parent);
}
#if !SILVERLIGHT && !NETFX_CORE
[Test]
public void ListChanged()
{
JProperty p = new JProperty("TestProperty", null);
IBindingList l = p;
ListChangedType? listChangedType = null;
int? index = null;
l.ListChanged += (sender, args) =>
{
listChangedType = args.ListChangedType;
index = args.NewIndex;
};
p.Value = 1;
Assert.AreEqual(ListChangedType.ItemChanged, listChangedType.Value);
Assert.AreEqual(0, index.Value);
}
#endif
[Test]
public void IListCount()
{
JProperty p = new JProperty("TestProperty", null);
IList l = p;
Assert.AreEqual(1, l.Count);
}
[Test]
[ExpectedException(typeof(Exception)
#if !NETFX_CORE
, ExpectedMessage = "Cannot add or remove items from Newtonsoft.Json.Linq.JProperty."
#endif
)]
public void IListClear()
{
JProperty p = new JProperty("TestProperty", null);
IList l = p;
l.Clear();
}
[Test]
[ExpectedException(typeof(Exception)
#if !NETFX_CORE
, ExpectedMessage = "Newtonsoft.Json.Linq.JProperty cannot have multiple values."
#endif
)]
public void IListAdd()
{
JProperty p = new JProperty("TestProperty", null);
IList l = p;
l.Add(null);
}
[Test]
[ExpectedException(typeof(Exception)
#if !NETFX_CORE
, ExpectedMessage = "Cannot add or remove items from Newtonsoft.Json.Linq.JProperty."
#endif
)]
public void IListRemove()
{
JProperty p = new JProperty("TestProperty", null);
IList l = p;
l.Remove(p.Value);
}
[Test]
public void Load()
{
JsonReader reader = new JsonTextReader(new StringReader("{'propertyname':['value1']}"));
reader.Read();
Assert.AreEqual(JsonToken.StartObject, reader.TokenType);
reader.Read();
JProperty property = JProperty.Load(reader);
Assert.AreEqual("propertyname", property.Name);
Assert.IsTrue(JToken.DeepEquals(JArray.Parse("['value1']"), property.Value));
Assert.AreEqual(JsonToken.EndObject, reader.TokenType);
reader = new JsonTextReader(new StringReader("{'propertyname':null}"));
reader.Read();
Assert.AreEqual(JsonToken.StartObject, reader.TokenType);
reader.Read();
property = JProperty.Load(reader);
Assert.AreEqual("propertyname", property.Name);
Assert.IsTrue(JToken.DeepEquals(new JValue(null, JTokenType.Null), property.Value));
Assert.AreEqual(JsonToken.EndObject, reader.TokenType);
}
[Test]
public void MultiContentConstructor()
{
JProperty p = new JProperty("error", new List<string> { "one", "two" });
JArray a = (JArray) p.Value;
Assert.AreEqual(a.Count, 2);
Assert.AreEqual("one", (string)a[0]);
Assert.AreEqual("two", (string)a[1]);
}
[Test]
[ExpectedException(typeof(Exception)
#if !NETFX_CORE
, ExpectedMessage = "Newtonsoft.Json.Linq.JProperty cannot have multiple values."
#endif
)]
public void IListGenericAdd()
{
IList<JToken> t = new JProperty("error", new List<string> { "one", "two" });
t.Add(1);
t.Add(2);
}
}
}

View File

@@ -0,0 +1,35 @@
#if !NETFX_CORE
using NUnit.Framework;
#else
using Microsoft.VisualStudio.TestTools.UnitTesting;
using TestFixture = Microsoft.VisualStudio.TestTools.UnitTesting.TestClassAttribute;
using Test = Microsoft.VisualStudio.TestTools.UnitTesting.TestMethodAttribute;
#endif
using Newtonsoft.Json.Linq;
namespace Newtonsoft.Json.Tests.Linq
{
[TestFixture]
public class JRawTests : TestFixtureBase
{
[Test]
public void RawEquals()
{
JRaw r1 = new JRaw("raw1");
JRaw r2 = new JRaw("raw1");
JRaw r3 = new JRaw("raw2");
Assert.IsTrue(JToken.DeepEquals(r1, r2));
Assert.IsFalse(JToken.DeepEquals(r1, r3));
}
[Test]
public void RawClone()
{
JRaw r1 = new JRaw("raw1");
JToken r2 = r1.CloneToken();
CustomAssert.IsInstanceOfType(typeof(JRaw), r2);
}
}
}

View File

@@ -0,0 +1,64 @@
using System.Collections.Generic;
#if !NETFX_CORE
using NUnit.Framework;
#else
using Microsoft.VisualStudio.TestTools.UnitTesting;
using TestFixture = Microsoft.VisualStudio.TestTools.UnitTesting.TestClassAttribute;
using Test = Microsoft.VisualStudio.TestTools.UnitTesting.TestMethodAttribute;
#endif
using Newtonsoft.Json.Linq;
namespace Newtonsoft.Json.Tests.Linq
{
[TestFixture]
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]);
}
}
}

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,178 @@
#region License
// Copyright (c) 2007 James Newton-King
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
#endregion
using System;
using System.Collections.Generic;
using System.Text;
#if !NETFX_CORE
using NUnit.Framework;
#else
using Microsoft.VisualStudio.TestTools.UnitTesting;
using TestFixture = Microsoft.VisualStudio.TestTools.UnitTesting.TestClassAttribute;
using Test = Microsoft.VisualStudio.TestTools.UnitTesting.TestMethodAttribute;
#endif
using Newtonsoft.Json;
using System.IO;
using Newtonsoft.Json.Linq;
#if NET20
using Newtonsoft.Json.Utilities.LinqBridge;
#else
using System.Linq;
#endif
namespace Newtonsoft.Json.Tests.Linq
{
[TestFixture]
public class JTokenWriterTest : TestFixtureBase
{
[Test]
public void ValueFormatting()
{
byte[] data = Encoding.UTF8.GetBytes("Hello world.");
JToken root;
using (JTokenWriter jsonWriter = new JTokenWriter())
{
jsonWriter.WriteStartArray();
jsonWriter.WriteValue('@');
jsonWriter.WriteValue("\r\n\t\f\b?{\\r\\n\"\'");
jsonWriter.WriteValue(true);
jsonWriter.WriteValue(10);
jsonWriter.WriteValue(10.99);
jsonWriter.WriteValue(0.99);
jsonWriter.WriteValue(0.000000000000000001d);
jsonWriter.WriteValue(0.000000000000000001m);
jsonWriter.WriteValue((string)null);
jsonWriter.WriteValue("This is a string.");
jsonWriter.WriteNull();
jsonWriter.WriteUndefined();
jsonWriter.WriteValue(data);
jsonWriter.WriteEndArray();
root = jsonWriter.Token;
}
CustomAssert.IsInstanceOfType(typeof(JArray), root);
Assert.AreEqual(13, root.Children().Count());
Assert.AreEqual("@", (string)root[0]);
Assert.AreEqual("\r\n\t\f\b?{\\r\\n\"\'", (string)root[1]);
Assert.AreEqual(true, (bool)root[2]);
Assert.AreEqual(10, (int)root[3]);
Assert.AreEqual(10.99, (double)root[4]);
Assert.AreEqual(0.99, (double)root[5]);
Assert.AreEqual(0.000000000000000001d, (double)root[6]);
Assert.AreEqual(0.000000000000000001m, (decimal)root[7]);
Assert.AreEqual(string.Empty, (string)root[8]);
Assert.AreEqual("This is a string.", (string)root[9]);
Assert.AreEqual(null, ((JValue)root[10]).Value);
Assert.AreEqual(null, ((JValue)root[11]).Value);
Assert.AreEqual(data, (byte[])root[12]);
}
[Test]
public void State()
{
using (JsonWriter jsonWriter = new JTokenWriter())
{
Assert.AreEqual(WriteState.Start, jsonWriter.WriteState);
jsonWriter.WriteStartObject();
Assert.AreEqual(WriteState.Object, jsonWriter.WriteState);
jsonWriter.WritePropertyName("CPU");
Assert.AreEqual(WriteState.Property, jsonWriter.WriteState);
jsonWriter.WriteValue("Intel");
Assert.AreEqual(WriteState.Object, jsonWriter.WriteState);
jsonWriter.WritePropertyName("Drives");
Assert.AreEqual(WriteState.Property, jsonWriter.WriteState);
jsonWriter.WriteStartArray();
Assert.AreEqual(WriteState.Array, jsonWriter.WriteState);
jsonWriter.WriteValue("DVD read/writer");
Assert.AreEqual(WriteState.Array, jsonWriter.WriteState);
jsonWriter.WriteValue(new byte[0]);
Assert.AreEqual(WriteState.Array, jsonWriter.WriteState);
jsonWriter.WriteEnd();
Assert.AreEqual(WriteState.Object, jsonWriter.WriteState);
jsonWriter.WriteEndObject();
Assert.AreEqual(WriteState.Start, jsonWriter.WriteState);
}
}
[Test]
public void WriteComment()
{
JTokenWriter writer = new JTokenWriter();
writer.WriteStartArray();
writer.WriteComment("fail");
writer.WriteEndArray();
Assert.AreEqual(@"[
/*fail*/]", writer.Token.ToString());
}
[Test]
public void WriteRaw()
{
JTokenWriter writer = new JTokenWriter();
writer.WriteStartArray();
writer.WriteRaw("fail");
writer.WriteRaw("fail");
writer.WriteEndArray();
// this is a bug. write raw shouldn't be autocompleting like this
// hard to fix without introducing Raw and RawValue token types
// meh
Assert.AreEqual(@"[
fail,
fail
]", writer.Token.ToString());
}
[Test]
public void WriteRawValue()
{
JTokenWriter writer = new JTokenWriter();
writer.WriteStartArray();
writer.WriteRawValue("fail");
writer.WriteRawValue("fail");
writer.WriteEndArray();
Assert.AreEqual(@"[
fail,
fail
]", writer.Token.ToString());
}
}
}

View File

@@ -0,0 +1,307 @@
#region License
// Copyright (c) 2007 James Newton-King
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
#endregion
using System;
using System.Collections.Generic;
using System.Text;
#if !NETFX_CORE
using NUnit.Framework;
#else
using Microsoft.VisualStudio.TestTools.UnitTesting;
using TestFixture = Microsoft.VisualStudio.TestTools.UnitTesting.TestClassAttribute;
using Test = Microsoft.VisualStudio.TestTools.UnitTesting.TestMethodAttribute;
#endif
using Newtonsoft.Json.Linq;
using System.Globalization;
#if NET20
using Newtonsoft.Json.Utilities.LinqBridge;
#else
using System.Linq;
#endif
namespace Newtonsoft.Json.Tests.Linq
{
[TestFixture]
public class JValueTests : TestFixtureBase
{
[Test]
public void ChangeValue()
{
JValue v = new JValue(true);
Assert.AreEqual(true, v.Value);
Assert.AreEqual(JTokenType.Boolean, v.Type);
v.Value = "Pie";
Assert.AreEqual("Pie", v.Value);
Assert.AreEqual(JTokenType.String, v.Type);
v.Value = null;
Assert.AreEqual(null, v.Value);
Assert.AreEqual(JTokenType.Null, v.Type);
v.Value = (int?)null;
Assert.AreEqual(null, v.Value);
Assert.AreEqual(JTokenType.Null, v.Type);
v.Value = "Pie";
Assert.AreEqual("Pie", v.Value);
Assert.AreEqual(JTokenType.String, v.Type);
#if !NETFX_CORE
v.Value = DBNull.Value;
Assert.AreEqual(DBNull.Value, v.Value);
Assert.AreEqual(JTokenType.Null, v.Type);
#endif
byte[] data = new byte[0];
v.Value = data;
Assert.AreEqual(data, v.Value);
Assert.AreEqual(JTokenType.Bytes, v.Type);
v.Value = StringComparison.OrdinalIgnoreCase;
Assert.AreEqual(StringComparison.OrdinalIgnoreCase, v.Value);
Assert.AreEqual(JTokenType.Integer, v.Type);
v.Value = new Uri("http://json.codeplex.com/");
Assert.AreEqual(new Uri("http://json.codeplex.com/"), v.Value);
Assert.AreEqual(JTokenType.Uri, v.Type);
v.Value = TimeSpan.FromDays(1);
Assert.AreEqual(TimeSpan.FromDays(1), v.Value);
Assert.AreEqual(JTokenType.TimeSpan, v.Type);
Guid g = Guid.NewGuid();
v.Value = g;
Assert.AreEqual(g, v.Value);
Assert.AreEqual(JTokenType.Guid, v.Type);
}
[Test]
public void CreateComment()
{
JValue commentValue = JValue.CreateComment(null);
Assert.AreEqual(null, commentValue.Value);
Assert.AreEqual(JTokenType.Comment, commentValue.Type);
commentValue.Value = "Comment";
Assert.AreEqual("Comment", commentValue.Value);
Assert.AreEqual(JTokenType.Comment, commentValue.Type);
}
[Test]
public void CreateString()
{
JValue stringValue = JValue.CreateString(null);
Assert.AreEqual(null, stringValue.Value);
Assert.AreEqual(JTokenType.String, stringValue.Type);
}
[Test]
public void ToString()
{
JValue v;
v = new JValue(true);
Assert.AreEqual("True", v.ToString());
v = new JValue(Encoding.UTF8.GetBytes("Blah"));
Assert.AreEqual("System.Byte[]", v.ToString(null, CultureInfo.InvariantCulture));
v = new JValue("I am a string!");
Assert.AreEqual("I am a string!", v.ToString());
v = new JValue(null, JTokenType.Null);
Assert.AreEqual("", v.ToString());
v = new JValue(null, JTokenType.Null);
Assert.AreEqual("", v.ToString(null, CultureInfo.InvariantCulture));
v = new JValue(new DateTime(2000, 12, 12, 20, 59, 59, DateTimeKind.Utc), JTokenType.Date);
Assert.AreEqual("12/12/2000 20:59:59", v.ToString(null, CultureInfo.InvariantCulture));
v = new JValue(new Uri("http://json.codeplex.com/"));
Assert.AreEqual("http://json.codeplex.com/", v.ToString(null, CultureInfo.InvariantCulture));
v = new JValue(TimeSpan.FromDays(1));
Assert.AreEqual("1.00:00:00", v.ToString(null, CultureInfo.InvariantCulture));
v = new JValue(new Guid("B282ADE7-C520-496C-A448-4084F6803DE5"));
Assert.AreEqual("b282ade7-c520-496c-a448-4084f6803de5", v.ToString(null, CultureInfo.InvariantCulture));
}
[Test]
public void Last()
{
ExceptionAssert.Throws<InvalidOperationException>("Cannot access child value on Newtonsoft.Json.Linq.JValue.",
() =>
{
JValue v = new JValue(true);
JToken last = v.Last;
});
}
[Test]
public void Children()
{
JValue v = new JValue(true);
var c = v.Children();
Assert.AreEqual(JEnumerable<JToken>.Empty, c);
}
[Test]
public void First()
{
ExceptionAssert.Throws<InvalidOperationException>("Cannot access child value on Newtonsoft.Json.Linq.JValue.",
() =>
{
JValue v = new JValue(true);
JToken first = v.First;
});
}
[Test]
public void Item()
{
ExceptionAssert.Throws<InvalidOperationException>("Cannot access child value on Newtonsoft.Json.Linq.JValue.",
() =>
{
JValue v = new JValue(true);
JToken first = v[0];
});
}
[Test]
public void Values()
{
ExceptionAssert.Throws<InvalidOperationException>("Cannot access child value on Newtonsoft.Json.Linq.JValue.",
() =>
{
JValue v = new JValue(true);
v.Values<int>();
});
}
[Test]
public void RemoveParentNull()
{
ExceptionAssert.Throws<InvalidOperationException>("The parent is missing.",
() =>
{
JValue v = new JValue(true);
v.Remove();
});
}
[Test]
public void Root()
{
JValue v = new JValue(true);
Assert.AreEqual(v, v.Root);
}
[Test]
public void Previous()
{
JValue v = new JValue(true);
Assert.IsNull(v.Previous);
}
[Test]
public void Next()
{
JValue v = new JValue(true);
Assert.IsNull(v.Next);
}
[Test]
public void DeepEquals()
{
Assert.IsTrue(JToken.DeepEquals(new JValue(5L), new JValue(5)));
Assert.IsFalse(JToken.DeepEquals(new JValue(5M), new JValue(5)));
Assert.IsTrue(JToken.DeepEquals(new JValue((ulong)long.MaxValue), new JValue(long.MaxValue)));
}
[Test]
public void HasValues()
{
Assert.IsFalse((new JValue(5L)).HasValues);
}
[Test]
public void SetValue()
{
ExceptionAssert.Throws<InvalidOperationException>("Cannot set child value on Newtonsoft.Json.Linq.JValue.",
() =>
{
JToken t = new JValue(5L);
t[0] = new JValue(3);
});
}
[Test]
public void CastNullValueToNonNullable()
{
ExceptionAssert.Throws<ArgumentException>("Can not convert Null to Int32.",
() =>
{
JValue v = new JValue((object)null);
int i = (int)v;
});
}
[Test]
public void ConvertValueToCompatibleType()
{
IComparable c = (new JValue(1).Value<IComparable>());
Assert.AreEqual(1L, c);
}
[Test]
public void ConvertValueToFormattableType()
{
IFormattable f = (new JValue(1).Value<IFormattable>());
Assert.AreEqual(1L, f);
Assert.AreEqual("01", f.ToString("00", CultureInfo.InvariantCulture));
}
[Test]
public void Ordering()
{
JObject o = new JObject(
new JProperty("Integer", new JValue(1)),
new JProperty("Float", new JValue(1.2d)),
new JProperty("Decimal", new JValue(1.1m))
);
IList<object> orderedValues = o.Values().Cast<JValue>().OrderBy(v => v).Select(v => v.Value).ToList();
Assert.AreEqual(1L, orderedValues[0]);
Assert.AreEqual(1.1m, orderedValues[1]);
Assert.AreEqual(1.2d, orderedValues[2]);
}
}
}

File diff suppressed because it is too large Load Diff