Skip to content

Commit

Permalink
Issue #8
Browse files Browse the repository at this point in the history
Added complete support for any/__ to Tuple<,> and Tuple<,,> pattern matchers.
  • Loading branch information
DavidArno committed Feb 2, 2017
1 parent 2094d11 commit a7389e4
Show file tree
Hide file tree
Showing 13 changed files with 751 additions and 91 deletions.
7 changes: 6 additions & 1 deletion SuccincT/PatternMatchers/Any.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,12 @@ public struct Any
[SuppressMessage("Microsoft.Usage", "CA1801:ReviewUnusedParameters")]
public static bool operator !=(Any any1, Any any2) => false;

[SuppressMessage("Microsoft.Naming", "CA1709:IdentifiersShouldBeCasedCorrectly", MessageId = "any")]
[SuppressMessage("Microsoft.Naming", "CA1707:IdentifiersShouldNotContainUnderscores")]
public static Any _ { get; } = AnAny;
// ReSharper disable once InconsistentNaming
public static Any any { get; } = AnAny;

[SuppressMessage("Microsoft.Naming", "CA1707:IdentifiersShouldNotContainUnderscores")]
public static Any __ { get; } = AnAny;
}
}
7 changes: 7 additions & 0 deletions SuccincT/PatternMatchers/IActionWithHandler{TM,T1,T2,T3}.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@ namespace SuccincT.PatternMatchers
public interface IActionWithHandler<out TMatcher, T1, T2, T3>
{
IActionWithHandler<TMatcher, T1, T2, T3> Or(T1 value1, T2 value2, T3 value3);
IActionWithHandler<TMatcher, T1, T2, T3> Or(Any value1, T2 value2, T3 value3);
IActionWithHandler<TMatcher, T1, T2, T3> Or(T1 value1, Any value2, T3 value3);
IActionWithHandler<TMatcher, T1, T2, T3> Or(T1 value1, T2 value2, Any value3);
IActionWithHandler<TMatcher, T1, T2, T3> Or(Any value1, Any value2, T3 value3);
IActionWithHandler<TMatcher, T1, T2, T3> Or(Any value1, T2 value2, Any value3);
IActionWithHandler<TMatcher, T1, T2, T3> Or(T1 value1, Any value2, Any value3);
IActionWithHandler<TMatcher, T1, T2, T3> Or(Any value1, Any value2, Any value3);

TMatcher Do(Action<T1, T2, T3> action);
}
Expand Down
7 changes: 7 additions & 0 deletions SuccincT/PatternMatchers/IFuncMatcher{T1,T2,T3,TR}.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@ namespace SuccincT.PatternMatchers
public interface IFuncMatcher<T1, T2, T3, TResult>
{
IFuncWithHandler<IFuncMatcher<T1, T2, T3, TResult>, T1, T2, T3, TResult> With(T1 value1, T2 value2, T3 value3);
IFuncWithHandler<IFuncMatcher<T1, T2, T3, TResult>, T1, T2, T3, TResult> With(Any value1, T2 value2, T3 value3);
IFuncWithHandler<IFuncMatcher<T1, T2, T3, TResult>, T1, T2, T3, TResult> With(T1 value1, Any value2, T3 value3);
IFuncWithHandler<IFuncMatcher<T1, T2, T3, TResult>, T1, T2, T3, TResult> With(T1 value1, T2 value2, Any value3);
IFuncWithHandler<IFuncMatcher<T1, T2, T3, TResult>, T1, T2, T3, TResult> With(Any value1, Any value2, T3 value3);
IFuncWithHandler<IFuncMatcher<T1, T2, T3, TResult>, T1, T2, T3, TResult> With(Any value1, T2 value2, Any value3);
IFuncWithHandler<IFuncMatcher<T1, T2, T3, TResult>, T1, T2, T3, TResult> With(T1 value1, Any value2, Any value3);
IFuncWithHandler<IFuncMatcher<T1, T2, T3, TResult>, T1, T2, T3, TResult> With(Any value1, Any value2, Any value3);

IFuncWhereHandler<IFuncMatcher<T1, T2, T3, TResult>, T1, T2, T3, TResult> Where(
Func<T1, T2, T3, bool> expression);
Expand Down
7 changes: 7 additions & 0 deletions SuccincT/PatternMatchers/IFuncWithHandler{TM,T1,T2,T3,TR}.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@ namespace SuccincT.PatternMatchers
public interface IFuncWithHandler<out TMatcher, T1, T2, T3, in TResult>
{
IFuncWithHandler<TMatcher, T1, T2, T3, TResult> Or(T1 value1, T2 value2, T3 value3);
IFuncWithHandler<TMatcher, T1, T2, T3, TResult> Or(Any value1, T2 value2, T3 value3);
IFuncWithHandler<TMatcher, T1, T2, T3, TResult> Or(T1 value1, Any value2, T3 value3);
IFuncWithHandler<TMatcher, T1, T2, T3, TResult> Or(T1 value1, T2 value2, Any value3);
IFuncWithHandler<TMatcher, T1, T2, T3, TResult> Or(Any value1, Any value2, T3 value3);
IFuncWithHandler<TMatcher, T1, T2, T3, TResult> Or(Any value1, T2 value2, Any value3);
IFuncWithHandler<TMatcher, T1, T2, T3, TResult> Or(T1 value1, Any value2, Any value3);
IFuncWithHandler<TMatcher, T1, T2, T3, TResult> Or(Any value1, Any value2, Any value3);

TMatcher Do(Func<T1, T2, T3, TResult> function);

Expand Down
161 changes: 161 additions & 0 deletions SuccincT/PatternMatchers/Matcher{T1,T2,T3,TR}.cs
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,69 @@ IFuncWithHandler<IFuncMatcher<T1, T2, T3, TResult>, T1, T2, T3, TResult> IFuncMa
return this;
}

IFuncWithHandler<IFuncMatcher<T1, T2, T3, TResult>, T1, T2, T3, TResult> IFuncMatcher<T1, T2, T3, TResult>.With(
Any value1,
T2 value2,
T3 value3)
{
_withValues = new List<EitherTuple<T1, T2, T3>> { EitherTuple.Create<T1, T2, T3>(value1, value2, value3) };
return this;
}

IFuncWithHandler<IFuncMatcher<T1, T2, T3, TResult>, T1, T2, T3, TResult> IFuncMatcher<T1, T2, T3, TResult>.With(
T1 value1,
Any value2,
T3 value3)
{
_withValues = new List<EitherTuple<T1, T2, T3>> { EitherTuple.Create<T1, T2, T3>(value1, value2, value3) };
return this;
}

IFuncWithHandler<IFuncMatcher<T1, T2, T3, TResult>, T1, T2, T3, TResult> IFuncMatcher<T1, T2, T3, TResult>.With(
T1 value1,
T2 value2,
Any value3)
{
_withValues = new List<EitherTuple<T1, T2, T3>> { EitherTuple.Create<T1, T2, T3>(value1, value2, value3) };
return this;
}

IFuncWithHandler<IFuncMatcher<T1, T2, T3, TResult>, T1, T2, T3, TResult> IFuncMatcher<T1, T2, T3, TResult>.With(
Any value1,
Any value2,
T3 value3)
{
_withValues = new List<EitherTuple<T1, T2, T3>> { EitherTuple.Create<T1, T2, T3>(value1, value2, value3) };
return this;
}

IFuncWithHandler<IFuncMatcher<T1, T2, T3, TResult>, T1, T2, T3, TResult> IFuncMatcher<T1, T2, T3, TResult>.With(
Any value1,
T2 value2,
Any value3)
{
_withValues = new List<EitherTuple<T1, T2, T3>> { EitherTuple.Create<T1, T2, T3>(value1, value2, value3) };
return this;
}

IFuncWithHandler<IFuncMatcher<T1, T2, T3, TResult>, T1, T2, T3, TResult> IFuncMatcher<T1, T2, T3, TResult>.With(
T1 value1,
Any value2,
Any value3)
{
_withValues = new List<EitherTuple<T1, T2, T3>> { EitherTuple.Create<T1, T2, T3>(value1, value2, value3) };
return this;
}

IFuncWithHandler<IFuncMatcher<T1, T2, T3, TResult>, T1, T2, T3, TResult> IFuncMatcher<T1, T2, T3, TResult>.With(
Any value1,
Any value2,
Any value3)
{
_withValues = new List<EitherTuple<T1, T2, T3>> { EitherTuple.Create<T1, T2, T3>(value1, value2, value3) };
return this;
}

IActionWhereHandler<IActionMatcher<T1, T2, T3>, T1, T2, T3> IMatcher<T1, T2, T3>.Where(
Func<T1, T2, T3, bool> function)
{
Expand Down Expand Up @@ -223,6 +286,55 @@ IFuncMatcherAfterElse<TResult> IFuncMatcher<T1, T2, T3, TResult>.Else(TResult re
return this;
}

IFuncWithHandler<IFuncMatcher<T1, T2, T3, TResult>, T1, T2, T3, TResult>
IFuncWithHandler<IFuncMatcher<T1, T2, T3, TResult>, T1, T2, T3, TResult>.Or(Any value1, T2 value2, T3 value3)
{
_withValues.Add(EitherTuple.Create<T1, T2, T3>(value1, value2, value3));
return this;
}

IFuncWithHandler<IFuncMatcher<T1, T2, T3, TResult>, T1, T2, T3, TResult>
IFuncWithHandler<IFuncMatcher<T1, T2, T3, TResult>, T1, T2, T3, TResult>.Or(T1 value1, Any value2, T3 value3)
{
_withValues.Add(EitherTuple.Create<T1, T2, T3>(value1, value2, value3));
return this;
}

IFuncWithHandler<IFuncMatcher<T1, T2, T3, TResult>, T1, T2, T3, TResult>
IFuncWithHandler<IFuncMatcher<T1, T2, T3, TResult>, T1, T2, T3, TResult>.Or(T1 value1, T2 value2, Any value3)
{
_withValues.Add(EitherTuple.Create<T1, T2, T3>(value1, value2, value3));
return this;
}

IFuncWithHandler<IFuncMatcher<T1, T2, T3, TResult>, T1, T2, T3, TResult>
IFuncWithHandler<IFuncMatcher<T1, T2, T3, TResult>, T1, T2, T3, TResult>.Or(Any value1, Any value2, T3 value3)
{
_withValues.Add(EitherTuple.Create<T1, T2, T3>(value1, value2, value3));
return this;
}

IFuncWithHandler<IFuncMatcher<T1, T2, T3, TResult>, T1, T2, T3, TResult>
IFuncWithHandler<IFuncMatcher<T1, T2, T3, TResult>, T1, T2, T3, TResult>.Or(Any value1, T2 value2, Any value3)
{
_withValues.Add(EitherTuple.Create<T1, T2, T3>(value1, value2, value3));
return this;
}

IFuncWithHandler<IFuncMatcher<T1, T2, T3, TResult>, T1, T2, T3, TResult>
IFuncWithHandler<IFuncMatcher<T1, T2, T3, TResult>, T1, T2, T3, TResult>.Or(T1 value1, Any value2, Any value3)
{
_withValues.Add(EitherTuple.Create<T1, T2, T3>(value1, value2, value3));
return this;
}

IFuncWithHandler<IFuncMatcher<T1, T2, T3, TResult>, T1, T2, T3, TResult>
IFuncWithHandler<IFuncMatcher<T1, T2, T3, TResult>, T1, T2, T3, TResult>.Or(Any value1, Any value2, Any value3)
{
_withValues.Add(EitherTuple.Create<T1, T2, T3>(value1, value2, value3));
return this;
}

IActionMatcher<T1, T2, T3> IActionWithHandler<IActionMatcher<T1, T2, T3>, T1, T2, T3>.Do(
Action<T1, T2, T3> action)
{
Expand Down Expand Up @@ -311,5 +423,54 @@ private static Func<Tuple<T1, T2, T3>, TResult> ActionToFunc(Action<T1, T2, T3>
_withValues.Add(EitherTuple.Create(value1, value2, value3));
return this;
}

IActionWithHandler<IActionMatcher<T1, T2, T3>, T1, T2, T3>
IActionWithHandler<IActionMatcher<T1, T2, T3>, T1, T2, T3>.Or(Any value1, T2 value2, T3 value3)
{
_withValues.Add(EitherTuple.Create<T1, T2, T3>(value1, value2, value3));
return this;
}

IActionWithHandler<IActionMatcher<T1, T2, T3>, T1, T2, T3>
IActionWithHandler<IActionMatcher<T1, T2, T3>, T1, T2, T3>.Or(T1 value1, Any value2, T3 value3)
{
_withValues.Add(EitherTuple.Create<T1, T2, T3>(value1, value2, value3));
return this;
}

IActionWithHandler<IActionMatcher<T1, T2, T3>, T1, T2, T3>
IActionWithHandler<IActionMatcher<T1, T2, T3>, T1, T2, T3>.Or(T1 value1, T2 value2, Any value3)
{
_withValues.Add(EitherTuple.Create<T1, T2, T3>(value1, value2, value3));
return this;
}

IActionWithHandler<IActionMatcher<T1, T2, T3>, T1, T2, T3>
IActionWithHandler<IActionMatcher<T1, T2, T3>, T1, T2, T3>.Or(Any value1, Any value2, T3 value3)
{
_withValues.Add(EitherTuple.Create<T1, T2, T3>(value1, value2, value3));
return this;
}

IActionWithHandler<IActionMatcher<T1, T2, T3>, T1, T2, T3>
IActionWithHandler<IActionMatcher<T1, T2, T3>, T1, T2, T3>.Or(Any value1, T2 value2, Any value3)
{
_withValues.Add(EitherTuple.Create<T1, T2, T3>(value1, value2, value3));
return this;
}

IActionWithHandler<IActionMatcher<T1, T2, T3>, T1, T2, T3>
IActionWithHandler<IActionMatcher<T1, T2, T3>, T1, T2, T3>.Or(T1 value1, Any value2, Any value3)
{
_withValues.Add(EitherTuple.Create<T1, T2, T3>(value1, value2, value3));
return this;
}

IActionWithHandler<IActionMatcher<T1, T2, T3>, T1, T2, T3>
IActionWithHandler<IActionMatcher<T1, T2, T3>, T1, T2, T3>.Or(Any value1, Any value2, Any value3)
{
_withValues.Add(EitherTuple.Create<T1, T2, T3>(value1, value2, value3));
return this;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@ public void WhenObjectIsNotNull_ToOptionReturnsValue()
[Test]
public void WhenObjectIsNull_ToOptionReturnsNone()
{
var obj = (object) null;
var opt = obj.ToOption();
var opt = ((object)null).ToOption();
Assert.IsNotNull(opt);
Assert.IsFalse(opt.HasValue);
}
Expand All @@ -29,19 +28,18 @@ public void WhenObjectIsNull_ToOptionReturnsNone()
public void WhenNullableStructHasValue_ToOptionReturnsValue()
{
int? obj = 15;
var opt = obj.ToOption();
Assert.IsNotNull(opt);
Assert.IsTrue(opt.HasValue);
Assert.AreEqual(15, opt.Value);
var option = obj.ToOption();
Assert.IsNotNull(option);
Assert.IsTrue(option.HasValue);
Assert.AreEqual(15, option.Value);
}

[Test]
public void WhenNullableStructHasNoValue_ToOptionReturnsNone()
{
int? obj = null;
var opt = obj.ToOption();
Assert.IsNotNull(opt);
Assert.IsFalse(opt.HasValue);
var option = ((int?) null).ToOption();
Assert.IsNotNull(option);
Assert.IsFalse(option.HasValue);
}

[Test]
Expand Down
77 changes: 2 additions & 75 deletions SuccincTTests/SuccincT/Tuples/TupleExecTestsT1T2.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
using System;
using NUnit.Framework;
using SuccincT.PatternMatchers;
using static SuccincT.PatternMatchers.Any;

namespace SuccincTTests.SuccincT.Tuples
{
[TestFixture]
public class TupleExecTestsT1T2
public class TupleExecTestsUsingT1T2
{
[Test]
public void Tuple_CanBeMatchedWithExec()
Expand All @@ -17,15 +16,6 @@ public void Tuple_CanBeMatchedWithExec()
Assert.IsTrue(result);
}

[Test]
public void TupleWithAnyString_CanBeMatchedWithExec()
{
var tuple = Tuple.Create(1, "a");
var result = false;
tuple.Match().With(1, _).Do((x, y) => result = true).Exec();
Assert.IsTrue(result);
}

[Test]
public void Tuple_CanBeMatchedViaOrWithExec()
{
Expand All @@ -35,33 +25,6 @@ public void Tuple_CanBeMatchedViaOrWithExec()
Assert.IsTrue(result);
}

[Test]
public void TupleWIthAnyInt_CanBeMatchedViaOrWithExec()
{
var tuple = Tuple.Create(1, "a");
var result = false;
tuple.Match().With(2, "a").Or(_, "a").Do((x, y) => result = true).Exec();
Assert.IsTrue(result);
}

[Test]
public void TupleWIthAnyString_CanBeMatchedViaOrWithExec()
{
var tuple = Tuple.Create(1, "a");
var result = false;
tuple.Match().With(2, "a").Or(1, _).Do((x, y) => result = true).Exec();
Assert.IsTrue(result);
}

[Test]
public void TupleWIthAnyAndAny_CanBeMatchedViaOrWithExec()
{
var tuple = Tuple.Create(1, "a");
var result = false;
tuple.Match().With(2, "a").Or(_, _).Do((x, y) => result = true).Exec();
Assert.IsTrue(result);
}

[Test]
public void Tuple_ExceptionIfNoMatchWithExec()
{
Expand Down Expand Up @@ -120,41 +83,5 @@ public void TupleWithAndWhereDefined_WithCorrectlyUsedWithExec()
.Exec();
Assert.IsFalse(result);
}

[Test]
public void TupleAnyInt_WithAndWhereDefined_WithCorrectlyUsedWithExec()
{
var tuple = Tuple.Create(1, "a");
var result = false;
tuple.Match()
.Where((x, y) => x == 3 && y == "a").Do((x, y) => result = true)
.With(_, "a").Or(2, "a").Do((x, y) => result = false)
.Exec();
Assert.IsFalse(result);
}

[Test]
public void TupleAnyString_WithAndWhereDefined_WithCorrectlyUsedWithExec()
{
var tuple = Tuple.Create(1, "a");
var result = false;
tuple.Match()
.Where((x, y) => x == 3 && y == "a").Do((x, y) => result = true)
.With(1, _).Or(2, "a").Do((x, y) => result = false)
.Exec();
Assert.IsFalse(result);
}

[Test]
public void TupleAnyANdAny_WithAndWhereDefined_WithCorrectlyUsedWithExec()
{
var tuple = Tuple.Create(1, "a");
var result = false;
tuple.Match()
.Where((x, y) => x == 3 && y == "a").Do((x, y) => result = true)
.With(_, _).Or(2, "a").Do((x, y) => result = false)
.Exec();
Assert.IsFalse(result);
}
}
}
}
Loading

0 comments on commit a7389e4

Please sign in to comment.