Skip to content

Commit

Permalink
Add some tests for binding vm helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
marner2 committed Jun 14, 2022
1 parent 4e7ef03 commit 1bfdd41
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 0 deletions.
101 changes: 101 additions & 0 deletions src/Elmish.WPF.Tests/BindingVmHelpersTests.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
module BindingVmHelpersTests.M

open System
open System.Collections.ObjectModel
open System.Collections.Specialized

open Xunit
open Hedgehog
open Swensen.Unquote

open Elmish.WPF
open Elmish.WPF.BindingVmHelpers



module Initialize =

let checker<'t when 't : equality> () =
Property.check <| property {
let! m1 = GenX.auto<'t>

let binding = BindingData.OneWay.create id

let vmBinding =
Initialize(LoggingViewModelArgs.none, "Nothing", (fun _ -> failwith "not selected item"))
.Recursive(m1, ignore, (fun () -> m1), binding)

let result =
match vmBinding with
| Some (BaseVmBinding (OneWay b)) -> b
| b -> failwithf "Expected a OneWay binding, instead found %O" b

test <@ result.OneWayData.Get m1 = m1 @>
}

[<Fact>]
let ``should initialize successfully with various types`` () =
checker<float> ()
checker<int32> ()
checker<string> ()
checker<Guid> ()
checker<obj> ()

module Get =

let checker<'t when 't : equality> () =
Property.check <| property {
let! m1 = GenX.auto<'t>

let binding = BindingData.OneWay.create id

let vmBinding =
Initialize(LoggingViewModelArgs.none, "Nothing", (fun _ -> failwith "not selected item"))
.Recursive(m1, ignore, (fun () -> m1), binding)

let getResult = vmBinding |> Option.map (fun b -> Get("Nothing").Recursive(m1, b))

let get =
match getResult with
| Some (Ok x) -> x
| x -> failwithf "Expected a success, instead got %O" x

test <@ get = m1 @>
}

[<Fact>]
let ``should get successfully with various types`` () =
checker<float> ()
checker<int32> ()
checker<string> ()
checker<Guid> ()
checker<obj> ()

module Set =

let checker<'t when 't : equality> () =
Property.check <| property {
let! m1 = GenX.auto<'t>
let! m2 = GenX.auto<'t> |> GenX.notEqualTo m1

let model = ref m1

let binding = BindingData.TwoWay.create id (fun m _ -> m)

let vmBinding =
Initialize(LoggingViewModelArgs.none, "Nothing", (fun _ -> failwith "not selected item"))
.Recursive(m1, (fun m -> model.Value <- m), (fun () -> model.Value), binding)

let _setResult = vmBinding |> Option.map (fun b -> Set(m2).Recursive(model.Value, b))

test <@ model.Value = m2 @>
test <@ model.Value <> m1 @>
}

[<Fact>]
let ``should set successfully with various types`` () =
checker<float> ()
checker<int32> ()
checker<string> ()
checker<Guid> ()
checker<obj> ()
1 change: 1 addition & 0 deletions src/Elmish.WPF.Tests/Elmish.WPF.Tests.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
<ItemGroup>
<Compile Include="AutoOpen.fs" />
<Compile Include="MergeTests.fs" />
<Compile Include="BindingVmHelpersTests.fs" />
<Compile Include="DynamicViewModelTests.fs" />
<Compile Include="BindingTests.fs" />
<Compile Include="UtilsTests.fs" />
Expand Down

0 comments on commit 1bfdd41

Please sign in to comment.