Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RFC FS-1007: Additions to Option module #1781

Merged
merged 16 commits into from
Dec 17, 2016
Merged
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,15 @@ Make sure each method works on:
[<TestFixture>]
type OptionModule() =

let assertWasNotCalledThunk () = raise (exn "Thunk should not have been called.")

[<Test>]
member this.Flatten () =
Assert.AreEqual( Option.flatten None, None)
Assert.AreEqual( Option.flatten (Some None), None)
Assert.AreEqual( Option.flatten (Some <| Some 1), Some 1)
Assert.AreEqual( Option.flatten (Some <| Some ""), Some "")

[<Test>]
member this.FilterSomeIntegerWhenPredicateReturnsTrue () =
let test x =
Expand Down Expand Up @@ -98,7 +107,17 @@ type OptionModule() =
let expected = None
Assert.AreEqual(expected, actual)
[""; " "; "Baz Quux"; "Corge grault"] |> List.iter test


[<Test>]
member this.Contains() =
Assert.IsFalse( Option.contains 1 None)
Assert.IsTrue( Option.contains 1 (Some 1))

Assert.IsFalse( Option.contains "" None)
Assert.IsTrue( Option.contains "" (Some ""))

Assert.IsFalse( Option.contains None None)
Assert.IsTrue( Option.contains None (Some None))
[<Test>]
member this.OfToNullable() =
Assert.IsTrue( Option.ofNullable (System.Nullable<int>()) = None)
Expand All @@ -121,4 +140,79 @@ type OptionModule() =
Assert.IsTrue( Option.ofObj (null : string array) = None)
Assert.IsTrue( Option.ofObj<string> null = None)
Assert.IsTrue( Option.ofObj<string[]> null = None)
Assert.IsTrue( Option.ofObj<int[]> null = None)
Assert.IsTrue( Option.ofObj<int[]> null = None)

[<Test>]
member this.DefaultValue() =
Assert.AreEqual( Option.defaultValue 3 None, 3)
Assert.AreEqual( Option.defaultValue 3 (Some 42), 42)
Assert.AreEqual( Option.defaultValue "" None, "")
Assert.AreEqual( Option.defaultValue "" (Some "x"), "x")

[<Test>]
member this.DefaultWith() =
Assert.AreEqual( Option.defaultWith (fun () -> 3) None, 3)
Assert.AreEqual( Option.defaultWith (fun () -> "") None, "")

Assert.AreEqual( Option.defaultWith assertWasNotCalledThunk (Some 42), 42)
Assert.AreEqual( Option.defaultWith assertWasNotCalledThunk (Some ""), "")

[<Test>]
member this.OrElse() =
Assert.AreEqual( Option.orElse None None, None)
Assert.AreEqual( Option.orElse (Some 3) None, Some 3)
Assert.AreEqual( Option.orElse None (Some 42), Some 42)
Assert.AreEqual( Option.orElse (Some 3) (Some 42), Some 42)

Assert.AreEqual( Option.orElse (Some "") None, Some "")
Assert.AreEqual( Option.orElse None (Some "x"), Some "x")
Assert.AreEqual( Option.orElse (Some "") (Some "x"), Some "x")

[<Test>]
member this.OrElseWith() =
Assert.AreEqual( Option.orElseWith (fun () -> None) None, None)
Assert.AreEqual( Option.orElseWith (fun () -> Some 3) None, Some 3)
Assert.AreEqual( Option.orElseWith (fun () -> Some "") None, Some "")

Assert.AreEqual( Option.orElseWith assertWasNotCalledThunk (Some 42), Some 42)
Assert.AreEqual( Option.orElseWith assertWasNotCalledThunk (Some ""), Some "")

[<Test>]
member this.Map2() =
Assert.AreEqual( Option.map2 (-) None None, None)
Assert.AreEqual( Option.map2 (-) (Some 1) None, None)
Assert.AreEqual( Option.map2 (-) None (Some 2), None)
Assert.AreEqual( Option.map2 (-) (Some 1) (Some 2), Some -1)

