react / react-0.13.3 / examples / basic-commonjs / node_modules / browserify / node_modules / util / test / node / inspect.js
80742 views// Copyright Joyent, Inc. and other Node contributors.1//2// Permission is hereby granted, free of charge, to any person obtaining a3// copy of this software and associated documentation files (the4// "Software"), to deal in the Software without restriction, including5// without limitation the rights to use, copy, modify, merge, publish,6// distribute, sublicense, and/or sell copies of the Software, and to permit7// persons to whom the Software is furnished to do so, subject to the8// following conditions:9//10// The above copyright notice and this permission notice shall be included11// in all copies or substantial portions of the Software.12//13// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS14// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF15// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN16// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,17// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR18// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE19// USE OR OTHER DEALINGS IN THE SOFTWARE.2021222324var assert = require('assert');25var util = require('../../');2627// test the internal isDate implementation28var Date2 = require('vm').runInNewContext('Date');29var d = new Date2();30var orig = util.inspect(d);31Date2.prototype.foo = 'bar';32var after = util.inspect(d);33assert.equal(orig, after);3435// test for sparse array36var a = ['foo', 'bar', 'baz'];37assert.equal(util.inspect(a), '[ \'foo\', \'bar\', \'baz\' ]');38delete a[1];39assert.equal(util.inspect(a), '[ \'foo\', , \'baz\' ]');40assert.equal(util.inspect(a, true), '[ \'foo\', , \'baz\', [length]: 3 ]');41assert.equal(util.inspect(new Array(5)), '[ , , , , ]');4243// test for property descriptors44var getter = Object.create(null, {45a: {46get: function() { return 'aaa'; }47}48});49var setter = Object.create(null, {50b: {51set: function() {}52}53});54var getterAndSetter = Object.create(null, {55c: {56get: function() { return 'ccc'; },57set: function() {}58}59});60assert.equal(util.inspect(getter, true), '{ [a]: [Getter] }');61assert.equal(util.inspect(setter, true), '{ [b]: [Setter] }');62assert.equal(util.inspect(getterAndSetter, true), '{ [c]: [Getter/Setter] }');6364// exceptions should print the error message, not '{}'65assert.equal(util.inspect(new Error()), '[Error]');66assert.equal(util.inspect(new Error('FAIL')), '[Error: FAIL]');67assert.equal(util.inspect(new TypeError('FAIL')), '[TypeError: FAIL]');68assert.equal(util.inspect(new SyntaxError('FAIL')), '[SyntaxError: FAIL]');69try {70undef();71} catch (e) {72assert.equal(util.inspect(e), '[ReferenceError: undef is not defined]');73}74var ex = util.inspect(new Error('FAIL'), true);75assert.ok(ex.indexOf('[Error: FAIL]') != -1);76assert.ok(ex.indexOf('[stack]') != -1);77assert.ok(ex.indexOf('[message]') != -1);7879// GH-194180// should not throw:81assert.equal(util.inspect(Object.create(Date.prototype)), '{}');8283// GH-194484assert.doesNotThrow(function() {85var d = new Date();86d.toUTCString = null;87util.inspect(d);88});8990assert.doesNotThrow(function() {91var r = /regexp/;92r.toString = null;93util.inspect(r);94});9596// bug with user-supplied inspect function returns non-string97assert.doesNotThrow(function() {98util.inspect([{99inspect: function() { return 123; }100}]);101});102103// GH-2225104var x = { inspect: util.inspect };105assert.ok(util.inspect(x).indexOf('inspect') != -1);106107// util.inspect.styles and util.inspect.colors108function test_color_style(style, input, implicit) {109var color_name = util.inspect.styles[style];110var color = ['', ''];111if(util.inspect.colors[color_name])112color = util.inspect.colors[color_name];113114var without_color = util.inspect(input, false, 0, false);115var with_color = util.inspect(input, false, 0, true);116var expect = '\u001b[' + color[0] + 'm' + without_color +117'\u001b[' + color[1] + 'm';118assert.equal(with_color, expect, 'util.inspect color for style '+style);119}120121test_color_style('special', function(){});122test_color_style('number', 123.456);123test_color_style('boolean', true);124test_color_style('undefined', undefined);125test_color_style('null', null);126test_color_style('string', 'test string');127test_color_style('date', new Date);128test_color_style('regexp', /regexp/);129130// an object with "hasOwnProperty" overwritten should not throw131assert.doesNotThrow(function() {132util.inspect({133hasOwnProperty: null134});135});136137// new API, accepts an "options" object138var subject = { foo: 'bar', hello: 31, a: { b: { c: { d: 0 } } } };139Object.defineProperty(subject, 'hidden', { enumerable: false, value: null });140141assert(util.inspect(subject, { showHidden: false }).indexOf('hidden') === -1);142assert(util.inspect(subject, { showHidden: true }).indexOf('hidden') !== -1);143assert(util.inspect(subject, { colors: false }).indexOf('\u001b[32m') === -1);144assert(util.inspect(subject, { colors: true }).indexOf('\u001b[32m') !== -1);145assert(util.inspect(subject, { depth: 2 }).indexOf('c: [Object]') !== -1);146assert(util.inspect(subject, { depth: 0 }).indexOf('a: [Object]') !== -1);147assert(util.inspect(subject, { depth: null }).indexOf('{ d: 0 }') !== -1);148149// "customInspect" option can enable/disable calling inspect() on objects150subject = { inspect: function() { return 123; } };151152assert(util.inspect(subject, { customInspect: true }).indexOf('123') !== -1);153assert(util.inspect(subject, { customInspect: true }).indexOf('inspect') === -1);154assert(util.inspect(subject, { customInspect: false }).indexOf('123') === -1);155assert(util.inspect(subject, { customInspect: false }).indexOf('inspect') !== -1);156157// custom inspect() functions should be able to return other Objects158subject.inspect = function() { return { foo: 'bar' }; };159160assert.equal(util.inspect(subject), '{ foo: \'bar\' }');161162subject.inspect = function(depth, opts) {163assert.strictEqual(opts.customInspectOptions, true);164};165166util.inspect(subject, { customInspectOptions: true });167168// util.inspect with "colors" option should produce as many lines as without it169function test_lines(input) {170var count_lines = function(str) {171return (str.match(/\n/g) || []).length;172}173174var without_color = util.inspect(input);175var with_color = util.inspect(input, {colors: true});176assert.equal(count_lines(without_color), count_lines(with_color));177}178179test_lines([1, 2, 3, 4, 5, 6, 7]);180test_lines(function() {181var big_array = [];182for (var i = 0; i < 100; i++) {183big_array.push(i);184}185return big_array;186}());187test_lines({foo: 'bar', baz: 35, b: {a: 35}});188test_lines({189foo: 'bar',190baz: 35,191b: {a: 35},192very_long_key: 'very_long_value',193even_longer_key: ['with even longer value in array']194});195196197