Skip to content

Commit

Permalink
Merge pull request #264 from krymtkts/feature/fscheck
Browse files Browse the repository at this point in the history
Replace tests for `unwrap` with property-based tests.
  • Loading branch information
krymtkts authored Nov 30, 2024
2 parents bcd8bd9 + 1fdfcc9 commit 9cb71c1
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 8 deletions.
80 changes: 72 additions & 8 deletions src/pocof.Test/Data.fs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,13 @@ open Microsoft.FSharp.Reflection

open Xunit
open FsUnitTyped
open FsCheck.FSharp
open FsCheck.Xunit

open Pocof
open Pocof.Data


module LanguageExtension =
module Option =
type Mock() =
Expand All @@ -33,17 +36,78 @@ module unwrap =
open System.Collections
open System.Management.Automation

[<Fact>]
let ``should return "a".`` () =
unwrap [ Entry.Obj(PSObject.AsPSObject "a") ]
let psObjectGen =
ArbMap.defaults
|> ArbMap.generate<string>
|> Gen.map (PSObject.AsPSObject >> Entry.Obj)

let dictionaryEntryGen =
ArbMap.defaults
|> ArbMap.generate<string>
|> Gen.two
|> Gen.map (DictionaryEntry >> Entry.Dict)

type EntryPSObject =
static member Double() = psObjectGen |> Arb.fromGen

[<Property(Arbitrary = [| typeof<EntryPSObject> |], EndSize = 1000)>]
let ``should return PSObject sequence.`` (data: Entry list) =
data
|> unwrap
|> List.ofSeq
|> shouldEqual [ PSObject.AsPSObject "a" ]
|> shouldEqual (
data
|> List.map (function
| Entry.Obj x -> x
| _ -> failwith "Dict is unreachable")
)
|> Prop.collect (List.length data)

[<Fact>]
let ``should return dictionary.`` () =
unwrap [ Entry.Dict(DictionaryEntry("Jane", "Doe")) ]
type EntryDictionaryEntry =
static member Double() = dictionaryEntryGen |> Arb.fromGen

[<Property(Arbitrary = [| typeof<EntryDictionaryEntry> |], EndSize = 1000)>]
let ``should return DictionaryEntry sequence.`` (data: Entry list) =
data
|> unwrap
|> List.ofSeq
|> shouldEqual [ DictionaryEntry("Jane", "Doe") ]
|> shouldEqual (
data
|> List.map (function
| Entry.Dict x -> x
| _ -> failwith "Obj is unreachable")
)
|> Prop.collect (List.length data)

type MixedEntry =
static member Generate() =
Gen.oneof [ psObjectGen; dictionaryEntryGen ] |> Gen.listOf |> Arb.fromGen

[<Property(Arbitrary = [| typeof<MixedEntry> |], EndSize = 1000)>]
let ``should return mixed sequence.`` (data: Entry list) =
data
|> unwrap
|> List.ofSeq
|> shouldEqual (
data
|> List.map (function
| Entry.Obj x -> x
| Entry.Dict x -> x)
)
|> Prop.collect (
List.length data,
// TODO: use .Is* after bumping to F# 9.
data
|> List.filter (function
| Entry.Obj _ -> true
| _ -> false)
|> List.length,
data
|> List.filter (function
| Entry.Dict _ -> true
| _ -> false)
|> List.length
)

module ``Action fromString`` =
[<Fact>]
Expand Down
2 changes: 2 additions & 0 deletions src/pocof.Test/pocof.Test.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="FsCheck" Version="3.0.0-rc3" />
<PackageReference Include="FsCheck.Xunit" Version="3.0.0-rc3" />
<PackageReference Include="FsUnit.xUnit" Version="6.0.1" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.11.1" />
<PackageReference Include="xunit" Version="2.9.2" />
Expand Down

0 comments on commit 9cb71c1

Please sign in to comment.