OSDN Git Service

[TEST] eos execution parameter check done.
[eos/zephyr.git] / test / mocha / Eos.test.js
1 (function() {
2     'use strict';
3     var eos = require('../../server/class/Eos').instance;
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;
11     var testCommand = require('./testCommand.json');
12
13     var test1 = `
14     /**
15     * コマンドとオプションのバリデーション
16     * @param {string} Eosコマンドの名前
17     * @param {Array<Object>} コマンドに渡すオプション
18     * @returns {valid: boolean, message: string}
19     * function validate(command, params) {
20     */
21     `;
22
23     var test2 = `
24     /**
25     * 実行時文字列への変換 
26     *
27     * @param command
28     * @param options
29     * @returns {string}
30     * function toExecString(command, params) {
31     */
32     `;
33
34     describe('Eos クラス', function() {
35         before(function() {
36         });
37
38         describe(test1, function() {
39
40             it('should return Array', function() {
41                 return expect(eos.getFiles()).to.eventually.be.length(2);
42             });
43
44             it('should be rejected(2)', function() {
45                 return eos.validate('hoge').should.be.rejectedWith(Error, 'Command name is invalid');
46             });
47
48             it('should be rejected(3)', function() {
49                 return eos.validate('mrcImageNoiseAdd').should.be.rejectedWith(Error, 'Options need to be Array');
50             });
51
52             it('should be rejected(4)', function() {
53                 return eos.validate('mrcImageNoiseAdd', []).should.be.rejectedWith(Error, 'At least one option is required.');
54             });
55
56             it('should return false when options is not Array', function() {
57                 return eos.validate('mrcImageNoiseAdd', {}).should.be.rejectedWith(Error, 'Options need to be Array');
58             });
59
60             it('should return false when options is invalid Object which have not "name" and "argumetns"', function() {
61                 return eos.validate('mrcImageInfo', [{hoge: 'hoge'}]).should.be.rejectedWith(Error, 'Options need to include Object which have properties "name" and "arguments"');
62             });
63             it('should return false when "argumetns" properties are not Array', function() {
64                 return eos.validate('mrcImageInfo', [{name: 'hoge', arguments: 'hoge'}]).should.be.rejectedWith(Error,'Each "arguments" properties needs to be Array');
65             });
66
67             it('should return false when required options do not exist', function() {
68                 return eos.validate('mrcImageInfo', [{name: 'hoge', arguments: []}]).should.be.rejectedWith('Option -i are required');
69             });
70
71             it('should return false when required options do not exist', function() {
72                 return eos.validate('dcdFilePrint', [{name: '-r', arguments: [1,2]}, { name: '-i', arguments: []}, { name: '-o', arguments: []} ]).should.be.rejectedWith(Error, 'Invalid arguments number');
73             });
74
75             it('should return false when input file does not exist', function() {
76                 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.');
77             });
78
79             it('should return false when output file is not string', function() {
80                 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');
81             });
82
83             it('should return false when output file has already existed', function() {
84                 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.');
85             });
86
87         });
88
89         describe(test2, function() {
90             it('should return true when all options is proper.', function() {
91                 var result = eos.toExecString('dcdFilePrint', [{name: '-r', arguments: [1,2,3]}, { name: '-i', arguments: ['file1.txt']}, { name: '-o', arguments: ['file3.txt']} ]);
92                 expect(result).to.equal('dcdFilePrint -r 1 2 3 -s 10 -e 100 -d 10 -m 0 -i file1.txt -o file3.txt');
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             it('should be resolved about testCommand', function() {
110                 var command = testCommand.command;
111                 var options = testCommand.options;
112                 var workspace = testCommand.workspace;
113                 return eos.validate(command, options, workspace).should.be.fulfilled;
114             });
115
116             after(function() {
117                 process.env.NODE_ENV = 'test';
118             });
119         });
120     });
121 })();