basic-test.js 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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'];
  16. // eslint-disable-next-line
  17. var stylesAll = stylesColors.concat(['bold', 'italic', 'underline',
  18. 'inverse', 'rainbow']);
  19. colors.mode = 'console';
  20. assert.equal(s.bold, '\x1B[1m' + s + '\x1B[22m');
  21. assert.equal(s.italic, '\x1B[3m' + s + '\x1B[23m');
  22. assert.equal(s.underline, '\x1B[4m' + s + '\x1B[24m');
  23. assert.equal(s.strikethrough, '\x1B[9m' + s + '\x1B[29m');
  24. assert.equal(s.inverse, '\x1B[7m' + s + '\x1B[27m');
  25. assert.ok(s.rainbow);
  26. aE(s, 'white', 37);
  27. aE(s, 'grey', 90);
  28. aE(s, 'black', 30);
  29. aE(s, 'blue', 34);
  30. aE(s, 'cyan', 36);
  31. aE(s, 'green', 32);
  32. aE(s, 'magenta', 35);
  33. aE(s, 'red', 31);
  34. aE(s, 'yellow', 33);
  35. assert.equal(s, 'string');
  36. var testStringWithNewLines = s + '\n' + s;
  37. // single style
  38. assert.equal(testStringWithNewLines.red, '\x1b[31m' + s + '\n' + s +
  39. '\x1b[39m');
  40. var testStringWithNewLinesStyled = s.underline + '\n' + s.bold;
  41. // nested styles
  42. assert.equal(testStringWithNewLinesStyled.red,
  43. '\x1b[31m' + '\x1b[4m' + s + '\x1b[24m' + '\n' + '\x1b[1m' + s +
  44. '\x1b[22m' + '\x1b[39m');
  45. colors.setTheme({error: 'red'});
  46. assert.equal(typeof ('astring'.red), 'string');
  47. assert.equal(typeof ('astring'.error), 'string');
  48. assert.equal(s, 'string');