Skip to content

Commit

Permalink
#49 - Added TryGetValue() to unions
Browse files Browse the repository at this point in the history
Added TryGetValue() methods to the union types. These return the value as a Option<T>.Some, or OPtion<T>.None if the type doesn't match any of the union cases.
  • Loading branch information
DavidArno committed Nov 26, 2018
1 parent 878d2a3 commit 5d55009
Show file tree
Hide file tree
Showing 6 changed files with 139 additions and 7 deletions.
21 changes: 17 additions & 4 deletions src/SuccincT/Unions/Union{T1,T2,T3,T4}.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using SuccincT.Functional;
using SuccincT.Options;
using SuccincT.Unions.PatternMatchers;

namespace SuccincT.Unions
Expand Down Expand Up @@ -45,14 +46,26 @@ public TResult Value<TResult>()
{
switch (typeof(TResult))
{
case var t when t == typeof(T1): return (TResult)(object)Case1;
case var t when t == typeof(T2): return (TResult)(object)Case2;
case var t when t == typeof(T3): return (TResult)(object)Case3;
case var t when t == typeof(T4): return (TResult)(object)Case4;
case var t when t == typeof(T1) && Case1 is TResult value: return value;
case var t when t == typeof(T2) && Case2 is TResult value: return value;
case var t when t == typeof(T3) && Case3 is TResult value: return value;
case var t when t == typeof(T4) && Case4 is TResult value: return value;
default: throw new InvalidCaseOfTypeException(typeof(TResult));
}
}

public Option<TResult> TryGetValue<TResult>()
{
switch (typeof(TResult))
{
case var t when t == typeof(T1) && Case1 is TResult value: return value;
case var t when t == typeof(T2) && Case2 is TResult value: return value;
case var t when t == typeof(T3) && Case3 is TResult value: return value;
case var t when t == typeof(T4) && Case4 is TResult value: return value;
default: return Option<TResult>.None();
}
}

public IUnionFuncPatternMatcher<T1, T2, T3, T4, TResult> Match<TResult>()
=> new UnionPatternMatcher<T1, T2, T3, T4, TResult>(this);

Expand Down
18 changes: 15 additions & 3 deletions src/SuccincT/Unions/Union{T1,T2,T3}.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using SuccincT.Functional;
using SuccincT.Options;
using SuccincT.Unions.PatternMatchers;

namespace SuccincT.Unions
Expand Down Expand Up @@ -37,13 +38,24 @@ public TResult Value<TResult>()
{
switch (typeof(TResult))
{
case var t when t == typeof(T1): return (TResult)(object)Case1;
case var t when t == typeof(T2): return (TResult)(object)Case2;
case var t when t == typeof(T3): return (TResult)(object)Case3;
case var t when t == typeof(T1) && Case1 is TResult value: return value;
case var t when t == typeof(T2) && Case2 is TResult value: return value;
case var t when t == typeof(T3) && Case3 is TResult value: return value;
default: throw new InvalidCaseOfTypeException(typeof(TResult));
}
}

public Option<TResult> TryGetValue<TResult>()
{
switch (typeof(TResult))
{
case var t when t == typeof(T1) && Case1 is TResult value: return value;
case var t when t == typeof(T2) && Case2 is TResult value: return value;
case var t when t == typeof(T3) && Case3 is TResult value: return value;
default: return Option<TResult>.None();
}
}

public IUnionFuncPatternMatcher<T1, T2, T3, TResult> Match<TResult>() =>
new UnionFuncPatternMatcher<T1, T2, T3, TResult>(this);

Expand Down
11 changes: 11 additions & 0 deletions src/SuccincT/Unions/Union{T1,T2}.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using SuccincT.Functional;
using SuccincT.Options;
using SuccincT.Unions.PatternMatchers;

namespace SuccincT.Unions
Expand Down Expand Up @@ -35,6 +36,16 @@ public TResult Value<TResult>()
}
}

public Option<TResult> TryGetValue<TResult>()
{
switch (typeof(TResult))
{
case var t when t == typeof(T1) && Case1 is TResult value: return value;
case var t when t == typeof(T2) && Case2 is TResult value: return value;
default: return Option<TResult>.None();
}
}

public IUnionFuncPatternMatcher<T1, T2, TResult> Match<TResult>()
=> new UnionPatternMatcher<T1, T2, TResult>(this);

Expand Down
24 changes: 24 additions & 0 deletions tests/SuccincT.Tests/SuccincT/Unions/UnionT1T2DirectValueTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,14 @@ public void UnionWithT1_ValueMatchesCorrectly()
AreEqual(1, result);
}

[Test]
public void UnionWithT1_TryGetValueMatchesCorrectly()
{
var union = new Union<int, string>(1);
var result = union.TryGetValue<int>();
AreEqual(1, result.Value);
}

