Skip to content

Commit 4f5d9e6

Browse files
committed
Merge branch 'features/Conversions'
2 parents 0d4b161 + d0fad06 commit 4f5d9e6

File tree

7 files changed

+489
-16
lines changed

7 files changed

+489
-16
lines changed
Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
using NUnit.Framework;
2+
using Here.Maybes;
3+
using Here.Results;
4+
using Here.Tests.Results;
5+
6+
namespace Here.Tests.Maybes
7+
{
8+
/// <summary>
9+
/// Tests for <see cref="Maybe{T}"/> conversions.
10+
/// </summary>
11+
[TestFixture]
12+
internal class MaybeConversionsTests : ResultTestsBase
13+
{
14+
[Test]
15+
public void MaybeToResult()
16+
{
17+
// Maybe has value
18+
// Explicit conversion
19+
var maybeInt = Maybe<int>.Some(42);
20+
Result result = maybeInt.ToResult();
21+
CheckResultOk(result);
22+
23+
result = maybeInt.ToResult("Empty maybeInt");
24+
CheckResultOk(result);
25+
26+
// Implicit conversion
27+
result = maybeInt;
28+
CheckResultOk(result);
29+
30+
// Empty Maybe
31+
// Explicit conversion
32+
var emptyMaybeInt = Maybe<int>.None;
33+
result = emptyMaybeInt.ToResult();
34+
CheckResultFail(result, string.Format(Maybe<int>.FailedToResultMessage, typeof(int)));
35+
36+
result = emptyMaybeInt.ToResult("Empty emptyMaybeInt");
37+
CheckResultFail(result, "Empty emptyMaybeInt");
38+
39+
// Implicit conversion
40+
result = emptyMaybeInt;
41+
CheckResultFail(result, string.Format(Maybe<int>.FailedToResultMessage, typeof(int)));
42+
}
43+
44+
[Test]
45+
public void MaybeToValueResult()
46+
{
47+
// Maybe has value
48+
// Explicit conversion
49+
var maybeInt = Maybe<int>.Some(42);
50+
Result<int> result = maybeInt.ToValueResult();
51+
CheckResultOk(result, 42);
52+
53+
result = maybeInt.ToValueResult("Empty maybeInt");
54+
CheckResultOk(result, 42);
55+
56+
// Implicit conversion
57+
result = maybeInt;
58+
CheckResultOk(result, 42);
59+
60+
// Empty Maybe
61+
// Explicit conversion
62+
var emptyMaybeInt = Maybe<int>.None;
63+
result = emptyMaybeInt.ToValueResult();
64+
CheckResultFail(result, string.Format(Maybe<int>.FailedToResultMessage, typeof(int)));
65+
66+
result = emptyMaybeInt.ToValueResult("Empty emptyMaybeInt");
67+
CheckResultFail(result, "Empty emptyMaybeInt");
68+
69+
// Implicit conversion
70+
result = emptyMaybeInt;
71+
CheckResultFail(result, string.Format(Maybe<int>.FailedToResultMessage, typeof(int)));
72+
}
73+
74+
[Test]
75+
public void MaybeToCustomResult()
76+
{
77+
// Maybe has value
78+
var customErrorObject = new CustomErrorTest { ErrorCode = -32 };
79+
var maybeInt = Maybe<int>.Some(42);
80+
CustomResult<CustomErrorTest> result = maybeInt.ToCustomResult(customErrorObject);
81+
CheckResultOk(result);
82+
83+
result = maybeInt.ToCustomResult(customErrorObject, "Empty maybeInt");
84+
CheckResultOk(result);
85+
86+
result = maybeInt.ToCustomResult(() => customErrorObject);
87+
CheckResultOk(result);
88+
89+
result = maybeInt.ToCustomResult(() => customErrorObject, "Empty maybeInt");
90+
CheckResultOk(result);
91+
92+
// Empty Maybe
93+
var emptyMaybeInt = Maybe<int>.None;
94+
result = emptyMaybeInt.ToCustomResult(customErrorObject);
95+
CheckResultFail(result, string.Format(Maybe<int>.FailedToResultMessage, typeof(int)), customErrorObject);
96+
97+
result = emptyMaybeInt.ToCustomResult(customErrorObject, "Empty emptyMaybeInt");
98+
CheckResultFail(result, "Empty emptyMaybeInt", customErrorObject);
99+
100+
result = emptyMaybeInt.ToCustomResult(() => customErrorObject);
101+
CheckResultFail(result, string.Format(Maybe<int>.FailedToResultMessage, typeof(int)), customErrorObject);
102+
103+
result = emptyMaybeInt.ToCustomResult(() => customErrorObject, "Empty emptyMaybeInt");
104+
CheckResultFail(result, "Empty emptyMaybeInt", customErrorObject);
105+
}
106+
107+
[Test]
108+
public void MaybeToCustomValueResult()
109+
{
110+
// Maybe has value
111+
var customErrorObject = new CustomErrorTest { ErrorCode = -32 };
112+
var maybeInt = Maybe<int>.Some(42);
113+
Result<int, CustomErrorTest> result = maybeInt.ToCustomValueResult(customErrorObject);
114+
CheckResultOk(result, 42);
115+
116+
result = maybeInt.ToCustomValueResult(customErrorObject, "Empty maybeInt");
117+
CheckResultOk(result, 42);
118+
119+
result = maybeInt.ToCustomValueResult(() => customErrorObject);
120+
CheckResultOk(result, 42);
121+
122+
result = maybeInt.ToCustomValueResult(() => customErrorObject, "Empty maybeInt");
123+
CheckResultOk(result, 42);
124+
125+
// Empty Maybe
126+
var emptyMaybeInt = Maybe<int>.None;
127+
result = emptyMaybeInt.ToCustomValueResult(customErrorObject);
128+
CheckResultFail(result, string.Format(Maybe<int>.FailedToResultMessage, typeof(int)), customErrorObject);
129+
130+
result = emptyMaybeInt.ToCustomValueResult(customErrorObject, "Empty emptyMaybeInt");
131+
CheckResultFail(result, "Empty emptyMaybeInt", customErrorObject);
132+
133+
result = emptyMaybeInt.ToCustomValueResult(() => customErrorObject);
134+
CheckResultFail(result, string.Format(Maybe<int>.FailedToResultMessage, typeof(int)), customErrorObject);
135+
136+
result = emptyMaybeInt.ToCustomValueResult(() => customErrorObject, "Empty emptyMaybeInt");
137+
CheckResultFail(result, "Empty emptyMaybeInt", customErrorObject);
138+
}
139+
}
140+
}
Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
using NUnit.Framework;
2+
using Here.Maybes;
3+
using Here.Tests.Maybes;
4+
using Here.Results;
5+
6+
namespace Here.Tests.Results
7+
{
8+
/// <summary>
9+
/// Tests for <see cref="Result"/>, <see cref="Result{T}"/>, <see cref="CustomResult{TError}"/> and <see cref="Result{T, TError}"/> conversions.
10+
/// </summary>
11+
[TestFixture]
12+
internal class ResultConversionsTests : MaybeTestsBase
13+
{
14+
#region Test class
15+
16+
private class CustomErrorTest
17+
{
18+
}
19+
20+
#endregion
21+
22+
[Test]
23+
public void ResultToMaybe()
24+
{
25+
// Explicit conversions
26+
Result resultOK = Result.Ok();
27+
Maybe<bool> maybeBool = resultOK.ToMaybe();
28+
CheckMaybeValue(maybeBool, true);
29+
30+
Result resultWarn = Result.Warn("Warning");
31+
maybeBool = resultWarn.ToMaybe();
32+
CheckMaybeValue(maybeBool, true);
33+
34+
Result resultFail = Result.Fail("Failure");
35+
maybeBool = resultFail.ToMaybe();
36+
CheckMaybeValue(maybeBool, false);
37+
38+
// Implicit conversions
39+
maybeBool = resultOK;
40+
CheckMaybeValue(maybeBool, true);
41+
42+
maybeBool = resultWarn;
43+
CheckMaybeValue(maybeBool, true);
44+
45+
maybeBool = resultFail;
46+
CheckMaybeValue(maybeBool, false);
47+
}
48+
49+
[Test]
50+
public void ValueResultToMaybe()
51+
{
52+
// Explicit conversions
53+
Result<int> resultOK = Result.Ok(42);
54+
Maybe<int> maybeInt = resultOK.ToMaybe();
55+
CheckMaybeValue(maybeInt, 42);
56+
57+
Result<int> resultWarn = Result.Warn(12, "Warning");
58+
maybeInt = resultWarn.ToMaybe();
59+
CheckMaybeValue(maybeInt, 12);
60+
61+
Result<int> resultFail = Result.Fail<int>("Failure");
62+
maybeInt = resultFail.ToMaybe();
63+
CheckEmptyMaybe(maybeInt);
64+
65+
// Implicit conversions
66+
maybeInt = resultOK;
67+
CheckMaybeValue(maybeInt, 42);
68+
69+
maybeInt = resultWarn;
70+
CheckMaybeValue(maybeInt, 12);
71+
72+
maybeInt = resultFail;
73+
CheckEmptyMaybe(maybeInt);
74+
}
75+
76+
[Test]
77+
public void CustomResultToMaybe()
78+
{
79+
// Explicit conversions
80+
CustomResult<CustomErrorTest> resultOK = Result.CustomOk<CustomErrorTest>();
81+
Maybe<bool> maybeBool = resultOK.ToMaybe();
82+
CheckMaybeValue(maybeBool, true);
83+
84+
CustomResult<CustomErrorTest> resultWarn = Result.CustomWarn<CustomErrorTest>("Warning");
85+
maybeBool = resultWarn.ToMaybe();
86+
CheckMaybeValue(maybeBool, true);
87+
88+
CustomResult<CustomErrorTest> resultFail = Result.CustomFail("Failure", new CustomErrorTest());
89+
maybeBool = resultFail.ToMaybe();
90+
CheckMaybeValue(maybeBool, false);
91+
92+
// Implicit conversions
93+
maybeBool = resultOK;
94+
CheckMaybeValue(maybeBool, true);
95+
96+
maybeBool = resultWarn;
97+
CheckMaybeValue(maybeBool, true);
98+
99+
maybeBool = resultFail;
100+
CheckMaybeValue(maybeBool, false);
101+
}
102+
103+
[Test]
104+
public void CustomValueResultToMaybe()
105+
{
106+
// Explicit conversions
107+
Result<int, CustomErrorTest> resultOK = Result.Ok<int, CustomErrorTest>(42);
108+
Maybe<int> maybeInt = resultOK.ToMaybe();
109+
CheckMaybeValue(maybeInt, 42);
110+
111+
Result<int, CustomErrorTest> resultWarn = Result.Warn<int, CustomErrorTest>(12, "Warning");
112+
maybeInt = resultWarn.ToMaybe();
113+
CheckMaybeValue(maybeInt, 12);
114+
115+
Result<int, CustomErrorTest> resultFail = Result.Fail<int, CustomErrorTest>("Failure", new CustomErrorTest());
116+
maybeInt = resultFail.ToMaybe();
117+
CheckEmptyMaybe(maybeInt);
118+
119+
// Implicit conversions
120+
maybeInt = resultOK;
121+
CheckMaybeValue(maybeInt, 42);
122+
123+
maybeInt = resultWarn;
124+
CheckMaybeValue(maybeInt, 12);
125+
126+
maybeInt = resultFail;
127+
CheckEmptyMaybe(maybeInt);
128+
}
129+
}
130+
}

