Skip to content

Commit

Permalink
Code coverage - partly fixes #1495 (hyperledger-archives#2320)
Browse files Browse the repository at this point in the history
* updates so far

Signed-off-by: Matthew B White <[email protected]>

* updates

Signed-off-by: Matthew B White <[email protected]>

* more coverage

Signed-off-by: Matthew B White <[email protected]>

* fix typo

Signed-off-by: Matthew B White <[email protected]>
mbwhite authored Oct 9, 2017

Verified

This commit was signed with the committer’s verified signature.
jorenham Joren Hammudoglu
1 parent af5a59d commit f4df1e4
Showing 16 changed files with 1,393 additions and 77 deletions.
Original file line number Diff line number Diff line change
@@ -23,7 +23,6 @@ const Field = require('../../../introspect/field');
const RelationshipDeclaration = require('../../../introspect/relationshipdeclaration');
const EnumDeclaration = require('../../../introspect/enumdeclaration');
const EnumValueDeclaration = require('../../../introspect/enumvaluedeclaration');
const FunctionDeclaration = require('../../../introspect/functiondeclaration');
const util = require('util');

/**
@@ -61,8 +60,6 @@ class TypescriptVisitor {
return this.visitRelationship(thing, parameters);
} else if (thing instanceof EnumValueDeclaration) {
return this.visitEnumValueDeclaration(thing, parameters);
} else if (thing instanceof FunctionDeclaration) {
// return this.visitEnum(thing, parameters);
} else {
throw new Error('Unrecognised type: ' + typeof thing + ', value: ' + util.inspect(thing, { showHidden: true, depth: 2 }));
}
64 changes: 34 additions & 30 deletions packages/composer-common/lib/codegen/fromjs/plantumlgenerator.js
Original file line number Diff line number Diff line change
@@ -36,34 +36,34 @@ class PlantUMLGenerator {
*/
generate(program, file, includes, classes, functions) {
// generate the output
if(classes.length > 0 || functions.length > 0) {
if (classes.length > 0 ) {
let fileWriter = new FileWriter(program.outputDir);
const umlFilename = toUMLFilename(program.inputDir, program.outputDir, file);
const umlFilename = this.toUMLFilename(program.inputDir, program.outputDir, file);
console.log('open file: ' + umlFilename);
fileWriter.openFile(umlFilename);

fileWriter.writeLine(0, '@startuml');
console.log('+');
fileWriter.writeLine(0, '!include '+ program.outputDir + '/../../jsdoc-template/umlstyle.uml');
for(let n=0; n < includes.length; n++) {
fileWriter.writeLine(0, '!include ' + program.outputDir + '/../../jsdoc-template/umlstyle.uml');
for (let n = 0; n < includes.length; n++) {
// only include files that exist
// the file may not exist if it was empty
const includeFile = program.outputDir + '/' + includes[n] + '.uml';
if (fs.existsSync(includeFile)) {
fileWriter.writeLine(0, '!include ' + includeFile);
}
}
for(let n=0; n < classes.length; n++) {
for (let n = 0; n < classes.length; n++) {
const clazz = classes[n];
fileWriter.writeLine(0, 'class ' + clazz.name + '{');
for(let i=0; i < clazz.methods.length; i++) {
for (let i = 0; i < clazz.methods.length; i++) {
const method = clazz.methods[i];
fileWriter.writeLine(1, method.visibility + ' ' + method.returnType + ' ' + method.name + paramsToString(method.methodArgs) );
fileWriter.writeLine(1, method.visibility + ' ' + method.returnType + ' ' + method.name + this.paramsToString(method.methodArgs));
}
fileWriter.writeLine(0, '}');
if(clazz.superClass) {
if (clazz.superClass) {
const filePath = path.parse(file);
fileWriter.writeBeforeLine(0, '!include ' + filePath.dir + '/' + clazz.superClass.toLowerCase() + '.uml');
fileWriter.writeBeforeLine(0, '!include ' + filePath.dir + '/' + clazz.superClass.toLowerCase() + '.uml');
fileWriter.writeLine(0, clazz.name + ' --|> ' + clazz.superClass);
}
}
@@ -72,26 +72,27 @@ class PlantUMLGenerator {
fileWriter.closeFile();
}
}
}

/**


/**
* Converts an array of parameter types to a string
* @param {string[]} paramTypes array of parameter type names
* @return {string} - string representation
* @private
*/
function paramsToString(paramTypes) {
let result = '(';
for(let n=0; n < paramTypes.length; n++) {
result += paramTypes[n];
if(n < paramTypes.length-1) {
result += ',';
paramsToString(paramTypes) {
let result = '(';
for (let n = 0; n < paramTypes.length; n++) {
result += paramTypes[n];
if (n < paramTypes.length - 1) {
result += ',';
}
}
}

result += ')';
return result;
}
result += ')';
return result;
}

/**
* @param {string} inputDir - the fully qualified input directory
@@ -100,18 +101,21 @@ function paramsToString(paramTypes) {
* @return {string} the UML file name to use
* @private
*/
function toUMLFilename(inputDir, outputDir, filename) {
console.log('inputDir' + inputDir);
console.log('outputDir' + outputDir);
console.log('filename' + filename);
toUMLFilename(inputDir, outputDir, filename) {
console.log('inputDir' + inputDir);
console.log('outputDir' + outputDir);
console.log('filename' + filename);

let index = filename.indexOf(inputDir);
console.log('index ' + index);
let rest = filename.substr(index + 1 + inputDir.length);
let index = filename.indexOf(inputDir);
console.log('index ' + index);
let rest = filename.substr(index + 1 + inputDir.length);
// let out = outputDir + '/' + rest;
let i = rest.lastIndexOf('.');
let i = rest.lastIndexOf('.');
// console.log('result' + out.substr(0, i) + '.uml' );
return (i < 0) ? '' : rest.substr(0, i) + '.uml';
return (i < 0) ? '' : rest.substr(0, i) + '.uml';
}
}



module.exports = PlantUMLGenerator;
35 changes: 0 additions & 35 deletions packages/composer-common/lib/codegen/jsonwriter.js
Original file line number Diff line number Diff line change
@@ -46,7 +46,6 @@ class JSONWriter extends Writer {
this.write('{');
this.indent++;
this.firstItem = true;
this.log('{');
}

/**
@@ -56,7 +55,6 @@ class JSONWriter extends Writer {
this.write('}');
this.indent--;
this.firstItem = false;
this.log('}');
}

/**
@@ -67,7 +65,6 @@ class JSONWriter extends Writer {
writeKey(key) {
this.writeComma();
this.write(JSON.stringify(key) + ':');
this.log('writeKey', key );
}

/**
@@ -76,10 +73,6 @@ class JSONWriter extends Writer {
writeComma() {
if(this.firstItem === false) {
this.write(',');
this.log(',');
}
else {
this.log(', (suppressed)');
}
}

@@ -91,7 +84,6 @@ class JSONWriter extends Writer {
writeStringValue(value) {
this.write(JSON.stringify(value));
this.firstItem = false;
this.log('writeStringValue', value);
}

/**
@@ -102,7 +94,6 @@ class JSONWriter extends Writer {
writeValue(value) {
this.write(`${value}`);
this.firstItem = false;
this.log('writeValue', value);
}

/**
@@ -115,7 +106,6 @@ class JSONWriter extends Writer {
this.writeKey(key);
this.writeStringValue(value);
this.firstItem = false;
this.log('writeKeyStringValue', (key + ':' + value));
}

/**
@@ -129,7 +119,6 @@ class JSONWriter extends Writer {
this.writeKey(key);
this.writeValue(value);
this.firstItem = false;
this.log('writeKeyValue', key + '=' + value);
}

/**
@@ -141,7 +130,6 @@ class JSONWriter extends Writer {
this.writeComma();
this.writeStringValue(value);
this.firstItem = false;
this.log('writeArrayStringValue', value);
}

/**
@@ -153,7 +141,6 @@ class JSONWriter extends Writer {
this.writeComma();
this.write(`${value}`);
this.firstItem = false;
this.log('writeArrayValue', value);
}

/**
@@ -163,7 +150,6 @@ class JSONWriter extends Writer {
this.write('[');
this.indent++;
this.firstItem = true;
this.log('[');
}

/**
@@ -173,7 +159,6 @@ class JSONWriter extends Writer {
this.write(']');
this.indent--;
this.firstItem = false;
this.log(']');
}

/**
@@ -183,26 +168,6 @@ class JSONWriter extends Writer {
return 'indent ' + this.indent + ' firstItem ' + this.firstItem;
}

/**
* @param {String} callSite - the location
* @param {String} message - the message to log.
*/
log(callSite, message) {
const log = false;
if(log) {
let spaces = '';
for(let n=0; n < this.indent; n++) {
spaces += ' ';
}

if(!message) {
message = '';
}

console.log(spaces + ' ' + message + ' ' + callSite );
}
}

/**
* Empties the underyling buffer and resets the line count.
*/
9 changes: 3 additions & 6 deletions packages/composer-common/package.json
Original file line number Diff line number Diff line change
@@ -19,7 +19,8 @@
"postdoc": "npm run browserify",
"browserify": "browserify ./index.js -t [ babelify --presets [ latest ] ] > ./out/composer-common.js",
"test": "node ./scripts/api-changelog.js && nyc mocha --recursive -t 10000",
"mocha": "nyc mocha --recursive -t 10000",
"mocha": "mocha --recursive -t 10000",
"nyc" :"nyc mocha --recursive -t 10000",
"regenmodel": "node ./scripts/systemmodelgen.js"
},
"repository": {
@@ -96,11 +97,7 @@
"nyc": {
"exclude": [
"coverage/**",
"lib/codegen/*",
"lib/codegen/fromcto/golang/**",
"lib/codegen/fromcto/plantuml/**",
"lib/codegen/fromcto/typescript/**",
"lib/codegen/fromjs/**",
"lib/codegen/**",
"lib/tools/changelog.js",
"lib/tools/plantumltoimage.js",
"lib/introspect/parser.js",
42 changes: 42 additions & 0 deletions packages/composer-common/test/codegen/_template.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

'use strict';

const chai = require('chai');
chai.should();
chai.use(require('chai-as-promised'));
chai.use(require('chai-things'));
const sinon = require('sinon');


describe('ClassUndertest', function () {

let sandbox;

beforeEach(() => {
sandbox = sinon.sandbox.create();
});

afterEach(() => {
sandbox.restore();
});

describe('#isPrimitiveType', function() {
it('check isPrimitiveType', function() {

});
});

});
214 changes: 214 additions & 0 deletions packages/composer-common/test/codegen/filewriter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,214 @@
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

'use strict';

const chai = require('chai');
const should = chai.should();
chai.use(require('chai-as-promised'));
chai.use(require('chai-things'));
const sinon = require('sinon');

const FileWriter = require('../../lib/codegen/filewriter');

const fs = require('fs');
const mkdirp = require('mkdirp');
const path = require('path');
const Writer = require('../../lib/codegen/writer');

describe('ClassUndertest', function () {

let sandbox;

beforeEach(() => {
sandbox = sinon.sandbox.create();
});

afterEach(() => {
sandbox.restore();
});

describe('#constructor', function () {
it('main code path', function () {
let syncStub = sandbox.stub(mkdirp, 'sync');
syncStub.returns();

let fileWriter = new FileWriter('dir');
should.exist(fileWriter);
// sinon.assert.exists(fileWriter);
sinon.assert.calledOnce(syncStub);
sinon.assert.calledWith(syncStub, 'dir');

});
});

describe('#openFile', function () {
it('main code path', function () {

let fileWriter = new FileWriter('dir');
should.exist(fileWriter);
fileWriter.openFile('filename');
fileWriter.fileName.should.equal('filename');
should.not.exist(fileWriter.relativeDir);
});
});

describe('#openRelativeFile', function () {
it('main code path', function () {
let fileWriter = new FileWriter('dir');
should.exist(fileWriter);
fileWriter.openRelativeFile('relativeDir', 'filename');
fileWriter.fileName.should.equal('filename');
fileWriter.relativeDir.should.equal('relativeDir');
});
});

describe('#writeLine', function () {
it('file not opened error code path', function () {
let stub = sandbox.stub(Writer, 'writeLine');
stub.returns();
let fileWriter = new FileWriter('dir');
should.exist(fileWriter);
(() => {
fileWriter.writeLine('tabs', 'text');
}).should.throws(/not been opened/);

});

it('main code path', function () {
let stub = sandbox.stub(Writer.prototype, 'writeLine');
stub.returns();
let fileWriter = new FileWriter('dir');
fileWriter.fileName='filename';
should.exist(fileWriter);

fileWriter.writeLine('tabs', 'text');

sinon.assert.calledWith(stub,'tabs','text');
});
});

describe('#writeBeforeLine', function () {
it('file not opened error code path', function () {
let stub = sandbox.stub(Writer, 'writeBeforeLine');
stub.returns();
let fileWriter = new FileWriter('dir');
should.exist(fileWriter);
(() => {
fileWriter.writeBeforeLine('tabs', 'text');
}).should.throws(/not been opened/);
});

it('main code path', function () {
let stub = sandbox.stub(Writer.prototype, 'writeBeforeLine');
stub.returns();
let fileWriter = new FileWriter('dir');
fileWriter.fileName='filename';
should.exist(fileWriter);

fileWriter.writeBeforeLine('tabs', 'text');

sinon.assert.calledWith(stub,'tabs','text');
});
});

describe('#closeFile', function () {
it('file not opened error code path', function () {
let fileWriter = new FileWriter('dir');
should.exist(fileWriter);
(() => {
fileWriter.closeFile();
}).should.throws(/No file open/);
});

it('main code path', function () {
let pathResolveStub = sandbox.stub(path, 'resolve');
pathResolveStub.returns('resolvedpath');

let pathdirnameStub = sandbox.stub(path, 'dirname');
pathdirnameStub.returns();

let fsWriteFileSyncStub = sandbox.stub(fs, 'writeFileSync');
fsWriteFileSyncStub.returns();

let syncStub = sandbox.stub(mkdirp, 'sync');
syncStub.returns();

let superClearBuffer = sandbox.stub(Writer.prototype, 'clearBuffer');
superClearBuffer.returns();

let superGetBuffer = sandbox.stub(Writer.prototype, 'getBuffer');
superGetBuffer.returns([0,1,2,3]);

// ---
let fileWriter = new FileWriter('dir');
should.exist(fileWriter);
fileWriter.fileName='filename';
fileWriter.outputDirectory='outputDir';

fileWriter.closeFile();

sinon.assert.calledOnce(pathResolveStub);
sinon.assert.calledWith(pathResolveStub,'outputDir','filename');
sinon.assert.calledOnce(pathdirnameStub);
sinon.assert.calledWith(pathdirnameStub,'resolvedpath');
sinon.assert.calledOnce(fsWriteFileSyncStub);
sinon.assert.calledWith(fsWriteFileSyncStub,'resolvedpath',[0,1,2,3]);
sinon.assert.calledTwice(syncStub);
sinon.assert.calledOnce(superClearBuffer);


});
it('main code path - relativedir', function () {
let pathResolveStub = sandbox.stub(path, 'resolve');
pathResolveStub.returns('resolvedpath');

let pathdirnameStub = sandbox.stub(path, 'dirname');
pathdirnameStub.returns();

let fsWriteFileSyncStub = sandbox.stub(fs, 'writeFileSync');
fsWriteFileSyncStub.returns();

let syncStub = sandbox.stub(mkdirp, 'sync');
syncStub.returns();

let superClearBuffer = sandbox.stub(Writer.prototype, 'clearBuffer');
superClearBuffer.returns();

let superGetBuffer = sandbox.stub(Writer.prototype, 'getBuffer');
superGetBuffer.returns([0,1,2,3]);

// ---
let fileWriter = new FileWriter('dir');
should.exist(fileWriter);
fileWriter.fileName='filename';
fileWriter.outputDirectory='outputDir';
fileWriter.relativeDir = 'relativeDir';

fileWriter.closeFile();

sinon.assert.calledTwice(pathResolveStub);
sinon.assert.calledWith(pathResolveStub,'outputDir','relativeDir');
sinon.assert.calledWith(pathResolveStub,'resolvedpath','filename');
sinon.assert.calledOnce(pathdirnameStub);
sinon.assert.calledWith(pathdirnameStub,'resolvedpath');
sinon.assert.calledOnce(fsWriteFileSyncStub);
sinon.assert.calledWith(fsWriteFileSyncStub,'resolvedpath',[0,1,2,3]);
sinon.assert.calledTwice(syncStub);
sinon.assert.calledOnce(superClearBuffer);


});
});
});
109 changes: 109 additions & 0 deletions packages/composer-common/test/codegen/fromjs/apisignaturegenerator.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

'use strict';

const chai = require('chai');
chai.should();
chai.use(require('chai-as-promised'));
chai.use(require('chai-things'));
const sinon = require('sinon');

const fs = require('fs');
const APISignatureGenerator = require('../../../lib/codegen/fromjs/apisignaturegenerator');
const Writer = require('../../../lib/codegen/writer');

describe('APISignatureGenerator', function () {

let sandbox;

beforeEach(() => {
sandbox = sinon.sandbox.create();
});

afterEach(() => {
sandbox.restore();
});

describe('#generate', function() {
it('Good path', function() {
let apigen = new APISignatureGenerator();

let appendFileSync = sandbox.stub(fs, 'appendFileSync');
appendFileSync.returns;

let writeLine = sandbox.stub(Writer.prototype,'writeLine');
writeLine.returns;

let program = {outdir:'outdir'};
let file = 'not used';
let includes = [];
let classes = [
{
methods:[
{name:'methodname1',visibility:'public',methodArgs:['string','int'],throws:'error'},
{name:'methodname2',visibility:'public',methodArgs:['string','int']}
],
name:'aclass'
},
{
methods:[
{name:'methodname1',visibility:'public',methodArgs:['string','int'],throws:'error'},
{name:'methodname2',visibility:'public',methodArgs:['string','int']}
],
name:'asubclass',
superClass :'aclass'
}
];
let functions = [
{
throws:'error',
visibility:'private',
methodArgs:['string','int'],
name:'func1',
returnType:'int'
},
{
visibility:'private',
methodArgs:['string','int'],
name:'func2',
returnType:'int'
}];
apigen.generate(program,file,includes,classes,functions);
sinon.assert.callCount(writeLine,10);


});

it('nothing to do', function() {
let apigen = new APISignatureGenerator();

let appendFileSync = sandbox.stub(fs, 'appendFileSync');
appendFileSync.returns;

let writeLine = sandbox.stub(Writer,'writeLine');
writeLine.returns;

let program = {outdir:'outdir'};
let file = 'not used';
let includes = [];
let classes = [];
let functions = [];
apigen.generate(program,file,includes,classes,functions);
sinon.assert.notCalled.appendFileSync;
sinon.assert.notCalled.writeLine;
});
});

});
144 changes: 144 additions & 0 deletions packages/composer-common/test/codegen/fromjs/jsonsignaturegenerator.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

'use strict';

const chai = require('chai');
chai.should();
chai.use(require('chai-as-promised'));
chai.use(require('chai-things'));
const sinon = require('sinon');

const fs = require('fs');
const JSONGenerator = require('../../../lib/codegen/fromjs/jsongenerator');
const path = require('path');

describe('JSONGenerator', function () {

let sandbox;

beforeEach(() => {
sandbox = sinon.sandbox.create();
});

afterEach(() => {
sandbox.restore();
});

describe('#generate', function() {
it('Good path', function() {
let apigen = new JSONGenerator();

let writeFileSync = sandbox.stub(fs, 'writeFileSync');
writeFileSync.returns;

let pathResolve = sandbox.stub(path,'resolve');
pathResolve.returns();

let pathParse = sandbox.stub(path,'parse');
pathParse.returns({name:'filename'});


let program = {outdir:'outdir'};
let file = 'not used';
let includes = [];
let classes = [
{
methods:[
{name:'methodname1',visibility:'public',methodArgs:['string','int'],throws:'error',
commentData: {description:'a description', tags:[

]}
},
{
name:'methodname2',
visibility:'public',
methodArgs:['string','int'],
commentData:
{
description:'a description',
tags:[
{

title:'param',
description:'superClass',
type:
{ name:'name',
type:'OptionalType',
expression: {name:'expressionnanme'}
}
},
{

title:'param',
description:'superClass',
type:
{ name:'name',

expression: {name:'expressionnanme'}
}
},
{

title:'return',
description:'returns',
type:
{ name:'name',

expression: {name:'expressionnanme'}
}
},
{

title:'whatever'
}]
}
}],
name:'aclass',
commentData: {description:'a class description', tags:[
{title:'extends',name:'superClass'},
{title:'see',description:'[Registry]{@link module:composer-client.Registry}'},
{title:'memberof',description:'module'},
{title:'private'},
{title:'protected'},
{title:'whatever'}

]}
}
];
let functions = [];
apigen.generate(program,file,includes,classes,functions);
sinon.assert.callCount(writeFileSync,1);


});

it('nothing to do', function() {
let apigen = new JSONGenerator();

let writeFileSync = sandbox.stub(fs, 'writeFileSync');
writeFileSync.returns;

let program = {outdir:'outdir'};
let file = 'not used';
let includes = [];
let classes = [];
let functions = [];
apigen.generate(program,file,includes,classes,functions);
sinon.assert.notCalled.appendFileSync;
sinon.assert.notCalled.writeLine;
});
});

});
274 changes: 274 additions & 0 deletions packages/composer-common/test/codegen/fromjs/platumlgenerator.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,274 @@
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

'use strict';

const chai = require('chai');
chai.should();
chai.use(require('chai-as-promised'));
chai.use(require('chai-things'));
const sinon = require('sinon');

const fs = require('fs');
const PlantUMLGenerator = require('../../../lib/codegen/fromjs/plantumlgenerator');
const path = require('path');
const FileWriter = require('../../../lib/codegen/filewriter');
const mkdirp = require('mkdirp');

describe('PlantUMLGenerator', function () {

let sandbox;



describe('#toUMLFilename', function() {
it('good path',function() {
let apigen = new PlantUMLGenerator();
let filename = apigen.toUMLFilename('inputDir','outputDir','inputDir/filename.js');
filename.should.equal('filename.uml');
});

it('missing extension',function() {
let apigen = new PlantUMLGenerator();
let filename = apigen.toUMLFilename('inputDir','outputDir','inputDir/filename');
filename.should.equal('');
});
});

describe('#generate', function() {
beforeEach(() => {
sandbox = sinon.sandbox.create();
});

afterEach(() => {
sandbox.restore();
});
it('Good path', function() {
sandbox.stub(mkdirp,'sync').returns;

let fwOpenStub = sandbox.stub(FileWriter.prototype,'openFile');
fwOpenStub.returns;

let fwWriteLine = sandbox.stub(FileWriter.prototype,'writeLine');
fwWriteLine.returns;

let fwCloseFile = sandbox.stub(FileWriter.prototype,'closeFile');
fwCloseFile.returns;

let fwWriteBeforeLine = sandbox.stub(FileWriter.prototype,'writeBeforeLine');
fwWriteBeforeLine.returns;

let existsSync = sandbox.stub(fs, 'existsSync');
existsSync.returns(true);

let pathResolve = sandbox.stub(path,'resolve');
pathResolve.returns();

let pathParse = sandbox.stub(path,'parse');
pathParse.returns({name:'filename'});

let stub = sandbox.stub(PlantUMLGenerator.prototype,'toUMLFilename');
stub.returns('nameoffile');

let apigen = new PlantUMLGenerator();

let program = {outdir:'outdir',indir:'indir',file:'file'};
let file = 'not used';
let includes = ['includeFile'];
let classes = [
{
superClass : 'ASuperClass',
methods:[
{name:'methodname1',visibility:'public',methodArgs:['string','int'],throws:'error',
commentData: {description:'a description', tags:[

]}
},
{
name:'methodname2',
visibility:'public',
methodArgs:['string','int'],
commentData:
{
description:'a description',
tags:[
{

title:'param',
description:'superClass',
type:
{ name:'name',
type:'OptionalType',
expression: {name:'expressionnanme'}
}
},
{

title:'param',
description:'superClass',
type:
{ name:'name',

expression: {name:'expressionnanme'}
}
},
{

title:'return',
description:'returns',
type:
{ name:'name',

expression: {name:'expressionnanme'}
}
},
{

title:'whatever'
}]
}
}],
name:'aclass',
commentData: {description:'a class description', tags:[
{title:'extends',name:'superClass'},
{title:'see',description:'[Registry]{@link module:composer-client.Registry}'},
{title:'memberof',description:'module'},
{title:'private'},
{title:'protected'},
{title:'whatever'}

]}
}
];
let functions = [];
apigen.generate(program,file,includes,classes,functions);


});
it('Else conditions for the list of functions', function() {
sandbox.stub(mkdirp,'sync').returns;

let fwOpenStub = sandbox.stub(FileWriter.prototype,'openFile');
fwOpenStub.returns;

let fwWriteLine = sandbox.stub(FileWriter.prototype,'writeLine');
fwWriteLine.returns;

let fwCloseFile = sandbox.stub(FileWriter.prototype,'closeFile');
fwCloseFile.returns;

let fwWriteBeforeLine = sandbox.stub(FileWriter.prototype,'writeBeforeLine');
fwWriteBeforeLine.returns;

let existsSync = sandbox.stub(fs, 'existsSync');
existsSync.returns(false);

let pathResolve = sandbox.stub(path,'resolve');
pathResolve.returns();

let pathParse = sandbox.stub(path,'parse');
pathParse.returns({name:'filename'});

let stub = sandbox.stub(PlantUMLGenerator.prototype,'toUMLFilename');
stub.returns('nameoffile');

let apigen = new PlantUMLGenerator();

let program = {outdir:'outdir',indir:'indir',file:'file'};
let file = 'not used';
let includes = ['includeFile'];
let classes = [
{
methods:[
{name:'methodname1',visibility:'public',methodArgs:['string','int'],throws:'error',
commentData: {description:'a description', tags:[

]}
},
{
name:'methodname2',
visibility:'public',
methodArgs:['string','int'],
commentData:
{
description:'a description',
tags:[
{

title:'param',
description:'superClass',
type:
{ name:'name',
type:'OptionalType',
expression: {name:'expressionnanme'}
}
},
{

title:'param',
description:'superClass',
type:
{ name:'name',

expression: {name:'expressionnanme'}
}
},
{

title:'return',
description:'returns',
type:
{ name:'name',

expression: {name:'expressionnanme'}
}
},
{

title:'whatever'
}]
}
}],
name:'aclass',
commentData: {description:'a class description', tags:[
{title:'extends',name:'superClass'},
{title:'see',description:'[Registry]{@link module:composer-client.Registry}'},
{title:'memberof',description:'module'},
{title:'private'},
{title:'protected'},
{title:'whatever'}

]}
}
];
let functions = [];
apigen.generate(program,file,includes,classes,functions);


});

it('No input',function() {
let apigen = new PlantUMLGenerator();

let program = {outdir:'outdir',indir:'indir',file:'file'};
let file = 'not used';
let includes = ['includeFile'];
let classes = [];
let functions= [];
apigen.generate(program,file,includes,classes,functions);
});

});

});
30 changes: 29 additions & 1 deletion packages/composer-common/test/codegen/golangvisitor.js
Original file line number Diff line number Diff line change
@@ -18,9 +18,14 @@ require('chai').should();
const ModelManager = require('../../lib/modelmanager');
const GoLangVisitor = require('../../lib/codegen/fromcto/golang/golangvisitor');
const FileWriter = require('../../lib/codegen/filewriter');
const BusinessNetworkDefinition = require('../../lib/businessnetworkdefinition');

const fs = require('fs');
const path = require('path');
const chai = require('chai');
chai.should();
chai.use(require('chai-as-promised'));
chai.use(require('chai-things'));
const sinon = require('sinon');

describe('GoLangVisitor', function(){
@@ -47,10 +52,33 @@ describe('GoLangVisitor', function(){
let parameters = {};
parameters.fileWriter = mockFileWriter;
modelManager.accept(visitor, parameters);

BusinessNetworkDefinition;
sinon.assert.calledWith(mockFileWriter.openFile, 'composer.go');
sinon.assert.calledWith(mockFileWriter.openFile, 'main.go');
sinon.assert.calledWith(mockFileWriter.openFile, 'orgacme.go');
});

it('coverage for business network definition',function(){
let mockBND = sinon.createStubInstance(BusinessNetworkDefinition);
let mockModelManager = sinon.createStubInstance(ModelManager);
mockBND.getModelManager.returns(mockModelManager);
let fakeObj = {accept: function(){}};


mockModelManager.getModelFiles.returns([fakeObj]);
let visitor = new GoLangVisitor();
visitor.visit(mockBND,{});
});

it('coverage for business network definition',function(){

let fakeObj = {accept: function(){}};
let visitor = new GoLangVisitor();
(()=>{
visitor.visit(fakeObj,{});
})
.should.throw(/Unrecognised type/);
});

});
});
346 changes: 344 additions & 2 deletions packages/composer-common/test/codegen/javascriptparser.js

Large diffs are not rendered by default.

17 changes: 17 additions & 0 deletions packages/composer-common/test/codegen/jsonwriter.js
Original file line number Diff line number Diff line change
@@ -86,4 +86,21 @@ describe('JSONWriter', () => {

});

describe('#toString', () => {
it('should write out a string', () => {
writer.writeArrayStringValue('hello world');
writer.getBuffer().should.equal('"hello world"');
writer.toString().should.equal('indent 0 firstItem false');

});
});

describe('#toString', () => {
it('should write out a string', () => {
writer.writeArrayStringValue('hello world');
writer.getBuffer().should.equal('"hello world"');
writer.toString().should.equal('indent 0 firstItem false');

});
});
});
42 changes: 42 additions & 0 deletions packages/composer-common/test/codegen/parsejs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

'use strict';

const chai = require('chai');
chai.should();
chai.use(require('chai-as-promised'));
chai.use(require('chai-things'));
const sinon = require('sinon');


describe('parsejs', function () {

let sandbox;

beforeEach(() => {
sandbox = sinon.sandbox.create();
});

afterEach(() => {
sandbox.restore();
});

describe('#maintest', function() {
it('main path', function() {
// require('../../lib/codegen/parsejs.js');
});
});

});
11 changes: 11 additions & 0 deletions packages/composer-common/test/codegen/plantumlvisitor.js
Original file line number Diff line number Diff line change
@@ -44,5 +44,16 @@ describe('PlantUMLVisitor', function(){
// check the file exists
fs.accessSync('./out/animaltracking/model.uml', fs.F_OK);
});


it('coverage for random object',function(){

let fakeObj = {accept: function(){}};
let visitor = new PlantUMLVisitor();
(()=>{
visitor.visit(fakeObj,{});
})
.should.throw(/Unrecognised/);
});
});
});
41 changes: 41 additions & 0 deletions packages/composer-common/test/codegen/typescriptvisitor.js
Original file line number Diff line number Diff line change
@@ -21,7 +21,12 @@ const FileWriter = require('../../lib/codegen/filewriter');

