Skip to content

Commit e0df8c4

Browse files
authored
feat: combine existing predicates (#227)
1 parent 99f12f6 commit e0df8c4

File tree

2 files changed

+106
-0
lines changed

2 files changed

+106
-0
lines changed
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
using Expressif.Predicates.Text;
7+
using Expressif.Predicates;
8+
9+
namespace Expressif.Testing.Predicates;
10+
public class PredicateCombinerTest
11+
{
12+
[Test]
13+
[TestCase("Nikola Tesla", true)]
14+
[TestCase("Albert Einstein", false)]
15+
public void Build_Combination_Success(string value, bool expected)
16+
{
17+
var combiner = new PredicateCombiner();
18+
var combination = combiner.With(new StartsWith(() => "Alb"))
19+
.Or(new EndsWith(() => "sla"))
20+
.And(new StartsWith(() => "Nik"));
21+
var combined = combination.Build();
22+
Assert.That(combined.Evaluate(value), Is.EqualTo(expected));
23+
}
24+
25+
[Test]
26+
[TestCase("Nikola Tesla", true)]
27+
[TestCase("Albert Einstein", false)]
28+
public void Build_CombinationWithNot_Success(string value, bool expected)
29+
{
30+
var combiner = new PredicateCombiner();
31+
var combination = combiner.WithNot(new StartsWith(() => "Alb"))
32+
.Or(new EndsWith(() => "ring"))
33+
.And(new StartsWith(() => "Nik"));
34+
var combined = combination.Build();
35+
Assert.That(combined.Evaluate(value), Is.EqualTo(expected));
36+
}
37+
38+
[Test]
39+
[TestCase("Nikola Tesla", true)]
40+
[TestCase("Albert Einstein", false)]
41+
public void Build_CombinationUsingNot_Success(string value, bool expected)
42+
{
43+
var combiner = new PredicateCombiner();
44+
var combination = combiner.With(new StartsWith(() => "Nik"))
45+
.Or(new EndsWith(() => "stein"))
46+
.AndNot(new StartsWith(() => "Alb"));
47+
var combined = combination.Build();
48+
Assert.That(combined.Evaluate(value), Is.EqualTo(expected));
49+
}
50+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
using Expressif.Predicates.Operators;
7+
8+
namespace Expressif.Predicates;
9+
public class PredicateCombiner
10+
{
11+
protected UnaryOperatorFactory UnaryFactory { get; }
12+
protected BinaryOperatorFactory BinaryFactory { get; }
13+
14+
public PredicateCombiner()
15+
: this(new(), new()) { }
16+
17+
public PredicateCombiner(UnaryOperatorFactory unaryFactory , BinaryOperatorFactory binaryFactory)
18+
=> (UnaryFactory, BinaryFactory) = (unaryFactory, binaryFactory);
19+
20+
public PredicateRightCombiner With(IPredicate left)
21+
=> new(UnaryFactory, BinaryFactory, left);
22+
23+
public PredicateRightCombiner WithNot(IPredicate left)
24+
=> new(UnaryFactory, BinaryFactory, UnaryFactory.Instantiate("!", left));
25+
26+
public class PredicateRightCombiner
27+
{
28+
protected UnaryOperatorFactory UnaryFactory { get; }
29+
protected BinaryOperatorFactory BinaryFactory { get; }
30+
private IPredicate State { get; }
31+
32+
internal PredicateRightCombiner(UnaryOperatorFactory unaryFactory, BinaryOperatorFactory binaryFactory, IPredicate state)
33+
=> (UnaryFactory, BinaryFactory, State) = (unaryFactory, binaryFactory, state);
34+
35+
public PredicateRightCombiner Or(IPredicate right)
36+
=> new(UnaryFactory, BinaryFactory, BinaryFactory.Instantiate("OR", State, right));
37+
38+
public PredicateRightCombiner OrNot(IPredicate right)
39+
=> Or(UnaryFactory.Instantiate("!", right));
40+
41+
public PredicateRightCombiner And(IPredicate right)
42+
=> new(UnaryFactory, BinaryFactory, BinaryFactory.Instantiate("AND", State, right));
43+
44+
public PredicateRightCombiner AndNot(IPredicate right)
45+
=> And(UnaryFactory.Instantiate("!", right));
46+
47+
public PredicateRightCombiner Xor(IPredicate right)
48+
=> new(UnaryFactory, BinaryFactory, BinaryFactory.Instantiate("XOR", State, right));
49+
50+
public PredicateRightCombiner XorNot(IPredicate right)
51+
=> Xor(UnaryFactory.Instantiate("!", right));
52+
53+
public IPredicate Build()
54+
=> State!;
55+
}
56+
}

0 commit comments

Comments
 (0)