src/Here/Maybe/Maybe.Implicit.cs

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using JetBrains.Annotations;
1+
using Here.Results;
2+
using JetBrains.Annotations;
23

34
namespace Here.Maybes
45
{
@@ -36,5 +37,27 @@ public static implicit operator Maybe<T>(Maybe<Maybe<T>> embeddedMaybe)
3637
{
3738
return embeddedMaybe.HasValue ? embeddedMaybe.Value : None;
3839
}
40+
41+
/// <summary>
42+
/// Implicit convertion from <see cref="Maybe{T}"/> to a <see cref="Result"/>.
43+
/// </summary>
44+
/// <param name="maybe">A <see cref="Maybe{T}"/> to convert.</param>
45+
/// <returns>The corresponding <see cref="Result"/>.</returns>
46+
[PublicAPI, Pure]
47+
public static implicit operator Result(Maybe<T> maybe)
48+
{
49+
return maybe.ToResult();
50+
}
51+
52+
/// <summary>
53+
/// Implicit convertion from <see cref="Maybe{T}"/> to a <see cref="Result{T}"/>.
54+
/// </summary>
55+
/// <param name="maybe">A <see cref="Maybe{T}"/> to convert.</param>
56+
/// <returns>The corresponding <see cref="Result{T}"/>.</returns>
57+
[PublicAPI, Pure]
58+
public static implicit operator Result<T>(Maybe<T> maybe)
59+
{
60+
return maybe.ToValueResult();
61+
}
3962
}
4063
}

0 commit comments

Comments
 (0)