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

Commit

Permalink
fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ramya-rao-a committed Nov 17, 2016
1 parent aa102a8 commit d0c6671
Show file tree
Hide file tree
Showing 6 changed files with 129 additions and 70 deletions.
3 changes: 2 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ install:
- npm install
- npm run vscode:prepublish
- go get -u -v github.com/nsf/gocode
- if [[ "$(go version)" =~ "go version go1.5" ]]; then go get -u -v github.com/rogpeppe/godef; else go get -u -v github.com/zmb3/gogetdoc; fi
- go get -u -v github.com/rogpeppe/godef
- if [[ "$(go version)" =~ "go version go1.5" ]]; then echo hello; else go get -u -v github.com/zmb3/gogetdoc; fi
- if [[ "$(go version)" =~ "go version go1.5" ]]; then echo cannot get golint; else go get -u -v github.com/golang/lint/golint; fi
- go get -u -v github.com/lukehoban/go-outline
- go get -u -v sourcegraph.com/sqs/goreturns
Expand Down
13 changes: 9 additions & 4 deletions src/goDeclaration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,12 @@ export interface GoDefinitionInformtation {
toolUsed: string;
}

export function definitionLocation(document: vscode.TextDocument, position: vscode.Position, includeDocs = true): Promise<GoDefinitionInformtation> {
let toolToUse = vscode.workspace.getConfiguration('go')['docsTool'];
export function definitionLocation(document: vscode.TextDocument, position: vscode.Position, toolForDocs: string, includeDocs = true): Promise<GoDefinitionInformtation> {
return getGoVersion().then((ver: SemVersion) => {
if (!ver) {
return Promise.resolve(null);
}
if (toolToUse === 'godoc' || ver.major < 1 || (ver.major === 1 && ver.minor < 6)) {
if (toolForDocs === 'godoc' || ver.major < 1 || (ver.major === 1 && ver.minor < 6)) {
return definitionLocation_godef(document, position, includeDocs);
}
return definitionLocation_gogetdoc(document, position);
Expand Down Expand Up @@ -146,8 +145,14 @@ function definitionLocation_gogetdoc(document: vscode.TextDocument, position: vs
}

export class GoDefinitionProvider implements vscode.DefinitionProvider {
private toolForDocs = 'godoc';

constructor(toolForDocs: string) {
this.toolForDocs = toolForDocs;
}

public provideDefinition(document: vscode.TextDocument, position: vscode.Position, token: vscode.CancellationToken): Thenable<vscode.Location> {
return definitionLocation(document, position, false).then(definitionInfo => {
return definitionLocation(document, position, this.toolForDocs, false).then(definitionInfo => {
if (definitionInfo == null || definitionInfo.file == null) return null;
let definitionResource = vscode.Uri.file(definitionInfo.file);
let pos = new vscode.Position(definitionInfo.line, definitionInfo.column);
Expand Down
8 changes: 7 additions & 1 deletion src/goExtraInfo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,14 @@ import { HoverProvider, Hover, MarkedString, TextDocument, Position, Cancellatio
import { definitionLocation } from './goDeclaration';

export class GoHoverProvider implements HoverProvider {
private toolForDocs = 'godoc';

constructor(toolForDocs: string) {
this.toolForDocs = toolForDocs;
}

public provideHover(document: TextDocument, position: Position, token: CancellationToken): Thenable<Hover> {
return definitionLocation(document, position, true).then(definitionInfo => {
return definitionLocation(document, position, this.toolForDocs, true).then(definitionInfo => {
if (definitionInfo == null) return null;
let lines = definitionInfo.declarationlines
.filter(line => !line.startsWith('\t//') && line !== '')
Expand Down
6 changes: 3 additions & 3 deletions src/goMain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,15 @@ let goFormatOnSaveDeprecated = true;

export function activate(ctx: vscode.ExtensionContext): void {

ctx.subscriptions.push(vscode.languages.registerHoverProvider(GO_MODE, new GoHoverProvider()));
ctx.subscriptions.push(vscode.languages.registerHoverProvider(GO_MODE, new GoHoverProvider(vscode.workspace.getConfiguration('go')['docsTool'])));
ctx.subscriptions.push(vscode.languages.registerCompletionItemProvider(GO_MODE, new GoCompletionItemProvider(), '.', '\"'));
ctx.subscriptions.push(vscode.languages.registerDefinitionProvider(GO_MODE, new GoDefinitionProvider()));
ctx.subscriptions.push(vscode.languages.registerDefinitionProvider(GO_MODE, new GoDefinitionProvider(vscode.workspace.getConfiguration('go')['docsTool'])));
ctx.subscriptions.push(vscode.languages.registerReferenceProvider(GO_MODE, new GoReferenceProvider()));
ctx.subscriptions.push(vscode.languages.registerDocumentFormattingEditProvider(GO_MODE, new GoDocumentFormattingEditProvider()));
ctx.subscriptions.push(vscode.languages.registerDocumentSymbolProvider(GO_MODE, new GoDocumentSymbolProvider()));
ctx.subscriptions.push(vscode.languages.registerWorkspaceSymbolProvider(new GoWorkspaceSymbolProvider()));
ctx.subscriptions.push(vscode.languages.registerRenameProvider(GO_MODE, new GoRenameProvider()));
ctx.subscriptions.push(vscode.languages.registerSignatureHelpProvider(GO_MODE, new GoSignatureHelpProvider(), '(', ','));
ctx.subscriptions.push(vscode.languages.registerSignatureHelpProvider(GO_MODE, new GoSignatureHelpProvider(vscode.workspace.getConfiguration('go')['docsTool']), '(', ','));
ctx.subscriptions.push(vscode.languages.registerCodeActionsProvider(GO_MODE, new GoCodeActionProvider()));

diagnosticCollection = vscode.languages.createDiagnosticCollection('go');
Expand Down
7 changes: 6 additions & 1 deletion src/goSignature.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,19 @@ import { definitionLocation } from './goDeclaration';
import { parameters } from './util';

export class GoSignatureHelpProvider implements SignatureHelpProvider {
private toolForDocs = 'godoc';

constructor(toolForDocs: string) {
this.toolForDocs = toolForDocs;
}

public provideSignatureHelp(document: TextDocument, position: Position, token: CancellationToken): Promise<SignatureHelp> {
let theCall = this.walkBackwardsToBeginningOfCall(document, position);
if (theCall == null) {
return Promise.resolve(null);
}
let callerPos = this.previousTokenPosition(document, theCall.openParen);
return definitionLocation(document, callerPos).then(res => {
return definitionLocation(document, callerPos, this.toolForDocs).then(res => {
if (!res) {
// The definition was not found
return null;
Expand Down
162 changes: 102 additions & 60 deletions test/go.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,33 +35,99 @@ suite('Go Extension Tests', () => {
fs.copySync(path.join(fixtureSourcePath, 'test.go'), path.join(fixturePath, 'test.go'));
fs.copySync(path.join(fixtureSourcePath, 'errorsTest', 'errors.go'), path.join(fixturePath, 'errorsTest', 'errors.go'));
fs.copySync(path.join(fixtureSourcePath, 'sample_test.go'), path.join(fixturePath, 'sample_test.go'));
fs.copySync(path.join(fixtureSourcePath, 'vendorTest', 'vd.go'), path.join(fixturePath, 'vendor', 'abc', 'vd.go'));
fs.copySync(path.join(fixtureSourcePath, 'vendorTest', 'vd.go'), path.join(fixturePath, 'vendor', 'abc', 'internal', 'vd.go'));
cp.execSync('go install test/testfixture/vendor/abc');
cp.execSync('go install test/testfixture/vendor/abc/internal');
});

suiteTeardown(() => {
fs.removeSync(repoPath);
});

test('Test Definition Provider', (done) => {
let provider = new GoDefinitionProvider();
function testDefinitionProvider(tool: string): Thenable<any> {
let provider = new GoDefinitionProvider(tool);
let uri = vscode.Uri.file(path.join(fixturePath, 'test.go'));
let position = new vscode.Position(10, 3);
vscode.workspace.openTextDocument(uri).then((textDocument) => {
return vscode.workspace.openTextDocument(uri).then((textDocument) => {
return provider.provideDefinition(textDocument, position, null).then(definitionInfo => {
assert.equal(definitionInfo.uri.path, uri.path, `${definitionInfo.uri.path} is not the same as ${uri.path}`);
assert.equal(definitionInfo.uri.path.toLowerCase(), uri.path.toLowerCase(), `${definitionInfo.uri.path} is not the same as ${uri.path}`);
assert.equal(definitionInfo.range.start.line, 6);
assert.equal(definitionInfo.range.start.character, 5);
});
}, (err) => {
assert.ok(false, `error in OpenTextDocument ${err}`);
return Promise.reject(err);
});
}

function testSignatureHelpProvider(tool: string): Thenable<any> {
let provider = new GoSignatureHelpProvider(tool);
let testCases: [vscode.Position, string][] = [
[new vscode.Position(7, 13), 'Println(a ...interface{}) (n int, err error)'],
[new vscode.Position(10, 7), 'print(txt string)']
];
let uri = vscode.Uri.file(path.join(fixturePath, 'test.go'));
return vscode.workspace.openTextDocument(uri).then((textDocument) => {
let promises = testCases.map(([position, expected]) =>
provider.provideSignatureHelp(textDocument, position, null).then(sigHelp => {
assert.equal(sigHelp.signatures.length, 1, 'unexpected number of overloads');
assert.equal(sigHelp.signatures[0].label, expected);
})
);
return Promise.all(promises);
}, (err) => {
assert.ok(false, `error in OpenTextDocument ${err}`);
return Promise.reject(err);
});
}

function testHoverProvider(tool: string, testCases: [vscode.Position, string, string][]): Thenable<any> {
let provider = new GoHoverProvider(tool);
let uri = vscode.Uri.file(path.join(fixturePath, 'test.go'));
return vscode.workspace.openTextDocument(uri).then((textDocument) => {
let promises = testCases.map(([position, expectedSignature, expectedDocumentation]) =>
provider.provideHover(textDocument, position, null).then(res => {
// TODO: Documentation appears to currently be broken on Go 1.7, so disabling these tests for now
// if (expectedDocumentation === null) {
// assert.equal(res.contents.length, 1);
// } else {
// assert.equal(res.contents.length, 2);
// assert.equal(expectedDocumentation, <string>(res.contents[0]));
// }
assert.equal(expectedSignature, (<{ language: string; value: string }>res.contents[0]).value);
})
);
return Promise.all(promises);
}, (err) => {
assert.ok(false, `error in OpenTextDocument ${err}`);
return Promise.reject(err);
});
}

test('Test Definition Provider using godoc', (done) => {
testDefinitionProvider('godoc').then(() => done(), done);
});

test('Test Definition Provider using gogetdoc', (done) => {
getGoVersion().then(version => {
if (version.major > 1 || (version.major === 1 && version.minor > 5)) {
return testDefinitionProvider('gogetdoc');
}
return Promise.resolve();
}).then(() => done(), done);
});

test('Test SignatureHelp Provider using godoc', (done) => {
testSignatureHelpProvider('godoc').then(() => done(), done);
});

test('Test SignatureHelp Provider using gogetdoc', (done) => {
getGoVersion().then(version => {
if (version.major > 1 || (version.major === 1 && version.minor > 5)) {
return testSignatureHelpProvider('gogetdoc');
}
return Promise.resolve();
}).then(() => done(), done);
});

test('Test Hover Provider', (done) => {
let provider = new GoHoverProvider();
test('Test Hover Provider using godoc', (done) => {
let printlnDoc = `Println formats using the default formats for its operands and writes to
standard output. Spaces are always added between operands and a newline
is appended. It returns the number of bytes written and any write error
Expand All @@ -74,33 +140,27 @@ encountered.
[new vscode.Position(7, 6), 'Println func(a ...interface{}) (n int, err error)', printlnDoc],
[new vscode.Position(10, 3), 'print func(txt string)', null]
];
let uri = vscode.Uri.file(path.join(fixturePath, 'test.go'));
testHoverProvider('godoc', testCases).then(() => done(), done);
});

test('Test Hover Provider using gogetdoc', (done) => {
let printlnDoc = `Println formats using the default formats for its operands and writes to
standard output. Spaces are always added between operands and a newline
is appended. It returns the number of bytes written and any write error
encountered.
`;
let testCases: [vscode.Position, string, string][] = [
// [new vscode.Position(3,3), '/usr/local/go/src/fmt'],
[new vscode.Position(9, 6), 'func main()', null],
[new vscode.Position(7, 2), 'package fmt', null],
[new vscode.Position(7, 6), 'func Println(a ...interface{}) (n int, err error)', printlnDoc],
[new vscode.Position(10, 3), 'func print(txt string)', null]
];
getGoVersion().then(version => {
if (version.major > 1 || (version.major === 1 && version.minor > 5)) {
testCases[0][1] = 'func main()';
testCases[1][1] = 'package fmt';
testCases[2][1] = 'func Println(a ...interface{}) (n int, err error)';
testCases[3][1] = 'func print(txt string)';
return testHoverProvider('gogetdoc', testCases);
}
return vscode.workspace.openTextDocument(uri).then((textDocument) => {
let promises = testCases.map(([position, expectedSignature, expectedDocumentation]) =>
provider.provideHover(textDocument, position, null).then(res => {
// TODO: Documentation appears to currently be broken on Go 1.7, so disabling these tests for now
// if (expectedDocumentation === null) {
// assert.equal(res.contents.length, 1);
// } else {
// assert.equal(res.contents.length, 2);
// assert.equal(expectedDocumentation, <string>(res.contents[0]));
// }
assert.equal(expectedSignature, (<{ language: string; value: string }>res.contents[0]).value);
})
);
return Promise.all(promises);
}, (err) => {
assert.ok(false, `error in OpenTextDocument ${err}`);
return Promise.reject(err);
});
return Promise.resolve();
}).then(() => done(), done);
});

Expand Down Expand Up @@ -172,26 +232,6 @@ encountered.
}).then(() => done(), done);
});

test('Test Signature Help', (done) => {
let provider = new GoSignatureHelpProvider();
let testCases: [vscode.Position, string][] = [
[new vscode.Position(7, 13), 'Println(a ...interface{}) (n int, err error)'],
[new vscode.Position(10, 7), 'print(txt string)']
];
let uri = vscode.Uri.file(path.join(fixturePath, 'test.go'));
vscode.workspace.openTextDocument(uri).then((textDocument) => {
let promises = testCases.map(([position, expected]) =>
provider.provideSignatureHelp(textDocument, position, null).then(sigHelp => {
assert.equal(sigHelp.signatures.length, 1, 'unexpected number of overloads');
assert.equal(sigHelp.signatures[0].label, expected);
})
);
return Promise.all(promises);
}, (err) => {
assert.ok(false, `error in OpenTextDocument ${err}`);
}).then(() => done(), done);
});

test('Error checking', (done) => {
let config = vscode.workspace.getConfiguration('go');
let expected = [
Expand Down Expand Up @@ -388,8 +428,8 @@ encountered.
});
});

// This test is failing in Travis for Mac OS X with Go 1.7.
// Commenting this and created issue https://github.com/Microsoft/vscode-go/issues/609 to track the problem
// This test is failing in Travis for Mac OS X with Go 1.7.
// Commenting this and created issue https://github.com/Microsoft/vscode-go/issues/609 to track the problem
// test('Test Env Variables are passed to Tests', (done) => {
// let config = Object.create(vscode.workspace.getConfiguration('go'), {
// 'testEnvVars': { value: { 'dummyEnvVar': 'dummyEnvValue' } }
Expand Down Expand Up @@ -467,14 +507,16 @@ encountered.
// will fail and will have to be replaced with any other go project with vendor packages

let vendorSupportPromise = isVendorSupported();
let filePath = path.join(fixturePath, 'vendor', 'abc', 'vd.go');
let filePath = path.join(process.env['GOPATH'], 'src', 'github.com', 'rogpeppe', 'godef', 'go', 'ast', 'ast.go');
let vendorPkgsFullPath = [
'test/testfixture/vendor/abc',
'test/testfixture/vendor/abc/internal'
'github.com/rogpeppe/godef/vendor/9fans.net/go/acme',
'github.com/rogpeppe/godef/vendor/9fans.net/go/plan9',
'github.com/rogpeppe/godef/vendor/9fans.net/go/plan9/client'
];
let vendorPkgsRelativePath = [
'abc',
'abc/internal'
'9fans.net/go/acme',
'9fans.net/go/plan9',
'9fans.net/go/plan9/client'
];

vendorSupportPromise.then((vendorSupport: boolean) => {
Expand Down

0 comments on commit d0c6671

Please sign in to comment.