2 people like it.

FsCheck example

No nulls FsCheck

 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
11: 
12: 
13: 
14: 
15: 
16: 
17: 
18: 
19: 
20: 
21: 
22: 
23: 
24: 
25: 
26: 
27: 
28: 
29: 
30: 
31: 
32: 
33: 
34: 
35: 
36: 
37: 
38: 
39: 
40: 
41: 
42: 
43: 
44: 
45: 
46: 
47: 
48: 
49: 
50: 
51: 
52: 
53: 
54: 
55: 
56: 
57: 
58: 
59: 
60: 
61: 
62: 
63: 
#r "packages/Aether/lib/net40/Aether.dll"
#r "packages/FParsec/lib/net40-client/FParsecCS.dll"
#r "packages/FParsec/lib/net40-client/FParsec.dll"
#r "packages/NUnit/lib/nunit.framework.dll"
#r "packages/Unquote/lib/net40/Unquote.dll"
#r "packages/FsCheck/lib/net45/FsCheck.dll"
#r "packages/FSharp.Core/lib/net40/FSharp.Core.dll"

#load "paket-files/xyncro/chiron/src/Chiron/Chiron.fs"

// Existing tests
open Chiron
open NUnit.Framework
open Swensen.Unquote

let t1 =
    Object (Map.ofList
        [ "bool", Bool true
          "number", Number 2. ])

[<Test>]
let ``Json.parse returns correct values`` () =
    Json.parse "\"hello\"" =? String "hello"
    Json.parse "\"\"" =? String ""
    Json.parse "\"\\n\"" =? String "\n"
    Json.parse "\"\\u005c\"" =? String "\\"
    Json.parse "\"\"" =? String "푟"

[<Test>]
let ``Json.format returns correct values`` () =
    (* String *)
    Json.format <| String "hello" =? "\"hello\""

    (* Complex type *)
    Json.format t1 =? "{\"bool\":true,\"number\":2}"

    Json.format (String "hello") =? "\"hello\""
    Json.format (String "") =? "\"\""
    Json.format (String "푟") =? "\"\""
    Json.format (String "\t") =? "\"\t\""

``Json.parse returns correct values`` ()
``Json.format returns correct values`` ()

// Let's add our properties!
open FsCheck

let inline roundTrip (thing : 'a) : 'a =
  Json.serialize thing
  |> Json.format
  |> Json.parse
  |> Json.deserialize

type Properties =
  (*
  static member ``Strings can be round tripped`` (str : string) =
    roundTrip str = str
  *)
  static member ``Strings can be round tripped`` (str : StringNoNulls) =
    let s = str.Get
    roundTrip s = s

FsCheck.Check.All<Properties>(FsCheck.Config.Quick)
namespace NUnit
namespace NUnit.Framework
namespace Swensen
namespace Swensen.Unquote
val t1 : obj

Full name: Script.t1
Multiple items
module Map

from Microsoft.FSharp.Collections

--------------------
type Map<'Key,'Value (requires comparison)> =
  interface IEnumerable
  interface IComparable
  interface IEnumerable<KeyValuePair<'Key,'Value>>
  interface ICollection<KeyValuePair<'Key,'Value>>
  interface IDictionary<'Key,'Value>
  new : elements:seq<'Key * 'Value> -> Map<'Key,'Value>
  member Add : key:'Key * value:'Value -> Map<'Key,'Value>
  member ContainsKey : key:'Key -> bool
  override Equals : obj -> bool
  member Remove : key:'Key -> Map<'Key,'Value>
  ...

Full name: Microsoft.FSharp.Collections.Map<_,_>

--------------------
new : elements:seq<'Key * 'Value> -> Map<'Key,'Value>
val ofList : elements:('Key * 'T) list -> Map<'Key,'T> (requires comparison)

Full name: Microsoft.FSharp.Collections.Map.ofList
Multiple items
type TestAttribute =
  inherit Attribute
  new : unit -> TestAttribute
  member Description : string with get, set

Full name: NUnit.Framework.TestAttribute

--------------------
TestAttribute() : unit
val ( Json.parse returns correct values ) : unit -> obj

Full name: Script.( Json.parse returns correct values )
module String

from Microsoft.FSharp.Core
val ( Json.format returns correct values ) : unit -> obj

Full name: Script.( Json.format returns correct values )
namespace FsCheck
val roundTrip : thing:'a -> 'a

Full name: Script.roundTrip
val thing : 'a
type Properties =
  static member ( Strings can be round tripped ) : str:'a -> bool

Full name: Script.Properties
static member Properties.( Strings can be round tripped ) : str:'a -> bool

Full name: Script.Properties.( Strings can be round tripped )
val str : 'a
val s : obj
type Check =
  static member All : config:Config -> unit
  static member All : config:Config * test:Type -> unit
  static member Method : config:Config * methodInfo:MethodInfo * ?target:obj -> unit
  static member One : config:Config * property:'Testable -> unit
  static member One : name:string * config:Config * property:'Testable -> unit
  static member Quick : property:'Testable -> unit
  static member Quick : name:string * property:'Testable -> unit
  static member QuickAll : unit -> unit
  static member QuickAll : test:Type -> unit
  static member QuickThrowOnFailure : property:'Testable -> unit
  ...

Full name: FsCheck.Check
static member FsCheck.Check.All : config:FsCheck.Config -> unit
static member FsCheck.Check.All : config:FsCheck.Config * test:System.Type -> unit
type Config =
  {MaxTest: int;
   MaxFail: int;
   Replay: StdGen option;
   Name: string;
   StartSize: int;
   EndSize: int;
   QuietOnSuccess: bool;
   Every: int -> obj list -> string;
   EveryShrink: obj list -> string;
   Arbitrary: Type list;
   ...}
  static member Default : Config
  static member Quick : Config
  static member QuickThrowOnFailure : Config
  static member Verbose : Config
  static member VerboseThrowOnFailure : Config
  static member private throwingRunner : IRunner

Full name: FsCheck.Config
property FsCheck.Config.Quick: FsCheck.Config
Raw view Test code New version

More information

Link:http://fssnip.net/qO
Posted:9 years ago
Author:mavnn
Tags: fscheck