[Test]
public void UnionWithT2_ValueMatchesCorrectly()
{
Expand All @@ -22,11 +30,27 @@ public void UnionWithT2_ValueMatchesCorrectly()
AreEqual("string", result);
}

[Test]
public void UnionWithT2_TryGetValueMatchesCorrectly()
{
var union = new Union<int, string>("string");
var result = union.TryGetValue<string>();
AreEqual("string", result.Value);
}

[Test]
public void UnionT1T2WithInvalidTypeValue_ThrowsException()
{
var union = new Union<int, string>(2);
Throws<InvalidCaseOfTypeException>(() => union.Value<float>());
}

[Test]
public void UnionT1T2WithInvalidTypeTryGetValue_ReturnNone()
{
var union = new Union<int, string>(2);
var result = union.TryGetValue<float>();
IsFalse(result.HasValue);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,14 @@ public void UnionWithT1_ValueMatchesCorrectly()
AreEqual(1, result);
}

[Test]
public void UnionWithT1_TryGetValueMatchesCorrectly()
{
var union = new Union<int, string, Plants>(1);
var result = union.TryGetValue<int>();
AreEqual(1, result.Value);
}

[Test]
public void UnionWithT2_ValueMatchesCorrectly()
{
Expand All @@ -24,6 +32,14 @@ public void UnionWithT2_ValueMatchesCorrectly()
AreEqual("string", result);
}

[Test]
public void UnionWithT2_TryGetValueMatchesCorrectly()
{
var union = new Union<int, string, Plants>("string");
var result = union.TryGetValue<string>();
AreEqual("string", result.Value);
}

[Test]
public void UnionWithT3_ValueMatchesCorrectly()
{
Expand All @@ -32,11 +48,27 @@ public void UnionWithT3_ValueMatchesCorrectly()
AreEqual(Plants.Rose, result);
}

[Test]
public void UnionWithT3_TryGetValueMatchesCorrectly()
{
var union = new Union<int, string, Plants>(Plants.Rose);
var result = union.TryGetValue<Plants>();
AreEqual(Plants.Rose, result.Value);
}

[Test]
public void UnionT1T2T3WithInvalidTypeValue_ThrowsException()
{
var union = new Union<int, string, Plants>(2);
Throws<InvalidCaseOfTypeException>(() => union.Value<float>());
}

[Test]
public void UnionT1T2T3WithInvalidTryGetTypeValue_ReturnsNone()
{
var union = new Union<int, string, Plants>(2);
var result = union.TryGetValue<float>();
IsFalse(result.HasValue);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,14 @@ public void UnionWithT1_ValueMatchesCorrectly()
AreEqual(1, result);
}

[Test]
public void UnionWithT1_TryGetValueMatchesCorrectly()
{
var union = new Union<int, string, Plants, Foods>(1);
var result = union.TryGetValue<int>();
AreEqual(1, result.Value);
}

[Test]
public void UnionWithT2_ValueMatchesCorrectly()
{
Expand All @@ -25,6 +33,14 @@ public void UnionWithT2_ValueMatchesCorrectly()
AreEqual("string", result);
}

[Test]
public void UnionWithT2_TryGetValueMatchesCorrectly()
{
var union = new Union<int, string, Plants, Foods>("string");
var result = union.TryGetValue<string>();
AreEqual("string", result.Value);
}

[Test]
public void UnionWithT3_ValueMatchesCorrectly()
{
Expand All @@ -33,6 +49,14 @@ public void UnionWithT3_ValueMatchesCorrectly()
AreEqual(Plants.Rose, result);
}

[Test]
public void UnionWithT3_TryGetValueMatchesCorrectly()
{
var union = new Union<int, string, Plants, Foods>(Plants.Rose);
var result = union.TryGetValue<Plants>();
AreEqual(Plants.Rose, result.Value);
}

[Test]
public void UnionWithT4_ValueMatchesCorrectly()
{
Expand All @@ -41,11 +65,27 @@ public void UnionWithT4_ValueMatchesCorrectly()
AreEqual(Foods.Cake, result);
}

[Test]
public void UnionWithT4_TryGetValueMatchesCorrectly()
{
var union = new Union<int, string, Plants, Foods>(Foods.Cake);
var result = union.TryGetValue<Foods>();
AreEqual(Foods.Cake, result.Value);
}

[Test]
public void UnionT1T2T3WithInvalidTypeValue_ThrowsException()
{
var union = new Union<int, string, Plants, Foods>(2);
Throws<InvalidCaseOfTypeException>(() => union.Value<float>());
}

[Test]
public void UnionT1T2T3WithInvalidTypeTryGetValue_ReturnsNone()
{
var union = new Union<int, string, Plants, Foods>(2);
var result = union.TryGetValue<float>();
IsFalse(result.HasValue);
}
}
}

0 comments on commit 5d55009

Please sign in to comment.