const fs = require('fs');
const path = require('path');
const chai = require('chai');
chai.should();
chai.use(require('chai-as-promised'));
chai.use(require('chai-things'));
const sinon = require('sinon');
const BusinessNetworkDefinition = require('../../lib/businessnetworkdefinition');

const initSampleNetworkModel = (mockFileWriter) => {
const carleaseModel = fs.readFileSync(path.resolve(__dirname, '../data/model/carlease.cto'), 'utf8');
@@ -62,5 +67,41 @@ describe('TypescriptVisitor', function(){
// check import was generated linking to the other file/namespace
sinon.assert.calledWith(mockFileWriter.writeLine, 0 , 'import {MyParticipant} from \'./composer\';');
});

it('coverage for business network definition',function(){
let mockBND = sinon.createStubInstance(BusinessNetworkDefinition);
let mockModelManager = sinon.createStubInstance(ModelManager);
mockBND.getModelManager.returns(mockModelManager);
let fakeObj = {accept: function(){}};


mockModelManager.getModelFiles.returns([fakeObj]);
let visitor = new TypescriptVisitor();
visitor.visit(mockBND,{});
});

it('coverage for random object',function(){

let fakeObj = {accept: function(){}};
let visitor = new TypescriptVisitor();
(()=>{
visitor.visit(fakeObj,{});
})
.should.throw(/Unrecognised type/);
});


it('coverage toTsType',function(){

let visitor = new TypescriptVisitor();
visitor.toTsType('Long').should.equal('number');
visitor.toTsType('Integer').should.equal('number');
visitor.toTsType('Double').should.equal('number');
visitor.toTsType('String').should.equal('string');
visitor.toTsType('Boolean').should.equal('boolean');
visitor.toTsType('DateTime').should.equal('Date');
visitor.toTsType('whatever').should.equal('whatever');
});

});
});
89 changes: 89 additions & 0 deletions packages/composer-common/test/codegen/writer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

