test.js 2.2 KB

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