Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,12 @@
"test": "npm run compile && node ./node_modules/vscode/bin/test"
},
"devDependencies": {
"@types/mocha": "^2.2.42",
"@types/node": "^7.0.43",
"@types/mocha": "^5.2.6",
"tslint": "^5.8.0",
"typescript": "^2.6.1",
"vscode": "^1.1.36"
"vscode": "^1.1.36",
"mocha": "^6.1.4",
"sinon": "^7.5.0"
}
}
91 changes: 75 additions & 16 deletions src/test/extension.test.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,81 @@
//
// Note: This example test is leveraging the Mocha test framework.
// Please refer to their documentation on https://mochajs.org/ for help.
//

// The module 'assert' provides assertion methods from node
import * as assert from 'assert';
import { after, before, it, describe } from 'mocha';
import * as vscode from 'vscode';
import {Docstring} from '../generateDocString';

// You can import and use all API from the 'vscode' module
// as well as import your extension to test it
// import * as vscode from 'vscode';
// import * as myExtension from '../extension';
const path = require('path');
const sinon = require('sinon');
const fs = require('fs');

// Defines a Mocha test suite to group tests of similar kind together
suite("Extension Tests", function () {

// Defines a Mocha unit test
test("Something 1", function() {
assert.equal(-1, [1, 2, 3].indexOf(5));
assert.equal(-1, [1, 2, 3].indexOf(0));
const fileFunctionPath = __dirname + 'example_function.py'
const fileClassPath = __dirname + 'example_class.py'
const fileFunctionContent = `def test1(arg1, arg2):`;
const fileClassContent = `class test1():`;

describe('GenerateDocString file strategies tests', () => {
before(() => {
fs.writeFile(fileFunctionPath, fileFunctionContent, (err: string) => console.log(err));
fs.writeFile(fileClassPath, fileClassContent, (err: string) => console.log(err));
});
after(() => {
fs.unlinkSync(fileFunctionPath);
fs.unlinkSync(fileClassPath);
});

it('Should call Function extract strategy', async function() {
this.timeout(7000);

const document = await vscode.workspace.openTextDocument(fileFunctionPath);
const editor = await vscode.window.showTextDocument(document);
editor.selection.with(new vscode.Position(0,0), new vscode.Position(0, 22));
const pydoc = new Docstring(editor);

const spyExtractFunctionHeader = sinon.spy(Docstring.prototype, <any>"extractFunctionHeader");
const spygenerateFuncDocstring = sinon.spy(Docstring.prototype, <any>"generateFuncDocstring");
const spygenerateParamDocstring = sinon.spy(Docstring.prototype, <any>"generateParamDocstring");
const spygenerateClassDocstring = sinon.spy(Docstring.prototype, <any>"generateClassDocstring");

pydoc.createDocstring();

assert.ok(spyExtractFunctionHeader.called);
assert.ok(spygenerateFuncDocstring.called);
assert.ok(spygenerateParamDocstring.called);

assert.ok(!spygenerateClassDocstring.called);

spyExtractFunctionHeader.restore();
spygenerateClassDocstring.restore();
spygenerateParamDocstring.restore();
spygenerateFuncDocstring.restore();
});
});

it('Should call Class extract strategy', async function() {
this.timeout(7000);

const document = await vscode.workspace.openTextDocument(fileClassPath);
const editor = await vscode.window.showTextDocument(document);
editor.selection.with(new vscode.Position(0,0), new vscode.Position(0, 22));
const pydoc = new Docstring(editor);

const spyExtractFunctionHeader = sinon.spy(Docstring.prototype, <any>"extractFunctionHeader");
const spygenerateFuncDocstring = sinon.spy(Docstring.prototype, <any>"generateFuncDocstring");
const spygenerateParamDocstring = sinon.spy(Docstring.prototype, <any>"generateParamDocstring");
const spygenerateClassDocstring = sinon.spy(Docstring.prototype, <any>"generateClassDocstring");

pydoc.createDocstring();

assert.ok(spyExtractFunctionHeader.called);
assert.ok(spygenerateClassDocstring.called);

assert.ok(!spygenerateFuncDocstring.called);
assert.ok(!spygenerateParamDocstring.called);

spyExtractFunctionHeader.restore();
spygenerateClassDocstring.restore();
spygenerateParamDocstring.restore();
spygenerateFuncDocstring.restore();
});
});