UNPKG

9.99 kBJavaScriptView Raw
1'use strict';
2
3var assert = require('assert')
4 , ase = assert.strictEqual
5 , ade = assert.deepEqual
6 , AsciiTable = require('./index')
7 , info = require('./package.json')
8
9describe('Ascii Table v' + info.version, function() {
10
11 describe('Examples', function() {
12
13 it('default', function() {
14 var table = new AsciiTable('A Title')
15 table
16 .setHeading('', 'Name', 'Age')
17 .addRow(1, 'Bob', 52)
18 .addRow(2, 'John', 34)
19 .addRow(3, 'Jim', 0)
20
21 var output = ''
22 + '.----------------.'
23 + '\n' + '| A Title |'
24 + '\n' + '|----------------|'
25 + '\n' + '| | Name | Age |'
26 + '\n' + '|---|------|-----|'
27 + '\n' + '| 1 | Bob | 52 |'
28 + '\n' + '| 2 | John | 34 |'
29 + '\n' + '| 3 | Jim | 0 |'
30 + '\n' + "'----------------'"
31
32 var table2 = new AsciiTable('A Title')
33 , headings = ['', 'Name', 'Age']
34 , row = [1, 'Bob', 52]
35
36 var matrix = [
37 [2, 'John', 34]
38 , [3, 'Jim', 0]
39 ]
40
41 table2
42 .setHeading(headings)
43 .addRow(row)
44 .addRowMatrix(matrix)
45
46 ase(table.toString(), output)
47 ase(table2.toString(), output)
48
49 })
50
51 it('dataObjects', function() {
52 var defaultOutput = ''
53 + '.----------------.'
54 + '\n' + '| A Title |'
55 + '\n' + '|----------------|'
56 + '\n' + '| | Name | Age |'
57 + '\n' + '|---|------|-----|'
58 + '\n' + '| 1 | Bob | 52 |'
59 + '\n' + '| 2 | John | 34 |'
60 + '\n' + '| 3 | Jim | 83 |'
61 + '\n' + "'----------------'"
62
63 var matrixOutput = ''
64 + '.----------------.'
65 + '\n' + '| A Title |'
66 + '\n' + '|----------------|'
67 + '\n' + '| | Name | Age |'
68 + '\n' + '|---|------|-----|'
69 + '\n' + '| 1 | Bob | 52 |'
70 + '\n' + '| 2 | John | 34 |'
71 + '\n' + '| | | 83 |'
72 + '\n' + "'----------------'"
73
74 var arrayData = [
75 {index: 2, name: 'John', age: 34, secondAge: 83}
76 , {index: 3, name: 'Jim', age: 83}
77 ]
78
79 var table = new AsciiTable('A Title')
80 , headings = ['', 'Name', 'Age']
81 , row = [1, 'Bob', 52]
82
83 table
84 .setHeading(headings)
85 .addRow(row)
86 .addData(arrayData, function(data) {return [data.index, data.name, data.age]})
87
88 var table2 = new AsciiTable('A Title')
89
90 table2
91 .setHeading(headings)
92 .addRow(row)
93 .addData([{index: 2, name: 'John', age: 34, secondAge: 83}], function(data) {return [[data.index, data.name, data.age], ["", "", data.secondAge]]}, true)
94
95 ase(table.toString(), defaultOutput)
96 ase(table2.toString(), matrixOutput)
97 })
98
99 it('prefixed', function() {
100 var table = new AsciiTable('A Title', {
101 prefix: ' '
102 })
103 table
104 .setHeading('', 'Name', 'Age')
105 .addRow(1, 'Bob', 52)
106 .addRow(2, 'John', 34)
107 .addRow(3, 'Jim', 83)
108
109 var output = ''
110 + ' .----------------.'
111 + '\n' + ' | A Title |'
112 + '\n' + ' |----------------|'
113 + '\n' + ' | | Name | Age |'
114 + '\n' + ' |---|------|-----|'
115 + '\n' + ' | 1 | Bob | 52 |'
116 + '\n' + ' | 2 | John | 34 |'
117 + '\n' + ' | 3 | Jim | 83 |'
118 + '\n' + " '----------------'"
119
120 ase(table.toString(), output)
121 })
122
123 it('all', function() {
124 var table = new AsciiTable('Something')
125 table
126 .setBorder()
127 .removeBorder()
128 .setAlign(0, AsciiTable.CENTER)
129 .setAlignLeft(1)
130 .setAlignCenter(1)
131 .setAlignRight(1)
132
133 .setTitle('Hi')
134 .setTitleAlign(AsciiTable.LEFT)
135 .setTitleAlignLeft(1)
136 .setTitleAlignCenter(1)
137 .setTitleAlignRight(1)
138
139 .setHeading('one', 'two', 'three')
140 .setHeading(['one', 'two', 'three'])
141 .setHeadingAlign(0, AsciiTable.CENTER)
142 .setHeadingAlignLeft(1)
143 .setHeadingAlignCenter(1)
144 .setHeadingAlignRight(1)
145
146 .addRow(1, 2, 3)
147 .addRow([4, 5, 6])
148 .addRowMatrix([
149 [7, 8, 9]
150 , [10, 11, 12]
151 ])
152 .setJustify()
153 .setJustify(false)
154
155 .sort(function(a, b) { return a })
156 .sortColumn(1, function(a, b) { return a })
157
158 table.toJSON()
159 table.toString()
160 table.valueOf()
161 table.render()
162 })
163
164 it('alignment', function() {
165 var table = new AsciiTable()
166 table
167 .setTitle('Something')
168 .setTitleAlign(AsciiTable.LEFT)
169 .setHeading('', 'Name', 'Age')
170 .setHeadingAlign(AsciiTable.RIGHT)
171 .setAlignCenter(0)
172 .setAlign(2, AsciiTable.RIGHT)
173 .addRow('a', 'apple', 'Some longer string')
174 .addRow('b', 'banana', 'hi')
175 .addRow('c', 'carrot', 'meow')
176 .addRow('efg', 'elephants')
177
178 var str = ""
179 + ".--------------------------------------."
180 + "\n" + "| Something |"
181 + "\n" + "|--------------------------------------|"
182 + "\n" + "| | Name | Age |"
183 + "\n" + "|-----|-----------|--------------------|"
184 + "\n" + "| a | apple | Some longer string |"
185 + "\n" + "| b | banana | hi |"
186 + "\n" + "| c | carrot | meow |"
187 + "\n" + "| efg | elephants | |"
188 + "\n" + "'--------------------------------------'"
189 ase(str, table.toString())
190 })
191 })
192
193 describe('Static methods', function() {
194
195 it('#version', function() {
196 ase(info.version, AsciiTable.VERSION)
197 ase(info.version, require('./ascii-table.min').VERSION)
198 ase(info.version, require('./bower.json').version)
199 })
200
201 it('#align', function() {
202 ase(AsciiTable.align(AsciiTable.LEFT, 'a', 10), AsciiTable.alignLeft('a', 10))
203 ase(AsciiTable.align(AsciiTable.CENTER, 'a', 10), AsciiTable.alignCenter('a', 10))
204 ase(AsciiTable.align(AsciiTable.RIGHT, 'a', 10), AsciiTable.alignRight('a', 10))
205 })
206
207 it('#alignLeft', function() {
208 var str = AsciiTable.alignLeft('foo', 30)
209 ase(str, 'foo ')
210
211 var str = AsciiTable.alignLeft(null, 30)
212 ase(str, ' ')
213
214 var str = AsciiTable.alignLeft('bar', 10, '-')
215 ase(str, 'bar-------')
216
217 var str = AsciiTable.alignLeft('meow', 1, '-')
218 ase(str, 'meow')
219 })
220
221 it('#alignRight', function() {
222 var str = AsciiTable.alignRight('foo', 30)
223 ase(str, ' foo')
224
225 var str = AsciiTable.alignRight(null, 30)
226 ase(str, ' ')
227
228 var str = AsciiTable.alignRight('bar', 10, '-')
229 ase(str, '-------bar')
230
231 var str = AsciiTable.alignRight('meow', 1, '-')
232 ase(str, 'meow')
233 })
234
235 it('#alignCenter', function() {
236 var str = AsciiTable.alignCenter('foo', 30)
237 ase(str, ' foo ')
238
239 var str = AsciiTable.alignCenter(null, 30)
240 ase(str, ' ')
241
242
243 var str = AsciiTable.alignCenter('bar', 10, '-')
244 ase(str, '---bar----')
245
246 var str = AsciiTable.alignCenter('bars', 10, '-')
247 ase(str, '---bars---')
248
249 var str = AsciiTable.alignCenter('bar', 11, '-')
250 ase(str, '----bar----')
251 })
252
253 it('#alignAuto', function() {
254
255 })
256
257 it('#arrayFill', function() {
258 var arr = AsciiTable.arrayFill(10, '-')
259 ase(arr.length, 10)
260 ase(arr[0], '-')
261 })
262
263 it('#factory', function() {
264 var table = AsciiTable.factory('title')
265 ase(table instanceof AsciiTable, true)
266 ase(table.getTitle(), 'title')
267 })
268
269 it('#factory with object', function() {
270 var obj = {
271 title: 'foo',
272 heading: ['id', 'name'],
273 rows: [
274 [1, 'bob'],
275 [2, 'jim']
276 ]
277 }
278 var table = AsciiTable.factory(obj)
279 ase(table instanceof AsciiTable, true)
280 ase(table.getTitle(), 'foo')
281 })
282 })
283
284 describe('Instance methods', function() {
285
286 it('#setBorder', function() {
287 var table = new AsciiTable('a')
288 table
289 .setBorder('*')
290 .setHeading('one', 'two')
291 .addRow('abc', 'def')
292
293 var str = ''
294 + '*************'
295 + '\n' + '* a *'
296 + '\n' + '*************'
297 + '\n' + '* one * two *'
298 + '\n' + '*************'
299 + '\n' + '* abc * def *'
300 + '\n' + '*************'
301
302 ase(str, table.toString())
303 })
304
305 it('#removeBorder', function() {
306
307 })
308
309 it('#setAlign', function() {
310
311 })
312
313 it('#setAlignLeft', function() {
314
315 })
316
317 it('#setAlignCenter', function() {
318
319 })
320
321 it('#setAlignRight', function() {
322
323 })
324
325 it('#setTitle', function() {
326 var table = new AsciiTable('meow')
327 ase(table.getTitle(), 'meow')
328 table.setTitle('bark')
329 ase(table.getTitle(), 'bark')
330 })
331
332 it('#sort', function() {
333
334 })
335
336 it('#sortColumn', function() {
337
338 })
339
340 it('#setHeading', function() {
341 })
342
343 it('#addRow', function() {
344
345 })
346
347 it('#setJustify', function() {
348
349 })
350
351 it('#toString', function() {
352
353 })
354
355 it('#toJSON', function() {
356 var table = new AsciiTable('cat')
357 table
358 .setHeading('one', 'two', 'three')
359 .addRow(1, 2, 3)
360 .addRow(4, 5, 6)
361
362 var output = {
363 title: 'cat'
364 , heading: ['one', 'two', 'three']
365 , rows: [
366 [1, 2, 3]
367 , [4, 5, 6]
368 ]
369 }
370 var js = table.toJSON()
371 ade(js, output)
372
373 js.heading[0] = 'test'
374 ase(table.getHeading()[0], 'one')
375
376 js.rows[0][0] = 'test'
377 ase(table.getRows()[0][0], 1)
378 })
379
380 it('#clear', function() {
381
382 })
383
384 it('#clearRows', function() {
385
386 })
387 })
388})