Skip to content

Commit

Permalink
Adjust test naming and remove commented code
Browse files Browse the repository at this point in the history
  • Loading branch information
bfarmer67 committed Jun 26, 2024
1 parent a25ca64 commit e9fd856
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 49 deletions.
23 changes: 8 additions & 15 deletions src/Hyperbee.Json/Filters/Parser/FilterParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ internal static Expression Parse( ReadOnlySpan<char> filter, FilterContext<TNode
return FilterTruthyExpression.IsTruthyExpression( expression );
}

internal static Expression Parse( ref ParserState state, FilterContext<TNode> context )
internal static Expression Parse( ref ParserState state, FilterContext<TNode> context ) // recursion entrypoint
{
// validate input
if ( context == null )
Expand Down Expand Up @@ -373,36 +373,30 @@ Operator.NotEquals when IsNumerical( left.Expression?.Type ) || IsNumerical( rig

private static class ExpressionConverter
{
// Cache the MethodInfo for GetAsValue method
private static readonly MethodInfo GetAsValueMethodInfo = typeof( IValueAccessor<TNode> )
.GetMethod( nameof( IValueAccessor<TNode>.GetAsValue ) );
// Cached delegate for calling IValueAccessor<TNode>GetAsValue( IEnumerable<TNode> nodes )

// Cache the compiled delegate for calling GetAsValue method
private static readonly Func<IValueAccessor<TNode>, IEnumerable<TNode>, object> GetAsValueDelegate;

static ExpressionConverter()
{
// Create parameters for the expression
// Pre-compile the delegate to call the GetAsValue method

var accessorParam = Expression.Parameter( typeof( IValueAccessor<TNode> ), "accessor" );
var expressionParam = Expression.Parameter( typeof( IEnumerable<TNode> ), "expression" );

// Create the expression to call the GetAsValue method
var callExpression = Expression.Call( accessorParam, GetAsValueMethodInfo, expressionParam );
var methodInfo = typeof(IValueAccessor<TNode>).GetMethod( nameof( IValueAccessor<TNode>.GetAsValue ) );
var callExpression = Expression.Call( accessorParam, methodInfo!, expressionParam );

// Compile the expression into a delegate
GetAsValueDelegate = Expression.Lambda<Func<IValueAccessor<TNode>, IEnumerable<TNode>, object>>(
callExpression, accessorParam, expressionParam ).Compile();
}

public static Expression ConvertToValue( IValueAccessor<TNode> accessor, Expression expression )
{
if ( expression == null )
return null;

if ( expression.Type != typeof( IEnumerable<TNode> ) )
if ( expression == null || expression.Type != typeof( IEnumerable<TNode> ) )
return expression;

// Create an expression representing the instance of the descriptor
// Create an expression representing the instance of the accessor
var accessorExpression = Expression.Constant( accessor );

// Use the compiled delegate to create an expression to call the GetAsValue method
Expand All @@ -427,7 +421,6 @@ public static Expression ConvertToNumber( Expression expression )

return expression;
}

}

private class ExprItem( Expression expression, Operator op )
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,13 @@
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text.Json;
using System.Text.Json.Nodes;
using Hyperbee.Json.Descriptors.Element;
using Hyperbee.Json.Descriptors.Node;
using Hyperbee.Json.Extensions;
using Hyperbee.Json.Filters.Parser;
using Hyperbee.Json.Tests.TestSupport;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Expression = System.Linq.Expressions.Expression;

namespace Hyperbee.Json.Tests.Evaluators;
namespace Hyperbee.Json.Tests.Parsers;

[TestClass]
public class FilterExtensionFunctionTests : JsonTestBase
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@
using Hyperbee.Json.Tests.TestSupport;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace Hyperbee.Json.Tests.Evaluators;
namespace Hyperbee.Json.Tests.Parsers;

[TestClass]
public class FilterExpressionParserTests : JsonTestBase
public class FilterParserTests : JsonTestBase
{
[DataTestMethod]
[DataRow( "((\"world\" == 'world') && (1 == 1))", true, typeof( JsonElement ) )]
Expand Down Expand Up @@ -126,13 +126,13 @@ public void Should_MatchExpectedResult_WhenUsingFunctions( string filter, bool e
}

[DataTestMethod]
[DataRow( "length(@.store.book) == 4", true, typeof( JsonElement ) )]
[DataRow( "length(@.store.book) == 4 ", true, typeof( JsonElement ) )]
[DataRow( "length (@.store.book) == 4 ", true, typeof(JsonElement) )]
[DataRow( " length(@.store.book) == 4", true, typeof( JsonElement ) )]
[DataRow( " length(@.store.book) == 4 ", true, typeof( JsonElement ) )]
[DataRow( " length( @.store.book ) == 4 ", true, typeof( JsonElement ) )]
[DataRow( "4 == length(@.store.book)", true, typeof( JsonElement ) )]
[DataRow( "4 == length( @.store.book ) ", true, typeof( JsonElement ) )]
[DataRow( "4 == length ( @.store.book ) ", true, typeof(JsonElement) )]
[DataRow( " 4 == length(@.store.book)", true, typeof( JsonElement ) )]
[DataRow( " 4 == length(@.store.book) ", true, typeof( JsonElement ) )]
[DataRow( " 4 == length( @.store.book ) ", true, typeof( JsonElement ) )]
Expand Down Expand Up @@ -192,18 +192,17 @@ private static bool Execute( Expression expression, ParameterExpression param, T

return func( new JsonElement() );
}
else if ( sourceType == typeof( JsonNode ) )

if ( sourceType == typeof( JsonNode ) )
{
var func = Expression
.Lambda<Func<JsonNode, bool>>( expression, param )
.Compile();

return func( JsonNode.Parse( "{}" ) );
}
else
{
throw new NotImplementedException();
}

throw new NotImplementedException();
}

private static bool CompileAndExecute( string filter, Type sourceType )
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace Hyperbee.Json.Tests.Tokenizer;
namespace Hyperbee.Json.Tests.Parsers;

[TestClass]
public class JsonPathQueryTokenizerTests
public class JsonPathQueryParserTests
{
[DataTestMethod]
[DataRow( "$", "{$|s}" )]
Expand Down Expand Up @@ -33,20 +33,20 @@ public class JsonPathQueryTokenizerTests
public void Should_tokenize_json_path( string jsonPath, string expected )
{
// act
var tokens = JsonPathQueryParser.Parse( jsonPath );
var pathSegment = JsonPathQueryParser.Parse( jsonPath );

// arrange
var result = TokensToString( tokens );
var result = SegmentsToString( pathSegment );

// assert
Assert.AreEqual( expected, result );
return;

static string TokensToString( JsonPathSegment segment )
static string SegmentsToString( JsonPathSegment segment )
{
return string.Join( ';', segment.AsEnumerable().Select( TokenToString ) );
return string.Join( ';', segment.AsEnumerable().Select( SegmentToString ) );

static string TokenToString( JsonPathSegment segment )
static string SegmentToString( JsonPathSegment segment )
{
var (singular, selectors) = segment;
var selectorType = singular ? "s" : "g"; // s:singular, g:group
Expand Down
7 changes: 0 additions & 7 deletions test/Hyperbee.Json.Tests/TestSupport/JsonPathPair.cs

This file was deleted.

6 changes: 0 additions & 6 deletions test/Hyperbee.Json.Tests/TestSupport/JsonTestBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,6 @@ public static TType GetDocument<TType>( string filename = null )
if ( type == typeof( JsonDocument ) )
return (TType) (object) ReadJsonDocument( filename );

//if ( type == typeof(JsonElement) )
// return (TType)(object) ReadJsonDocument( filename )?.RootElement;

if ( type == typeof( JsonNode ) )
return (TType) (object) ReadJsonNode( filename );

Expand All @@ -59,9 +56,6 @@ public static object GetDocument( Type target, string filename = null )
if ( target == typeof( JsonDocument ) )
return GetDocument<JsonDocument>( filename );

//if ( target == typeof(JsonElement) )
// return GetDocument<JsonDocument>( filename )?.RootElement;

if ( target == typeof( JsonNode ) )
return GetDocument<JsonNode>( filename );

Expand Down

0 comments on commit e9fd856

Please sign in to comment.