From 6ff27095bb1dc641b2f91718b395d948a7a77312 Mon Sep 17 00:00:00 2001 From: himetani_cafe Date: Fri, 22 Jan 2016 01:55:56 +0900 Subject: [PATCH] arranged test code --- server/class/Eos.js | 403 +++++++++++++++++++++++++++---------------------- test/mocha/Eos.test.js | 98 ++++++------ 2 files changed, 273 insertions(+), 228 deletions(-) diff --git a/server/class/Eos.js b/server/class/Eos.js index d87d1b7..8771854 100644 --- a/server/class/Eos.js +++ b/server/class/Eos.js @@ -1,231 +1,270 @@ - /** - * Eosコマンドをエミュレートするクラス - * @constructor - * @returns {object} - * function execute(command, params) { - */ +/** + * Eosコマンドをエミュレートするクラス + * @varructor + * @returns {object} + * function execute(command, params) { + */ function Eos() { /** - * include all Eos command's info. - * For seaching with O(n), the object key name is command name. + * + * Class variables */ - const commandReference = { + + // include all Eos command's info. + // For seaching with O(n), the object key name is command name. + var commandReference = { mrcImageInfo: { }, dcdFilePrint: { } }; - const workspace = ['file1.txt', 'file2.txt']; + // for unit test + var workspace = ['file1.txt', 'file2.txt']; - return { - /** - * execute - * - * @param command - * @param params - * @returns {object} - */ - execute: function(command, options) { - var result = { - hasDone: false, // true | false - comment: ''// string - }; - - try { - - /** - * Check of command name - */ - if(typeof command !== 'string') { - errorMsg = 'Command parameter need to be string'; - throw new Error(errorMsg); - } - const hasCommand = Object.keys(commandReference).indexOf(command) > -1; - if(!hasCommand) { - errorMsg = 'Command name is invalid'; - throw new Error(errorMsg); - } - - /** - * Check of options - */ - - if(!(Array.isArray(options))) { - errorMsg = 'Options need to be Array'; - throw new Error(errorMsg); - } + /** + * + * Class variables + */ - - if(options.length === 0) { - errorMsg = 'At least one option is required.'; - throw new Error(errorMsg); - } - // translate options to key-value and check whether options include correct member - const optionsObj = {}; - var hasCorrectMember = true; - var isArgumentsArray = true; + /** + * validate + * コマンドとオプションのバリデーション + * @param command + * @param params + * @returns {valid: boolean, message: string} + */ + function validate(command, options) { + var result = { hasDone: false, // true | false + comment: ''// string + }; + + var ocf; // Array + var ocfObj = {}; // key-value + + try { + /** + * Check of command name + */ + if(typeof command !== 'string') { + errorMsg = 'Command parameter need to be string'; + throw new Error(errorMsg); + } + + var hasCommand = Object.keys(commandReference).indexOf(command) > -1; + if(!hasCommand) { + errorMsg = 'Command name is invalid'; + throw new Error(errorMsg); + } + + /** + * Check of options + */ + + if(!(Array.isArray(options))) { + errorMsg = 'Options need to be Array'; + throw new Error(errorMsg); + } - options.forEach(function(o) { - if(!(o.name) && !(o.arguments)) { - hasCorrectMember = false; + + if(options.length === 0) { + errorMsg = 'At least one option is required.'; + throw new Error(errorMsg); + } + + // translate options to key-value and check whether options include correct member + var optionsObj = {}; + var hasCorrectMember = true; + var isArgumentsArray = true; + + options.forEach(function(o) { + if(!(o.name) && !(o.arguments)) { + hasCorrectMember = false; + } else { + if(Array.isArray(o.arguments)) { + optionsObj[o.name] = o.arguments; } else { - if(Array.isArray(o.arguments)) { - optionsObj[o.name] = o.arguments; - } else { - isArgumentsArray = false; - } + isArgumentsArray = false; } - }); - - // check each object has proberties "name" and "argumets" - if(!hasCorrectMember) { - errorMsg = 'Options need to include Object which have properties "name" and "arguments"'; - throw new Error(errorMsg); } + }); - // check each "argumets" properties is Array - if(!isArgumentsArray) { - errorMsg = 'Each "arguments" properties needs to be Array'; - throw new Error(errorMsg); - } + // check each object has proberties "name" and "argumets" + if(!hasCorrectMember) { + errorMsg = 'Options need to include Object which have properties "name" and "arguments"'; + throw new Error(errorMsg); + } - // Read OptionControlFile info of command - const ocf = require(__dirname + '/../../user-specific-files/OptionControlFile/commands/' + command); - var ocfObj = {}; + // check each "argumets" properties is Array + if(!isArgumentsArray) { + errorMsg = 'Each "arguments" properties needs to be Array'; + throw new Error(errorMsg); + } - // translate ocf info to key-value - var notIncludingRequiredOptions = []; - ocf.forEach(function(o) { - if(o.optionProperties && Object.keys(optionsObj).indexOf(o.option) < 0) { - notIncludingRequiredOptions.push(o.option); - } - ocfObj[o.option] = o; - }); + // Read OptionControlFile info of command + ocf = require(__dirname + '/../../user-specific-files/OptionControlFile/commands/' + command); - // check whether all required option exist - if(notIncludingRequiredOptions.length > 0) { - errorMsg = 'Option ' + notIncludingRequiredOptions.toString() + ' are required'; - throw new Error(errorMsg); + // translate ocf info to key-value + var notIncludingRequiredOptions = []; + ocf.forEach(function(o) { + if(o.optionProperties && Object.keys(optionsObj).indexOf(o.option) < 0) { + notIncludingRequiredOptions.push(o.option); } + ocfObj[o.option] = o; + }); - var invalidArgumentsNumber= []; - var invalidArgumentType = []; - var invalidOutputFileName = []; + // check whether all required option exist + if(notIncludingRequiredOptions.length > 0) { + errorMsg = 'Option ' + notIncludingRequiredOptions.toString() + ' are required'; + throw new Error(errorMsg); + } - // output file Regexp - const outRegExp = /out|append/; + var invalidArgumentsNumber= []; + var invalidArgumentType = []; + var invalidOutputFileName = []; - options.forEach(function(o) { - // option number - const expectNum = ocfObj[o.name].optionNumber; - const actualNum = o.arguments.length; - if(expectNum !== actualNum) { - invalidArgumentsNumber.push({name: o.name, expect: expectNum, actual: actualNum}); - } + // output file Regexp + var outRegExp = /out|append/; + options.forEach(function(o) { + // option number + var expectNum = ocfObj[o.name].optionNumber; + var actualNum = o.arguments.length; + if(expectNum !== actualNum) { + invalidArgumentsNumber.push({name: o.name, expect: expectNum, actual: actualNum}); + } - // argType and outFile name - o.arguments.forEach(function(arg,i) { - // argType - var formType = ocfObj[o.name].arg[i].formType - if(formType === 'select') { // This argument is filename - var exist = workspace.indexOf(arg) > -1; - if(!exist) { - invalidArgumentType.push({name: o.name, file: arg}); - } - } else { - const expectType = formType === 'text' ? 'string' : 'number'; - const actualType = typeof arg; - if(expectType !== actualType) { - invalidArgumentType.push({name: o.name, expect: expectType, actual: actualType}); - } + + // argType and outFile name + o.arguments.forEach(function(arg,i) { + // argType + var formType = ocfObj[o.name].arg[i].formType + if(formType === 'select') { // This argument is filename + var exist = workspace.indexOf(arg) > -1; + if(!exist) { + invalidArgumentType.push({name: o.name, file: arg}); } + } else { + var expectType = formType === 'text' ? 'string' : 'number'; + var actualType = typeof arg; + if(expectType !== actualType) { + invalidArgumentType.push({name: o.name, expect: expectType, actual: actualType}); + } + } - // outFile name - if(outRegExp.test(ocfObj[o.name].arg[i].argType)) { - if(workspace.indexOf(o.arguments[i]) > -1) { - invalidOutputFileName.push({name: o.name, file: arg}); - } + // outFile name + if(outRegExp.test(ocfObj[o.name].arg[i].argType)) { + if(workspace.indexOf(o.arguments[i]) > -1) { + invalidOutputFileName.push({name: o.name, file: arg}); } - }); + } }); + }); - // check arguments number value - if(invalidArgumentsNumber.length > 0) { - errorMsg = '"arguments" properties is invalid number.\n'; - invalidArgumentsNumber.forEach(function(i) { - errorMsg += ' ' + i.name + ' expect to ' + i.expect + ', but actual ' + i.actual + '.\n'; - }); - throw new Error(errorMsg); - } + // check arguments number value + if(invalidArgumentsNumber.length > 0) { + errorMsg = '"arguments" properties is invalid number.\n'; + invalidArgumentsNumber.forEach(function(i) { + errorMsg += i.name + ' expect to ' + i.expect + ', but actual ' + i.actual + '.\n'; + }); + throw new Error(errorMsg); + } - // check arguments type - if(invalidArgumentType.length > 0) { - errorMsg = '"arguments" type is invalid.\n'; - invalidArgumentType.forEach(function(i) { - if(i.file) { - errorMsg += ' ' + i.name + ' ' + i.file + ' does not exist.\n'; - } else { - errorMsg += ' ' + i.name + ' expect to ' + i.expect + ', but actual ' + i.actual + '.\n'; - } - }); - throw new Error(errorMsg); - } + // check arguments type + if(invalidArgumentType.length > 0) { + errorMsg = '"arguments" type is invalid.\n'; + invalidArgumentType.forEach(function(i) { + if(i.file) { + errorMsg += i.name + ' ' + i.file + ' does not exist.\n'; + } else { + errorMsg += i.name + ' expect to ' + i.expect + ', but actual ' + i.actual + '.\n'; + } + }); + throw new Error(errorMsg); + } - // check outFile name - if(invalidOutputFileName.length > 0) { - errorMsg = 'output file name is invalid.\n'; - invalidOutputFileName.forEach(function(i) { - errorMsg += ' ' + i.name + ' ' + i.file + ' has already existed.\n'; - }); - throw new Error(errorMsg); - } - } catch(e) { - result.message = e.message; - return result; + // check outFile name + if(invalidOutputFileName.length > 0) { + errorMsg = 'output file name is invalid.\n'; + invalidOutputFileName.forEach(function(i) { + errorMsg += i.name + ' ' + i.file + ' has already existed.\n'; + }); + throw new Error(errorMsg); } + } catch(e) { + result.message = e.message; + return result; + } + } - // set default parameters - var finalOptions = {}; - ocf.forEach(function(o) { - o.arg.forEach(function(arg) { - if(!(arg.initialValue === "") && arg.initialValue) { - if(!(finalOptions[o.option])) { - finalOptions[o.option] = []; - finalOptions[o.option].push(arg.initialValue); - } else { - finalOptions[o.option].push(arg.initialValue); - } + /** + * toExecString + * + * @param command + * @param options + * @returns {string} + */ + function toExecString(command, options) { + var ocf = require(__dirname + '/../../user-specific-files/OptionControlFile/commands/' + command); // Array + var finalOptions = {}; + var execStr = command + ' '; + + // set default parameters + ocf.forEach(function(o) { + o.arg.forEach(function(arg) { + if(!(arg.initialValue === "") && arg.initialValue) { + if(!(finalOptions[o.option])) { + finalOptions[o.option] = []; + finalOptions[o.option].push(arg.initialValue); + } else { + finalOptions[o.option].push(arg.initialValue); } - }); - }); - - // set user setting parameters - options.forEach(function(o) { - finalOptions[o.name] = o.arguments; + } }); + }); - // set execution string - var execStr = command + ' '; - Object.keys(finalOptions).forEach(function(key) { - execStr += key + ' '; - finalOptions[key].forEach(function(arg) { - execStr += arg + ' '; - }); + // set user setting parameters + options.forEach(function(o) { + finalOptions[o.name] = o.arguments; + }); + + // set execution string + Object.keys(finalOptions).forEach(function(key) { + execStr += key + ' '; + finalOptions[key].forEach(function(arg) { + execStr += arg + ' '; }); + }); + + // remove last blank + execStr = execStr.slice(0,execStr.length-1); + + return execStr; + } + + + return { + /** + * execute + * + * @param command + * @param params + * @returns {object} + */ + execute: function(command, options) { - // remove last blank - execStr = execStr.slice(0,execStr.length-1); - /** * End in Success */ result.hasDone = true; result.message = command + ' has done.'; return result; - } + }, + + // For unit test + validate: validate, + toExecString: toExecString } } diff --git a/test/mocha/Eos.test.js b/test/mocha/Eos.test.js index 4b30493..ed73de0 100644 --- a/test/mocha/Eos.test.js +++ b/test/mocha/Eos.test.js @@ -3,100 +3,106 @@ var expect = require('chai').expect; var test1 = ` /** -* Eosコマンドをエミュレートするクラス -* +* コマンドとオプションのバリデーション * @param {string} Eosコマンドの名前 * @param {Array} コマンドに渡すオプション -* @returns -* function execute(command, params) { +* @returns {valid: boolean, message: string} +* function validate(command, params) { */ +`; +var test2 = ` +/** +* 実行時文字列への変換 +* +* @param command +* @param options +* @returns {string} +* function toExecString(command, params) { +*/ `; -describe(/*execute()*/test1, function() { - describe('#1: check whether command is valid or not', function() { - var eos; - before(function() { - eos = new Eos(); - }); +describe('Eos クラス', function() { + var eos; + before(function() { + eos = new Eos(); + }); + + describe(test1, function() { it('should return false when the command is typeof number.', function() { - var result = eos.execute(2); - expect(result.hasDone).to.equal(false); - console.log(' '+result.comment); + var result = eos.validate(2); + expect(result.message).to.equal('Command parameter need to be string'); }); it('should return false when command name is invalid', function() { - var result = eos.execute('hoge'); - expect(result.hasDone).to.equal(false); - console.log(' '+result.comment); + var result = eos.validate('hoge'); + expect(result.message).to.equal('Command name is invalid'); }); it('should return false when options is not Array', function() { - var result = eos.execute('mrcImageInfo', {}); - expect(result.hasDone).to.equal(false); - console.log(' '+result.comment); + var result = eos.validate('mrcImageInfo', {}); + expect(result.message).to.equal('Options need to be Array'); }); it('should return false when options is Array whose length is 0.', function() { - var result = eos.execute('mrcImageInfo', []); - expect(result.hasDone).to.equal(false); - console.log(' '+result.comment); + var result = eos.validate('mrcImageInfo', []); + expect(result.message).to.equal('At least one option is required.'); }); it('should return false when options is invalid Object which have not "name" and "argumetns"', function() { - var result = eos.execute('mrcImageInfo', [{hoge: 'hoge'}]); - expect(result.hasDone).to.equal(false); - console.log(' '+result.comment); + var result = eos.validate('mrcImageInfo', [{hoge: 'hoge'}]); + expect(result.message).to.equal('Options need to include Object which have properties "name" and "arguments"'); }); it('should return false when "argumetns" properties are not Array', function() { - var result = eos.execute('mrcImageInfo', [{name: 'hoge', arguments: 'hoge'}]); - expect(result.hasDone).to.equal(false); - console.log(' '+result.comment); + var result = eos.validate('mrcImageInfo', [{name: 'hoge', arguments: 'hoge'}]); + expect(result.message).to.equal('Each "arguments" properties needs to be Array'); }); it('should return false when "argumetns" properties are not Array', function() { - var result = eos.execute('mrcImageInfo', [{name: 'hoge', arguments: 'hoge'}]); - expect(result.hasDone).to.equal(false); - console.log(' '+result.comment); + var result = eos.validate('mrcImageInfo', [{name: 'hoge', arguments: 'hoge'}]); + expect(result.message).to.equal('Each "arguments" properties needs to be Array'); }); it('should return false when required options do not exist', function() { - var result = eos.execute('mrcImageInfo', [{name: 'hoge', arguments: []}]); - expect(result.hasDone).to.equal(false); - console.log(' '+result.comment); + var result = eos.validate('mrcImageInfo', [{name: 'hoge', arguments: []}]); + expect(result.message).to.equal('Option -i are required'); }); it('should return false when required options do not exist', function() { - var result = eos.execute('dcdFilePrint', [{name: '-r', arguments: [1,2]}, { name: '-i', arguments: []}, { name: '-o', arguments: []} ]); + var result = eos.validate('dcdFilePrint', [{name: '-r', arguments: [1,2]}, { name: '-i', arguments: []}, { name: '-o', arguments: []} ]); expect(result.hasDone).to.equal(false); - console.log(' '+result.comment); + console.log(result.message); }); it('should return false when input file does not exist', function() { - var result = eos.execute('dcdFilePrint', [{name: '-r', arguments: [1,2,3]}, { name: '-i', arguments: ['hoge.txt']}, { name: '-o', arguments: ['hoge.txt']} ]); + var result = eos.validate('dcdFilePrint', [{name: '-r', arguments: [1,2,3]}, { name: '-i', arguments: ['hoge.txt']}, { name: '-o', arguments: ['hoge.txt']} ]); expect(result.hasDone).to.equal(false); - console.log(' '+result.comment); + console.log(result.message); + }); it('should return false when output file is not string', function() { - var result = eos.execute('dcdFilePrint', [{name: '-r', arguments: [1,2,3]}, { name: '-i', arguments: ['file1.txt']}, { name: '-o', arguments: [3]} ]); + var result = eos.validate('dcdFilePrint', [{name: '-r', arguments: [1,2,3]}, { name: '-i', arguments: ['file1.txt']}, { name: '-o', arguments: [3]} ]); expect(result.hasDone).to.equal(false); - console.log(' '+result.comment); + console.log(result.message); + }); it('should return false when output file has already existed', function() { - var result = eos.execute('dcdFilePrint', [{name: '-r', arguments: [1,2,3]}, { name: '-i', arguments: ['file1.txt']}, { name: '-o', arguments: ['file1.txt']} ]); + var result = eos.validate('dcdFilePrint', [{name: '-r', arguments: [1,2,3]}, { name: '-i', arguments: ['file1.txt']}, { name: '-o', arguments: ['file1.txt']} ]); expect(result.hasDone).to.equal(false); - console.log(' '+result.comment); + console.log(result.message); + }); + }); + + describe(test2, function() { it('should return true when all options is proper.', function() { - var result = eos.execute('dcdFilePrint', [{name: '-r', arguments: [1,2,3]}, { name: '-i', arguments: ['file1.txt']}, { name: '-o', arguments: ['file3.txt']} ]); - expect(result.hasDone).to.equal(true); - console.log(' '+result.comment); + var result = eos.toExecString('dcdFilePrint', [{name: '-r', arguments: [1,2,3]}, { name: '-i', arguments: ['file1.txt']}, { name: '-o', arguments: ['file3.txt']} ]); + expect(result).to.equal('dcdFilePrint -r 1 2 3 -s 10 -e 100 -d 10 -m 0 -i file1.txt -o file3.txt'); }); - }); }); -- 2.11.0