FsCheckUtils

FsCheckUtils adds FsCheck utilities aimed at C# developers


Keywords
FsCheck, ScalaCheck, QuickCheck, C#
License
MIT
Install
Install-Package FsCheckUtils -Version 1.0.1

Documentation

Build status

Introduction

FsCheckUtils provides the facilities described in the following sections.

NuGet Package

FsCheckUtils is available as a NuGet package:

Convenience extension methods

Config

FsCheck.Config is an immutable class. The following extension methods create a new FsCheck.Config object with an updated value.

  • Config.WithMaxTest
  • Config.WithMaxFail
  • Config.WithReplay
  • Config.WithNoReplay
  • Config.WithName
  • Config.WithStartSize
  • Config.WithEndSize
  • Config.WithEvery
  • Config.WithEveryShrink
  • Config.WithArbitrary
  • Config.WithRunner

In F#, we can do this:

let config = { Config.Default with MaxTest = 1000; Name = "My Config" }

With the above extension methods, we can achieve something pretty similar in C#:

var config = Config.Default
                    .WithMaxTest(1000)
                    .WithName("My Config");

Configuration

FsCheck.Fluent.Configuration is a mutable version of FsCheck.Config from the fluent part of the FsCheck API. SpecBuilder.Check takes a FsCheck.Fluent.Configuration parameter. The following method makes it easy to create an instance of FsCheck.Fluent.Configuration from an instance of FsCheck.Config.

  • Config.ToConfiguration

As mentioned above, FsCheck.Fluent.Configuration is mutable. However, the following extension methods allow multiple properties to be set in a chained style.

  • Configuration.WithMaxTest
  • Configuration.WithMaxFail
  • Configuration.WithName
  • Configuration.WithStartSize
  • Configuration.WithEndSize
  • Configuration.WithEvery
  • Configuration.WithEveryShrink
  • Configuration.WithRunner
var configuration = Config.Default
                            .ToConfiguration()
                            .WithMaxTest(1000)
                            .WithName("My Configuration");

Wrappers around FsCheck operators

FsCheck's Prop operators e.g. .&., are visible to C# as static methods with names that begin with op_ and have symbol names in place of the symbols themselves e.g. op_DotAmpDot. It is possible to call these static methods from C# (as a normal method call - not as an infix operator). However, the names are very odd and ReSharper thinks they are errors (even in ReSharper 9.0 Update 1 - see the screenshot below). For these reasons, FsCheckUtils provides wrappers around these operators.

ReSharper error

  • PropExtensions.And (wraps .&.)
  • PropExtensions.AndAll (a convenience method that applies .&. to a params array of properties)
  • PropExtensions.Or (wraps .|.)
  • PropExtensions.OrAll (a convenience method that applies .|. to a params array of properties)
  • PropExtensions.Label (wraps |@)
  • PropExtensions.Implies (wraps ==>)

Generators and combinators that are in ScalaCheck but not in FsCheck

  • GenExtensions.PickValues (Gen.pick)
  • GenExtensions.PickGenerators (Gen.pick)
  • GenExtensions.SomeOfValues (Gen.someOf)
  • GenExtensions.SomeOfGenerators (Gen.someOf)
  • GenExtensions.RetryUntil (Gen.retryUntil)
  • GenExtensions.NumChar (Gen.numChar)
  • GenExtensions.AlphaUpperChar (Gen.alphaUpperChar)
  • GenExtensions.AlphaLowerChar (Gen.alphaLowerChar)
  • GenExtensions.AlphaChar (Gen.alphaChar)
  • GenExtensions.AlphaNumChar (Gen.alphaNumChar)
  • GenExtensions.AlphaStr (Gen.alphaStr)
  • GenExtensions.NumStr (Gen.numStr)
  • GenExtensions.Identifier (Gen.identifier)
  • GenExtensions.Guid (Gen.uuid)
  • GenExtensions.Zip (Gen.zip)

Documentation

Documentation, built from XML documentation comments using Sandcastle Help File Builder, can be found here:

TODO

  • Ensure all public methods are covered by unit tests (only a couple aren't)
  • Write some GitHub wiki articles about using FsCheck from C#

Using FsCheck from C#: some useful resources