Skip to content

Commit d45ca33

Browse files
* refactor(FSharp.Editor.Tests): remove async tasks from test cases and simplify code
* feat(RefactorTestFramework.fs): add GetTaskResult function to handle task results * fix(RemoveExplicitReturnType.fs, AddExplicitReturnType.fs): refactor test cases to use GetTaskResult function * feat(RefactorTestFramework.fs): update tryRefactor and tryGetRefactoringActions to return task result directly * fix(FSharp.Editor.Tests.fsproj): remove AsyncBugReproduction.fs from project * feat(FSharp.Editor.Tests): delete AsyncBugReproduction.fs test file
1 parent a2cf26b commit d45ca33

File tree

5 files changed

+151
-252
lines changed

5 files changed

+151
-252
lines changed

vsintegration/tests/FSharp.Editor.Tests/FSharp.Editor.Tests.fsproj

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,6 @@
7373
<Compile Include="Refactors\RefactorTestFramework.fs" />
7474
<Compile Include="Refactors\AddExplicitReturnType.fs" />
7575
<Compile Include="Refactors\RemoveExplicitReturnType.fs" />
76-
<Compile Include="Refactors\AsyncBugReproduction.fs" />
7776
<Compile Include="Hints\HintTestFramework.fs" />
7877
<Compile Include="Hints\OptionParserTests.fs" />
7978
<Compile Include="Hints\InlineParameterNameHintTests.fs" />

vsintegration/tests/FSharp.Editor.Tests/Refactors/AddExplicitReturnType.fs

