|  | 
|  | 1 | +// Copyright (c) Microsoft Corporation. All rights reserved. | 
|  | 2 | +// Licensed under the MIT License. | 
|  | 3 | +'use strict'; | 
|  | 4 | + | 
|  | 5 | +import * as assert from 'assert'; | 
|  | 6 | +import * as path from 'path'; | 
|  | 7 | +import * as vscode from 'vscode'; | 
|  | 8 | +import { PythonSettings } from '../../client/common/configSettings'; | 
|  | 9 | +import { execPythonFile } from '../../client/common/utils'; | 
|  | 10 | +import { rootWorkspaceUri } from '../common'; | 
|  | 11 | +import { closeActiveWindows, initialize, initializeTest } from '../initialize'; | 
|  | 12 | + | 
|  | 13 | +const autoCompPath = path.join(__dirname, '..', '..', '..', 'src', 'test', 'pythonFiles', 'signature'); | 
|  | 14 | + | 
|  | 15 | +class SignatureHelpResult { | 
|  | 16 | + constructor( | 
|  | 17 | + public line: number, | 
|  | 18 | + public index: number, | 
|  | 19 | + public signaturesCount: number, | 
|  | 20 | + public activeParameter: number, | 
|  | 21 | + public parameterName: string | null) { } | 
|  | 22 | +} | 
|  | 23 | + | 
|  | 24 | +// tslint:disable-next-line:max-func-body-length | 
|  | 25 | +suite('Signatures', () => { | 
|  | 26 | + let isPython3: Promise<boolean>; | 
|  | 27 | + suiteSetup(async () => { | 
|  | 28 | + await initialize(); | 
|  | 29 | + const version = await execPythonFile(rootWorkspaceUri, PythonSettings.getInstance(rootWorkspaceUri).pythonPath, ['--version'], __dirname, true); | 
|  | 30 | + isPython3 = Promise.resolve(version.indexOf('3.') >= 0); | 
|  | 31 | + }); | 
|  | 32 | + setup(initializeTest); | 
|  | 33 | + suiteTeardown(closeActiveWindows); | 
|  | 34 | + teardown(closeActiveWindows); | 
|  | 35 | + | 
|  | 36 | + test('For ctor', async () => { | 
|  | 37 | + const expected = [ | 
|  | 38 | + new SignatureHelpResult(5, 11, 0, 0, null), | 
|  | 39 | + new SignatureHelpResult(5, 12, 1, 0, 'name'), | 
|  | 40 | + new SignatureHelpResult(5, 13, 1, 0, 'name'), | 
|  | 41 | + new SignatureHelpResult(5, 14, 1, 0, 'name'), | 
|  | 42 | + new SignatureHelpResult(5, 15, 1, 0, 'name'), | 
|  | 43 | + new SignatureHelpResult(5, 16, 1, 0, 'name'), | 
|  | 44 | + new SignatureHelpResult(5, 17, 1, 0, 'name'), | 
|  | 45 | + new SignatureHelpResult(5, 18, 1, 1, 'age'), | 
|  | 46 | + new SignatureHelpResult(5, 19, 1, 1, 'age'), | 
|  | 47 | + new SignatureHelpResult(5, 20, 0, 0, null) | 
|  | 48 | + ]; | 
|  | 49 | + | 
|  | 50 | + const document = await openDocument(path.join(autoCompPath, 'classCtor.py')); | 
|  | 51 | + for (let i = 0; i < expected.length; i += 1) { | 
|  | 52 | + await checkSignature(expected[i], document!.uri, i); | 
|  | 53 | + } | 
|  | 54 | + }); | 
|  | 55 | + | 
|  | 56 | + test('For intrinsic', async () => { | 
|  | 57 | + const expected = [ | 
|  | 58 | + new SignatureHelpResult(0, 0, 0, 0, null), | 
|  | 59 | + new SignatureHelpResult(0, 1, 0, 0, null), | 
|  | 60 | + new SignatureHelpResult(0, 2, 0, 0, null), | 
|  | 61 | + new SignatureHelpResult(0, 3, 0, 0, null), | 
|  | 62 | + new SignatureHelpResult(0, 4, 0, 0, null), | 
|  | 63 | + new SignatureHelpResult(0, 5, 0, 0, null), | 
|  | 64 | + new SignatureHelpResult(0, 6, 1, 0, 'start'), | 
|  | 65 | + new SignatureHelpResult(0, 7, 1, 0, 'start'), | 
|  | 66 | + new SignatureHelpResult(0, 8, 1, 1, 'stop'), | 
|  | 67 | + new SignatureHelpResult(0, 9, 1, 1, 'stop'), | 
|  | 68 | + new SignatureHelpResult(0, 10, 1, 1, 'stop'), | 
|  | 69 | + new SignatureHelpResult(0, 11, 1, 2, 'step'), | 
|  | 70 | + new SignatureHelpResult(1, 0, 1, 2, 'step') | 
|  | 71 | + ]; | 
|  | 72 | + | 
|  | 73 | + const document = await openDocument(path.join(autoCompPath, 'basicSig.py')); | 
|  | 74 | + for (let i = 0; i < expected.length; i += 1) { | 
|  | 75 | + await checkSignature(expected[i], document!.uri, i); | 
|  | 76 | + } | 
|  | 77 | + }); | 
|  | 78 | + | 
|  | 79 | + test('For ellipsis', async () => { | 
|  | 80 | + if (!await isPython3) { | 
|  | 81 | + return; | 
|  | 82 | + } | 
|  | 83 | + const expected = [ | 
|  | 84 | + new SignatureHelpResult(0, 5, 0, 0, null), | 
|  | 85 | + new SignatureHelpResult(0, 6, 1, 0, 'value'), | 
|  | 86 | + new SignatureHelpResult(0, 7, 1, 0, 'value'), | 
|  | 87 | + new SignatureHelpResult(0, 8, 1, 1, '...'), | 
|  | 88 | + new SignatureHelpResult(0, 9, 1, 1, '...'), | 
|  | 89 | + new SignatureHelpResult(0, 10, 1, 1, '...'), | 
|  | 90 | + new SignatureHelpResult(0, 11, 1, 2, 'sep'), | 
|  | 91 | + new SignatureHelpResult(0, 12, 1, 2, 'sep') | 
|  | 92 | + ]; | 
|  | 93 | + | 
|  | 94 | + const document = await openDocument(path.join(autoCompPath, 'ellipsis.py')); | 
|  | 95 | + for (let i = 0; i < expected.length; i += 1) { | 
|  | 96 | + await checkSignature(expected[i], document!.uri, i); | 
|  | 97 | + } | 
|  | 98 | + }); | 
|  | 99 | + | 
|  | 100 | + test('For pow', async () => { | 
|  | 101 | + let expected: SignatureHelpResult; | 
|  | 102 | + if (await isPython3) { | 
|  | 103 | + expected = new SignatureHelpResult(0, 4, 1, 0, null); | 
|  | 104 | + } else { | 
|  | 105 | + expected = new SignatureHelpResult(0, 4, 1, 0, 'x'); | 
|  | 106 | + } | 
|  | 107 | + | 
|  | 108 | + const document = await openDocument(path.join(autoCompPath, 'noSigPy3.py')); | 
|  | 109 | + await checkSignature(expected, document!.uri, 0); | 
|  | 110 | + }); | 
|  | 111 | +}); | 
|  | 112 | + | 
|  | 113 | +async function openDocument(documentPath: string): Promise<vscode.TextDocument | undefined> { | 
|  | 114 | + const document = await vscode.workspace.openTextDocument(documentPath); | 
|  | 115 | + await vscode.window.showTextDocument(document!); | 
|  | 116 | + return document; | 
|  | 117 | +} | 
|  | 118 | + | 
|  | 119 | +async function checkSignature(expected: SignatureHelpResult, uri: vscode.Uri, caseIndex: number) { | 
|  | 120 | + const position = new vscode.Position(expected.line, expected.index); | 
|  | 121 | + const actual = await vscode.commands.executeCommand<vscode.SignatureHelp>('vscode.executeSignatureHelpProvider', uri, position); | 
|  | 122 | + assert.equal(actual!.signatures.length, expected.signaturesCount, `Signature count does not match, case ${caseIndex}`); | 
|  | 123 | + if (expected.signaturesCount > 0) { | 
|  | 124 | + assert.equal(actual!.activeParameter, expected.activeParameter, `Parameter index does not match, case ${caseIndex}`); | 
|  | 125 | + if (expected.parameterName) { | 
|  | 126 | + const parameter = actual!.signatures[0].parameters[expected.activeParameter]; | 
|  | 127 | + assert.equal(parameter.label, expected.parameterName, `Parameter name is incorrect, case ${caseIndex}`); | 
|  | 128 | + } | 
|  | 129 | + } | 
|  | 130 | +} | 
0 commit comments