'use strict';

const chai = require('chai');
const should = chai.should();
chai.use(require('chai-as-promised'));
chai.use(require('chai-things'));
const sinon = require('sinon');
const Writer = require('../../lib/codegen/writer');

describe('Writer', function () {

let sandbox;

beforeEach(() => {
sandbox = sinon.sandbox.create();
});

afterEach(() => {
sandbox.restore();
});

describe('#constructor', function() {
it('main path', function() {
let writer = new Writer();
should.exist(writer);
writer.clearBuffer();

});
});

describe('#writeBeforeLine', function() {
it('main path', function() {
let writer = new Writer();
should.exist(writer);
writer.writeBeforeLine(1,'Hello World');
writer.beforeBuffer.should.equal(' Hello World\n');
writer.linesWritten.should.equal(1);
writer.getLineCount().should.equal(1);
});
});


describe('#writeLine', function() {
it('main path', function() {
let writer = new Writer();
should.exist(writer);
writer.writeLine(1,'Hello World');
writer.buffer.should.equal(' Hello World\n');
writer.linesWritten.should.equal(5);
writer.getBuffer().should.equal(' Hello World\n');
});
});
describe('#writeIndented', function() {
it('main path', function() {
let writer = new Writer();
should.exist(writer);
writer.writeIndented(1,'Hello World');

writer.linesWritten.should.equal(2);
writer.getBuffer().should.equal(' Hello World');
});
});

describe('#write', ()=>{
it('writes a line that is not a string', ()=>{
(()=>{
let writer = new Writer();
should.exist(writer);
writer.write(false);
}).should.throws(/Can only append strings/);

});

});
});

0 comments on commit f4df1e4

Please sign in to comment.