log.js 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. // Copyright Joyent, Inc. and other Node contributors.
  2. //
  3. // Permission is hereby granted, free of charge, to any person obtaining a
  4. // copy of this software and associated documentation files (the
  5. // "Software"), to deal in the Software without restriction, including
  6. // without limitation the rights to use, copy, modify, merge, publish,
  7. // distribute, sublicense, and/or sell copies of the Software, and to permit
  8. // persons to whom the Software is furnished to do so, subject to the
  9. // following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included
  12. // in all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  15. // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  16. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
  17. // NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
  18. // DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  19. // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
  20. // USE OR OTHER DEALINGS IN THE SOFTWARE.
  21. var assert = require('assert');
  22. var util = require('../../');
  23. assert.ok(process.stdout.writable);
  24. assert.ok(process.stderr.writable);
  25. var stdout_write = global.process.stdout.write;
  26. var strings = [];
  27. global.process.stdout.write = function(string) {
  28. strings.push(string);
  29. };
  30. console._stderr = process.stdout;
  31. var tests = [
  32. {input: 'foo', output: 'foo'},
  33. {input: undefined, output: 'undefined'},
  34. {input: null, output: 'null'},
  35. {input: false, output: 'false'},
  36. {input: 42, output: '42'},
  37. {input: function(){}, output: '[Function]'},
  38. {input: parseInt('not a number', 10), output: 'NaN'},
  39. {input: {answer: 42}, output: '{ answer: 42 }'},
  40. {input: [1,2,3], output: '[ 1, 2, 3 ]'}
  41. ];
  42. // test util.log()
  43. tests.forEach(function(test) {
  44. util.log(test.input);
  45. var result = strings.shift().trim(),
  46. re = (/[0-9]{1,2} [A-Z][a-z]{2} [0-9]{2}:[0-9]{2}:[0-9]{2} - (.+)$/),
  47. match = re.exec(result);
  48. assert.ok(match);
  49. assert.equal(match[1], test.output);
  50. });
  51. global.process.stdout.write = stdout_write;