-Initial JsonPath

This commit is contained in:
James Newton-King
2013-11-15 00:20:20 +13:00
parent 12932404d4
commit d8ddf1b433
3 changed files with 484 additions and 101 deletions
+104 -31
View File
@@ -50,7 +50,23 @@ namespace Newtonsoft.Json.Tests.Linq
{
JPath path = new JPath("Blah");
Assert.AreEqual(1, path.Parts.Count);
Assert.AreEqual("Blah", path.Parts[0]);
Assert.AreEqual("Blah", ((FieldFilter)path.Parts[0]).Name);
}
[Test]
public void SinglePropertyWithRoot()
{
JPath path = new JPath("$.Blah");
Assert.AreEqual(1, path.Parts.Count);
Assert.AreEqual("Blah", ((FieldFilter)path.Parts[0]).Name);
}
[Test]
public void SingleScanWithRoot()
{
JPath path = new JPath("$..Blah");
Assert.AreEqual(1, path.Parts.Count);
Assert.AreEqual("Blah", ((ScanFilter)path.Parts[0]).Name);
}
[Test]
@@ -58,8 +74,17 @@ namespace Newtonsoft.Json.Tests.Linq
{
JPath path = new JPath("Blah.Two");
Assert.AreEqual(2, path.Parts.Count);
Assert.AreEqual("Blah", path.Parts[0]);
Assert.AreEqual("Two", path.Parts[1]);
Assert.AreEqual("Blah", ((FieldFilter)path.Parts[0]).Name);
Assert.AreEqual("Two", ((FieldFilter)path.Parts[1]).Name);
}
[Test]
public void OnePropertyOneScan()
{
JPath path = new JPath("Blah..Two");
Assert.AreEqual(2, path.Parts.Count);
Assert.AreEqual("Blah", ((FieldFilter)path.Parts[0]).Name);
Assert.AreEqual("Two", ((ScanFilter)path.Parts[1]).Name);
}
[Test]
@@ -67,21 +92,34 @@ namespace Newtonsoft.Json.Tests.Linq
{
JPath path = new JPath("Blah[0]");
Assert.AreEqual(2, path.Parts.Count);
Assert.AreEqual("Blah", path.Parts[0]);
Assert.AreEqual(0, path.Parts[1]);
Assert.AreEqual("Blah", ((FieldFilter)path.Parts[0]).Name);
Assert.AreEqual(0, ((ArrayIndexFilter)path.Parts[1]).Index);
}
[Test]
public void SinglePropertyAndFilter()
{
JPath path = new JPath("Blah[?(@.name=='hi')]");
Assert.AreEqual(2, path.Parts.Count);
Assert.AreEqual("Blah", ((FieldFilter)path.Parts[0]).Name);
List<object> expressions = ((QueryFilter)path.Parts[1]).Expression;
Assert.AreEqual(1, expressions.Count);
}
[Test]
public void MultiplePropertiesAndIndexers()
{
JPath path = new JPath("Blah[0].Two.Three[1].Four");
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]);
Assert.AreEqual(FilterType.Field, path.Parts[0].Type);
Assert.AreEqual("Blah", ((FieldFilter) path.Parts[0]).Name);
Assert.AreEqual(FilterType.ArrayIndex, path.Parts[1].Type);
Assert.AreEqual(0, ((ArrayIndexFilter) path.Parts[1]).Index);
Assert.AreEqual(FilterType.Scan, path.Parts[2].Type);
Assert.AreEqual("Two", ((ScanFilter)path.Parts[2]).Name);
Assert.AreEqual("Three", ((FieldFilter)path.Parts[3]).Name);
Assert.AreEqual(1, ((ArrayIndexFilter)path.Parts[4]).Index);
Assert.AreEqual("Four", ((FieldFilter)path.Parts[5]).Name);
}
[Test]
@@ -96,36 +134,46 @@ namespace Newtonsoft.Json.Tests.Linq
public void UnclosedIndexer()
{
ExceptionAssert.Throws<JsonException>(
@"Path ended with open indexer. Expected ]",
@"Path ended with open indexer.",
() => { 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 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]);
Assert.AreEqual(111119990, ((ArrayIndexFilter)path.Parts[0]).Index);
}
[Test]
public void MultipleIndexes()
{
JPath path = new JPath("[111119990,3]");
Assert.AreEqual(1, path.Parts.Count);
Assert.AreEqual(2, ((ArrayMultipleIndexFilter)path.Parts[0]).Indexes.Count);
Assert.AreEqual(111119990, ((ArrayMultipleIndexFilter)path.Parts[0]).Indexes[0]);
Assert.AreEqual(3, ((ArrayMultipleIndexFilter)path.Parts[0]).Indexes[1]);
}
[Test]
public void EmptyIndexer()
{
ExceptionAssert.Throws<JsonException>(
"Empty path indexer.",
"Array index expected.",
() => { new JPath("[]"); });
}
@@ -142,10 +190,10 @@ namespace Newtonsoft.Json.Tests.Linq
{
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]);
Assert.AreEqual(1, ((ArrayIndexFilter)path.Parts[0]).Index);
Assert.AreEqual(0, ((ArrayIndexFilter)path.Parts[1]).Index);
Assert.AreEqual(0, ((ArrayIndexFilter)path.Parts[2]).Index);
Assert.AreEqual(int.MaxValue, ((ArrayIndexFilter)path.Parts[3]).Index);
}
[Test]
@@ -168,6 +216,18 @@ namespace Newtonsoft.Json.Tests.Linq
Assert.AreEqual(1, (int)t);
}
[Test]
public void QuoteName()
{
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()
{
@@ -269,6 +329,19 @@ namespace Newtonsoft.Json.Tests.Linq
Assert.AreEqual(2, (int)t);
}
[Test]
public void EvaluateArrayMultipleIndexes()
{
JArray a = new JArray(1, 2, 3, 4);
IEnumerable<JToken> t = a.SelectTokens("[1,2,0]");
Assert.IsNotNull(t);
Assert.AreEqual(3, t.Count());
Assert.AreEqual(2, (int)t.ElementAt(0));
Assert.AreEqual(3, (int)t.ElementAt(1));
Assert.AreEqual(1, (int)t.ElementAt(2));
}
[Test]
public void EvaluateSinglePropertyReturningArray()
{