safe-test.js 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. var assert = require('assert');
  2. var colors = require('../safe');
  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(colors[color](s), a(s, code));
  9. assert.equal(colors.strip(s), s);
  10. }
  11. var stylesColors = ['white', 'black', 'blue', 'cyan', 'green', 'magenta',
  12. 'red', 'yellow', 'brightYellow', 'brightRed',
  13. 'brightGreen', 'brightBlue', 'brightWhite', 'brightCyan',
  14. 'brightMagenta'];
  15. // eslint-disable-next-line
  16. var stylesAll = stylesColors.concat(['bold', 'italic', 'underline', 'inverse',
  17. 'rainbow']);
  18. colors.mode = 'console';
  19. assert.equal(colors.bold(s), '\x1B[1m' + s + '\x1B[22m');
  20. assert.equal(colors.italic(s), '\x1B[3m' + s + '\x1B[23m');
  21. assert.equal(colors.underline(s), '\x1B[4m' + s + '\x1B[24m');
  22. assert.equal(colors.strikethrough(s), '\x1B[9m' + s + '\x1B[29m');
  23. assert.equal(colors.inverse(s), '\x1B[7m' + s + '\x1B[27m');
  24. assert.ok(colors.rainbow);
  25. aE(s, 'white', 37);
  26. aE(s, 'grey', 90);
  27. aE(s, 'black', 30);
  28. aE(s, 'blue', 34);
  29. aE(s, 'cyan', 36);
  30. aE(s, 'green', 32);
  31. aE(s, 'magenta', 35);
  32. aE(s, 'red', 31);
  33. aE(s, 'yellow', 33);
  34. aE(s, 'brightWhite', 97);
  35. aE(s, 'brightBlue', 94);
  36. aE(s, 'brightCyan', 96);
  37. aE(s, 'brightGreen', 92);
  38. aE(s, 'brightMagenta', 95);
  39. aE(s, 'brightRed', 91);
  40. aE(s, 'brightYellow', 93);
  41. assert.equal(s, 'string');
  42. var testStringWithNewLines = s + '\n' + s;
  43. // single style
  44. assert.equal(colors.red(testStringWithNewLines),
  45. '\x1b[31m' + s + '\x1b[39m' + '\n' + '\x1b[31m' + s + '\x1b[39m');
  46. var testStringWithNewLinesStyled = colors.underline(s) + '\n' + colors.bold(s);
  47. // nested styles
  48. assert.equal(colors.red(testStringWithNewLinesStyled),
  49. '\x1b[31m' + '\x1b[4m' + s + '\x1b[24m' + '\x1b[39m' + '\n' + '\x1b[31m' +
  50. '\x1b[1m' + s + '\x1b[22m' + '\x1b[39m');
  51. colors.setTheme({error: 'red'});
  52. assert.equal(typeof (colors.red('astring')), 'string');
  53. assert.equal(typeof (colors.error('astring')), 'string');
  54. colors.setTheme({custom: ['blue', 'bold', 'underline']});
  55. assert.equal(colors.custom(s),
  56. '\x1b[4m' + '\x1b[1m' + '\x1b[34m' + s +
  57. '\x1b[39m' + '\x1b[22m' + '\x1b[24m' );
  58. colors.setTheme({custom: ['red', 'italic', 'inverse']});
  59. assert.equal(colors.custom(s),
  60. '\x1b[7m' + '\x1b[3m' + '\x1b[31m' + s +
  61. '\x1b[39m' + '\x1b[23m' + '\x1b[27m' );
  62. // should not throw error on null or undefined values
  63. var undef;
  64. assert.equal(colors.yellow(undef), '\x1b[33mundefined\x1b[39m');
  65. // was failing:
  66. assert.equal(colors.red(null), '\x1b[31mnull\x1b[39m');