-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add unit tests for provided types (covering additional/custom methods)
- Loading branch information
Showing
7 changed files
with
233 additions
and
0 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
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,18 @@ | ||
namespace W4k.Either; | ||
|
||
public class EitherShould | ||
{ | ||
[Fact] | ||
public void BeLeft() | ||
{ | ||
Either<int, string> either = 42; | ||
Assert.True(either.IsLeft); | ||
} | ||
|
||
[Fact] | ||
public void BeRight() | ||
{ | ||
Either<int, string> either = "foo"; | ||
Assert.True(either.IsRight); | ||
} | ||
} |
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,22 @@ | ||
namespace W4k.Either; | ||
|
||
public class MaybeShould | ||
{ | ||
[Fact] | ||
public void BeCreatedWithSome() | ||
{ | ||
var maybe = Maybe.Some(42); | ||
|
||
Assert.True(maybe.HasValue); | ||
Assert.Equal(42, maybe.Value); | ||
} | ||
|
||
[Fact] | ||
public void BeCreatedWithNone() | ||
{ | ||
var maybe = Maybe.None<int>(); | ||
|
||
Assert.False(maybe.HasValue); | ||
Assert.Equal(default, maybe.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,87 @@ | ||
namespace W4k.Either; | ||
|
||
public class OptionalResultShould | ||
{ | ||
[Fact] | ||
public void CreateSuccessResultWithValue() | ||
{ | ||
const string value = "le value"; | ||
var result = OptionalResult.Success<string, IError>(value); | ||
|
||
Assert.True(result.IsSuccess); | ||
Assert.True(result.HasValue); | ||
Assert.False(result.IsFailed); | ||
|
||
Assert.Equal(value, result.Value); | ||
Assert.Throws<InvalidOperationException>(() => result.Error); | ||
} | ||
|
||
[Fact] | ||
public void CreateSuccessResultWithoutValue() | ||
{ | ||
var result = OptionalResult.Empty<string, IError>(); | ||
|
||
Assert.True(result.IsSuccess); | ||
Assert.False(result.HasValue); | ||
Assert.False(result.IsFailed); | ||
|
||
Assert.Equal(default, result.Value); | ||
} | ||
|
||
[Fact] | ||
public void CreateFailedResult() | ||
{ | ||
var error = new TestError(); | ||
var result = OptionalResult.Failed<string, IError>(error); | ||
|
||
Assert.False(result.IsSuccess); | ||
Assert.False(result.HasValue); | ||
Assert.True(result.IsFailed); | ||
|
||
Assert.Throws<InvalidOperationException>(() => result.Value); | ||
Assert.Equal(error, result.Error); | ||
} | ||
|
||
[Fact] | ||
public void DeconstructSuccessResultWithValue() | ||
{ | ||
var result = OptionalResult.Success<string, IError>("le success"); | ||
|
||
var (v, e) = result; | ||
|
||
Assert.NotNull(v); | ||
Assert.Null(e); | ||
} | ||
|
||
[Fact] | ||
public void DeconstructSuccessResultWithoutValue() | ||
{ | ||
var result = OptionalResult.Empty<string, IError>(); | ||
|
||
var (v, e) = result; | ||
|
||
Assert.Null(v); | ||
Assert.Null(e); | ||
} | ||
|
||
[Fact] | ||
public void DeconstructFailedResultWithValue() | ||
{ | ||
var result = OptionalResult.Failed<string, IError>(new TestError()); | ||
|
||
var (v, e) = result; | ||
|
||
Assert.Null(v); | ||
Assert.NotNull(e); | ||
} | ||
|
||
private interface IError | ||
{ | ||
public string Message { get; } | ||
} | ||
|
||
private sealed class TestError : IError | ||
{ | ||
public string Message => "le error"; | ||
} | ||
} |
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,83 @@ | ||
namespace W4k.Either; | ||
|
||
public class ResultShould | ||
{ | ||
[Fact] | ||
public void CreateSuccessResultWithoutValue() | ||
{ | ||
var result = Result.Success<IError>(); | ||
|
||
Assert.True(result.IsSuccess); | ||
Assert.False(result.IsFailed); | ||
} | ||
|
||
[Fact] | ||
public void CreateFailedResultWithoutValue() | ||
{ | ||
var error = new TestError(); | ||
var result = Result.Failed<IError>(error); | ||
|
||
Assert.False(result.IsSuccess); | ||
Assert.True(result.IsFailed); | ||
|
||
Assert.Equal(error, result.Error); | ||
} | ||
|
||
[Fact] | ||
public void CreateSuccessResultWithValue() | ||
{ | ||
const string value = "le success"; | ||
var result = Result.Success<string, IError>(value); | ||
|
||
Assert.True(result.IsSuccess); | ||
Assert.False(result.IsFailed); | ||
|
||
Assert.Equal(value, result.Value); | ||
Assert.Throws<InvalidOperationException>(() => result.Error); | ||
} | ||
|
||
[Fact] | ||
public void CreateFailedResultWithValue() | ||
{ | ||
var error = new TestError(); | ||
var result = Result.Failed<string, IError>(error); | ||
|
||
Assert.False(result.IsSuccess); | ||
Assert.True(result.IsFailed); | ||
|
||
Assert.Throws<InvalidOperationException>(() => result.Value); | ||
Assert.Equal(error, result.Error); | ||
} | ||
|
||
[Fact] | ||
public void DeconstructSuccessResultWithValue() | ||
{ | ||
var result = Result.Success<string, IError>("le success"); | ||
|
||
var (v, e) = result; | ||
|
||
Assert.NotNull(v); | ||
Assert.Null(e); | ||
} | ||
|
||
[Fact] | ||
public void DeconstructFailedResultWithValue() | ||
{ | ||
var result = Result.Failed<string, IError>(new TestError()); | ||
|
||
var (v, e) = result; | ||
|
||
Assert.Null(v); | ||
Assert.NotNull(e); | ||
} | ||
|
||
private interface IError | ||
{ | ||
public string Message { get; } | ||
} | ||
|
||
private sealed class TestError : IError | ||
{ | ||
public string Message => "le error"; | ||
} | ||
} |
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 @@ | ||
global using Xunit; |
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,15 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net6.0</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
<IsPackable>false</IsPackable> | ||
<RootNamespace>W4k.Either</RootNamespace> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\..\src\W4k.Either\W4k.Either.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |