Skip to content

Commit

Permalink
Merge pull request #2 from futurum-dev/feature/adding-tests
Browse files Browse the repository at this point in the history
Adding tests and Result and Option WithValueAssertion methods
  • Loading branch information
futurum-dev authored Jan 29, 2022
2 parents f96f87c + e644980 commit 0ea889c
Show file tree
Hide file tree
Showing 8 changed files with 819 additions and 1 deletion.
8 changes: 7 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,10 @@ jobs:
run: dotnet build --configuration Release --no-restore

- name: Test on .NET 6
run: dotnet test --no-restore --verbosity normal /p:CollectCoverage=true /p:CoverletOutput=TestResults/ /p:CoverletOutputFormat=lcov
run: dotnet test --no-restore --verbosity normal /p:CollectCoverage=true /p:CoverletOutput=TestResults/ /p:CoverletOutputFormat=lcov

- name: Publish coverage report to coveralls.io
uses: coverallsapp/github-action@master
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
path-to-lcov: ./test/Futurum.Test.Tests/TestResults/coverage.info
11 changes: 11 additions & 0 deletions Futurum.Test.sln
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "build", "build", "{A2CA104E
release.yml = .github\workflows\release.yml
EndProjectSection
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "tests", "tests", "{48A9AC0D-7FA4-4084-A707-153EF553485E}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Futurum.Test.Tests", "test\Futurum.Test.Tests\Futurum.Test.Tests.csproj", "{CA4C29B2-9282-41FB-93E5-7667CD79C027}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -25,5 +29,12 @@ Global
{4DF53C8F-6DF9-4BE1-99D1-19D26A65F1E9}.Debug|Any CPU.Build.0 = Debug|Any CPU
{4DF53C8F-6DF9-4BE1-99D1-19D26A65F1E9}.Release|Any CPU.ActiveCfg = Release|Any CPU
{4DF53C8F-6DF9-4BE1-99D1-19D26A65F1E9}.Release|Any CPU.Build.0 = Release|Any CPU
{CA4C29B2-9282-41FB-93E5-7667CD79C027}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{CA4C29B2-9282-41FB-93E5-7667CD79C027}.Debug|Any CPU.Build.0 = Debug|Any CPU
{CA4C29B2-9282-41FB-93E5-7667CD79C027}.Release|Any CPU.ActiveCfg = Release|Any CPU
{CA4C29B2-9282-41FB-93E5-7667CD79C027}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{CA4C29B2-9282-41FB-93E5-7667CD79C027} = {48A9AC0D-7FA4-4084-A707-153EF553485E}
EndGlobalSection
EndGlobal
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# dotnet.futurum.test

