-
-
Notifications
You must be signed in to change notification settings - Fork 383
Expand file tree
/
Copy pathLambadaExtensionsTest.cs
More file actions
803 lines (688 loc) · 27.1 KB
/
LambadaExtensionsTest.cs
File metadata and controls
803 lines (688 loc) · 27.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the Apache 2.0 License
// See the LICENSE file in the project root for more information.
// Maintainer: Argo Zhang(argo@live.ca) Website: https://www.blazor.zone
using System.ComponentModel.DataAnnotations;
using System.Data;
using System.Dynamic;
using System.Linq.Expressions;
namespace UnitTest.Extensions;
public class LambadaExtensionsTest : BootstrapBlazorTestBase
{
[Fact]
public void GetFilterFunc_Comparison()
{
var filter = new FilterKeyValueAction()
{
FieldKey = "Name",
FilterAction = FilterAction.Contains,
FieldValue = "T"
};
var foos = new Foo[]
{
new() { Name = "Test1" },
new() { Name = "test2" },
};
var items = foos.Where(filter.GetFilterFunc<Foo>());
Assert.Single(items);
// 忽略大小写
items = foos.Where(filter.GetFilterFunc<Foo>(StringComparison.OrdinalIgnoreCase));
Assert.Equal(2, items.Count());
}
[Fact]
public void GetFilterFunc_Null()
{
var foos = new Foo[]
{
new() { Count = 1 },
new() { Count = 2 },
new() { Count = 10 },
new() { Count = 11 }
};
var filter = Array.Empty<MockFilterActionBase>();
var items = foos.Where(filter.GetFilterFunc<Foo>());
Assert.Equal(4, items.Count());
}
[Fact]
public void GetFilterLambda_Nullable()
{
var foos = new Foo[]
{
new() { Count = 1 },
new() { Count = 2 },
new() { Count = 10 },
new() { Count = 11 }
};
var filter = new FilterKeyValueAction()
{
FieldKey = "DateTime",
FilterAction = FilterAction.NotEqual,
FieldValue = DateTime.MinValue
};
var items = foos.Where(filter.GetFilterFunc<Foo>());
Assert.Empty(items);
}
[Fact]
public void GetFilterLambda_Filter()
{
var foos = new Foo[]
{
new() { Count = 1 },
new() { Count = 2 },
new() { Count = 10 },
new() { Count = 11 }
};
var filter = new FilterKeyValueAction()
{
Filters =
[
new FilterKeyValueAction()
{
FilterLogic = FilterLogic.Or,
Filters =
[
new FilterKeyValueAction() { FieldKey = "Count", FilterAction = FilterAction.Equal, FieldValue = 1 },
new FilterKeyValueAction() { FieldKey = "Count", FilterAction = FilterAction.Equal, FieldValue = 2 }
]
},
new FilterKeyValueAction() { FieldKey = "Count", FilterAction = FilterAction.GreaterThan, FieldValue = 1 },
new FilterKeyValueAction() { FieldKey = "Count", FilterAction = FilterAction.LessThan, FieldValue = 10 }
]
};
var items = foos.Where(filter.GetFilterFunc<Foo>());
Assert.Single(items);
}
[Fact]
public void GetFilterLambda_Enum()
{
var filters = new FilterKeyValueAction() { FieldKey = nameof(Dummy.Education), FieldValue = "Middle" };
var exp = filters.GetFilterLambda<Dummy>();
Assert.True(exp.Compile().Invoke(new Dummy() { Education = EnumEducation.Middle }));
}
[Fact]
public void GetFilterLambda_And()
{
var foos = new Foo[]
{
new() { Count = 1 },
new() { Count = 2 },
new() { Count = 10 },
new() { Count = 11 }
};
var filter = new MockFilterActionBase[]
{
new MockAndFilterAction1(),
new MockAndFilterAction2()
};
var items = foos.Where(filter.GetFilterFunc<Foo>());
Assert.Single(items);
}
[Fact]
public void GetFilterLambda_Or()
{
var foos = new Foo[]
{
new() { Count = 1 },
new() { Count = 2 },
new() { Count = 10 },
new() { Count = 11 }
};
var filter = new MockFilterActionBase[]
{
new MockOrFilterAction1(),
new MockOrFilterAction2()
};
var items = foos.Where(filter.GetFilterFunc<Foo>(FilterLogic.Or));
Assert.Equal(3, items.Count());
}
[Fact]
public void FilterKeyValueAction_FieldName_Null()
{
// FieldValue 为 null 时 均返回 true
var filter = new FilterKeyValueAction() { FieldKey = "", FieldValue = 1 };
var invoker = filter.GetFilterLambda<Foo>().Compile();
// 符合条件
Assert.True(invoker.Invoke(new Foo()));
Assert.True(invoker.Invoke(new Foo() { Name = "Test" }));
}
[Fact]
public void FilterKeyValueAction_FieldValue_Null()
{
// FieldValue 为 null 时 均返回 true
var filter = new FilterKeyValueAction() { FieldKey = "Name", FieldValue = null };
var invoker = filter.GetFilterLambda<Foo>().Compile();
// 符合条件
Assert.True(invoker.Invoke(new Foo()));
Assert.True(invoker.Invoke(new Foo() { Name = "Test" }));
}
[Fact]
public void FilterKeyValueAction_SimpleFilterExpression()
{
var filter = new FilterKeyValueAction() { FieldKey = "Name", FieldValue = "Name" };
var invoker = filter.GetFilterLambda<Foo>().Compile();
Assert.True(invoker.Invoke(new Foo() { Name = "Name" }));
Assert.False(invoker.Invoke(new Foo() { Name = "Name1" }));
}
[Fact]
public void FilterKeyValueAction_SimpleFilterExpression_Exception()
{
var filter = new FilterKeyValueAction() { FieldKey = "Name", FieldValue = "Name" };
Assert.Throws<InvalidOperationException>(() => filter.GetFilterLambda<Dummy>());
}
[Fact]
public void FilterKeyValueAction_ComplexFilterExpression()
{
var filter = new FilterKeyValueAction() { FieldKey = "Foo.Name", FieldValue = "Name" };
var invoker = filter.GetFilterLambda<Dummy>().Compile();
Assert.True(invoker.Invoke(new Dummy() { Foo = new Foo() { Name = "Name" } }));
}
[Fact]
public void FilterKeyValueAction_ComplexFilterExpression_Exception()
{
var filter = new FilterKeyValueAction() { FieldKey = "Foo.TestName", FieldValue = "Name" };
Assert.Throws<InvalidOperationException>(() => filter.GetFilterLambda<Dummy>());
filter = new FilterKeyValueAction() { FieldKey = "Foo1.TestName", FieldValue = "Name" };
Assert.Throws<InvalidOperationException>(() => filter.GetFilterLambda<Dummy>());
}
[Fact]
public void FilterKeyValueAction_ComplexFilterExpression_Nullable()
{
// 搜索条件为 DateTime.Now
var filter = new FilterKeyValueAction() { FieldKey = "Foo.DateTime", FieldValue = DateTime.Now };
var invoker = filter.GetFilterLambda<Dummy>().Compile();
// 均不符合条件
Assert.False(invoker.Invoke(new Dummy() { Foo = new Foo() { DateTime = DateTime.MinValue } }));
Assert.False(invoker.Invoke(new Dummy() { Foo = new Foo() { DateTime = null } }));
// 搜索条件为 Null
filter = new FilterKeyValueAction() { FieldKey = "Foo.DateTime", FieldValue = null };
invoker = filter.GetFilterLambda<Dummy>().Compile();
// 均符合条件
Assert.True(invoker.Invoke(new Dummy() { Foo = new Foo() { DateTime = DateTime.MinValue } }));
Assert.True(invoker.Invoke(new Dummy() { Foo = new Foo() { DateTime = null } }));
}
[Fact]
public void FilterKeyValueAction_ComplexFilterExpression_Enum()
{
var filter = new FilterKeyValueAction() { FieldKey = "Cat.Education", FieldValue = "Middle" };
var invoker = filter.GetFilterLambda<Dummy>().Compile();
Assert.True(invoker.Invoke(new Dummy() { Cat = new Cat() { Education = EnumEducation.Middle } }));
}
[Fact]
public void GetExpression_NotEqual()
{
var filter = new FilterKeyValueAction() { FieldKey = "Count", FieldValue = 1, FilterAction = FilterAction.NotEqual };
var invoker = filter.GetFilterLambda<Foo>().Compile();
Assert.True(invoker.Invoke(new Foo() { Count = 2 }));
}
[Fact]
public void GetExpression_GreaterThanOrEqual()
{
var filter = new FilterKeyValueAction() { FieldKey = "Count", FieldValue = 10, FilterAction = FilterAction.GreaterThanOrEqual };
var invoker = filter.GetFilterLambda<Foo>().Compile();
Assert.False(invoker.Invoke(new Foo() { Count = 9 }));
Assert.True(invoker.Invoke(new Foo() { Count = 10 }));
Assert.True(invoker.Invoke(new Foo() { Count = 11 }));
}
[Fact]
public void GetExpression_LessThanOrEqual()
{
var filter = new FilterKeyValueAction() { FieldKey = "Count", FieldValue = 10, FilterAction = FilterAction.LessThanOrEqual };
var invoker = filter.GetFilterLambda<Foo>().Compile();
Assert.True(invoker.Invoke(new Foo() { Count = 9 }));
Assert.True(invoker.Invoke(new Foo() { Count = 10 }));
Assert.False(invoker.Invoke(new Foo() { Count = 11 }));
}
[Fact]
public void GetExpression_Contains()
{
var filter = new FilterKeyValueAction() { FieldKey = "Name", FieldValue = "test", FilterAction = FilterAction.Contains };
var invoker = filter.GetFilterLambda<Foo>().Compile();
Assert.True(invoker.Invoke(new Foo() { Name = "1test1" }));
Assert.False(invoker.Invoke(new Foo() { Name = "1Test1" }));
Assert.False(invoker.Invoke(new Foo() { Name = "1Test123" }));
Assert.False(invoker.Invoke(new Foo() { Name = "Test" }));
Assert.False(invoker.Invoke(new Foo() { Name = "Test2" }));
}
[Fact]
public void GetExpression_NotContains()
{
var filter = new FilterKeyValueAction() { FieldKey = "Name", FieldValue = "test", FilterAction = FilterAction.NotContains };
var invoker = filter.GetFilterLambda<Foo>().Compile();
Assert.True(invoker.Invoke(new Foo() { Name = "11" }));
}
[Fact]
public void GetExpression_CustomPredicate()
{
var foo = new Foo() { Name = "11" };
var val = new Func<string, bool>(p1 => p1 == foo.Name);
var filter = new FilterKeyValueAction() { FieldKey = "Name", FieldValue = val, FilterAction = FilterAction.CustomPredicate };
var invoker = filter.GetFilterLambda<Foo>().Compile();
Assert.True(invoker.Invoke(foo));
// Expression
Expression<Func<string, bool>> p1 = p => p == foo.Name;
filter.FieldValue = p1;
invoker = filter.GetFilterLambda<Foo>().Compile();
Assert.True(invoker.Invoke(foo));
filter.FieldValue = new object();
Assert.Throws<InvalidOperationException>(() => filter.GetFilterLambda<Foo>());
}
[Fact]
public void Sort_Queryable()
{
var foos = new List<Foo>
{
new() { Name = "10", Count = 10 },
new() { Name = "10", Count = 20 },
new() { Name = "20", Count = 20 },
}.AsQueryable();
var orderFoos = LambdaExtensions.Sort(foos, ["Count desc", "Name"]);
Assert.Equal(20, orderFoos.ElementAt(0).Count);
Assert.Equal("20", orderFoos.ElementAt(1).Name);
orderFoos = LambdaExtensions.Sort(foos, "Count", SortOrder.Unset);
Assert.Equal(10, orderFoos.ElementAt(0).Count);
orderFoos = LambdaExtensions.Sort(foos, "Count", SortOrder.Desc);
Assert.Equal(20, orderFoos.ElementAt(0).Count);
orderFoos = LambdaExtensions.Sort(foos, "Count", SortOrder.Asc);
Assert.Equal(10, orderFoos.ElementAt(0).Count);
orderFoos = LambdaExtensions.Sort(foos, "Test", SortOrder.Asc);
Assert.Equal(10, orderFoos.ElementAt(0).Count);
}
[Fact]
public void Sort_Enumerable()
{
var foos = new List<Foo>
{
new() { Name = "10", Count = 10 },
new() { Name = "10", Count = 20 },
new() { Name = "20", Count = 20 },
};
var orderFoos = LambdaExtensions.Sort(foos, "Count", SortOrder.Unset);
Assert.Equal(10, orderFoos.ElementAt(0).Count);
orderFoos = LambdaExtensions.Sort(foos, "Count", SortOrder.Desc);
Assert.Equal(20, orderFoos.ElementAt(0).Count);
orderFoos = LambdaExtensions.Sort(foos, "Count", SortOrder.Asc);
Assert.Equal(10, orderFoos.ElementAt(0).Count);
orderFoos = LambdaExtensions.Sort(foos, "Test", SortOrder.Asc);
Assert.Equal(10, orderFoos.ElementAt(0).Count);
orderFoos = LambdaExtensions.Sort(foos, ["Count desc", "Name"]);
Assert.Equal(20, orderFoos.ElementAt(0).Count);
orderFoos = LambdaExtensions.Sort(foos, ["Count", "Name desc"]);
Assert.Equal(10, orderFoos.ElementAt(0).Count);
orderFoos = LambdaExtensions.Sort(foos, ["Count", "Test desc"]);
Assert.Equal(10, orderFoos.ElementAt(0).Count);
}
[Fact]
public void Sort_Queryable_Enumerable()
{
var foos = new List<Foo>
{
new() { Name = "10", Count = 10 },
new() { Name = "10", Count = 20 },
new() { Name = "20", Count = 20 },
}.AsQueryable();
var orderFoos = LambdaExtensions.Sort(foos, "Count", SortOrder.Unset);
Assert.Equal(10, orderFoos.ElementAt(0).Count);
orderFoos = LambdaExtensions.Sort(foos, "Count", SortOrder.Desc);
Assert.Equal(20, orderFoos.ElementAt(0).Count);
orderFoos = LambdaExtensions.Sort(foos, "Count", SortOrder.Asc);
Assert.Equal(10, orderFoos.ElementAt(0).Count);
orderFoos = LambdaExtensions.Sort(foos, "Test", SortOrder.Asc);
Assert.Equal(10, orderFoos.ElementAt(0).Count);
orderFoos = LambdaExtensions.Sort(foos, ["Count desc", "Name"]);
Assert.Equal(20, orderFoos.ElementAt(0).Count);
orderFoos = LambdaExtensions.Sort(foos, ["Count", "Name desc"]);
Assert.Equal(10, orderFoos.ElementAt(0).Count);
orderFoos = LambdaExtensions.Sort(foos, ["Count", "Test desc"]);
Assert.Equal(10, orderFoos.ElementAt(0).Count);
}
[Fact]
public void Sort_Complex()
{
var foos = new List<Dummy>
{
new() { Foo = new() { Name = "10", Count = 10 } },
new() { Foo = new() { Name = "10", Count = 20 } },
new() { Foo = new() { Name = "20", Count = 20 } }
};
var orderFoos = LambdaExtensions.Sort(foos, "Foo.Count", SortOrder.Asc);
Assert.Equal(10, orderFoos.ElementAt(0).Foo!.Count);
orderFoos = LambdaExtensions.Sort(foos, "Foo.Count", SortOrder.Desc);
Assert.Equal(20, orderFoos.ElementAt(0).Foo!.Count);
orderFoos = LambdaExtensions.Sort(foos, "Foo1.Count", SortOrder.Desc);
Assert.Equal(10, orderFoos.ElementAt(0).Foo!.Count);
orderFoos = LambdaExtensions.Sort(foos, ["Foo.Count desc", "Foo.Name"]);
Assert.Equal(20, orderFoos.ElementAt(0).Foo!.Count);
orderFoos = LambdaExtensions.Sort(foos, ["Foo.Count", "Foo.Name Desc"]);
Assert.Equal(10, orderFoos.ElementAt(0).Foo!.Count);
orderFoos = LambdaExtensions.Sort(foos, ["Foo.Count", "Foo.Test Desc"]);
Assert.Equal(10, orderFoos.ElementAt(0).Foo!.Count);
}
[Fact]
public void Sort_Queryable_Complex()
{
var foos = new List<Dummy>
{
new() { Foo = new() { Name = "10", Count = 10 } },
new() { Foo = new() { Name = "10", Count = 20 } },
new() { Foo = new() { Name = "20", Count = 20 } }
}.AsQueryable();
var orderFoos = LambdaExtensions.Sort(foos, "Foo.Count", SortOrder.Asc);
Assert.Equal(10, orderFoos.ElementAt(0).Foo!.Count);
orderFoos = LambdaExtensions.Sort(foos, "Foo.Count", SortOrder.Desc);
Assert.Equal(20, orderFoos.ElementAt(0).Foo!.Count);
orderFoos = LambdaExtensions.Sort(foos, "Foo1.Count", SortOrder.Desc);
Assert.Equal(10, orderFoos.ElementAt(0).Foo!.Count);
orderFoos = LambdaExtensions.Sort(foos, ["Foo.Count desc", "Foo.Name"]);
Assert.Equal(20, orderFoos.ElementAt(0).Foo!.Count);
orderFoos = LambdaExtensions.Sort(foos, ["Foo.Count", "Foo.Name Desc"]);
Assert.Equal(10, orderFoos.ElementAt(0).Foo!.Count);
orderFoos = LambdaExtensions.Sort(foos, ["Foo.Count", "Foo.Test Desc"]);
Assert.Equal(10, orderFoos.ElementAt(0).Foo!.Count);
}
[Fact]
public void Sort_IDynamicObject_Ok()
{
var dataTable = new DataTable();
DataColumn column = new DataColumn
{
DataType = Type.GetType("System.Int32"),
ColumnName = "ID"
};
dataTable.Columns.Add(column);
column = new DataColumn
{
DataType = Type.GetType("System.String"),
ColumnName = "Name"
};
dataTable.Columns.Add(column);
//Creating some rows
DataRow row = dataTable.NewRow();
row["ID"] = 1;
row["Name"] = "Bob";
dataTable.Rows.Add(row);
row = dataTable.NewRow();
row["ID"] = 3;
row["Name"] = "Adam";
dataTable.Rows.Add(row);
row = dataTable.NewRow();
row["ID"] = 2;
row["Name"] = "Jane";
dataTable.Rows.Add(row);
var context = new DataTableDynamicContext(dataTable, (context, col) => { });
var items = context.GetItems().ToList();
// 未排序
Assert.Equal("Bob", items[0].GetValue("Name"));
Assert.Equal("Adam", items[1].GetValue("Name"));
Assert.Equal("Jane", items[2].GetValue("Name"));
// Name 排序
var nameItems = items.Sort("Name", SortOrder.Asc).ToList();
Assert.Equal("Adam", nameItems[0].GetValue("Name"));
Assert.Equal("Bob", nameItems[1].GetValue("Name"));
Assert.Equal("Jane", nameItems[2].GetValue("Name"));
// Name 倒序
nameItems = items.Sort("Name", SortOrder.Desc).ToList();
Assert.Equal("Adam", nameItems[2].GetValue("Name"));
Assert.Equal("Bob", nameItems[1].GetValue("Name"));
Assert.Equal("Jane", nameItems[0].GetValue("Name"));
}
[Fact]
public void GetPropertyValueLambda_Null()
{
Foo? foo = null;
Assert.Throws<ArgumentNullException>(() => LambdaExtensions.GetPropertyValueLambda<object?, string>(foo, "Name"));
}
[Fact]
public void GetPropertyValueLambda_Dynamic()
{
var foo = new CustomDynamicData(new Dictionary<string, string>() { ["Name"] = "Test1" });
var invoker = LambdaExtensions.GetPropertyValueLambda<CustomDynamicData, string>(foo, "Name").Compile();
var t = invoker(foo);
Assert.Equal("Test1", t);
}
[Fact]
public void SetPropertyValueLambda_Null()
{
Foo? foo = null;
Assert.Throws<ArgumentNullException>(() => LambdaExtensions.SetPropertyValueLambda<object?, string>(foo, "Name"));
foo = new Foo() { Name = "Test1" };
Assert.Throws<InvalidOperationException>(() => LambdaExtensions.SetPropertyValueLambda<Foo, string>(foo, "Test1"));
var dummy = new Dummy() { Foo = foo };
var invoker1 = LambdaExtensions.SetPropertyValueLambda<Dummy, string>(dummy, "Foo.Name").Compile();
Assert.Throws<InvalidOperationException>(() => LambdaExtensions.SetPropertyValueLambda<Dummy, string>(dummy, "Foo.Test1"));
}
[Fact]
public void GetPropertyValueLambda_Ok()
{
var foo = new Foo() { Name = "Test1", Count = 10 };
var invoker = LambdaExtensions.GetPropertyValueLambda<Foo, string>(foo, "Name").Compile();
Assert.Equal("Test1", invoker(foo));
Assert.Throws<InvalidOperationException>(() => LambdaExtensions.GetPropertyValueLambda<Foo, string>(foo, "Test1"));
var dummy = new Dummy() { Foo = foo };
var invoker1 = LambdaExtensions.GetPropertyValueLambda<Dummy, string>(dummy, "Foo.Name").Compile();
Assert.Equal("Test1", invoker1(dummy));
var invoker2 = LambdaExtensions.GetPropertyValueLambda<Dummy, int>(dummy, "Foo.Count").Compile();
Assert.Equal(10, invoker2(dummy));
dummy.Foo = null;
Assert.Null(invoker1(dummy));
Assert.Throws<InvalidOperationException>(() => LambdaExtensions.GetPropertyValueLambda<Dummy, string>(dummy, "Foo.Test1"));
}
[Fact]
public void GetKeyValue_Ok()
{
var foo1 = new Dog() { Id = 123, Name = "Test", Age = 20 };
var foo2 = new Dog() { Id = 234, Name = "Test", Age = 20 };
var invoker1 = LambdaExtensions.GetKeyValue<Dog?, int>().Compile();
Assert.Equal(123, invoker1(foo1));
var invoker2 = LambdaExtensions.GetKeyValue<Dog?, object>().Compile();
Assert.Equal(123, invoker2(foo1));
Assert.Throws<InvalidOperationException>(() => LambdaExtensions.GetKeyValue<Dog?, DateTime>());
foo2.Id = 123;
var invoker3 = LambdaExtensions.GetKeyValue<Dog?, object>(typeof(DogKeyAttribute)).Compile();
Assert.Equal(invoker3(foo1), invoker3(foo2));
foo1.Age = 40;
Assert.NotEqual(invoker3(foo1), invoker3(foo2));
}
[Fact]
public void GetKeyValue_Tuple()
{
var val = new Tuple<int, string, int>(1, "Test1", 2);
var dog1 = new Dog()
{
Id = 1,
Age = 2,
Name = "Test1"
};
var invoker1 = LambdaExtensions.GetKeyValue<Dog, object>(typeof(DogKeyAttribute)).Compile();
var val1 = invoker1(dog1);
Assert.Equal(val, val1);
var invoker2 = LambdaExtensions.GetKeyValue<Dog, Tuple<int, string, int>>(typeof(DogKeyAttribute)).Compile();
var val2 = invoker1(dog1);
Assert.Equal(val, val2);
}
[Fact]
public void TryParse_Ok()
{
Func<int?, bool> func = _ => false;
var exp = Expression.Parameter(typeof(int?));
var pi = typeof(int?).GetProperty("HasValue");
if (pi != null)
{
var exp_p = Expression.Property(exp, pi);
func = Expression.Lambda<Func<int?, bool>>(exp_p, exp).Compile();
}
Assert.True(func.Invoke(10));
Assert.False(func.Invoke(null));
}
private abstract class MockFilterActionBase : IFilterAction
{
public abstract FilterKeyValueAction GetFilterConditions();
public virtual void Reset()
{
}
public virtual Task SetFilterConditionsAsync(FilterKeyValueAction conditions)
{
return Task.CompletedTask;
}
}
private class MockAndFilterAction1 : MockFilterActionBase
{
public override FilterKeyValueAction GetFilterConditions()
{
var filters = new FilterKeyValueAction()
{
Filters =
[
new()
{
FieldKey = "Count",
FieldValue = 1,
FilterAction = FilterAction.GreaterThan,
FilterLogic = FilterLogic.And
},
new()
{
FieldKey = "Count",
FieldValue = 10,
FilterAction = FilterAction.LessThan
}
]
};
return filters;
}
}
private class MockAndFilterAction2 : MockFilterActionBase
{
public override FilterKeyValueAction GetFilterConditions()
{
var filters = new FilterKeyValueAction()
{
Filters =
[
new()
{
FieldKey = "Count",
FieldValue = 2,
FilterAction = FilterAction.Equal,
FilterLogic = FilterLogic.And
}
]
};
return filters;
}
}
private class MockOrFilterAction1 : MockFilterActionBase
{
public override FilterKeyValueAction GetFilterConditions()
{
var filters = new FilterKeyValueAction()
{
FilterLogic = FilterLogic.Or,
Filters =
[
new()
{
FieldKey = "Count",
FieldValue = 1,
FilterAction = FilterAction.Equal
},
new()
{
FieldKey = "Count",
FieldValue = 2,
FilterAction = FilterAction.Equal
}
]
};
return filters;
}
}
private class MockOrFilterAction2 : MockFilterActionBase
{
public override FilterKeyValueAction GetFilterConditions()
{
var filters = new FilterKeyValueAction()
{
Filters =
[
new()
{
FieldKey = "Count",
FieldValue = 10,
FilterAction = FilterAction.Equal
}
]
};
return filters;
}
}
private class CustomDynamicData(Dictionary<string, string> data) : System.Dynamic.DynamicObject
{
/// <summary>
/// 存储每列值信息 Key 列名 Value 为列值
/// </summary>
public Dictionary<string, string> Dynamic { get; set; } = data;
/// <summary>
///
/// </summary>
public CustomDynamicData() : this([]) { }
/// <summary>
/// <inheritdoc/>
/// </summary>
/// <param name="binder"></param>
/// <param name="result"></param>
/// <returns></returns>
public override bool TryGetMember(GetMemberBinder binder, out object? result)
{
if (Dynamic.TryGetValue(binder.Name, out string? value))
{
result = value;
}
else
{
// When property name not found, return empty
result = "";
}
return true;
}
/// <summary>
/// <inheritdoc/>
/// </summary>
/// <param name="binder"></param>
/// <param name="value"></param>
/// <returns></returns>
public override bool TrySetMember(SetMemberBinder binder, object? value)
{
var ret = false;
var v = value?.ToString() ?? string.Empty;
if (Dynamic.ContainsKey(binder.Name))
{
Dynamic[binder.Name] = v;
ret = true;
}
return ret;
}
}
private class Dummy
{
public Foo? Foo { get; set; }
public EnumEducation Education { get; set; }
public Cat? Cat { get; set; }
}
private class Cat
{
public EnumEducation Education { get; set; }
}
private class Dog
{
[DogKey]
[Key]
public int Id { get; set; }
[DogKey]
public string? Name { get; set; }
[DogKey]
public int Age { get; set; }
}
private class DogKeyAttribute : Attribute
{
}
}