This repository has been archived by the owner on Apr 29, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #34 from dafny-lang/improvement/signature-integrat…
…ion-tests Added integration tests for the signature help.
- Loading branch information
Showing
1 changed file
with
226 additions
and
0 deletions.
There are no files selected for viewing
226 changes: 226 additions & 0 deletions
226
Source/DafnyLS.IntegrationTest/Lookup/SignatureHelpTest.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,226 @@ | ||
using Microsoft.Dafny.LanguageServer.IntegrationTest.Extensions; | ||
using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
using OmniSharp.Extensions.LanguageServer.Protocol.Client; | ||
using OmniSharp.Extensions.LanguageServer.Protocol.Document; | ||
using OmniSharp.Extensions.LanguageServer.Protocol.Models; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
|
||
namespace Microsoft.Dafny.LanguageServer.IntegrationTest.Lookup { | ||
[TestClass] | ||
public class SignatureHelpTest : DafnyLanguageServerTestBase { | ||
// At this time, the main logic happens within ISymbolGuesser since we have to primarily work | ||
// with migrated symbol tables. Therefore, we apply modifications prior requesting signature help | ||
// just like a user would do. | ||
private ILanguageClient _client; | ||
|
||
private void ApplyChanges(TextDocumentItem documentItem, params TextDocumentContentChangeEvent[] changes) { | ||
_client.DidChangeTextDocument(new DidChangeTextDocumentParams { | ||
TextDocument = new VersionedTextDocumentIdentifier { | ||
Uri = documentItem.Uri, | ||
Version = documentItem.Version + 1 | ||
}, | ||
ContentChanges = changes | ||
}); | ||
} | ||
|
||
private Task<SignatureHelp> RequestSignatureHelpAsync(TextDocumentItem documentItem, Position position) { | ||
// TODO at this time we do not set the context since it appears that's also the case when used within VSCode. | ||
return _client.RequestSignatureHelp( | ||
new SignatureHelpParams { | ||
TextDocument = documentItem.Uri, | ||
Position = position | ||
}, | ||
CancellationToken | ||
); | ||
} | ||
|
||
[TestInitialize] | ||
public async Task SetUp() { | ||
_client = await InitializeClient(); | ||
} | ||
|
||
[TestMethod] | ||
public async Task SignatureHelpForUnloadedDocumentReturnsNoSignatures() { | ||
var documentItem = CreateTestDocument(""); | ||
var signatureHelp = await RequestSignatureHelpAsync(documentItem, (7, 11)); | ||
var signatures = signatureHelp.Signatures.ToArray(); | ||
Assert.AreEqual(0, signatures.Length); | ||
} | ||
|
||
[TestMethod] | ||
public async Task SignatureHelpOnOpeningParenthesesReturnsSignatureForExistingMethod() { | ||
var source = @" | ||
method Multiply(x: int, y: int) returns (p: int) | ||
ensures p == x * y | ||
{ | ||
return x * y; | ||
} | ||
method Main() { | ||
}".TrimStart(); | ||
var documentItem = CreateTestDocument(source); | ||
_client.OpenDocument(documentItem); | ||
ApplyChanges( | ||
documentItem, | ||
new TextDocumentContentChangeEvent { | ||
Range = new Range((7, 2), (7, 2)), | ||
Text = "Multiply(" | ||
} | ||
); | ||
|
||
var signatureHelp = await RequestSignatureHelpAsync(documentItem, (7, 11)); | ||
var signatures = signatureHelp.Signatures.ToArray(); | ||
Assert.AreEqual(1, signatures.Length); | ||
var markup = signatures[0].Documentation.MarkupContent; | ||
Assert.AreEqual(MarkupKind.Markdown, markup.Kind); | ||
Assert.AreEqual("```dafny\nmethod Multiply(x: int, y: int) returns (p: int)\n```", markup.Value); | ||
} | ||
|
||
[TestMethod] | ||
public async Task SignatureHelpOnOpeningParenthesesReturnsSignatureForExistingFunction() { | ||
var source = @" | ||
function Multiply(x: int, y: int): int | ||
{ | ||
x * y | ||
} | ||
method Main() { | ||
}".TrimStart(); | ||
var documentItem = CreateTestDocument(source); | ||
_client.OpenDocument(documentItem); | ||
ApplyChanges( | ||
documentItem, | ||
new TextDocumentContentChangeEvent { | ||
Range = new Range((6, 2), (6, 2)), | ||
Text = "Multiply(" | ||
} | ||
); | ||
|
||
var signatureHelp = await RequestSignatureHelpAsync(documentItem, (6, 11)); | ||
var signatures = signatureHelp.Signatures.ToArray(); | ||
Assert.AreEqual(1, signatures.Length); | ||
var markup = signatures[0].Documentation.MarkupContent; | ||
Assert.AreEqual(MarkupKind.Markdown, markup.Kind); | ||
Assert.AreEqual("```dafny\nfunction Multiply(x: int, y: int): int\n```", markup.Value); | ||
} | ||
|
||
[TestMethod] | ||
public async Task SignatureHelpOnOpeningParenthesesReturnsNoSignatureIfNoSuchMethodOrFunctionExists() { | ||
var source = @" | ||
method Main() { | ||
}".TrimStart(); | ||
var documentItem = CreateTestDocument(source); | ||
_client.OpenDocument(documentItem); | ||
ApplyChanges( | ||
documentItem, | ||
new TextDocumentContentChangeEvent { | ||
Range = new Range((1, 2), (1, 2)), | ||
Text = "Multiply(" | ||
} | ||
); | ||
|
||
var signatureHelp = await RequestSignatureHelpAsync(documentItem, (1, 11)); | ||
var signatures = signatureHelp.Signatures.ToArray(); | ||
Assert.AreEqual(0, signatures.Length); | ||
} | ||
|
||
[TestMethod] | ||
public async Task SignatureHelpOnOpeningParenthesesReturnsSignatureOfClosestFunction() { | ||
var source = @" | ||
function Multiply(x: int, y: int): int | ||
{ | ||
x * y | ||
} | ||
module Mod { | ||
function Multiply(a: int, b: int): int | ||
{ | ||
a * b | ||
} | ||
class SomeClass { | ||
function Multiply(n: int, m: int): int | ||
{ | ||
n * m | ||
} | ||
method GetProduct(x: int, y: int) returns (p: int) | ||
ensures p == x * y | ||
{ | ||
} | ||
} | ||
}".TrimStart(); | ||
var documentItem = CreateTestDocument(source); | ||
_client.OpenDocument(documentItem); | ||
ApplyChanges( | ||
documentItem, | ||
new TextDocumentContentChangeEvent { | ||
Range = new Range((20, 6), (20, 6)), | ||
Text = "Multiply(" | ||
} | ||
); | ||
|
||
var signatureHelp = await RequestSignatureHelpAsync(documentItem, (20, 15)); | ||
var signatures = signatureHelp.Signatures.ToArray(); | ||
Assert.AreEqual(1, signatures.Length); | ||
var markup = signatures[0].Documentation.MarkupContent; | ||
Assert.AreEqual(MarkupKind.Markdown, markup.Kind); | ||
Assert.AreEqual("```dafny\nfunction SomeClass.Multiply(n: int, m: int): int\n```", markup.Value); | ||
} | ||
|
||
[TestMethod] | ||
public async Task SignatureHelpOnOpeningParenthesesReturnsSignatureOfClassMemberOfDesignator() { | ||
var source = @" | ||
class A { | ||
constructor() {} | ||
function Multiply(n: int, m: int): int | ||
{ | ||
n * m | ||
} | ||
} | ||
class B { | ||
var a: A; | ||
constructor() { | ||
a := new A(); | ||
} | ||
function Multiply(x: int, y: int): int | ||
{ | ||
x * y | ||
} | ||
method GetProduct(x: int, y: int) returns (p: int) | ||
ensures p == x * y | ||
{ | ||
} | ||
}".TrimStart(); | ||
var documentItem = CreateTestDocument(source); | ||
_client.OpenDocument(documentItem); | ||
ApplyChanges( | ||
documentItem, | ||
new TextDocumentContentChangeEvent { | ||
Range = new Range((25, 4), (25, 4)), | ||
Text = "a.Multiply(" | ||
} | ||
); | ||
|
||
var signatureHelp = await RequestSignatureHelpAsync(documentItem, (25, 15)); | ||
var signatures = signatureHelp.Signatures.ToArray(); | ||
Assert.AreEqual(1, signatures.Length); | ||
var markup = signatures[0].Documentation.MarkupContent; | ||
Assert.AreEqual(MarkupKind.Markdown, markup.Kind); | ||
Assert.AreEqual("```dafny\nfunction A.Multiply(n: int, m: int): int\n```", markup.Value); | ||
} | ||
} | ||
} |