-Added JToken.AncestorsAndSelf
-Added Extensions.AncestorsAndSelf and Extensions.DescendantsAndSelf -Tests
This commit is contained in:
@@ -697,6 +697,85 @@ namespace Newtonsoft.Json.Tests.Linq
|
||||
Assert.AreEqual(a, ancestors[1]);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void AncestorsAndSelf()
|
||||
{
|
||||
JArray a =
|
||||
new JArray(
|
||||
5,
|
||||
new JArray(1),
|
||||
new JArray(1, 2),
|
||||
new JArray(1, 2, 3)
|
||||
);
|
||||
|
||||
JToken t = a[1][0];
|
||||
List<JToken> ancestors = t.AncestorsAndSelf().ToList();
|
||||
Assert.AreEqual(3, ancestors.Count());
|
||||
Assert.AreEqual(t, ancestors[0]);
|
||||
Assert.AreEqual(a[1], ancestors[1]);
|
||||
Assert.AreEqual(a, ancestors[2]);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void AncestorsAndSelf_Many()
|
||||
{
|
||||
JArray a =
|
||||
new JArray(
|
||||
5,
|
||||
new JArray(1),
|
||||
new JArray(1, 2),
|
||||
new JArray(1, 2, 3)
|
||||
);
|
||||
|
||||
JObject o = new JObject
|
||||
{
|
||||
{"prop1", "value1"}
|
||||
};
|
||||
|
||||
JToken t1 = a[1][0];
|
||||
JToken t2 = o["prop1"];
|
||||
|
||||
List<JToken> source = new List<JToken> {t1, t2};
|
||||
|
||||
List<JToken> ancestors = source.AncestorsAndSelf().ToList();
|
||||
Assert.AreEqual(6, ancestors.Count());
|
||||
Assert.AreEqual(t1, ancestors[0]);
|
||||
Assert.AreEqual(a[1], ancestors[1]);
|
||||
Assert.AreEqual(a, ancestors[2]);
|
||||
Assert.AreEqual(t2, ancestors[3]);
|
||||
Assert.AreEqual(o.Property("prop1"), ancestors[4]);
|
||||
Assert.AreEqual(o, ancestors[5]);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Ancestors_Many()
|
||||
{
|
||||
JArray a =
|
||||
new JArray(
|
||||
5,
|
||||
new JArray(1),
|
||||
new JArray(1, 2),
|
||||
new JArray(1, 2, 3)
|
||||
);
|
||||
|
||||
JObject o = new JObject
|
||||
{
|
||||
{"prop1", "value1"}
|
||||
};
|
||||
|
||||
JToken t1 = a[1][0];
|
||||
JToken t2 = o["prop1"];
|
||||
|
||||
List<JToken> source = new List<JToken> { t1, t2 };
|
||||
|
||||
List<JToken> ancestors = source.Ancestors().ToList();
|
||||
Assert.AreEqual(4, ancestors.Count());
|
||||
Assert.AreEqual(a[1], ancestors[0]);
|
||||
Assert.AreEqual(a, ancestors[1]);
|
||||
Assert.AreEqual(o.Property("prop1"), ancestors[2]);
|
||||
Assert.AreEqual(o, ancestors[3]);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Descendants()
|
||||
{
|
||||
@@ -717,6 +796,35 @@ namespace Newtonsoft.Json.Tests.Linq
|
||||
Assert.AreEqual(3, (int)descendants[descendants.Count - 1]);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Descendants_Many()
|
||||
{
|
||||
JArray a =
|
||||
new JArray(
|
||||
5,
|
||||
new JArray(1),
|
||||
new JArray(1, 2),
|
||||
new JArray(1, 2, 3)
|
||||
);
|
||||
|
||||
JObject o = new JObject
|
||||
{
|
||||
{"prop1", "value1"}
|
||||
};
|
||||
|
||||
List<JContainer> source = new List<JContainer> { a, o };
|
||||
|
||||
List<JToken> descendants = source.Descendants().ToList();
|
||||
Assert.AreEqual(12, descendants.Count());
|
||||
Assert.AreEqual(5, (int)descendants[0]);
|
||||
Assert.IsTrue(JToken.DeepEquals(new JArray(1, 2, 3), descendants[descendants.Count - 6]));
|
||||
Assert.AreEqual(1, (int)descendants[descendants.Count - 5]);
|
||||
Assert.AreEqual(2, (int)descendants[descendants.Count - 4]);
|
||||
Assert.AreEqual(3, (int)descendants[descendants.Count - 3]);
|
||||
Assert.AreEqual(o.Property("prop1"), descendants[descendants.Count - 2]);
|
||||
Assert.AreEqual(o["prop1"], descendants[descendants.Count - 1]);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void DescendantsAndSelf()
|
||||
{
|
||||
@@ -738,6 +846,37 @@ namespace Newtonsoft.Json.Tests.Linq
|
||||
Assert.AreEqual(3, (int)descendantsAndSelf[descendantsAndSelf.Count - 1]);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void DescendantsAndSelf_Many()
|
||||
{
|
||||
JArray a =
|
||||
new JArray(
|
||||
5,
|
||||
new JArray(1),
|
||||
new JArray(1, 2),
|
||||
new JArray(1, 2, 3)
|
||||
);
|
||||
|
||||
JObject o = new JObject
|
||||
{
|
||||
{"prop1", "value1"}
|
||||
};
|
||||
|
||||
List<JContainer> source = new List<JContainer> { a, o };
|
||||
|
||||
List<JToken> descendantsAndSelf = source.DescendantsAndSelf().ToList();
|
||||
Assert.AreEqual(14, 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 - 7]));
|
||||
Assert.AreEqual(1, (int)descendantsAndSelf[descendantsAndSelf.Count - 6]);
|
||||
Assert.AreEqual(2, (int)descendantsAndSelf[descendantsAndSelf.Count - 5]);
|
||||
Assert.AreEqual(3, (int)descendantsAndSelf[descendantsAndSelf.Count - 4]);
|
||||
Assert.AreEqual(o, descendantsAndSelf[descendantsAndSelf.Count - 3]);
|
||||
Assert.AreEqual(o.Property("prop1"), descendantsAndSelf[descendantsAndSelf.Count - 2]);
|
||||
Assert.AreEqual(o["prop1"], descendantsAndSelf[descendantsAndSelf.Count - 1]);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void CreateWriter()
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user