Skip to content

Commit b664eaa

Browse files
author
maximv
committed
add test with event for #287
1 parent b65da76 commit b664eaa

File tree

2 files changed

+43
-36
lines changed

2 files changed

+43
-36
lines changed

test/FastExpressionCompiler.IssueTests/Issue274_Failing_Expressions_in_Linq2DB.cs

Lines changed: 41 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -544,46 +544,42 @@ public void Test_case_4_Full_InvalidCastException()
544544
[Test]
545545
public void Test_287_Case2_SimpleDelegate_as_nested_lambda_in_TesCollection_test()
546546
{
547-
var p = new ParameterExpression[3]; // the parameter expressions
548-
var e = new Expression[8]; // the unique expressions
547+
string gotS = null;
548+
SimpleDelegate sd = s => gotS = s;
549+
550+
var p = new ParameterExpression[9]; // the parameter expressions
551+
var e = new Expression[63]; // the unique expressions
549552
var l = new LabelTarget[0]; // the labels
550-
var expr = Lambda<Action<SampleClass, string>>( // $
553+
var expr = Lambda<Action<Dynamic.SampleClass>>( // $
551554
e[0] = Block(
552555
typeof(void),
553-
new[] {
554-
p[0]=Parameter(typeof(SimpleDelegate), "handler")
555-
},
556-
e[1] = MakeBinary(ExpressionType.Assign,
557-
p[0 // (SimpleDelegate handler)
558-
],
559-
e[2] = Field(
560-
p[1] = Parameter(typeof(SampleClass)),
561-
typeof(SampleClass).GetTypeInfo().GetDeclaredField("_SimpleDelegateEvent"))),
562-
e[3] = Condition(
563-
e[4] = MakeBinary(ExpressionType.NotEqual,
564-
p[0 // (SimpleDelegate handler)
565-
],
566-
e[5] = Constant(null, typeof(System.Delegate))),
567-
e[6] = Invoke(
568-
p[0 // (SimpleDelegate handler)
569-
],
570-
p[2] = Parameter(typeof(string))),
571-
e[7] = Empty(),
572-
typeof(void))),
573-
p[1 // (SampleClass)
574-
],
575-
p[2 // (string string__41619574)
576-
]);
556+
new ParameterExpression[0],
557+
e[1] = Call(
558+
p[0] = Parameter(typeof(Dynamic.SampleClass)),
559+
typeof(Dynamic.SampleClass).GetMethods().Single(x => !x.IsGenericMethod && x.Name == "add_SimpleDelegateEvent" && x.GetParameters().Select(y => y.ParameterType).SequenceEqual(new[] { typeof(Dynamic.SimpleDelegate) })),
560+
e[4] = Lambda<Dynamic.SimpleDelegate>(Invoke(
561+
Constant(sd, typeof(SimpleDelegate)),
562+
p[1] = Parameter(typeof(string), "_")),
563+
p[1]))
564+
),
565+
p[0 // (SampleClass sampleclass__26320983)
566+
]);
577567

578568
expr.PrintCSharp();
579569

580570
var fs = expr.CompileSys();
581571
fs.PrintIL();
582-
fs(new SampleClass(), "43");
572+
var s = new Dynamic.SampleClass();
573+
fs(s);
574+
s.InvokeSimpleDelegateEvent("1");
575+
Assert.AreEqual("1", gotS);
583576

584577
var fx = expr.CompileFast(true);
585578
fx.PrintIL();
586-
fx(new SampleClass(), "43");
579+
s = new Dynamic.SampleClass();
580+
fx(s);
581+
s.InvokeSimpleDelegateEvent("2");
582+
Assert.AreEqual("2", gotS);
587583
}
588584

589585
[Test]
@@ -876,14 +872,27 @@ class c__DisplayClass6_0
876872
public class Dynamic
877873
{
878874
public delegate void SimpleDelegate(string input);
875+
876+
public class SampleClass
877+
{
878+
public event SimpleDelegate SimpleDelegateEvent;
879+
880+
public void InvokeSimpleDelegateEvent(string s) => SimpleDelegateEvent?.Invoke(s);
881+
}
879882
}
880883

881884
public static void HandleString(string s)
882885
{
883886

884887
}
885888

886-
class SampleClass
889+
class TypeWrapper
890+
{
891+
public object instance_ { get; }
892+
}
893+
894+
895+
class SampleClass : TypeWrapper
887896
{
888897
public int Id { get; set; }
889898
public int Value { get; set; }
@@ -897,8 +906,8 @@ class SampleClass
897906
private SimpleDelegate _SimpleDelegateEvent;
898907
public event SimpleDelegate SimpleDelegateEvent
899908
{
900-
add => _SimpleDelegateEvent = (SimpleDelegate)Delegate.Combine(_SimpleDelegateEvent, value);
901-
remove => _SimpleDelegateEvent = (SimpleDelegate)Delegate.Remove (_SimpleDelegateEvent, value);
909+
add => _SimpleDelegateEvent = (SimpleDelegate)Delegate.Combine(_SimpleDelegateEvent, value);
910+
remove => _SimpleDelegateEvent = (SimpleDelegate)Delegate.Remove(_SimpleDelegateEvent, value);
902911
}
903912

904913

test/FastExpressionCompiler.TestsRunner/Program.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,10 @@ public static void Main()
1111
{
1212
RunAllTests();
1313

14+
//new FastExpressionCompiler.LightExpression.IssueTests.Issue274_Failing_Expressions_in_Linq2DB().Run();
15+
1416
// new FastExpressionCompiler.LightExpression.IssueTests.Issue159_NumericConversions().Run();
15-
1617
// new FastExpressionCompiler.LightExpression.UnitTests.BinaryExpressionTests().Run();
17-
18-
// new FastExpressionCompiler.LightExpression.IssueTests.Issue274_Failing_Expressions_in_Linq2DB().Run();
19-
2018
// new FastExpressionCompiler.LightExpression.IssueTests.Issue284_Invalid_Program_after_Coalesce().Run();
2119
}
2220

0 commit comments

Comments
 (0)