Skip to content

Commit

Permalink
Merge pull request #391 from gdziadkiewicz/gdziadkiewicz/issue313
Browse files Browse the repository at this point in the history
Add Option.someIf and tests for it.
  • Loading branch information
gdziadkiewicz authored May 31, 2020
2 parents e8ba1fe + 03b3ca4 commit 1c30663
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 2 deletions.
1 change: 1 addition & 0 deletions paket.dependencies
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ group Test
nuget NUnit
nuget NUnit3TestAdapter
nuget FsCheck
nuget FsCheck.NUnit
nuget FsUnit
nuget Microsoft.NET.Test.Sdk
nuget Appveyor.TestLogger
Expand Down
3 changes: 3 additions & 0 deletions paket.lock
Original file line number Diff line number Diff line change
Expand Up @@ -708,6 +708,9 @@ NUGET
Appveyor.TestLogger (2.0)
FsCheck (2.14.2)
FSharp.Core (>= 4.2.3)
FsCheck.NUnit (2.14.2)
FsCheck (2.14.2)
NUnit (>= 3.10.1 < 4.0)
FSharp.Core (4.7.2)
FsUnit (3.8.1)
FSharp.Core (>= 4.2.3)
Expand Down
5 changes: 4 additions & 1 deletion src/FSharpx.Extras/ComputationExpressions/Option.fs
Original file line number Diff line number Diff line change
Expand Up @@ -242,4 +242,7 @@ module Option =
let inline isSome (o:Option<'a>) : bool =
match o with
| Some _ -> true
| _ -> false
| _ -> false

/// Checks condition on x - if true returns Some x else None
let someIf (condition:'a->bool) (x:'a) : 'a option = if condition x then Some x else None
1 change: 1 addition & 0 deletions tests/FSharpx.Tests/FSharpx.Tests.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
<TargetFrameworks>net46;netcoreapp31</TargetFrameworks>
<IsPackable>false</IsPackable>
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
<LangVersion>preview</LangVersion>
</PropertyGroup>
<PropertyGroup Condition=" '$(OS)' != 'Windows_NT' ">
<TargetFrameworks>netcoreapp31</TargetFrameworks>
Expand Down
20 changes: 19 additions & 1 deletion tests/FSharpx.Tests/OptionTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

open System
open NUnit.Framework
open FsUnitTyped
open FsCheck.NUnit
open FSharpx.Functional
open FSharpx
open FSharpx.Option
Expand Down Expand Up @@ -75,4 +77,20 @@ let ``orElseLazy Some``() =
[<Test>]
let ``orElseLazy None``() =
let r = None |> Option.orElseLazy (lazy Some 2)
Assert.AreEqual(Some 2, r)
Assert.AreEqual(Some 2, r)

[<Property>]
let ``someIf with always true predicate`` (x:int) =
Option.someIf (konst true) x = Some x

[<Property>]
let ``someIf with always false predicate`` (x:int) =
Option.someIf (konst false) x = None

let someIfBoolTestCases = [
TestCaseData(true, Some true)
TestCaseData(false, None)
]
[<TestCaseSource(nameof someIfBoolTestCases)>]
let ``someIf with id`` (input:bool, expectedOutput:bool option) =
Option.someIf id input |> shouldEqual expectedOutput
1 change: 1 addition & 0 deletions tests/FSharpx.Tests/paket.references
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
group Test
FsCheck
FsUnit
FsCheck.NUnit
NUnit
Microsoft.NET.Test.Sdk
NUnit3TestAdapter
Expand Down

0 comments on commit 1c30663

Please sign in to comment.