Skip to content

Commit f67ce6f

Browse files
author
Joseph Phillips
committed
Tests for expressions and filtering.
1 parent 39bf974 commit f67ce6f

File tree

2 files changed

+59
-22
lines changed

2 files changed

+59
-22
lines changed

src/Enflow.Base.Test/StateRuleTests.cs

Lines changed: 58 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,24 @@
1-
using System.Collections.Generic;
1+
using System;
2+
using System.Collections.Generic;
23
using System.Linq;
4+
using System.Linq.Expressions;
35
using Xunit;
46

57
namespace Enflow.Base.Test
68
{
79
public class PositiveCounterRule : StateRule<CounterModel>
810
{
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; } }
1012
}
1113

1214
public class NegativeCounterRule : StateRule<CounterModel>
1315
{
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; } }
1522
}
1623

1724
public class StateRuleTests
@@ -24,14 +31,6 @@ public void SimpleSatisfiedRuleReturnsTrue()
2431
Assert.True(new PositiveCounterRule().IsSatisfied(model));
2532
}
2633

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-
3534
[Fact]
3635
public void DescribeExtensionSetsDescription()
3736
{
@@ -56,7 +55,15 @@ public void AndExtensionEvaluatesCorrectly()
5655
}
5756

5857
[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()
6067
{
6168
var validCandidate = new CounterModel { Counter = 1 };
6269
var invalidCandidate = new CounterModel { Counter = -1 };
@@ -67,5 +74,44 @@ public void AsExpressionExtensionFiltersCorrectly()
6774
Assert.Equal(1, filtered.Count);
6875
Assert.Equal(validCandidate.Id, filtered[0].Id);
6976
}
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+
}
70116
}
71117
}

src/Enflow.Base.Test/TestingImplementations.cs

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-

2-
using System;
1+
using System;
32

43
namespace Enflow.Base.Test
54
{
@@ -18,12 +17,4 @@ public CounterIncrementWorkflow() { }
1817
public CounterIncrementWorkflow(IStateRule<CounterModel> preRule) : base(preRule) { }
1918
protected override void ExecuteWorkflow(CounterModel candidate) { candidate.Increment(); }
2019
}
21-
22-
public class LessThanTen : StateRule<CounterModel>
23-
{
24-
public override bool IsSatisfied(CounterModel candidate)
25-
{
26-
return candidate.Counter < 10;
27-
}
28-
}
2920
}

0 commit comments

Comments
 (0)