-
Notifications
You must be signed in to change notification settings - Fork 1
/
spec.coffee
301 lines (243 loc) · 9.25 KB
/
spec.coffee
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
vorpalSOP = require './index'
Vorpal = require 'vorpal'
vorpalLog = require 'vorpal-log'
Command = require 'vorpal/lib/command'
# Needed because too many vorpal emmiters are added otherwise
require('events').EventEmitter.defaultMaxListeners = Infinity
vorpal = null
sop = null
obj = null
key = null
cmd = null
cmdOptions = null
customValidationResult = null
initVorpal = ->
vorpal = null
sop = null
vorpal = Vorpal()
vorpal.use vorpalSOP
sop = vorpal.sop
obj = {foo: 'bar'}
addExampleCommand = ->
key = 'foo'
sop.command key, obj
cmd = vorpal.find key
addCommandWithOptions = ->
key = 'foo'
customValidationResult = 'öwalföwa'
cmdOptions =
validate: (arg) ->
if arg is 'baz'
return null
else
return customValidationResult
print: (arg) ->
return true
passedValidation: (key, arg, value) ->
return true
failedValidation: (key, arg) ->
return true
sop.command key, obj, cmdOptions
cmd = vorpal.find key
describe 'The vorpal-sop extension', ->
beforeEach ->
vorpal = null
sop = null
it 'adds a sop object to vorpal', ->
vorpal = Vorpal()
expect(vorpal.sop).not.toBeDefined()
vorpal.use vorpalSOP
expect(vorpal.sop).toBeDefined()
expect(vorpal.sop).not.toBeNull()
it 'saves options as sop.options', ->
options = {foo: 'bar'}
vorpal = Vorpal()
vorpal.use vorpalSOP, options
expect(vorpal.sop.options).toBe options
describe 'The command function', ->
beforeEach ->
initVorpal()
it 'is exposed by the sop object', ->
expect(sop.command).toBeDefined()
expect(typeof sop.command).toBe 'function'
it 'adds a command called [key] to vorpal', ->
key = 'foo'
expect(vorpal.find key).toBeUndefined()
sop.command key, obj
expect(vorpal.find key).not.toBeUndefined()
it 'returns the added command', ->
expect(sop.command(key, obj) instanceof Command).toBe true
describe 'An added command', ->
beforeEach ->
initVorpal()
addExampleCommand()
it 'has a description based on sop.options.describe', ->
spyOn(sop.options, 'describe').andCallThrough()
expect(cmd.description()).toBe sop.options.describe key
expect(sop.options.describe.calls.length).toBe 1
expect(sop.options.describe.calls[0].args[0]).toBe key
it 'calls sop.options.print with obj[key] if called without argument', ->
spyOn sop.options, 'print'
vorpal.exec "#{key}", (err, data) ->
expect(sop.options.print.calls.length).toBe 1
expect(sop.options.print.calls[0].args[0]).toBe obj[key]
it 'sets obj.key to the argument if called with one', ->
newValue = 'flkewjfö'
vorpal.exec "#{key} #{newValue}", (err, data) ->
expect(obj[key]).toBe newValue
it 'calls sop.options.passedValidation with key, arg, value (if validation passed)', ->
spyOn sop.options, 'passedValidation'
newValue = 'flkewjfö'
vorpal.exec "#{key} #{newValue}", (err, data) ->
expect(sop.options.passedValidation.calls.length).toBe 1
expect(sop.options.passedValidation.calls[0].args[0]).toBe key
expect(sop.options.passedValidation.calls[0].args[1]).toBe newValue
expect(sop.options.passedValidation.calls[0].args[2]).toBe newValue
describe 'A command with a validate function but without a validationFailed callback', ->
beforeEach ->
initVorpal()
key = 'foo'
cmdOptions =
validate: (arg) ->
return null
sop.command key, obj, cmdOptions
cmd = vorpal.find key
it 'calls sop.options.failedValidation with key, arg (if validation failed)', ->
spyOn sop.options, 'failedValidation'
newValue = 'flkewjfö'
vorpal.exec "#{key} #{newValue}", (err, data) ->
expect(sop.options.failedValidation.calls.length).toBe 1
expect(sop.options.failedValidation.calls[0].args[0]).toBe key
expect(sop.options.failedValidation.calls[0].args[1]).toBe newValue
describe 'A command added with a validation method', ->
beforeEach ->
initVorpal()
addCommandWithOptions()
it 'sets obj[key] to the result of val(args[key])', ->
arg = 'öjföwifjäwf'
vorpal.exec "#{key} #{arg}", (err, data) ->
expect(obj[key]).toBe cmdOptions.validate(arg)
it 'lets obj.key remain unchanged if val(args[key] is null)', ->
expect(cmdOptions.validate 'baz').toBeNull()
oldValue = obj[key]
vorpal.exec "#{key} baz", (err, data) ->
expect(obj[key]).toBe oldValue
describe 'A command added with a passedValidation method', ->
beforeEach ->
initVorpal()
addCommandWithOptions()
it 'calls this passedValidation method with key, arg, value (if validation actually passed)', ->
spyOn sop.options, 'passedValidation'
spyOn cmdOptions, 'passedValidation'
newValue = 'flkewjfö'
vorpal.exec "#{key} #{newValue}", (err, data) ->
expect(sop.options.passedValidation.calls.length).toBe 0
expect(cmdOptions.passedValidation.calls.length).toBe 1
expect(cmdOptions.passedValidation.calls[0].args[0]).toBe key
expect(cmdOptions.passedValidation.calls[0].args[1]).toBe newValue
expect(cmdOptions.passedValidation.calls[0].args[2]).toBe customValidationResult
describe 'A command added with a failedValidation method', ->
beforeEach ->
initVorpal()
addCommandWithOptions()
it 'calls this failedValidation method with key, arg (if validation actually passed)', ->
spyOn sop.options, 'failedValidation'
spyOn cmdOptions, 'failedValidation'
newValue = 'baz'
vorpal.exec "#{key} #{newValue}", (err, data) ->
expect(sop.options.failedValidation.calls.length).toBe 0
expect(cmdOptions.failedValidation.calls.length).toBe 1
expect(cmdOptions.failedValidation.calls[0].args[0]).toBe key
expect(cmdOptions.failedValidation.calls[0].args[1]).toBe newValue
describe 'A command added with a print method', ->
beforeEach ->
initVorpal()
addCommandWithOptions()
it 'calls this print method with obj[key] if called without argument, instead of the global default print', ->
spyOn sop.options, 'print'
spyOn cmdOptions, 'print'
vorpal.exec "#{key}", (err, data) ->
expect(sop.options.print.calls.length).toBe 0
expect(cmdOptions.print.calls.length).toBe 1
expect(cmdOptions.print.calls[0].args[0]).toBe obj[key]
describe 'The default describe method', ->
beforeEach ->
initVorpal()
addExampleCommand()
it 'exists', ->
expect(sop.options.describe).toBeDefined()
expect(typeof sop.options.describe).toBe 'function'
it 'returns "set or print #{key}"', ->
expect(sop.options.describe key).toBe "set or print #{key}"
describe 'The default print method', ->
msg = null
beforeEach ->
initVorpal()
addExampleCommand()
msg = 'foo'
it 'exists', ->
expect(sop.options.print).toBeDefined()
expect(typeof sop.options.print).toBe 'function'
it 'delegates to vorpal.log outside of a command and without vorpal-log', ->
spyOn vorpal, 'log'
# make sure the default behavior is used
expect(vorpal.logger).toBeUndefined()
sop.options.print msg
expect(vorpal.log.calls.length).toBe 1
expect(vorpal.log.calls[0].args[0]).toBe msg
it 'uses vorpal.logger methods if available', ->
vorpal.use vorpalLog
expect(vorpal.logger).toBeDefined()
spyOn vorpal.logger, 'info'
sop.options.print msg
expect(vorpal.logger.info.calls.length).toBe 1
expect(vorpal.logger.info.calls[0].args[0]).toBe msg
describe 'The default failedValidation method', ->
key = 'foo'
arg = 'qux'
expectedMsg = "#{arg} is an invalid value for #{key}"
beforeEach ->
initVorpal()
addExampleCommand()
it 'exists', ->
expect(sop.options.failedValidation).toBeDefined()
expect(typeof sop.options.failedValidation).toBe 'function'
it 'uses vorpal.instance.print without vorpal-log', ->
spyOn vorpal.session, 'log'
# make sure the default behavior is used
expect(vorpal.logger).toBeUndefined()
sop.options.failedValidation key, arg
expect(vorpal.session.log.calls.length).toBe 1
expect(vorpal.session.log.calls[0].args[0]).toBe expectedMsg
it 'uses vorpal.logger methods if available', ->
vorpal.use vorpalLog
expect(vorpal.logger).toBeDefined()
spyOn vorpal.logger, 'error'
sop.options.failedValidation key, arg
expect(vorpal.logger.error.calls.length).toBe 1
expect(vorpal.logger.error.calls[0].args[0]).toBe expectedMsg
describe 'The default passedValidation method', ->
key = 'foo'
arg = 'qux'
value = 'qux'
expectedMsg = "set #{key} to #{arg}"
beforeEach ->
initVorpal()
addExampleCommand()
it 'exists', ->
expect(sop.options.passedValidation).toBeDefined()
expect(typeof sop.options.failedValidation).toBe 'function'
it 'uses vorpal.instance.print without vorpal-log', ->
spyOn vorpal.session, 'log'
# make sure the default behavior is used
expect(vorpal.logger).toBeUndefined()
sop.options.passedValidation key, arg, value
expect(vorpal.session.log.calls.length).toBe 1
expect(vorpal.session.log.calls[0].args[0]).toBe expectedMsg
it 'uses vorpal.logger methods if available', ->
vorpal.use vorpalLog
expect(vorpal.logger).toBeDefined()
spyOn vorpal.logger, 'confirm'
sop.options.passedValidation key, arg
expect(vorpal.logger.confirm.calls.length).toBe 1
expect(vorpal.logger.confirm.calls[0].args[0]).toBe expectedMsg