Bug:Fix file validation issue
[vnfsdk/refrepo.git] / vnfmarket / src / main / webapp / vnfmarket / node_modules / cliui / test / cliui.js
1 /* global describe, it */
2
3 require('chai').should()
4
5 var cliui = require('../')
6
7 describe('cliui', function () {
8   describe('div', function () {
9     it("wraps text at 'width' if a single column is given", function () {
10       var ui = cliui({
11         width: 10
12       })
13
14       ui.div('i am a string that should be wrapped')
15
16       ui.toString().split('\n').forEach(function (row) {
17         row.length.should.be.lte(10)
18       })
19     })
20
21     it('evenly divides text across columns if multiple columns are given', function () {
22       var ui = cliui({
23         width: 40
24       })
25
26       ui.div(
27         {text: 'i am a string that should be wrapped', width: 15},
28         'i am a second string that should be wrapped',
29         'i am a third string that should be wrapped'
30       )
31
32       // total width of all columns is <=
33       // the width cliui is initialized with.
34       ui.toString().split('\n').forEach(function (row) {
35         row.length.should.be.lte(40)
36       })
37
38       // it should wrap each column appropriately.
39       var expected = [
40        'i am a string  i am a      i am a third',
41        'that should be second      string that',
42        'wrapped        string that should be',
43        '               should be   wrapped',
44        '               wrapped'
45       ]
46
47       ui.toString().split('\n').should.eql(expected)
48     })
49
50     it('allows for a blank row to be appended', function () {
51       var ui = cliui({
52         width: 40
53       })
54
55       ui.div()
56
57       // it should wrap each column appropriately.
58       var expected = ['']
59
60       ui.toString().split('\n').should.eql(expected)
61     })
62   })
63
64   describe('_columnWidths', function () {
65     it('uses same width for each column by default', function () {
66       var ui = cliui({
67           width: 40
68         }),
69         widths = ui._columnWidths([{}, {}, {}])
70
71       widths[0].should.equal(13)
72       widths[1].should.equal(13)
73       widths[2].should.equal(13)
74     })
75
76     it('divides width over remaining columns if first column has width specified', function () {
77       var ui = cliui({
78           width: 40
79         }),
80         widths = ui._columnWidths([{width: 20}, {}, {}])
81
82       widths[0].should.equal(20)
83       widths[1].should.equal(10)
84       widths[2].should.equal(10)
85     })
86
87     it('divides width over remaining columns if middle column has width specified', function () {
88       var ui = cliui({
89           width: 40
90         }),
91         widths = ui._columnWidths([{}, {width: 10}, {}])
92
93       widths[0].should.equal(15)
94       widths[1].should.equal(10)
95       widths[2].should.equal(15)
96     })
97
98     it('keeps track of remaining width if multiple columns have width specified', function () {
99       var ui = cliui({
100           width: 40
101         }),
102         widths = ui._columnWidths([{width: 20}, {width: 12}, {}])
103
104       widths[0].should.equal(20)
105       widths[1].should.equal(12)
106       widths[2].should.equal(8)
107     })
108
109     it('uses a sane default if impossible widths are specified', function () {
110       var ui = cliui({
111           width: 40
112         }),
113         widths = ui._columnWidths([{width: 30}, {width: 30}, {padding: [0, 2, 0, 1]}])
114
115       widths[0].should.equal(30)
116       widths[1].should.equal(30)
117       widths[2].should.equal(4)
118     })
119   })
120
121   describe('alignment', function () {
122     it('allows a column to be right aligned', function () {
123       var ui = cliui({
124         width: 40
125       })
126
127       ui.div(
128         'i am a string',
129         {text: 'i am a second string', align: 'right'},
130         'i am a third string that should be wrapped'
131       )
132
133       // it should right-align the second column.
134       var expected = [
135        'i am a stringi am a secondi am a third',
136        '                    stringstring that',
137        '                          should be',
138        '                          wrapped'
139       ]
140
141       ui.toString().split('\n').should.eql(expected)
142     })
143
144     it('allows a column to be center aligned', function () {
145       var ui = cliui({
146         width: 60
147       })
148
149       ui.div(
150         'i am a string',
151         {text: 'i am a second string', align: 'center', padding: [0, 2, 0, 2]},
152         'i am a third string that should be wrapped'
153       )
154
155       // it should right-align the second column.
156       var expected = [
157        'i am a string          i am a second       i am a third string',
158        '                           string          that should be',
159        '                                           wrapped'
160       ]
161
162       ui.toString().split('\n').should.eql(expected)
163     })
164   })
165
166   describe('padding', function () {
167     it('handles left/right padding', function () {
168       var ui = cliui({
169         width: 40
170       })
171
172       ui.div(
173         {text: 'i have padding on my left', padding: [0, 0, 0, 4]},
174         {text: 'i have padding on my right', padding: [0, 2, 0, 0], align: 'center'},
175         {text: 'i have no padding', padding: [0, 0, 0, 0]}
176       )
177
178       // it should add left/right padding to columns.
179       var expected = [
180        '    i have     i have      i have no',
181        '    padding  padding on    padding',
182        '    on my     my right',
183        '    left'
184       ]
185
186       ui.toString().split('\n').should.eql(expected)
187     })
188
189     it('handles top/bottom padding', function () {
190       var ui = cliui({
191         width: 40
192       })
193
194       ui.div(
195         'i am a string',
196         {text: 'i am a second string', padding: [2, 0, 0, 0]},
197         {text: 'i am a third string that should be wrapped', padding: [0, 0, 1, 0]}
198       )
199
200       // it should add top/bottom padding to second
201       // and third columns.
202       var expected = [
203        'i am a string             i am a third',
204        '                          string that',
205        '             i am a secondshould be',
206        '             string       wrapped',
207        ''
208       ]
209
210       ui.toString().split('\n').should.eql(expected)
211     })
212   })
213
214   describe('wrap', function () {
215     it('allows wordwrap to be disabled', function () {
216       var ui = cliui({
217         wrap: false
218       })
219
220       ui.div(
221         {text: 'i am a string', padding: [0, 1, 0, 0]},
222         {text: 'i am a second string', padding: [0, 2, 0, 0]},
223         {text: 'i am a third string that should not be wrapped', padding: [0, 0, 0, 2]}
224       )
225
226       ui.toString().should.equal('i am a string i am a second string    i am a third string that should not be wrapped')
227     })
228   })
229
230   describe('span', function () {
231     it('appends the next row to the end of the prior row if it fits', function () {
232       var ui = cliui({
233         width: 40
234       })
235
236       ui.span(
237         {text: 'i am a string that will be wrapped', width: 30}
238       )
239
240       ui.div(
241         {text: ' [required] [default: 99]', align: 'right'}
242       )
243
244       var expected = [
245        'i am a string that will be',
246        'wrapped         [required] [default: 99]'
247       ]
248
249       ui.toString().split('\n').should.eql(expected)
250     })
251
252     it('does not append the string if it does not fit on the prior row', function () {
253       var ui = cliui({
254         width: 40
255       })
256
257       ui.span(
258         {text: 'i am a string that will be wrapped', width: 30}
259       )
260
261       ui.div(
262         {text: 'i am a second row', align: 'left'}
263       )
264
265       var expected = [
266        'i am a string that will be',
267        'wrapped',
268        'i am a second row'
269       ]
270
271       ui.toString().split('\n').should.eql(expected)
272     })
273
274     it('always appends text to prior span if wrap is disabled', function () {
275       var ui = cliui({
276         wrap: false,
277         width: 40
278       })
279
280       ui.span(
281         {text: 'i am a string that will be wrapped', width: 30}
282       )
283
284       ui.div(
285         {text: 'i am a second row', align: 'left', padding: [0, 0, 0, 3]}
286       )
287
288       ui.div('a third line')
289
290       var expected = [
291        'i am a string that will be wrapped   i am a second row',
292        'a third line'
293       ]
294
295       ui.toString().split('\n').should.eql(expected)
296     })
297   })
298
299   describe('layoutDSL', function () {
300     it('turns tab into multiple columns', function () {
301       var ui = cliui({
302         width: 60
303       })
304
305       ui.div(
306         '  <regex>  \tmy awesome regex\n  <my second thing>  \tanother row\t  a third column'
307       )
308
309       var expected = [
310        '  <regex>            my awesome regex',
311        '  <my second thing>  another row          a third column'
312       ]
313
314       ui.toString().split('\n').should.eql(expected)
315     })
316
317     it('turns newline into multiple rows', function () {
318       var ui = cliui({
319         width: 40
320       })
321
322       ui.div(
323         'Usage: $0\n  <regex>\t  my awesome regex\n  <glob>\t  my awesome glob\t  [required]'
324       )
325       var expected = [
326        'Usage: $0',
327        '  <regex>  my awesome regex',
328        '  <glob>   my awesome     [required]',
329        '           glob'
330       ]
331
332       ui.toString().split('\n').should.eql(expected)
333     })
334
335     it('does not apply DSL if wrap is false', function () {
336       var ui = cliui({
337         width: 40,
338         wrap: false
339       })
340
341       ui.div(
342         'Usage: $0\ttwo\tthree'
343       )
344
345       ui.toString().should.eql('Usage: $0\ttwo\tthree')
346     })
347
348   })
349 })