OSDN Git Service

Check Eos command execution parameters. v0.3.0p0004
authorhimetani_cafe <fumifumi@yasunaga-lab.bio.kyutech.ac.jp>
Wed, 20 Jan 2016 14:06:59 +0000 (23:06 +0900)
committerhimetani_cafe <fumifumi@yasunaga-lab.bio.kyutech.ac.jp>
Wed, 20 Jan 2016 14:06:59 +0000 (23:06 +0900)
server/class/Eos.js
test/mocha/Eos.test.js

index 2c0d6c1..63342a5 100644 (file)
@@ -12,9 +12,11 @@ function Eos() {
      */
     const commandReference = {
         mrcImageInfo: { },
-        dcdFilePring: { }
+        dcdFilePrint: { }
     };
 
+    const workspace = ['file1.txt', 'file2.txt'];
+
     return {
         /**
          * execute
@@ -33,13 +35,13 @@ function Eos() {
              * Check of command name
              */
             if(typeof command !== 'string') {
-                result.comment = 'command parameter need to be string';
+                result.comment = 'Command parameter need to be string';
                 return result;
             }
 
-            const hasCommand = Object.keys(commandReference).indexOf(command) > 0;
+            const hasCommand = Object.keys(commandReference).indexOf(command) > -1;
             if(!hasCommand) {
-                result.comment = 'command name is invalid';
+                result.comment = 'Command name is invalid';
                 return result;
             }
             
@@ -47,9 +49,172 @@ function Eos() {
              * Check of options
              */
 
+            if(!(Array.isArray(options))) {
+                result.comment = 'Options need to be Array';
+                return result;
+            }
+
+            
+            if(options.length === 0) {
+                result.comment = 'At least one option is required.';
+                return result;
+            }
+
+            // translate options to key-value and check whether options include correct member
+            const 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 {
+                        isArgumentsArray = false;
+                    }
+                }
+            });
+
+            // check each object has proberties "name" and "argumets"
+            if(!hasCorrectMember) {
+                result.comment = 'Options need to include Object which have properties "name" and "arguments"';
+                return result;
+            }
+
+            // check each "argumets" properties is Array
+            if(!isArgumentsArray) {
+                result.comment = 'Each "arguments" properties needs to be Array';
+                return result;
+            }
+
+            // Read OptionControlFile info of command
+            const ocf = require(__dirname + '/../../user-specific-files/OptionControlFile/commands/' + command);
+            var ocfObj = {};
+
+            // 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;
+            });
+
+            // check whether all required option exist 
+            if(notIncludingRequiredOptions.length > 0) {
+                result.comment = 'Option ' + notIncludingRequiredOptions.toString() + ' are required';
+                return result;
+            }
 
+            var invalidArgumentsNumber= [];
+            var invalidArgumentType = [];
+            var invalidOutputFileName = [];
+
+            // output file Regexp
+            const outRegExp = /out|append/;
+
+            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});
+                }
+
+
+                // 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});
+                        }
+                    }
+
+                    // 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) {
+                result.comment = '"arguments" properties is invalid number.\n';
+                invalidArgumentsNumber.forEach(function(i) {
+                    result.comment += '     ' + i.name + ' expect to  ' + i.expect + ', but actual ' + i.actual + '.\n';
+                });
+                return result;
+            }
+
+            // check arguments type
+            if(invalidArgumentType.length > 0) {
+                result.comment = '"arguments" type is invalid.\n';
+                invalidArgumentType.forEach(function(i) {
+                    if(i.file) {
+                        result.comment += '     ' + i.name + ' ' + i.file + ' does not exist.\n';
+                    } else {
+                        result.comment += '     ' + i.name + ' expect to ' + i.expect + ', but actual ' + i.actual + '.\n';
+                    }
+                });
+                return result;
+            }
+
+            // check outFile name
+            if(invalidOutputFileName.length > 0) {
+                result.comment = 'output file name is invalid.\n';
+                invalidOutputFileName.forEach(function(i) {
+                    result.comment += '     ' + i.name + ' ' + i.file + ' has already existed.\n';
+                });
+                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);
+                        }
+                    }
+                });
+            });
+            
+            // 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 + ' ';
+                });
+            });
+
+            // remove last blank
+            execStr = execStr.slice(0,execStr.length-1);
+            
             /**
-             * End of Success
+             * End in Success
              */
             result.hasDone = true;
             result.comment = command + ' has done.';
index c2477d3..a477abf 100644 (file)
@@ -2,30 +2,90 @@ var Eos = require('../../server/class/Eos');
 var expect = require('chai').expect;
 
 var test1 = `
-    /**
-     * Eosコマンドをエミュレートするクラス 
-     *
-     * @param {string} Eosコマンドの名前
-     * @param {Array<Object>} コマンドに渡すオプション
-     * @returns 
-     * function execute(command, params) {
-     */
-    
+/**
+* Eosコマンドをエミュレートするクラス 
+*
+* @param {string} Eosコマンドの名前
+* @param {Array<Object>} コマンドに渡すオプション
+* @returns 
+* function execute(command, params) {
+*/
+
 `;
 
 describe(/*execute()*/test1, function() {
     describe('#1: check whether command is valid or not', function() {
+        var eos;
+        before(function() {
+            eos = new Eos();
+        });
+
         it('should return false when the command is typeof number.', function() {
-            const eos = new Eos();
-            const result = eos.execute(2);
+            var  result = eos.execute(2);
             expect(result.hasDone).to.equal(false);
         });
 
         it('should return false when command name is invalid', function() {
-            const eos = new Eos();
-            const result = eos.execute('hoge');
+            var result = eos.execute('hoge');
+            expect(result.hasDone).to.equal(false);
+        });
+
+        it('should return false when options is not Array', function() {
+            var result = eos.execute('mrcImageInfo', {});
+            expect(result.hasDone).to.equal(false);
+        });
+
+        it('should return false when options is Array whose length is 0.', function() {
+            var result = eos.execute('mrcImageInfo', []);
+            expect(result.hasDone).to.equal(false);
+        });
+
+        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);
+        });
+
+        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);
+        });
+
+        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);
         });
 
+        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);
+        });
+
+        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: []} ]);
+            expect(result.hasDone).to.equal(false);
+        });
+
+        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']} ]);
+            expect(result.hasDone).to.equal(false);
+        });
+
+        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]} ]);
+            expect(result.hasDone).to.equal(false);
+        });
+
+        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']} ]);
+            console.log(result.comment);
+            expect(result.hasDone).to.equal(false);
+        });
+
+        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']} ]);
+            //console.log(result.comment);
+            expect(result.hasDone).to.equal(true);
+        });
+
     });
 });