Skip to content

Commit

Permalink
fixed: #284
Browse files Browse the repository at this point in the history
  • Loading branch information
dadhi committed Feb 2, 2022
1 parent 7160c0f commit 0d61aa6
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<Func<Variable, string, Variable>>(
Block(
Coalesce(input, New(ctor, Constant("default"))),
Assign(Property(input, prop), text),
input),
input),
input, text);

// var t = lambda.ToExpressionString();
Expand All @@ -54,17 +55,17 @@ 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<Func<Variable, string, Variable>>(
Block(
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();
Expand All @@ -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<Func<Variable, string, Variable>>(
Expand All @@ -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<Func<Variable, string, Variable>>(
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; }
Expand Down
2 changes: 1 addition & 1 deletion test/FastExpressionCompiler.TestsRunner/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit 0d61aa6

Please sign in to comment.