-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#36 - Started adding cons pattern matching.
This is taking a TDD approach, so code is currently written to make the tests pass and needs refactoring.
- Loading branch information
Showing
9 changed files
with
180 additions
and
2 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,13 @@ | ||
using System.Collections.Generic; | ||
|
||
namespace SuccincT.PatternMatchers | ||
{ | ||
internal class ConsActionMatcher<T> : IConsActionMatcher<T> | ||
{ | ||
private readonly IEnumerable<T> _collection; | ||
|
||
public ConsActionMatcher(IEnumerable<T> collection) => _collection = collection; | ||
|
||
public IConsFuncMatcher<T, TResult> To<TResult>() => new ConsFuncMatcher<T, TResult>(_collection); | ||
} | ||
} |
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,63 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
|
||
namespace SuccincT.PatternMatchers | ||
{ | ||
internal class ConsFuncMatcher<T, TResult> : IConsFuncMatcher<T, TResult>, | ||
IConsFuncNoneHandler<T, TResult>, | ||
IConsFuncSingleHandler<T, TResult>, | ||
IConsFuncSingleWhereHandler<T, TResult> | ||
{ | ||
private readonly IEnumerable<T> _collection; | ||
private TResult _emptyValue; | ||
private Func<T, bool> _whereFunc; | ||
private List<(Func<T, bool> testFunc, TResult value)> _singleTests; | ||
|
||
internal ConsFuncMatcher(IEnumerable<T> collection) | ||
{ | ||
_collection = collection; | ||
_singleTests = new List<(Func<T, bool> testFunc, TResult value)>(); | ||
} | ||
|
||
IConsFuncNoneHandler<T, TResult> IConsFuncMatcher<T, TResult>.Empty() => this; | ||
IConsFuncSingleHandler<T, TResult> IConsFuncMatcher<T, TResult>.Single() => this; | ||
|
||
TResult IConsFuncMatcher<T, TResult>.Result() => _collection.Count() == 0 ? _emptyValue : SingleMatch(); | ||
|
||
IConsFuncMatcher<T, TResult> IConsFuncNoneHandler<T, TResult>.Do(TResult value) | ||
{ | ||
_emptyValue = value; | ||
return this; | ||
} | ||
|
||
IConsFuncMatcher<T, TResult> IConsFuncSingleHandler<T, TResult>.Do(TResult value) | ||
{ | ||
_singleTests.Add((x => true, value)); | ||
return this; | ||
} | ||
|
||
IConsFuncSingleWhereHandler<T, TResult> IConsFuncSingleHandler<T, TResult>.Where(Func<T, bool> testFunc) | ||
{ | ||
_whereFunc = testFunc; | ||
return this; | ||
} | ||
|
||
IConsFuncMatcher<T, TResult> IConsFuncSingleWhereHandler<T, TResult>.Do(TResult value) | ||
{ | ||
_singleTests.Add((_whereFunc, value)); | ||
return this; | ||
} | ||
private TResult SingleMatch() | ||
{ | ||
foreach(var (testFunc, value) in _singleTests) | ||
{ | ||
if (testFunc(_collection.First())) | ||
{ | ||
return value; | ||
} | ||
} | ||
return default(TResult); | ||
} | ||
} | ||
} |
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,7 @@ | ||
namespace SuccincT.PatternMatchers | ||
{ | ||
public interface IConsActionMatcher<T> | ||
{ | ||
IConsFuncMatcher<T, TResult> To<TResult>(); | ||
} | ||
} |
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,11 @@ | ||
namespace SuccincT.PatternMatchers | ||
{ | ||
public interface IConsFuncMatcher<T, TResult> | ||
{ | ||
IConsFuncNoneHandler<T, TResult> Empty(); | ||
|
||
IConsFuncSingleHandler<T, TResult> Single(); | ||
|
||
TResult Result(); | ||
} | ||
} |
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,7 @@ | ||
namespace SuccincT.PatternMatchers | ||
{ | ||
public interface IConsFuncNoneHandler<T, TResult> | ||
{ | ||
IConsFuncMatcher<T, TResult> Do(TResult value); | ||
} | ||
} |
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,7 @@ | ||
namespace SuccincT.PatternMatchers | ||
{ | ||
public interface IConsFuncSingleWhereHandler<T, TResult> | ||
{ | ||
IConsFuncMatcher<T, TResult> Do(TResult value); | ||
} | ||
} |
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,11 @@ | ||
using System; | ||
|
||
namespace SuccincT.PatternMatchers | ||
{ | ||
public interface IConsFuncSingleHandler<T, TResult> | ||
{ | ||
IConsFuncMatcher<T, TResult> Do(TResult value); | ||
|
||
IConsFuncSingleWhereHandler<T, TResult> Where(Func<T, bool> testFunc); | ||
} | ||
} |
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
55 changes: 55 additions & 0 deletions
55
tests/SuccincT.Tests/SuccincT/PatternMatchers/ConsFuncMatcherTests.cs
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,55 @@ | ||
using System.Collections.Generic; | ||
using NUnit.Framework; | ||
using SuccincT.PatternMatchers; | ||
using static NUnit.Framework.Assert; | ||
|
||
namespace SuccincTTests.SuccincT.PatternMatchers | ||
{ | ||
[TestFixture] | ||
public class ConsFuncMatcherTests | ||
{ | ||
[Test] | ||
public void EmptyList_CanBeMatchedWithEmpty() | ||
{ | ||
var list = new List<int>(); | ||
var result = list.Match().To<int>() | ||
.Empty().Do(0) | ||
.Single().Do(1) | ||
.Result(); | ||
AreEqual(0, result); | ||
} | ||
|
||
[Test] | ||
public void SingleItemList_CanBeMatchedWithSingle() | ||
{ | ||
var list = new List<int> { 1 }; | ||
var result = list.Match().To<bool>() | ||
.Empty().Do(false) | ||
.Single().Do(true) | ||
.Result(); | ||
IsTrue(result); | ||
} | ||
|
||
[Test] | ||
public void SingleItemList_CanBeMatchedWithWhere() | ||
{ | ||
var list = new List<int> { 1 }; | ||
var result = list.Match().To<bool>() | ||
.Single().Where(x => x == 1).Do(true) | ||
.Single().Do(false) | ||
.Result(); | ||
IsTrue(result); | ||
} | ||
|
||
[Test] | ||
public void SingleItemList_CanBeMatchedWhenWhereDoesntMatch() | ||
{ | ||
var list = new List<int> { 0 }; | ||
var result = list.Match().To<bool>() | ||
.Single().Where(x => x == 1).Do(true) | ||
.Single().Do(false) | ||
.Result(); | ||
IsFalse(result); | ||
} | ||
} | ||
} |