From e0ef0186dc44459e8757db6c20df6fb729c739e0 Mon Sep 17 00:00:00 2001 From: ThiagoDella Date: Thu, 24 Oct 2019 23:31:55 +0200 Subject: [PATCH] Add tests to cover all different strategies' function calls (class and function docstring) --- package.json | 6 ++- src/test/extension.test.ts | 91 +++++++++++++++++++++++++++++++------- 2 files changed, 79 insertions(+), 18 deletions(-) diff --git a/package.json b/package.json index 43a56c9..a676ab7 100644 --- a/package.json +++ b/package.json @@ -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" } } diff --git a/src/test/extension.test.ts b/src/test/extension.test.ts index a7a297f..2e9b1f1 100644 --- a/src/test/extension.test.ts +++ b/src/test/extension.test.ts @@ -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, "extractFunctionHeader"); + const spygenerateFuncDocstring = sinon.spy(Docstring.prototype, "generateFuncDocstring"); + const spygenerateParamDocstring = sinon.spy(Docstring.prototype, "generateParamDocstring"); + const spygenerateClassDocstring = sinon.spy(Docstring.prototype, "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(); }); -}); \ No newline at end of file + + 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, "extractFunctionHeader"); + const spygenerateFuncDocstring = sinon.spy(Docstring.prototype, "generateFuncDocstring"); + const spygenerateParamDocstring = sinon.spy(Docstring.prototype, "generateParamDocstring"); + const spygenerateClassDocstring = sinon.spy(Docstring.prototype, "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(); + }); +}); +