UNPKG

3.34 kBtext/coffeescriptView Raw
1_ = require('lodash')
2chai = require('chai')
3expect = chai.expect
4
5state = require('../lib/state')
6Command = require('../lib/command')
7Signature = require('../lib/signature')
8settings = require('../lib/settings')
9
10describe 'State:', ->
11
12 it 'should have a commands array', ->
13 expect(state.commands).to.be.an.instanceof(Array)
14
15 it 'should have a globalOptions array', ->
16 expect(state.globalOptions).to.be.an.instanceof(Array)
17
18 it 'should have a permissions object', ->
19 expect(_.isObject(state.permissions)).to.be.true
20 expect(_.isArray(state.permissions)).to.be.false
21
22 describe '#getMatchCommand()', ->
23
24 beforeEach ->
25 @command = new Command
26 signature: new Signature('foo <bar>')
27 action: _.noop
28
29 state.commands = []
30 state.commands.push(@command)
31
32 it 'should return the command', (done) ->
33 state.getMatchCommand 'foo hello', (error, result) =>
34 expect(error).to.not.exist
35 expect(result).to.deep.equal(@command)
36 done()
37
38 it 'should return undefined if no match', (done) ->
39 state.getMatchCommand 'bar baz foo', (error, result) ->
40 expect(error).to.not.exist
41 expect(result).to.be.undefined
42 done()
43
44 it 'should match variadic arguments', (done) ->
45 state.commands = []
46 command = new Command
47 signature: new Signature('help [command...]')
48 action: _.noop
49 state.commands.push(command)
50
51 state.getMatchCommand 'help foo bar', (error, result) ->
52 expect(error).to.not.exist
53 expect(result).to.deep.equal(command)
54 done()
55
56 it 'should match required stdin arguments', (done) ->
57 state.commands = []
58 command = new Command
59 signature: new Signature('foo <|bar>')
60 action: _.noop
61 state.commands.push(command)
62
63 state.getMatchCommand 'foo', (error, result) ->
64 expect(error).to.not.exist
65 expect(result).to.deep.equal(command)
66 done()
67
68 it 'should match optional stdin arguments', (done) ->
69 state.commands = []
70 command = new Command
71 signature: new Signature('foo [|bar]')
72 action: _.noop
73 state.commands.push(command)
74
75 state.getMatchCommand 'foo', (error, result) ->
76 expect(error).to.not.exist
77 expect(result).to.deep.equal(command)
78 done()
79
80 describe 'if wildcard command is defined', ->
81
82 beforeEach ->
83 @wilcardCommand = new Command
84 signature: new Signature(settings.signatures.wildcard)
85 action: _.noop
86
87 state.commands.push(@wilcardCommand)
88
89 it 'should return that command if no match', (done) ->
90 state.getMatchCommand 'not defined command', (error, result) =>
91 expect(error).to.not.exist
92 expect(result).to.deep.equal(@wilcardCommand)
93 done()
94
95 describe '#findCommandBySignature()', ->
96
97 beforeEach ->
98 state.commands = []
99
100 state.commands.push new Command
101 signature: new Signature('foo <bar>')
102 action: _.noop
103
104 state.commands.push new Command
105 signature: new Signature('bar <baz>')
106 action: _.noop
107
108 state.commands.push new Command
109 signature: new Signature('version')
110 action: _.noop
111
112 it 'should be able to find a command', ->
113 command = state.findCommandBySignature('version')
114 expect(command).to.be.an.instanceof(Command)
115 expect(command.signature.toString()).to.equal('version')
116
117 it 'should return undefined if not found', ->
118 command = state.findCommandBySignature('not defined command')
119 expect(command).to.be.undefined