Assert.AreEqual( Option.map2 (+) None None, None)
Assert.AreEqual( Option.map2 (+) (Some "x") None, None)
Assert.AreEqual( Option.map2 (+) None (Some "y"), None)
Assert.AreEqual( Option.map2 (+) (Some "x") (Some "y"), Some "xy")

[<Test>]
member this.Map3() =
let add3 x y z = string x + string y + string z
Assert.AreEqual( Option.map3 add3 None None None, None)
Assert.AreEqual( Option.map3 add3 (Some 1) None None, None)
Assert.AreEqual( Option.map3 add3 None (Some 2) None, None)
Assert.AreEqual( Option.map3 add3 (Some 1) (Some 2) None, None)
Assert.AreEqual( Option.map3 add3 None None (Some 3), None)
Assert.AreEqual( Option.map3 add3 (Some 1) None (Some 3), None)
Assert.AreEqual( Option.map3 add3 None (Some 2) (Some 3), None)
Assert.AreEqual( Option.map3 add3 (Some 1) (Some 2) (Some 3), Some "123")

let concat3 x y z = x + y + z
Assert.AreEqual( Option.map3 concat3 None None None, None)
Assert.AreEqual( Option.map3 concat3 (Some "x") None None, None)
Assert.AreEqual( Option.map3 concat3 None (Some "y") None, None)
Assert.AreEqual( Option.map3 concat3 (Some "x") (Some "y") None, None)
Assert.AreEqual( Option.map3 concat3 None None (Some "z"), None)
Assert.AreEqual( Option.map3 concat3 (Some "x") None (Some "z"), None)
Assert.AreEqual( Option.map3 concat3 None (Some "y") (Some "z"), None)
Assert.AreEqual( Option.map3 concat3 (Some "x") (Some "y") (Some "z"), Some "xyz")

