• R/O
  • HTTP
  • SSH
  • HTTPS

zephyr: Commit

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


Commit MetaInfo

Revisiondd26ea86201c28cf1606c033a11bda4fe1ca199c (tree)
Time2016-02-04 03:20:22
Authorhimetani_cafe <fumifumi@yasu...>
Commiterhimetani_cafe

Log Message

[DEBUG] Eos class and Testing with promise.

Change Summary

Incremental Difference

--- a/package.json
+++ b/package.json
@@ -4,7 +4,7 @@
44 "description": "",
55 "main": "./server/app.js",
66 "scripts": {
7- "test": "mocha test/mocha/DB.test.js"
7+ "test": "NODE_ENV=test mocha test/mocha/Eos.test.js"
88 },
99 "author": "",
1010 "license": "ISC",
--- a/server/class/Eos.js
+++ b/server/class/Eos.js
@@ -1,3 +1,4 @@
1+
12 /**
23 * Eosコマンドをエミュレートするクラス
34 * @varructor
@@ -7,7 +8,8 @@
78 var eos = {
89 validate: validate,
910 toExecString: toExecString,
10- execute: execute
11+ execute: execute,
12+ getFiles: getFiles
1113 }
1214
1315
@@ -18,13 +20,16 @@ var eos = {
1820
1921 // include all Eos command's info.
2022 // For seaching with O(n), the object key name is command name.
21-var commandReference = {
22- mrcImageInfo: { },
23- dcdFilePrint: { }
24-};
23+var db = require('./DB.js').instance;
24+var commandList = require(__dirname + '/../../user-specific-files/OptionControlFile/command_list.json');
25+var ocfReference = {};
26+
27+commandList.forEach(function(c) {
28+ ocfReference[c.name] = require(__dirname + '/../../user-specific-files/OptionControlFile/commands/' + c.name);
29+});
30+
2531
2632 // for unit test
27-var workspace = ['file1.txt', 'file2.txt'];
2833
2934
3035 /**
@@ -32,175 +37,212 @@ var workspace = ['file1.txt', 'file2.txt'];
3237 * Class variables
3338 */
3439
35-
36-/**
37- * validate
38- * コマンドとオプションのバリデーション
39- * @param command
40- * @param params
41- * @returns {valid: boolean, message: string}
42- */
43-function validate(command, options) {
44- var result = { hasDone: false, // true | false
45- comment: ''// string
46- };
47-
48- var ocf; // Array
49- var ocfObj = {}; // key-value
50-
51- try {
52- /**
53- * Check of command name
54- */
55- if(typeof command !== 'string') {
56- errorMsg = 'Command parameter need to be string';
57- throw new Error(errorMsg);
58- }
59-
60- var hasCommand = Object.keys(commandReference).indexOf(command) > -1;
61- if(!hasCommand) {
62- errorMsg = 'Command name is invalid';
63- throw new Error(errorMsg);
64- }
65-
66- /**
67- * Check of options
68- */
69-
70- if(!(Array.isArray(options))) {
71- errorMsg = 'Options need to be Array';
72- throw new Error(errorMsg);
73- }
74-
75-
40+function hasOneProperty(options) {
41+ return new Promise(function(resolve, reject) {
7642 if(options.length === 0) {
77- errorMsg = 'At least one option is required.';
78- throw new Error(errorMsg);
79- }
80-
81- // translate options to key-value and check whether options include correct member
82- var optionsObj = {};
83- var hasCorrectMember = true;
84- var isArgumentsArray = true;
85-
86- options.forEach(function(o) {
87- if(!(o.name) && !(o.arguments)) {
88- hasCorrectMember = false;
89- } else {
90- if(Array.isArray(o.arguments)) {
91- optionsObj[o.name] = o.arguments;
92- } else {
93- isArgumentsArray = false;
94- }
95- }
96- });
97-
98- // check each object has proberties "name" and "argumets"
99- if(!hasCorrectMember) {
100- errorMsg = 'Options need to include Object which have properties "name" and "arguments"';
43+ var errorMsg = 'At least one option is required.';
10144 throw new Error(errorMsg);
10245 }
46+ });
47+}
10348
104- // check each "argumets" properties is Array
105- if(!isArgumentsArray) {
106- errorMsg = 'Each "arguments" properties needs to be Array';
107- throw new Error(errorMsg);
108- }
109-
110- // Read OptionControlFile info of command
111- ocf = require(__dirname + '/../../user-specific-files/OptionControlFile/commands/' + command);
112-
113- // translate ocf info to key-value
49+function matchOption(options, ocf) {
50+ return new Promise(function(resolve, reject) {
51+ var ok = {};
11452 var notIncludingRequiredOptions = [];
53+ options.forEach(function(o) {
54+ ok[o.name] = o.arguments;
55+ });
11556 ocf.forEach(function(o) {
116- if(o.optionProperties && Object.keys(optionsObj).indexOf(o.option) < 0) {
57+ if(o.optionProperties && !ok[o.option]) {
11758 notIncludingRequiredOptions.push(o.option);
11859 }
119- ocfObj[o.option] = o;
12060 });
12161
12262 // check whether all required option exist
12363 if(notIncludingRequiredOptions.length > 0) {
124- errorMsg = 'Option ' + notIncludingRequiredOptions.toString() + ' are required';
125- throw new Error(errorMsg);
64+ var errorMsg = 'Option ' + notIncludingRequiredOptions.toString() + ' are required';
65+ reject(new Error(errorMsg));
66+ } else {
67+ resolve();
12668 }
69+ });
70+}
12771
128- var invalidArgumentsNumber= [];
129- var invalidArgumentType = [];
130- var invalidOutputFileName = [];
131-
132- // output file Regexp
133- var outRegExp = /out|append/;
134-
72+function validArgumentsNumber(options, ocfObj) {
73+ return new Promise(function(resolve, reject) {
13574 options.forEach(function(o) {
13675 // option number
13776 var expectNum = ocfObj[o.name].optionNumber;
13877 var actualNum = o.arguments.length;
13978 if(expectNum !== actualNum) {
140- invalidArgumentsNumber.push({name: o.name, expect: expectNum, actual: actualNum});
79+ reject(new Error('Invalid arguments number'));
14180 }
81+ });
82+ resolve();
83+ });
84+};
14285
143-
144- // argType and outFile name
86+function validArgumentsType(options, ocfObj, workspace) {
87+ return new Promise(function(resolve, reject) {
88+ options.forEach(function(o) {
14589 o.arguments.forEach(function(arg,i) {
14690 // argType
14791 var formType = ocfObj[o.name].arg[i].formType
14892 if(formType === 'select') { // This argument is filename
14993 var exist = workspace.indexOf(arg) > -1;
15094 if(!exist) {
151- invalidArgumentType.push({name: o.name, file: arg});
95+ reject(new Error(arg + ' doesn\'t exist.'));
15296 }
15397 } else {
15498 var expectType = formType === 'text' ? 'string' : 'number';
15599 var actualType = typeof arg;
156100 if(expectType !== actualType) {
157- invalidArgumentType.push({name: o.name, expect: expectType, actual: actualType});
101+ reject(new Error('argType is invalid'));
158102 }
159103 }
104+ });
105+ });
106+ resolve();
107+ });
108+}
160109
110+function validOutfileName(options, ocfObj, workspace) {
111+ return new Promise(function(resolve, reject) {
112+ // output file Regexp
113+ var outRegExp = /out|append/;
114+
115+ options.forEach(function(o) {
116+ o.arguments.forEach(function(arg,i) {
161117 // outFile name
162118 if(outRegExp.test(ocfObj[o.name].arg[i].argType)) {
163119 if(workspace.indexOf(o.arguments[i]) > -1) {
164- invalidOutputFileName.push({name: o.name, file: arg});
120+ reject(new Error('Invalid outfile name.'));
165121 }
166122 }
167123 });
168124 });
125+ resolve();
126+ });
127+}
169128
170- // check arguments number value
171- if(invalidArgumentsNumber.length > 0) {
172- errorMsg = '"arguments" properties is invalid number.\n';
173- invalidArgumentsNumber.forEach(function(i) {
174- errorMsg += i.name + ' expect to ' + i.expect + ', but actual ' + i.actual + '.\n';
175- });
176- throw new Error(errorMsg);
129+/**
130+ * validate
131+ * コマンドとオプションのバリデーション
132+ * @param command
133+ * @param params
134+ * @returns {valid: boolean, message: string}
135+ */
136+function validate(command, options, workspaceId) {
137+ return new Promise(function(resolve, reject) {
138+
139+ var ocf;
140+ if(ocfReference[command]) {
141+ ocf = ocfReference[command];
142+ } else {
143+ var errorMsg = 'Command name is invalid';
144+ reject(new Error(errorMsg));
177145 }
178146
179- // check arguments type
180- if(invalidArgumentType.length > 0) {
181- errorMsg = '"arguments" type is invalid.\n';
182- invalidArgumentType.forEach(function(i) {
183- if(i.file) {
184- errorMsg += i.name + ' ' + i.file + ' does not exist.\n';
147+ var ocfObj = {};
148+ ocf.forEach(function(o) {
149+ ocfObj[o.option] = o;
150+ });
151+
152+ var optionsObj = {};
153+ if(Array.isArray(options)) {
154+ options.forEach(function(o) {
155+ if(o.name && o.arguments) {
156+ if(Array.isArray(o.arguments)) {
157+ optionsObj[o.name] = o.arguments;
158+ } else {
159+ var errorMsg = 'Each "arguments" properties needs to be Array';
160+ reject(new Error(errorMsg));
161+ }
185162 } else {
186- errorMsg += i.name + ' expect to ' + i.expect + ', but actual ' + i.actual + '.\n';
163+ var errorMsg = 'Options need to include Object which have properties "name" and "arguments"';
164+ reject(new Error(errorMsg));
187165 }
188166 });
189- throw new Error(errorMsg);
167+ } else {
168+ var errorMsg = 'Options need to be Array';
169+ reject(new Error(errorMsg));
190170 }
191171
192- // check outFile name
193- if(invalidOutputFileName.length > 0) {
194- errorMsg = 'output file name is invalid.\n';
195- invalidOutputFileName.forEach(function(i) {
196- errorMsg += i.name + ' ' + i.file + ' has already existed.\n';
172+ getFiles(workspaceId)
173+ .then(function(workspace) {
174+
175+ // validate function
176+ var promises = [];
177+ promises.push(hasOneProperty(options));
178+ promises.push(matchOption(options, ocf));
179+ promises.push(validArgumentsNumber(options, ocfObj));
180+ promises.push(validArgumentsType(options, ocfObj, workspace));
181+ promises.push(validOutfileName(options, ocfObj, workspace));
182+
183+ // do validation
184+ return Promise.all(promises)
185+ .then(function() {
186+ resolve('Success');
187+ })
188+ .catch(function(error) {
189+ reject(error);
197190 });
198- throw new Error(errorMsg);
191+ });
192+ });
193+ /*
194+ var invalidArgumentType = [];
195+ var invalidOutputFileName = [];
196+
197+ // output file Regexp
198+ var outRegExp = /out|append/;
199+
200+ options.forEach(function(o) {
201+ // outFile name
202+ if(outRegExp.test(ocfObj[o.name].arg[i].argType)) {
203+ if(workspace.indexOf(o.arguments[i]) > -1) {
204+ invalidOutputFileName.push({name: o.name, file: arg});
205+ }
206+ }
207+ });
208+ });
209+
210+ // check arguments number value
211+ if(invalidArgumentsNumber.length > 0) {
212+ errorMsg = '"arguments" properties is invalid number.\n';
213+ invalidArgumentsNumber.forEach(function(i) {
214+ errorMsg += i.name + ' expect to ' + i.expect + ', but actual ' + i.actual + '.\n';
215+ });
216+ throw new Error(errorMsg);
217+ }
218+
219+ // check arguments type
220+ if(invalidArgumentType.length > 0) {
221+ errorMsg = '"arguments" type is invalid.\n';
222+ invalidArgumentType.forEach(function(i) {
223+ if(i.file) {
224+ errorMsg += i.name + ' ' + i.file + ' does not exist.\n';
225+ } else {
226+ errorMsg += i.name + ' expect to ' + i.expect + ', but actual ' + i.actual + '.\n';
227+ }
228+ });
229+ throw new Error(errorMsg);
230+ }
231+
232+ // check outFile name
233+ if(invalidOutputFileName.length > 0) {
234+ errorMsg = 'output file name is invalid.\n';
235+ invalidOutputFileName.forEach(function(i) {
236+ errorMsg += i.name + ' ' + i.file + ' has already existed.\n';
237+ });
238+ throw new Error(errorMsg);
239+ }
240+ } catch(e) {
241+ result.message = e.message;
242+ return result;
199243 }
200- } catch(e) {
201- result.message = e.message;
202- return result;
203- }
244+ });
245+ */
204246 }
205247
206248 /**
@@ -217,30 +259,30 @@ function toExecString(command, options) {
217259
218260 // set default parameters
219261 ocf.forEach(function(o) {
220- o.arg.forEach(function(arg) {
221- if(!(arg.initialValue === "") && arg.initialValue) {
222- if(!(finalOptions[o.option])) {
262+ o.arg.forEach(function(arg) {
263+ if(!(arg.initialValue === "") && arg.initialValue) {
264+ if(!(finalOptions[o.option])) {
223265 finalOptions[o.option] = [];
224266 finalOptions[o.option].push(arg.initialValue);
225- } else {
267+ } else {
226268 finalOptions[o.option].push(arg.initialValue);
227- }
228- }
229- });
230- });
269+ }
270+ }
271+ });
272+ });
231273
232274 // set user setting parameters
233275 options.forEach(function(o) {
234- finalOptions[o.name] = o.arguments;
235- });
276+ finalOptions[o.name] = o.arguments;
277+ });
236278
237279 // set execution string
238280 Object.keys(finalOptions).forEach(function(key) {
239- execStr += key + ' ';
240- finalOptions[key].forEach(function(arg) {
241- execStr += arg + ' ';
242- });
243- });
281+ execStr += key + ' ';
282+ finalOptions[key].forEach(function(arg) {
283+ execStr += arg + ' ';
284+ });
285+ });
244286
245287 // remove last blank
246288 execStr = execStr.slice(0,execStr.length-1);
@@ -264,4 +306,23 @@ function execute(command, options) {
264306 }
265307 }
266308
309+/**
310+ * getFiles
311+ * @param fileId
312+ * @returns {promise} resolve(Array)
313+ */
314+function getFiles(fileId) {
315+ return new Promise(function(resolve, reject) {
316+ if(process.env.NODE_ENV) {
317+ resolve(['file1.txt', 'file2.txt']);
318+ } else {
319+ db.getFiles(fileId)
320+ .then(function(r) {
321+ var workspace = r.map(function(f) { return f.name });
322+ resolve(workspace);
323+ });
324+ }
325+ });
326+}
327+
267328 module.exports = { instance: eos };
--- a/test/mocha/Eos.test.js
+++ b/test/mocha/Eos.test.js
@@ -1,7 +1,13 @@
11 (function() {
22 'use strict';
33 var eos = require('../../server/class/Eos').instance;
4- var expect = require('chai').expect;
4+ var chai = require('chai');
5+ var chaiAsPromised = require('chai-as-promised');
6+ chai.use(chaiAsPromised);
7+ var expect = chai.expect;
8+ chai.should();
9+
10+ var db = require('../../server/class/DB').instance;
511
612 var test1 = `
713 /**
@@ -30,80 +36,79 @@
3036
3137 describe(test1, function() {
3238
33- it('should return false when the command is typeof number.', function() {
34- var result = eos.validate(2);
35- expect(result.message).to.equal('Command parameter need to be string');
39+ it('should return Array', function() {
40+ return expect(eos.getFiles()).to.eventually.be.length(2);
3641 });
3742
38- it('should return false when command name is invalid', function() {
39- var result = eos.validate('hoge');
40- expect(result.message).to.equal('Command name is invalid');
43+ it('should be rejected(2)', function() {
44+ return eos.validate('hoge').should.be.rejectedWith(Error, 'Command name is invalid');
4145 });
4246
43- it('should return false when options is not Array', function() {
44- var result = eos.validate('mrcImageInfo', {});
45- expect(result.message).to.equal('Options need to be Array');
47+ it('should be rejected(3)', function() {
48+ return eos.validate('mrcImageNoiseAdd').should.be.rejectedWith(Error, 'Options need to be Array');
4649 });
4750
48- it('should return false when options is Array whose length is 0.', function() {
49- var result = eos.validate('mrcImageInfo', []);
50- expect(result.message).to.equal('At least one option is required.');
51+ it('should be rejected(4)', function() {
52+ return eos.validate('mrcImageNoiseAdd', []).should.be.rejectedWith(Error, 'At least one option is required.');
5153 });
5254
53- it('should return false when options is invalid Object which have not "name" and "argumetns"', function() {
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"');
55+ it('should return false when options is not Array', function() {
56+ return eos.validate('mrcImageNoiseAdd', {}).should.be.rejectedWith(Error, 'Options need to be Array');
5657 });
5758
58- it('should return false when "argumetns" properties are not Array', function() {
59- var result = eos.validate('mrcImageInfo', [{name: 'hoge', arguments: 'hoge'}]);
60- expect(result.message).to.equal('Each "arguments" properties needs to be Array');
59+ it('should return false when options is invalid Object which have not "name" and "argumetns"', function() {
60+ return eos.validate('mrcImageInfo', [{hoge: 'hoge'}]).should.be.rejectedWith(Error, 'Options need to include Object which have properties "name" and "arguments"');
6161 });
62-
6362 it('should return false when "argumetns" properties are not Array', function() {
64- var result = eos.validate('mrcImageInfo', [{name: 'hoge', arguments: 'hoge'}]);
65- expect(result.message).to.equal('Each "arguments" properties needs to be Array');
63+ return eos.validate('mrcImageInfo', [{name: 'hoge', arguments: 'hoge'}]).should.be.rejectedWith(Error,'Each "arguments" properties needs to be Array');
6664 });
6765
6866 it('should return false when required options do not exist', function() {
69- var result = eos.validate('mrcImageInfo', [{name: 'hoge', arguments: []}]);
70- expect(result.message).to.equal('Option -i are required');
67+ return eos.validate('mrcImageInfo', [{name: 'hoge', arguments: []}]).should.be.rejectedWith('Option -i are required');
7168 });
7269
7370 it('should return false when required options do not exist', function() {
74- var result = eos.validate('dcdFilePrint', [{name: '-r', arguments: [1,2]}, { name: '-i', arguments: []}, { name: '-o', arguments: []} ]);
75- expect(result.hasDone).to.equal(false);
76- console.log(result.message);
71+ return eos.validate('dcdFilePrint', [{name: '-r', arguments: [1,2]}, { name: '-i', arguments: []}, { name: '-o', arguments: []} ]).should.be.rejectedWith(Error, 'Invalid arguments number');
7772 });
7873
7974 it('should return false when input file does not exist', function() {
80- var result = eos.validate('dcdFilePrint', [{name: '-r', arguments: [1,2,3]}, { name: '-i', arguments: ['hoge.txt']}, { name: '-o', arguments: ['hoge.txt']} ]);
81- expect(result.hasDone).to.equal(false);
82- console.log(result.message);
83-
75+ return eos.validate('dcdFilePrint', [{name: '-r', arguments: [1,2,3]}, { name: '-i', arguments: ['hoge.txt']}, { name: '-o', arguments: ['hoge.txt']} ]).should.be.rejectedWith(Error, 'hoge.txt doesn\'t exist.');
8476 });
8577
8678 it('should return false when output file is not string', function() {
87- var result = eos.validate('dcdFilePrint', [{name: '-r', arguments: [1,2,3]}, { name: '-i', arguments: ['file1.txt']}, { name: '-o', arguments: [3]} ]);
88- expect(result.hasDone).to.equal(false);
89- console.log(result.message);
90-
79+ return eos.validate('dcdFilePrint', [{name: '-r', arguments: [1,2,3]}, { name: '-i', arguments: ['file1.txt']}, { name: '-o', arguments: [3]} ]).should.be.rejectedWith(Error, 'argType is invalid');
9180 });
9281
9382 it('should return false when output file has already existed', function() {
94- var result = eos.validate('dcdFilePrint', [{name: '-r', arguments: [1,2,3]}, { name: '-i', arguments: ['file1.txt']}, { name: '-o', arguments: ['file1.txt']} ]);
95- expect(result.hasDone).to.equal(false);
96- console.log(result.message);
97-
83+ return eos.validate('dcdFilePrint', [{name: '-r', arguments: [1,2,3]}, { name: '-i', arguments: ['file1.txt']}, { name: '-o', arguments: ['file1.txt']} ]).should.be.rejectedWith(Error, 'Invalid outfile name.');
9884 });
99- });
10085
86+ /*
10187 describe(test2, function() {
10288
10389 it('should return true when all options is proper.', function() {
10490 var result = eos.toExecString('dcdFilePrint', [{name: '-r', arguments: [1,2,3]}, { name: '-i', arguments: ['file1.txt']}, { name: '-o', arguments: ['file3.txt']} ]);
10591 expect(result).to.equal('dcdFilePrint -r 1 2 3 -s 10 -e 100 -d 10 -m 0 -i file1.txt -o file3.txt');
10692 });
93+ */
94+ });
95+
96+ describe('getFiles', function() {
97+ before(function() {
98+ process.env.NODE_ENV = '';
99+ return db.init()
100+ .then(function() {
101+ return Promise.all([db.test1(), db.test2(), db.testRest()])
102+ });
103+ });
104+
105+ it('should be resolved with length 4', function() {
106+ return expect(eos.getFiles('1f83f620-c1ed-11e5-9657-7942989daa00')).to.eventually.length(4);
107+ });
108+
109+ after(function() {
110+ process.env.NODE_ENV = 'test';
111+ });
107112 });
108113 });
109114 })();
Show on old repository browser