Skip to content

Instantly share code, notes, and snippets.

@kjnilsson
Last active August 29, 2015 14:09
Show Gist options
  • Save kjnilsson/8c8e4c86fc5ea316c3e0 to your computer and use it in GitHub Desktop.
Save kjnilsson/8c8e4c86fc5ea316c3e0 to your computer and use it in GitHub Desktop.
FsCheck problem/query
#r "./bin/Debug/FsCheck.dll"
open System
open FsCheck
//ensure fscheck is fully initialised.
Runner.init.Force()
let whiteList =
let x = ['a' .. 'z'] @ ['A' .. 'Z']
Gen.choose (0, x.Length - 1)
|> Gen.map (fun i -> x.[i])
type SafeString = SafeString of string with
member x.Get = match x with SafeString r -> r
override x.ToString() = x.Get
type UriGenerator =
static member SafeString() =
Gen.map (fun (chars:char[]) -> String(chars)) (Gen.arrayOf whiteList)
|> Arb.fromGen
|> Arb.filter (fun s -> not (String.IsNullOrEmpty s))
|> Arb.convert SafeString string
Arb.register<UriGenerator>
// works as expected
Gen.sample 10 10 (UriGenerator.SafeString().Generator)
// returns SafeStrings but not using the defined 'safe' generator above
Gen.sample 10 10 (Arb.generate<SafeString>)
@kjnilsson
Copy link
Author

When using Arb.generate the strings returned aren't "safe" and appear to use the default string generator.

@kjnilsson
Copy link
Author

Bah I found it needed to be Arb.register()

As you were. :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment