1
- using System . Collections . Generic ;
1
+ using System ;
2
+ using System . Collections . Generic ;
2
3
using System . Linq ;
4
+ using System . Linq . Expressions ;
3
5
using Xunit ;
4
6
5
7
namespace Enflow . Base . Test
6
8
{
7
9
public class PositiveCounterRule : StateRule < CounterModel >
8
10
{
9
- public override bool IsSatisfied ( CounterModel candidate ) { return candidate . Counter > 0 ; }
11
+ public override Expression < Func < CounterModel , bool > > Predicate { get { return candidate => candidate . Counter > 0 ; } }
10
12
}
11
13
12
14
public class NegativeCounterRule : StateRule < CounterModel >
13
15
{
14
- public override bool IsSatisfied ( CounterModel candidate ) { return candidate . Counter < 0 ; }
16
+ public override Expression < Func < CounterModel , bool > > Predicate { get { return candidate => candidate . Counter < 0 ; } }
17
+ }
18
+
19
+ public class LessThanTenCounterRule : StateRule < CounterModel >
20
+ {
21
+ public override Expression < Func < CounterModel , bool > > Predicate { get { return candidate => candidate . Counter < 10 ; } }
15
22
}
16
23
17
24
public class StateRuleTests
@@ -24,14 +31,6 @@ public void SimpleSatisfiedRuleReturnsTrue()
24
31
Assert . True ( new PositiveCounterRule ( ) . IsSatisfied ( model ) ) ;
25
32
}
26
33
27
- [ Fact ]
28
- public void NotExtensionEvaluesCorrectly ( )
29
- {
30
- var model = new CounterModel ( ) ;
31
- model . Increment ( ) ;
32
- Assert . False ( new PositiveCounterRule ( ) . Not ( ) . IsSatisfied ( model ) ) ;
33
- }
34
-
35
34
[ Fact ]
36
35
public void DescribeExtensionSetsDescription ( )
37
36
{
@@ -56,7 +55,15 @@ public void AndExtensionEvaluatesCorrectly()
56
55
}
57
56
58
57
[ Fact ]
59
- public void AsExpressionExtensionFiltersCorrectly ( )
58
+ public void NotExtensionEvaluesCorrectly ( )
59
+ {
60
+ var model = new CounterModel ( ) ;
61
+ model . Increment ( ) ;
62
+ Assert . False ( new PositiveCounterRule ( ) . Not ( ) . IsSatisfied ( model ) ) ;
63
+ }
64
+
65
+ [ Fact ]
66
+ public void PredicateFiltersCorrectly ( )
60
67
{
61
68
var validCandidate = new CounterModel { Counter = 1 } ;
62
69
var invalidCandidate = new CounterModel { Counter = - 1 } ;
@@ -67,5 +74,44 @@ public void AsExpressionExtensionFiltersCorrectly()
67
74
Assert . Equal ( 1 , filtered . Count ) ;
68
75
Assert . Equal ( validCandidate . Id , filtered [ 0 ] . Id ) ;
69
76
}
77
+
78
+ [ Fact ]
79
+ public void AndPredicateFiltersCorrectly ( )
80
+ {
81
+ var validCandidate = new CounterModel { Counter = 1 } ;
82
+ var invalidCandidate = new CounterModel { Counter = - 1 } ;
83
+
84
+ var candidates = new List < CounterModel > { validCandidate , invalidCandidate } . AsQueryable ( ) ;
85
+ var filtered = candidates . Where ( new PositiveCounterRule ( ) . And ( new LessThanTenCounterRule ( ) ) . Predicate ) . ToList ( ) ;
86
+
87
+ Assert . Equal ( 1 , filtered . Count ) ;
88
+ Assert . Equal ( validCandidate . Id , filtered [ 0 ] . Id ) ;
89
+ }
90
+
91
+ [ Fact ]
92
+ public void OrPredicateFiltersCorrectly ( )
93
+ {
94
+ var validCandidate = new CounterModel { Counter = 1 } ;
95
+ var invalidCandidate = new CounterModel { Counter = 20 } ;
96
+
97
+ var candidates = new List < CounterModel > { validCandidate , invalidCandidate } . AsQueryable ( ) ;
98
+ var filtered = candidates . Where ( new NegativeCounterRule ( ) . Or ( new LessThanTenCounterRule ( ) ) . Predicate ) . ToList ( ) ;
99
+
100
+ Assert . Equal ( 1 , filtered . Count ) ;
101
+ Assert . Equal ( validCandidate . Id , filtered [ 0 ] . Id ) ;
102
+ }
103
+
104
+ [ Fact ]
105
+ public void NotPredicateFiltersCorrectly ( )
106
+ {
107
+ var validCandidate = new CounterModel { Counter = 1 } ;
108
+ var invalidCandidate = new CounterModel { Counter = - 1 } ;
109
+
110
+ var candidates = new List < CounterModel > { validCandidate , invalidCandidate } . AsQueryable ( ) ;
111
+ var filtered = candidates . Where ( new NegativeCounterRule ( ) . Not ( ) . Predicate ) . ToList ( ) ;
112
+
113
+ Assert . Equal ( 1 , filtered . Count ) ;
114
+ Assert . Equal ( validCandidate . Id , filtered [ 0 ] . Id ) ;
115
+ }
70
116
}
71
117
}
0 commit comments