-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Kevin <kevin.dinh@lissi.id>
- Loading branch information
Showing
5 changed files
with
320 additions
and
6 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
{ | ||
"sdk": { | ||
"version": "6.0.0", | ||
"rollForward": "latestFeature" | ||
} | ||
} | ||
{ | ||
"sdk": { | ||
"version": "8.0.0", | ||
"rollForward": "latestFeature" | ||
} | ||
} |
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,34 @@ | ||
using FluentAssertions; | ||
using WalletFramework.Core.Functional; | ||
using Xunit; | ||
|
||
namespace WalletFramework.Core.Tests.Validation; | ||
|
||
public class ApplyTests | ||
{ | ||
private record Sample(int X1, int X2, int X3, int X4, int X5, int X6, int X7) | ||
{ | ||
public int Sum() => X1 + X2 + X3 + X4 + X5 + X6 + X7; | ||
} | ||
|
||
private static Sample CreateSample(int x1, int x2, int x3, int x4, int x5, int x6, int x7) => | ||
new(x1, x2, x3, x4, x5, x6, x7); | ||
|
||
[Fact] | ||
public void ApplyWorks() | ||
{ | ||
const int expected = 1 + 2 + 3 + 4 + 5 + 6 + 7; | ||
var func = ValidationFun.Valid(CreateSample); | ||
|
||
var sut = func | ||
.Apply(1) | ||
.Apply(2) | ||
.Apply(3) | ||
.Apply(4) | ||
.Apply(5) | ||
.Apply(6) | ||
.Apply(7); | ||
|
||
sut.UnwrapOrThrow().Sum().Should().Be(expected); | ||
} | ||
} |
244 changes: 244 additions & 0 deletions
244
src/WalletFramework.Core.Tests/Validation/ValidationTests.cs
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,244 @@ | ||
using FluentAssertions; | ||
using LanguageExt; | ||
using WalletFramework.Core.Functional; | ||
using WalletFramework.Core.Functional.Errors; | ||
using Xunit; | ||
|
||
namespace WalletFramework.Core.Tests.Validation; | ||
|
||
public class ValidationTests | ||
{ | ||
[Fact] | ||
public void AggregationWorks() | ||
{ | ||
Validator<int> greaterThanZero = i => i > 0 | ||
? ValidationFun.Valid(i) | ||
: new SampleError(); | ||
|
||
Validator<int> isEven = i => i % 2 == 0 | ||
? ValidationFun.Valid(i) | ||
: new SampleError(); | ||
|
||
var sut = new List<Validator<int>> { greaterThanZero, isEven }.AggregateValidators(); | ||
|
||
var one = sut(1); | ||
var two = sut(2); | ||
|
||
one.IsSuccess.Should().BeFalse(); | ||
two.IsSuccess.Should().BeTrue(); | ||
} | ||
|
||
[Fact] | ||
public void FallbackWorks() | ||
{ | ||
var one = new SampleError().ToInvalid<Validation<int>>(); | ||
|
||
var sut = one.Fallback(2); | ||
|
||
sut.UnwrapOrThrow().Should().Be(2); | ||
} | ||
|
||
[Fact] | ||
public void FirstValidWorks() | ||
{ | ||
const string one = "1"; | ||
const string nan = "NaN"; | ||
Validator<string, int> valid = str => | ||
{ | ||
try | ||
{ | ||
return ValidationFun.Valid(int.Parse(str)); | ||
} | ||
catch (Exception ) | ||
{ | ||
return new SampleError(); | ||
} | ||
}; | ||
Validator<string, int> invalid = _ => new SampleError(); | ||
|
||
var sut = new List<Validator<string, int>> { invalid, valid }.FirstValid(); | ||
|
||
var oneValid = sut(one); | ||
var nanInvalid = sut(nan); | ||
|
||
oneValid.UnwrapOrThrow().Should().Be(1); | ||
nanInvalid.Match( | ||
i => Assert.Fail("Validation must fail"), | ||
errors => | ||
{ | ||
errors.Should().AllBeOfType<NoItemsSucceededValidationError<int>>(); | ||
errors.Should().ContainSingle(); | ||
} | ||
); | ||
} | ||
|
||
[Theory] | ||
[InlineData(true)] | ||
[InlineData(false)] | ||
public void MatchWorks(bool valid) | ||
{ | ||
Validation<int> validation = valid | ||
? 1 | ||
: new SampleError(); | ||
|
||
validation.Match( | ||
_ => valid.Should().BeTrue(), | ||
errors => | ||
{ | ||
valid.Should().BeFalse(); | ||
errors.Should().AllBeOfType<SampleError>(); | ||
errors.Should().ContainSingle(); | ||
}); | ||
} | ||
|
||
[Fact] | ||
public void OnSuccessWorks() | ||
{ | ||
var one = ValidationFun.Valid("1"); | ||
|
||
var sut = one.OnSuccess(int.Parse); | ||
|
||
sut.UnwrapOrThrow().Should().Be(1); | ||
} | ||
|
||
[Fact] | ||
public async Task OnSuccessAsyncWorks() | ||
{ | ||
var one = ValidationFun.Valid("1").AsTask(); | ||
|
||
var sut = await one.OnSuccess(int.Parse); | ||
|
||
sut.UnwrapOrThrow().Should().Be(1); | ||
} | ||
|
||
[Fact] | ||
public void SelectManyWorks() | ||
{ | ||
var one = ValidationFun.Valid("1"); | ||
|
||
var sut = one.SelectMany( | ||
_ => ValidationFun.Valid(1), | ||
(e1, e2) => int.Parse(e1) + e2 | ||
); | ||
|
||
sut.UnwrapOrThrow().Should().Be(2); | ||
} | ||
|
||
[Fact] | ||
public void SelectWorks() | ||
{ | ||
var one = ValidationFun.Valid("1"); | ||
|
||
var sut = one.Select(int.Parse); | ||
|
||
sut.Match( | ||
i => i.Should().Be(1), | ||
_ => Assert.Fail("Validation must not fail")); | ||
} | ||
|
||
[Fact] | ||
public void TraverseAllWorks() | ||
{ | ||
var validStrs = new List<string> | ||
{ | ||
"1", | ||
"2", | ||
"3" | ||
}; | ||
|
||
var invalidStrs = new List<string> | ||
{ | ||
"1", | ||
"2", | ||
"Three" | ||
}; | ||
|
||
var sutValid = validStrs.TraverseAll(s => | ||
{ | ||
try | ||
{ | ||
return ValidationFun.Valid(int.Parse(s)); | ||
} | ||
catch (Exception) | ||
{ | ||
return new SampleError(); | ||
} | ||
}); | ||
|
||
var sutInvalid = invalidStrs.TraverseAll(str => | ||
{ | ||
try | ||
{ | ||
return ValidationFun.Valid(int.Parse(str)); | ||
} | ||
catch (Exception) | ||
{ | ||
return new SampleError(); | ||
} | ||
}); | ||
|
||
sutValid.IsSuccess.Should().BeTrue(); | ||
sutInvalid.IsSuccess.Should().BeFalse(); | ||
} | ||
|
||
[Fact] | ||
public void TraverseAnyWorks() | ||
{ | ||
var strs = new List<string> | ||
{ | ||
"One", | ||
"2", | ||
"Three" | ||
}; | ||
|
||
var sut = strs.TraverseAny(str => | ||
{ | ||
try | ||
{ | ||
return ValidationFun.Valid(int.Parse(str)); | ||
} | ||
catch (Exception) | ||
{ | ||
return new SampleError(); | ||
} | ||
}); | ||
|
||
sut.Match( | ||
ints => | ||
{ | ||
var list = ints.ToList(); | ||
|
||
list.Should().ContainSingle(); | ||
list.First().Should().Be(2); | ||
}, | ||
errors => | ||
{ | ||
Assert.Fail("Validation must not fail"); | ||
errors.Should().ContainSingle(); | ||
errors.Should().AllBeOfType<SampleError>(); | ||
} | ||
); | ||
} | ||
|
||
[Theory] | ||
[InlineData(true)] | ||
[InlineData(false)] | ||
public void UnwrapOrThrowWorks(bool valid) | ||
{ | ||
Validation<int> validation = valid | ||
? 1 | ||
: new SampleError(); | ||
|
||
try | ||
{ | ||
validation.UnwrapOrThrow(); | ||
valid.Should().BeTrue(); | ||
} | ||
catch (Exception) | ||
{ | ||
valid.Should().BeFalse(); | ||
} | ||
} | ||
|
||
private record SampleError() : Error("This is sample error for testing"); | ||
} |
29 changes: 29 additions & 0 deletions
29
src/WalletFramework.Core.Tests/WalletFramework.Core.Tests.csproj
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,29 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net8.0</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
|
||
<IsPackable>false</IsPackable> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="FluentAssertions" Version="6.12.0" /> | ||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.1.0"/> | ||
<PackageReference Include="xunit" Version="2.9.0" /> | ||
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3"> | ||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> | ||
<PrivateAssets>all</PrivateAssets> | ||
</PackageReference> | ||
<PackageReference Include="coverlet.collector" Version="3.1.2"> | ||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> | ||
<PrivateAssets>all</PrivateAssets> | ||
</PackageReference> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\WalletFramework.Core\WalletFramework.Core.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
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