Skip to content

Commit a2decac

Browse files
Added ToOptional and ToOptionalAsync methods that work with reference types and value types, and renamed ToSuccessResult and ToSuccessResultAsync to ToSuccess and ToSuccessAsync respectively. (#95)
Added ToOptional and ToOptionalAsync methods that work with reference types and value types, and renamed ToSuccessResult and ToSuccessResultAsync to ToSuccess and ToSuccessAsync respectively.
1 parent 18ee3d9 commit a2decac

File tree

3 files changed

+155
-11
lines changed

3 files changed

+155
-11
lines changed

Directory.Build.props

+3-3
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88
<NeutralLanguage>en</NeutralLanguage>
99
<Copyright>Copyright © ONIXLabs 2020</Copyright>
1010
<RepositoryUrl>https://github.com/onix-labs/onixlabs-dotnet</RepositoryUrl>
11-
<Version>11.3.0</Version>
12-
<PackageVersion>11.3.0</PackageVersion>
13-
<AssemblyVersion>11.3.0</AssemblyVersion>
11+
<Version>12.0.0</Version>
12+
<PackageVersion>12.0.0</PackageVersion>
13+
<AssemblyVersion>12.0.0</AssemblyVersion>
1414
</PropertyGroup>
1515
</Project>

OnixLabs.Core.UnitTests/ObjectExtensionTests.cs

+114-6
Original file line numberDiff line numberDiff line change
@@ -225,29 +225,137 @@ public void ToStringOrNullShouldProduceExpectedResultWhenObjectIsNotNull()
225225
Assert.Equal(expected, actual);
226226
}
227227

228-
[Fact(DisplayName = "ToSuccessResult should produce the expected result")]
229-
public void ToSuccessResultShouldProduceTheExpectedResult()
228+
[Fact(DisplayName = "ToOptional should produce the expected result when using a non-null reference type")]
229+
public void ToOptionalShouldProduceExpectedResultWhenUsingNonNullReferenceType()
230230
{
231231
// Given
232232
const string expected = "abc";
233233

234234
// When
235-
Result<string> result = expected.ToSuccessResult();
235+
Optional<string> optional = expected.ToOptional();
236+
237+
// Then
238+
Some<string> some = Assert.IsType<Some<string>>(optional);
239+
Assert.Equal(expected, some.Value);
240+
}
241+
242+
[Fact(DisplayName = "ToOptional should produce the expected result when using a null reference type")]
243+
public void ToOptionalShouldProduceExpectedResultWhenUsingNullReferenceType()
244+
{
245+
// Given
246+
const string? expected = null;
247+
248+
// When
249+
Optional<string> optional = expected.ToOptional();
250+
251+
// Then
252+
Assert.IsType<None<string>>(optional);
253+
}
254+
255+
[Fact(DisplayName = "ToOptional should produce the expected result when using a non-null value type")]
256+
public void ToOptionalShouldProduceExpectedResultWhenUsingNonNullValueType()
257+
{
258+
// Given
259+
const int expected = 123;
260+
261+
// When
262+
Optional<int> optional = expected.ToOptional();
263+
264+
// Then
265+
Some<int> some = Assert.IsType<Some<int>>(optional);
266+
Assert.Equal(expected, some.Value);
267+
}
268+
269+
[Fact(DisplayName = "ToOptional should produce the expected result when using a null value type")]
270+
public void ToOptionalShouldProduceExpectedResultWhenUsingNullValueType()
271+
{
272+
// Given
273+
int? expected = null;
274+
275+
// When
276+
Optional<int> optional = expected.ToOptional();
277+
278+
// Then
279+
Assert.IsType<None<int>>(optional);
280+
}
281+
282+
[Fact(DisplayName = "ToOptionalAsync should produce the expected result when using a non-null reference type")]
283+
public async Task ToOptionalAsyncShouldProduceExpectedResultWhenUsingNonNullReferenceType()
284+
{
285+
// Given
286+
const string expected = "abc";
287+
288+
// When
289+
Optional<string> optional = await Task.FromResult<string?>(expected).ToOptionalAsync();
290+
291+
// Then
292+
Some<string> some = Assert.IsType<Some<string>>(optional);
293+
Assert.Equal(expected, some.Value);
294+
}
295+
296+
[Fact(DisplayName = "ToOptionalAsync should produce the expected result when using a null reference type")]
297+
public async Task ToOptionalAsyncShouldProduceExpectedResultWhenUsingNullReferenceType()
298+
{
299+
// Given
300+
const string? expected = null;
301+
302+
// When
303+
Optional<string> optional = await Task.FromResult(expected).ToOptionalAsync();
304+
305+
// Then
306+
Assert.IsType<None<string>>(optional);
307+
}
308+
309+
[Fact(DisplayName = "ToOptionalAsync should produce the expected result when using a non-null value type")]
310+
public async Task ToOptionalAsyncShouldProduceExpectedResultWhenUsingNonNullValueType()
311+
{
312+
// Given
313+
const int expected = 123;
314+
315+
// When
316+
Optional<int> optional = await Task.FromResult(expected).ToOptionalAsync();
317+
318+
// Then
319+
Some<int> some = Assert.IsType<Some<int>>(optional);
320+
Assert.Equal(expected, some.Value);
321+
}
322+
323+
[Fact(DisplayName = "ToOptionalAsync should produce the expected result when using a null value type")]
324+
public async Task ToOptionalAsyncShouldProduceExpectedResultWhenUsingNullValueType()
325+
{
326+
// Given
327+
int? expected = null;
328+
329+
// When
330+
Optional<int> optional = await Task.FromResult(expected).ToOptionalAsync();
331+
332+
// Then
333+
Assert.IsType<None<int>>(optional);
334+
}
335+
336+
[Fact(DisplayName = "ToSuccess should produce the expected result")]
337+
public void ToSuccessShouldProduceTheExpectedResult()
338+
{
339+
// Given
340+
const string expected = "abc";
341+
342+
// When
343+
Result<string> result = expected.ToSuccess();
236344

237345
// Then
238346
Success<string> success = Assert.IsType<Success<string>>(result);
239347
Assert.Equal(expected, success.Value);
240348
}
241349

242-
[Fact(DisplayName = "ToSuccessResultAsync should produce the expected result")]
243-
public async Task ToSuccessResultAsyncShouldProduceTheExpectedResult()
350+
[Fact(DisplayName = "ToSuccessAsync should produce the expected result")]
351+
public async Task ToSuccessAsyncShouldProduceTheExpectedResult()
244352
{
245353
// Given
246354
const string expected = "abc";
247355

248356
// When
249357
Task<string> task = Task.FromResult(expected);
250-
Result<string> result = await task.ToSuccessResultAsync();
358+
Result<string> result = await task.ToSuccessAsync();
251359

252360
// Then
253361
Success<string> success = Assert.IsType<Success<string>>(result);

OnixLabs.Core/Extensions.Object.cs

+38-2
Original file line numberDiff line numberDiff line change
@@ -173,13 +173,49 @@ public static string ToRecordString(this object? value)
173173
/// <returns>Returns a <see cref="String"/> representation of the current <see cref="Object"/>, or a string literal null if the current object is <see langword="null"/>.</returns>
174174
public static string ToStringOrNull(this object? value) => value?.ToString() ?? Null;
175175

176+
/// <summary>
177+
/// Obtains an <see cref="Optional{T}"/> representation of the current <see cref="Object"/>.
178+
/// </summary>
179+
/// <param name="value">The <see cref="Object"/> to wrap as an <see cref="Optional{T}"/> value.</param>
180+
/// <typeparam name="T">The underlying type of the value.</typeparam>
181+
/// <returns>Returns an <see cref="Optional{T}"/> representation of the current <see cref="Object"/>.</returns>
182+
public static Optional<T> ToOptional<T>(this T? value) where T : notnull => Optional<T>.Of(value);
183+
184+
/// <summary>
185+
/// Obtains an <see cref="Optional{T}"/> representation of the current <see cref="Object"/>.
186+
/// </summary>
187+
/// <param name="value">The <see cref="Object"/> to wrap as an <see cref="Optional{T}"/> value.</param>
188+
/// <typeparam name="T">The underlying type of the value.</typeparam>
189+
/// <returns>Returns an <see cref="Optional{T}"/> representation of the current <see cref="Object"/>.</returns>
190+
public static Optional<T> ToOptional<T>(this T? value) where T : struct => Optional<T>.Of(value);
191+
192+
/// <summary>
193+
/// Asynchronously obtains an <see cref="Optional{T}"/> representation of the current <see cref="Object"/>.
194+
/// </summary>
195+
/// <param name="value">The <see cref="Object"/> to wrap as an <see cref="Optional{T}"/> value.</param>
196+
/// /// <param name="token">The cancellation token that can be used to cancel long-running tasks.</param>
197+
/// <typeparam name="T">The underlying type of the value.</typeparam>
198+
/// <returns>Returns an <see cref="Optional{T}"/> representation of the current <see cref="Object"/>.</returns>
199+
public static async Task<Optional<T>> ToOptionalAsync<T>(this Task<T?> value, CancellationToken token = default) where T : notnull =>
200+
Optional<T>.Of(await value.WaitAsync(token).ConfigureAwait(false));
201+
202+
/// <summary>
203+
/// Asynchronously obtains an <see cref="Optional{T}"/> representation of the current <see cref="Object"/>.
204+
/// </summary>
205+
/// <param name="value">The <see cref="Object"/> to wrap as an <see cref="Optional{T}"/> value.</param>
206+
/// /// <param name="token">The cancellation token that can be used to cancel long-running tasks.</param>
207+
/// <typeparam name="T">The underlying type of the value.</typeparam>
208+
/// <returns>Returns an <see cref="Optional{T}"/> representation of the current <see cref="Object"/>.</returns>
209+
public static async Task<Optional<T>> ToOptionalAsync<T>(this Task<T?> value, CancellationToken token = default) where T : struct =>
210+
Optional<T>.Of(await value.WaitAsync(token).ConfigureAwait(false));
211+
176212
/// <summary>
177213
/// Obtains a <see cref="Success{T}"/> representation of the current <see cref="Object"/>.
178214
/// </summary>
179215
/// <param name="value">The <see cref="Object"/> to wrap as a <see cref="Success{T}"/> result.</param>
180216
/// <typeparam name="T">The underlying type of the value.</typeparam>
181217
/// <returns>Returns a <see cref="Success{T}"/> representation of the current <see cref="Object"/>.</returns>
182-
public static Result<T> ToSuccessResult<T>(this T value) => Result<T>.Success(value);
218+
public static Success<T> ToSuccess<T>(this T value) => Result<T>.Success(value);
183219

184220
/// <summary>
185221
/// Asynchronously obtains a <see cref="Success{T}"/> representation of the current <see cref="Object"/>.
@@ -188,6 +224,6 @@ public static string ToRecordString(this object? value)
188224
/// <param name="token">The cancellation token that can be used to cancel long-running tasks.</param>
189225
/// <typeparam name="T">The underlying type of the value.</typeparam>
190226
/// <returns>Returns a <see cref="Success{T}"/> representation of the current <see cref="Object"/>.</returns>
191-
public static async Task<Result<T>> ToSuccessResultAsync<T>(this Task<T> value, CancellationToken token = default) =>
227+
public static async Task<Success<T>> ToSuccessAsync<T>(this Task<T> value, CancellationToken token = default) =>
192228
Result<T>.Success(await value.WaitAsync(token).ConfigureAwait(false));
193229
}

0 commit comments

Comments
 (0)