From 0d61aa6d82d71bff4937509000aacf3bf352df04 Mon Sep 17 00:00:00 2001 From: dadhi Date: Wed, 2 Feb 2022 23:20:52 +0100 Subject: [PATCH] fixed: #284 --- ...Issue284_Invalid_Program_after_Coalesce.cs | 55 +++++++++++++++---- .../Program.cs | 2 +- 2 files changed, 44 insertions(+), 13 deletions(-) diff --git a/test/FastExpressionCompiler.IssueTests/Issue284_Invalid_Program_after_Coalesce.cs b/test/FastExpressionCompiler.IssueTests/Issue284_Invalid_Program_after_Coalesce.cs index 3aca7838..584a5f47 100644 --- a/test/FastExpressionCompiler.IssueTests/Issue284_Invalid_Program_after_Coalesce.cs +++ b/test/FastExpressionCompiler.IssueTests/Issue284_Invalid_Program_after_Coalesce.cs @@ -20,22 +20,23 @@ public int Run() Invalid_expresion_with_Coalesce_when_invoked_should_throw_NullRef_the_same_as_system_compiled(); Invalid_Program_after_Coalesce(); Coalesce_in_Assign_in_Block(); - return 3; + New_test(); + return 4; } [Test] public void Invalid_expresion_with_Coalesce_when_invoked_should_throw_NullRef_the_same_as_system_compiled() { var input = Parameter(typeof(Variable)); - var text = Parameter(typeof(string)); - var ctor = typeof(Variable).GetConstructor(new Type[] { typeof(string) }); - var prop = typeof(Variable).GetProperty(nameof(Variable.Name)); + var text = Parameter(typeof(string)); + var ctor = typeof(Variable).GetConstructor(new Type[] { typeof(string) }); + var prop = typeof(Variable).GetProperty(nameof(Variable.Name)); var lambda = Lambda>( Block( Coalesce(input, New(ctor, Constant("default"))), Assign(Property(input, prop), text), - input), + input), input, text); // var t = lambda.ToExpressionString(); @@ -54,9 +55,9 @@ public void Invalid_expresion_with_Coalesce_when_invoked_should_throw_NullRef_th [Test] public void Invalid_Program_after_Coalesce() { - var input = Parameter(typeof(Variable)); - var text = Parameter(typeof(string)); - var ctor = typeof(Variable).GetConstructor(new Type[] { typeof(string) }); + var input = Parameter(typeof(Variable)); + var text = Parameter(typeof(string)); + var ctor = typeof(Variable).GetConstructor(new Type[] { typeof(string) }); var property = typeof(Variable).GetProperty(nameof(Variable.Name)); var lambda = Lambda>( @@ -64,7 +65,7 @@ public void Invalid_Program_after_Coalesce() Assign(input, // NOTE: Don't forget to Assign! Coalesce(input, New(ctor, Constant("default")))), Assign(Property(input, property), text), - input), + input), input, text); lambda.PrintCSharp(); @@ -82,9 +83,9 @@ public void Invalid_Program_after_Coalesce() [Test] public void Coalesce_in_Assign_in_Block() { - var input = Parameter(typeof(Variable)); - var text = Parameter(typeof(string)); - var ctor = typeof(Variable).GetConstructor(new Type[] { typeof(string) }); + var input = Parameter(typeof(Variable)); + var text = Parameter(typeof(string)); + var ctor = typeof(Variable).GetConstructor(new Type[] { typeof(string) }); var property = typeof(Variable).GetProperty(nameof(Variable.Name)); var lambda = Lambda>( @@ -105,6 +106,36 @@ public void Coalesce_in_Assign_in_Block() Assert.AreEqual("default", v.Name); } + [Test] + public void New_test() + { + var input = Parameter(typeof(Variable)); + var text = Parameter(typeof(string)); + var ctor = typeof(Variable).GetConstructor(new Type[] { typeof(string) }); + var property = typeof(Variable).GetProperty(nameof(Variable.Name)); + + var lambda = Lambda>( + Block( + Coalesce(input, New(ctor, Constant("default"))), + Assign(Property(input, property), text), + input), + input, + text); + + lambda.PrintCSharp(s => s.Replace(GetType().Name + ".", "")); + + var fSys = lambda.CompileSys(); + fSys.PrintIL(); + Assert.Throws(typeof(NullReferenceException), () => + fSys(null, "a")); + + var fFec = lambda.CompileFast(); + Assert.IsNotNull(fFec); + fFec.PrintIL(); + Assert.Throws(typeof(NullReferenceException), () => + fFec(null, "a")); + } + public class Variable { public string Name { get; set; } diff --git a/test/FastExpressionCompiler.TestsRunner/Program.cs b/test/FastExpressionCompiler.TestsRunner/Program.cs index 9a52842c..53e6a1bb 100644 --- a/test/FastExpressionCompiler.TestsRunner/Program.cs +++ b/test/FastExpressionCompiler.TestsRunner/Program.cs @@ -36,7 +36,7 @@ public static void Main() // new FastExpressionCompiler.LightExpression.IssueTests.Issue274_Failing_Expressions_in_Linq2DB().Run(); // new FastExpressionCompiler.LightExpression.IssueTests.Issue159_NumericConversions().Run(); // new FastExpressionCompiler.LightExpression.UnitTests.BinaryExpressionTests().Run(); - // new FastExpressionCompiler.LightExpression.IssueTests.Issue284_Invalid_Program_after_Coalesce().Run(); + //new FastExpressionCompiler.LightExpression.IssueTests.Issue284_Invalid_Program_after_Coalesce().Run(); } public static void RunAllTests()