From 8c6fffe082a5f97c3fecada07823ef8b52f8e6ed Mon Sep 17 00:00:00 2001 From: Matt Edwards Date: Tue, 10 Sep 2024 16:48:42 -0400 Subject: [PATCH] Fix Task casting issues in tests --- .../AsyncBlockExpression.cs | 2 +- .../AsyncBlockTests.cs | 34 +++++++++---------- 2 files changed, 18 insertions(+), 18 deletions(-) diff --git a/src/Hyperbee.AsyncExpressions/AsyncBlockExpression.cs b/src/Hyperbee.AsyncExpressions/AsyncBlockExpression.cs index ee755ff..187e1c4 100644 --- a/src/Hyperbee.AsyncExpressions/AsyncBlockExpression.cs +++ b/src/Hyperbee.AsyncExpressions/AsyncBlockExpression.cs @@ -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; diff --git a/test/Hyperbee.AsyncExpressions.Tests/AsyncBlockTests.cs b/test/Hyperbee.AsyncExpressions.Tests/AsyncBlockTests.cs index 521753a..b4e45da 100644 --- a/test/Hyperbee.AsyncExpressions.Tests/AsyncBlockTests.cs +++ b/test/Hyperbee.AsyncExpressions.Tests/AsyncBlockTests.cs @@ -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( @@ -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), @@ -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); @@ -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(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( reducedExpression ); var compiledLambda = lambda.Compile(); compiledLambda(); // Should execute without assertion failures