From ac3725b5a30234156e244947d1ee7b2a2eeff25b Mon Sep 17 00:00:00 2001 From: Greg Dennis Date: Fri, 23 Sep 2016 15:57:17 +1200 Subject: [PATCH] Cleaned up tests. Fixed errors caused by previous changes. --- .../Path/FilterExpressionTest.cs | 4 +- .../Path/IndexExpressionParseTest.cs | 80 +++---------------- Manatee.Json.Tests/Path/ParsingFails.cs | 17 +--- Manatee.Json.Tests/Path/ParsingTest.cs | 51 +----------- Manatee.Json.Tests/Path/ToStringTest.cs | 2 +- Manatee.Json/Path/Parsing/ObjectParser.cs | 2 +- Manatee.Json/Path/Parsing/SearchParser.cs | 2 +- 7 files changed, 23 insertions(+), 135 deletions(-) diff --git a/Manatee.Json.Tests/Path/FilterExpressionTest.cs b/Manatee.Json.Tests/Path/FilterExpressionTest.cs index 90306818..2a489ad8 100644 --- a/Manatee.Json.Tests/Path/FilterExpressionTest.cs +++ b/Manatee.Json.Tests/Path/FilterExpressionTest.cs @@ -6,13 +6,13 @@ namespace Manatee.Json.Tests.Path [TestClass] public class FilterExpressionParseTest { - private void Run(JsonPath expected, string text) + private static void Run(JsonPath expected, string text) { var actual = JsonPath.Parse(text); Assert.AreEqual(expected, actual); } - private void CompareEval(JsonPath expected, string text) + private static void CompareEval(JsonPath expected, string text) { var data = new JsonArray { diff --git a/Manatee.Json.Tests/Path/IndexExpressionParseTest.cs b/Manatee.Json.Tests/Path/IndexExpressionParseTest.cs index 2ad3e11f..f879151d 100644 --- a/Manatee.Json.Tests/Path/IndexExpressionParseTest.cs +++ b/Manatee.Json.Tests/Path/IndexExpressionParseTest.cs @@ -6,7 +6,7 @@ namespace Manatee.Json.Tests.Path [TestClass] public class IndexExpressionParseTest { - private void Run(JsonPath expected, string text) + private static void Run(JsonPath expected, string text) { var actual = JsonPath.Parse(text); Assert.AreEqual(expected, actual); @@ -15,82 +15,42 @@ private void Run(JsonPath expected, string text) [TestMethod] public void Length() { - var text = "$[(@.length)]"; - var expected = JsonPathWith.Array(jv => jv.Length()); - - var actual = JsonPath.Parse(text); - - Assert.AreEqual(expected, actual); + Run(JsonPathWith.Array(jv => jv.Length()), "$[(@.length)]"); } [TestMethod] public void Length_Root() { - var text = "$[($.length)]"; - var expected = JsonPathWith.Array(jv => JsonPathRoot.Length()); - - var actual = JsonPath.Parse(text); - - Assert.AreEqual(expected, actual); + Run(JsonPathWith.Array(jv => JsonPathRoot.Length()), "$[($.length)]"); } [TestMethod] public void ExtendedLength() { - var text = "$[(@.name.length)]"; - var expected = JsonPathWith.Array(jv => jv.Name("name").Length()); - - var actual = JsonPath.Parse(text); - - Assert.AreEqual(expected, actual); + Run(JsonPathWith.Array(jv => jv.Name("name").Length()), "$[(@.name.length)]"); } [TestMethod] public void Addition() { - var text = "$[(@.length+1)]"; - var expected = JsonPathWith.Array(jv => jv.Length() + 1); - - var actual = JsonPath.Parse(text); - - Assert.AreEqual(expected, actual); + Run(JsonPathWith.Array(jv => jv.Length() + 1), "$[(@.length+1)]"); } [TestMethod] public void Subtraction() { - var text = "$[(@.length-1)]"; - var expected = JsonPathWith.Array(jv => jv.Length() - 1); - - var actual = JsonPath.Parse(text); - - Assert.AreEqual(expected, actual); + Run(JsonPathWith.Array(jv => jv.Length() - 1), "$[(@.length-1)]"); } [TestMethod] public void Multiplication() { - var text = "$[(@.length*1)]"; - var expected = JsonPathWith.Array(jv => jv.Length()*1); - - var actual = JsonPath.Parse(text); - - Assert.AreEqual(expected, actual); + Run(JsonPathWith.Array(jv => jv.Length() * 1), "$[(@.length*1)]"); } [TestMethod] public void Division() { - var text = "$[(@.length/1)]"; - var expected = JsonPathWith.Array(jv => jv.Length()/1); - - var actual = JsonPath.Parse(text); - - Assert.AreEqual(expected, actual); + Run(JsonPathWith.Array(jv => jv.Length() / 1), "$[(@.length/1)]"); } [TestMethod] public void Modulus() { - var text = "$[(@.length%1)]"; - var expected = JsonPathWith.Array(jv => jv.Length()%1); - - var actual = JsonPath.Parse(text); - - Assert.AreEqual(expected, actual); + Run(JsonPathWith.Array(jv => jv.Length() % 1), "$[(@.length%1)]"); } [TestMethod] [Ignore] @@ -99,32 +59,18 @@ public void Modulus() // Also not really sure JS supports it as an exponentiation operator, either. public void Exponent() { - var text = "$[(@.length^1)]"; - var expected = JsonPathWith.Array(jv => jv.Length() ^ 1); - - var actual = JsonPath.Parse(text); - - Assert.AreEqual(expected, actual); + Run(JsonPathWith.Array(jv => jv.Length() ^ 1), "$[(@.length^1)]"); } [TestMethod] public void Add3() { - var text = "$[(3+@.length+3)]"; - var expected = JsonPathWith.Array(jv => 3 + jv.Length() + 3); - - var actual = JsonPath.Parse(text); - - Assert.AreEqual(expected, actual); + Run(JsonPathWith.Array(jv => 3 + jv.Length() + 3), "$[(3+@.length+3)]"); } [TestMethod] public void WhyOnGodsGreenEarthWouldAnyoneDoThis() { - var text = "$[(4+@.length*($.name.length-2)+5)]"; - var expected = JsonPathWith.Array(jv => 4 + jv.Length()*(JsonPathRoot.Name("name").Length() - 2) + 5); - - var actual = JsonPath.Parse(text); - - Assert.AreEqual(expected, actual); + Run(JsonPathWith.Array(jv => 4 + jv.Length()*(JsonPathRoot.Name("name").Length() - 2) + 5), + "$[(4+@.length*($.name.length-2)+5)]"); } [TestMethod] public void JustAnInteger() diff --git a/Manatee.Json.Tests/Path/ParsingFails.cs b/Manatee.Json.Tests/Path/ParsingFails.cs index 12893e27..b89f0085 100644 --- a/Manatee.Json.Tests/Path/ParsingFails.cs +++ b/Manatee.Json.Tests/Path/ParsingFails.cs @@ -7,7 +7,7 @@ namespace Manatee.Json.Tests.Path [TestClass] public class ParsingFails { - private void Run(string text) + private static void Run(string text) { try { @@ -38,21 +38,18 @@ public void EmptyFilterExpressionArray() { Run("$[?()]"); } - [TestMethod] [ExpectedException(typeof(JsonPathSyntaxException))] public void EmptyObject() { Run("$."); } - [TestMethod] [ExpectedException(typeof(JsonPathSyntaxException))] public void InvalidPropertyName() { Run("$.tes*t"); } - [TestMethod] [ExpectedException(typeof(JsonPathSyntaxException))] public void StringIndex() @@ -60,84 +57,72 @@ public void StringIndex() // TODO: This may actually be the key to parsing bracket notation Run("$[\"test\"]"); } - [TestMethod] [ExpectedException(typeof(JsonPathSyntaxException))] public void BoolIndex() { Run("$[false]"); } - [TestMethod] [ExpectedException(typeof(JsonPathSyntaxException))] public void MissingCloseBracket() { Run("$[1.test"); } - [TestMethod] [ExpectedException(typeof(JsonPathSyntaxException))] public void BadSliceFormat() { Run("$[1-5]"); } - [TestMethod] [ExpectedException(typeof(JsonPathSyntaxException))] public void MissingCloseParenthesisOnIndexExpression() { Run("$[(1]"); } - [TestMethod] [ExpectedException(typeof(JsonPathSyntaxException))] public void MissingCloseBracketOnIndexExpression() { Run("$[(1)"); } - [TestMethod] [ExpectedException(typeof(JsonPathSyntaxException))] public void MissingCloseBracketOnIndexExpression2() { Run("$[(1).test"); } - [TestMethod] [ExpectedException(typeof(JsonPathSyntaxException))] public void MissingCloseParenthesisOnFilterExpression() { Run("$[?(@.name == 4].test"); } - [TestMethod] [ExpectedException(typeof(JsonPathSyntaxException))] public void MissingCloseBracketOnFilterExpression() { Run("$[?(@.name == 4)"); } - [TestMethod] [ExpectedException(typeof(JsonPathSyntaxException))] public void MissingCloseBracketOnFilterExpression2() { Run("$[?(@.name == 4).test"); } - [TestMethod] [ExpectedException(typeof(JsonPathSyntaxException))] public void MissingDot() { Run("$name"); } - [TestMethod] [ExpectedException(typeof(JsonPathSyntaxException))] public void DotBeforeArray() { Run("$.[0]"); } - [TestMethod] [ExpectedException(typeof(JsonPathSyntaxException))] public void TooManyDots() diff --git a/Manatee.Json.Tests/Path/ParsingTest.cs b/Manatee.Json.Tests/Path/ParsingTest.cs index 4c01b66e..b6257c68 100644 --- a/Manatee.Json.Tests/Path/ParsingTest.cs +++ b/Manatee.Json.Tests/Path/ParsingTest.cs @@ -6,7 +6,7 @@ namespace Manatee.Json.Tests.Path [TestClass] public class ParsingTest { - private void Run(JsonPath expected, string text) + private static void Run(JsonPath expected, string text) { var actual = JsonPath.Parse(text); @@ -18,40 +18,21 @@ public void SingleNamedObject() { Run(JsonPathWith.Name("name"), "$.name"); } - [TestMethod] public void SingleQuotedNamedObject() { - var text = "$.'quoted name'"; - var expected = JsonPathWith.Name("quoted name"); - - var actual = JsonPath.Parse(text); - - Assert.AreEqual(expected, actual); + Run(JsonPathWith.Name("quoted name"), "$.'quoted name'"); } - [TestMethod] public void DoubleQuotedNamedObject() { - var text = "$.\"quoted name\""; - var expected = JsonPathWith.Name("quoted name"); - - var actual = JsonPath.Parse(text); - - Assert.AreEqual(expected, actual); + Run(JsonPathWith.Name("quoted name"), "$.\"quoted name\""); } - [TestMethod] public void SingleWildcardObject() { - var text = "$.*"; - var expected = JsonPathWith.Wildcard(); - - var actual = JsonPath.Parse(text); - - Assert.AreEqual(expected, actual); + Run(JsonPathWith.Wildcard(), "$.*"); } - [TestMethod] public void NamedObjectWithWildcardObject() { @@ -62,7 +43,6 @@ public void NamedObjectWithWildcardObject() Assert.AreEqual(expected, actual); } - [TestMethod] public void WildcardObjectWithNamedObject() { @@ -83,7 +63,6 @@ public void SingleNamedSearch() Assert.AreEqual(expected, actual); } - [TestMethod] public void SingleQuotedNamedSearch() { @@ -94,7 +73,6 @@ public void SingleQuotedNamedSearch() Assert.AreEqual(expected, actual); } - [TestMethod] public void DoubleQuotedNamedSearch() { @@ -105,7 +83,6 @@ public void DoubleQuotedNamedSearch() Assert.AreEqual(expected, actual); } - [TestMethod] public void SingleWildcardSearch() { @@ -116,7 +93,6 @@ public void SingleWildcardSearch() Assert.AreEqual(expected, actual); } - [TestMethod] public void NamedObjectWithWildcardSearch() { @@ -127,7 +103,6 @@ public void NamedObjectWithWildcardSearch() Assert.AreEqual(expected, actual); } - [TestMethod] public void WildcardObjectWithNamedSearch() { @@ -138,7 +113,6 @@ public void WildcardObjectWithNamedSearch() Assert.AreEqual(expected, actual); } - [TestMethod] public void SingleIndexedArray() { @@ -149,7 +123,6 @@ public void SingleIndexedArray() Assert.AreEqual(expected, actual); } - [TestMethod] public void SingleSlicedArray() { @@ -160,7 +133,6 @@ public void SingleSlicedArray() Assert.AreEqual(expected, actual); } - [TestMethod] public void SteppedSlicedArray() { @@ -171,7 +143,6 @@ public void SteppedSlicedArray() Assert.AreEqual(expected, actual); } - [TestMethod] public void IndexedSlicedArray() { @@ -182,7 +153,6 @@ public void IndexedSlicedArray() Assert.AreEqual(expected, actual); } - [TestMethod] public void SlicedIndexedArray() { @@ -193,7 +163,6 @@ public void SlicedIndexedArray() Assert.AreEqual(expected, actual); } - [TestMethod] public void MultiSlicedArray() { @@ -204,7 +173,6 @@ public void MultiSlicedArray() Assert.AreEqual(expected, actual); } - [TestMethod] public void MultiIndexedArray() { @@ -215,7 +183,6 @@ public void MultiIndexedArray() Assert.AreEqual(expected, actual); } - [TestMethod] public void WildcardArray() { @@ -226,7 +193,6 @@ public void WildcardArray() Assert.AreEqual(expected, actual); } - [TestMethod] public void SearchIndexedArray() { @@ -237,49 +203,41 @@ public void SearchIndexedArray() Assert.AreEqual(expected, actual); } - [TestMethod] public void ChainedNameIndexedArray() { Run(JsonPathWith.Name("name").Array(4), "$.name[4]"); } - [TestMethod] public void ChainedIndexedArrayName() { Run(JsonPathWith.Array(4).Name("name"), "$[4].name"); } - [TestMethod] public void ChainedNameName() { Run(JsonPathWith.Name("name").Name("test"), "$.name.test"); } - [TestMethod] public void ChainedIndexedArrayIndexedArray() { Run(JsonPathWith.Array(2).Array(4), "$[2][4]"); } - [TestMethod] public void MultipleConditionsAdd() { Run(JsonPathWith.Array(jv => jv.Length() == 3 && jv.ArrayIndex(1) == false), "$[?(@.length == 3 && @[1] == false)]"); } - [TestMethod] public void MultipleConditionsOr() { Run(JsonPathWith.Array(jv => jv.Length() == 3 || jv.ArrayIndex(1) == false), "$[?(@.length == 3 || @[1] == false)]"); } - [TestMethod] public void Group() { Run(JsonPathWith.Array(jv => (jv.Length()+1)*2 == 6), "$[?((@.length+1)*2 == 6)]"); } - [TestMethod] public void NotGroup() { @@ -287,7 +245,6 @@ public void NotGroup() // Don't simplify this. It's a parsing test. Run(JsonPathWith.Array(jv => !(jv.Length() == 3) && jv.ArrayIndex(1) == false), "$[?(!(@.length == 3) && @[1] == false)]"); } - [TestMethod] public void WeirdPropertyNameQuoted() { diff --git a/Manatee.Json.Tests/Path/ToStringTest.cs b/Manatee.Json.Tests/Path/ToStringTest.cs index 8cec1530..4c59e72e 100644 --- a/Manatee.Json.Tests/Path/ToStringTest.cs +++ b/Manatee.Json.Tests/Path/ToStringTest.cs @@ -6,7 +6,7 @@ namespace Manatee.Json.Tests.Path [TestClass] public class ToStringTest { - private void Run(string expected, JsonPath path) + private static void Run(string expected, JsonPath path) { var actual = path.ToString(); Assert.AreEqual(expected, actual); diff --git a/Manatee.Json/Path/Parsing/ObjectParser.cs b/Manatee.Json/Path/Parsing/ObjectParser.cs index ed69c8ab..6fdd3b41 100644 --- a/Manatee.Json/Path/Parsing/ObjectParser.cs +++ b/Manatee.Json/Path/Parsing/ObjectParser.cs @@ -28,7 +28,7 @@ internal class ObjectParser : IJsonPathParser { public bool Handles(string input) { - return input.Length > 2 && input[0] == '.' && (char.IsLetterOrDigit(input[1]) || input[1].In('_', '\'', '"')); + return input.Length >= 2 && input[0] == '.' && (char.IsLetterOrDigit(input[1]) || input[1].In('_', '\'', '"', '*')); } public string TryParse(string source, ref int index, ref JsonPath path) { diff --git a/Manatee.Json/Path/Parsing/SearchParser.cs b/Manatee.Json/Path/Parsing/SearchParser.cs index 7d715dcf..ff282186 100644 --- a/Manatee.Json/Path/Parsing/SearchParser.cs +++ b/Manatee.Json/Path/Parsing/SearchParser.cs @@ -28,7 +28,7 @@ internal class SearchParser : IJsonPathParser { public bool Handles(string input) { - return input.StartsWith("..") && input.Length > 3 && (char.IsLetterOrDigit(input[2]) || input[2].In('_', '\'', '"')); + return input.StartsWith("..") && input.Length >= 3 && (char.IsLetterOrDigit(input[2]) || input[2].In('_', '\'', '"', '*')); } public string TryParse(string source, ref int index, ref JsonPath path) {