1'use strict'; 2 3const assert = require('assert'); 4const chalk = require('chalk'); 5const stripAnsi = require('strip-ansi'); 6const columns = require('./index.js'); 7const tests = []; 8 9function test(msg, fn) { 10 tests.push([msg, fn]); 11} 12 13process.nextTick(async function run() { 14 for (const [msg, fn] of tests) { 15 try { 16 await fn(assert); 17 console.log(`pass - ${msg}`); 18 } catch (error) { 19 console.error(`fail - ${msg}`, error); 20 process.exit(1); 21 } 22 } 23}); 24 25// prettier-ignore 26test('should print one column list', t => { 27 const cols = columns(['foo', ['bar', 'baz'], ['bar', 'qux']], { 28 width: 1 29 }); 30 31 const expected = 32 'bar\n' + 33 'bar\n' + 34 'baz\n' + 35 'foo\n' + 36 'qux'; 37 38 t.equal(cols, expected); 39}); 40 41// prettier-ignore 42test('should print three column list', t => { 43 const cols = columns(['foo', ['bar', 'baz'], ['bat', 'qux']], { 44 width: 16 45 }); 46 47 const expected = 48 'bar baz qux \n' + 49 'bat foo '; 50 51 t.equal(cols, expected); 52}); 53 54// prettier-ignore 55test('should print complex list', t => { 56 const cols = columns( 57 [ 58 'foo', 'bar', 'baz', 59 chalk.cyan('嶜憃撊') + ' 噾噿嚁', 60 'blue' + chalk.bgBlue('berry'), 61 chalk.red('apple'), 'pomegranate', 62 'durian', chalk.green('star fruit'), 63 'apricot', 'banana pineapple' 64 ], 65 { 66 width: 80 67 } 68 ); 69 70 const expected = 71 'apple bar durian star fruit \n' + 72 'apricot baz foo 嶜憃撊 噾噿嚁 \n' + 73 'banana pineapple blueberry pomegranate '; 74 75 t.equal(stripAnsi(cols), expected); 76}); 77 78// prettier-ignore 79test('should optionally not sort', t => { 80 const cols = columns( 81 [ 82 'foo', 'bar', 'baz', 83 chalk.cyan('嶜憃撊') + ' 噾噿嚁', 84 'blue' + chalk.bgBlue('berry'), 85 chalk.red('apple'), 'pomegranate', 86 'durian', chalk.green('star fruit'), 87 'apricot', 'banana pineapple' 88 ], 89 { 90 sort: false, 91 width: 80 92 } 93 ); 94 95 const expected = 96 'foo 嶜憃撊 噾噿嚁 pomegranate apricot \n' + 97 'bar blueberry durian banana pineapple \n' + 98 'baz apple star fruit '; 99 100 t.equal(stripAnsi(cols), expected); 101}); 102