diff --git a/global.json b/global.json index 1d43b356..17fb2005 100644 --- a/global.json +++ b/global.json @@ -1,6 +1,6 @@ { - "sdk": { + "sdk": { "version": "3.1.101", "rollForward": "latestMajor" - } + } } diff --git a/src/FsCheck.Xunit/PropertyAttribute.fs b/src/FsCheck.Xunit/PropertyAttribute.fs index 67697e20..f13ed3e0 100644 --- a/src/FsCheck.Xunit/PropertyAttribute.fs +++ b/src/FsCheck.Xunit/PropertyAttribute.fs @@ -184,8 +184,16 @@ type PropertyTestCase(diagnosticMessageSink:IMessageSink, defaultMethodDisplay:T let summary = new RunSummary(Total = 1); let outputHelper = new TestOutputHelper() outputHelper.Initialize(messageBus, test) - let testExec() = - + + let dispose testClass = + match testClass with + | None -> () + | Some obj -> + match box obj with + | :? IDisposable as d -> d.Dispose() + | _ -> () + + let testExec() = let config = this.Init(outputHelper) let timer = ExecutionTimer() let result = @@ -203,6 +211,8 @@ type PropertyTestCase(diagnosticMessageSink:IMessageSink, defaultMethodDisplay:T timer.Aggregate(fun () -> Check.Method(config, runMethod, ?target=target)) + dispose target + match xunitRunner.Result with | TestResult.True _ -> let output = Runner.onFinishedToString "" xunitRunner.Result diff --git a/tests/FsCheck.Test/Runner.fs b/tests/FsCheck.Test/Runner.fs index f0b63a9e..fa3f8ff4 100644 --- a/tests/FsCheck.Test/Runner.fs +++ b/tests/FsCheck.Test/Runner.fs @@ -285,4 +285,43 @@ module Deprecated = [ |] )>] let ``should use Arb instance on method preferentially``(underTest:float) = - underTest >= 0.0 \ No newline at end of file + underTest >= 0.0 + +// see https://github.com/fscheck/FsCheck/issues/514 +// Dispose not called +module BugReproIssue514 = + open System + open System.Threading + open FsCheck + open FsCheck.Xunit + open global.Xunit + open Xunit.Sdk + + type TestMessageBus() = + interface IMessageBus with + member _.QueueMessage _ = true + member _.Dispose() = () + + let mutable disposed = false + + type DisposableTestClass() = + + [] + member _.FakeTest (x:int) = + Check.One(Config.Quick, true) + + interface IDisposable with + member _.Dispose() = + disposed <- true + + [] + let ``should call Dispose on classes inheriting from IDisposable`` () = + let methodInfo = typeof.GetMethod("FakeTest") |> ReflectionMethodInfo + let typeInfo = typeof |> ReflectionTypeInfo + let assemblyInfo = typeof.Assembly |> ReflectionAssemblyInfo |> TestAssembly + let testCollection = TestCollection(assemblyInfo, typeInfo, typeof.Name) + let testClass = TestClass(testCollection, typeInfo) + let testMethod = TestMethod(testClass, methodInfo) + let testCase = new PropertyTestCase(null, TestMethodDisplay.ClassAndMethod, testMethod) + testCase.RunAsync(null, new TestMessageBus(), [||], new ExceptionAggregator(), new CancellationTokenSource()) |> Async.AwaitTask |> ignore + Check.One(Config.Quick, disposed) \ No newline at end of file