Skip to content

Commit

Permalink
Fix Task casting issues in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
MattEdwardsWaggleBee committed Sep 10, 2024
1 parent 41ca75a commit 8c6fffe
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 18 deletions.
2 changes: 1 addition & 1 deletion src/Hyperbee.AsyncExpressions/AsyncBlockExpression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public AsyncBlockExpression( ParameterExpression[] variables, Expression[] expre
{
splitVisitor.Visit( expr );
}
_expressions = splitVisitor.Expressions.ToArray();
_expressions = [..splitVisitor.Expressions];
}

public override bool CanReduce => true;
Expand Down
34 changes: 17 additions & 17 deletions test/Hyperbee.AsyncExpressions.Tests/AsyncBlockTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@ public void TestAsyncBlock_VariableScopeWithMultipleAwaits()
var assignExpr1 = Expression.Assign(varExpr, Expression.Constant(10));

// First await expression
var awaitExpr1 = AsyncExpression.Await(Expression.Constant(Task.CompletedTask), false);
var awaitExpr1 = AsyncExpression.Await(Expression.Constant(Task.CompletedTask, typeof( Task ) ), false);

// Increment variable after first await
var assignExpr2 = Expression.Assign(varExpr, Expression.Increment(varExpr));

// Second await expression
var awaitExpr2 = AsyncExpression.Await(Expression.Constant(Task.CompletedTask), false);
var awaitExpr2 = AsyncExpression.Await(Expression.Constant(Task.CompletedTask, typeof( Task ) ), false);

// Assert to check if variable maintains scope and has the expected value
var assertExpr1 = Expression.Call(
Expand Down Expand Up @@ -178,9 +178,9 @@ public void TestAsyncBlock_MultipleAwaitAndVariableUpdate()
// Arrange
var varExpr = Expression.Variable(typeof(int), "x");
var assignExpr1 = Expression.Assign(varExpr, Expression.Constant(1));
var awaitExpr1 = AsyncExpression.Await(Expression.Constant(Task.CompletedTask), false);
var awaitExpr1 = AsyncExpression.Await(Expression.Constant(Task.CompletedTask, typeof( Task ) ), false);
var assignExpr2 = Expression.Assign(varExpr, Expression.Add(varExpr, Expression.Constant(2)));
var awaitExpr2 = AsyncExpression.Await(Expression.Constant(Task.CompletedTask), false);
var awaitExpr2 = AsyncExpression.Await(Expression.Constant(Task.CompletedTask, typeof( Task ) ), false);
var assertExpr = Expression.Call(
GetMethod(nameof(AreEqual)),
Expression.Constant(3),
Expand All @@ -189,7 +189,7 @@ public void TestAsyncBlock_MultipleAwaitAndVariableUpdate()
var asyncBlock = AsyncExpression.BlockAsync( assignExpr1, awaitExpr1, assignExpr2, awaitExpr2, assertExpr );

// Act
var reducedExpression = asyncBlock.Reduce() as BlockExpression;
var reducedExpression = asyncBlock.ReduceBlock( out _ );

// Assert
Assert.IsNotNull(reducedExpression);
Expand All @@ -204,29 +204,29 @@ public void TestAsyncBlock_MultipleAwaitAndVariableUpdate()
public void TestAsyncBlock_NestedAsyncBlock()
{
// Arrange
var varExpr = Expression.Variable(typeof(int), "x");
var assignExpr1 = Expression.Assign(varExpr, Expression.Constant(1));
var awaitExpr1 = AsyncExpression.Await(Expression.Constant(Task.CompletedTask), false);
var varExpr = Expression.Variable( typeof(int), "x" );
var assignExpr1 = Expression.Assign( varExpr, Expression.Constant( 1 ) );
var awaitExpr1 = AsyncExpression.Await( Expression.Constant( Task.CompletedTask, typeof(Task) ), false );

// Inner async block
var innerAssign = Expression.Assign(varExpr, Expression.Add(varExpr, Expression.Constant(2)));
var innerAwait = AsyncExpression.Await(Expression.Constant(Task.CompletedTask), false);
var innerAssign = Expression.Assign( varExpr, Expression.Add( varExpr, Expression.Constant( 2 ) ) );
var innerAwait = AsyncExpression.Await( Expression.Constant( Task.CompletedTask, typeof(Task) ), false );
var innerBlock = AsyncExpression.BlockAsync( innerAssign, innerAwait );

var assertExpr = Expression.Call(
GetMethod(nameof(AreEqual)),
Expression.Constant(3),
varExpr);
GetMethod( nameof(AreEqual) ),
Expression.Constant( 3 ),
varExpr );

var asyncBlock = AsyncExpression.BlockAsync( assignExpr1, awaitExpr1, innerBlock, assertExpr );

// Act
var reducedExpression = asyncBlock.Reduce() as BlockExpression;
var reducedExpression = asyncBlock.ReduceBlock( out _ );

// Assert
Assert.IsNotNull(reducedExpression);
Assert.AreEqual(2, reducedExpression.Expressions.Count); // Should result in three sub-blocks (outer block and inner async treated as a single block)
var lambda = Expression.Lambda<Action>(reducedExpression);
Assert.IsNotNull( reducedExpression );
Assert.AreEqual( 2, reducedExpression.Expressions.Count ); // Should result in three sub-blocks (outer block and inner async treated as a single block)
var lambda = Expression.Lambda<Action>( reducedExpression );
var compiledLambda = lambda.Compile();

compiledLambda(); // Should execute without assertion failures
Expand Down

0 comments on commit 8c6fffe

Please sign in to comment.