Skip to content

Commit

Permalink
Start adding some basic pinning tests for jsdoc -> snippet
Browse files Browse the repository at this point in the history
  • Loading branch information
mjbvz committed May 2, 2018
1 parent 83d4bb6 commit 7b3c343
Show file tree
Hide file tree
Showing 6 changed files with 1,642 additions and 21 deletions.
14 changes: 14 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,20 @@
"${workspaceFolder}/extensions/markdown-language-features/out/**/*.js"
]
},
{
"type": "extensionHost",
"request": "launch",
"name": "TypeScript Extension Tests",
"runtimeExecutable": "${execPath}",
"args": [
"${workspaceFolder}/extensions/typescript-language-features/test-fixtures",
"--extensionDevelopmentPath=${workspaceFolder}/extensions/typescript-language-features",
"--extensionTestsPath=${workspaceFolder}/extensions/typescript-language-features/out/test"
],
"outFiles": [
"${workspaceFolder}/extensions/typescript-language-features/out/**/*.js"
]
},
{
"type": "node",
"request": "launch",
Expand Down
3 changes: 2 additions & 1 deletion extensions/typescript-language-features/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@
},
"devDependencies": {
"@types/node": "8.0.33",
"@types/semver": "5.4.0"
"@types/semver": "5.4.0",
"vscode": "^1.1.10"
},
"scripts": {
"vscode:prepublish": "node ../../node_modules/gulp/bin/gulp.js --gulpfile ../../build/gulpfile.extensions.js compile-extension:typescript ./tsconfig.json"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -176,28 +176,10 @@ class TryCompleteJsDocCommand implements Command {
if (res.body.newText === '/** */') {
return undefined;
}
return TryCompleteJsDocCommand.templateToSnippet(res.body.newText);
return templateToSnippet(res.body.newText);
}, () => undefined);
}

private static templateToSnippet(template: string): SnippetString {
// TODO: use append placeholder
let snippetIndex = 1;
template = template.replace(/^\s*(?=(\/|[ ]\*))/gm, '');
template = template.replace(/^(\/\*\*\s*\*[ ]*)$/m, (x) => x + `\$0`);
template = template.replace(/\* @param([ ]\{\S+\})?\s+(\S+)\s*$/gm, (_param, type, post) => {
let out = '* @param ';
if (type === ' {any}' || type === ' {*}') {
out += `{\$\{${snippetIndex++}:*\}} `;
} else if (type) {
out += type + ' ';
}
out += post + ` \${${snippetIndex++}}`;
return out;
});
return new SnippetString(template);
}

/**
* Insert the default JSDoc
*/
Expand All @@ -206,3 +188,22 @@ class TryCompleteJsDocCommand implements Command {
return editor.insertSnippet(snippet, position, { undoStopBefore: false, undoStopAfter: true });
}
}


export function templateToSnippet(template: string): SnippetString {
// TODO: use append placeholder
let snippetIndex = 1;
template = template.replace(/^\s*(?=(\/|[ ]\*))/gm, '');
template = template.replace(/^(\/\*\*\s*\*[ ]*)$/m, (x) => x + `\$0`);
template = template.replace(/\* @param([ ]\{\S+\})?\s+(\S+)\s*$/gm, (_param, type, post) => {
let out = '* @param ';
if (type === ' {any}' || type === ' {*}') {
out += `{\$\{${snippetIndex++}:*\}} `;
} else if (type) {
out += type + ' ';
}
out += post + ` \${${snippetIndex++}}`;
return out;
});
return new SnippetString(template);
}
28 changes: 28 additions & 0 deletions extensions/typescript-language-features/src/test/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/

//
// PLEASE DO NOT MODIFY / DELETE UNLESS YOU KNOW WHAT YOU ARE DOING
//
// This file is providing the test runner to use when running extension tests.
// By default the test runner in use is Mocha based.
//
// You can provide your own test runner if you want to override it by exporting
// a function run(testRoot: string, clb: (error:Error) => void) that the extension
// host can call to run the tests. The test runner is expected to use console.log
// to report the results back to the caller. When the tests are finished, return
// a possible error to the callback or null if none.

const testRunner = require('vscode/lib/testrunner');

// You can directly control Mocha options by uncommenting the following lines
// See https://github.com/mochajs/mocha/wiki/Using-mocha-programmatically#set-options for more info
testRunner.configure({
ui: 'tdd', // the TDD UI is being used in extension.test.ts (suite, test, etc.)
useColors: process.platform !== 'win32', // colored output from test results (only windows cannot handle)
timeout: 60000
});

export = testRunner;
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/

import * as assert from 'assert';
import * as vscode from 'vscode';
import 'mocha';
import { templateToSnippet } from '../features/jsDocCompletionProvider';

suite('typescript.jsDocSnippet', () => {
test('Should do nothing for single line input', async () => {
const input = `/** */`;
assert.strictEqual(templateToSnippet(input).value, input);
});

test('Should put curosr inside multiline line input', async () => {
assert.strictEqual(
templateToSnippet([
'/**',
' * ',
' */'
].join('\n')).value,
[
'/**',
' * $0',
' */'
].join('\n'));
});
});

Loading

0 comments on commit 7b3c343

Please sign in to comment.