Skip to content
This repository has been archived by the owner on Jul 15, 2023. It is now read-only.

Commit

Permalink
Test coverage for snippets in completions
Browse files Browse the repository at this point in the history
  • Loading branch information
ramya-rao-a committed May 13, 2018
1 parent 9db6593 commit 8dd3f33
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 7 deletions.
13 changes: 6 additions & 7 deletions src/goSuggest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ export class GoCompletionItemProvider implements vscode.CompletionItemProvider {
let inputText = document.getText();
let includeUnimportedPkgs = autocompleteUnimportedPackages && !inString;

return this.runGoCode(document, filename, inputText, offset, inString, position, lineText, currentWord, includeUnimportedPkgs).then(suggestions => {
return this.runGoCode(document, filename, inputText, offset, inString, position, lineText, currentWord, includeUnimportedPkgs, config).then(suggestions => {
// gocode does not suggest keywords, so we have to do it
if (currentWord.length > 0) {
goKeywords.forEach(keyword => {
Expand All @@ -101,7 +101,7 @@ export class GoCompletionItemProvider implements vscode.CompletionItemProvider {
offset += textToAdd.length;

// Now that we have the package imported in the inputText, run gocode again
return this.runGoCode(document, filename, inputText, offset, inString, position, lineText, currentWord, false).then(newsuggestions => {
return this.runGoCode(document, filename, inputText, offset, inString, position, lineText, currentWord, false, config).then(newsuggestions => {
// Since the new suggestions are due to the package that we imported,
// add additionalTextEdits to do the same in the actual document in the editor
// We use additionalTextEdits instead of command so that 'useCodeSnippetsOnFunctionSuggest' feature continues to work
Expand All @@ -118,7 +118,7 @@ export class GoCompletionItemProvider implements vscode.CompletionItemProvider {
});
}

private runGoCode(document: vscode.TextDocument, filename: string, inputText: string, offset: number, inString: boolean, position: vscode.Position, lineText: string, currentWord: string, includeUnimportedPkgs: boolean): Thenable<vscode.CompletionItem[]> {
private runGoCode(document: vscode.TextDocument, filename: string, inputText: string, offset: number, inString: boolean, position: vscode.Position, lineText: string, currentWord: string, includeUnimportedPkgs: boolean, config: vscode.WorkspaceConfiguration): Thenable<vscode.CompletionItem[]> {
return new Promise<vscode.CompletionItem[]>((resolve, reject) => {
let gocode = getBinPath('gocode');

Expand Down Expand Up @@ -167,16 +167,15 @@ export class GoCompletionItemProvider implements vscode.CompletionItemProvider {
suggest.name
);
}
let conf = vscode.workspace.getConfiguration('go', vscode.window.activeTextEditor ? vscode.window.activeTextEditor.document.uri : null);
if ((conf.get('useCodeSnippetsOnFunctionSuggest') || conf.get('useCodeSnippetsOnFunctionSuggestWithoutType'))
if ((config['useCodeSnippetsOnFunctionSuggest'] || config['useCodeSnippetsOnFunctionSuggestWithoutType'])
&& (suggest.class === 'func' || suggest.class === 'var' && suggest.type.startsWith('func('))) {
let params = parameters(suggest.type.substring(4));
let paramSnippets = [];
for (let i = 0; i < params.length; i++) {
let param = params[i].trim();
if (param) {
param = param.replace('${', '\\${').replace('}', '\\}');
if (conf.get('useCodeSnippetsOnFunctionSuggestWithoutType')) {
if (config['useCodeSnippetsOnFunctionSuggestWithoutType']) {
if (param.includes(' ')) {
// Separate the variable name from the type
param = param.substr(0, param.indexOf(' '));
Expand All @@ -187,7 +186,7 @@ export class GoCompletionItemProvider implements vscode.CompletionItemProvider {
}
item.insertText = new vscode.SnippetString(suggest.name + '(' + paramSnippets.join(', ') + ')');
}
if (conf.get('useCodeSnippetsOnFunctionSuggest') && suggest.class === 'type' && suggest.type.startsWith('func(')) {
if (config['useCodeSnippetsOnFunctionSuggest'] && suggest.class === 'type' && suggest.type.startsWith('func(')) {
let params = parameters(suggest.type.substring(4));
let paramSnippets = [];
for (let i = 0; i < params.length; i++) {
Expand Down
15 changes: 15 additions & 0 deletions test/fixtures/completions/snippets.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package main

import "fmt"
import "net/http"

type HandlerFunc func(http.ResponseWriter, *http.Request)
type HandlerFuncWithArgNames func(w http.ResponseWriter, r *http.Request)

func main(){
fmt.Println("hello")
funcAsVariable := func (k string) {}
funcAsVariable("hello")
}

H
66 changes: 66 additions & 0 deletions test/go.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ suite('Go Extension Tests', () => {
fs.copySync(path.join(fixtureSourcePath, 'linterTest', 'linter_2.go'), path.join(testPath, 'linterTest', 'linter_2.go'));
fs.copySync(path.join(fixtureSourcePath, 'buildTags', 'hello.go'), path.join(fixturePath, 'buildTags', 'hello.go'));
fs.copySync(path.join(fixtureSourcePath, 'completions', 'unimportedPkgs.go'), path.join(fixturePath, 'completions', 'unimportedPkgs.go'));
fs.copySync(path.join(fixtureSourcePath, 'completions', 'snippets.go'), path.join(fixturePath, 'completions', 'snippets.go'));
fs.copySync(path.join(fixtureSourcePath, 'importTest', 'noimports.go'), path.join(fixturePath, 'importTest', 'noimports.go'));
fs.copySync(path.join(fixtureSourcePath, 'importTest', 'groupImports.go'), path.join(fixturePath, 'importTest', 'groupImports.go'));
fs.copySync(path.join(fixtureSourcePath, 'importTest', 'singleImports.go'), path.join(fixturePath, 'importTest', 'singleImports.go'));
Expand Down Expand Up @@ -765,6 +766,71 @@ It returns the number of bytes written and any write error encountered.
}).then(() => done(), done);
});

test('Test Completion Snippets For Functions', (done) => {
let provider = new GoCompletionItemProvider();
let uri = vscode.Uri.file(path.join(fixturePath, 'completions', 'snippets.go'));
let testCases: [vscode.Position, string[]][] = [
[new vscode.Position(5, 6), ['Print']]
];
let baseConfig = vscode.workspace.getConfiguration('go');
vscode.workspace.openTextDocument(uri).then((textDocument) => {
return vscode.window.showTextDocument(textDocument).then(editor => {

let noFunctionSnippet = provider.provideCompletionItemsInternal(editor.document, new vscode.Position(9, 6), null, Object.create(baseConfig, {'useCodeSnippetsOnFunctionSuggest': { value: false }})).then(items => {
let item = items.find(x => x.label === 'Print');
assert.equal(!item.insertText, true);
});

let withFunctionSnippet = provider.provideCompletionItemsInternal(editor.document, new vscode.Position(9, 6), null, Object.create(baseConfig, {'useCodeSnippetsOnFunctionSuggest': { value: true }})).then(items => {
let item = items.find(x => x.label === 'Print');
assert.equal((<vscode.SnippetString>item.insertText).value, 'Print(${1:a ...interface{\\}})');

});

let withFunctionSnippetNotype = provider.provideCompletionItemsInternal(editor.document, new vscode.Position(9, 6), null, Object.create(baseConfig, {'useCodeSnippetsOnFunctionSuggestWithoutType': { value: true }})).then(items => {
let item = items.find(x => x.label === 'Print');
assert.equal((<vscode.SnippetString>item.insertText).value, 'Print(${1:a})');
});

let noFunctionAsVarSnippet = provider.provideCompletionItemsInternal(editor.document, new vscode.Position(11, 3), null, Object.create(baseConfig, {'useCodeSnippetsOnFunctionSuggest': { value: false }})).then(items => {
let item = items.find(x => x.label === 'funcAsVariable');
assert.equal(!item.insertText, true);
});

let withFunctionAsVarSnippet = provider.provideCompletionItemsInternal(editor.document, new vscode.Position(11, 3), null, Object.create(baseConfig, {'useCodeSnippetsOnFunctionSuggest': { value: true }})).then(items => {
let item = items.find(x => x.label === 'funcAsVariable');
assert.equal((<vscode.SnippetString>item.insertText).value, 'funcAsVariable(${1:k string})');
});

let withFunctionAsVarSnippetNoType = provider.provideCompletionItemsInternal(editor.document, new vscode.Position(11, 3), null, Object.create(baseConfig, {'useCodeSnippetsOnFunctionSuggestWithoutType': { value: true }})).then(items => {
let item = items.find(x => x.label === 'funcAsVariable');
assert.equal((<vscode.SnippetString>item.insertText).value, 'funcAsVariable(${1:k})');
});

let noFunctionAsTypeSnippet = provider.provideCompletionItemsInternal(editor.document, new vscode.Position(14, 0), null, Object.create(baseConfig, {'useCodeSnippetsOnFunctionSuggest': { value: false }})).then(items => {
let item1 = items.find(x => x.label === 'HandlerFunc');
let item2 = items.find(x => x.label === 'HandlerFuncWithArgNames');
assert.equal(!item1.insertText, true);
assert.equal(!item2.insertText, true);
});

let withFunctionAsTypeSnippet = provider.provideCompletionItemsInternal(editor.document, new vscode.Position(14, 0), null, Object.create(baseConfig, {'useCodeSnippetsOnFunctionSuggest': { value: true }})).then(items => {
let item1 = items.find(x => x.label === 'HandlerFunc');
let item2 = items.find(x => x.label === 'HandlerFuncWithArgNames');
assert.equal((<vscode.SnippetString>item1.insertText).value, 'HandlerFunc(func(${1:arg1} http.ResponseWriter, ${2:arg2} *http.Request) {\n\t$3\n})');
assert.equal((<vscode.SnippetString>item2.insertText).value, 'HandlerFuncWithArgNames(func(${1:w} http.ResponseWriter, ${2:r} *http.Request) {\n\t$3\n})');
});

return Promise.all([
noFunctionSnippet, withFunctionSnippet, withFunctionSnippetNotype,
noFunctionAsVarSnippet, withFunctionAsVarSnippet, withFunctionAsVarSnippetNoType,
noFunctionAsTypeSnippet, withFunctionAsTypeSnippet]).then(() => vscode.commands.executeCommand('workbench.action.closeActiveEditor'));
});
}, (err) => {
assert.ok(false, `error in OpenTextDocument ${err}`);
}).then(() => done(), done);
});

test('Test Completion on unimported packages', (done) => {
let config = Object.create(vscode.workspace.getConfiguration('go'), {
'autocompleteUnimportedPackages': { value: true }
Expand Down

0 comments on commit 8dd3f33

Please sign in to comment.