diff --git a/SuccincT/Functional/ConsListBuilderEnumerator.cs b/SuccincT/Functional/ConsListBuilderEnumerator.cs index bd08b22..ad717fe 100644 --- a/SuccincT/Functional/ConsListBuilderEnumerator.cs +++ b/SuccincT/Functional/ConsListBuilderEnumerator.cs @@ -5,13 +5,12 @@ namespace SuccincT.Functional { - internal sealed class ConsListBuilderEnumerator : IEnumerator where - TConsNode : ConsNode, new() + internal sealed class ConsListBuilderEnumerator : IEnumerator> { private readonly IEnumerator _enumerator; - private TConsNode _node; + private ConsNode _node; - internal ConsListBuilderEnumerator(TConsNode node) + internal ConsListBuilderEnumerator(ConsNode node) { _enumerator = node.Enumeration.GetEnumerator(); } @@ -20,7 +19,7 @@ public bool MoveNext() { if (!_enumerator.MoveNext()) return false; - _node = new TConsNode + _node = new ConsNode { Value = _enumerator.Current, State = HasValue @@ -31,7 +30,7 @@ public bool MoveNext() public void Reset() { throw new NotSupportedException(); } - public TConsNode Current => _node; + public ConsNode Current => _node; object IEnumerator.Current => Current; diff --git a/SuccincT/Functional/ConsNode.cs b/SuccincT/Functional/ConsNode.cs index 230c1f7..7672098 100644 --- a/SuccincT/Functional/ConsNode.cs +++ b/SuccincT/Functional/ConsNode.cs @@ -2,7 +2,7 @@ namespace SuccincT.Functional { - internal class ConsNode + internal sealed class ConsNode { internal ConsNodeState State { get; set; } internal IEnumerable Enumeration { get; set; } diff --git a/SuccincT/Functional/ConsNodeEnumerator.cs b/SuccincT/Functional/ConsNodeEnumerator.cs index ca5d251..70c9a2d 100644 --- a/SuccincT/Functional/ConsNodeEnumerator.cs +++ b/SuccincT/Functional/ConsNodeEnumerator.cs @@ -20,7 +20,7 @@ public bool MoveNext() if (_node.State == HasEnumeration) { - _node.Enumerating(new ConsListBuilderEnumerator, T>(_node)); + _node.Enumerating(new ConsListBuilderEnumerator(_node)); } if (_node.State == HasValue) return true; diff --git a/SuccincT/PatternMatchers/EitherTuple.cs b/SuccincT/PatternMatchers/EitherTuple.cs index b6338be..2fed2a8 100644 --- a/SuccincT/PatternMatchers/EitherTuple.cs +++ b/SuccincT/PatternMatchers/EitherTuple.cs @@ -1,41 +1,23 @@ -namespace SuccincT.PatternMatchers +using SuccincT.Unions; + +namespace SuccincT.PatternMatchers { internal static class EitherTuple { - public static EitherTuple Create(T1 value1, T2 value2) => - new EitherTuple(value1, value2); - - public static EitherTuple Create(Any value1, T2 value2) => - new EitherTuple(value1, value2); - - public static EitherTuple Create(T1 value1, Any value2) => + public static EitherTuple Create(Either value1, + Either value2) => new EitherTuple(value1, value2); - public static EitherTuple Create(Any value1, Any value2) => - new EitherTuple(value1, value2); - - public static EitherTuple Create(T1 value1, T2 value2, T3 value3) => - new EitherTuple(value1, value2, value3); - - public static EitherTuple Create(Any value1, T2 value2, T3 value3) => - new EitherTuple(value1, value2, value3); - public static EitherTuple Create(T1 value1, Any value2, T3 value3) => + public static EitherTuple Create(Either value1, + Either value2, + Either value3) => new EitherTuple(value1, value2, value3); - public static EitherTuple Create(T1 value1, T2 value2, Any value3) => - new EitherTuple(value1, value2, value3); - - public static EitherTuple Create(Any value1, Any value2, T3 value3) => - new EitherTuple(value1, value2, value3); - - public static EitherTuple Create(Any value1, T2 value2, Any value3) => - new EitherTuple(value1, value2, value3); - - public static EitherTuple Create(T1 value1, Any value2, Any value3) => - new EitherTuple(value1, value2, value3); - - public static EitherTuple Create(Any value1, Any value2, Any value3) => - new EitherTuple(value1, value2, value3); + public static EitherTuple Create(Either value1, + Either value2, + Either value3, + Either value4) => + new EitherTuple(value1, value2, value3, value4); } } \ No newline at end of file diff --git a/SuccincT/PatternMatchers/EitherTuple{T1,T2,T3,T4}.cs b/SuccincT/PatternMatchers/EitherTuple{T1,T2,T3,T4}.cs new file mode 100644 index 0000000..9c2d446 --- /dev/null +++ b/SuccincT/PatternMatchers/EitherTuple{T1,T2,T3,T4}.cs @@ -0,0 +1,31 @@ +using System; +using System.Collections.Generic; +using SuccincT.Unions; + +namespace SuccincT.PatternMatchers +{ + internal struct EitherTuple + { + private readonly Either _value1; + private readonly Either _value2; + private readonly Either _value3; + private readonly Either _value4; + + internal EitherTuple(Either value1, + Either value2, + Either value3, + Either value4) + { + _value1 = value1; + _value2 = value2; + _value3 = value3; + _value4 = value4; + } + + public bool MatchesTuple(Tuple tuple) => + (!_value1.IsLeft || EqualityComparer.Default.Equals(_value1.Left, tuple.Item1)) && + (!_value2.IsLeft || EqualityComparer.Default.Equals(_value2.Left, tuple.Item2)) && + (!_value3.IsLeft || EqualityComparer.Default.Equals(_value3.Left, tuple.Item3)) && + (!_value4.IsLeft || EqualityComparer.Default.Equals(_value4.Left, tuple.Item4)); + } +} diff --git a/SuccincT/PatternMatchers/EitherTuple{T1,T2,T3}.cs b/SuccincT/PatternMatchers/EitherTuple{T1,T2,T3}.cs index b963502..5c6b6c6 100644 --- a/SuccincT/PatternMatchers/EitherTuple{T1,T2,T3}.cs +++ b/SuccincT/PatternMatchers/EitherTuple{T1,T2,T3}.cs @@ -1,69 +1,22 @@ using System; using System.Collections.Generic; -using SuccincT.Functional; +using SuccincT.Unions; namespace SuccincT.PatternMatchers { internal struct EitherTuple { - private Either _value1; - private Either _value2; - private Either _value3; + private readonly Either _value1; + private readonly Either _value2; + private readonly Either _value3; - public EitherTuple(T1 value1, T2 value2, T3 value3) + public EitherTuple(Either value1, + Either value2, + Either value3) { - _value1 = new Either(value1); - _value2 = new Either(value2); - _value3 = new Either(value3); - } - - public EitherTuple(Any value1, T2 value2, T3 value3) - { - _value1 = new Either(value1); - _value2 = new Either(value2); - _value3 = new Either(value3); - } - - public EitherTuple(T1 value1, Any value2, T3 value3) - { - _value1 = new Either(value1); - _value2 = new Either(value2); - _value3 = new Either(value3); - } - - public EitherTuple(T1 value1, T2 value2, Any value3) - { - _value1 = new Either(value1); - _value2 = new Either(value2); - _value3 = new Either(value3); - } - - public EitherTuple(Any value1, Any value2, T3 value3) - { - _value1 = new Either(value1); - _value2 = new Either(value2); - _value3 = new Either(value3); - } - - public EitherTuple(Any value1, T2 value2, Any value3) - { - _value1 = new Either(value1); - _value2 = new Either(value2); - _value3 = new Either(value3); - } - - public EitherTuple(T1 value1, Any value2, Any value3) - { - _value1 = new Either(value1); - _value2 = new Either(value2); - _value3 = new Either(value3); - } - - public EitherTuple(Any value1, Any value2, Any value3) - { - _value1 = new Either(value1); - _value2 = new Either(value2); - _value3 = new Either(value3); + _value1 = value1; + _value2 = value2; + _value3 = value3; } public bool MatchesTuple(Tuple tuple) => diff --git a/SuccincT/PatternMatchers/EitherTuple{T1,T2}.cs b/SuccincT/PatternMatchers/EitherTuple{T1,T2}.cs index 03873ea..4ddb82b 100644 --- a/SuccincT/PatternMatchers/EitherTuple{T1,T2}.cs +++ b/SuccincT/PatternMatchers/EitherTuple{T1,T2}.cs @@ -1,36 +1,18 @@ using System; using System.Collections.Generic; -using SuccincT.Functional; +using SuccincT.Unions; namespace SuccincT.PatternMatchers { internal struct EitherTuple { - private Either _value1; - private Either _value2; + private readonly Either _value1; + private readonly Either _value2; - public EitherTuple(T1 value1, T2 value2) + public EitherTuple(Either value1, Either value2) { - _value1 = new Either(value1); - _value2 = new Either(value2); - } - - public EitherTuple(Any value1, T2 value2) - { - _value1 = new Either(value1); - _value2 = new Either(value2); - } - - public EitherTuple(T1 value1, Any value2) - { - _value1 = new Either(value1); - _value2 = new Either(value2); - } - - public EitherTuple(Any value1, Any value2) - { - _value1 = new Either(value1); - _value2 = new Either(value2); + _value1 = value1; + _value2 = value2; } public bool MatchesTuple(Tuple tuple) => diff --git a/SuccincT/PatternMatchers/IActionMatcher{T1,T2,T3,T4}.cs b/SuccincT/PatternMatchers/IActionMatcher{T1,T2,T3,T4}.cs index 593ad55..6830ea2 100644 --- a/SuccincT/PatternMatchers/IActionMatcher{T1,T2,T3,T4}.cs +++ b/SuccincT/PatternMatchers/IActionMatcher{T1,T2,T3,T4}.cs @@ -1,10 +1,14 @@ using System; +using SuccincT.Unions; namespace SuccincT.PatternMatchers { public interface IActionMatcher { - IActionWithHandler, T1, T2, T3, T4> With(T1 value1, T2 value2, T3 value3, T4 value4); + IActionWithHandler, T1, T2, T3, T4> With(Either value1, + Either value2, + Either value3, + Either value4); IActionWhereHandler, T1, T2, T3, T4> Where(Func expression); IActionMatcherAfterElse Else(Action action); IActionMatcherAfterElse IgnoreElse(); diff --git a/SuccincT/PatternMatchers/IActionMatcher{T1,T2,T3}.cs b/SuccincT/PatternMatchers/IActionMatcher{T1,T2,T3}.cs index 463aa8f..a6b6f76 100644 --- a/SuccincT/PatternMatchers/IActionMatcher{T1,T2,T3}.cs +++ b/SuccincT/PatternMatchers/IActionMatcher{T1,T2,T3}.cs @@ -1,17 +1,14 @@ using System; +using SuccincT.Unions; namespace SuccincT.PatternMatchers { public interface IActionMatcher { - IActionWithHandler, T1, T2, T3> With(T1 value1, T2 value2, T3 value3); - IActionWithHandler, T1, T2, T3> With(Any value1, T2 value2, T3 value3); - IActionWithHandler, T1, T2, T3> With(T1 value1, Any value2, T3 value3); - IActionWithHandler, T1, T2, T3> With(T1 value1, T2 value2, Any value3); - IActionWithHandler, T1, T2, T3> With(Any value1, Any value2, T3 value3); - IActionWithHandler, T1, T2, T3> With(Any value1, T2 value2, Any value3); - IActionWithHandler, T1, T2, T3> With(T1 value1, Any value2, Any value3); - IActionWithHandler, T1, T2, T3> With(Any value1, Any value2, Any value3); + IActionWithHandler, T1, T2, T3> With(Either value1, + Either value2, + Either value3); + IActionWhereHandler, T1, T2, T3> Where(Func expression); IActionMatcherAfterElse Else(Action action); diff --git a/SuccincT/PatternMatchers/IActionMatcher{T1,T2}.cs b/SuccincT/PatternMatchers/IActionMatcher{T1,T2}.cs index b0181ba..a3bce57 100644 --- a/SuccincT/PatternMatchers/IActionMatcher{T1,T2}.cs +++ b/SuccincT/PatternMatchers/IActionMatcher{T1,T2}.cs @@ -1,13 +1,11 @@ using System; +using SuccincT.Unions; namespace SuccincT.PatternMatchers { public interface IActionMatcher { - IActionWithHandler, T1, T2> With(T1 value1, T2 value2); - IActionWithHandler, T1, T2> With(Any value1, T2 value2); - IActionWithHandler, T1, T2> With(T1 value1, Any value2); - IActionWithHandler, T1, T2> With(Any value1, Any value2); + IActionWithHandler, T1, T2> With(Either value1, Either value2); IActionWhereHandler, T1, T2> Where(Func expression); IActionMatcherAfterElse Else(Action action); IActionMatcherAfterElse IgnoreElse(); diff --git a/SuccincT/PatternMatchers/IActionWithHandler{TM,T1,T2,T3,T4}.cs b/SuccincT/PatternMatchers/IActionWithHandler{TM,T1,T2,T3,T4}.cs index 116f51b..72ca764 100644 --- a/SuccincT/PatternMatchers/IActionWithHandler{TM,T1,T2,T3,T4}.cs +++ b/SuccincT/PatternMatchers/IActionWithHandler{TM,T1,T2,T3,T4}.cs @@ -1,10 +1,14 @@ using System; +using SuccincT.Unions; namespace SuccincT.PatternMatchers { public interface IActionWithHandler { - IActionWithHandler Or(T1 value1, T2 value2, T3 value3, T4 value4); + IActionWithHandler Or(Either value1, + Either value2, + Either value3, + Either value4); TMatcher Do(Action action); } diff --git a/SuccincT/PatternMatchers/IActionWithHandler{TM,T1,T2,T3}.cs b/SuccincT/PatternMatchers/IActionWithHandler{TM,T1,T2,T3}.cs index 700e54a..647bf84 100644 --- a/SuccincT/PatternMatchers/IActionWithHandler{TM,T1,T2,T3}.cs +++ b/SuccincT/PatternMatchers/IActionWithHandler{TM,T1,T2,T3}.cs @@ -1,17 +1,13 @@ using System; +using SuccincT.Unions; namespace SuccincT.PatternMatchers { public interface IActionWithHandler { - IActionWithHandler Or(T1 value1, T2 value2, T3 value3); - IActionWithHandler Or(Any value1, T2 value2, T3 value3); - IActionWithHandler Or(T1 value1, Any value2, T3 value3); - IActionWithHandler Or(T1 value1, T2 value2, Any value3); - IActionWithHandler Or(Any value1, Any value2, T3 value3); - IActionWithHandler Or(Any value1, T2 value2, Any value3); - IActionWithHandler Or(T1 value1, Any value2, Any value3); - IActionWithHandler Or(Any value1, Any value2, Any value3); + IActionWithHandler Or(Either value1, + Either value2, + Either value3); TMatcher Do(Action action); } diff --git a/SuccincT/PatternMatchers/IActionWithHandler{TM,T1,T2}.cs b/SuccincT/PatternMatchers/IActionWithHandler{TM,T1,T2}.cs index 8bbfea7..7447f92 100644 --- a/SuccincT/PatternMatchers/IActionWithHandler{TM,T1,T2}.cs +++ b/SuccincT/PatternMatchers/IActionWithHandler{TM,T1,T2}.cs @@ -1,13 +1,11 @@ using System; +using SuccincT.Unions; namespace SuccincT.PatternMatchers { public interface IActionWithHandler { - IActionWithHandler Or(T1 value1, T2 value2); - IActionWithHandler Or(T1 value1, Any value2); - IActionWithHandler Or(Any value1, T2 value2); - IActionWithHandler Or(Any value1, Any value2); + IActionWithHandler Or(Either value1, Either value2); TMatcher Do(Action action); } diff --git a/SuccincT/PatternMatchers/IFuncMatcher{T1,T2,T3,T4,TR}.cs b/SuccincT/PatternMatchers/IFuncMatcher{T1,T2,T3,T4,TR}.cs index 5ef8f12..b493670 100644 --- a/SuccincT/PatternMatchers/IFuncMatcher{T1,T2,T3,T4,TR}.cs +++ b/SuccincT/PatternMatchers/IFuncMatcher{T1,T2,T3,T4,TR}.cs @@ -1,13 +1,14 @@ using System; +using SuccincT.Unions; namespace SuccincT.PatternMatchers { public interface IFuncMatcher { - IFuncWithHandler, T1, T2, T3, T4, TResult> With(T1 value1, - T2 value2, - T3 value3, - T4 value4); + IFuncWithHandler, T1, T2, T3, T4, TResult> With(Either value1, + Either value2, + Either value3, + Either value4); IFuncWhereHandler, T1, T2, T3, T4, TResult> Where( Func expression); diff --git a/SuccincT/PatternMatchers/IFuncMatcher{T1,T2,T3,TR}.cs b/SuccincT/PatternMatchers/IFuncMatcher{T1,T2,T3,TR}.cs index f664c5f..6a6371d 100644 --- a/SuccincT/PatternMatchers/IFuncMatcher{T1,T2,T3,TR}.cs +++ b/SuccincT/PatternMatchers/IFuncMatcher{T1,T2,T3,TR}.cs @@ -1,17 +1,13 @@ using System; +using SuccincT.Unions; namespace SuccincT.PatternMatchers { public interface IFuncMatcher { - IFuncWithHandler, T1, T2, T3, TResult> With(T1 value1, T2 value2, T3 value3); - IFuncWithHandler, T1, T2, T3, TResult> With(Any value1, T2 value2, T3 value3); - IFuncWithHandler, T1, T2, T3, TResult> With(T1 value1, Any value2, T3 value3); - IFuncWithHandler, T1, T2, T3, TResult> With(T1 value1, T2 value2, Any value3); - IFuncWithHandler, T1, T2, T3, TResult> With(Any value1, Any value2, T3 value3); - IFuncWithHandler, T1, T2, T3, TResult> With(Any value1, T2 value2, Any value3); - IFuncWithHandler, T1, T2, T3, TResult> With(T1 value1, Any value2, Any value3); - IFuncWithHandler, T1, T2, T3, TResult> With(Any value1, Any value2, Any value3); + IFuncWithHandler, T1, T2, T3, TResult> With(Either value1, + Either value2, + Either value3); IFuncWhereHandler, T1, T2, T3, TResult> Where( Func expression); diff --git a/SuccincT/PatternMatchers/IFuncMatcher{T1,T2,TR}.cs b/SuccincT/PatternMatchers/IFuncMatcher{T1,T2,TR}.cs index 8dde2ce..532a5de 100644 --- a/SuccincT/PatternMatchers/IFuncMatcher{T1,T2,TR}.cs +++ b/SuccincT/PatternMatchers/IFuncMatcher{T1,T2,TR}.cs @@ -1,13 +1,12 @@ using System; +using SuccincT.Unions; namespace SuccincT.PatternMatchers { public interface IFuncMatcher { - IFuncWithHandler, T1, T2, TResult> With(T1 value1, T2 value2); - IFuncWithHandler, T1, T2, TResult> With(Any value1, T2 value2); - IFuncWithHandler, T1, T2, TResult> With(T1 value1, Any value2); - IFuncWithHandler, T1, T2, TResult> With(Any value1, Any value2); + IFuncWithHandler, T1, T2, TResult> With(Either value1, + Either value2); IFuncWhereHandler, T1, T2, TResult> Where(Func expression); IFuncMatcherAfterElse Else(Func function); diff --git a/SuccincT/PatternMatchers/IFuncWithHandler{TM,T1,T2,T3,T4,TR}.cs b/SuccincT/PatternMatchers/IFuncWithHandler{TM,T1,T2,T3,T4,TR}.cs index f8589e5..60226df 100644 --- a/SuccincT/PatternMatchers/IFuncWithHandler{TM,T1,T2,T3,T4,TR}.cs +++ b/SuccincT/PatternMatchers/IFuncWithHandler{TM,T1,T2,T3,T4,TR}.cs @@ -1,10 +1,14 @@ using System; +using SuccincT.Unions; namespace SuccincT.PatternMatchers { public interface IFuncWithHandler { - IFuncWithHandler Or(T1 value1, T2 value2, T3 value3, T4 value4); + IFuncWithHandler Or(Either value1, + Either value2, + Either value3, + Either value4); TMatcher Do(Func function); diff --git a/SuccincT/PatternMatchers/IFuncWithHandler{TM,T1,T2,T3,TR}.cs b/SuccincT/PatternMatchers/IFuncWithHandler{TM,T1,T2,T3,TR}.cs index ca6155e..bde1830 100644 --- a/SuccincT/PatternMatchers/IFuncWithHandler{TM,T1,T2,T3,TR}.cs +++ b/SuccincT/PatternMatchers/IFuncWithHandler{TM,T1,T2,T3,TR}.cs @@ -1,17 +1,13 @@ using System; +using SuccincT.Unions; namespace SuccincT.PatternMatchers { public interface IFuncWithHandler { - IFuncWithHandler Or(T1 value1, T2 value2, T3 value3); - IFuncWithHandler Or(Any value1, T2 value2, T3 value3); - IFuncWithHandler Or(T1 value1, Any value2, T3 value3); - IFuncWithHandler Or(T1 value1, T2 value2, Any value3); - IFuncWithHandler Or(Any value1, Any value2, T3 value3); - IFuncWithHandler Or(Any value1, T2 value2, Any value3); - IFuncWithHandler Or(T1 value1, Any value2, Any value3); - IFuncWithHandler Or(Any value1, Any value2, Any value3); + IFuncWithHandler Or(Either value1, + Either value2, + Either value3); TMatcher Do(Func function); diff --git a/SuccincT/PatternMatchers/IFuncWithHandler{TM,T1,T2,TR}.cs b/SuccincT/PatternMatchers/IFuncWithHandler{TM,T1,T2,TR}.cs index 288b9e1..240a761 100644 --- a/SuccincT/PatternMatchers/IFuncWithHandler{TM,T1,T2,TR}.cs +++ b/SuccincT/PatternMatchers/IFuncWithHandler{TM,T1,T2,TR}.cs @@ -1,13 +1,11 @@ using System; +using SuccincT.Unions; namespace SuccincT.PatternMatchers { public interface IFuncWithHandler { - IFuncWithHandler Or(T1 value1, T2 value2); - IFuncWithHandler Or(T1 value1, Any value2); - IFuncWithHandler Or(Any value1, T2 value2); - IFuncWithHandler Or(Any value1, Any value2); + IFuncWithHandler Or(Either value1, Either value2); TMatcher Do(Func function); diff --git a/SuccincT/PatternMatchers/IMatcher{T1,T2,T3,T4}.cs b/SuccincT/PatternMatchers/IMatcher{T1,T2,T3,T4}.cs index e26cdbb..2d6e071 100644 --- a/SuccincT/PatternMatchers/IMatcher{T1,T2,T3,T4}.cs +++ b/SuccincT/PatternMatchers/IMatcher{T1,T2,T3,T4}.cs @@ -1,13 +1,14 @@ using System; +using SuccincT.Unions; namespace SuccincT.PatternMatchers { public interface IMatcher { - IActionWithHandler, T1, T2, T3, T4> With(T1 value1, - T2 value2, - T3 value3, - T4 value4); + IActionWithHandler, T1, T2, T3, T4> With(Either value1, + Either value2, + Either value3, + Either value4); IActionWhereHandler, T1, T2, T3, T4> Where(Func function); IFuncMatcher To(); diff --git a/SuccincT/PatternMatchers/IMatcher{T1,T2,T3}.cs b/SuccincT/PatternMatchers/IMatcher{T1,T2,T3}.cs index 41b29d2..03bc4e4 100644 --- a/SuccincT/PatternMatchers/IMatcher{T1,T2,T3}.cs +++ b/SuccincT/PatternMatchers/IMatcher{T1,T2,T3}.cs @@ -1,17 +1,13 @@ using System; +using SuccincT.Unions; namespace SuccincT.PatternMatchers { public interface IMatcher { - IActionWithHandler, T1, T2, T3> With(T1 value1, T2 value2, T3 value3); - IActionWithHandler, T1, T2, T3> With(Any value1, T2 value2, T3 value3); - IActionWithHandler, T1, T2, T3> With(T1 value1, Any value2, T3 value3); - IActionWithHandler, T1, T2, T3> With(T1 value1, T2 value2, Any value3); - IActionWithHandler, T1, T2, T3> With(Any value1, Any value2, T3 value3); - IActionWithHandler, T1, T2, T3> With(Any value1, T2 value2, Any value3); - IActionWithHandler, T1, T2, T3> With(T1 value1, Any value2, Any value3); - IActionWithHandler, T1, T2, T3> With(Any value1, Any value2, Any value3); + IActionWithHandler, T1, T2, T3> With(Either value1, + Either value2, + Either value3); IActionWhereHandler, T1, T2, T3> Where(Func function); IFuncMatcher To(); diff --git a/SuccincT/PatternMatchers/IMatcher{T1,T2}.cs b/SuccincT/PatternMatchers/IMatcher{T1,T2}.cs index c2cf1e0..88335d3 100644 --- a/SuccincT/PatternMatchers/IMatcher{T1,T2}.cs +++ b/SuccincT/PatternMatchers/IMatcher{T1,T2}.cs @@ -1,13 +1,11 @@ using System; +using SuccincT.Unions; namespace SuccincT.PatternMatchers { public interface IMatcher { - IActionWithHandler, T1, T2> With(T1 value1, T2 value2); - IActionWithHandler, T1, T2> With(Any value1, T2 value2); - IActionWithHandler, T1, T2> With(T1 value1, Any value2); - IActionWithHandler, T1, T2> With(Any value1, Any value2); + IActionWithHandler, T1, T2> With(Either value1, Either value2); IActionWhereHandler, T1, T2> Where(Func function); IFuncMatcher To(); } diff --git a/SuccincT/PatternMatchers/Matcher{T1,T2,T3,T4,TR}.cs b/SuccincT/PatternMatchers/Matcher{T1,T2,T3,T4,TR}.cs index 486a8c8..57a979d 100644 --- a/SuccincT/PatternMatchers/Matcher{T1,T2,T3,T4,TR}.cs +++ b/SuccincT/PatternMatchers/Matcher{T1,T2,T3,T4,TR}.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.Linq; +using SuccincT.Unions; using static SuccincT.Functional.Unit; namespace SuccincT.PatternMatchers @@ -16,42 +17,54 @@ internal sealed class Matcher : IFuncWhereHandler, T1, T2, T3, T4, TResult>, IFuncMatcherAfterElse { - private readonly MatchFunctionSelector, Tuple, TResult> _functionSelector; + private readonly MatchFunctionSelector, EitherTuple, TResult> + _functionSelector; + private readonly Tuple _item; - private IList> _withValues; + private IList> _withValues; private Func, bool> _whereExpression; private Func, TResult> _elseFunction; internal Matcher(Tuple item) { _item = item; - _functionSelector = new MatchFunctionSelector, Tuple, TResult>(x => - { - throw new NoMatchException( - $"No match action exists for value of ({_item.Item1}, {_item.Item2}, {_item.Item3}, {_item.Item4}"); - }); + _functionSelector = + new MatchFunctionSelector, EitherTuple, TResult>(x => + { + throw new NoMatchException("No match action exists for value of " + + $"({_item.Item1}, {_item.Item2}, {_item.Item3}, {_item.Item4}"); + }); } IFuncMatcher IMatcher.To() => new Matcher(_item); IActionWithHandler, T1, T2, T3, T4> IMatcher.With( - T1 value1, T2 value2, T3 value3, T4 value4) + Either value1, + Either value2, + Either value3, + Either value4) { - _withValues = new List> { Tuple.Create(value1, value2, value3, value4) }; + _withValues = new List> {EitherTuple.Create(value1, value2, value3, value4)}; return this; } IActionWithHandler, T1, T2, T3, T4> IActionMatcher.With( - T1 value1, T2 value2, T3 value3, T4 value4) + Either value1, + Either value2, + Either value3, + Either value4) { - _withValues = new List> { Tuple.Create(value1, value2, value3, value4) }; + _withValues = new List> { EitherTuple.Create(value1, value2, value3, value4) }; return this; } IFuncWithHandler, T1, T2, T3, T4, TResult> - IFuncMatcher.With(T1 value1, T2 value2, T3 value3, T4 value4) + IFuncMatcher.With(Either value1, + Either value2, + Either value3, + Either value4) { - _withValues = new List> { Tuple.Create(value1, value2, value3, value4) }; + _withValues = new List> {EitherTuple.Create(value1, value2, value3, value4)}; return this; } @@ -101,19 +114,19 @@ IFuncMatcherAfterElse IFuncMatcher.Else(TResul } IFuncWithHandler, T1, T2, T3, T4, TResult> - IFuncWithHandler, T1, T2, T3, T4, TResult>.Or( - T1 value1, T2 value2, T3 value3, T4 value4) + IFuncWithHandler, T1, T2, T3, T4, TResult>.Or(Either value1, + Either value2, + Either value3, + Either value4) { - _withValues.Add(Tuple.Create(value1, value2, value3, value4)); + _withValues.Add(EitherTuple.Create(value1, value2, value3, value4)); return this; } IActionMatcher IActionWithHandler, T1, T2, T3, T4>.Do( Action action) { - RecordFunction((x, y) => y.Any(v => EqualityComparer>.Default.Equals(x, v)), - _withValues, - ActionToFunc(action)); + RecordFunction((x, y) => y.Any(v => v.MatchesTuple(x)), _withValues, ActionToFunc(action)); return this; } @@ -127,7 +140,7 @@ IActionMatcher IActionWhereHandler IFuncWithHandler, T1, T2, T3, T4, TResult>.Do( Func function) { - RecordFunction((x, y) => y.Any(v => EqualityComparer>.Default.Equals(x, v)), + RecordFunction((x, y) => y.Any(v => v.MatchesTuple(x)), _withValues, x => function(x.Item1, x.Item2, x.Item3, x.Item4)); return this; @@ -136,9 +149,7 @@ IFuncMatcher IFuncWithHandler IFuncWithHandler, T1, T2, T3, T4, TResult>.Do( TResult value) { - RecordFunction((x, y) => y.Any(v => EqualityComparer>.Default.Equals(x, v)), - _withValues, - x => value); + RecordFunction((x, y) => y.Any(v => v.MatchesTuple(x)), _withValues, x => value); return this; } @@ -173,8 +184,8 @@ TResult IFuncMatcherAfterElse.Result() return possibleResult.HasValue ? possibleResult.Value : _elseFunction(_item); } - private void RecordFunction(Func, IList>, bool> test, - IList> values, + private void RecordFunction(Func, IList>, bool> test, + IList> values, Func, TResult> function) { _functionSelector.AddTestAndAction(test, values, null, function); @@ -191,10 +202,12 @@ private static Func, TResult> ActionToFunc(Action, T1, T2, T3, T4> - IActionWithHandler, T1, T2, T3, T4>.Or( - T1 value1, T2 value2, T3 value3, T4 value4) + IActionWithHandler, T1, T2, T3, T4>.Or(Either value1, + Either value2, + Either value3, + Either value4) { - _withValues.Add(Tuple.Create(value1, value2, value3, value4)); + _withValues.Add(EitherTuple.Create(value1, value2, value3, value4)); return this; } } diff --git a/SuccincT/PatternMatchers/Matcher{T1,T2,T3,TR}.cs b/SuccincT/PatternMatchers/Matcher{T1,T2,T3,TR}.cs index bf74c69..3506e31 100644 --- a/SuccincT/PatternMatchers/Matcher{T1,T2,T3,TR}.cs +++ b/SuccincT/PatternMatchers/Matcher{T1,T2,T3,TR}.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.Linq; +using SuccincT.Unions; using static SuccincT.Functional.Unit; namespace SuccincT.PatternMatchers @@ -34,206 +35,32 @@ internal Matcher(Tuple item) IFuncMatcher IMatcher.To() => new Matcher(_item); - IActionWithHandler, T1, T2, T3> IMatcher.With(T1 value1, - T2 value2, - T3 value3) + IActionWithHandler, T1, T2, T3> IMatcher.With(Either value1, + Either value2, + Either value3) { _withValues = new List> { EitherTuple.Create(value1, value2, value3) }; return this; } - IActionWithHandler, T1, T2, T3> IMatcher.With(Any value1, - T2 value2, - T3 value3) + IActionWithHandler, T1, T2, T3> IActionMatcher.With( + Either value1, + Either value2, + Either value3) { - _withValues = new List> { EitherTuple.Create(value1, value2, value3) }; - return this; - } - - IActionWithHandler, T1, T2, T3> IMatcher.With(T1 value1, - Any value2, - T3 value3) - { - _withValues = new List> { EitherTuple.Create(value1, value2, value3) }; - return this; - } - - IActionWithHandler, T1, T2, T3> IMatcher.With(T1 value1, - T2 value2, - Any value3) - { - _withValues = new List> { EitherTuple.Create(value1, value2, value3) }; - return this; - } - - IActionWithHandler, T1, T2, T3> IActionMatcher.With(T1 value1, - T2 value2, - T3 value3) - { - _withValues = new List> { EitherTuple.Create(value1, value2, value3) }; - return this; - } - - IActionWithHandler, T1, T2, T3> IActionMatcher.With(Any value1, - T2 value2, - T3 value3) - { - _withValues = new List> { EitherTuple.Create(value1, value2, value3) }; - return this; - } - - IActionWithHandler, T1, T2, T3> IActionMatcher.With(T1 value1, - Any value2, - T3 value3) - { - _withValues = new List> { EitherTuple.Create(value1, value2, value3) }; - return this; - } - - IActionWithHandler, T1, T2, T3> IActionMatcher.With(T1 value1, - T2 value2, - Any value3) - { - _withValues = new List> { EitherTuple.Create(value1, value2, value3) }; - return this; - } - - IActionWithHandler, T1, T2, T3> IActionMatcher.With(Any value1, - Any value2, - T3 value3) - { - _withValues = new List> { EitherTuple.Create(value1, value2, value3) }; - return this; - } - - IActionWithHandler, T1, T2, T3> IActionMatcher.With(Any value1, - T2 value2, - Any value3) - { - _withValues = new List> { EitherTuple.Create(value1, value2, value3) }; - return this; - } - - IActionWithHandler, T1, T2, T3> IActionMatcher.With(T1 value1, - Any value2, - Any value3) - { - _withValues = new List> { EitherTuple.Create(value1, value2, value3) }; - return this; - } - - IActionWithHandler, T1, T2, T3> IActionMatcher.With(Any value1, - Any value2, - Any value3) - { - _withValues = new List> { EitherTuple.Create(value1, value2, value3) }; - return this; - } - - IActionWithHandler, T1, T2, T3> IMatcher.With(Any value1, - Any value2, - T3 value3) - { - _withValues = new List> { EitherTuple.Create(value1, value2, value3) }; - return this; - } - - IActionWithHandler, T1, T2, T3> IMatcher.With(Any value1, - T2 value2, - Any value3) - { - _withValues = new List> { EitherTuple.Create(value1, value2, value3) }; - return this; - } - - IActionWithHandler, T1, T2, T3> IMatcher.With(T1 value1, - Any value2, - Any value3) - { - _withValues = new List> { EitherTuple.Create(value1, value2, value3) }; - return this; - } - - IActionWithHandler, T1, T2, T3> IMatcher.With(Any value1, - Any value2, - Any value3) - { - _withValues = new List> { EitherTuple.Create(value1, value2, value3) }; + _withValues = new List> {EitherTuple.Create(value1, value2, value3)}; return this; } IFuncWithHandler, T1, T2, T3, TResult> IFuncMatcher.With( - T1 value1, - T2 value2, - T3 value3) + Either value1, + Either value2, + Either value3) { _withValues = new List> { EitherTuple.Create(value1, value2, value3) }; return this; } - IFuncWithHandler, T1, T2, T3, TResult> IFuncMatcher.With( - Any value1, - T2 value2, - T3 value3) - { - _withValues = new List> { EitherTuple.Create(value1, value2, value3) }; - return this; - } - - IFuncWithHandler, T1, T2, T3, TResult> IFuncMatcher.With( - T1 value1, - Any value2, - T3 value3) - { - _withValues = new List> { EitherTuple.Create(value1, value2, value3) }; - return this; - } - - IFuncWithHandler, T1, T2, T3, TResult> IFuncMatcher.With( - T1 value1, - T2 value2, - Any value3) - { - _withValues = new List> { EitherTuple.Create(value1, value2, value3) }; - return this; - } - - IFuncWithHandler, T1, T2, T3, TResult> IFuncMatcher.With( - Any value1, - Any value2, - T3 value3) - { - _withValues = new List> { EitherTuple.Create(value1, value2, value3) }; - return this; - } - - IFuncWithHandler, T1, T2, T3, TResult> IFuncMatcher.With( - Any value1, - T2 value2, - Any value3) - { - _withValues = new List> { EitherTuple.Create(value1, value2, value3) }; - return this; - } - - IFuncWithHandler, T1, T2, T3, TResult> IFuncMatcher.With( - T1 value1, - Any value2, - Any value3) - { - _withValues = new List> { EitherTuple.Create(value1, value2, value3) }; - return this; - } - - IFuncWithHandler, T1, T2, T3, TResult> IFuncMatcher.With( - Any value1, - Any value2, - Any value3) - { - _withValues = new List> { EitherTuple.Create(value1, value2, value3) }; - return this; - } - IActionWhereHandler, T1, T2, T3> IMatcher.Where( Func function) { @@ -280,61 +107,14 @@ IFuncMatcherAfterElse IFuncMatcher.Else(TResult re } IFuncWithHandler, T1, T2, T3, TResult> - IFuncWithHandler, T1, T2, T3, TResult>.Or(T1 value1, T2 value2, T3 value3) + IFuncWithHandler, T1, T2, T3, TResult>.Or(Either value1, + Either value2, + Either value3) { _withValues.Add(EitherTuple.Create(value1, value2, value3)); return this; } - IFuncWithHandler, T1, T2, T3, TResult> - IFuncWithHandler, T1, T2, T3, TResult>.Or(Any value1, T2 value2, T3 value3) - { - _withValues.Add(EitherTuple.Create(value1, value2, value3)); - return this; - } - - IFuncWithHandler, T1, T2, T3, TResult> - IFuncWithHandler, T1, T2, T3, TResult>.Or(T1 value1, Any value2, T3 value3) - { - _withValues.Add(EitherTuple.Create(value1, value2, value3)); - return this; - } - - IFuncWithHandler, T1, T2, T3, TResult> - IFuncWithHandler, T1, T2, T3, TResult>.Or(T1 value1, T2 value2, Any value3) - { - _withValues.Add(EitherTuple.Create(value1, value2, value3)); - return this; - } - - IFuncWithHandler, T1, T2, T3, TResult> - IFuncWithHandler, T1, T2, T3, TResult>.Or(Any value1, Any value2, T3 value3) - { - _withValues.Add(EitherTuple.Create(value1, value2, value3)); - return this; - } - - IFuncWithHandler, T1, T2, T3, TResult> - IFuncWithHandler, T1, T2, T3, TResult>.Or(Any value1, T2 value2, Any value3) - { - _withValues.Add(EitherTuple.Create(value1, value2, value3)); - return this; - } - - IFuncWithHandler, T1, T2, T3, TResult> - IFuncWithHandler, T1, T2, T3, TResult>.Or(T1 value1, Any value2, Any value3) - { - _withValues.Add(EitherTuple.Create(value1, value2, value3)); - return this; - } - - IFuncWithHandler, T1, T2, T3, TResult> - IFuncWithHandler, T1, T2, T3, TResult>.Or(Any value1, Any value2, Any value3) - { - _withValues.Add(EitherTuple.Create(value1, value2, value3)); - return this; - } - IActionMatcher IActionWithHandler, T1, T2, T3>.Do( Action action) { @@ -418,59 +198,12 @@ private static Func, TResult> ActionToFunc(Action }; IActionWithHandler, T1, T2, T3> - IActionWithHandler, T1, T2, T3>.Or(T1 value1, T2 value2, T3 value3) + IActionWithHandler, T1, T2, T3>.Or(Either value1, + Either value2, + Either value3) { _withValues.Add(EitherTuple.Create(value1, value2, value3)); return this; } - - IActionWithHandler, T1, T2, T3> - IActionWithHandler, T1, T2, T3>.Or(Any value1, T2 value2, T3 value3) - { - _withValues.Add(EitherTuple.Create(value1, value2, value3)); - return this; - } - - IActionWithHandler, T1, T2, T3> - IActionWithHandler, T1, T2, T3>.Or(T1 value1, Any value2, T3 value3) - { - _withValues.Add(EitherTuple.Create(value1, value2, value3)); - return this; - } - - IActionWithHandler, T1, T2, T3> - IActionWithHandler, T1, T2, T3>.Or(T1 value1, T2 value2, Any value3) - { - _withValues.Add(EitherTuple.Create(value1, value2, value3)); - return this; - } - - IActionWithHandler, T1, T2, T3> - IActionWithHandler, T1, T2, T3>.Or(Any value1, Any value2, T3 value3) - { - _withValues.Add(EitherTuple.Create(value1, value2, value3)); - return this; - } - - IActionWithHandler, T1, T2, T3> - IActionWithHandler, T1, T2, T3>.Or(Any value1, T2 value2, Any value3) - { - _withValues.Add(EitherTuple.Create(value1, value2, value3)); - return this; - } - - IActionWithHandler, T1, T2, T3> - IActionWithHandler, T1, T2, T3>.Or(T1 value1, Any value2, Any value3) - { - _withValues.Add(EitherTuple.Create(value1, value2, value3)); - return this; - } - - IActionWithHandler, T1, T2, T3> - IActionWithHandler, T1, T2, T3>.Or(Any value1, Any value2, Any value3) - { - _withValues.Add(EitherTuple.Create(value1, value2, value3)); - return this; - } } } \ No newline at end of file diff --git a/SuccincT/PatternMatchers/Matcher{T1,T2,TR}.cs b/SuccincT/PatternMatchers/Matcher{T1,T2,TR}.cs index cca2793..83509b8 100644 --- a/SuccincT/PatternMatchers/Matcher{T1,T2,TR}.cs +++ b/SuccincT/PatternMatchers/Matcher{T1,T2,TR}.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.Linq; +using SuccincT.Unions; using static SuccincT.Functional.Unit; namespace SuccincT.PatternMatchers @@ -33,79 +34,25 @@ internal Matcher(Tuple item) IFuncMatcher IMatcher.To() => new Matcher(_item); - IActionWithHandler, T1, T2> IActionMatcher.With(T1 value1, T2 value2) + IActionWithHandler, T1, T2> IActionMatcher.With(Either value1, + Either value2) { - _withValues = new List> { EitherTuple.Create(value1, value2) }; + _withValues = new List> {EitherTuple.Create(value1, value2)}; return this; } - IActionWithHandler, T1, T2> IActionMatcher.With(Any value1, T2 value2) + IFuncWithHandler, T1, T2, TResult> IFuncMatcher.With( + Either value1, + Either value2) { - _withValues = new List> { EitherTuple.Create(value1, value2) }; + _withValues = new List> {EitherTuple.Create(value1, value2)}; return this; } - IActionWithHandler, T1, T2> IActionMatcher.With(T1 value1, Any value2) + IActionWithHandler, T1, T2> IMatcher.With(Either value1, + Either value2) { - _withValues = new List> { EitherTuple.Create(value1, value2) }; - return this; - } - - IActionWithHandler, T1, T2> IActionMatcher.With(Any value1, Any value2) - { - _withValues = new List> { EitherTuple.Create(value1, value2) }; - return this; - } - - IFuncWithHandler, T1, T2, TResult> IFuncMatcher.With(T1 value1, - T2 value2) - { - _withValues = new List> { EitherTuple.Create(value1, value2) }; - return this; - } - - IFuncWithHandler, T1, T2, TResult> IFuncMatcher.With(Any value1, - T2 value2) - { - _withValues = new List> { EitherTuple.Create(value1, value2) }; - return this; - } - - IFuncWithHandler, T1, T2, TResult> IFuncMatcher.With(T1 value1, - Any value2) - { - _withValues = new List> { EitherTuple.Create(value1, value2) }; - return this; - } - - IFuncWithHandler, T1, T2, TResult> IFuncMatcher.With(Any value1, - Any value2) - { - _withValues = new List> { EitherTuple.Create(value1, value2) }; - return this; - } - - IActionWithHandler, T1, T2> IMatcher.With(T1 value1, T2 value2) - { - _withValues = new List> { EitherTuple.Create(value1, value2) }; - return this; - } - - IActionWithHandler, T1, T2> IMatcher.With(Any value1, T2 value2) - { - _withValues = new List> { EitherTuple.Create(value1, value2) }; - return this; - } - - IActionWithHandler, T1, T2> IMatcher.With(T1 value1, Any value2) - { - _withValues = new List> { EitherTuple.Create(value1, value2) }; - return this; - } - - IActionWithHandler, T1, T2> IMatcher.With(Any value1, Any value2) - { - _withValues = new List> { EitherTuple.Create(value1, value2) }; + _withValues = new List> {EitherTuple.Create(value1, value2)}; return this; } @@ -153,65 +100,21 @@ IFuncMatcherAfterElse IFuncMatcher.Else(TResult result } IActionWithHandler, T1, T2> IActionWithHandler, T1, T2>.Or( - T1 value1, - T2 value2) + Either value1, + Either value2) { _withValues.Add(EitherTuple.Create(value1, value2)); return this; } - IActionWithHandler, T1, T2> IActionWithHandler, T1, T2>.Or( - Any value1, - T2 value2) - { - _withValues.Add(EitherTuple.Create(value1, value2)); - return this; - } - - IActionWithHandler, T1, T2> IActionWithHandler, T1, T2>.Or( - T1 value1, - Any value2) - { - _withValues.Add(EitherTuple.Create(value1, value2)); - return this; - } - - IActionWithHandler, T1, T2> IActionWithHandler, T1, T2>.Or( - Any value1, - Any value2) - { - _withValues.Add(EitherTuple.Create(value1, value2)); - return this; - } - IFuncWithHandler, T1, T2, TResult> - IFuncWithHandler, T1, T2, TResult>.Or(T1 value1, T2 value2) + IFuncWithHandler, T1, T2, TResult>.Or(Either value1, + Either value2) { _withValues.Add(EitherTuple.Create(value1, value2)); return this; } - IFuncWithHandler, T1, T2, TResult> - IFuncWithHandler, T1, T2, TResult>.Or(Any value1, T2 value2) - { - _withValues.Add(EitherTuple.Create(value1, value2)); - return this; - } - - IFuncWithHandler, T1, T2, TResult> - IFuncWithHandler, T1, T2, TResult>.Or(T1 value1, Any value2) - { - _withValues.Add(EitherTuple.Create(value1, value2)); - return this; - } - - IFuncWithHandler, T1, T2, TResult> - IFuncWithHandler, T1, T2, TResult>.Or(Any value1, Any value2) - { - _withValues.Add(EitherTuple.Create(value1, value2)); - return this; - } - IActionMatcher IActionWithHandler, T1, T2>.Do(Action action) { RecordFunction((x, y) => y.Any(v => v.MatchesTuple(x)), diff --git a/SuccincT/SuccincT.csproj b/SuccincT/SuccincT.csproj index 2fb371e..2698029 100644 --- a/SuccincT/SuccincT.csproj +++ b/SuccincT/SuccincT.csproj @@ -59,7 +59,7 @@ - + @@ -96,6 +96,7 @@ + diff --git a/SuccincT/Functional/Either.cs b/SuccincT/Unions/Either.cs similarity index 88% rename from SuccincT/Functional/Either.cs rename to SuccincT/Unions/Either.cs index fe830d8..ba5aef7 100644 --- a/SuccincT/Functional/Either.cs +++ b/SuccincT/Unions/Either.cs @@ -1,7 +1,7 @@ -using SuccincT.Options; -using System; +using System; +using SuccincT.Options; -namespace SuccincT.Functional +namespace SuccincT.Unions { public struct Either { @@ -73,5 +73,8 @@ public override int GetHashCode() } public static bool operator !=(Either x, Either y) => !(x == y); + + public static implicit operator Either(TLeft value) => new Either(value); + public static implicit operator Either(TRight value) => new Either(value); } } diff --git a/SuccincTTests/SuccincT/Functional/EitherTests.cs b/SuccincTTests/SuccincT/Functional/EitherTests.cs index 09770a1..0dd881e 100644 --- a/SuccincTTests/SuccincT/Functional/EitherTests.cs +++ b/SuccincTTests/SuccincT/Functional/EitherTests.cs @@ -1,6 +1,7 @@ using System; using NUnit.Framework; using SuccincT.Functional; +using SuccincT.Unions; using static NUnit.Framework.Assert; namespace SuccincTTests.SuccincT.Functional diff --git a/SuccincTTests/SuccincT/Tuples/TupleExecTestsUsingAnyT1T2T3T4.cs b/SuccincTTests/SuccincT/Tuples/TupleExecTestsUsingAnyT1T2T3T4.cs new file mode 100644 index 0000000..8076596 --- /dev/null +++ b/SuccincTTests/SuccincT/Tuples/TupleExecTestsUsingAnyT1T2T3T4.cs @@ -0,0 +1,180 @@ +using System; +using NUnit.Framework; +using SuccincT.PatternMatchers; +using static SuccincT.PatternMatchers.Any; + +namespace SuccincTTests.SuccincT.Tuples +{ + [TestFixture] + public class TupleExecTestsUsingAnyT1T2T3T4 + { + private enum Colors { Red, Green, Blue } + private enum Animals { Cow, Pig, Goat } + + [Test] + public void Tuple_CanBeMatchedUsingAnyIntWithExec() + { + var tuple = Tuple.Create(1, "a", Colors.Red, Animals.Cow); + var result = false; + tuple.Match().With(any, "a", Colors.Red, Animals.Cow).Do((w, x, y, z) => result = true).Exec(); + Assert.IsTrue(result); + } + + [Test] + public void Tuple_CanBeMatchedUsingAnyStringWithExec() + { + var tuple = Tuple.Create(1, "a", Colors.Red, Animals.Cow); + var result = false; + tuple.Match().With(1, any, Colors.Red, Animals.Cow).Do((w, x, y, z) => result = true).Exec(); + Assert.IsTrue(result); + } + + [Test] + public void Tuple_CanBeMatchedUsingAnyColorWithExec() + { + var tuple = Tuple.Create(1, "a", Colors.Red, Animals.Cow); + var result = false; + tuple.Match().With(1, "a", any, Animals.Cow).Do((w, x, y, z) => result = true).Exec(); + Assert.IsTrue(result); + } + + [Test] + public void Tuple_CanBeMatchedUsingAnyAnimalWithExec() + { + var tuple = Tuple.Create(1, "a", Colors.Red, Animals.Cow); + var result = false; + tuple.Match().With(1, "a", Colors.Red, any).Do((w, x, y, z) => result = true).Exec(); + Assert.IsTrue(result); + } + + [Test] + public void Tuple_CanBeMatchedUsingFourAnysWithExec() + { + var tuple = Tuple.Create(1, "a", Colors.Red, Animals.Cow); + var result = false; + tuple.Match().With(any, any, any, any).Do((w, x, y, z) => result = true).Exec(); + Assert.IsTrue(result); + } + + [Test] + public void Tuple_CanBeMatchedViaOrUsingAnyIntWithExec() + { + var tuple = Tuple.Create(1, "a", Colors.Green, Animals.Cow); + var result = false; + tuple.Match().With(1, "a", Colors.Green, Animals.Goat) + .Or(__, "a", Colors.Green, Animals.Cow).Do((w, x, y, z) => result = true) + .Exec(); + Assert.IsTrue(result); + } + + [Test] + public void Tuple_CanBeMatchedViaOrUsingAnyStringWithExec() + { + var tuple = Tuple.Create(1, "a", Colors.Green, Animals.Cow); + var result = false; + tuple.Match().With(1, "a", Colors.Green, Animals.Goat) + .Or(1, __, Colors.Green, Animals.Cow).Do((w, x, y, z) => result = true) + .Exec(); + Assert.IsTrue(result); + } + + [Test] + public void Tuple_CanBeMatchedViaOrUsingAnyColorWithExec() + { + var tuple = Tuple.Create(1, "a", Colors.Green, Animals.Cow); + var result = false; + tuple.Match().With(1, "a", Colors.Green, Animals.Goat) + .Or(1, "a", __, Animals.Cow).Do((w, x, y, z) => result = true) + .Exec(); + Assert.IsTrue(result); + } + + [Test] + public void Tuple_CanBeMatchedViaOrUsingAnyAnimalWithExec() + { + var tuple = Tuple.Create(1, "a", Colors.Green, Animals.Cow); + var result = false; + tuple.Match().With(1, "a", Colors.Green, Animals.Goat) + .Or(1, "a", Colors.Green, __).Do((w, x, y, z) => result = true) + .Exec(); + Assert.IsTrue(result); + } + + [Test] + public void Tuple_CanBeMatchedViaOrUsingFourAnysWithExec() + { + var tuple = Tuple.Create(1, "a", Colors.Green, Animals.Cow); + var result = false; + tuple.Match().With(1, "a", Colors.Green, Animals.Goat) + .Or(__, __, __, __).Do((w, x, y, z) => result = true) + .Exec(); + Assert.IsTrue(result); + } + + [Test] + public void TupleWhenNoMatchDespiteAnyInt_ElseUsedWithExec() + { + var tuple = Tuple.Create(1, "a", Colors.Red, Animals.Cow); + var result = false; + tuple.Match().With(__, "a", Colors.Red, Animals.Goat).Do((w, x, y, z) => result = true) + .Else((w, x, y, z) => result = false).Exec(); + Assert.IsFalse(result); + } + + [Test] + public void TupleWhenNoMatchDespiteAnyString_ElseUsedWithExec() + { + var tuple = Tuple.Create(1, "a", Colors.Red, Animals.Cow); + var result = false; + tuple.Match().With(1, __, Colors.Red, Animals.Goat).Do((w, x, y, z) => result = true) + .Else((w, x, y, z) => result = false).Exec(); + Assert.IsFalse(result); + } + + [Test] + public void TupleWhenNoMatchDespiteAnyColor_ElseUsedWithExec() + { + var tuple = Tuple.Create(1, "a", Colors.Red, Animals.Cow); + var result = false; + tuple.Match().With(1, "a", __, Animals.Goat).Do((w, x, y, z) => result = true) + .Else((w, x, y, z) => result = false).Exec(); + Assert.IsFalse(result); + } + + [Test] + public void TupleWhenNoMatchDespiteAnyAnimal_ElseUsedWithExec() + { + var tuple = Tuple.Create(1, "a", Colors.Red, Animals.Cow); + var result = false; + tuple.Match().With(1, "a", Colors.Blue, __).Do((w, x, y, z) => result = true) + .Else((w, x, y, z) => result = false).Exec(); + Assert.IsFalse(result); + } + + [Test] + public void TupleWithAndWhereDefinedUsingAnyInt_WhereCorrectlyUsedWithExec() + { + var tuple = Tuple.Create(1, "a", Colors.Red, Animals.Cow); + var result = false; + tuple.Match().With(__, "a", Colors.Red, Animals.Goat) + .Or(__, "a", Colors.Red, Animals.Pig).Do((w, x, y, z) => result = false) + .Where((w, x, y, z) => w == 1 && x == "a" && y == Colors.Red && z == Animals.Cow) + .Do((w, x, y, z) => result = true) + .Exec(); + Assert.IsTrue(result); + } + + [Test] + public void TupleWithAndWhereDefinedUsingFourAnys_WithCorrectlyUsedWithExec() + { + var tuple = Tuple.Create(1, "a", Colors.Red, Animals.Cow); + var result = false; + tuple.Match() + .Where((w, x, y, z) => z == Animals.Pig).Do((w, x, y, z) => result = true) + .With(1, "a", Colors.Red, Animals.Goat).Or(__, __, __, __) + .Do((w, x, y, z) => result = false) + .Exec(); + Assert.IsFalse(result); + } + } +} \ No newline at end of file diff --git a/SuccincTTests/SuccincT/Tuples/TupleMatchableTestsUsingAnyT1T2T3T4.cs b/SuccincTTests/SuccincT/Tuples/TupleMatchableTestsUsingAnyT1T2T3T4.cs new file mode 100644 index 0000000..c7d3fe7 --- /dev/null +++ b/SuccincTTests/SuccincT/Tuples/TupleMatchableTestsUsingAnyT1T2T3T4.cs @@ -0,0 +1,110 @@ +using System; +using NUnit.Framework; +using SuccincT.PatternMatchers; +using SuccincT.Tuples; +using static SuccincT.PatternMatchers.Any; + +namespace SuccincTTests.SuccincT.Tuples +{ + [TestFixture] + internal class TupleMatchableTestsUsingAnyT1T2T3T4 + { + private enum Colors { Red, Green, Blue } + private enum Animals { Cow, Pig, Goat } + + private class TestClass : ITupleMatchable + { + public int A; + public string B; + public Colors C; + public Animals D; + public Tuple PropertiesToMatch => Tuple.Create(A, B, C, D); + } + + [Test] + public void Tuple_CanBeMatchedWithAnyInt() + { + var tuple = new TestClass { A = 1, B = "a", C = Colors.Red, D = Animals.Goat }; + var result = tuple.Match().To() + .With(any, "a", Colors.Red, Animals.Goat).Do((w, x, y, z) => true).Result(); + Assert.IsTrue(result); + } + + [Test] + public void Tuple_CanBeMatchedWithAnyString() + { + var tuple = new TestClass { A = 1, B = "a", C = Colors.Red, D = Animals.Goat }; + var result = tuple.Match().To() + .With(1, any, Colors.Red, Animals.Goat).Do((w, x, y, z) => true).Result(); + Assert.IsTrue(result); + } + + [Test] + public void Tuple_CanBeMatchedWithAnyColor() + { + var tuple = new TestClass { A = 1, B = "a", C = Colors.Red, D = Animals.Goat }; + var result = tuple.Match().To() + .With(1, "a", any, Animals.Goat).Do((w, x, y, z) => true).Result(); + Assert.IsTrue(result); + } + + [Test] + public void Tuple_CanBeMatchedWithAnyAnimal() + { + var tuple = new TestClass { A = 1, B = "a", C = Colors.Red, D = Animals.Goat }; + var result = tuple.Match().To() + .With(1, "a", Colors.Red, any).Do((w, x, y, z) => true).Result(); + Assert.IsTrue(result); + } + + [Test] + public void Tuple_CanBeMatchedViaOrWithAnyInt() + { + var tuple = new TestClass { A = 1, B = "a", C = Colors.Blue, D = Animals.Cow }; + var result = tuple.Match().To().With(1, "a", Colors.Blue, Animals.Goat) + .Or(__, "a", Colors.Blue, Animals.Cow).Do((w, x, y, z) => true) + .Result(); + Assert.IsTrue(result); + } + + [Test] + public void Tuple_CanBeMatchedViaOrWithAnyString() + { + var tuple = new TestClass { A = 1, B = "a", C = Colors.Blue, D = Animals.Cow }; + var result = tuple.Match().To().With(1, "a", Colors.Blue, Animals.Goat) + .Or(1, __, Colors.Blue, Animals.Cow).Do((w, x, y, z) => true) + .Result(); + Assert.IsTrue(result); + } + + [Test] + public void Tuple_CanBeMatchedViaOrWithAnyColor() + { + var tuple = new TestClass { A = 1, B = "a", C = Colors.Blue, D = Animals.Cow }; + var result = tuple.Match().To().With(1, "a", Colors.Blue, Animals.Goat) + .Or(1, "a", __, Animals.Cow).Do((w, x, y, z) => true) + .Result(); + Assert.IsTrue(result); + } + + [Test] + public void Tuple_CanBeMatchedViaOrWithAnyAnimal() + { + var tuple = new TestClass { A = 1, B = "a", C = Colors.Blue, D = Animals.Cow }; + var result = tuple.Match().To().With(1, "a", Colors.Blue, Animals.Goat) + .Or(1, "a", Colors.Blue, __).Do((w, x, y, z) => true) + .Result(); + Assert.IsTrue(result); + } + + [Test] + public void Tuple_CanBeMatchedViaOrWithFourAnys() + { + var tuple = new TestClass { A = 1, B = "a", C = Colors.Blue, D = Animals.Cow }; + var result = tuple.Match().To().With(1, "a", Colors.Green, Animals.Pig) + .Or(__, __, __, __).Do((w, x, y, z) => true) + .Result(); + Assert.IsTrue(result); + } + } +} \ No newline at end of file diff --git a/SuccincTTests/SuccincTTests.csproj b/SuccincTTests/SuccincTTests.csproj index 323b71b..68a9672 100644 --- a/SuccincTTests/SuccincTTests.csproj +++ b/SuccincTTests/SuccincTTests.csproj @@ -112,11 +112,13 @@ + +