test.js 7.4 KB

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