1 /* global describe, it */
3 require('chai').should()
5 var cliui = require('../')
7 describe('cliui', function () {
8 describe('div', function () {
9 it("wraps text at 'width' if a single column is given", function () {
14 ui.div('i am a string that should be wrapped')
16 ui.toString().split('\n').forEach(function (row) {
17 row.length.should.be.lte(10)
21 it('evenly divides text across columns if multiple columns are given', function () {
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'
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)
38 // it should wrap each column appropriately.
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',
47 ui.toString().split('\n').should.eql(expected)
50 it('allows for a blank row to be appended', function () {
57 // it should wrap each column appropriately.
60 ui.toString().split('\n').should.eql(expected)
64 describe('_columnWidths', function () {
65 it('uses same width for each column by default', function () {
69 widths = ui._columnWidths([{}, {}, {}])
71 widths[0].should.equal(13)
72 widths[1].should.equal(13)
73 widths[2].should.equal(13)
76 it('divides width over remaining columns if first column has width specified', function () {
80 widths = ui._columnWidths([{width: 20}, {}, {}])
82 widths[0].should.equal(20)
83 widths[1].should.equal(10)
84 widths[2].should.equal(10)
87 it('divides width over remaining columns if middle column has width specified', function () {
91 widths = ui._columnWidths([{}, {width: 10}, {}])
93 widths[0].should.equal(15)
94 widths[1].should.equal(10)
95 widths[2].should.equal(15)
98 it('keeps track of remaining width if multiple columns have width specified', function () {
102 widths = ui._columnWidths([{width: 20}, {width: 12}, {}])
104 widths[0].should.equal(20)
105 widths[1].should.equal(12)
106 widths[2].should.equal(8)
109 it('uses a sane default if impossible widths are specified', function () {
113 widths = ui._columnWidths([{width: 30}, {width: 30}, {padding: [0, 2, 0, 1]}])
115 widths[0].should.equal(30)
116 widths[1].should.equal(30)
117 widths[2].should.equal(4)
121 describe('alignment', function () {
122 it('allows a column to be right aligned', function () {
129 {text: 'i am a second string', align: 'right'},
130 'i am a third string that should be wrapped'
133 // it should right-align the second column.
135 'i am a stringi am a secondi am a third',
136 ' stringstring that',
141 ui.toString().split('\n').should.eql(expected)
144 it('allows a column to be center aligned', function () {
151 {text: 'i am a second string', align: 'center', padding: [0, 2, 0, 2]},
152 'i am a third string that should be wrapped'
155 // it should right-align the second column.
157 'i am a string i am a second i am a third string',
158 ' string that should be',
162 ui.toString().split('\n').should.eql(expected)
166 describe('padding', function () {
167 it('handles left/right padding', function () {
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]}
178 // it should add left/right padding to columns.
180 ' i have i have i have no',
181 ' padding padding on padding',
186 ui.toString().split('\n').should.eql(expected)
189 it('handles top/bottom padding', function () {
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]}
200 // it should add top/bottom padding to second
201 // and third columns.
203 'i am a string i am a third',
205 ' i am a secondshould be',
210 ui.toString().split('\n').should.eql(expected)
214 describe('wrap', function () {
215 it('allows wordwrap to be disabled', function () {
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]}
226 ui.toString().should.equal('i am a string i am a second string i am a third string that should not be wrapped')
230 describe('span', function () {
231 it('appends the next row to the end of the prior row if it fits', function () {
237 {text: 'i am a string that will be wrapped', width: 30}
241 {text: ' [required] [default: 99]', align: 'right'}
245 'i am a string that will be',
246 'wrapped [required] [default: 99]'
249 ui.toString().split('\n').should.eql(expected)
252 it('does not append the string if it does not fit on the prior row', function () {
258 {text: 'i am a string that will be wrapped', width: 30}
262 {text: 'i am a second row', align: 'left'}
266 'i am a string that will be',
271 ui.toString().split('\n').should.eql(expected)
274 it('always appends text to prior span if wrap is disabled', function () {
281 {text: 'i am a string that will be wrapped', width: 30}
285 {text: 'i am a second row', align: 'left', padding: [0, 0, 0, 3]}
288 ui.div('a third line')
291 'i am a string that will be wrapped i am a second row',
295 ui.toString().split('\n').should.eql(expected)
299 describe('layoutDSL', function () {
300 it('turns tab into multiple columns', function () {
306 ' <regex> \tmy awesome regex\n <my second thing> \tanother row\t a third column'
310 ' <regex> my awesome regex',
311 ' <my second thing> another row a third column'
314 ui.toString().split('\n').should.eql(expected)
317 it('turns newline into multiple rows', function () {
323 'Usage: $0\n <regex>\t my awesome regex\n <glob>\t my awesome glob\t [required]'
327 ' <regex> my awesome regex',
328 ' <glob> my awesome [required]',
332 ui.toString().split('\n').should.eql(expected)
335 it('does not apply DSL if wrap is false', function () {
342 'Usage: $0\ttwo\tthree'
345 ui.toString().should.eql('Usage: $0\ttwo\tthree')