[<Test>]
member this.MapBindEquivalenceProperties () =
let fn x = x + 3
Assert.AreEqual(Option.map fn None, Option.bind (fn >> Some) None)
Assert.AreEqual(Option.map fn (Some 5), Option.bind (fn >> Some) (Some 5))
8 changes: 8 additions & 0 deletions src/fsharp/FSharp.Core.Unittests/SurfaceArea.coreclr.fs
Original file line number Diff line number Diff line change
Expand Up @@ -2713,6 +2713,7 @@ Microsoft.FSharp.Core.OptimizedClosures: Microsoft.FSharp.Core.OptimizedClosures
Microsoft.FSharp.Core.OptimizedClosures: Microsoft.FSharp.Core.OptimizedClosures+FSharpFunc`6[T1,T2,T3,T4,T5,TResult]
Microsoft.FSharp.Core.OptimizedClosures: System.String ToString()
Microsoft.FSharp.Core.OptimizedClosures: System.Type GetType()
Microsoft.FSharp.Core.OptionModule: Boolean Contains[T](T, Microsoft.FSharp.Core.FSharpOption`1[T])
Microsoft.FSharp.Core.OptionModule: Boolean Equals(System.Object)
Microsoft.FSharp.Core.OptionModule: Boolean Exists[T](Microsoft.FSharp.Core.FSharpFunc`2[T,System.Boolean], Microsoft.FSharp.Core.FSharpOption`1[T])
Microsoft.FSharp.Core.OptionModule: Boolean ForAll[T](Microsoft.FSharp.Core.FSharpFunc`2[T,System.Boolean], Microsoft.FSharp.Core.FSharpOption`1[T])
Expand All @@ -2722,13 +2723,20 @@ Microsoft.FSharp.Core.OptionModule: Int32 Count[T](Microsoft.FSharp.Core.FSharpO
Microsoft.FSharp.Core.OptionModule: Int32 GetHashCode()
Microsoft.FSharp.Core.OptionModule: Microsoft.FSharp.Collections.FSharpList`1[T] ToList[T](Microsoft.FSharp.Core.FSharpOption`1[T])
Microsoft.FSharp.Core.OptionModule: Microsoft.FSharp.Core.FSharpOption`1[TResult] Bind[T,TResult](Microsoft.FSharp.Core.FSharpFunc`2[T,Microsoft.FSharp.Core.FSharpOption`1[TResult]], Microsoft.FSharp.Core.FSharpOption`1[T])
Microsoft.FSharp.Core.OptionModule: Microsoft.FSharp.Core.FSharpOption`1[TResult] Map2[T1,T2,TResult](Microsoft.FSharp.Core.FSharpFunc`2[T1,Microsoft.FSharp.Core.FSharpFunc`2[T2,TResult]], Microsoft.FSharp.Core.FSharpOption`1[T1], Microsoft.FSharp.Core.FSharpOption`1[T2])
Microsoft.FSharp.Core.OptionModule: Microsoft.FSharp.Core.FSharpOption`1[TResult] Map3[T1,T2,T3,TResult](Microsoft.FSharp.Core.FSharpFunc`2[T1,Microsoft.FSharp.Core.FSharpFunc`2[T2,Microsoft.FSharp.Core.FSharpFunc`2[T3,TResult]]], Microsoft.FSharp.Core.FSharpOption`1[T1], Microsoft.FSharp.Core.FSharpOption`1[T2], Microsoft.FSharp.Core.FSharpOption`1[T3])
Microsoft.FSharp.Core.OptionModule: Microsoft.FSharp.Core.FSharpOption`1[TResult] Map[T,TResult](Microsoft.FSharp.Core.FSharpFunc`2[T,TResult], Microsoft.FSharp.Core.FSharpOption`1[T])
Microsoft.FSharp.Core.OptionModule: Microsoft.FSharp.Core.FSharpOption`1[T] Filter[T](Microsoft.FSharp.Core.FSharpFunc`2[T,System.Boolean], Microsoft.FSharp.Core.FSharpOption`1[T])
Microsoft.FSharp.Core.OptionModule: Microsoft.FSharp.Core.FSharpOption`1[T] Flatten[T](Microsoft.FSharp.Core.FSharpOption`1[Microsoft.FSharp.Core.FSharpOption`1[T]])
Microsoft.FSharp.Core.OptionModule: Microsoft.FSharp.Core.FSharpOption`1[T] OfNullable[T](System.Nullable`1[T])
Microsoft.FSharp.Core.OptionModule: Microsoft.FSharp.Core.FSharpOption`1[T] OfObj[T](T)
Microsoft.FSharp.Core.OptionModule: Microsoft.FSharp.Core.FSharpOption`1[T] OrElseWith[T](Microsoft.FSharp.Core.FSharpFunc`2[Microsoft.FSharp.Core.Unit,Microsoft.FSharp.Core.FSharpOption`1[T]], Microsoft.FSharp.Core.FSharpOption`1[T])
Microsoft.FSharp.Core.OptionModule: Microsoft.FSharp.Core.FSharpOption`1[T] OrElse[T](Microsoft.FSharp.Core.FSharpOption`1[T], Microsoft.FSharp.Core.FSharpOption`1[T])
Microsoft.FSharp.Core.OptionModule: System.Nullable`1[T] ToNullable[T](Microsoft.FSharp.Core.FSharpOption`1[T])
Microsoft.FSharp.Core.OptionModule: System.String ToString()
Microsoft.FSharp.Core.OptionModule: System.Type GetType()
Microsoft.FSharp.Core.OptionModule: T DefaultValue[T](T, Microsoft.FSharp.Core.FSharpOption`1[T])
Microsoft.FSharp.Core.OptionModule: T DefaultWith[T](Microsoft.FSharp.Core.FSharpFunc`2[Microsoft.FSharp.Core.Unit,T], Microsoft.FSharp.Core.FSharpOption`1[T])
Microsoft.FSharp.Core.OptionModule: T GetValue[T](Microsoft.FSharp.Core.FSharpOption`1[T])
Microsoft.FSharp.Core.OptionModule: T ToObj[T](Microsoft.FSharp.Core.FSharpOption`1[T])
Microsoft.FSharp.Core.OptionModule: TState FoldBack[T,TState](Microsoft.FSharp.Core.FSharpFunc`2[T,Microsoft.FSharp.Core.FSharpFunc`2[TState,TState]], Microsoft.FSharp.Core.FSharpOption`1[T], TState)
Expand Down
8 changes: 8 additions & 0 deletions src/fsharp/FSharp.Core.Unittests/SurfaceArea.net40.fs
Original file line number Diff line number Diff line change
Expand Up @@ -2747,6 +2747,7 @@ Microsoft.FSharp.Core.OptimizedClosures: Microsoft.FSharp.Core.OptimizedClosures
Microsoft.FSharp.Core.OptimizedClosures: Microsoft.FSharp.Core.OptimizedClosures+FSharpFunc`6[T1,T2,T3,T4,T5,TResult]
Microsoft.FSharp.Core.OptimizedClosures: System.String ToString()
Microsoft.FSharp.Core.OptimizedClosures: System.Type GetType()
Microsoft.FSharp.Core.OptionModule: Boolean Contains[T](T, Microsoft.FSharp.Core.FSharpOption`1[T])
Microsoft.FSharp.Core.OptionModule: Boolean Equals(System.Object)
Microsoft.FSharp.Core.OptionModule: Boolean Exists[T](Microsoft.FSharp.Core.FSharpFunc`2[T,System.Boolean], Microsoft.FSharp.Core.FSharpOption`1[T])
Microsoft.FSharp.Core.OptionModule: Boolean ForAll[T](Microsoft.FSharp.Core.FSharpFunc`2[T,System.Boolean], Microsoft.FSharp.Core.FSharpOption`1[T])
Expand All @@ -2756,13 +2757,20 @@ Microsoft.FSharp.Core.OptionModule: Int32 Count[T](Microsoft.FSharp.Core.FSharpO
Microsoft.FSharp.Core.OptionModule: Int32 GetHashCode()
Microsoft.FSharp.Core.OptionModule: Microsoft.FSharp.Collections.FSharpList`1[T] ToList[T](Microsoft.FSharp.Core.FSharpOption`1[T])
Microsoft.FSharp.Core.OptionModule: Microsoft.FSharp.Core.FSharpOption`1[TResult] Bind[T,TResult](Microsoft.FSharp.Core.FSharpFunc`2[T,Microsoft.FSharp.Core.FSharpOption`1[TResult]], Microsoft.FSharp.Core.FSharpOption`1[T])
Microsoft.FSharp.Core.OptionModule: Microsoft.FSharp.Core.FSharpOption`1[TResult] Map2[T1,T2,TResult](Microsoft.FSharp.Core.FSharpFunc`2[T1,Microsoft.FSharp.Core.FSharpFunc`2[T2,TResult]], Microsoft.FSharp.Core.FSharpOption`1[T1], Microsoft.FSharp.Core.FSharpOption`1[T2])
Microsoft.FSharp.Core.OptionModule: Microsoft.FSharp.Core.FSharpOption`1[TResult] Map3[T1,T2,T3,TResult](Microsoft.FSharp.Core.FSharpFunc`2[T1,Microsoft.FSharp.Core.FSharpFunc`2[T2,Microsoft.FSharp.Core.FSharpFunc`2[T3,TResult]]], Microsoft.FSharp.Core.FSharpOption`1[T1], Microsoft.FSharp.Core.FSharpOption`1[T2], Microsoft.FSharp.Core.FSharpOption`1[T3])
Microsoft.FSharp.Core.OptionModule: Microsoft.FSharp.Core.FSharpOption`1[TResult] Map[T,TResult](Microsoft.FSharp.Core.FSharpFunc`2[T,TResult], Microsoft.FSharp.Core.FSharpOption`1[T])
Microsoft.FSharp.Core.OptionModule: Microsoft.FSharp.Core.FSharpOption`1[T] Filter[T](Microsoft.FSharp.Core.FSharpFunc`2[T,System.Boolean], Microsoft.FSharp.Core.FSharpOption`1[T])
Microsoft.FSharp.Core.OptionModule: Microsoft.FSharp.Core.FSharpOption`1[T] Flatten[T](Microsoft.FSharp.Core.FSharpOption`1[Microsoft.FSharp.Core.FSharpOption`1[T]])
Microsoft.FSharp.Core.OptionModule: Microsoft.FSharp.Core.FSharpOption`1[T] OfNullable[T](System.Nullable`1[T])
Microsoft.FSharp.Core.OptionModule: Microsoft.FSharp.Core.FSharpOption`1[T] OfObj[T](T)
Microsoft.FSharp.Core.OptionModule: Microsoft.FSharp.Core.FSharpOption`1[T] OrElseWith[T](Microsoft.FSharp.Core.FSharpFunc`2[Microsoft.FSharp.Core.Unit,Microsoft.FSharp.Core.FSharpOption`1[T]], Microsoft.FSharp.Core.FSharpOption`1[T])
Microsoft.FSharp.Core.OptionModule: Microsoft.FSharp.Core.FSharpOption`1[T] OrElse[T](Microsoft.FSharp.Core.FSharpOption`1[T], Microsoft.FSharp.Core.FSharpOption`1[T])
Microsoft.FSharp.Core.OptionModule: System.Nullable`1[T] ToNullable[T](Microsoft.FSharp.Core.FSharpOption`1[T])
Microsoft.FSharp.Core.OptionModule: System.String ToString()
Microsoft.FSharp.Core.OptionModule: System.Type GetType()
Microsoft.FSharp.Core.OptionModule: T DefaultValue[T](T, Microsoft.FSharp.Core.FSharpOption`1[T])
Microsoft.FSharp.Core.OptionModule: T DefaultWith[T](Microsoft.FSharp.Core.FSharpFunc`2[Microsoft.FSharp.Core.Unit,T], Microsoft.FSharp.Core.FSharpOption`1[T])
Microsoft.FSharp.Core.OptionModule: T GetValue[T](Microsoft.FSharp.Core.FSharpOption`1[T])
Microsoft.FSharp.Core.OptionModule: T ToObj[T](Microsoft.FSharp.Core.FSharpOption`1[T])
Microsoft.FSharp.Core.OptionModule: TState FoldBack[T,TState](Microsoft.FSharp.Core.FSharpFunc`2[T,Microsoft.FSharp.Core.FSharpFunc`2[TState,TState]], Microsoft.FSharp.Core.FSharpOption`1[T], TState)
Expand Down
8 changes: 8 additions & 0 deletions src/fsharp/FSharp.Core.Unittests/SurfaceArea.portable259.fs
Original file line number Diff line number Diff line change
Expand Up @@ -2718,6 +2718,7 @@ Microsoft.FSharp.Core.OptimizedClosures: Microsoft.FSharp.Core.OptimizedClosures
Microsoft.FSharp.Core.OptimizedClosures: Microsoft.FSharp.Core.OptimizedClosures+FSharpFunc`6[T1,T2,T3,T4,T5,TResult]
Microsoft.FSharp.Core.OptimizedClosures: System.String ToString()
Microsoft.FSharp.Core.OptimizedClosures: System.Type GetType()
Microsoft.FSharp.Core.OptionModule: Boolean Contains[T](T, Microsoft.FSharp.Core.FSharpOption`1[T])
Microsoft.FSharp.Core.OptionModule: Boolean Equals(System.Object)
Microsoft.FSharp.Core.OptionModule: Boolean Exists[T](Microsoft.FSharp.Core.FSharpFunc`2[T,System.Boolean], Microsoft.FSharp.Core.FSharpOption`1[T])
Microsoft.FSharp.Core.OptionModule: Boolean ForAll[T](Microsoft.FSharp.Core.FSharpFunc`2[T,System.Boolean], Microsoft.FSharp.Core.FSharpOption`1[T])
Expand All @@ -2727,13 +2728,20 @@ Microsoft.FSharp.Core.OptionModule: Int32 Count[T](Microsoft.FSharp.Core.FSharpO
Microsoft.FSharp.Core.OptionModule: Int32 GetHashCode()
Microsoft.FSharp.Core.OptionModule: Microsoft.FSharp.Collections.FSharpList`1[T] ToList[T](Microsoft.FSharp.Core.FSharpOption`1[T])
Microsoft.FSharp.Core.OptionModule: Microsoft.FSharp.Core.FSharpOption`1[TResult] Bind[T,TResult](Microsoft.FSharp.Core.FSharpFunc`2[T,Microsoft.FSharp.Core.FSharpOption`1[TResult]], Microsoft.FSharp.Core.FSharpOption`1[T])
Microsoft.FSharp.Core.OptionModule: Microsoft.FSharp.Core.FSharpOption`1[TResult] Map2[T1,T2,TResult](Microsoft.FSharp.Core.FSharpFunc`2[T1,Microsoft.FSharp.Core.FSharpFunc`2[T2,TResult]], Microsoft.FSharp.Core.FSharpOption`1[T1], Microsoft.FSharp.Core.FSharpOption`1[T2])
Microsoft.FSharp.Core.OptionModule: Microsoft.FSharp.Core.FSharpOption`1[TResult] Map3[T1,T2,T3,TResult](Microsoft.FSharp.Core.FSharpFunc`2[T1,Microsoft.FSharp.Core.FSharpFunc`2[T2,Microsoft.FSharp.Core.FSharpFunc`2[T3,TResult]]], Microsoft.FSharp.Core.FSharpOption`1[T1], Microsoft.FSharp.Core.FSharpOption`1[T2], Microsoft.FSharp.Core.FSharpOption`1[T3])
Microsoft.FSharp.Core.OptionModule: Microsoft.FSharp.Core.FSharpOption`1[TResult] Map[T,TResult](Microsoft.FSharp.Core.FSharpFunc`2[T,TResult], Microsoft.FSharp.Core.FSharpOption`1[T])
Microsoft.FSharp.Core.OptionModule: Microsoft.FSharp.Core.FSharpOption`1[T] Filter[T](Microsoft.FSharp.Core.FSharpFunc`2[T,System.Boolean], Microsoft.FSharp.Core.FSharpOption`1[T])
Microsoft.FSharp.Core.OptionModule: Microsoft.FSharp.Core.FSharpOption`1[T] Flatten[T](Microsoft.FSharp.Core.FSharpOption`1[Microsoft.FSharp.Core.FSharpOption`1[T]])
Microsoft.FSharp.Core.OptionModule: Microsoft.FSharp.Core.FSharpOption`1[T] OfNullable[T](System.Nullable`1[T])
Microsoft.FSharp.Core.OptionModule: Microsoft.FSharp.Core.FSharpOption`1[T] OfObj[T](T)
Microsoft.FSharp.Core.OptionModule: Microsoft.FSharp.Core.FSharpOption`1[T] OrElseWith[T](Microsoft.FSharp.Core.FSharpFunc`2[Microsoft.FSharp.Core.Unit,Microsoft.FSharp.Core.FSharpOption`1[T]], Microsoft.FSharp.Core.FSharpOption`1[T])
Microsoft.FSharp.Core.OptionModule: Microsoft.FSharp.Core.FSharpOption`1[T] OrElse[T](Microsoft.FSharp.Core.FSharpOption`1[T], Microsoft.FSharp.Core.FSharpOption`1[T])
Microsoft.FSharp.Core.OptionModule: System.Nullable`1[T] ToNullable[T](Microsoft.FSharp.Core.FSharpOption`1[T])
Microsoft.FSharp.Core.OptionModule: System.String ToString()
Microsoft.FSharp.Core.OptionModule: System.Type GetType()
Microsoft.FSharp.Core.OptionModule: T DefaultValue[T](T, Microsoft.FSharp.Core.FSharpOption`1[T])
Microsoft.FSharp.Core.OptionModule: T DefaultWith[T](Microsoft.FSharp.Core.FSharpFunc`2[Microsoft.FSharp.Core.Unit,T], Microsoft.FSharp.Core.FSharpOption`1[T])
Microsoft.FSharp.Core.OptionModule: T GetValue[T](Microsoft.FSharp.Core.FSharpOption`1[T])
Microsoft.FSharp.Core.OptionModule: T ToObj[T](Microsoft.FSharp.Core.FSharpOption`1[T])
Microsoft.FSharp.Core.OptionModule: TState FoldBack[T,TState](Microsoft.FSharp.Core.FSharpFunc`2[T,Microsoft.FSharp.Core.FSharpFunc`2[TState,TState]], Microsoft.FSharp.Core.FSharpOption`1[T], TState)
Expand Down
Loading