1 | import 'reflect-metadata';
|
2 | import kernel from '../inversify.config';
|
3 | import {expect} from 'chai';
|
4 | import * as sinon from 'sinon';
|
5 | import {CommandLine} from "../interfaces/command-line";
|
6 | import {Command} from "../interfaces/command";
|
7 | describe('CommandLine', function () {
|
8 | let commandLine: CommandLine;
|
9 | let testCommandArray: string[] = [];
|
10 | let testCommandArrayArray: string[][] = [];
|
11 | before(()=> {
|
12 | commandLine = kernel.get<CommandLine>('CommandLine');
|
13 | });
|
14 | describe('create using kernel', ()=> {
|
15 | it('should be created by kernel', function (done) {
|
16 | expect(commandLine).to.not.equal(null);
|
17 | done();
|
18 | });
|
19 | });
|
20 | before(()=> {
|
21 | commandLine = kernel.get<CommandLine>('CommandLine');
|
22 | });
|
23 | describe('printTable', ()=> {
|
24 | let consoleTableStub: any;
|
25 | let testRows = ['row1', 'row2'];
|
26 | before(()=> {
|
27 | consoleTableStub = sinon.stub(console, 'table', (rows)=> {
|
28 | expect(rows).to.equal(testRows);
|
29 | });
|
30 | });
|
31 | it('should call printTable', function (done) {
|
32 | commandLine.printTable(testRows);
|
33 | expect(consoleTableStub.called).to.equal(true);
|
34 | done();
|
35 | });
|
36 | after(()=> {
|
37 | (<any>console.table).restore();
|
38 | });
|
39 | });
|
40 | before(()=> {
|
41 | commandLine = kernel.get<CommandLine>('CommandLine');
|
42 | });
|
43 | describe('CommandLine.addCommand', ()=> {
|
44 | before(()=> {
|
45 | commandLine.init({
|
46 | version: ()=> {
|
47 | return 'version_number';
|
48 | }
|
49 | });
|
50 | commandLine.addCommand(testCommand(testCommandArrayArray, (argv)=> {
|
51 | for(let i = 0;i < argv._.length;++i){
|
52 | expect(argv._[i]).to.equal(testCommandArray[i]);
|
53 | }
|
54 | }));
|
55 | });
|
56 | describe('commandLine.exec()', ()=> {
|
57 | it('commands invoked correct functions', done=> {
|
58 | testCommandArrayArray.forEach(command=> {
|
59 | commandLine.exec(testCommandArray = command);
|
60 | });
|
61 | done();
|
62 | });
|
63 | });
|
64 | }
|
65 | );
|
66 | });
|
67 | function testCommand(commands: string[][], cb: (argv: any)=>void) {
|
68 | let newCommand = kernel.get<Command>('CommandImpl');
|
69 | newCommand.aliases = ['alias-top-1', 'alias-top-2'];
|
70 | newCommand.commandDesc = 'topCommandDesc';
|
71 | for (let i = 0; i < 3; ++i) {
|
72 | let newSubCommand = kernel.get<Command>('CommandImpl');
|
73 | newSubCommand.aliases = ['alias-sub-1-1-' + i, 'alias-sub-1-2-' + i];
|
74 | newSubCommand.commandDesc = 'subCommandDesc-' + i;
|
75 |
|
76 | if (i & 1) {
|
77 | commands.push([newCommand.aliases[0], newSubCommand.aliases[0]]);
|
78 | newSubCommand.handler = cb;
|
79 | } else {
|
80 | for (let j = 0; j < 3; ++j) {
|
81 | let newSubSubCommand = kernel.get<Command>('CommandImpl');
|
82 | newSubSubCommand.aliases = ['alias-sub-1-1-' + i + '-' + j, 'alias-sub-1-2-' + i + '-' + j];
|
83 | newSubSubCommand.commandDesc = 'subCommandDesc-' + i + '-' + j;
|
84 | commands.push([newCommand.aliases[0], newSubCommand.aliases[0], newSubSubCommand.aliases[0]]);
|
85 | newSubSubCommand.handler = cb;
|
86 | newSubCommand.subCommands.push(newSubSubCommand);
|
87 | }
|
88 | }
|
89 | newCommand.subCommands.push(newSubCommand);
|
90 | }
|
91 | return newCommand;
|
92 | }
|