Skip to content

Commit 27e2012

Browse files
committed
Improve tests
1 parent a3036ba commit 27e2012

File tree

6 files changed

+113
-355
lines changed

6 files changed

+113
-355
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
open System
2+
3+
type Disposable() =
4+
[<DefaultValue>] static val mutable private disposedTimes: int
5+
[<DefaultValue>] static val mutable private constructedTimes: int
6+
7+
do Disposable.constructedTimes <- Disposable.constructedTimes + 1
8+
9+
static member DisposeCallCount() = Disposable.disposedTimes
10+
static member ConstructorCallCount() = Disposable.constructedTimes
11+
12+
interface IDisposable with
13+
member _.Dispose() =
14+
Disposable.disposedTimes <- Disposable.disposedTimes + 1
15+
16+
type DisposableBuilder() =
17+
member _.Using(resource: #IDisposable, f) =
18+
async {
19+
use res = resource
20+
return! f res
21+
}
22+
23+
member _.Bind(disposable: Disposable, f) =
24+
async {
25+
let c = disposable
26+
return! f c
27+
}
28+
29+
member _.Return(x) = async.Return x
30+
31+
member _.ReturnFrom(x) = x
32+
33+
member _.Bind(task, f) = async.Bind(task, f)
34+
35+
let counterDisposable = DisposableBuilder()
36+
37+
let example() =
38+
counterDisposable {
39+
use! res = new Disposable()
40+
use! __ = new Disposable()
41+
use! (res1) = new Disposable()
42+
use! (___) = new Disposable()
43+
use! _ = new Disposable()
44+
return ()
45+
}
46+
47+
example()
48+
|> Async.RunSynchronously
49+
50+
let disposeCalls = Disposable.DisposeCallCount()
51+
if disposeCalls <> 5 then
52+
failwithf $"unexpected dispose call count: %i{disposeCalls}"
53+
let ctorCalls = Disposable.ConstructorCallCount()
54+
if ctorCalls <> 5 then
55+
failwithf $"unexpected constructor call count: %i{ctorCalls}"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// Copyright (c) Microsoft Corporation. All Rights Reserved. See License.txt in the project root for license information.
2+
3+
namespace Language
4+
5+
open FSharp.Test
6+
open Xunit
7+
open FSharp.Test.Compiler
8+
9+
module UseBangBindingsVersion9 =
10+
[<Theory; Directory(__SOURCE_DIRECTORY__, Includes=[|"UseBang01.fs"|])>]
11+
let ``UseBangBindings - UseBang01_fs - Current LangVersion`` compilation =
12+
compilation
13+
|> asFsx
14+
|> withLangVersion90
15+
|> typecheck
16+
|> shouldFail
17+
|> withDiagnostics [
18+
(Error 1228, Line 43, Col 14, Line 43, Col 15, "'use!' bindings must be of the form 'use! <var> = <expr>'")
19+
]
20+
21+
module UseBangBindingsPreview =
22+
[<Theory; Directory(__SOURCE_DIRECTORY__, Includes=[|"UseBang01.fs"|])>]
23+
let ``UseBangBindings - UseBang01_fs - Preview LangVersion`` compilation =
24+
compilation
25+
|> asExe
26+
|> withLangVersionPreview
27+
|> compileAndRun
28+
|> shouldSucceed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
open System
2+
3+
type private Disposable() =
4+
[<DefaultValue>] static val mutable private disposedTimes: int
5+
[<DefaultValue>] static val mutable private constructedTimes: int
6+
7+
do Disposable.constructedTimes <- Disposable.constructedTimes + 1
8+
9+
static member DisposeCallCount() = Disposable.disposedTimes
10+
static member ConstructorCallCount() = Disposable.constructedTimes
11+
12+
interface System.IDisposable with
13+
member _.Dispose() =
14+
Disposable.disposedTimes <- Disposable.disposedTimes + 1
15+
16+
let _scope =
17+
use _ = new Disposable()
18+
()
19+
20+
let disposeCalls = Disposable.DisposeCallCount()
21+
if disposeCalls <> 1 then
22+
failwith "was not disposed or disposed too many times"
23+
24+
let ctorCalls = Disposable.ConstructorCallCount()
25+
if ctorCalls <> 1 then
26+
failwithf "unexpected constructor call count: %i" ctorCalls

tests/FSharp.Compiler.ComponentTests/Conformance/BasicGrammarElements/UseBindings/UseBindings.fs

+3-29
Original file line numberDiff line numberDiff line change
@@ -27,35 +27,9 @@ module UseBindings =
2727
|> withErrorCode 3350
2828
|> withDiagnosticMessageMatches "Feature 'discard pattern in use binding' is not available.*"
2929

30-
[<Fact>]
31-
let ``Dispose called for discarded value of use binding`` () =
32-
Fsx """
33-
type private Disposable() =
34-
[<DefaultValue>] static val mutable private disposedTimes: int
35-
[<DefaultValue>] static val mutable private constructedTimes: int
36-
37-
do Disposable.constructedTimes <- Disposable.constructedTimes + 1
38-
39-
static member DisposeCallCount() = Disposable.disposedTimes
40-
static member ConstructorCallCount() = Disposable.constructedTimes
41-
42-
interface System.IDisposable with
43-
member _.Dispose() =
44-
Disposable.disposedTimes <- Disposable.disposedTimes + 1
45-
46-
let _scope =
47-
use _ = new Disposable()
48-
()
49-
50-
let disposeCalls = Disposable.DisposeCallCount()
51-
if disposeCalls <> 1 then
52-
failwith "was not disposed or disposed too many times"
53-
54-
let ctorCalls = Disposable.ConstructorCallCount()
55-
if ctorCalls <> 1 then
56-
failwithf "unexpected constructor call count: %i" ctorCalls
57-
58-
"""
30+
[<Theory; Directory(__SOURCE_DIRECTORY__, Includes=[|"UseBindingDiscard02.fs"|])>]
31+
let ``Dispose called for discarded value of use binding`` compilation =
32+
compilation
5933
|> asExe
6034
|> withLangVersion60
6135
|> compileAndRun

tests/FSharp.Compiler.ComponentTests/FSharp.Compiler.ComponentTests.fsproj

+1-1
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@
7676
<Compile Include="Conformance\BasicGrammarElements\TypeAbbreviations\WarnForAttributeAlias.fs" />
7777
<Compile Include="Conformance\BasicGrammarElements\ValueRestriction\ValueRestriction.fs" />
7878
<Compile Include="Conformance\BasicGrammarElements\UseBindings\UseBindings.fs" />
79+
<Compile Include="Conformance\BasicGrammarElements\UseBindings\UseBangBindings.fs" />
7980
<Compile Include="Conformance\Constraints\Unmanaged.fs" />
8081
<Compile Include="Conformance\GeneratedEqualityHashingComparison\Attributes\Diags\Attributes_Diags.fs" />
8182
<Compile Include="Conformance\GeneratedEqualityHashingComparison\Attributes\Legacy\Attributes_Legacy.fs" />
@@ -232,7 +233,6 @@
232233
<Compile Include="Language\InterpolatedStringsTests.fs" />
233234
<Compile Include="Language\ComputationExpressionTests.fs" />
234235
<Compile Include="Language\StateMachineTests.fs" />
235-
<Compile Include="Language\UseBangBindingsTests.fs" />
236236
<Compile Include="Language\DynamicAssignmentOperatorTests.fs" />
237237
<Compile Include="Language\CastingTests.fs" />
238238
<Compile Include="Language\NameofTests.fs" />

0 commit comments

Comments
 (0)