-
Notifications
You must be signed in to change notification settings - Fork 675
Add baseline tests from signature help #1485
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+8,152
−1
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
744ddf3
Recognize calls to `baselineSignatureHelp`
DanielRosenwasser 4319654
Implement `VerifyBaselineSignatureHelp`.
DanielRosenwasser d0fa88c
Add signature help tests.
DanielRosenwasser 12b984a
Don't set the active param line if there was no doc for it.
DanielRosenwasser 0fac324
Accept baselines.
DanielRosenwasser 090b132
Update failing tests list.
DanielRosenwasser 46f4555
Merge remote-tracking branch 'origin/main' into signatureHelpBaselines
DanielRosenwasser 19214aa
Update baselines.
DanielRosenwasser File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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 |
---|---|---|
|
@@ -996,6 +996,136 @@ func appendLinesForMarkedStringWithLanguage(result []string, ms *lsproto.MarkedS | |
return result | ||
} | ||
|
||
func (f *FourslashTest) VerifyBaselineSignatureHelp(t *testing.T) { | ||
if f.baseline != nil { | ||
t.Fatalf("Error during test '%s': Another baseline is already in progress", t.Name()) | ||
} else { | ||
f.baseline = &baselineFromTest{ | ||
content: &strings.Builder{}, | ||
baselineName: "signatureHelp/" + strings.TrimPrefix(t.Name(), "Test"), | ||
ext: ".baseline", | ||
} | ||
} | ||
|
||
// empty baseline after test completes | ||
defer func() { | ||
f.baseline = nil | ||
}() | ||
|
||
markersAndItems := core.MapFiltered(f.Markers(), func(marker *Marker) (markerAndItem[*lsproto.SignatureHelp], bool) { | ||
if marker.Name == nil { | ||
return markerAndItem[*lsproto.SignatureHelp]{}, false | ||
} | ||
|
||
params := &lsproto.SignatureHelpParams{ | ||
TextDocument: lsproto.TextDocumentIdentifier{ | ||
Uri: ls.FileNameToDocumentURI(f.activeFilename), | ||
}, | ||
Position: marker.LSPosition, | ||
} | ||
|
||
resMsg, result, resultOk := sendRequest(t, f, lsproto.TextDocumentSignatureHelpInfo, params) | ||
var prefix string | ||
if f.lastKnownMarkerName != nil { | ||
prefix = fmt.Sprintf("At marker '%s': ", *f.lastKnownMarkerName) | ||
} else { | ||
prefix = fmt.Sprintf("At position (Ln %d, Col %d): ", f.currentCaretPosition.Line, f.currentCaretPosition.Character) | ||
} | ||
if resMsg == nil { | ||
t.Fatalf(prefix+"Nil response received for signature help request", f.lastKnownMarkerName) | ||
} | ||
if !resultOk { | ||
t.Fatalf(prefix+"Unexpected response type for signature help request: %T", resMsg.AsResponse().Result) | ||
} | ||
|
||
return markerAndItem[*lsproto.SignatureHelp]{Marker: marker, Item: result.SignatureHelp}, true | ||
}) | ||
|
||
getRange := func(item *lsproto.SignatureHelp) *lsproto.Range { | ||
// SignatureHelp doesn't have a range like hover does | ||
return nil | ||
} | ||
|
||
getTooltipLines := func(item, _prev *lsproto.SignatureHelp) []string { | ||
if item == nil || len(item.Signatures) == 0 { | ||
return []string{"No signature help available"} | ||
} | ||
|
||
// Show active signature if specified, otherwise first signature | ||
activeSignature := 0 | ||
if item.ActiveSignature != nil && int(*item.ActiveSignature) < len(item.Signatures) { | ||
activeSignature = int(*item.ActiveSignature) | ||
} | ||
|
||
sig := item.Signatures[activeSignature] | ||
|
||
// Build signature display | ||
signatureLine := sig.Label | ||
activeParamLine := "" | ||
|
||
// Show active parameter if specified, and the signature text. | ||
if item.ActiveParameter != nil && sig.Parameters != nil { | ||
activeParamIndex := int(*item.ActiveParameter.Uinteger) | ||
if activeParamIndex >= 0 && activeParamIndex < len(*sig.Parameters) { | ||
activeParam := (*sig.Parameters)[activeParamIndex] | ||
|
||
// Get the parameter label and bold the | ||
// parameter text within the original string. | ||
activeParamLabel := "" | ||
if activeParam.Label.String != nil { | ||
activeParamLabel = *activeParam.Label.String | ||
} else if activeParam.Label.Tuple != nil { | ||
activeParamLabel = signatureLine[(*activeParam.Label.Tuple)[0]:(*activeParam.Label.Tuple)[1]] | ||
} else { | ||
t.Fatal("Unsupported param label kind.") | ||
} | ||
signatureLine = strings.Replace(signatureLine, activeParamLabel, "**"+activeParamLabel+"**", 1) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should really only be done if we don't come back with the tuple representation. |
||
|
||
if activeParam.Documentation != nil { | ||
if activeParam.Documentation.MarkupContent != nil { | ||
activeParamLine = activeParam.Documentation.MarkupContent.Value | ||
} else if activeParam.Documentation.String != nil { | ||
activeParamLine = *activeParam.Documentation.String | ||
} | ||
|
||
activeParamLine = fmt.Sprintf("- `%s`: %s", activeParamLabel, activeParamLine) | ||
} | ||
|
||
} | ||
} | ||
|
||
result := make([]string, 0, 16) | ||
result = append(result, signatureLine) | ||
if activeParamLine != "" { | ||
result = append(result, activeParamLine) | ||
} | ||
|
||
// ORIGINALLY we would "only display signature documentation on the last argument when multiple arguments are marked". | ||
// !!! | ||
// Note that this is harder than in Strada, because LSP signature help has no concept of | ||
// applicable spans. | ||
if sig.Documentation != nil { | ||
if sig.Documentation.MarkupContent != nil { | ||
result = append(result, strings.Split(sig.Documentation.MarkupContent.Value, "\n")...) | ||
} else if sig.Documentation.String != nil { | ||
result = append(result, strings.Split(*sig.Documentation.String, "\n")...) | ||
} else { | ||
t.Fatal("Unsupported documentation format.") | ||
} | ||
} | ||
|
||
return result | ||
} | ||
|
||
f.baseline.addResult("SignatureHelp", annotateContentWithTooltips(t, f, markersAndItems, "signaturehelp", getRange, getTooltipLines)) | ||
if jsonStr, err := core.StringifyJson(markersAndItems, "", " "); err == nil { | ||
f.baseline.content.WriteString(jsonStr) | ||
} else { | ||
t.Fatalf("Failed to stringify markers and items for baseline: %v", err) | ||
} | ||
baseline.Run(t, f.baseline.getBaselineFileName(), f.baseline.content.String(), baseline.Options{}) | ||
} | ||
|
||
// Collects all named markers if provided, or defaults to anonymous ranges | ||
func (f *FourslashTest) lookupMarkersOrGetRanges(t *testing.T, markers []string) []MarkerOrRange { | ||
var referenceLocations []MarkerOrRange | ||
|
33 changes: 33 additions & 0 deletions
33
internal/fourslash/tests/gen/jsDocDontBreakWithNamespaces_test.go
This file contains hidden or 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,33 @@ | ||
package fourslash_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/microsoft/typescript-go/internal/fourslash" | ||
"github.com/microsoft/typescript-go/internal/testutil" | ||
) | ||
|
||
func TestJsDocDontBreakWithNamespaces(t *testing.T) { | ||
t.Parallel() | ||
|
||
defer testutil.RecoverAndFail(t, "Panic on fourslash test") | ||
const content = `// @allowJs: true | ||
// @Filename: jsDocDontBreakWithNamespaces.js | ||
/** | ||
* @returns {module:@nodefuel/web~Webserver~wsServer#hello} Websocket server object | ||
*/ | ||
function foo() { } | ||
foo(''/*foo*/); | ||
|
||
/** | ||
* @type {module:xxxxx} */ | ||
*/ | ||
function bar() { } | ||
bar(''/*bar*/); | ||
|
||
/** @type {function(module:xxxx, module:xxxx): module:xxxxx} */ | ||
function zee() { } | ||
zee(''/*zee*/);` | ||
f := fourslash.NewFourslash(t, nil /*capabilities*/, content) | ||
f.VerifyBaselineSignatureHelp(t) | ||
} |
30 changes: 30 additions & 0 deletions
30
internal/fourslash/tests/gen/jsDocFunctionSignatures5_test.go
This file contains hidden or 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,30 @@ | ||
package fourslash_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/microsoft/typescript-go/internal/fourslash" | ||
"github.com/microsoft/typescript-go/internal/testutil" | ||
) | ||
|
||
func TestJsDocFunctionSignatures5(t *testing.T) { | ||
t.Parallel() | ||
|
||
defer testutil.RecoverAndFail(t, "Panic on fourslash test") | ||
const content = `// @allowJs: true | ||
// @Filename: Foo.js | ||
/** | ||
* Filters a path based on a regexp or glob pattern. | ||
* @param {String} basePath The base path where the search will be performed. | ||
* @param {String} pattern A string defining a regexp of a glob pattern. | ||
* @param {String} type The search pattern type, can be a regexp or a glob. | ||
* @param {Object} options A object containing options to the search. | ||
* @return {Array} A list containing the filtered paths. | ||
*/ | ||
function pathFilter(basePath, pattern, type, options){ | ||
//... | ||
} | ||
pathFilter(/**/'foo', 'bar', 'baz', {});` | ||
f := fourslash.NewFourslash(t, nil /*capabilities*/, content) | ||
f.VerifyBaselineSignatureHelp(t) | ||
} |
26 changes: 26 additions & 0 deletions
26
internal/fourslash/tests/gen/jsDocFunctionSignatures6_test.go
This file contains hidden or 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,26 @@ | ||
package fourslash_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/microsoft/typescript-go/internal/fourslash" | ||
"github.com/microsoft/typescript-go/internal/testutil" | ||
) | ||
|
||
func TestJsDocFunctionSignatures6(t *testing.T) { | ||
t.Parallel() | ||
|
||
defer testutil.RecoverAndFail(t, "Panic on fourslash test") | ||
const content = `// @allowJs: true | ||
// @Filename: Foo.js | ||
/** | ||
* @param {string} p1 - A string param | ||
* @param {string?} p2 - An optional param | ||
* @param {string} [p3] - Another optional param | ||
* @param {string} [p4="test"] - An optional param with a default value | ||
*/ | ||
function f1(p1, p2, p3, p4){} | ||
f1(/*1*/'foo', /*2*/'bar', /*3*/'baz', /*4*/'qux');` | ||
f := fourslash.NewFourslash(t, nil /*capabilities*/, content) | ||
f.VerifyBaselineSignatureHelp(t) | ||
} |
This file contains hidden or 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,21 @@ | ||
package fourslash_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/microsoft/typescript-go/internal/fourslash" | ||
"github.com/microsoft/typescript-go/internal/testutil" | ||
) | ||
|
||
func TestJsDocSignature_43394(t *testing.T) { | ||
t.Parallel() | ||
|
||
defer testutil.RecoverAndFail(t, "Panic on fourslash test") | ||
const content = `/** | ||
* @typedef {Object} Foo | ||
* @property {number} ... | ||
* /**/@typedef {number} Bar | ||
*/` | ||
f := fourslash.NewFourslash(t, nil /*capabilities*/, content) | ||
f.VerifyBaselineSignatureHelp(t) | ||
} |
This file contains hidden or 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,28 @@ | ||
package fourslash_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/microsoft/typescript-go/internal/fourslash" | ||
"github.com/microsoft/typescript-go/internal/testutil" | ||
) | ||
|
||
func TestJsdocReturnsTag(t *testing.T) { | ||
t.Parallel() | ||
|
||
defer testutil.RecoverAndFail(t, "Panic on fourslash test") | ||
const content = `// @allowJs: true | ||
// @Filename: dummy.js | ||
/** | ||
* Find an item | ||
* @template T | ||
* @param {T[]} l | ||
* @param {T} x | ||
* @returns {?T} The names of the found item(s). | ||
*/ | ||
function find(l, x) { | ||
} | ||
find(''/**/);` | ||
f := fourslash.NewFourslash(t, nil /*capabilities*/, content) | ||
f.VerifyBaselineSignatureHelp(t) | ||
} |
This file contains hidden or 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,41 @@ | ||
package fourslash_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/microsoft/typescript-go/internal/fourslash" | ||
"github.com/microsoft/typescript-go/internal/testutil" | ||
) | ||
|
||
func TestQuickInfoJsDocTags13(t *testing.T) { | ||
t.Parallel() | ||
|
||
defer testutil.RecoverAndFail(t, "Panic on fourslash test") | ||
const content = `// @allowJs: true | ||
// @checkJs: true | ||
// @filename: ./a.js | ||
/** | ||
* First overload | ||
* @overload | ||
* @param {number} a | ||
* @returns {void} | ||
*/ | ||
|
||
/** | ||
* Second overload | ||
* @overload | ||
* @param {string} a | ||
* @returns {void} | ||
*/ | ||
|
||
/** | ||
* @param {string | number} a | ||
* @returns {void} | ||
*/ | ||
function f(a) {} | ||
|
||
f(/*a*/1); | ||
f(/*b*/"");` | ||
f := fourslash.NewFourslash(t, nil /*capabilities*/, content) | ||
f.VerifyBaselineSignatureHelp(t) | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is slightly wrong because these are supposed to be UTF-16 offsets. But it's not really "testable' today because we don't even come back with this data.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You could utf16.Encode + slice + utf16.Decode