yadop
Version:
Yet another Doc Parser
151 lines • 7.21 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
var fs = require("fs-extra");
var glob = require("glob");
var sinon = require("sinon");
var processor_1 = require("./processor");
var espree = require("espree");
var doctrine = require("doctrine");
var path = require("path");
(function () {
describe('processor', function () {
var globSyncFn;
var fsReadFileSyncFn;
var espreeParseFn;
var doctrineParseFn;
var configuration;
var processor;
var result;
var code = 'code';
beforeEach(function () {
globSyncFn = sinon.stub(glob, 'sync').callsFake(function () { return [
'one', 'two', 'three'
]; });
fsReadFileSyncFn = sinon.stub(fs, 'readFileSync').callsFake(function (file) { return file + '-content'; });
espreeParseFn = sinon.stub(espree, 'parse');
espreeParseFn.onCall(0).returns({ comments: [{ value: 'one' }] });
espreeParseFn.onCall(1).returns({ comments: [{ value: 'two' }, { value: 'another two' }] });
espreeParseFn.onCall(2).returns({ comments: [{ value: 'three' }] });
doctrineParseFn = sinon.stub(doctrine, 'parse');
doctrineParseFn.callsFake(function (comment, options) { return ({
description: comment + '-doctrine',
tags: [{ title: 'param', 'description': 'some param' }]
}); });
});
describe('process', function () {
describe('provided with configuration options', function () {
beforeEach(function () {
configuration = {
cwd: 'cwd',
pattern: 'pattern',
ignore: ['ignore']
};
processor = new processor_1.default(configuration);
result = processor.process();
});
it('processes the code from the provided configuration options', function () {
sinon.assert.calledWith(globSyncFn, 'pattern', {
cwd: 'cwd',
ignore: ['ignore'],
nodir: true,
nosort: true
});
});
it('processes each file using espree', function () {
sinon.assert.calledWith(espreeParseFn, path.join('cwd', 'one-content'));
sinon.assert.calledWith(espreeParseFn, path.join('cwd', 'two-content'));
sinon.assert.calledWith(espreeParseFn, path.join('cwd', 'three-content'));
});
it('processes each comment using doctrine', function () {
sinon.assert.calledWith(doctrineParseFn, 'one');
sinon.assert.calledWith(doctrineParseFn, 'two');
sinon.assert.calledWith(doctrineParseFn, 'another two');
sinon.assert.calledWith(doctrineParseFn, 'three');
});
it('returns the doctrine.Annotations', function () {
expect(result.length).toBe(4);
expect(result[0]).toEqual({
description: 'one-doctrine',
tags: [{ title: 'param', 'description': 'some param' }]
});
expect(result[1]).toEqual({
description: 'two-doctrine',
tags: [{ title: 'param', 'description': 'some param' }]
});
expect(result[2]).toEqual({
description: 'another two-doctrine',
tags: [{ title: 'param', 'description': 'some param' }]
});
expect(result[3]).toEqual({
description: 'three-doctrine',
tags: [{ title: 'param', 'description': 'some param' }]
});
});
afterEach(function () {
globSyncFn.reset();
fsReadFileSyncFn.reset();
espreeParseFn.reset();
doctrineParseFn.reset();
});
});
describe('provided without configuration options', function () {
beforeEach(function () {
configuration = {};
processor = new processor_1.default(configuration);
result = processor.process();
});
it('processes the code from the default configuration options', function () {
sinon.assert.calledWith(globSyncFn, '**/*.js', {
cwd: process.cwd(),
ignore: [],
nodir: true,
nosort: true
});
});
it('processes each file using espree', function () {
sinon.assert.calledWith(espreeParseFn, path.join(process.cwd(), 'one-content'));
sinon.assert.calledWith(espreeParseFn, path.join(process.cwd(), 'two-content'));
sinon.assert.calledWith(espreeParseFn, path.join(process.cwd(), 'three-content'));
});
it('processes each comment using doctrine', function () {
sinon.assert.calledWith(doctrineParseFn, 'one');
sinon.assert.calledWith(doctrineParseFn, 'two');
sinon.assert.calledWith(doctrineParseFn, 'another two');
sinon.assert.calledWith(doctrineParseFn, 'three');
});
it('returns the doctrine.Annotations', function () {
expect(result.length).toBe(4);
expect(result[0]).toEqual({
description: 'one-doctrine',
tags: [{ title: 'param', 'description': 'some param' }]
});
expect(result[1]).toEqual({
description: 'two-doctrine',
tags: [{ title: 'param', 'description': 'some param' }]
});
expect(result[2]).toEqual({
description: 'another two-doctrine',
tags: [{ title: 'param', 'description': 'some param' }]
});
expect(result[3]).toEqual({
description: 'three-doctrine',
tags: [{ title: 'param', 'description': 'some param' }]
});
});
afterEach(function () {
globSyncFn.reset();
fsReadFileSyncFn.reset();
espreeParseFn.reset();
doctrineParseFn.reset();
});
});
});
afterEach(function () {
globSyncFn.restore();
fsReadFileSyncFn.restore();
espreeParseFn.restore();
doctrineParseFn.restore();
});
});
})();
//# sourceMappingURL=processor.spec.js.map