![CI](https://github.com/futurum-dev/dotnet.futurum.test/workflows/CI/badge.svg)
[![Coverage Status](https://coveralls.io/repos/github/futurum-dev/dotnet.futurum.test/badge.svg?branch=main)](https://coveralls.io/github/futurum-dev/dotnet.futurum.test?branch=main)
[![NuGet version](https://img.shields.io/nuget/v/futurum.test.svg?style=flat&label=nuget%3A%20futurum.test)](https://www.nuget.org/packages/futurum.test)

Small dotnet testing library, allowing you to test code that uses Futurum.Core
10 changes: 10 additions & 0 deletions src/Futurum.Test/Option/FluentAssertionOptionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,16 @@ public static void ShouldBeHasValueWithValueEquivalentTo<T, TR>(this Futurum.Cor
selectorFunc(option.Value).Should().BeEquivalentTo(value);
}

/// <summary>
/// Specifies that the <see cref="Futurum.Core.Option.Option{T}"/> should be <see cref="Futurum.Core.Option.Option{T}.HasValue"/> true and <paramref name="valueAssertion"/>
/// </summary>
public static void ShouldBeHasValueWithValueAssertion<T>(this Futurum.Core.Option.Option<T> option, Action<T> valueAssertion)
{
option.HasValue.Should().BeTrue();

valueAssertion(option.Value);
}

/// <summary>
/// Specifies that the <see cref="Futurum.Core.Option.Option{T}"/> should be <see cref="Futurum.Core.Option.Option{T}.HasNoValue"/> true.
/// </summary>
Expand Down
12 changes: 12 additions & 0 deletions src/Futurum.Test/Result/FluentAssertionResultExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,18 @@ static async Task<IEnumerable<TData>> ConvertIAsyncEnumerableToIEnumerable(IAsyn
}
}

/// <summary>
/// Specifies that the <see cref="Futurum.Core.Result.Result{T}"/> should be <see cref="Futurum.Core.Result.Result{T}.IsSuccess"/> true and <paramref name="valueAssertion"/>.
/// </summary>
public static void ShouldBeSuccessWithValueAssertion<T>(this Result<T> result, Action<T> valueAssertion)
{
var errorMessage = result.IsSuccess ? string.Empty : $"Error : '{result.Error.Value.ToErrorString()}'";

result.IsSuccess.Should().BeTrue(errorMessage);

valueAssertion(result.Value.Value);
}

/// <summary>
/// Specifies that the <see cref="Futurum.Core.Result.Result"/> should be <see cref="Futurum.Core.Result.Result.IsFailure"/> true.
/// </summary>
Expand Down
33 changes: 33 additions & 0 deletions test/Futurum.Test.Tests/Futurum.Test.Tests.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="FluentAssertions" Version="6.3.0" />
<PackageReference Include="Futurum.Core" Version="1.0.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.0.0" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<DotNetCliToolReference Include="dotnet-xunit" Version="2.3.1" />
<PackageReference Include="coverlet.msbuild" Version="3.1.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="coverlet.collector" Version="3.1.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\src\Futurum.Test\Futurum.Test.csproj" />
</ItemGroup>

</Project>
244 changes: 244 additions & 0 deletions test/Futurum.Test.Tests/Option/FluentAssertionOptionExtensionsTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,244 @@
using FluentAssertions;

using Futurum.Test.Option;

using Xunit;

namespace Futurum.Test.Tests.Option;

public class FluentAssertionOptionExtensionsTests
{
public class ShouldBeHasValue
{
[Fact]
public void HasValue()
{
var option = Core.Option.Option.From(10);

option.ShouldBeHasValue();
}

[Fact]
public void HasNoValue()
{
var option = Core.Option.Option.None<int>();

var action = () => option.ShouldBeHasValue();

action.Should().Throw<Xunit.Sdk.XunitException>().Which.Message.Should().Contain("Expected option.HasValue to be true, but found False.");
}
}

public class ShouldBeHasValueWithValue
{
[Fact]
public void HasValue_matches()
{
var value = 10;

var option = Core.Option.Option.From(value);

option.ShouldBeHasValueWithValue(value);
}

[Fact]
public void HasValue_doesnt_match()
{
var value = 10;

var option = Core.Option.Option.From(value);

var action = () => option.ShouldBeHasValueWithValue(20);

action.Should().Throw<Xunit.Sdk.XunitException>().Which.Message.Should().Contain("Expected option.Value to be 20, but found 10.");
}

[Fact]
public void HasNoValue()
{
var option = Core.Option.Option.None<int>();

var action = () => option.ShouldBeHasValueWithValue(10);

action.Should().Throw<Xunit.Sdk.XunitException>().Which.Message.Should().Contain("Expected option.HasValue to be true, but found False.");
}
}

public class ShouldBeHasValueWithValueWithSelector
{
[Fact]
public void HasValue_matches()
{
var value = new TestClass(10);

var option = Core.Option.Option.From(value);

option.ShouldBeHasValueWithValue(x => x.Number, value.Number);
}

[Fact]
public void HasValue_doesnt_match()
{
var value = new TestClass(10);

var option = Core.Option.Option.From(value);

var action = () => option.ShouldBeHasValueWithValue(x => x.Number, 20);

action.Should().Throw<Xunit.Sdk.XunitException>().Which.Message.Should().Contain("Expected selectorFunc(option.Value) to be 20, but found 10.");
}

[Fact]
public void HasNoValue()
{
var option = Core.Option.Option.None<TestClass>();

var action = () => option.ShouldBeHasValueWithValue(x => x.Number, 10);

action.Should().Throw<Xunit.Sdk.XunitException>().Which.Message.Should().Contain("Expected option.HasValue to be true, but found False.");
}
}

public class ShouldBeHasValueWithValueEquivalentTo
{
[Fact]
public void HasValue_matches()
{
var value = new TestClass(10);

var option = Core.Option.Option.From(value);

option.ShouldBeHasValueWithValueEquivalentTo(new TestClass(10));
}

[Fact]
public void HasValue_doesnt_match()
{
var value = new TestClass(10);

var option = Core.Option.Option.From(value);

var action = () => option.ShouldBeHasValueWithValueEquivalentTo(new TestClass(20));

action.Should().Throw<Xunit.Sdk.XunitException>().Which.Message.Should().Contain("Expected property option.Value.Number to be 20, but found 10.");
}

[Fact]
public void HasNoValue()
{
var option = Core.Option.Option.None<TestClass>();

var action = () => option.ShouldBeHasValueWithValueEquivalentTo(new TestClass(20));

action.Should().Throw<Xunit.Sdk.XunitException>().Which.Message.Should().Contain("Expected option.HasValue to be true, but found False.");
}
}

public class ShouldBeHasValueWithValueEquivalentToWithSelector
{
[Fact]
public void HasValue_matches()
{
var value = new TestChildClass(10);

var option = Core.Option.Option.From(value);

option.ShouldBeHasValueWithValueEquivalentTo(x => x.Child, new TestChildClass.ChildClass(10));
}

[Fact]
public void HasValue_doesnt_match()
{
var value = new TestChildClass(10);

var option = Core.Option.Option.From(value);

var action = () => option.ShouldBeHasValueWithValueEquivalentTo(x => x.Child, new TestChildClass.ChildClass(20));

action.Should().Throw<Xunit.Sdk.XunitException>().Which.Message.Should().Contain("Expected property selectorFunc(option.Value).Number to be 20, but found 10.");
}

[Fact]
public void HasNoValue()
{
var option = Core.Option.Option.None<TestChildClass>();

var action = () => option.ShouldBeHasValueWithValueEquivalentTo(x => x.Child, new TestChildClass.ChildClass(10));

action.Should().Throw<Xunit.Sdk.XunitException>().Which.Message.Should().Contain("Expected option.HasValue to be true, but found False.");
}
}

public class ShouldBeHasValueWithValueAssertion
{
[Fact]
public void HasValue_matches()
{
var value = 10;

var option = Core.Option.Option.From(value);

option.ShouldBeHasValueWithValueAssertion(x => x.Should().Be(value));
}

[Fact]
public void HasValue_doesnt_match()
{
var value = 10;

var option = Core.Option.Option.From(value);

var action = () => option.ShouldBeHasValueWithValueAssertion(x => x.Should().NotBe(value));

action.Should().Throw<Xunit.Sdk.XunitException>().Which.Message.Should().Contain("Did not expect x to be 10");
}

[Fact]
public void HasNoValue()
{
var option = Core.Option.Option.None<int>();

var action = () => option.ShouldBeHasValueWithValueAssertion(x => x.Should().Be(10));

action.Should().Throw<Xunit.Sdk.XunitException>().Which.Message.Should().Contain("Expected option.HasValue to be true, but found False.");
}
}

[Fact]
public void ShouldBeHasNoValue()
{
var option = Core.Option.Option.None<int>();

option.ShouldBeHasNoValue();
}

private class TestClass
{
public int Number { get; }

public TestClass(int number)
{
Number = number;
}
}

private class TestChildClass
{
public ChildClass Child { get; }

public TestChildClass(int number)
{
Child = new ChildClass(number);
}

public class ChildClass
{
public int Number { get; }

public ChildClass(int number)
{
Number = number;
}
}
}
}
Loading

0 comments on commit 0ea889c

Please sign in to comment.