├── LanguageExt.UnitTesting ├── TryExtensions.cs ├── OptionAsyncExtensions.cs ├── FinExtensions.cs ├── OptionExtensions.cs ├── EitherExtensions.cs ├── ValidationExtentions.cs ├── EitherAsyncExtensions.cs ├── TryAsyncExtensions.cs ├── TryOptionExtensions.cs ├── TryOptionAsyncExtensions.cs ├── ResultExtensions.cs ├── Common.cs └── LanguageExt.UnitTesting.csproj ├── LICENSE ├── LanguageExt.UnitTesting.Tests ├── LanguageExt.UnitTesting.Tests.csproj ├── OptionExtensionsTests.cs ├── OptionAsyncExtensionsTests.cs ├── EitherExtensionsTests.cs ├── TryExtensionsTests.cs ├── ValidationExtensionsTests.cs ├── EitherAsyncExtensionsTests.cs ├── TryAsyncExtensionsTests.cs ├── FinExtensionsTests.cs ├── ResultExtensionsTests.cs ├── TryOptionExtensionsTests.cs └── TryOptionAsyncExtensionsTests.cs ├── LanguageExt.UnitTesting.sln ├── .gitignore └── README.md /LanguageExt.UnitTesting/TryExtensions.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace LanguageExt.UnitTesting 4 | { 5 | public static class TryExtensions 6 | { 7 | public static void ShouldBeSuccess(this Try @this, Action successValidation = null) 8 | => @this.Match(successValidation ?? Common.Noop, Common.ThrowIfFail); 9 | 10 | public static void ShouldBeFail(this Try @this, Action failValidation = null) 11 | => @this.Match(Common.ThrowIfSuccess, failValidation ?? Common.Noop); 12 | } 13 | } -------------------------------------------------------------------------------- /LanguageExt.UnitTesting/OptionAsyncExtensions.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Threading.Tasks; 3 | 4 | namespace LanguageExt.UnitTesting 5 | { 6 | public static class OptionAsyncExtensions 7 | { 8 | public static async Task ShouldBeSome(this OptionAsync @this, Action someValidation = null) => 9 | await @this.Match(Some: someValidation ?? Common.Noop, None: Common.ThrowIfNone); 10 | 11 | public static async Task ShouldBeNone(this OptionAsync @this) => 12 | await @this.Match(Some: Common.ThrowIfSome, None: Common.SuccessfulNone); 13 | } 14 | } -------------------------------------------------------------------------------- /LanguageExt.UnitTesting/FinExtensions.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using LanguageExt.Common; 3 | 4 | namespace LanguageExt.UnitTesting 5 | { 6 | public static class FinExtensions 7 | { 8 | public static void ShouldBeSuccess(this Fin @this, 9 | Action successFin = null) 10 | => @this.Match(successFin ?? Common.Noop, Common.ThrowIfFail); 11 | 12 | public static void ShouldBeFail(this Fin @this, 13 | Action errFin = null) 14 | => @this.Match(Common.ThrowIfSuccess, errFin ?? Common.Noop); 15 | } 16 | } -------------------------------------------------------------------------------- /LanguageExt.UnitTesting/OptionExtensions.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace LanguageExt.UnitTesting 4 | { 5 | 6 | public static class OptionExtensions 7 | { 8 | public static void ShouldBeSome(this Option @this, Action someValidation = null) 9 | => @this.Match( 10 | Some: someValidation ?? Common.Noop, 11 | None: Common.ThrowIfNone); 12 | 13 | public static void ShouldBeNone(this Option @this) 14 | => @this.Match( 15 | Some: Common.ThrowIfSome, 16 | None: Common.SuccessfulNone); 17 | } 18 | } -------------------------------------------------------------------------------- /LanguageExt.UnitTesting/EitherExtensions.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace LanguageExt.UnitTesting 4 | { 5 | public static class EitherExtensions 6 | { 7 | public static void ShouldBeRight(this Either @this, 8 | Action rightValidation = null) 9 | => @this.Match(rightValidation ?? Common.Noop, Common.ThrowIfLeft); 10 | 11 | public static void ShouldBeLeft(this Either @this, 12 | Action leftValidation = null) 13 | => @this.Match(Common.ThrowIfRight, leftValidation ?? Common.Noop); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /LanguageExt.UnitTesting/ValidationExtentions.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace LanguageExt.UnitTesting 4 | { 5 | public static class ValidationExtentions 6 | { 7 | public static void ShouldBeSuccess(this Validation @this, 8 | Action successValidation = null) 9 | => @this.Match(successValidation ?? Common.Noop, Common.ThrowIfFail); 10 | 11 | public static void ShouldBeFail(this Validation @this, 12 | Action> failValidation = null) 13 | => @this.Match(Common.ThrowIfSuccess, failValidation ?? Common.Noop); 14 | } 15 | } -------------------------------------------------------------------------------- /LanguageExt.UnitTesting/EitherAsyncExtensions.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Threading.Tasks; 3 | 4 | namespace LanguageExt.UnitTesting 5 | { 6 | public static class EitherAsyncExtensions 7 | { 8 | public static async Task ShouldBeRight(this EitherAsync @this, 9 | Action rightValidation = null) 10 | => await @this.Match(rightValidation ?? Common.Noop, Common.ThrowIfLeft); 11 | 12 | public static async Task ShouldBeLeft(this EitherAsync @this, 13 | Action leftValidation = null) 14 | => await @this.Match(Common.ThrowIfRight, leftValidation ?? Common.Noop); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /LanguageExt.UnitTesting/TryAsyncExtensions.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Threading.Tasks; 3 | 4 | namespace LanguageExt.UnitTesting 5 | { 6 | public static class TryAsyncExtensions 7 | { 8 | public static async Task ShouldBeSuccess(this TryAsync @this, Action successValidation = null) 9 | => await @this.Match( 10 | Succ: successValidation ?? Common.Noop, 11 | Fail: _ => throw new Exception("Expected Success, got Fail instead.") 12 | ); 13 | 14 | public static async Task ShouldBeFail(this TryAsync @this, Action failValidation = null) 15 | => await @this.Match(Succ: _ => throw new Exception("Expected Fail, got Success instead."), 16 | Fail: failValidation ?? Common.Noop); 17 | } 18 | } -------------------------------------------------------------------------------- /LanguageExt.UnitTesting/TryOptionExtensions.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace LanguageExt.UnitTesting 4 | { 5 | public static class TryOptionExtensions 6 | { 7 | public static void ShouldBeSome(this TryOption @this, Action someValidation = null) 8 | => @this.Match( 9 | Some: someValidation ?? Common.Noop, 10 | None: Common.ThrowIfNone, 11 | Fail: ex => throw ex 12 | ); 13 | 14 | public static void ShouldBeNone(this TryOption @this) 15 | => @this.Match( 16 | Some: Common.ThrowIfSome, 17 | None: Common.SuccessfulNone, 18 | Fail: ex => throw ex 19 | ); 20 | 21 | public static void ShouldBeFail(this TryOption @this, Action failValidation = null) 22 | => @this.Match( 23 | Some: Common.ThrowExpectedFailGotSome, 24 | None: Common.ThrowExpectedFailGotNone, 25 | Fail: ex => (failValidation ?? Common.Noop)(ex) 26 | ); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2018 Yuriy Lyeshchenko 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /LanguageExt.UnitTesting.Tests/LanguageExt.UnitTesting.Tests.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | net8.0 5 | latest 6 | enable 7 | enable 8 | false 9 | true 10 | true 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | all 19 | runtime; build; native; contentfiles; analyzers 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | -------------------------------------------------------------------------------- /LanguageExt.UnitTesting/TryOptionAsyncExtensions.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Threading.Tasks; 3 | 4 | namespace LanguageExt.UnitTesting 5 | { 6 | public static class TryOptionAsyncExtensions 7 | { 8 | public static async Task ShouldBeSome(this TryOptionAsync @this, Action someValidation = null) 9 | => await @this.Match( 10 | Some: someValidation ?? Common.Noop, 11 | None: Common.ThrowIfNone, 12 | Fail: ex => throw ex 13 | ); 14 | 15 | public static async Task ShouldBeNone(this TryOptionAsync @this) 16 | => await @this.Match( 17 | Some: Common.ThrowIfSome, 18 | None: Common.SuccessfulNone, 19 | Fail: ex => throw ex 20 | ); 21 | 22 | public static async Task ShouldBeFail(this TryOptionAsync @this, Action failValidation = null) 23 | => await @this.Match( 24 | Some: Common.ThrowExpectedFailGotSome, 25 | None: Common.ThrowExpectedFailGotNone, 26 | Fail: ex => (failValidation ?? Common.Noop)(ex) 27 | ); 28 | } 29 | } -------------------------------------------------------------------------------- /LanguageExt.UnitTesting/ResultExtensions.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using LanguageExt.Common; 3 | 4 | namespace LanguageExt.UnitTesting 5 | { 6 | public static class ResultExtensions 7 | { 8 | public static void ShouldBeSuccess(this Result @this, Action successValidation = null) 9 | { 10 | _ = @this.Match( 11 | Succ: s => 12 | { 13 | successValidation?.Invoke(s); 14 | return Prelude.None; 15 | }, 16 | Fail: e => 17 | { 18 | Common.ThrowIfFail(e); 19 | return Prelude.None; 20 | }); 21 | } 22 | 23 | public static void ShouldBeFail(this Result @this, Action failValidation = null) 24 | { 25 | _ = @this.Match( 26 | Succ: s => 27 | { 28 | Common.ThrowIfSuccess(s); 29 | return Prelude.None; 30 | }, 31 | Fail: e => 32 | { 33 | failValidation?.Invoke(e); 34 | return Prelude.None; 35 | }); 36 | } 37 | } 38 | } -------------------------------------------------------------------------------- /LanguageExt.UnitTesting/Common.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace LanguageExt.UnitTesting 4 | { 5 | internal static class Common 6 | { 7 | internal static void ThrowIfSome(T _) 8 | => throw new Exception("Expected None, got Some instead."); 9 | 10 | internal static void ThrowIfNone() 11 | => throw new Exception("Expected Some, got None instead."); 12 | 13 | internal static void ThrowIfFail(T _) 14 | => throw new Exception("Expected Success, got Fail instead."); 15 | 16 | internal static void ThrowIfSuccess(T _) 17 | => throw new Exception("Expected Fail, got Success instead."); 18 | 19 | internal static void ThrowIfRight(T _) 20 | => throw new Exception("Expected Left, got Right instead."); 21 | 22 | internal static void ThrowIfLeft(T _) 23 | => throw new Exception("Expected Right, got Left instead."); 24 | 25 | internal static void ThrowExpectedFailGotSome(T _) 26 | => throw new Exception("Expected Fail, got Some instead."); 27 | 28 | internal static void ThrowExpectedFailGotNone() 29 | => throw new Exception("Expected Fail, got None instead."); 30 | 31 | internal static void SuccessfulNone() 32 | { 33 | /* we should end up in here*/ 34 | } 35 | 36 | internal static void Noop(T _) {} 37 | } 38 | } -------------------------------------------------------------------------------- /LanguageExt.UnitTesting/LanguageExt.UnitTesting.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | netstandard2.0 5 | 6 | 7 | 8 | latest 9 | true 10 | true 11 | Yuriy Lyeshchenko 12 | Extension methods to simplify writing unit tests for code written using LanguageExt library 13 | https://github.com/yuriylsh/LanguageExt.UnitTesting 14 | MIT 15 | https://github.com/yuriylsh/LanguageExt.UnitTesting 16 | https://github.com/yuriylsh/LanguageExt.UnitTesting.git 17 | git 18 | LanguageExt 19 | Adding Fin extenstion methods 20 | Yuriy Lyeshchenko © 2018 21 | 4.4.9.0 22 | 4.4.9.0 23 | 4.4.9 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | -------------------------------------------------------------------------------- /LanguageExt.UnitTesting.Tests/OptionExtensionsTests.cs: -------------------------------------------------------------------------------- 1 | using FluentAssertions; 2 | using Xunit; 3 | 4 | namespace LanguageExt.UnitTesting.Tests 5 | { 6 | public static class OptionExtensionsTests 7 | { 8 | [Fact] 9 | public static void ShouldBeSome_GivenNone_Throws() 10 | { 11 | Action act = () => GetNone().ShouldBeSome(); 12 | act.Should().Throw().WithMessage("Expected Some, got None instead."); 13 | } 14 | 15 | [Fact] 16 | public static void ShouldBeNone_GivenSome_Throws() 17 | { 18 | Action act = () => GetSome().ShouldBeNone(); 19 | act.Should().Throw().WithMessage("Expected None, got Some instead."); 20 | } 21 | 22 | [Fact] 23 | public static void ShouldBeSome_GivenSomeWithValidation_RunsValidation() 24 | { 25 | var validationRan = false; 26 | GetSome().ShouldBeSome(x => validationRan = true); 27 | validationRan.Should().BeTrue(); 28 | } 29 | 30 | [Fact] 31 | public static void ShouldBeSome_GivenSomeNoValidation_DoesNotThrow() 32 | => GetSome().ShouldBeSome(); 33 | 34 | [Fact] 35 | public static void ShouldBeNone_GivenNone_DoesNotThrow() => GetNone().ShouldBeNone(); 36 | 37 | private static Option GetNone() => Option.None; 38 | private static Option GetSome() => "some"; 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /LanguageExt.UnitTesting.Tests/OptionAsyncExtensionsTests.cs: -------------------------------------------------------------------------------- 1 | using FluentAssertions; 2 | using Xunit; 3 | 4 | namespace LanguageExt.UnitTesting.Tests 5 | { 6 | public static class OptionAsyncExtensionsTests 7 | { 8 | [Fact] 9 | public static async Task ShouldBeSome_GivenNone_Throws() 10 | { 11 | Func act = () => GetNone().ShouldBeSome(); 12 | await act.Should().ThrowAsync().WithMessage("Expected Some, got None instead."); 13 | } 14 | 15 | [Fact] 16 | public static async Task ShouldBeNone_GivenSome_Throws() 17 | { 18 | Func act = () => GetSome().ShouldBeNone(); 19 | await act.Should().ThrowAsync().WithMessage("Expected None, got Some instead."); 20 | } 21 | 22 | [Fact] 23 | public static async Task ShouldBeSome_GivenSomeWithValidation_RunsValidation() 24 | { 25 | var validationRan = false; 26 | await GetSome().ShouldBeSome(x => validationRan = true); 27 | validationRan.Should().BeTrue(); 28 | } 29 | 30 | [Fact] 31 | public static async Task ShouldBeSome_GivenSomeNoValidation_DoesNotThrow() 32 | => await GetSome().ShouldBeSome(); 33 | 34 | [Fact] 35 | public static async Task ShouldBeNone_GivenNone_DoesNotThrow() 36 | { 37 | Func act = () => GetNone().ShouldBeNone(); 38 | await act(); 39 | } 40 | 41 | private static OptionAsync GetNone() => OptionAsync.None; 42 | private static OptionAsync GetSome() => "some"; 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /LanguageExt.UnitTesting.Tests/EitherExtensionsTests.cs: -------------------------------------------------------------------------------- 1 | using FluentAssertions; 2 | using Xunit; 3 | 4 | namespace LanguageExt.UnitTesting.Tests 5 | { 6 | public static class EitherExtensionsTests 7 | { 8 | [Fact] 9 | public static void ShouldBeRight_GivenLeft_Throws() 10 | { 11 | Action act = () => GetLeft().ShouldBeRight(); 12 | act.Should().Throw().WithMessage("Expected Right, got Left instead."); 13 | } 14 | 15 | [Fact] 16 | public static void ShouldBeLeft_GivenRight_Throws() 17 | { 18 | Action act = () => GetRight().ShouldBeLeft(); 19 | act.Should().Throw().WithMessage("Expected Left, got Right instead."); 20 | } 21 | 22 | [Fact] 23 | public static void ShouldBeLeft_GivenLeftWithValidation_RunsValidation() 24 | { 25 | var validationRan = false; 26 | GetLeft().ShouldBeLeft(x => validationRan = true); 27 | validationRan.Should().BeTrue(); 28 | } 29 | 30 | [Fact] 31 | public static void ShouldBeLeft_GivenLeftNoValidation_DoesNotThrow() 32 | => GetLeft().ShouldBeLeft(); 33 | 34 | [Fact] 35 | public static void ShouldBeRight_GivenRightWithValidation_RunsValidation() 36 | { 37 | var validationRan = false; 38 | GetRight().ShouldBeRight(x => validationRan = true); 39 | validationRan.Should().BeTrue(); 40 | } 41 | 42 | [Fact] 43 | public static void ShouldBeRight_GivenRightNoValidation_DoesNotThrow() 44 | => GetRight().ShouldBeRight(); 45 | 46 | private static Either GetLeft() => 123; 47 | private static Either GetRight() => "right"; 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /LanguageExt.UnitTesting.sln: -------------------------------------------------------------------------------- 1 | 2 | Microsoft Visual Studio Solution File, Format Version 12.00 3 | # Visual Studio 15 4 | VisualStudioVersion = 15.0.27428.2015 5 | MinimumVisualStudioVersion = 10.0.40219.1 6 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "LanguageExt.UnitTesting", "LanguageExt.UnitTesting\LanguageExt.UnitTesting.csproj", "{318C847C-584C-4853-AEE5-7D460A0C175D}" 7 | EndProject 8 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "LanguageExt.UnitTesting.Tests", "LanguageExt.UnitTesting.Tests\LanguageExt.UnitTesting.Tests.csproj", "{F71F2579-1DCF-4FF3-9D47-CED324D5BEE6}" 9 | EndProject 10 | Global 11 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 12 | Debug|Any CPU = Debug|Any CPU 13 | Release|Any CPU = Release|Any CPU 14 | EndGlobalSection 15 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 16 | {318C847C-584C-4853-AEE5-7D460A0C175D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 17 | {318C847C-584C-4853-AEE5-7D460A0C175D}.Debug|Any CPU.Build.0 = Debug|Any CPU 18 | {318C847C-584C-4853-AEE5-7D460A0C175D}.Release|Any CPU.ActiveCfg = Release|Any CPU 19 | {318C847C-584C-4853-AEE5-7D460A0C175D}.Release|Any CPU.Build.0 = Release|Any CPU 20 | {F71F2579-1DCF-4FF3-9D47-CED324D5BEE6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 21 | {F71F2579-1DCF-4FF3-9D47-CED324D5BEE6}.Debug|Any CPU.Build.0 = Debug|Any CPU 22 | {F71F2579-1DCF-4FF3-9D47-CED324D5BEE6}.Release|Any CPU.ActiveCfg = Release|Any CPU 23 | {F71F2579-1DCF-4FF3-9D47-CED324D5BEE6}.Release|Any CPU.Build.0 = Release|Any CPU 24 | EndGlobalSection 25 | GlobalSection(SolutionProperties) = preSolution 26 | HideSolutionNode = FALSE 27 | EndGlobalSection 28 | GlobalSection(ExtensibilityGlobals) = postSolution 29 | SolutionGuid = {4FEEA2B7-7A44-4DA5-8BE1-87D1A0C1FED2} 30 | EndGlobalSection 31 | EndGlobal 32 | -------------------------------------------------------------------------------- /LanguageExt.UnitTesting.Tests/TryExtensionsTests.cs: -------------------------------------------------------------------------------- 1 | using FluentAssertions; 2 | using Xunit; 3 | 4 | namespace LanguageExt.UnitTesting.Tests 5 | { 6 | public static class TryExtensionsTests 7 | { 8 | [Fact] 9 | public static void ShouldBeFail_GivenSuccess_Throws() 10 | { 11 | Action act = () => GetSuccess().ShouldBeFail(); 12 | act.Should().Throw().WithMessage("Expected Fail, got Success instead."); 13 | } 14 | 15 | [Fact] 16 | public static void ShouldBeSuccess_GivenFail_Throws() 17 | { 18 | Action act = () => GetFail().ShouldBeSuccess(); 19 | act.Should().Throw().WithMessage("Expected Success, got Fail instead."); 20 | } 21 | 22 | [Fact] 23 | public static void ShouldBeFail_GivenFailWithValidation_RunsValidation() 24 | { 25 | var validationRan = false; 26 | GetFail().ShouldBeFail(x => validationRan = true); 27 | validationRan.Should().BeTrue(); 28 | } 29 | 30 | [Fact] 31 | public static void ShouldBeFail_GivenFailNoValidation_DoesNotThrow() 32 | => GetFail().ShouldBeFail(); 33 | 34 | [Fact] 35 | public static void ShouldBeSuccess_GivenSuccessWithValidation_RunsValidation() 36 | { 37 | var validationRan = false; 38 | GetSuccess().ShouldBeSuccess(x => validationRan = true); 39 | validationRan.Should().BeTrue(); 40 | } 41 | [Fact] 42 | public static void ShouldBeSuccess_GivenSuccessNoValidation_DoesNotThrow() 43 | => GetSuccess().ShouldBeSuccess(); 44 | 45 | private static Try GetFail() => () => throw new Exception(); 46 | private static Try GetSuccess() => () => "success"; 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /LanguageExt.UnitTesting.Tests/ValidationExtensionsTests.cs: -------------------------------------------------------------------------------- 1 | using FluentAssertions; 2 | using Xunit; 3 | 4 | namespace LanguageExt.UnitTesting.Tests 5 | { 6 | public static class ValidationExtensionsTests 7 | { 8 | [Fact] 9 | public static void ShouldBeFail_GivenSuccess_Throws() 10 | { 11 | Action act = () => GetSuccess().ShouldBeFail(); 12 | act.Should().Throw().WithMessage("Expected Fail, got Success instead."); 13 | } 14 | 15 | [Fact] 16 | public static void ShouldBeSuccess_GivenFail_Throws() 17 | { 18 | Action act = () => GetFail().ShouldBeSuccess(); 19 | act.Should().Throw().WithMessage("Expected Success, got Fail instead."); 20 | } 21 | 22 | [Fact] 23 | public static void ShouldBeFail_GivenFailWithValidation_RunsValidation() 24 | { 25 | var validationRan = false; 26 | GetFail().ShouldBeFail(x => validationRan = true); 27 | validationRan.Should().BeTrue(); 28 | } 29 | 30 | [Fact] 31 | public static void ShouldBeFail_GivenFailNoValidation_DoesNotThrow() 32 | => GetFail().ShouldBeFail(); 33 | 34 | [Fact] 35 | public static void ShouldBeSuccess_GivenSuccessWithValidation_RunsValidation() 36 | { 37 | var validationRan = false; 38 | GetSuccess().ShouldBeSuccess(x => validationRan = true); 39 | validationRan.Should().BeTrue(); 40 | } 41 | 42 | [Fact] 43 | public static void ShouldBeSuccess_GivenSuccessNoValidation_DoesNotThrow() 44 | => GetSuccess().ShouldBeSuccess(); 45 | 46 | private static Validation GetFail() => 123; 47 | private static Validation GetSuccess() => "success"; 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /LanguageExt.UnitTesting.Tests/EitherAsyncExtensionsTests.cs: -------------------------------------------------------------------------------- 1 | using FluentAssertions; 2 | using Xunit; 3 | 4 | namespace LanguageExt.UnitTesting.Tests 5 | { 6 | public static class EitherAsyncExtensionsTests 7 | { 8 | [Fact] 9 | public static async Task ShouldBeRight_GivenLeft_Throws() 10 | { 11 | Func act = () => GetLeft().ShouldBeRight(); 12 | await act.Should().ThrowAsync().WithMessage("Expected Right, got Left instead."); 13 | } 14 | 15 | [Fact] 16 | public static async Task ShouldBeLeft_GivenRight_Throws() 17 | { 18 | Func act = () => GetRight().ShouldBeLeft(); 19 | await act.Should().ThrowAsync().WithMessage("Expected Left, got Right instead."); 20 | } 21 | 22 | [Fact] 23 | public static async Task ShouldBeLeft_GivenLeftWithValidation_RunsValidation() 24 | { 25 | var validationRan = false; 26 | await GetLeft().ShouldBeLeft(x => validationRan = true); 27 | validationRan.Should().BeTrue(); 28 | } 29 | 30 | [Fact] 31 | public static async Task ShouldBeLeft_GivenLeftNoValidation_DoesNotThrow() 32 | => await GetLeft().ShouldBeLeft(); 33 | 34 | [Fact] 35 | public static async Task ShouldBeRight_GivenRightWithValidation_RunsValidation() 36 | { 37 | var validationRan = false; 38 | await GetRight().ShouldBeRight(x => validationRan = true); 39 | validationRan.Should().BeTrue(); 40 | } 41 | 42 | [Fact] 43 | public static async Task ShouldBeRight_GivenRightNoValidation_DoesNotThrow() 44 | => await GetRight().ShouldBeRight(); 45 | 46 | private static EitherAsync GetLeft() => 123; 47 | private static EitherAsync GetRight() => "right"; 48 | } 49 | } -------------------------------------------------------------------------------- /LanguageExt.UnitTesting.Tests/TryAsyncExtensionsTests.cs: -------------------------------------------------------------------------------- 1 | using FluentAssertions; 2 | using Xunit; 3 | namespace LanguageExt.UnitTesting.Tests 4 | { 5 | public static class TryAsyncExtensionsTests 6 | { 7 | [Fact] 8 | public static async Task ShouldBeFail_GivenSuccess_Throws() 9 | { 10 | Func act = () => GetSuccess().ShouldBeFail(); 11 | await act.Should().ThrowAsync().WithMessage("Expected Fail, got Success instead."); 12 | } 13 | 14 | [Fact] 15 | public static async Task ShouldBeSuccess_GivenFail_Throws() 16 | { 17 | Func act = () => GetFail().ShouldBeSuccess(); 18 | await act.Should().ThrowAsync().WithMessage("Expected Success, got Fail instead."); 19 | } 20 | 21 | [Fact] 22 | public static async Task ShouldBeFail_GivenFailWithValidation_RunsValidation() 23 | { 24 | var validationRan = false; 25 | await GetFail().ShouldBeFail(x => validationRan = true); 26 | validationRan.Should().BeTrue(); 27 | } 28 | [Fact] 29 | public static async Task ShouldBeFail_GivenFailNoValidation_DoesNotThrow() 30 | => await GetFail().ShouldBeFail(); 31 | 32 | [Fact] 33 | public static async Task ShouldBeSuccess_GivenSuccessWithValidation_RunsValidation() 34 | { 35 | var validationRan = false; 36 | await GetSuccess().ShouldBeSuccess(x => validationRan = true); 37 | validationRan.Should().BeTrue(); 38 | } 39 | 40 | [Fact] 41 | public static async Task ShouldBeSuccess_GivenSuccessNoValidation_DoesNotThrow() 42 | => await GetSuccess().ShouldBeSuccess(); 43 | 44 | private static TryAsync GetFail() => () => throw new Exception(); 45 | private static TryAsync GetSuccess() => async () => await Task.FromResult("success"); 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /LanguageExt.UnitTesting.Tests/FinExtensionsTests.cs: -------------------------------------------------------------------------------- 1 | using FluentAssertions; 2 | using LanguageExt.Common; 3 | using static LanguageExt.Prelude; 4 | using Xunit; 5 | 6 | namespace LanguageExt.UnitTesting.Tests 7 | { 8 | 9 | public static class FinExtensionsTests 10 | { 11 | [Fact] 12 | public static void ShouldBeFail_GivenSuccess_Throws() 13 | { 14 | Action act = () => GetSuccess().ShouldBeFail(); 15 | act.Should().Throw().WithMessage("Expected Fail, got Success instead."); 16 | } 17 | 18 | [Fact] 19 | public static void ShouldBeSuccess_GivenFail_Throws() 20 | { 21 | Action act = () => GetFail().ShouldBeSuccess(); 22 | act.Should().Throw().WithMessage("Expected Success, got Fail instead."); 23 | } 24 | 25 | [Fact] 26 | public static void ShouldBeFail_GivenFailWithFin_RunsFin() 27 | { 28 | var finRan = false; 29 | GetFail().ShouldBeFail(x => finRan = true); 30 | finRan.Should().BeTrue(); 31 | } 32 | 33 | [Fact] 34 | public static void ShouldBeFail_GivenNoFail_DoesNotThrow() 35 | => GetFail().ShouldBeFail(); 36 | 37 | [Fact] 38 | public static void ShouldBeSuccess_GivenSuccessWithFin_RunsFin() 39 | { 40 | var finRan = false; 41 | GetSuccess().ShouldBeSuccess(x => finRan = true); 42 | finRan.Should().BeTrue(); 43 | } 44 | 45 | [Fact] 46 | public static void ShouldBeSuccess_GivenSuccessNoFail_DoesNotThrow() 47 | => GetSuccess().ShouldBeSuccess(); 48 | 49 | [Fact] 50 | public static void ShoulBeFail_GivenFailedFin_DoesNotThrow() 51 | => Fin.Fail(Error.New(5, "Test Error")).ShouldBeFail(); 52 | 53 | private static Fin GetFail() => FinFail("error"); 54 | private static Fin GetSuccess() => FinSucc("success"); 55 | } 56 | } -------------------------------------------------------------------------------- /LanguageExt.UnitTesting.Tests/ResultExtensionsTests.cs: -------------------------------------------------------------------------------- 1 | using FluentAssertions; 2 | using LanguageExt.Common; 3 | using Xunit; 4 | 5 | namespace LanguageExt.UnitTesting.Tests 6 | { 7 | public static class ResultExtensionsTests 8 | { 9 | [Fact] 10 | public static void ShouldBeSuccess_GivenFail_Throws() 11 | { 12 | Action act = () => GetFail().ShouldBeSuccess(); 13 | act.Should().Throw().WithMessage("Expected Success, got Fail instead."); 14 | } 15 | 16 | [Fact] 17 | public static void ShouldBeFail_GivenSuccess_Throws() 18 | { 19 | Action act = () => GetSuccess().ShouldBeFail(); 20 | act.Should().Throw().WithMessage("Expected Fail, got Success instead."); 21 | } 22 | 23 | [Fact] 24 | public static void ShouldBeSuccess_GivenSuccessWithValidation_RunsValidation() 25 | { 26 | var validationRan = false; 27 | GetSuccess().ShouldBeSuccess(_ => validationRan = true); 28 | validationRan.Should().BeTrue(); 29 | } 30 | 31 | [Fact] 32 | public static void ShouldBeFail_GivenFailWithValidation_RunsValidation() 33 | { 34 | var validationRan = false; 35 | GetFail().ShouldBeFail(_ => validationRan = true); 36 | validationRan.Should().BeTrue(); 37 | } 38 | 39 | [Fact] 40 | public static void ShouldBeFail_GivenFailWithValidation_RunsValidation_CheckMessage() 41 | { 42 | var validationRan = false; 43 | var validationMessage = ""; 44 | GetFail().ShouldBeFail(x => 45 | { 46 | validationRan = true; 47 | validationMessage = x.Message; 48 | }); 49 | validationRan.Should().BeTrue(); 50 | validationMessage.Should().Be("fail"); 51 | } 52 | 53 | [Fact] 54 | public static void ShouldBeSuccess_GivenSuccessNoValidation_DoesNotThrow() 55 | => GetSuccess().ShouldBeSuccess(); 56 | 57 | [Fact] 58 | public static void ShouldBeFail_GivenFail_DoesNotThrow() => GetFail().ShouldBeFail(); 59 | 60 | private static Result GetFail() => new(new Exception("fail")); 61 | private static Result GetSuccess() => "success"; 62 | } 63 | } -------------------------------------------------------------------------------- /LanguageExt.UnitTesting.Tests/TryOptionExtensionsTests.cs: -------------------------------------------------------------------------------- 1 | using FluentAssertions; 2 | using Xunit; 3 | 4 | namespace LanguageExt.UnitTesting.Tests 5 | { 6 | public static class TryOptionExtensionsTests 7 | { 8 | [Fact] 9 | public static void ShouldBeFail_GivenSuccessSome_Throws() 10 | { 11 | Action act = () => GetSuccessSome().ShouldBeFail(); 12 | act.Should().Throw().WithMessage("Expected Fail, got Some instead."); 13 | } 14 | 15 | [Fact] 16 | public static void ShouldBeFail_GivenSuccessNone_Throws() 17 | { 18 | Action act = () => GetSuccessNone().ShouldBeFail(); 19 | act.Should().Throw().WithMessage("Expected Fail, got None instead."); 20 | } 21 | 22 | [Fact] 23 | public static void ShouldBeFail_GivenFailWithValidation_RunsValidation() 24 | { 25 | var validationRan = false; 26 | GetFail().ShouldBeFail(x => validationRan = true); 27 | validationRan.Should().BeTrue(); 28 | } 29 | 30 | [Fact] 31 | public static void ShouldBeFail_GivenFailNoValidation_DoesNotThrow() 32 | => GetFail().ShouldBeFail(); 33 | 34 | [Fact] 35 | public static void ShouldBeSome_GivenNone_Throws() 36 | { 37 | Action act = () => GetSuccessNone().ShouldBeSome(); 38 | act.Should().Throw().WithMessage("Expected Some, got None instead."); 39 | } 40 | 41 | [Fact] 42 | public static void ShouldBeSome_GivenFail_Throws() 43 | { 44 | Action act = () => GetFail().ShouldBeSome(); 45 | act.Should().Throw().WithMessage("something went wrong"); 46 | } 47 | 48 | [Fact] 49 | public static void ShouldBeSome_GivenSomeWithValidation_RunsValidation() 50 | { 51 | var validationRan = false; 52 | GetSuccessSome().ShouldBeSome(x => 53 | { 54 | x.Should().Be(123); 55 | validationRan = true; 56 | }); 57 | validationRan.Should().BeTrue(); 58 | } 59 | 60 | [Fact] 61 | public static void ShouldBeSome_GivenSomeNoValidation_DoesNotThrow() 62 | => GetSuccessSome().ShouldBeSome(); 63 | 64 | [Fact] 65 | public static void ShouldBeNone_GivenSome_Throws() 66 | { 67 | Action act = () => GetSuccessSome().ShouldBeNone(); 68 | act.Should().Throw().WithMessage("Expected None, got Some instead."); 69 | } 70 | 71 | [Fact] 72 | public static void ShouldBeNone_GivenFail_Throws() 73 | { 74 | Action act = () => GetFail().ShouldBeNone(); 75 | act.Should().Throw().WithMessage("something went wrong"); 76 | } 77 | 78 | [Fact] 79 | public static void ShouldBeNone_GivenNone_DoesNotThrow() 80 | => GetSuccessNone().ShouldBeNone(); 81 | 82 | private static TryOption GetFail() => () => throw new Exception("something went wrong"); 83 | private static TryOption GetSuccessSome() => () => 123; 84 | private static TryOption GetSuccessNone() => () => Prelude.None; 85 | } 86 | } 87 | -------------------------------------------------------------------------------- /LanguageExt.UnitTesting.Tests/TryOptionAsyncExtensionsTests.cs: -------------------------------------------------------------------------------- 1 | using FluentAssertions; 2 | using Xunit; 3 | 4 | namespace LanguageExt.UnitTesting.Tests 5 | { 6 | public static class TryOptionAsyncExtensionsTests 7 | { 8 | [Fact] 9 | public static async Task ShouldBeFail_GivenSuccessSome_Throws() 10 | { 11 | Func act = () => GetSuccessSome().ShouldBeFail(); 12 | await act.Should().ThrowAsync().WithMessage("Expected Fail, got Some instead."); 13 | } 14 | 15 | [Fact] 16 | public static async Task ShouldBeFail_GivenSuccessNone_Throws() 17 | { 18 | Func act = () => GetSuccessNone().ShouldBeFail(); 19 | await act.Should().ThrowAsync().WithMessage("Expected Fail, got None instead."); 20 | } 21 | 22 | [Fact] 23 | public static async Task ShouldBeFail_GivenFailWithValidation_RunsValidation() 24 | { 25 | var validationRan = false; 26 | await GetFail().ShouldBeFail(x => validationRan = true); 27 | validationRan.Should().BeTrue(); 28 | } 29 | 30 | [Fact] 31 | public static async Task ShouldBeFail_GivenFailNoValidation_DoesNotThrow() 32 | => await GetFail().ShouldBeFail(); 33 | 34 | [Fact] 35 | public static async Task ShouldBeSome_GivenNone_Throws() 36 | { 37 | Func act = () => GetSuccessNone().ShouldBeSome(); 38 | await act.Should().ThrowAsync().WithMessage("Expected Some, got None instead."); 39 | } 40 | 41 | [Fact] 42 | public static async Task ShouldBeSome_GivenFail_Throws() 43 | { 44 | Func act = () => GetFail().ShouldBeSome(); 45 | await act.Should().ThrowAsync().WithMessage("something went wrong"); 46 | } 47 | 48 | [Fact] 49 | public static async Task ShouldBeSome_GivenSomeWithValidation_RunsValidation() 50 | { 51 | var validationRan = false; 52 | await GetSuccessSome().ShouldBeSome(x => 53 | { 54 | x.Should().Be(123); 55 | validationRan = true; 56 | }); 57 | validationRan.Should().BeTrue(); 58 | } 59 | 60 | [Fact] 61 | public static async Task ShouldBeSome_GivenSomeNoValidation_DoesNotThrow() 62 | => await GetSuccessSome().ShouldBeSome(); 63 | 64 | [Fact] 65 | public static async Task ShouldBeNone_GivenSome_Throws() 66 | { 67 | Func act = () => GetSuccessSome().ShouldBeNone(); 68 | await act.Should().ThrowAsync().WithMessage("Expected None, got Some instead."); 69 | } 70 | 71 | [Fact] 72 | public static async Task ShouldBeNone_GivenFail_Throws() 73 | { 74 | Func act = () => GetFail().ShouldBeNone(); 75 | await act.Should().ThrowAsync().WithMessage("something went wrong"); 76 | } 77 | 78 | [Fact] 79 | public static async Task ShouldBeNone_GivenNone_DoesNotThrow() 80 | { 81 | await GetSuccessNone().ShouldBeNone(); 82 | } 83 | 84 | private static TryOptionAsync GetFail() => () => throw new Exception("something went wrong"); 85 | private static TryOptionAsync GetSuccessSome() => async () => await Task.FromResult(123); 86 | private static TryOptionAsync GetSuccessNone() => async () => await Task.FromResult(Prelude.None); 87 | } 88 | } 89 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | ## Ignore Visual Studio temporary files, build results, and 2 | ## files generated by popular Visual Studio add-ons. 3 | ## 4 | ## Get latest from https://github.com/github/gitignore/blob/master/VisualStudio.gitignore 5 | 6 | # User-specific files 7 | *.suo 8 | *.user 9 | *.userosscache 10 | *.sln.docstates 11 | 12 | # User-specific files (MonoDevelop/Xamarin Studio) 13 | *.userprefs 14 | 15 | # Build results 16 | [Dd]ebug/ 17 | [Dd]ebugPublic/ 18 | [Rr]elease/ 19 | [Rr]eleases/ 20 | x64/ 21 | x86/ 22 | bld/ 23 | [Bb]in/ 24 | [Oo]bj/ 25 | [Ll]og/ 26 | 27 | # Visual Studio 2015 cache/options directory 28 | .vs/ 29 | # Uncomment if you have tasks that create the project's static files in wwwroot 30 | #wwwroot/ 31 | 32 | # MSTest test Results 33 | [Tt]est[Rr]esult*/ 34 | [Bb]uild[Ll]og.* 35 | 36 | # NUNIT 37 | *.VisualState.xml 38 | TestResult.xml 39 | 40 | # Build Results of an ATL Project 41 | [Dd]ebugPS/ 42 | [Rr]eleasePS/ 43 | dlldata.c 44 | 45 | # .NET Core 46 | project.lock.json 47 | project.fragment.lock.json 48 | artifacts/ 49 | **/Properties/launchSettings.json 50 | 51 | *_i.c 52 | *_p.c 53 | *_i.h 54 | *.ilk 55 | *.meta 56 | *.obj 57 | *.pch 58 | *.pdb 59 | *.pgc 60 | *.pgd 61 | *.rsp 62 | *.sbr 63 | *.tlb 64 | *.tli 65 | *.tlh 66 | *.tmp 67 | *.tmp_proj 68 | *.log 69 | *.vspscc 70 | *.vssscc 71 | .builds 72 | *.pidb 73 | *.svclog 74 | *.scc 75 | 76 | # Chutzpah Test files 77 | _Chutzpah* 78 | 79 | # Visual C++ cache files 80 | ipch/ 81 | *.aps 82 | *.ncb 83 | *.opendb 84 | *.opensdf 85 | *.sdf 86 | *.cachefile 87 | *.VC.db 88 | *.VC.VC.opendb 89 | 90 | # Visual Studio profiler 91 | *.psess 92 | *.vsp 93 | *.vspx 94 | *.sap 95 | 96 | # TFS 2012 Local Workspace 97 | $tf/ 98 | 99 | # Guidance Automation Toolkit 100 | *.gpState 101 | 102 | # ReSharper is a .NET coding add-in 103 | _ReSharper*/ 104 | *.[Rr]e[Ss]harper 105 | *.DotSettings.user 106 | 107 | # JustCode is a .NET coding add-in 108 | .JustCode 109 | 110 | # TeamCity is a build add-in 111 | _TeamCity* 112 | 113 | # DotCover is a Code Coverage Tool 114 | *.dotCover 115 | 116 | # Visual Studio code coverage results 117 | *.coverage 118 | *.coveragexml 119 | 120 | # NCrunch 121 | _NCrunch_* 122 | .*crunch*.local.xml 123 | nCrunchTemp_* 124 | 125 | # MightyMoose 126 | *.mm.* 127 | AutoTest.Net/ 128 | 129 | # Web workbench (sass) 130 | .sass-cache/ 131 | 132 | # Installshield output folder 133 | [Ee]xpress/ 134 | 135 | # DocProject is a documentation generator add-in 136 | DocProject/buildhelp/ 137 | DocProject/Help/*.HxT 138 | DocProject/Help/*.HxC 139 | DocProject/Help/*.hhc 140 | DocProject/Help/*.hhk 141 | DocProject/Help/*.hhp 142 | DocProject/Help/Html2 143 | DocProject/Help/html 144 | 145 | # Click-Once directory 146 | publish/ 147 | 148 | # Publish Web Output 149 | *.[Pp]ublish.xml 150 | *.azurePubxml 151 | # TODO: Comment the next line if you want to checkin your web deploy settings 152 | # but database connection strings (with potential passwords) will be unencrypted 153 | *.pubxml 154 | *.publishproj 155 | 156 | # Microsoft Azure Web App publish settings. Comment the next line if you want to 157 | # checkin your Azure Web App publish settings, but sensitive information contained 158 | # in these scripts will be unencrypted 159 | PublishScripts/ 160 | 161 | # NuGet Packages 162 | *.nupkg 163 | # The packages folder can be ignored because of Package Restore 164 | **/packages/* 165 | # except build/, which is used as an MSBuild target. 166 | !**/packages/build/ 167 | # Uncomment if necessary however generally it will be regenerated when needed 168 | #!**/packages/repositories.config 169 | # NuGet v3's project.json files produces more ignorable files 170 | *.nuget.props 171 | *.nuget.targets 172 | 173 | # Microsoft Azure Build Output 174 | csx/ 175 | *.build.csdef 176 | 177 | # Microsoft Azure Emulator 178 | ecf/ 179 | rcf/ 180 | 181 | # Windows Store app package directories and files 182 | AppPackages/ 183 | BundleArtifacts/ 184 | Package.StoreAssociation.xml 185 | _pkginfo.txt 186 | 187 | # Visual Studio cache files 188 | # files ending in .cache can be ignored 189 | *.[Cc]ache 190 | # but keep track of directories ending in .cache 191 | !*.[Cc]ache/ 192 | 193 | # Others 194 | ClientBin/ 195 | ~$* 196 | *~ 197 | *.dbmdl 198 | *.dbproj.schemaview 199 | *.jfm 200 | *.pfx 201 | *.publishsettings 202 | orleans.codegen.cs 203 | 204 | # Since there are multiple workflows, uncomment next line to ignore bower_components 205 | # (https://github.com/github/gitignore/pull/1529#issuecomment-104372622) 206 | #bower_components/ 207 | 208 | # RIA/Silverlight projects 209 | Generated_Code/ 210 | 211 | # Backup & report files from converting an old project file 212 | # to a newer Visual Studio version. Backup files are not needed, 213 | # because we have git ;-) 214 | _UpgradeReport_Files/ 215 | Backup*/ 216 | UpgradeLog*.XML 217 | UpgradeLog*.htm 218 | 219 | # SQL Server files 220 | *.mdf 221 | *.ldf 222 | *.ndf 223 | 224 | # Business Intelligence projects 225 | *.rdl.data 226 | *.bim.layout 227 | *.bim_*.settings 228 | 229 | # Microsoft Fakes 230 | FakesAssemblies/ 231 | 232 | # GhostDoc plugin setting file 233 | *.GhostDoc.xml 234 | 235 | # Node.js Tools for Visual Studio 236 | .ntvs_analysis.dat 237 | node_modules/ 238 | 239 | # Typescript v1 declaration files 240 | typings/ 241 | 242 | # Visual Studio 6 build log 243 | *.plg 244 | 245 | # Visual Studio 6 workspace options file 246 | *.opt 247 | 248 | # Visual Studio 6 auto-generated workspace file (contains which files were open etc.) 249 | *.vbw 250 | 251 | # Visual Studio LightSwitch build output 252 | **/*.HTMLClient/GeneratedArtifacts 253 | **/*.DesktopClient/GeneratedArtifacts 254 | **/*.DesktopClient/ModelManifest.xml 255 | **/*.Server/GeneratedArtifacts 256 | **/*.Server/ModelManifest.xml 257 | _Pvt_Extensions 258 | 259 | # Paket dependency manager 260 | .paket/paket.exe 261 | paket-files/ 262 | 263 | # FAKE - F# Make 264 | .fake/ 265 | 266 | # JetBrains Rider 267 | .idea/ 268 | *.sln.iml 269 | 270 | # CodeRush 271 | .cr/ 272 | 273 | # Python Tools for Visual Studio (PTVS) 274 | __pycache__/ 275 | *.pyc 276 | 277 | # Cake - Uncomment if you are using it 278 | # tools/** 279 | # !tools/packages.config 280 | 281 | # Telerik's JustMock configuration file 282 | *.jmconfig 283 | 284 | # BizTalk build output 285 | *.btp.cs 286 | *.btm.cs 287 | *.odx.cs 288 | *.xsd.cs 289 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # LanguageExt.UnitTesting 2 | Extension methods to simplify writing unit tests for code written using LanguageExt library. These extension methods throw an exception if expectation fails. 3 | 4 | Avalable as [LanguageExt.UnitTesting](https://www.nuget.org/packages/LanguageExt.UnitTesting/ "nuget package url") nuget package. 5 | 6 | ## Option 7 | * ```ShouldBeSome(Action someValidation)``` 8 | * ```ShouldBeNone()``` 9 | ```C# 10 | Option subject = UnitUnderTest(); 11 | 12 | // the following line throws an exception if subject is Option.None 13 | // or the integer value wrapped by Some does not equal 5 14 | subject.ShouldBeSome(some => Assert.Equal(5, some)); 15 | 16 | // the following line throws an exception if subject is Option.None 17 | subject.ShouldBeSome(); 18 | 19 | // the following line throws an exception if subject is not Option.None 20 | subject.ShouldBeNone(); 21 | ``` 22 | 23 | ## OptionAsync 24 | * ```ShouldBeSome(Action someValidation)``` 25 | * ```ShouldBeNone()``` 26 | ```C# 27 | OptionAsync subject = UnitUnderTest(); 28 | 29 | // the following line throws an exception if subject is OptionAsync.None 30 | // or the integer value wrapped by Some does not equal 5 31 | await subject.ShouldBeSome(some => Assert.Equal(5, some)); 32 | 33 | // the following line throws an exception if subject is OptionAsync.None 34 | await subject.ShouldBeSome(); 35 | 36 | // the following line throws an exception if subject is not OptionAsync.None 37 | await subject.ShouldBeNone(); 38 | ``` 39 | 40 | ## Validation 41 | * ```ShouldBeSuccess(Action successValidation)``` 42 | * ```ShouldBeFail(Action> failValidation)``` 43 | ```C# 44 | Validation subject = UnitUnderTest(); 45 | 46 | // the following line throws an exception if subject represents failure 47 | // or in case of successful validation the integer value does not equal 5 48 | subject.ShouldBeSuccess(success => Assert.Equal(5, success)); 49 | 50 | // the following line throws an exception if subject represents failure 51 | subject.ShouldBeSuccess(); 52 | 53 | // the following line throws an exception if subject does not represent failed validation 54 | // or in case of failed validation the failure value does not meet expectation 55 | subject.ShouldBeFail(errors => Assert("value is not valid", errors.First())); 56 | 57 | // the following line throws an exception if subject does not represent failed validation 58 | subject.ShouldBeFail(); 59 | ``` 60 | 61 | 62 | ## Fin 63 | * ```ShouldBeSuccess(Action successFin)``` 64 | * ```ShouldBeFail(Action errFin)``` 65 | ```C# 66 | Fin subject = UnitUnderTest(); 67 | 68 | // the following line throws an exception if subject represents failure 69 | // or in case of successful fin the integer value does not equal 5 70 | subject.ShouldBeSuccess(success => Assert.Equal(5, success)); 71 | 72 | // the following line throws an exception if subject represents failure 73 | subject.ShouldBeSuccess(); 74 | 75 | // the following line throws an exception if subject does not represent failure 76 | // or in case of failed fin the failure value does not meet expectation 77 | subject.ShouldBeFail(error => Assert("value is not valid", error.Message)); 78 | 79 | // the following line throws an exception if subject does not represent failed fin 80 | subject.ShouldBeFail(); 81 | ``` 82 | 83 | ## Either 84 | * ```ShouldBeRight(Action rightValidation)``` 85 | * ```ShouldBeLeft(Action leftValidation)``` 86 | ```C# 87 | Either subject = UnitUnderTest(); 88 | 89 | // the following line throws an exception if subject represents left side of Either 90 | // or in case of right side of Either when the integer value does not equal 5 91 | subject.ShouldBeRight(right => Assert.Equal(5, right)); 92 | 93 | // the following line throws an exception if subject represents left side of Either 94 | subject.ShouldBeRight(); 95 | 96 | // the following line throws an exception if subject represents right side of Either 97 | // or in case of left side of Either when the string value does not equal "abcd" 98 | subject.ShouldBeLeft(left => Assert.Equal("abcd", left)); 99 | 100 | // the following line throws an exception if subject represents right side of Either 101 | subject.ShouldBeLeft(); 102 | ``` 103 | 104 | 105 | ## EitherAsync 106 | * ```ShouldBeRight(Action rightValidation)``` 107 | * ```ShouldBeLeft(Action leftValidation)``` 108 | ```C# 109 | EitherAsync subject = UnitUnderTest(); 110 | 111 | // the following line throws an exception if subject represents left side of Either 112 | // or in case of right side of Either when the integer value does not equal 5 113 | await subject.ShouldBeRight(right => Assert.Equal(5, right)); 114 | 115 | // the following line throws an exception if subject represents left side of Either 116 | await subject.ShouldBeRight(); 117 | 118 | // the following line throws an exception if subject represents right side of Either 119 | // or in case of left side of Either when the string value does not equal "abcd" 120 | await subject.ShouldBeLeft(left => Assert.Equal("abcd", left)); 121 | 122 | // the following line throws an exception if subject represents right side of Either 123 | await subject.ShouldBeLeft(); 124 | ``` 125 | 126 | ## Try 127 | * ```ShouldBeSuccess(Action successValidation)``` 128 | * ```ShouldBeFail(Action failValidation)``` 129 | ```C# 130 | Try subject = UnitUnderTest(); 131 | 132 | // the following line throws an exception if subject represents failure 133 | // or in case of successful try the integer value does not equal 5 134 | subject.ShouldBeSuccess(success => Assert.Equal(5, success)); 135 | 136 | // the following line throws an exception if subject represents failure 137 | subject.ShouldBeSuccess(); 138 | 139 | // the following line throws an exception if subject does not represent failure 140 | // or in case of failure the exception has wrong message 141 | subject.ShouldBeFail(ex => Assert.Equal("something went wrong", ex.Message)); 142 | 143 | // the following line throws an exception if subject does not represent failure 144 | subject.ShouldBeFail(); 145 | ``` 146 | 147 | ## TryAsync 148 | * ```ShouldBeSuccess(Action successValidation)``` 149 | * ```ShouldBeFail(Action failValidation)``` 150 | ```C# 151 | TryAsync subject = UnitUnderTest(); 152 | 153 | // the following line throws an exception if subject represents failure 154 | // or in case of successful try the integer value does not equal 5 155 | await subject.ShouldBeSuccess(success => Assert.Equal(5, success)); 156 | 157 | // the following line throws an exception if subject represents failure 158 | await subject.ShouldBeSuccess(); 159 | 160 | // the following line throws an exception if subject does not represent failure 161 | // or in case of failure the exception has wrong message 162 | await subject.ShouldBeFail(ex => Assert.Equal("something went wrong", ex.Message)); 163 | 164 | // the following line throws an exception if subject does not represent failure 165 | await subject.ShouldBeFail(); 166 | ``` 167 | 168 | ## TryOption 169 | * ```ShouldBeSome(Action someValidation)``` 170 | * ```ShouldBeNone()``` 171 | * ```ShouldBeFail(Action failValidation)``` 172 | ```C# 173 | TryOption subject = UnitUnderTest(); 174 | 175 | // the following line throws an exception if subject represents failure or Option.None 176 | // or the integer value wrapped by Some does not equal 5 177 | subject.ShouldBeSome(some => Assert.Equal(5, some)); 178 | 179 | // the following line throws an exception if subject represents failure or Option.None 180 | subject.ShouldBeSome(some => Assert.Equal(5, some)); 181 | 182 | // the following line throws an exception if subject is not Option.None 183 | subject.ShouldBeNone(); 184 | 185 | // the following line throws an exception if subject does not represent failure 186 | // or in case of failure the exception has wrong message 187 | subject.ShouldBeFail(ex => Assert.Equal("something went wrong", ex.Message)); 188 | 189 | // the following line throws an exception if subject does not represent failure 190 | subject.ShouldBeFail(); 191 | ``` 192 | 193 | ## TryOptionAsync 194 | * ```ShouldBeSome(Action someValidation)``` 195 | * ```ShouldBeNone()``` 196 | * ```ShouldBeFail(Action failValidation)``` 197 | ```C# 198 | TryOptionAsync subject = UnitUnderTest(); 199 | 200 | // the following line throws an exception if subject represents failure or Option.None 201 | // or the integer value wrapped by Some does not equal 5 202 | await subject.ShouldBeSome(some => Assert.Equal(5, some)); 203 | 204 | // the following line throws an exception if subject represents failure or Option.None 205 | await subject.ShouldBeSome(); 206 | 207 | // the following line throws an exception if subject is not Option.None 208 | await subject.ShouldBeNone(); 209 | 210 | // the following line throws an exception if subject does not represent failure 211 | // or in case of failure the exception has wrong message 212 | await subject.ShouldBeFail(ex => Assert.Equal("something went wrong", ex.Message)); 213 | 214 | // the following line throws an exception if subject does not represent failure 215 | await subject.ShouldBeFail(); 216 | ``` 217 | ## Result 218 | * ```ShouldBeSuccess(Action successValidation)``` 219 | * ```ShouldBeFail(Action failValidation)``` 220 | ```C# 221 | Result subject = UnitUnderTest(); 222 | 223 | // the following line throws an exception if subject represents failure 224 | // or in case of successful result the integer value does not equal 5 225 | subject.ShouldBeSuccess(success => Assert.Equal(5, success)); 226 | 227 | // the following line throws an exception if subject represents failure 228 | subject.ShouldBeSuccess(); 229 | 230 | // the following line throws an exception if subject does not represent failure 231 | // or in case of failed result the failure value does not meet expectation 232 | subject.ShouldBeFail(error => Assert("value is not valid", error.Message)); 233 | 234 | // the following line throws an exception if subject does not represent failed result 235 | subject.ShouldBeFail(); 236 | ``` --------------------------------------------------------------------------------