| 1 | "use strict"; |
||
| 2 | |||
| 3 | const assert = require ('assert'), |
||
| 4 | asTable = require ('./as-table'), |
||
| 5 | ansi = require ('ansicolor').nice |
||
|
0 ignored issues
–
show
Unused Code
introduced
by
Loading history...
|
|||
| 6 | |||
| 7 | describe ('as-table', () => { |
||
| 8 | |||
| 9 | it ('array printing works', () => { |
||
| 10 | |||
| 11 | var testData = [['qwe', '123456789', 'zxcvbnm'], |
||
| 12 | ['qwerty', '12', 'zxcvb'], |
||
| 13 | ['qwertyiop', '1234567', 'z']] |
||
| 14 | |||
| 15 | assert.equal (asTable (testData), |
||
| 16 | |||
| 17 | 'qwe 123456789 zxcvbnm\n' + |
||
| 18 | 'qwerty 12 zxcvb \n' + |
||
| 19 | 'qwertyiop 1234567 z ') |
||
| 20 | |||
| 21 | assert.equal (asTable.configure ({ maxTotalWidth: 22, delimiter: ' | ' }) (testData), |
||
| 22 | |||
| 23 | 'qwe | 1234… | zxc…\n' + |
||
| 24 | 'qwer… | 12 | zxc…\n' + |
||
| 25 | 'qwer… | 1234… | z ') |
||
| 26 | }) |
||
| 27 | |||
| 28 | it ('object printing works', () => { |
||
| 29 | |||
| 30 | var testData = |
||
| 31 | [ { foo: true, string: 'abcde', num: 42 }, |
||
| 32 | { foo: false, string: 'qwertyuiop', num: 43 }, |
||
| 33 | { string: null, num: 44 } ] |
||
| 34 | |||
| 35 | assert.equal (asTable (testData), |
||
| 36 | |||
| 37 | 'foo string num\n' + |
||
| 38 | '----------------------\n' + |
||
| 39 | 'true abcde 42 \n' + |
||
| 40 | 'false qwertyuiop 43 \n' + |
||
| 41 | ' null 44 ') |
||
| 42 | }) |
||
| 43 | |||
| 44 | |||
| 45 | it ('object printing works (with ANSI styling)', () => { |
||
| 46 | |||
| 47 | var testData = |
||
| 48 | [ { foo: true, string: 'abcde'.cyan.bgYellow, num: 42 }, |
||
| 49 | { foo: false, string: 'qwertyuiop', num: 43 }, |
||
| 50 | { string: null, num: 44 } ] |
||
| 51 | |||
| 52 | assert.equal (asTable (testData), |
||
| 53 | |||
| 54 | 'foo string num\n' + |
||
| 55 | '----------------------\n' + |
||
| 56 | 'true \u001b[43m\u001b[36mabcde\u001b[39m\u001b[49m 42 \n' + |
||
| 57 | 'false qwertyuiop 43 \n' + |
||
| 58 | ' null 44 ') |
||
| 59 | }) |
||
| 60 | |||
| 61 | it ('maxTotalWidth correctly handles object field names', () => { |
||
| 62 | |||
| 63 | assert.equal ( |
||
| 64 | |||
| 65 | asTable.configure ({ maxTotalWidth: 15 }) ([{ |
||
| 66 | |||
| 67 | '0123456789': '0123456789', |
||
| 68 | 'abcdefxyzw': 'abcdefxyzw' }]), |
||
| 69 | |||
| 70 | '01234… abcde…\n' + |
||
| 71 | '--------------\n' + |
||
| 72 | '01234… abcde…' |
||
| 73 | ) |
||
| 74 | }) |
||
| 75 | |||
| 76 | it ('maxTotalWidth correctly handles object field names (with ANSI styling)', () => { |
||
| 77 | |||
| 78 | assert.equal ( |
||
| 79 | |||
| 80 | asTable.configure ({ maxTotalWidth: 15 }) ([{ |
||
| 81 | |||
| 82 | '0123456789': '0123456789', |
||
| 83 | 'abcdefxyzw': 'abcdefxyzw'.cyan.bgYellow.italic.inverse.bright }]), |
||
| 84 | |||
| 85 | '01234… abcde…\n' + |
||
| 86 | '--------------\n' + |
||
| 87 | '01234… ' + 'abcde'.cyan.bgYellow.italic.inverse.bright + '…' |
||
| 88 | ) |
||
| 89 | }) |
||
| 90 | |||
| 91 | it ('everything renders as singleline', () => { |
||
| 92 | |||
| 93 | assert.equal (asTable ([['fooo\n\nbar']]), 'fooo\\n\\nbar') |
||
| 94 | }) |
||
| 95 | |||
| 96 | it ('configuring works', () => { |
||
| 97 | |||
| 98 | const asTable25 = asTable.configure ({ maxTotalWidth: 25 }), |
||
| 99 | asTable25Delim = asTable25.configure ({ delimiter: ' | ' }) |
||
| 100 | |||
| 101 | assert.notEqual (asTable25, asTable25Delim) |
||
| 102 | assert.equal (asTable25.maxTotalWidth, 25) |
||
| 103 | assert.equal (asTable25Delim.delimiter, ' | ') |
||
| 104 | }) |
||
| 105 | |||
| 106 | it ('degenerate case works', () => { |
||
| 107 | |||
| 108 | assert.equal (asTable ([]), '\n') |
||
| 109 | assert.equal (asTable ([{}]), '\n\n') |
||
| 110 | }) |
||
| 111 | |||
| 112 | it ('null/undefined prints correctly', () => { |
||
| 113 | |||
| 114 | assert.equal (asTable.configure ({ delimiter: '|' }) ([[null, undefined, 1, 2, 3]]), 'null||1|2|3') |
||
| 115 | }) |
||
| 116 | |||
| 117 | it ('custom printer works', () => { |
||
| 118 | |||
| 119 | var testData = |
||
| 120 | [ { foo: true, string: 'abcde', num: 42 }, |
||
| 121 | { foo: false, string: 'qwertyuiop', num: 43 }, |
||
| 122 | { string: null, num: 44 } ] |
||
| 123 | |||
| 124 | const formatsBooleansAsYesNo = asTable.configure ({ print: obj => (typeof obj === 'boolean') ? (obj ? 'yes' : 'no') : String (obj) }) |
||
| 125 | |||
| 126 | assert.equal (formatsBooleansAsYesNo (testData), |
||
| 127 | |||
| 128 | 'foo string num\n' + |
||
| 129 | '--------------------\n' + |
||
| 130 | 'yes abcde 42 \n' + |
||
| 131 | 'no qwertyuiop 43 \n' + |
||
| 132 | ' null 44 ') |
||
| 133 | }) |
||
| 134 | |||
| 135 | it ('ANSI coloring works', () => { |
||
| 136 | |||
| 137 | const testData = |
||
| 138 | [ { foo: true, string: 'abcde', num: 42 }, |
||
| 139 | { foo: false, string: 'qwertyuiop'.bgMagenta.green.bright, num: 43 } ] |
||
| 140 | |||
| 141 | const d = ' | '.dim.cyan |
||
| 142 | const _ = '-'.bright.cyan |
||
| 143 | |||
| 144 | const result = asTable.configure ({ title: x => x.bright, delimiter: d, dash: _ }) (testData) |
||
| 145 | |||
| 146 | console.log (result) |
||
|
0 ignored issues
–
show
|
|||
| 147 | |||
| 148 | assert.equal (result, |
||
| 149 | |||
| 150 | ['foo'.bright + ' ', 'string'.bright + ' ', 'num'.bright].join (d) + '\n' + |
||
| 151 | _.repeat (24) + '\n' + |
||
| 152 | ['true ', 'abcde ', '42 '].join (d) + '\n' + |
||
| 153 | ['false', 'qwertyuiop'.bgMagenta.green.bright, '43 '].join (d)) |
||
| 154 | }) |
||
| 155 | }) |
||
| 156 | |||
| 157 | |||
| 158 | |||
| 159 |