-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: combine existing predicates (#227)
- Loading branch information
Showing
2 changed files
with
106 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
using Expressif.Predicates.Text; | ||
using Expressif.Predicates; | ||
|
||
namespace Expressif.Testing.Predicates; | ||
public class PredicateCombinerTest | ||
{ | ||
[Test] | ||
[TestCase("Nikola Tesla", true)] | ||
[TestCase("Albert Einstein", false)] | ||
public void Build_Combination_Success(string value, bool expected) | ||
{ | ||
var combiner = new PredicateCombiner(); | ||
var combination = combiner.With(new StartsWith(() => "Alb")) | ||
.Or(new EndsWith(() => "sla")) | ||
.And(new StartsWith(() => "Nik")); | ||
var combined = combination.Build(); | ||
Assert.That(combined.Evaluate(value), Is.EqualTo(expected)); | ||
} | ||
|
||
[Test] | ||
[TestCase("Nikola Tesla", true)] | ||
[TestCase("Albert Einstein", false)] | ||
public void Build_CombinationWithNot_Success(string value, bool expected) | ||
{ | ||
var combiner = new PredicateCombiner(); | ||
var combination = combiner.WithNot(new StartsWith(() => "Alb")) | ||
.Or(new EndsWith(() => "ring")) | ||
.And(new StartsWith(() => "Nik")); | ||
var combined = combination.Build(); | ||
Assert.That(combined.Evaluate(value), Is.EqualTo(expected)); | ||
} | ||
|
||
[Test] | ||
[TestCase("Nikola Tesla", true)] | ||
[TestCase("Albert Einstein", false)] | ||
public void Build_CombinationUsingNot_Success(string value, bool expected) | ||
{ | ||
var combiner = new PredicateCombiner(); | ||
var combination = combiner.With(new StartsWith(() => "Nik")) | ||
.Or(new EndsWith(() => "stein")) | ||
.AndNot(new StartsWith(() => "Alb")); | ||
var combined = combination.Build(); | ||
Assert.That(combined.Evaluate(value), Is.EqualTo(expected)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
using Expressif.Predicates.Operators; | ||
|
||
namespace Expressif.Predicates; | ||
public class PredicateCombiner | ||
{ | ||
protected UnaryOperatorFactory UnaryFactory { get; } | ||
protected BinaryOperatorFactory BinaryFactory { get; } | ||
|
||
public PredicateCombiner() | ||
: this(new(), new()) { } | ||
|
||
public PredicateCombiner(UnaryOperatorFactory unaryFactory , BinaryOperatorFactory binaryFactory) | ||
=> (UnaryFactory, BinaryFactory) = (unaryFactory, binaryFactory); | ||
|
||
public PredicateRightCombiner With(IPredicate left) | ||
=> new(UnaryFactory, BinaryFactory, left); | ||
|
||
public PredicateRightCombiner WithNot(IPredicate left) | ||
=> new(UnaryFactory, BinaryFactory, UnaryFactory.Instantiate("!", left)); | ||
|
||
public class PredicateRightCombiner | ||
{ | ||
protected UnaryOperatorFactory UnaryFactory { get; } | ||
protected BinaryOperatorFactory BinaryFactory { get; } | ||
private IPredicate State { get; } | ||
|
||
internal PredicateRightCombiner(UnaryOperatorFactory unaryFactory, BinaryOperatorFactory binaryFactory, IPredicate state) | ||
=> (UnaryFactory, BinaryFactory, State) = (unaryFactory, binaryFactory, state); | ||
|
||
public PredicateRightCombiner Or(IPredicate right) | ||
=> new(UnaryFactory, BinaryFactory, BinaryFactory.Instantiate("OR", State, right)); | ||
|
||
public PredicateRightCombiner OrNot(IPredicate right) | ||
=> Or(UnaryFactory.Instantiate("!", right)); | ||
|
||
public PredicateRightCombiner And(IPredicate right) | ||
=> new(UnaryFactory, BinaryFactory, BinaryFactory.Instantiate("AND", State, right)); | ||
|
||
public PredicateRightCombiner AndNot(IPredicate right) | ||
=> And(UnaryFactory.Instantiate("!", right)); | ||
|
||
public PredicateRightCombiner Xor(IPredicate right) | ||
=> new(UnaryFactory, BinaryFactory, BinaryFactory.Instantiate("XOR", State, right)); | ||
|
||
public PredicateRightCombiner XorNot(IPredicate right) | ||
=> Xor(UnaryFactory.Instantiate("!", right)); | ||
|
||
public IPredicate Build() | ||
=> State!; | ||
} | ||
} |