Skip to content

Instantly share code, notes, and snippets.

@sgoguen
Created October 17, 2017 17:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sgoguen/566aed709a29c6d53e7856cddb8960d4 to your computer and use it in GitHub Desktop.
Save sgoguen/566aed709a29c6d53e7856cddb8960d4 to your computer and use it in GitHub Desktop.
Using FsCheck to Enumerate Invalid Instances
// In this example, we're using FsCheck in an unorthodox way.
// Typically you tell FsCheck to look for something that violates
// your rule. Here, we're reappropriating FsCheck's fuzz testing
// capabilities to enumerate everything that has been deemed
// "valid" to see if it's actually valid to help us figure out the rules.
type Money = decimal
type CouponType = FreeMail | FreeSide
type PaymentOption =
| Cash of Amount:Money
| Coupon of CouponType
| Credit of Amount:Money * CardNumber:string * Expiration:string
| GiftCard of Amount:Money * Balance:Money * CardNumber:Guid
// We'll first assume everything is valid
let isValidPayment(paymentType:PaymentOption) = true
// We pass this function to tell FsCheck everything is valid
// so it keeps throwing data at us. We only print the ones that
// we've deemed valid.
let showValidPayment(paymentType) =
if isValidPayment(paymentType) then
printfn "%A" paymentType
true
FsCheck.Check.Quick(showValidPayment)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment