Skip to content

Commit

Permalink
Added implicit cast for success.
Browse files Browse the repository at this point in the history
  • Loading branch information
jscarle committed Sep 14, 2024
1 parent 8e50960 commit 904339b
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 3 deletions.
6 changes: 3 additions & 3 deletions src/LightResults/LightResults.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<Nullable>enable</Nullable>
<RootNamespace>LightResults</RootNamespace>
<LangVersion>latest</LangVersion>
<Version>8.0.10</Version>
<Version>8.0.11</Version>
<Title>LightResults</Title>
<Authors>Jean-Sebastien Carle</Authors>
<Description>An extremely light and modern Result Pattern library.</Description>
Expand All @@ -19,8 +19,8 @@
<RepositoryUrl>https://github.com/jscarle/LightResults</RepositoryUrl>
<RepositoryType>git</RepositoryType>
<PackageTags>result results pattern fluentresults error handling</PackageTags>
<AssemblyVersion>8.0.10.0</AssemblyVersion>
<FileVersion>8.0.10.0</FileVersion>
<AssemblyVersion>8.0.11.0</AssemblyVersion>
<FileVersion>8.0.11.0</FileVersion>
<NeutralLanguage>en-US</NeutralLanguage>
<IncludeSymbols>true</IncludeSymbols>
<SymbolPackageFormat>snupkg</SymbolPackageFormat>
Expand Down
10 changes: 10 additions & 0 deletions src/LightResults/Result`1.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Immutable;
using System.Diagnostics.CodeAnalysis;
using LightResults.Common;

namespace LightResults;
Expand Down Expand Up @@ -162,4 +163,13 @@ public override string ToString()
var errorString = StringHelper.GetResultErrorString(_errors);
return StringHelper.GetResultString(nameof(Result), "False", errorString);
}

/// <summary>Implicitly converts a value to a success <see cref="Result{TValue}"/>.</summary>
/// <param name="value">The value to convert into a success result.</param>
/// <returns>A new instance of <see cref="Result{TValue}"/> representing a success result with the specified value.</returns>
[SuppressMessage("Usage", "CA2225: Operator overloads have named alternates", Justification = $"{nameof(Ok)} is the named alternate.")]
public static implicit operator Result<TValue>(TValue value)
{
return Ok(value);
}
}
28 changes: 28 additions & 0 deletions tests/LightResults.Tests/ResultTValueTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using FluentAssertions;
using FluentAssertions.Execution;
using Xunit;

namespace LightResults.Tests;
Expand Down Expand Up @@ -422,5 +423,32 @@ public void ToString_ShouldReturnProperRepresentationForUInt128(bool success, st
}
#endif


[Fact]
public void ImplicitOperator_ShouldCreateSuccessResultWithValue()
{
// Arrange
const int value = 42;

// Act
Result<int> result = value;

// Assert
using (new AssertionScope())
{
result.IsSuccess
.Should()
.BeTrue();
result.Value.Should()
.Be(value);
result.IsFailed
.Should()
.BeFalse();
result.Errors
.Should()
.BeEmpty();
}
}

private class ValidationError(string errorMessage) : Error(errorMessage);
}

0 comments on commit 904339b

Please sign in to comment.