UNPKG

8.1 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', 83)
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 | 83 |'
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', 83]
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 it('prefixed', function() {
51 var table = new AsciiTable('A Title', {
52 prefix: ' '
53 })
54 table
55 .setHeading('', 'Name', 'Age')
56 .addRow(1, 'Bob', 52)
57 .addRow(2, 'John', 34)
58 .addRow(3, 'Jim', 83)
59
60 var output = ''
61 + ' .----------------.'
62 + '\n' + ' | A Title |'
63 + '\n' + ' |----------------|'
64 + '\n' + ' | | Name | Age |'
65 + '\n' + ' |---|------|-----|'
66 + '\n' + ' | 1 | Bob | 52 |'
67 + '\n' + ' | 2 | John | 34 |'
68 + '\n' + ' | 3 | Jim | 83 |'
69 + '\n' + " '----------------'"
70
71 ase(table.toString(), output)
72 })
73
74 it('all', function() {
75 var table = new AsciiTable('Something')
76 table
77 .setBorder()
78 .removeBorder()
79 .setAlign(0, AsciiTable.CENTER)
80 .setAlignLeft(1)
81 .setAlignCenter(1)
82 .setAlignRight(1)
83
84 .setTitle('Hi')
85 .setTitleAlign(AsciiTable.LEFT)
86 .setTitleAlignLeft(1)
87 .setTitleAlignCenter(1)
88 .setTitleAlignRight(1)
89
90 .setHeading('one', 'two', 'three')
91 .setHeading(['one', 'two', 'three'])
92 .setHeadingAlign(0, AsciiTable.CENTER)
93 .setHeadingAlignLeft(1)
94 .setHeadingAlignCenter(1)
95 .setHeadingAlignRight(1)
96
97 .addRow(1, 2, 3)
98 .addRow([4, 5, 6])
99 .addRowMatrix([
100 [7, 8, 9]
101 , [10, 11, 12]
102 ])
103 .setJustify()
104 .setJustify(false)
105
106 .sort(function(a, b) { return a })
107 .sortColumn(1, function(a, b) { return a })
108
109 table.toJSON()
110 table.toString()
111 table.valueOf()
112 table.render()
113 })
114
115 it('alignment', function() {
116 var table = new AsciiTable()
117 table
118 .setTitle('Something')
119 .setTitleAlign(AsciiTable.LEFT)
120 .setHeading('', 'Name', 'Age')
121 .setHeadingAlign(AsciiTable.RIGHT)
122 .setAlignCenter(0)
123 .setAlign(2, AsciiTable.RIGHT)
124 .addRow('a', 'apple', 'Some longer string')
125 .addRow('b', 'banana', 'hi')
126 .addRow('c', 'carrot', 'meow')
127 .addRow('efg', 'elephants')
128
129 var str = ""
130 + ".--------------------------------------."
131 + "\n" + "| Something |"
132 + "\n" + "|--------------------------------------|"
133 + "\n" + "| | Name | Age |"
134 + "\n" + "|-----|-----------|--------------------|"
135 + "\n" + "| a | apple | Some longer string |"
136 + "\n" + "| b | banana | hi |"
137 + "\n" + "| c | carrot | meow |"
138 + "\n" + "| efg | elephants | |"
139 + "\n" + "'--------------------------------------'"
140 ase(str, table.toString())
141 })
142 })
143
144 describe('Static methods', function() {
145
146 it('#version', function() {
147 ase(info.version, AsciiTable.VERSION)
148 ase(info.version, require('./ascii-table.min').VERSION)
149 ase(info.version, require('./bower.json').version)
150 })
151
152 it('#align', function() {
153 ase(AsciiTable.align(AsciiTable.LEFT, 'a', 10), AsciiTable.alignLeft('a', 10))
154 ase(AsciiTable.align(AsciiTable.CENTER, 'a', 10), AsciiTable.alignCenter('a', 10))
155 ase(AsciiTable.align(AsciiTable.RIGHT, 'a', 10), AsciiTable.alignRight('a', 10))
156 })
157
158 it('#alignLeft', function() {
159 var str = AsciiTable.alignLeft('foo', 30)
160 ase(str, 'foo ')
161
162 var str = AsciiTable.alignLeft('bar', 10, '-')
163 ase(str, 'bar-------')
164
165 var str = AsciiTable.alignLeft('meow', 1, '-')
166 ase(str, 'meow')
167 })
168
169 it('#alignRight', function() {
170 var str = AsciiTable.alignRight('foo', 30)
171 ase(str, ' foo')
172
173 var str = AsciiTable.alignRight('bar', 10, '-')
174 ase(str, '-------bar')
175
176 var str = AsciiTable.alignRight('meow', 1, '-')
177 ase(str, 'meow')
178 })
179
180 it('#alignCenter', function() {
181 var str = AsciiTable.alignCenter('foo', 30)
182 ase(str, ' foo ')
183
184 var str = AsciiTable.alignCenter('bar', 10, '-')
185 ase(str, '---bar----')
186
187 var str = AsciiTable.alignCenter('bars', 10, '-')
188 ase(str, '---bars---')
189
190 var str = AsciiTable.alignCenter('bar', 11, '-')
191 ase(str, '----bar----')
192 })
193
194 it('#alignAuto', function() {
195
196 })
197
198 it('#arrayFill', function() {
199 var arr = AsciiTable.arrayFill(10, '-')
200 ase(arr.length, 10)
201 ase(arr[0], '-')
202 })
203
204 it('#factory', function() {
205 var table = AsciiTable.factory('title')
206 ase(table instanceof AsciiTable, true)
207 ase(table.getTitle(), 'title')
208 })
209
210 it('#factory with object', function() {
211 var obj = {
212 title: 'foo',
213 heading: ['id', 'name'],
214 rows: [
215 [1, 'bob'],
216 [2, 'jim']
217 ]
218 }
219 var table = AsciiTable.factory(obj)
220 ase(table instanceof AsciiTable, true)
221 ase(table.getTitle(), 'foo')
222 })
223 })
224
225 describe('Instance methods', function() {
226
227 it('#setBorder', function() {
228 var table = new AsciiTable('a')
229 table
230 .setBorder('*')
231 .setHeading('one', 'two')
232 .addRow('abc', 'def')
233
234 var str = ''
235 + '*************'
236 + '\n' + '* a *'
237 + '\n' + '*************'
238 + '\n' + '* one * two *'
239 + '\n' + '*************'
240 + '\n' + '* abc * def *'
241 + '\n' + '*************'
242
243 ase(str, table.toString())
244 })
245
246 it('#removeBorder', function() {
247
248 })
249
250 it('#setAlign', function() {
251
252 })
253
254 it('#setAlignLeft', function() {
255
256 })
257
258 it('#setAlignCenter', function() {
259
260 })
261
262 it('#setAlignRight', function() {
263
264 })
265
266 it('#setTitle', function() {
267 var table = new AsciiTable('meow')
268 ase(table.getTitle(), 'meow')
269 table.setTitle('bark')
270 ase(table.getTitle(), 'bark')
271 })
272
273 it('#sort', function() {
274
275 })
276
277 it('#sortColumn', function() {
278
279 })
280
281 it('#setHeading', function() {
282 })
283
284 it('#addRow', function() {
285
286 })
287
288 it('#setJustify', function() {
289
290 })
291
292 it('#toString', function() {
293
294 })
295
296 it('#toJSON', function() {
297 var table = new AsciiTable('cat')
298 table
299 .setHeading('one', 'two', 'three')
300 .addRow(1, 2, 3)
301 .addRow(4, 5, 6)
302
303 var output = {
304 title: 'cat'
305 , heading: ['one', 'two', 'three']
306 , rows: [
307 [1, 2, 3]
308 , [4, 5, 6]
309 ]
310 }
311 var js = table.toJSON()
312 ade(js, output)
313
314 js.heading[0] = 'test'
315 ase(table.getHeading()[0], 'one')
316
317 js.rows[0][0] = 'test'
318 ase(table.getRows()[0][0], 1)
319 })
320
321 it('#clear', function() {
322
323 })
324
325 it('#clearRows', function() {
326
327 })
328 })
329})