Guest User

Untitled

a guest
Oct 16th, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.46 KB | None | 0 0
  1. module Persimmon.FsCheck
  2.  
  3. open Persimmon
  4. open FsCheck
  5. open System.Diagnostics
  6.  
  7. exception private FsCheckFailException of string
  8.  
  9. let private runner =
  10. { new IRunner with
  11. member __.OnStartFixture(_) = ()
  12. member __.OnArguments(_, _, _) = ()
  13. member __.OnShrink(_, _) = ()
  14. member __.OnFinished(name, result) =
  15. match result with
  16. | TestResult.True _ -> ()
  17. | _ -> raise (FsCheckFailException(Runner.onFinishedToString name result))
  18. }
  19.  
  20. type PropertyBuilder(name: string option) =
  21. new() = PropertyBuilder(None)
  22. new(name: string) = PropertyBuilder(Some name)
  23.  
  24. member __.Yield(()) =
  25. let config = { Config.Default with Runner = runner }
  26. let runCheck (_: Config) = []
  27. config, runCheck
  28.  
  29. [<CustomOperation("maxTest")>]
  30. member __.MaxTest((config: Config, f), maxTest) = { config with MaxTest = maxTest }, f
  31.  
  32. [<CustomOperation("maxFail")>]
  33. member __.MaxFail((config: Config, f), maxFail) = { config with MaxFail = maxFail }, f
  34.  
  35. [<CustomOperation("replay")>]
  36. member __.Replay((config: Config, f), replay) = { config with Replay = replay }, f
  37.  
  38. [<CustomOperation("startSize")>]
  39. member __.StartSize((config: Config, f), startSize) = { config with StartSize = startSize }, f
  40.  
  41. [<CustomOperation("endSize")>]
  42. member __.EndSize((config: Config, f), endSize) = { config with EndSize = endSize }, f
  43.  
  44. [<CustomOperation("quietOnSuccess")>]
  45. member __.QuietOnSuccess((config: Config, f), quietOnSuccess) = { config with QuietOnSuccess = quietOnSuccess }, f
  46.  
  47. [<CustomOperation("every")>]
  48. member __.Every((config: Config, f), every) = { config with Every = every }, f
  49.  
  50. [<CustomOperation("everyShrink")>]
  51. member __.EveryShrink((config: Config, f), everyShrink) = { config with EveryShrink = everyShrink }, f
  52.  
  53. [<CustomOperation("arbitrary")>]
  54. member __.Arbitrary((config: Config, f), arbitrary) = { config with Arbitrary = arbitrary }, f
  55.  
  56. [<CustomOperation("apply")>]
  57. member __.Apply((config: Config, runPrevChecks: Config -> AssertionResult<unit> list), testable: 'testable) =
  58. let run config =
  59. let prevResults = runPrevChecks config
  60. let result =
  61. try
  62. Check.One(config, testable)
  63. Passed()
  64. with
  65. | FsCheckFailException e -> NotPassed (None, NotPassedCause.Violated e)
  66. | _ -> reraise()
  67. result:: prevResults
  68. config, run
  69.  
  70. member __.Delay(f: unit -> _) = f
  71.  
  72. member __.Run(f: unit -> Config * (Config -> AssertionResult<unit> list)) : TestCase<unit> =
  73. let body (tc: TestCase) = async {
  74. let watch = Stopwatch.StartNew()
  75. try
  76. let config, runCheck = f()
  77. let config = { config with Name = Option.defaultValue config.Name tc.Name }
  78. let results =
  79. match runCheck config with
  80. | [] -> NonEmptyList.singleton (Passed ())
  81. | xs -> NonEmptyList.ofSeq (List.rev xs)
  82. do watch.Stop()
  83. return Done(tc, results, watch.Elapsed)
  84. with
  85. | e ->
  86. do watch.Stop()
  87. return Error (tc, [| ExceptionWrapper(e) |], [], watch.Elapsed)
  88. }
  89. TestCase.init name [] [] body
  90.  
  91. [<AutoOpen>]
  92. module Syntax =
  93.  
  94. let property (name: string) = PropertyBuilder(name)
  95.  
  96. module UseTestNameByReflection =
  97. let property = PropertyBuilder()
  98.  
  99. module private Test =
  100. open Syntax.UseTestNameByReflection
  101.  
  102. let t: TestCase<unit> = property {
  103. apply (Prop.forAll Arb.from<int> (fun _ -> false))
  104. apply (Prop.forAll Arb.from<string> (fun _ -> true))
  105. apply (Prop.forAll Arb.from<int list> (fun _ -> false))
  106. }
Add Comment
Please, Sign In to add comment