Skip to content
This repository has been archived by the owner on Feb 15, 2021. It is now read-only.

Commit

Permalink
Added some negative testing. Fixed some things as a result.
Browse files Browse the repository at this point in the history
  • Loading branch information
Greg Dennis committed Sep 23, 2016
1 parent 525724e commit 2ff9403
Show file tree
Hide file tree
Showing 15 changed files with 273 additions and 128 deletions.
1 change: 1 addition & 0 deletions Manatee.Json.Tests/Manatee.Json.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@
<Compile Include="Path\EvaluationTest.cs" />
<Compile Include="Path\FilterExpressionTest.cs" />
<Compile Include="Path\IndexExpressionParseTest.cs" />
<Compile Include="Path\ParsingFails.cs" />
<Compile Include="Path\ParsingTest.cs" />
<Compile Include="Path\TestSuite\JsonPathTestSuite.cs" />
<Compile Include="Path\TestSuite\PathTest.cs" />
Expand Down
66 changes: 1 addition & 65 deletions Manatee.Json.Tests/Path/FilterExpressionTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
namespace Manatee.Json.Tests.Path
{
[TestClass]
public class FilterExpressionTest
public class FilterExpressionParseTest
{
private void Run(JsonPath expected, string text)
{
Expand Down Expand Up @@ -73,70 +73,6 @@ public void ArrayIndexEqualsValue()
Run(JsonPathWith.Array(jv => jv.ArrayIndex(1) == 5), "$[?(@[1] == 5)]");
}
[TestMethod]
[ExpectedException(typeof(JsonPathSyntaxException))]
public void ArrayMultiIndex()
{
var text = "$[?(@[1,3] == 5)]";

try
{
var actual = JsonPath.Parse(text);
}
catch (JsonPathSyntaxException e)
{
Assert.IsTrue(e.Message.StartsWith("JSON Path expression indexers only support single constant values."));
throw;
}
}
[TestMethod]
[ExpectedException(typeof(JsonPathSyntaxException))]
public void ArraySliceEqualsValue()
{
var text = "$[?(@[1:3] == 5)]";

try
{
var actual = JsonPath.Parse(text);
}
catch (JsonPathSyntaxException e)
{
Assert.IsTrue(e.Message.StartsWith("JSON Path expression indexers only support single constant values."));
throw;
}
}
[TestMethod]
[ExpectedException(typeof(JsonPathSyntaxException))]
public void ArrayIndexExpressionEqualsValue()
{
var text = "$[?(@[(@.length-1))]";

try
{
var actual = JsonPath.Parse(text);
}
catch (JsonPathSyntaxException e)
{
Assert.IsTrue(e.Message.StartsWith("JSON Path expression indexers only support single constant values."));
throw;
}
}
[TestMethod]
[ExpectedException(typeof(JsonPathSyntaxException))]
public void ArrayFilterExpressionEqualsValue()
{
var text = "$[?(@[?(@.name == 5))]";

try
{
var actual = JsonPath.Parse(text);
}
catch (JsonPathSyntaxException e)
{
Assert.IsTrue(e.Message.StartsWith("JSON Path expression indexers only support single constant values."));
throw;
}
}
[TestMethod]
public void HasProperty()
{
Run(JsonPathWith.Array(jv => jv.HasProperty("test")), "$[?(@.test)]");
Expand Down
11 changes: 11 additions & 0 deletions Manatee.Json.Tests/Path/IndexExpressionParseTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@ namespace Manatee.Json.Tests.Path
[TestClass]
public class IndexExpressionParseTest
{
private void Run(JsonPath expected, string text)
{
var actual = JsonPath.Parse(text);
Assert.AreEqual(expected, actual);
}

[TestMethod]
public void Length()
{
Expand Down Expand Up @@ -120,5 +126,10 @@ public void WhyOnGodsGreenEarthWouldAnyoneDoThis()

Assert.AreEqual(expected, actual);
}
[TestMethod]
public void JustAnInteger()
{
Run(JsonPathWith.Array(jv => 1), "$[(1)]");
}
}
}
184 changes: 184 additions & 0 deletions Manatee.Json.Tests/Path/ParsingFails.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,184 @@
using System;
using Manatee.Json.Path;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace Manatee.Json.Tests.Path
{
[TestClass]
public class ParsingFails
{
private void Run(string text)
{
try
{
Console.WriteLine($"\n{JsonPath.Parse(text)}");
}
catch (Exception e)
{
Console.WriteLine(e.Message);
throw;
}
}

[TestMethod]
[ExpectedException(typeof(JsonPathSyntaxException))]
public void EmptyIndexedArray()
{
Run("$[]");
}
[TestMethod]
[ExpectedException(typeof(JsonPathSyntaxException))]
public void EmptyIndexExpression()
{
Run("$[()]");
}
[TestMethod]
[ExpectedException(typeof(JsonPathSyntaxException))]
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()
{
// 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()
{
Run("$...name");
}
[TestMethod]
[ExpectedException(typeof(JsonPathSyntaxException))]
public void ArrayMultiIndex()
{
Run("$[?(@[1,3] == 5)]");
}
[TestMethod]
[ExpectedException(typeof(JsonPathSyntaxException))]
public void ArraySliceEqualsValue()
{
Run("$[?(@[1:3] == 5)]");
}
[TestMethod]
[ExpectedException(typeof(JsonPathSyntaxException))]
public void ArrayIndexExpressionEqualsValue()
{
Run("$[?(@[(@.length-1))]");
}
[TestMethod]
[ExpectedException(typeof(JsonPathSyntaxException))]
public void ArrayFilterExpressionEqualsValue()
{
Run("$[?(@[?(@.name == 5))]");
}
[TestMethod]
[ExpectedException(typeof(JsonPathSyntaxException))]
public void MissingSearchParameter()
{
Run("$..");
}
[TestMethod]
[ExpectedException(typeof(JsonPathSyntaxException))]
public void MissingKey()
{
Run("$..");
}
}
}
60 changes: 42 additions & 18 deletions Manatee.Json.Tests/Path/ParsingTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -216,24 +216,6 @@ public void MultiIndexedArray()
Assert.AreEqual(expected, actual);
}

[TestMethod]
[ExpectedException(typeof(JsonPathSyntaxException))]
public void EmptyIndexedArray()
{
var text = "$[]";

JsonPath.Parse(text);
}

[TestMethod]
[ExpectedException(typeof(JsonPathSyntaxException))]
public void EmptyObject()
{
var text = "$.";

JsonPath.Parse(text);
}

[TestMethod]
public void WildcardArray()
{
Expand Down Expand Up @@ -279,5 +261,47 @@ 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()
{
// ReSharper disable once NegativeEqualityExpression
// 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()
{
Run(JsonPathWith.Name("tes*t"), "$.\"tes*t\"");
}
[TestMethod]
public void EmptyKey()
{
Run(JsonPathWith.Name(""), "$.''");
}
[TestMethod]
public void EmptySearch()
{
Run(JsonPathWith.Search(""), "$..''");
}
}
}
12 changes: 11 additions & 1 deletion Manatee.Json.Tests/Path/ToStringTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,17 @@ public void Length()
{
Run("$.length", JsonPathWith.Length());
}

[TestMethod]
public void EmptyKey()
{
Run("$.''", JsonPathWith.Name(""));
}
[TestMethod]
public void EmptySearch()
{
Run("$..''", JsonPathWith.Search(""));
}

#endregion

#region Indexed Arrays
Expand Down
2 changes: 1 addition & 1 deletion Manatee.Json.sln.DotSettings.user
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<s:String x:Key="/Default/CodeInspection/Highlighting/AnalysisEnabled/@EntryValue">SOLUTION</s:String>
<s:Boolean x:Key="/Default/CoverageSessionsPersistenceManagerSettings/SessionPersistentData/=570F9EFBC8B44DC0AE101FD339D3EE8D/@KeyIndexDefined">True</s:Boolean>
<s:String x:Key="/Default/CoverageSessionsPersistenceManagerSettings/SessionPersistentData/=570F9EFBC8B44DC0AE101FD339D3EE8D/ProductVersion/@EntryValue">2016.2</s:String>
<s:String x:Key="/Default/CoverageSessionsPersistenceManagerSettings/SessionPersistentData/=570F9EFBC8B44DC0AE101FD339D3EE8D/SnapshotLocation/@EntryValue">C:\Users\Gregd\AppData\Local\Temp\ssm.Qageqif.tmp</s:String>
<s:String x:Key="/Default/CoverageSessionsPersistenceManagerSettings/SessionPersistentData/=570F9EFBC8B44DC0AE101FD339D3EE8D/SnapshotLocation/@EntryValue">C:\Users\Gregd\AppData\Local\Temp\ssm.Zymurij.tmp</s:String>



Expand Down
Loading

0 comments on commit 2ff9403

Please sign in to comment.