Lines changed: 67 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -36,145 +36,127 @@ open System.Runtime.InteropServices
3636
[<InlineData(" : int")>]
3737
[<InlineData(" : int")>]
3838
let ``Refactor should not trigger`` (shouldNotTrigger: string) =
39-
task {
40-
let symbolName = "sum"
39+
let symbolName = "sum"
4140

42-
let code =
43-
$"""
41+
let code =
42+
$"""
4443
let sum a b {shouldNotTrigger}= a + b
4544
"""
4645

47-
use context = TestContext.CreateWithCode code
46+
use context = TestContext.CreateWithCode code
4847

49-
let spanStart = code.IndexOf symbolName
48+
let spanStart = code.IndexOf symbolName
5049

51-
let! actions = tryGetRefactoringActions code spanStart context (new AddExplicitReturnType())
50+
let actions =
51+
tryGetRefactoringActions code spanStart context (new AddExplicitReturnType())
5252

53-
do Assert.Empty(actions)
54-
}
53+
do Assert.Empty(actions)
5554

5655
[<Fact>]
5756
let ``Correctly infer int as explicit return type`` () =
58-
task {
57+
let symbolName = "sum"
5958

60-
let symbolName = "sum"
59+
let code =
60+
"""
61+
let sum a b = a + b
62+
"""
6163

62-
let code =
63-
"""
64-
let sum a b = a + b
65-
"""
64+
use context = TestContext.CreateWithCode code
6665

67-
use context = TestContext.CreateWithCode code
66+
let spanStart = code.IndexOf symbolName
6867

69-
let spanStart = code.IndexOf symbolName
68+
let newDoc = tryRefactor code spanStart context (new AddExplicitReturnType())
7069

71-
let! newDoc = tryRefactor code spanStart context (new AddExplicitReturnType())
72-
73-
do! AssertHasSpecificExplicitReturnType symbolName "int" newDoc context.CT
74-
}
70+
AssertHasSpecificExplicitReturnType symbolName "int" newDoc context.CT
7571

7672
[<Theory>]
7773
[<InlineData("(a:float) (b:int)", "float")>]
7874
[<InlineData("a:int b:int", "int")>]
7975
let ``Infer explicit return type`` (functionHeader: string) (returnType: string) =
80-
task {
76+
let symbolName = "sum"
8177

82-
let symbolName = "sum"
78+
let code =
79+
$"""
80+
let sum {functionHeader}= a + b
81+
"""
8382

84-
let code =
85-
$"""
86-
let sum {functionHeader}= a + b
87-
"""
83+
use context = TestContext.CreateWithCode code
8884

89-
use context = TestContext.CreateWithCode code
85+
let spanStart = code.IndexOf(symbolName)
9086

91-
let spanStart = code.IndexOf(symbolName)
87+
let newDoc = tryRefactor code spanStart context (new AddExplicitReturnType())
88+
let text = newDoc.GetTextAsync() |> GetTaskResult
9289

93-
let! newDoc = tryRefactor code spanStart context (new AddExplicitReturnType())
94-
95-
do! AssertHasSpecificExplicitReturnType symbolName returnType newDoc context.CT
96-
}
90+
AssertHasSpecificExplicitReturnType symbolName returnType newDoc context.CT
9791

9892
[<Fact>]
9993
let ``Infer on rec method`` () =
100-
task {
101-
102-
let symbolName = "fib"
94+
let symbolName = "fib"
10395

104-
let code =
105-
$"""
106-
let rec fib n =
107-
if n < 2 then 1 else fib (n - 1) + fib (n - 2)
108-
"""
96+
let code =
97+
$"""
98+
let rec fib n =
99+
if n < 2 then 1 else fib (n - 1) + fib (n - 2)
100+
"""
109101

110-
use context = TestContext.CreateWithCode code
102+
use context = TestContext.CreateWithCode code
111103

112-
let spanStart = code.IndexOf symbolName
104+
let spanStart = code.IndexOf symbolName
113105

114-
let! newDoc = tryRefactor code spanStart context (new AddExplicitReturnType())
106+
let newDoc = tryRefactor code spanStart context (new AddExplicitReturnType())
115107

116-
do! AssertHasSpecificExplicitReturnType symbolName "int" newDoc context.CT
117-
}
108+
AssertHasSpecificExplicitReturnType symbolName "int" newDoc context.CT
118109

119110
[<Fact>]
120111
let ``Infer with function parameter method`` () =
121-
task {
112+
let symbolName = "apply1"
122113

123-
let symbolName = "apply1"
114+
let code =
115+
$"""
116+
let apply1 (transform: int -> int) y = transform y
117+
"""
124118

125-
let code =
126-
$"""
127-
let apply1 (transform: int -> int) y = transform y
128-
"""
129-
130-
use context = TestContext.CreateWithCode code
119+
use context = TestContext.CreateWithCode code
131120

132-
let spanStart = code.IndexOf symbolName
121+
let spanStart = code.IndexOf symbolName
133122

134-
let! newDoc = tryRefactor code spanStart context (new AddExplicitReturnType())
123+
let newDoc = tryRefactor code spanStart context (new AddExplicitReturnType())
135124

136-
do! AssertHasSpecificExplicitReturnType symbolName "int" newDoc context.CT
137-
}
125+
AssertHasSpecificExplicitReturnType symbolName "int" newDoc context.CT
138126

139127
[<Fact>]
140128
let ``Infer on member function`` () =
141-
task {
129+
let symbolName = "SomeMethod"
142130

143-
let symbolName = "SomeMethod"
144-
145-
let code =
146-
$"""
147-
type SomeType(factor0: int) =
148-
let factor = factor0
149-
member this.SomeMethod(a, b, c) = (a + b + c) * factor
150-
"""
131+
let code =
132+
$"""
133+
type SomeType(factor0: int) =
134+
let factor = factor0
135+
member this.SomeMethod(a, b, c) = (a + b + c) * factor
136+
"""
151137

152-
use context = TestContext.CreateWithCode code
138+
use context = TestContext.CreateWithCode code
153139

154-
let spanStart = code.IndexOf symbolName
140+
let spanStart = code.IndexOf symbolName
155141

156-
let! newDoc = tryRefactor code spanStart context (new AddExplicitReturnType())
142+
let newDoc = tryRefactor code spanStart context (new AddExplicitReturnType())
157143

158-
do! AssertHasSpecificExplicitReturnType symbolName "int" newDoc context.CT
159-
}
144+
AssertHasSpecificExplicitReturnType symbolName "int" newDoc context.CT
160145

161146
[<Fact>]
162147
let ``Correctly infer custom type that is declared earlier in file`` () =
163-
task {
164-
let symbolName = "sum"
165-
166-
let code =
167-
"""
168-
type MyType = { Value: int }
169-
let sum a b = {Value=a+b}
170-
"""
148+
let symbolName = "sum"
171149

172-
use context = TestContext.CreateWithCode code
150+
let code =
151+
"""
152+
type MyType = { Value: int }
153+
let sum a b = {Value=a+b}
154+
"""
173155

174-
let spanStart = code.IndexOf symbolName
156+
use context = TestContext.CreateWithCode code
175157

176-
let! newDoc = tryRefactor code spanStart context (new AddExplicitReturnType())
158+
let spanStart = code.IndexOf symbolName
177159

178-
do! AssertHasSpecificExplicitReturnType symbolName "MyType" newDoc context.CT
160+
let newDoc = tryRefactor code spanStart context (new AddExplicitReturnType())
179161

180-
}
162+
AssertHasSpecificExplicitReturnType symbolName "MyType" newDoc context.CT

vsintegration/tests/FSharp.Editor.Tests/Refactors/AsyncBugReproduction.fs

Lines changed: 0 additions & 70 deletions
This file was deleted.

vsintegration/tests/FSharp.Editor.Tests/Refactors/RefactorTestFramework.fs

Lines changed: 20 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,11 @@ open FSharp.Compiler.Symbols
2525
open FSharp.Compiler.Text
2626
open NUnit.Framework
2727

28+
let GetTaskResult (task: Tasks.Task<'T>) = task.GetAwaiter().GetResult()
29+
2830
let GetSymbol (symbolName: string) (document: Document) ct =
2931
task {
30-
let! (_, checkFileResults) =
31-
document.GetFSharpParseAndCheckResultsAsync symbolName
32-
|> CancellableTask.start ct
32+
let! (_, checkFileResults) = document.GetFSharpParseAndCheckResultsAsync "" |> CancellableTask.start ct
3333

3434
let symbols = checkFileResults.GetAllUsesOfAllSymbolsInFile ct
3535
let symbolUse = symbols |> Seq.find (fun s -> s.Symbol.DisplayName = symbolName)
@@ -42,20 +42,17 @@ let GetSymbol (symbolName: string) (document: Document) ct =
4242
}
4343

4444
let GetReturnTypeOfSymbol (symbolName: string) (document: Document) ct =
45-
task {
46-
let! (_, checkFileResults) =
47-
document.GetFSharpParseAndCheckResultsAsync symbolName
48-
|> CancellableTask.start ct
45+
let (_, checkFileResults) =
46+
document.GetFSharpParseAndCheckResultsAsync ""
47+
|> CancellableTask.start ct
48+
|> GetTaskResult
4949

50-
let symbols = checkFileResults.GetAllUsesOfAllSymbolsInFile ct |> List.ofSeq
51-
let symbolUse = symbols |> Seq.find (fun s -> s.Symbol.DisplayName = symbolName)
50+
let symbols = checkFileResults.GetAllUsesOfAllSymbolsInFile ct |> List.ofSeq
51+
let symbolUse = symbols |> Seq.find (fun s -> s.Symbol.DisplayName = symbolName)
5252

53-
return
54-
match symbolUse.Symbol with
55-
| :? FSharpMemberOrFunctionOrValue as v -> Some(v.ReturnParameter.Type.TypeDefinition.CompiledName)
56-
| _ -> None
57-
58-
}
53+
match symbolUse.Symbol with
54+
| :? FSharpMemberOrFunctionOrValue as v -> Some(v.ReturnParameter.Type.TypeDefinition.CompiledName)
55+
| _ -> None
5956

6057
let TryGetRangeOfExplicitReturnType (symbolName: string) (document: Document) ct =
6158
task {
@@ -82,15 +79,14 @@ let AssertHasAnyExplicitReturnType (symbolName: string) (document: Document) ct
8279
}
8380

8481
let AssertHasSpecificExplicitReturnType (symbolName: string) (expectedTypeName: string) (document: Document) ct =
85-
task {
86-
let! returnType = GetReturnTypeOfSymbol symbolName document ct
8782

88-
match returnType with
89-
| Some t -> Assert.AreEqual(expectedTypeName, t)
90-
| None -> Assert.Fail($"Unexpected type. Expected {expectedTypeName} but was t")
83+
let returnType = GetReturnTypeOfSymbol symbolName document ct
9184

92-
()
93-
}
85+
match returnType with
86+
| Some t -> Assert.AreEqual(expectedTypeName, t)
87+
| None -> Assert.Fail($"Unexpected type. Expected {expectedTypeName} but was t")
88+
89+
()
9490

9591
let AssertHasNoExplicitReturnType (symbolName: string) (document: Document) ct =
9692
task {
@@ -154,6 +150,7 @@ let tryRefactor (code: string) (cursorPosition) (context: TestContext) (refactor
154150
return newDocument
155151
}
156152
|> CancellableTask.startWithoutCancellation
153+
|> fun task -> task.Result
157154

158155
let tryGetRefactoringActions (code: string) (cursorPosition) (context: TestContext) (refactorProvider: 'T :> CodeRefactoringProvider) =
159156
cancellableTask {
@@ -174,3 +171,4 @@ let tryGetRefactoringActions (code: string) (cursorPosition) (context: TestConte
174171
return refactoringActions
175172
}
176173
|> CancellableTask.startWithoutCancellation
174+
|> fun task -> task.Result

0 commit comments

Comments
 (0)