-Added JContainer.DescendantsAndSelf
-Added CurrentToken to JTokenReader
This commit is contained in:
@@ -0,0 +1,250 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using Newtonsoft.Json.Linq;
|
||||
using NUnit.Framework;
|
||||
|
||||
namespace Newtonsoft.Json.Tests.Linq
|
||||
{
|
||||
[TestFixture]
|
||||
public class AnnotationsTests : TestFixtureBase
|
||||
{
|
||||
[Test]
|
||||
public void AddAnnotation()
|
||||
{
|
||||
JObject o = new JObject();
|
||||
o.AddAnnotation("A string!");
|
||||
|
||||
string s = o.Annotation<string>();
|
||||
Assert.AreEqual("A string!", s);
|
||||
|
||||
s = (string)o.Annotation(typeof(string));
|
||||
Assert.AreEqual("A string!", s);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void AddAnnotation_MultipleOfTheSameType()
|
||||
{
|
||||
JObject o = new JObject();
|
||||
o.AddAnnotation("A string!");
|
||||
o.AddAnnotation("Another string!");
|
||||
|
||||
string s = o.Annotation<string>();
|
||||
Assert.AreEqual("A string!", s);
|
||||
|
||||
s = (string)o.Annotation(typeof(string));
|
||||
Assert.AreEqual("A string!", s);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void AddAnnotation_MultipleOfDifferentTypes()
|
||||
{
|
||||
JObject o = new JObject();
|
||||
o.AddAnnotation("A string!");
|
||||
o.AddAnnotation(new Uri("http://www.google.com/"));
|
||||
|
||||
string s = o.Annotation<string>();
|
||||
Assert.AreEqual("A string!", s);
|
||||
|
||||
s = (string)o.Annotation(typeof(string));
|
||||
Assert.AreEqual("A string!", s);
|
||||
|
||||
Uri i = o.Annotation<Uri>();
|
||||
Assert.AreEqual(new Uri("http://www.google.com/"), i);
|
||||
|
||||
i = (Uri)o.Annotation(typeof(Uri));
|
||||
Assert.AreEqual(new Uri("http://www.google.com/"), i);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void GetAnnotation_NeverSet()
|
||||
{
|
||||
JObject o = new JObject();
|
||||
|
||||
string s = o.Annotation<string>();
|
||||
Assert.AreEqual(null, s);
|
||||
|
||||
s = (string)o.Annotation(typeof(string));
|
||||
Assert.AreEqual(null, s);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void GetAnnotations()
|
||||
{
|
||||
JObject o = new JObject();
|
||||
o.AddAnnotation("A string!");
|
||||
o.AddAnnotation("A string 2!");
|
||||
o.AddAnnotation("A string 3!");
|
||||
|
||||
IList<string> l = o.Annotations<string>().ToList();
|
||||
|
||||
Assert.AreEqual(3, l.Count);
|
||||
Assert.AreEqual("A string!", l[0]);
|
||||
Assert.AreEqual("A string 2!", l[1]);
|
||||
Assert.AreEqual("A string 3!", l[2]);
|
||||
|
||||
l = o.Annotations(typeof(string)).Cast<string>().ToList();
|
||||
|
||||
Assert.AreEqual(3, l.Count);
|
||||
Assert.AreEqual("A string!", l[0]);
|
||||
Assert.AreEqual("A string 2!", l[1]);
|
||||
Assert.AreEqual("A string 3!", l[2]);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void GetAnnotations_MultipleTypes()
|
||||
{
|
||||
JObject o = new JObject();
|
||||
o.AddAnnotation("A string!");
|
||||
o.AddAnnotation("A string 2!");
|
||||
o.AddAnnotation("A string 3!");
|
||||
o.AddAnnotation(new Uri("http://www.google.com/"));
|
||||
|
||||
IList<object> l = o.Annotations<object>().ToList();
|
||||
|
||||
Assert.AreEqual(4, l.Count);
|
||||
Assert.AreEqual("A string!", l[0]);
|
||||
Assert.AreEqual("A string 2!", l[1]);
|
||||
Assert.AreEqual("A string 3!", l[2]);
|
||||
Assert.AreEqual(new Uri("http://www.google.com/"), l[3]);
|
||||
|
||||
l = o.Annotations(typeof(object)).ToList();
|
||||
|
||||
Assert.AreEqual(4, l.Count);
|
||||
Assert.AreEqual("A string!", l[0]);
|
||||
Assert.AreEqual("A string 2!", l[1]);
|
||||
Assert.AreEqual("A string 3!", l[2]);
|
||||
Assert.AreEqual(new Uri("http://www.google.com/"), l[3]);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void RemoveAnnotation()
|
||||
{
|
||||
JObject o = new JObject();
|
||||
o.AddAnnotation("A string!");
|
||||
|
||||
o.RemoveAnnotations<string>();
|
||||
|
||||
string s = o.Annotation<string>();
|
||||
Assert.AreEqual(null, s);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void RemoveAnnotation_NonGeneric()
|
||||
{
|
||||
JObject o = new JObject();
|
||||
o.AddAnnotation("A string!");
|
||||
|
||||
o.RemoveAnnotations(typeof(string));
|
||||
|
||||
string s = o.Annotation<string>();
|
||||
Assert.AreEqual(null, s);
|
||||
|
||||
s = (string)o.Annotation(typeof(string));
|
||||
Assert.AreEqual(null, s);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void RemoveAnnotation_Multiple()
|
||||
{
|
||||
JObject o = new JObject();
|
||||
o.AddAnnotation("A string!");
|
||||
o.AddAnnotation("A string 2!");
|
||||
o.AddAnnotation("A string 3!");
|
||||
|
||||
o.RemoveAnnotations<string>();
|
||||
|
||||
string s = o.Annotation<string>();
|
||||
Assert.AreEqual(null, s);
|
||||
|
||||
o.AddAnnotation("A string 4!");
|
||||
|
||||
s = o.Annotation<string>();
|
||||
Assert.AreEqual("A string 4!", s);
|
||||
|
||||
Uri i = (Uri)o.Annotation(typeof(Uri));
|
||||
Assert.AreEqual(null, i);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void RemoveAnnotation_MultipleCalls()
|
||||
{
|
||||
JObject o = new JObject();
|
||||
o.AddAnnotation("A string!");
|
||||
o.AddAnnotation(new Uri("http://www.google.com/"));
|
||||
|
||||
o.RemoveAnnotations<string>();
|
||||
o.RemoveAnnotations<Uri>();
|
||||
|
||||
string s = o.Annotation<string>();
|
||||
Assert.AreEqual(null, s);
|
||||
|
||||
Uri i = o.Annotation<Uri>();
|
||||
Assert.AreEqual(null, i);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void RemoveAnnotation_Multiple_NonGeneric()
|
||||
{
|
||||
JObject o = new JObject();
|
||||
o.AddAnnotation("A string!");
|
||||
o.AddAnnotation("A string 2!");
|
||||
|
||||
o.RemoveAnnotations(typeof(string));
|
||||
|
||||
string s = o.Annotation<string>();
|
||||
Assert.AreEqual(null, s);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void RemoveAnnotation_MultipleCalls_NonGeneric()
|
||||
{
|
||||
JObject o = new JObject();
|
||||
o.AddAnnotation("A string!");
|
||||
o.AddAnnotation(new Uri("http://www.google.com/"));
|
||||
|
||||
o.RemoveAnnotations(typeof(string));
|
||||
o.RemoveAnnotations(typeof(Uri));
|
||||
|
||||
string s = o.Annotation<string>();
|
||||
Assert.AreEqual(null, s);
|
||||
|
||||
Uri i = o.Annotation<Uri>();
|
||||
Assert.AreEqual(null, i);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void RemoveAnnotation_MultipleWithDifferentTypes()
|
||||
{
|
||||
JObject o = new JObject();
|
||||
o.AddAnnotation("A string!");
|
||||
o.AddAnnotation(new Uri("http://www.google.com/"));
|
||||
|
||||
o.RemoveAnnotations<string>();
|
||||
|
||||
string s = o.Annotation<string>();
|
||||
Assert.AreEqual(null, s);
|
||||
|
||||
Uri i = o.Annotation<Uri>();
|
||||
Assert.AreEqual(new Uri("http://www.google.com/"), i);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void RemoveAnnotation_MultipleWithDifferentTypes_NonGeneric()
|
||||
{
|
||||
JObject o = new JObject();
|
||||
o.AddAnnotation("A string!");
|
||||
o.AddAnnotation(new Uri("http://www.google.com/"));
|
||||
|
||||
o.RemoveAnnotations(typeof(string));
|
||||
|
||||
string s = o.Annotation<string>();
|
||||
Assert.AreEqual(null, s);
|
||||
|
||||
Uri i = o.Annotation<Uri>();
|
||||
Assert.AreEqual(new Uri("http://www.google.com/"), i);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -208,8 +208,6 @@ namespace Newtonsoft.Json.Tests.Linq
|
||||
]
|
||||
}";
|
||||
|
||||
StringReader sr = new StringReader(input);
|
||||
|
||||
JObject o = JObject.Parse(input);
|
||||
|
||||
using (JTokenReader jsonReader = new JTokenReader(o))
|
||||
@@ -220,12 +218,14 @@ namespace Newtonsoft.Json.Tests.Linq
|
||||
Assert.AreEqual(0, lineInfo.LineNumber);
|
||||
Assert.AreEqual(0, lineInfo.LinePosition);
|
||||
Assert.AreEqual(false, lineInfo.HasLineInfo());
|
||||
Assert.AreEqual(null, jsonReader.CurrentToken);
|
||||
|
||||
jsonReader.Read();
|
||||
Assert.AreEqual(jsonReader.TokenType, JsonToken.StartObject);
|
||||
Assert.AreEqual(1, lineInfo.LineNumber);
|
||||
Assert.AreEqual(1, lineInfo.LinePosition);
|
||||
Assert.AreEqual(true, lineInfo.HasLineInfo());
|
||||
Assert.AreEqual(o, jsonReader.CurrentToken);
|
||||
|
||||
jsonReader.Read();
|
||||
Assert.AreEqual(jsonReader.TokenType, JsonToken.PropertyName);
|
||||
@@ -233,6 +233,7 @@ namespace Newtonsoft.Json.Tests.Linq
|
||||
Assert.AreEqual(2, lineInfo.LineNumber);
|
||||
Assert.AreEqual(7, lineInfo.LinePosition);
|
||||
Assert.AreEqual(true, lineInfo.HasLineInfo());
|
||||
Assert.AreEqual(o.Property("CPU"), jsonReader.CurrentToken);
|
||||
|
||||
jsonReader.Read();
|
||||
Assert.AreEqual(jsonReader.TokenType, JsonToken.String);
|
||||
@@ -240,6 +241,7 @@ namespace Newtonsoft.Json.Tests.Linq
|
||||
Assert.AreEqual(2, lineInfo.LineNumber);
|
||||
Assert.AreEqual(15, lineInfo.LinePosition);
|
||||
Assert.AreEqual(true, lineInfo.HasLineInfo());
|
||||
Assert.AreEqual(o.Property("CPU").Value, jsonReader.CurrentToken);
|
||||
|
||||
jsonReader.Read();
|
||||
Assert.AreEqual(jsonReader.TokenType, JsonToken.PropertyName);
|
||||
@@ -247,12 +249,14 @@ namespace Newtonsoft.Json.Tests.Linq
|
||||
Assert.AreEqual(3, lineInfo.LineNumber);
|
||||
Assert.AreEqual(10, lineInfo.LinePosition);
|
||||
Assert.AreEqual(true, lineInfo.HasLineInfo());
|
||||
Assert.AreEqual(o.Property("Drives"), jsonReader.CurrentToken);
|
||||
|
||||
jsonReader.Read();
|
||||
Assert.AreEqual(jsonReader.TokenType, JsonToken.StartArray);
|
||||
Assert.AreEqual(3, lineInfo.LineNumber);
|
||||
Assert.AreEqual(12, lineInfo.LinePosition);
|
||||
Assert.AreEqual(true, lineInfo.HasLineInfo());
|
||||
Assert.AreEqual(o.Property("Drives").Value, jsonReader.CurrentToken);
|
||||
|
||||
jsonReader.Read();
|
||||
Assert.AreEqual(jsonReader.TokenType, JsonToken.String);
|
||||
@@ -260,6 +264,7 @@ namespace Newtonsoft.Json.Tests.Linq
|
||||
Assert.AreEqual(4, lineInfo.LineNumber);
|
||||
Assert.AreEqual(22, lineInfo.LinePosition);
|
||||
Assert.AreEqual(true, lineInfo.HasLineInfo());
|
||||
Assert.AreEqual(o["Drives"][0], jsonReader.CurrentToken);
|
||||
|
||||
jsonReader.Read();
|
||||
Assert.AreEqual(jsonReader.TokenType, JsonToken.String);
|
||||
@@ -267,21 +272,29 @@ namespace Newtonsoft.Json.Tests.Linq
|
||||
Assert.AreEqual(5, lineInfo.LineNumber);
|
||||
Assert.AreEqual(30, lineInfo.LinePosition);
|
||||
Assert.AreEqual(true, lineInfo.HasLineInfo());
|
||||
Assert.AreEqual(o["Drives"][1], jsonReader.CurrentToken);
|
||||
|
||||
jsonReader.Read();
|
||||
Assert.AreEqual(jsonReader.TokenType, JsonToken.EndArray);
|
||||
Assert.AreEqual(3, lineInfo.LineNumber);
|
||||
Assert.AreEqual(12, lineInfo.LinePosition);
|
||||
Assert.AreEqual(true, lineInfo.HasLineInfo());
|
||||
Assert.AreEqual(o["Drives"], jsonReader.CurrentToken);
|
||||
|
||||
jsonReader.Read();
|
||||
Assert.AreEqual(jsonReader.TokenType, JsonToken.EndObject);
|
||||
Assert.AreEqual(1, lineInfo.LineNumber);
|
||||
Assert.AreEqual(1, lineInfo.LinePosition);
|
||||
Assert.AreEqual(true, lineInfo.HasLineInfo());
|
||||
Assert.AreEqual(o, jsonReader.CurrentToken);
|
||||
|
||||
jsonReader.Read();
|
||||
Assert.AreEqual(jsonReader.TokenType, JsonToken.None);
|
||||
Assert.AreEqual(null, jsonReader.CurrentToken);
|
||||
|
||||
jsonReader.Read();
|
||||
Assert.AreEqual(jsonReader.TokenType, JsonToken.None);
|
||||
Assert.AreEqual(null, jsonReader.CurrentToken);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -471,15 +484,18 @@ namespace Newtonsoft.Json.Tests.Linq
|
||||
|
||||
JObject o = JObject.Parse(json);
|
||||
|
||||
JsonReader reader = o.CreateReader();
|
||||
JTokenReader reader = (JTokenReader)o.CreateReader();
|
||||
|
||||
Assert.IsTrue(reader.Read());
|
||||
Assert.AreEqual(JsonToken.StartObject, reader.TokenType);
|
||||
Assert.AreEqual(o, reader.CurrentToken);
|
||||
|
||||
Assert.IsTrue(reader.Read());
|
||||
Assert.AreEqual(JsonToken.PropertyName, reader.TokenType);
|
||||
Assert.AreEqual(o.Property("Name"), reader.CurrentToken);
|
||||
|
||||
reader.ReadAsInt32();
|
||||
Assert.AreEqual(o["Name"], reader.CurrentToken);
|
||||
Assert.AreEqual(JsonToken.Integer, reader.TokenType);
|
||||
Assert.AreEqual(typeof(int), reader.ValueType);
|
||||
Assert.AreEqual(1, reader.Value);
|
||||
|
||||
@@ -25,6 +25,7 @@
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Xml.Linq;
|
||||
#if !(NET20 || NET35 || PORTABLE || ASPNETCORE50)
|
||||
using System.Numerics;
|
||||
#endif
|
||||
@@ -717,6 +718,35 @@ namespace Newtonsoft.Json.Tests.Linq
|
||||
Assert.AreEqual(3, (int)descendants[descendants.Count - 1]);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void DescendantsAndSelf()
|
||||
{
|
||||
XElement e = new XElement("name");
|
||||
e.Add(new XElement("child"));
|
||||
|
||||
var x = e.DescendantsAndSelf().ToList();
|
||||
Assert.AreEqual(2, x.Count);
|
||||
Assert.AreEqual(e, x[0]);
|
||||
Assert.AreEqual(e.Element("child"), x[1]);
|
||||
|
||||
JArray a =
|
||||
new JArray(
|
||||
5,
|
||||
new JArray(1),
|
||||
new JArray(1, 2),
|
||||
new JArray(1, 2, 3)
|
||||
);
|
||||
|
||||
List<JToken> descendantsAndSelf = a.DescendantsAndSelf().ToList();
|
||||
Assert.AreEqual(11, descendantsAndSelf.Count());
|
||||
Assert.AreEqual(a, descendantsAndSelf[0]);
|
||||
Assert.AreEqual(5, (int)descendantsAndSelf[1]);
|
||||
Assert.IsTrue(JToken.DeepEquals(new JArray(1, 2, 3), descendantsAndSelf[descendantsAndSelf.Count - 4]));
|
||||
Assert.AreEqual(1, (int)descendantsAndSelf[descendantsAndSelf.Count - 3]);
|
||||
Assert.AreEqual(2, (int)descendantsAndSelf[descendantsAndSelf.Count - 2]);
|
||||
Assert.AreEqual(3, (int)descendantsAndSelf[descendantsAndSelf.Count - 1]);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void CreateWriter()
|
||||
{
|
||||
|
||||
@@ -213,6 +213,7 @@
|
||||
<Compile Include="LinqToSql\LinqToSqlClassesSerializationTests.cs" />
|
||||
<Compile Include="LinqToSql\Person.cs" />
|
||||
<Compile Include="LinqToSql\Role.cs" />
|
||||
<Compile Include="Linq\AnnotationsTests.cs" />
|
||||
<Compile Include="Linq\ComponentModel\BindingTests.cs" />
|
||||
<Compile Include="Linq\ComponentModel\JPropertyDescriptorTests.cs" />
|
||||
<Compile Include="Linq\DynamicTests.cs" />
|
||||
|
||||
Reference in New Issue
Block a user