• R/O
  • HTTP
  • SSH
  • HTTPS

zephyr: Commit

This repository is for zephyr, which is implemented for a GUI of Eos.


Commit MetaInfo

Revision6ff27095bb1dc641b2f91718b395d948a7a77312 (tree)
Time2016-01-22 01:55:56
Authorhimetani_cafe <fumifumi@yasu...>
Commiterhimetani_cafe

Log Message

arranged test code

Change Summary

Incremental Difference

--- a/server/class/Eos.js
+++ b/server/class/Eos.js
@@ -1,231 +1,270 @@
1- /**
2- * Eosコマンドをエミュレートするクラス
3- * @constructor
4- * @returns {object}
5- * function execute(command, params) {
6- */
1+/**
2+ * Eosコマンドをエミュレートするクラス
3+ * @varructor
4+ * @returns {object}
5+ * function execute(command, params) {
6+ */
77 function Eos() {
88
99 /**
10- * include all Eos command's info.
11- * For seaching with O(n), the object key name is command name.
10+ *
11+ * Class variables
1212 */
13- const commandReference = {
13+
14+ // include all Eos command's info.
15+ // For seaching with O(n), the object key name is command name.
16+ var commandReference = {
1417 mrcImageInfo: { },
1518 dcdFilePrint: { }
1619 };
1720
18- const workspace = ['file1.txt', 'file2.txt'];
21+ // for unit test
22+ var workspace = ['file1.txt', 'file2.txt'];
1923
20- return {
21- /**
22- * execute
23- *
24- * @param command
25- * @param params
26- * @returns {object}
27- */
28- execute: function(command, options) {
29- var result = {
30- hasDone: false, // true | false
31- comment: ''// string
32- };
33-
34- try {
35-
36- /**
37- * Check of command name
38- */
39- if(typeof command !== 'string') {
40- errorMsg = 'Command parameter need to be string';
41- throw new Error(errorMsg);
42- }
4324
44- const hasCommand = Object.keys(commandReference).indexOf(command) > -1;
45- if(!hasCommand) {
46- errorMsg = 'Command name is invalid';
47- throw new Error(errorMsg);
48- }
49-
50- /**
51- * Check of options
52- */
53-
54- if(!(Array.isArray(options))) {
55- errorMsg = 'Options need to be Array';
56- throw new Error(errorMsg);
57- }
25+ /**
26+ *
27+ * Class variables
28+ */
5829
59-
60- if(options.length === 0) {
61- errorMsg = 'At least one option is required.';
62- throw new Error(errorMsg);
63- }
6430
65- // translate options to key-value and check whether options include correct member
66- const optionsObj = {};
67- var hasCorrectMember = true;
68- var isArgumentsArray = true;
31+ /**
32+ * validate
33+ * コマンドとオプションのバリデーション
34+ * @param command
35+ * @param params
36+ * @returns {valid: boolean, message: string}
37+ */
38+ function validate(command, options) {
39+ var result = { hasDone: false, // true | false
40+ comment: ''// string
41+ };
42+
43+ var ocf; // Array
44+ var ocfObj = {}; // key-value
45+
46+ try {
47+ /**
48+ * Check of command name
49+ */
50+ if(typeof command !== 'string') {
51+ errorMsg = 'Command parameter need to be string';
52+ throw new Error(errorMsg);
53+ }
54+
55+ var hasCommand = Object.keys(commandReference).indexOf(command) > -1;
56+ if(!hasCommand) {
57+ errorMsg = 'Command name is invalid';
58+ throw new Error(errorMsg);
59+ }
60+
61+ /**
62+ * Check of options
63+ */
64+
65+ if(!(Array.isArray(options))) {
66+ errorMsg = 'Options need to be Array';
67+ throw new Error(errorMsg);
68+ }
6969
70- options.forEach(function(o) {
71- if(!(o.name) && !(o.arguments)) {
72- hasCorrectMember = false;
70+
71+ if(options.length === 0) {
72+ errorMsg = 'At least one option is required.';
73+ throw new Error(errorMsg);
74+ }
75+
76+ // translate options to key-value and check whether options include correct member
77+ var optionsObj = {};
78+ var hasCorrectMember = true;
79+ var isArgumentsArray = true;
80+
81+ options.forEach(function(o) {
82+ if(!(o.name) && !(o.arguments)) {
83+ hasCorrectMember = false;
84+ } else {
85+ if(Array.isArray(o.arguments)) {
86+ optionsObj[o.name] = o.arguments;
7387 } else {
74- if(Array.isArray(o.arguments)) {
75- optionsObj[o.name] = o.arguments;
76- } else {
77- isArgumentsArray = false;
78- }
88+ isArgumentsArray = false;
7989 }
80- });
81-
82- // check each object has proberties "name" and "argumets"
83- if(!hasCorrectMember) {
84- errorMsg = 'Options need to include Object which have properties "name" and "arguments"';
85- throw new Error(errorMsg);
8690 }
91+ });
8792
88- // check each "argumets" properties is Array
89- if(!isArgumentsArray) {
90- errorMsg = 'Each "arguments" properties needs to be Array';
91- throw new Error(errorMsg);
92- }
93+ // check each object has proberties "name" and "argumets"
94+ if(!hasCorrectMember) {
95+ errorMsg = 'Options need to include Object which have properties "name" and "arguments"';
96+ throw new Error(errorMsg);
97+ }
9398
94- // Read OptionControlFile info of command
95- const ocf = require(__dirname + '/../../user-specific-files/OptionControlFile/commands/' + command);
96- var ocfObj = {};
99+ // check each "argumets" properties is Array
100+ if(!isArgumentsArray) {
101+ errorMsg = 'Each "arguments" properties needs to be Array';
102+ throw new Error(errorMsg);
103+ }
97104
98- // translate ocf info to key-value
99- var notIncludingRequiredOptions = [];
100- ocf.forEach(function(o) {
101- if(o.optionProperties && Object.keys(optionsObj).indexOf(o.option) < 0) {
102- notIncludingRequiredOptions.push(o.option);
103- }
104- ocfObj[o.option] = o;
105- });
105+ // Read OptionControlFile info of command
106+ ocf = require(__dirname + '/../../user-specific-files/OptionControlFile/commands/' + command);
106107
107- // check whether all required option exist
108- if(notIncludingRequiredOptions.length > 0) {
109- errorMsg = 'Option ' + notIncludingRequiredOptions.toString() + ' are required';
110- throw new Error(errorMsg);
108+ // translate ocf info to key-value
109+ var notIncludingRequiredOptions = [];
110+ ocf.forEach(function(o) {
111+ if(o.optionProperties && Object.keys(optionsObj).indexOf(o.option) < 0) {
112+ notIncludingRequiredOptions.push(o.option);
111113 }
114+ ocfObj[o.option] = o;
115+ });
112116
113- var invalidArgumentsNumber= [];
114- var invalidArgumentType = [];
115- var invalidOutputFileName = [];
117+ // check whether all required option exist
118+ if(notIncludingRequiredOptions.length > 0) {
119+ errorMsg = 'Option ' + notIncludingRequiredOptions.toString() + ' are required';
120+ throw new Error(errorMsg);
121+ }
116122
117- // output file Regexp
118- const outRegExp = /out|append/;
123+ var invalidArgumentsNumber= [];
124+ var invalidArgumentType = [];
125+ var invalidOutputFileName = [];
119126
120- options.forEach(function(o) {
121- // option number
122- const expectNum = ocfObj[o.name].optionNumber;
123- const actualNum = o.arguments.length;
124- if(expectNum !== actualNum) {
125- invalidArgumentsNumber.push({name: o.name, expect: expectNum, actual: actualNum});
126- }
127+ // output file Regexp
128+ var outRegExp = /out|append/;
127129
130+ options.forEach(function(o) {
131+ // option number
132+ var expectNum = ocfObj[o.name].optionNumber;
133+ var actualNum = o.arguments.length;
134+ if(expectNum !== actualNum) {
135+ invalidArgumentsNumber.push({name: o.name, expect: expectNum, actual: actualNum});
136+ }
128137
129- // argType and outFile name
130- o.arguments.forEach(function(arg,i) {
131- // argType
132- var formType = ocfObj[o.name].arg[i].formType
133- if(formType === 'select') { // This argument is filename
134- var exist = workspace.indexOf(arg) > -1;
135- if(!exist) {
136- invalidArgumentType.push({name: o.name, file: arg});
137- }
138- } else {
139- const expectType = formType === 'text' ? 'string' : 'number';
140- const actualType = typeof arg;
141- if(expectType !== actualType) {
142- invalidArgumentType.push({name: o.name, expect: expectType, actual: actualType});
143- }
138+
139+ // argType and outFile name
140+ o.arguments.forEach(function(arg,i) {
141+ // argType
142+ var formType = ocfObj[o.name].arg[i].formType
143+ if(formType === 'select') { // This argument is filename
144+ var exist = workspace.indexOf(arg) > -1;
145+ if(!exist) {
146+ invalidArgumentType.push({name: o.name, file: arg});
144147 }
148+ } else {
149+ var expectType = formType === 'text' ? 'string' : 'number';
150+ var actualType = typeof arg;
151+ if(expectType !== actualType) {
152+ invalidArgumentType.push({name: o.name, expect: expectType, actual: actualType});
153+ }
154+ }
145155
146- // outFile name
147- if(outRegExp.test(ocfObj[o.name].arg[i].argType)) {
148- if(workspace.indexOf(o.arguments[i]) > -1) {
149- invalidOutputFileName.push({name: o.name, file: arg});
150- }
156+ // outFile name
157+ if(outRegExp.test(ocfObj[o.name].arg[i].argType)) {
158+ if(workspace.indexOf(o.arguments[i]) > -1) {
159+ invalidOutputFileName.push({name: o.name, file: arg});
151160 }
152- });
161+ }
153162 });
163+ });
154164
155- // check arguments number value
156- if(invalidArgumentsNumber.length > 0) {
157- errorMsg = '"arguments" properties is invalid number.\n';
158- invalidArgumentsNumber.forEach(function(i) {
159- errorMsg += ' ' + i.name + ' expect to ' + i.expect + ', but actual ' + i.actual + '.\n';
160- });
161- throw new Error(errorMsg);
162- }
165+ // check arguments number value
166+ if(invalidArgumentsNumber.length > 0) {
167+ errorMsg = '"arguments" properties is invalid number.\n';
168+ invalidArgumentsNumber.forEach(function(i) {
169+ errorMsg += i.name + ' expect to ' + i.expect + ', but actual ' + i.actual + '.\n';
170+ });
171+ throw new Error(errorMsg);
172+ }
163173
164- // check arguments type
165- if(invalidArgumentType.length > 0) {
166- errorMsg = '"arguments" type is invalid.\n';
167- invalidArgumentType.forEach(function(i) {
168- if(i.file) {
169- errorMsg += ' ' + i.name + ' ' + i.file + ' does not exist.\n';
170- } else {
171- errorMsg += ' ' + i.name + ' expect to ' + i.expect + ', but actual ' + i.actual + '.\n';
172- }
173- });
174- throw new Error(errorMsg);
175- }
174+ // check arguments type
175+ if(invalidArgumentType.length > 0) {
176+ errorMsg = '"arguments" type is invalid.\n';
177+ invalidArgumentType.forEach(function(i) {
178+ if(i.file) {
179+ errorMsg += i.name + ' ' + i.file + ' does not exist.\n';
180+ } else {
181+ errorMsg += i.name + ' expect to ' + i.expect + ', but actual ' + i.actual + '.\n';
182+ }
183+ });
184+ throw new Error(errorMsg);
185+ }
176186
177- // check outFile name
178- if(invalidOutputFileName.length > 0) {
179- errorMsg = 'output file name is invalid.\n';
180- invalidOutputFileName.forEach(function(i) {
181- errorMsg += ' ' + i.name + ' ' + i.file + ' has already existed.\n';
182- });
183- throw new Error(errorMsg);
184- }
185- } catch(e) {
186- result.message = e.message;
187- return result;
187+ // check outFile name
188+ if(invalidOutputFileName.length > 0) {
189+ errorMsg = 'output file name is invalid.\n';
190+ invalidOutputFileName.forEach(function(i) {
191+ errorMsg += i.name + ' ' + i.file + ' has already existed.\n';
192+ });
193+ throw new Error(errorMsg);
188194 }
195+ } catch(e) {
196+ result.message = e.message;
197+ return result;
198+ }
199+ }
189200
190- // set default parameters
191- var finalOptions = {};
192- ocf.forEach(function(o) {
193- o.arg.forEach(function(arg) {
194- if(!(arg.initialValue === "") && arg.initialValue) {
195- if(!(finalOptions[o.option])) {
196- finalOptions[o.option] = [];
197- finalOptions[o.option].push(arg.initialValue);
198- } else {
199- finalOptions[o.option].push(arg.initialValue);
200- }
201+ /**
202+ * toExecString
203+ *
204+ * @param command
205+ * @param options
206+ * @returns {string}
207+ */
208+ function toExecString(command, options) {
209+ var ocf = require(__dirname + '/../../user-specific-files/OptionControlFile/commands/' + command); // Array
210+ var finalOptions = {};
211+ var execStr = command + ' ';
212+
213+ // set default parameters
214+ ocf.forEach(function(o) {
215+ o.arg.forEach(function(arg) {
216+ if(!(arg.initialValue === "") && arg.initialValue) {
217+ if(!(finalOptions[o.option])) {
218+ finalOptions[o.option] = [];
219+ finalOptions[o.option].push(arg.initialValue);
220+ } else {
221+ finalOptions[o.option].push(arg.initialValue);
201222 }
202- });
203- });
204-
205- // set user setting parameters
206- options.forEach(function(o) {
207- finalOptions[o.name] = o.arguments;
223+ }
208224 });
225+ });
209226
210- // set execution string
211- var execStr = command + ' ';
212- Object.keys(finalOptions).forEach(function(key) {
213- execStr += key + ' ';
214- finalOptions[key].forEach(function(arg) {
215- execStr += arg + ' ';
216- });
227+ // set user setting parameters
228+ options.forEach(function(o) {
229+ finalOptions[o.name] = o.arguments;
230+ });
231+
232+ // set execution string
233+ Object.keys(finalOptions).forEach(function(key) {
234+ execStr += key + ' ';
235+ finalOptions[key].forEach(function(arg) {
236+ execStr += arg + ' ';
217237 });
238+ });
239+
240+ // remove last blank
241+ execStr = execStr.slice(0,execStr.length-1);
242+
243+ return execStr;
244+ }
245+
246+
247+ return {
248+ /**
249+ * execute
250+ *
251+ * @param command
252+ * @param params
253+ * @returns {object}
254+ */
255+ execute: function(command, options) {
218256
219- // remove last blank
220- execStr = execStr.slice(0,execStr.length-1);
221-
222257 /**
223258 * End in Success
224259 */
225260 result.hasDone = true;
226261 result.message = command + ' has done.';
227262 return result;
228- }
263+ },
264+
265+ // For unit test
266+ validate: validate,
267+ toExecString: toExecString
229268 }
230269 }
231270
--- a/test/mocha/Eos.test.js
+++ b/test/mocha/Eos.test.js
@@ -3,100 +3,106 @@ var expect = require('chai').expect;
33
44 var test1 = `
55 /**
6-* Eosコマンドをエミュレートするクラス
7-*
6+* コマンドとオプションのバリデーション
87 * @param {string} Eosコマンドの名前
98 * @param {Array<Object>} コマンドに渡すオプション
10-* @returns
11-* function execute(command, params) {
9+* @returns {valid: boolean, message: string}
10+* function validate(command, params) {
1211 */
12+`;
1313
14+var test2 = `
15+/**
16+* 実行時文字列への変換
17+*
18+* @param command
19+* @param options
20+* @returns {string}
21+* function toExecString(command, params) {
22+*/
1423 `;
1524
16-describe(/*execute()*/test1, function() {
17- describe('#1: check whether command is valid or not', function() {
18- var eos;
19- before(function() {
20- eos = new Eos();
21- });
25+describe('Eos クラス', function() {
26+ var eos;
27+ before(function() {
28+ eos = new Eos();
29+ });
30+
31+ describe(test1, function() {
2232
2333 it('should return false when the command is typeof number.', function() {
24- var result = eos.execute(2);
25- expect(result.hasDone).to.equal(false);
26- console.log(' '+result.comment);
34+ var result = eos.validate(2);
35+ expect(result.message).to.equal('Command parameter need to be string');
2736 });
2837
2938 it('should return false when command name is invalid', function() {
30- var result = eos.execute('hoge');
31- expect(result.hasDone).to.equal(false);
32- console.log(' '+result.comment);
39+ var result = eos.validate('hoge');
40+ expect(result.message).to.equal('Command name is invalid');
3341 });
3442
3543 it('should return false when options is not Array', function() {
36- var result = eos.execute('mrcImageInfo', {});
37- expect(result.hasDone).to.equal(false);
38- console.log(' '+result.comment);
44+ var result = eos.validate('mrcImageInfo', {});
45+ expect(result.message).to.equal('Options need to be Array');
3946 });
4047
4148 it('should return false when options is Array whose length is 0.', function() {
42- var result = eos.execute('mrcImageInfo', []);
43- expect(result.hasDone).to.equal(false);
44- console.log(' '+result.comment);
49+ var result = eos.validate('mrcImageInfo', []);
50+ expect(result.message).to.equal('At least one option is required.');
4551 });
4652
4753 it('should return false when options is invalid Object which have not "name" and "argumetns"', function() {
48- var result = eos.execute('mrcImageInfo', [{hoge: 'hoge'}]);
49- expect(result.hasDone).to.equal(false);
50- console.log(' '+result.comment);
54+ var result = eos.validate('mrcImageInfo', [{hoge: 'hoge'}]);
55+ expect(result.message).to.equal('Options need to include Object which have properties "name" and "arguments"');
5156 });
5257
5358 it('should return false when "argumetns" properties are not Array', function() {
54- var result = eos.execute('mrcImageInfo', [{name: 'hoge', arguments: 'hoge'}]);
55- expect(result.hasDone).to.equal(false);
56- console.log(' '+result.comment);
59+ var result = eos.validate('mrcImageInfo', [{name: 'hoge', arguments: 'hoge'}]);
60+ expect(result.message).to.equal('Each "arguments" properties needs to be Array');
5761 });
5862
5963 it('should return false when "argumetns" properties are not Array', function() {
60- var result = eos.execute('mrcImageInfo', [{name: 'hoge', arguments: 'hoge'}]);
61- expect(result.hasDone).to.equal(false);
62- console.log(' '+result.comment);
64+ var result = eos.validate('mrcImageInfo', [{name: 'hoge', arguments: 'hoge'}]);
65+ expect(result.message).to.equal('Each "arguments" properties needs to be Array');
6366 });
6467
6568 it('should return false when required options do not exist', function() {
66- var result = eos.execute('mrcImageInfo', [{name: 'hoge', arguments: []}]);
67- expect(result.hasDone).to.equal(false);
68- console.log(' '+result.comment);
69+ var result = eos.validate('mrcImageInfo', [{name: 'hoge', arguments: []}]);
70+ expect(result.message).to.equal('Option -i are required');
6971 });
7072
7173 it('should return false when required options do not exist', function() {
72- var result = eos.execute('dcdFilePrint', [{name: '-r', arguments: [1,2]}, { name: '-i', arguments: []}, { name: '-o', arguments: []} ]);
74+ var result = eos.validate('dcdFilePrint', [{name: '-r', arguments: [1,2]}, { name: '-i', arguments: []}, { name: '-o', arguments: []} ]);
7375 expect(result.hasDone).to.equal(false);
74- console.log(' '+result.comment);
76+ console.log(result.message);
7577 });
7678
7779 it('should return false when input file does not exist', function() {
78- var result = eos.execute('dcdFilePrint', [{name: '-r', arguments: [1,2,3]}, { name: '-i', arguments: ['hoge.txt']}, { name: '-o', arguments: ['hoge.txt']} ]);
80+ var result = eos.validate('dcdFilePrint', [{name: '-r', arguments: [1,2,3]}, { name: '-i', arguments: ['hoge.txt']}, { name: '-o', arguments: ['hoge.txt']} ]);
7981 expect(result.hasDone).to.equal(false);
80- console.log(' '+result.comment);
82+ console.log(result.message);
83+
8184 });
8285
8386 it('should return false when output file is not string', function() {
84- var result = eos.execute('dcdFilePrint', [{name: '-r', arguments: [1,2,3]}, { name: '-i', arguments: ['file1.txt']}, { name: '-o', arguments: [3]} ]);
87+ var result = eos.validate('dcdFilePrint', [{name: '-r', arguments: [1,2,3]}, { name: '-i', arguments: ['file1.txt']}, { name: '-o', arguments: [3]} ]);
8588 expect(result.hasDone).to.equal(false);
86- console.log(' '+result.comment);
89+ console.log(result.message);
90+
8791 });
8892
8993 it('should return false when output file has already existed', function() {
90- var result = eos.execute('dcdFilePrint', [{name: '-r', arguments: [1,2,3]}, { name: '-i', arguments: ['file1.txt']}, { name: '-o', arguments: ['file1.txt']} ]);
94+ var result = eos.validate('dcdFilePrint', [{name: '-r', arguments: [1,2,3]}, { name: '-i', arguments: ['file1.txt']}, { name: '-o', arguments: ['file1.txt']} ]);
9195 expect(result.hasDone).to.equal(false);
92- console.log(' '+result.comment);
96+ console.log(result.message);
97+
9398 });
99+ });
100+
101+ describe(test2, function() {
94102
95103 it('should return true when all options is proper.', function() {
96- var result = eos.execute('dcdFilePrint', [{name: '-r', arguments: [1,2,3]}, { name: '-i', arguments: ['file1.txt']}, { name: '-o', arguments: ['file3.txt']} ]);
97- expect(result.hasDone).to.equal(true);
98- console.log(' '+result.comment);
104+ var result = eos.toExecString('dcdFilePrint', [{name: '-r', arguments: [1,2,3]}, { name: '-i', arguments: ['file1.txt']}, { name: '-o', arguments: ['file3.txt']} ]);
105+ expect(result).to.equal('dcdFilePrint -r 1 2 3 -s 10 -e 100 -d 10 -m 0 -i file1.txt -o file3.txt');
99106 });
100-
101107 });
102108 });
Show on old repository browser