inspect.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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. // test the internal isDate implementation
  24. var Date2 = require('vm').runInNewContext('Date');
  25. var d = new Date2();
  26. var orig = util.inspect(d);
  27. Date2.prototype.foo = 'bar';
  28. var after = util.inspect(d);
  29. assert.equal(orig, after);
  30. // test for sparse array
  31. var a = ['foo', 'bar', 'baz'];
  32. assert.equal(util.inspect(a), '[ \'foo\', \'bar\', \'baz\' ]');
  33. delete a[1];
  34. assert.equal(util.inspect(a), '[ \'foo\', , \'baz\' ]');
  35. assert.equal(util.inspect(a, true), '[ \'foo\', , \'baz\', [length]: 3 ]');
  36. assert.equal(util.inspect(new Array(5)), '[ , , , , ]');
  37. // test for property descriptors
  38. var getter = Object.create(null, {
  39. a: {
  40. get: function() { return 'aaa'; }
  41. }
  42. });
  43. var setter = Object.create(null, {
  44. b: {
  45. set: function() {}
  46. }
  47. });
  48. var getterAndSetter = Object.create(null, {
  49. c: {
  50. get: function() { return 'ccc'; },
  51. set: function() {}
  52. }
  53. });
  54. assert.equal(util.inspect(getter, true), '{ [a]: [Getter] }');
  55. assert.equal(util.inspect(setter, true), '{ [b]: [Setter] }');
  56. assert.equal(util.inspect(getterAndSetter, true), '{ [c]: [Getter/Setter] }');
  57. // exceptions should print the error message, not '{}'
  58. assert.equal(util.inspect(new Error()), '[Error]');
  59. assert.equal(util.inspect(new Error('FAIL')), '[Error: FAIL]');
  60. assert.equal(util.inspect(new TypeError('FAIL')), '[TypeError: FAIL]');
  61. assert.equal(util.inspect(new SyntaxError('FAIL')), '[SyntaxError: FAIL]');
  62. try {
  63. undef();
  64. } catch (e) {
  65. assert.equal(util.inspect(e), '[ReferenceError: undef is not defined]');
  66. }
  67. var ex = util.inspect(new Error('FAIL'), true);
  68. assert.ok(ex.indexOf('[Error: FAIL]') != -1);
  69. assert.ok(ex.indexOf('[stack]') != -1);
  70. assert.ok(ex.indexOf('[message]') != -1);
  71. // GH-1941
  72. // should not throw:
  73. assert.equal(util.inspect(Object.create(Date.prototype)), '{}');
  74. // GH-1944
  75. assert.doesNotThrow(function() {
  76. var d = new Date();
  77. d.toUTCString = null;
  78. util.inspect(d);
  79. });
  80. assert.doesNotThrow(function() {
  81. var r = /regexp/;
  82. r.toString = null;
  83. util.inspect(r);
  84. });
  85. // bug with user-supplied inspect function returns non-string
  86. assert.doesNotThrow(function() {
  87. util.inspect([{
  88. inspect: function() { return 123; }
  89. }]);
  90. });
  91. // GH-2225
  92. var x = { inspect: util.inspect };
  93. assert.ok(util.inspect(x).indexOf('inspect') != -1);
  94. // util.inspect.styles and util.inspect.colors
  95. function test_color_style(style, input, implicit) {
  96. var color_name = util.inspect.styles[style];
  97. var color = ['', ''];
  98. if(util.inspect.colors[color_name])
  99. color = util.inspect.colors[color_name];
  100. var without_color = util.inspect(input, false, 0, false);
  101. var with_color = util.inspect(input, false, 0, true);
  102. var expect = '\u001b[' + color[0] + 'm' + without_color +
  103. '\u001b[' + color[1] + 'm';
  104. assert.equal(with_color, expect, 'util.inspect color for style '+style);
  105. }
  106. test_color_style('special', function(){});
  107. test_color_style('number', 123.456);
  108. test_color_style('boolean', true);
  109. test_color_style('undefined', undefined);
  110. test_color_style('null', null);
  111. test_color_style('string', 'test string');
  112. test_color_style('date', new Date);
  113. test_color_style('regexp', /regexp/);
  114. // an object with "hasOwnProperty" overwritten should not throw
  115. assert.doesNotThrow(function() {
  116. util.inspect({
  117. hasOwnProperty: null
  118. });
  119. });
  120. // new API, accepts an "options" object
  121. var subject = { foo: 'bar', hello: 31, a: { b: { c: { d: 0 } } } };
  122. Object.defineProperty(subject, 'hidden', { enumerable: false, value: null });
  123. assert(util.inspect(subject, { showHidden: false }).indexOf('hidden') === -1);
  124. assert(util.inspect(subject, { showHidden: true }).indexOf('hidden') !== -1);
  125. assert(util.inspect(subject, { colors: false }).indexOf('\u001b[32m') === -1);
  126. assert(util.inspect(subject, { colors: true }).indexOf('\u001b[32m') !== -1);
  127. assert(util.inspect(subject, { depth: 2 }).indexOf('c: [Object]') !== -1);
  128. assert(util.inspect(subject, { depth: 0 }).indexOf('a: [Object]') !== -1);
  129. assert(util.inspect(subject, { depth: null }).indexOf('{ d: 0 }') !== -1);
  130. // "customInspect" option can enable/disable calling inspect() on objects
  131. subject = { inspect: function() { return 123; } };
  132. assert(util.inspect(subject, { customInspect: true }).indexOf('123') !== -1);
  133. assert(util.inspect(subject, { customInspect: true }).indexOf('inspect') === -1);
  134. assert(util.inspect(subject, { customInspect: false }).indexOf('123') === -1);
  135. assert(util.inspect(subject, { customInspect: false }).indexOf('inspect') !== -1);
  136. // custom inspect() functions should be able to return other Objects
  137. subject.inspect = function() { return { foo: 'bar' }; };
  138. assert.equal(util.inspect(subject), '{ foo: \'bar\' }');
  139. subject.inspect = function(depth, opts) {
  140. assert.strictEqual(opts.customInspectOptions, true);
  141. };
  142. util.inspect(subject, { customInspectOptions: true });
  143. // util.inspect with "colors" option should produce as many lines as without it
  144. function test_lines(input) {
  145. var count_lines = function(str) {
  146. return (str.match(/\n/g) || []).length;
  147. }
  148. var without_color = util.inspect(input);
  149. var with_color = util.inspect(input, {colors: true});
  150. assert.equal(count_lines(without_color), count_lines(with_color));
  151. }
  152. test_lines([1, 2, 3, 4, 5, 6, 7]);
  153. test_lines(function() {
  154. var big_array = [];
  155. for (var i = 0; i < 100; i++) {
  156. big_array.push(i);
  157. }
  158. return big_array;
  159. }());
  160. test_lines({foo: 'bar', baz: 35, b: {a: 35}});
  161. test_lines({
  162. foo: 'bar',
  163. baz: 35,
  164. b: {a: 35},
  165. very_long_key: 'very_long_value',
  166. even_longer_key: ['with even longer value in array']
  167. });