|
1 | | -module CSharpLanguageServer.Tests.CodeActionTests |
| 1 | +namespace CSharpLanguageServer.Tests |
2 | 2 |
|
3 | 3 | open NUnit.Framework |
4 | 4 | open Ionide.LanguageServerProtocol.Types |
5 | | - |
6 | 5 | open CSharpLanguageServer.Tests.Tooling |
7 | 6 |
|
8 | | -[<TestCase>] |
9 | | -let testCodeActionOnMethodNameWorks () = |
10 | | - use client = |
11 | | - setupServerClient defaultClientProfile "TestData/testCodeActionOnMethodNameWorks" |
12 | | - |
13 | | - client.StartAndWaitForSolutionLoad() |
14 | | - |
15 | | - use classFile = client.Open("Project/Class.cs") |
16 | | - |
17 | | - let caParams0: CodeActionParams = |
18 | | - { TextDocument = { Uri = classFile.Uri } |
19 | | - Range = |
20 | | - { Start = { Line = 2u; Character = 16u } |
21 | | - End = { Line = 2u; Character = 16u } } |
22 | | - Context = |
23 | | - { Diagnostics = [||] |
24 | | - Only = None |
25 | | - TriggerKind = None } |
26 | | - WorkDoneToken = None |
27 | | - PartialResultToken = None } |
28 | | - |
29 | | - let caResult0: TextDocumentCodeActionResult option = |
30 | | - client.Request("textDocument/codeAction", caParams0) |
31 | | - |
32 | | - Assert.IsTrue(caResult0.IsSome) |
33 | | - |
34 | | - match caResult0 with |
35 | | - | Some [| U2.C2 x |] -> |
36 | | - Assert.AreEqual("Extract base class...", x.Title) |
37 | | - Assert.AreEqual(None, x.Kind) |
38 | | - Assert.AreEqual(None, x.Diagnostics) |
39 | | - Assert.AreEqual(None, x.Disabled) |
40 | | - Assert.IsTrue(x.Edit.IsSome) |
41 | | - |
42 | | - | _ -> failwith "Some [| U2.C1 x |] was expected" |
| 7 | +[<TestFixture>] |
| 8 | +type CodeActionTests() = |
| 9 | + |
| 10 | + static let mutable client: ClientController = |
| 11 | + setupServerClient defaultClientProfile "TestData/testCodeActions" |
| 12 | + |
| 13 | + [<OneTimeSetUp>] |
| 14 | + member _.Setup() = client.StartAndWaitForSolutionLoad() |
| 15 | + |
| 16 | + [<Test>] |
| 17 | + member _.``code action menu appears on request``() = |
| 18 | + use classFile = client.Open("Project/Class.cs") |
| 19 | + |
| 20 | + let caParams: CodeActionParams = |
| 21 | + { TextDocument = { Uri = classFile.Uri } |
| 22 | + Range = |
| 23 | + { Start = { Line = 1u; Character = 0u } |
| 24 | + End = { Line = 1u; Character = 0u } } |
| 25 | + Context = |
| 26 | + { Diagnostics = [||] |
| 27 | + Only = None |
| 28 | + TriggerKind = None } |
| 29 | + WorkDoneToken = None |
| 30 | + PartialResultToken = None } |
| 31 | + |
| 32 | + let caResult: TextDocumentCodeActionResult option = |
| 33 | + client.Request("textDocument/codeAction", caParams) |
| 34 | + |
| 35 | + let assertCodeActionHasTitle (ca: CodeAction, title: string) = |
| 36 | + Assert.AreEqual(title, ca.Title) |
| 37 | + Assert.AreEqual(None, ca.Kind) |
| 38 | + Assert.AreEqual(None, ca.Diagnostics) |
| 39 | + Assert.AreEqual(None, ca.Disabled) |
| 40 | + Assert.IsTrue(ca.Edit.IsSome) |
| 41 | + |
| 42 | + match caResult with |
| 43 | + | Some [| U2.C2 generateOverrides |
| 44 | + U2.C2 extractInterface |
| 45 | + U2.C2 generateConstructor |
| 46 | + U2.C2 extractBaseClass |
| 47 | + U2.C2 addDebuggerDisplay |] -> |
| 48 | + assertCodeActionHasTitle (generateOverrides, "Generate overrides...") |
| 49 | + assertCodeActionHasTitle (extractInterface, "Extract interface...") |
| 50 | + assertCodeActionHasTitle (generateConstructor, "Generate constructor 'Class()'") |
| 51 | + assertCodeActionHasTitle (extractBaseClass, "Extract base class...") |
| 52 | + assertCodeActionHasTitle (addDebuggerDisplay, "Add 'DebuggerDisplay' attribute") |
| 53 | + |
| 54 | + | _ -> failwith "Not all code actions were matched as expected" |
| 55 | + |
| 56 | + [<Test>] |
| 57 | + member _.``extract base class request extracts base class``() = |
| 58 | + use classFile = client.Open("Project/Class.cs") |
| 59 | + |
| 60 | + let caParams0: CodeActionParams = |
| 61 | + { TextDocument = { Uri = classFile.Uri } |
| 62 | + Range = |
| 63 | + { Start = { Line = 2u; Character = 16u } |
| 64 | + End = { Line = 2u; Character = 16u } } |
| 65 | + Context = |
| 66 | + { Diagnostics = [||] |
| 67 | + Only = None |
| 68 | + TriggerKind = None } |
| 69 | + WorkDoneToken = None |
| 70 | + PartialResultToken = None } |
| 71 | + |
| 72 | + let caResult: TextDocumentCodeActionResult option = |
| 73 | + client.Request("textDocument/codeAction", caParams0) |
| 74 | + |
| 75 | + match caResult with |
| 76 | + | Some [| U2.C2 x |] -> Assert.AreEqual("Extract base class...", x.Title) |
| 77 | + // TODO: match extract base class edit structure |
| 78 | + |
| 79 | + | _ -> failwith "Some [| U2.C2 x |] was expected" |
| 80 | + |
| 81 | + [<Test>] |
| 82 | + member _.``extract interface code action should extract an interface``() = |
| 83 | + use classFile = client.Open("Project/Class.cs") |
| 84 | + |
| 85 | + let caArgs: CodeActionParams = |
| 86 | + { TextDocument = { Uri = classFile.Uri } |
| 87 | + Range = |
| 88 | + { Start = { Line = 0u; Character = 0u } |
| 89 | + End = { Line = 0u; Character = 0u } } |
| 90 | + Context = |
| 91 | + { Diagnostics = [||] |
| 92 | + Only = Some [| "refactor.extract.interface" |] |
| 93 | + TriggerKind = Some CodeActionTriggerKind.Invoked } |
| 94 | + WorkDoneToken = None |
| 95 | + PartialResultToken = None } |
| 96 | + |
| 97 | + let caOptions: TextDocumentCodeActionResult option = |
| 98 | + match client.Request("textDocument/codeAction", caArgs) with |
| 99 | + | Some opts -> opts |
| 100 | + | None -> failwith "Expected code actions" |
| 101 | + |
| 102 | + let codeAction = |
| 103 | + match caOptions |> Option.bind (Array.tryItem 1) with |
| 104 | + | Some(U2.C2 ca) -> |
| 105 | + Assert.AreEqual("Extract interface...", ca.Title) |
| 106 | + ca |
| 107 | + | _ -> failwith "Extract interface action not found" |
| 108 | + |
| 109 | + let expectedImplementInterfaceEdits = |
| 110 | + { Range = |
| 111 | + { Start = { Line = 0u; Character = 11u } |
| 112 | + End = { Line = 0u; Character = 11u } } |
| 113 | + NewText = " : IClass" } |
| 114 | + |
| 115 | + let expectedCreateInterfaceEdits = |
| 116 | + { Range = |
| 117 | + { Start = { Line = 0u; Character = 0u } |
| 118 | + End = { Line = 0u; Character = 0u } } |
| 119 | + NewText = "internal interface IClass\n{\n void Method(string arg);\n}" } |
| 120 | + |
| 121 | + match codeAction.Edit with |
| 122 | + | Some { DocumentChanges = Some [| U4.C1 create; U4.C1 implement |] } -> |
| 123 | + match create.Edits, implement.Edits with |
| 124 | + | [| U2.C1 createEdits |], [| U2.C1 implementEdits |] -> |
| 125 | + Assert.AreEqual(expectedCreateInterfaceEdits, createEdits |> TextEdit.normalizeNewText) |
| 126 | + |
| 127 | + Assert.AreEqual(expectedImplementInterfaceEdits, implementEdits |> TextEdit.normalizeNewText) |
| 128 | + |
| 129 | + | _ -> failwith "Expected exactly one U2.C1 edit in both create/implement" |
| 130 | + |
| 131 | + | _ -> failwith "Unexpected edit structure" |
0 commit comments