Skip to content

Commit

Permalink
Added Option.ofPair and ValueOption.ofPair #207 (#208)
Browse files Browse the repository at this point in the history
Co-authored-by: Alberto De Pena <alberto.depena@craneww.com>
  • Loading branch information
AlbertoDePena and Alberto De Pena committed Jan 20, 2023
1 parent aa87d3d commit a6eb4bc
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/FsToolkit.ErrorHandling/Option.fs
Expand Up @@ -138,3 +138,8 @@ module Option =
match input with
| Some x -> onSome x
| None -> onNone ()

let ofPair (input: bool * 'a) =
match input with
| true, x -> Some x
| false, _ -> None
5 changes: 5 additions & 0 deletions src/FsToolkit.ErrorHandling/ValueOption.fs
Expand Up @@ -143,4 +143,9 @@ module ValueOption =
| ValueSome x -> onSome x
| ValueNone -> onNone ()

let inline ofPair (input: bool * 'a) =
match input with
| true, x -> ValueSome x
| false, _ -> ValueNone

#endif
46 changes: 46 additions & 0 deletions tests/FsToolkit.ErrorHandling.Tests/Option.fs
Expand Up @@ -127,6 +127,51 @@ let eitherTests =
Expect.equal (Option.either add2 f value1) 42 ""
]

let ofPairTests =
testList "Option.ofPair Tests" [
testCase "Int32.TryParse => Some Int32"
<| fun _ ->
let input = "1989"
let pair = Int32.TryParse input
Expect.equal (Option.ofPair pair) (Some 1989) ""
testCase "Int32.TryParse => None"
<| fun _ ->
let input = "FsToolkit.ErrorHandling"
let pair = Int32.TryParse input
Expect.equal (Option.ofPair pair) (None) ""
testCase "Int64.TryParse => Some Int64"
<| fun _ ->
let input = "1989"
let pair = Int64.TryParse input
Expect.equal (Option.ofPair pair) (Some 1989L) ""
testCase "Int64.TryParse => None"
<| fun _ ->
let input = "FsToolkit.ErrorHandling"
let pair = Int64.TryParse input
Expect.equal (Option.ofPair pair) (None) ""
testCase "Decimal.TryParse => Some Decimal"
<| fun _ ->
let input = "1989"
let pair = Decimal.TryParse input
Expect.equal (Option.ofPair pair) (Some 1989M) ""
testCase "Decimal.TryParse => None"
<| fun _ ->
let input = "FsToolkit.ErrorHandling"
let pair = Decimal.TryParse input
Expect.equal (Option.ofPair pair) (None) ""
testCase "Guid.TryParse => Some Guid"
<| fun _ ->
let guid = Guid.NewGuid()
let input = guid.ToString()
let pair = Guid.TryParse input
Expect.equal (Option.ofPair pair) (Some guid) ""
testCase "Guid.TryParse => None"
<| fun _ ->
let input = "FsToolkit.ErrorHandling"
let pair = Guid.TryParse input
Expect.equal (Option.ofPair pair) (None) ""
]

let allTests =
testList "Option Tests" [
traverseResultTests
Expand All @@ -136,4 +181,5 @@ let allTests =
ofNullTests
bindNullTests
eitherTests
ofPairTests
]
46 changes: 46 additions & 0 deletions tests/FsToolkit.ErrorHandling.Tests/ValueOption.fs
Expand Up @@ -131,6 +131,51 @@ let eitherTests =
Expect.equal (ValueOption.either add2 f value1) 42 ""
]

let ofPairTests =
testList "ValueOption.ofPair Tests" [
testCase "Int32.TryParse => ValueSome Int32"
<| fun _ ->
let input = "1989"
let pair = Int32.TryParse input
Expect.equal (ValueOption.ofPair pair) (ValueSome 1989) ""
testCase "Int32.TryParse => ValueNone"
<| fun _ ->
let input = "FsToolkit.ErrorHandling"
let pair = Int32.TryParse input
Expect.equal (ValueOption.ofPair pair) (ValueNone) ""
testCase "Int64.TryParse => ValueSome Int64"
<| fun _ ->
let input = "1989"
let pair = Int64.TryParse input
Expect.equal (ValueOption.ofPair pair) (ValueSome 1989L) ""
testCase "Int64.TryParse => ValueNone"
<| fun _ ->
let input = "FsToolkit.ErrorHandling"
let pair = Int64.TryParse input
Expect.equal (ValueOption.ofPair pair) (ValueNone) ""
testCase "Decimal.TryParse => ValueSome Decimal"
<| fun _ ->
let input = "1989"
let pair = Decimal.TryParse input
Expect.equal (ValueOption.ofPair pair) (ValueSome 1989M) ""
testCase "Decimal.TryParse => ValueNone"
<| fun _ ->
let input = "FsToolkit.ErrorHandling"
let pair = Decimal.TryParse input
Expect.equal (ValueOption.ofPair pair) (ValueNone) ""
testCase "Guid.TryParse => ValueSome Guid"
<| fun _ ->
let guid = Guid.NewGuid()
let input = guid.ToString()
let pair = Guid.TryParse input
Expect.equal (ValueOption.ofPair pair) (ValueSome guid) ""
testCase "Guid.TryParse => ValueNone"
<| fun _ ->
let input = "FsToolkit.ErrorHandling"
let pair = Guid.TryParse input
Expect.equal (ValueOption.ofPair pair) (ValueNone) ""
]

let allTests =
testList "ValueOption Tests" [
traverseResultTests
Expand All @@ -140,6 +185,7 @@ let allTests =
ofNullTests
bindNullTests
eitherTests
ofPairTests
]
#else

Expand Down

0 comments on commit a6eb4bc

Please sign in to comment.