Skip to content

Commit

Permalink
Merge from master.
Browse files Browse the repository at this point in the history
  • Loading branch information
kurtschelfthout committed Jan 8, 2022
2 parents 4406d82 + 5cbd257 commit 7a2919e
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 3 deletions.
8 changes: 8 additions & 0 deletions FsCheck Release Notes.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

* Add a few missing `Prop.ForAll` overloads.

* Includes changes in 2.16.4.


### 3.0.0-beta1 - 5 September 2021

Expand Down Expand Up @@ -67,6 +69,12 @@

* Simplify sampling data: Gen.sample et al.

### 2.16.4 - 8 January 2022

* Fixed display issue affecting custom F# exceptions.

* `Prop.throws` now fails the test if an unexpected exception is thrown.

### 2.16.3 - 4 September 2021

* Allow configuration in FsCheck.Xunit.PropertiesAttribute to affect properties on nested types or modules. PropertiesAttribute on the closest enclosing type takes precedence.
Expand Down
2 changes: 1 addition & 1 deletion docs/QuickStart.fsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ with existing test frameworks like NUnit, xUnit.NET or MsTest.
First install FsCheck, open an fsx file and start with:*)
#r "FsCheck"
#r "nuget:FsCheck"
open FsCheck
Expand Down
10 changes: 9 additions & 1 deletion src/FsCheck/FSharp.Prop.fs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,15 @@ module Prop =
///and a failure otherwise.
[<CompiledName("Throws")>]
let throws<'Exception, 'Testable when 'Exception :> exn> (p : Lazy<'Testable>) =
property <| try ignore p.Value; Res.failedFalse with :? 'Exception -> Res.passed
try
ignore p.Value
Res.failedException (exn "Expected exception, none was thrown")
with
| :? 'Exception ->
Res.passed
| e ->
Res.failedException e
|> property

let private stamp str =
let add res =
Expand Down
2 changes: 1 addition & 1 deletion src/FsCheck/Runner.fs
Original file line number Diff line number Diff line change
Expand Up @@ -522,7 +522,7 @@ module Runner =
name data.NumberOfTests (pluralize data.NumberOfTests) (data.Stamps |> stampsToString)
| TestResult.Failed (data, originalArgs, args, Outcome.Failed exc, originalSeed, lastSeed, lastSize) ->
onFailureToString name data originalArgs args originalSeed lastSeed lastSize
+ sprintf "with exception:%s%O%s" newline exc newline
+ sprintf "with exception:%s%A%s" newline exc newline
| TestResult.Failed (data, originalArgs, args, _, originalSeed, lastSeed, lastSize) ->
onFailureToString name data originalArgs args originalSeed lastSeed lastSize
| TestResult.Exhausted data ->
Expand Down
22 changes: 22 additions & 0 deletions tests/FsCheck.Test/Property.fs
Original file line number Diff line number Diff line change
Expand Up @@ -142,10 +142,17 @@ module Property =
override __.OnFinished(_,testResult) =
result <- Some testResult

let private checkResult (prop:Property) =
let resultRunner = GetResultRunner()
let config = Config.Quick.WithRunner(resultRunner).WithMaxTest(2)
Check.One(config, prop)
resultRunner.Result

[<Property>]
let DSL() =
Prop.forAll (Arb.fromGen symPropGen) (fun symprop ->
let expected = determineResult symprop
let actual = checkResult (toProperty symprop)
let resultRunner = GetResultRunner()
let config = Config.Quick.WithRunner(resultRunner).WithMaxTest(2)
Check.One(config,toProperty symprop)
Expand Down Expand Up @@ -196,3 +203,18 @@ module Property =
| TestResult.Passed _ -> false
| TestResult.Failed _ -> true
| TestResult.Exhausted _ -> false @>

[<Fact>]
let ``throws should fail on unexpected exception``() =
let test() =
(lazy invalidOp "boom")
|> Prop.throws<ArgumentException, _>
|> Prop.label "Expected ArgumentException"
let actual = checkResult (Prop.ofTestable test)
match actual with
| TestResult.Failed (td,_,_,Outcome.Failed e,_,_,_) when (e :? InvalidOperationException) ->
if not (td.Labels.Contains("Expected ArgumentException")) then
failwith "Expected label to be applied"
| t -> failwithf "Expected failing test with exception, got %A" t


0 comments on commit 7a2919e

Please sign in to comment.