basic-test.js 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. var assert = require('assert');
  2. var colors = require('../lib/index');
  3. var s = 'string';
  4. function a(s, code) {
  5. return '\x1B[' + code.toString() + 'm' + s + '\x1B[39m';
  6. }
  7. function aE(s, color, code) {
  8. assert.equal(s[color], a(s, code));
  9. assert.equal(colors[color](s), a(s, code));
  10. assert.equal(s[color], colors[color](s));
  11. assert.equal(s[color].strip, s);
  12. assert.equal(s[color].strip, colors.strip(s));
  13. }
  14. var stylesColors = ['white', 'black', 'blue', 'cyan', 'green', 'magenta',
  15. 'red', 'yellow', 'brightYellow', 'brightRed',
  16. 'brightGreen', 'brightBlue', 'brightWhite', 'brightCyan',
  17. 'brightMagenta'];
  18. // eslint-disable-next-line
  19. var stylesAll = stylesColors.concat(['bold', 'italic', 'underline',
  20. 'inverse', 'rainbow']);
  21. colors.mode = 'console';
  22. assert.equal(s.bold, '\x1B[1m' + s + '\x1B[22m');
  23. assert.equal(s.italic, '\x1B[3m' + s + '\x1B[23m');
  24. assert.equal(s.underline, '\x1B[4m' + s + '\x1B[24m');
  25. assert.equal(s.strikethrough, '\x1B[9m' + s + '\x1B[29m');
  26. assert.equal(s.inverse, '\x1B[7m' + s + '\x1B[27m');
  27. assert.ok(s.rainbow);
  28. assert.equal(colors.stylize("foo", "rainbow"), '\u001b[31mf\u001b[39m\u001b[33mo\u001b[39m\u001b[32mo\u001b[39m');
  29. assert.ok(colors.stylize(s, "america"));
  30. assert.ok(colors.stylize(s, "zebra"));
  31. assert.ok(colors.stylize(s, "trap"));
  32. assert.ok(colors.stylize(s, "random"));
  33. aE(s, 'white', 37);
  34. aE(s, 'grey', 90);
  35. aE(s, 'black', 30);
  36. aE(s, 'blue', 34);
  37. aE(s, 'cyan', 36);
  38. aE(s, 'green', 32);
  39. aE(s, 'magenta', 35);
  40. aE(s, 'red', 31);
  41. aE(s, 'yellow', 33);
  42. aE(s, 'brightWhite', 97);
  43. aE(s, 'brightBlue', 94);
  44. aE(s, 'brightCyan', 96);
  45. aE(s, 'brightGreen', 92);
  46. aE(s, 'brightMagenta', 95);
  47. aE(s, 'brightRed', 91);
  48. aE(s, 'brightYellow', 93);
  49. assert.equal(s, 'string');
  50. var testStringWithNewLines = s + '\n' + s;
  51. // single style
  52. assert.equal(testStringWithNewLines.red, '\x1b[31m' + s + '\n' + s +
  53. '\x1b[39m');
  54. var testStringWithNewLinesStyled = s.underline + '\n' + s.bold;
  55. // nested styles
  56. assert.equal(testStringWithNewLinesStyled.red,
  57. '\x1b[31m' + '\x1b[4m' + s + '\x1b[24m' + '\n' + '\x1b[1m' + s +
  58. '\x1b[22m' + '\x1b[39m');
  59. colors.setTheme({error: 'red'});
  60. assert.equal(typeof ('astring'.red), 'string');
  61. assert.equal(typeof ('astring'.error), 'string');
  62. assert.equal(s, 'string');
  63. colors.setTheme({custom: ['blue', 'bold', 'underline']});
  64. assert.equal(colors.custom(s),
  65. '\x1b[4m' + '\x1b[1m' + '\x1b[34m' + s +
  66. '\x1b[39m' + '\x1b[22m' + '\x1b[24m' );
  67. colors.setTheme({custom: ['red', 'italic', 'inverse']});
  68. assert.equal(colors.custom(s),
  69. '\x1b[7m' + '\x1b[3m' + '\x1b[31m' + s +
  70. '\x1b[39m' + '\x1b[23m' + '\x1b[27m' );