| 1 | 'use strict';
|
|---|
| 2 |
|
|---|
| 3 | var inspect = require('../');
|
|---|
| 4 | var test = require('tape');
|
|---|
| 5 | var mockProperty = require('mock-property');
|
|---|
| 6 | var hasSymbols = require('has-symbols/shams')();
|
|---|
| 7 | var hasToStringTag = require('has-tostringtag/shams')();
|
|---|
| 8 | var forEach = require('for-each');
|
|---|
| 9 | var semver = require('semver');
|
|---|
| 10 |
|
|---|
| 11 | test('values', function (t) {
|
|---|
| 12 | t.plan(1);
|
|---|
| 13 | var obj = [{}, [], { 'a-b': 5 }];
|
|---|
| 14 | t.equal(inspect(obj), '[ {}, [], { \'a-b\': 5 } ]');
|
|---|
| 15 | });
|
|---|
| 16 |
|
|---|
| 17 | test('arrays with properties', function (t) {
|
|---|
| 18 | t.plan(1);
|
|---|
| 19 | var arr = [3];
|
|---|
| 20 | arr.foo = 'bar';
|
|---|
| 21 | var obj = [1, 2, arr];
|
|---|
| 22 | obj.baz = 'quux';
|
|---|
| 23 | obj.index = -1;
|
|---|
| 24 | t.equal(inspect(obj), '[ 1, 2, [ 3, foo: \'bar\' ], baz: \'quux\', index: -1 ]');
|
|---|
| 25 | });
|
|---|
| 26 |
|
|---|
| 27 | test('has', function (t) {
|
|---|
| 28 | t.plan(1);
|
|---|
| 29 | t.teardown(mockProperty(Object.prototype, 'hasOwnProperty', { 'delete': true }));
|
|---|
| 30 |
|
|---|
| 31 | t.equal(inspect({ a: 1, b: 2 }), '{ a: 1, b: 2 }');
|
|---|
| 32 | });
|
|---|
| 33 |
|
|---|
| 34 | test('indexOf seen', function (t) {
|
|---|
| 35 | t.plan(1);
|
|---|
| 36 | var xs = [1, 2, 3, {}];
|
|---|
| 37 | xs.push(xs);
|
|---|
| 38 |
|
|---|
| 39 | var seen = [];
|
|---|
| 40 | seen.indexOf = undefined;
|
|---|
| 41 |
|
|---|
| 42 | t.equal(
|
|---|
| 43 | inspect(xs, {}, 0, seen),
|
|---|
| 44 | '[ 1, 2, 3, {}, [Circular] ]'
|
|---|
| 45 | );
|
|---|
| 46 | });
|
|---|
| 47 |
|
|---|
| 48 | test('seen seen', function (t) {
|
|---|
| 49 | t.plan(1);
|
|---|
| 50 | var xs = [1, 2, 3];
|
|---|
| 51 |
|
|---|
| 52 | var seen = [xs];
|
|---|
| 53 | seen.indexOf = undefined;
|
|---|
| 54 |
|
|---|
| 55 | t.equal(
|
|---|
| 56 | inspect(xs, {}, 0, seen),
|
|---|
| 57 | '[Circular]'
|
|---|
| 58 | );
|
|---|
| 59 | });
|
|---|
| 60 |
|
|---|
| 61 | test('seen seen seen', function (t) {
|
|---|
| 62 | t.plan(1);
|
|---|
| 63 | var xs = [1, 2, 3];
|
|---|
| 64 |
|
|---|
| 65 | var seen = [5, xs];
|
|---|
| 66 | seen.indexOf = undefined;
|
|---|
| 67 |
|
|---|
| 68 | t.equal(
|
|---|
| 69 | inspect(xs, {}, 0, seen),
|
|---|
| 70 | '[Circular]'
|
|---|
| 71 | );
|
|---|
| 72 | });
|
|---|
| 73 |
|
|---|
| 74 | test('symbols', { skip: !hasSymbols }, function (t) {
|
|---|
| 75 | var sym = Symbol('foo');
|
|---|
| 76 | t.equal(inspect(sym), 'Symbol(foo)', 'Symbol("foo") should be "Symbol(foo)"');
|
|---|
| 77 | if (typeof sym === 'symbol') {
|
|---|
| 78 | // Symbol shams are incapable of differentiating boxed from unboxed symbols
|
|---|
| 79 | t.equal(inspect(Object(sym)), 'Object(Symbol(foo))', 'Object(Symbol("foo")) should be "Object(Symbol(foo))"');
|
|---|
| 80 | }
|
|---|
| 81 |
|
|---|
| 82 | t.test('toStringTag', { skip: !hasToStringTag }, function (st) {
|
|---|
| 83 | st.plan(1);
|
|---|
| 84 |
|
|---|
| 85 | var faker = {};
|
|---|
| 86 | faker[Symbol.toStringTag] = 'Symbol';
|
|---|
| 87 | st.equal(
|
|---|
| 88 | inspect(faker),
|
|---|
| 89 | '{ [Symbol(Symbol.toStringTag)]: \'Symbol\' }',
|
|---|
| 90 | 'object lying about being a Symbol inspects as an object'
|
|---|
| 91 | );
|
|---|
| 92 | });
|
|---|
| 93 |
|
|---|
| 94 | t.end();
|
|---|
| 95 | });
|
|---|
| 96 |
|
|---|
| 97 | test('Map', { skip: typeof Map !== 'function' }, function (t) {
|
|---|
| 98 | var map = new Map();
|
|---|
| 99 | map.set({ a: 1 }, ['b']);
|
|---|
| 100 | map.set(3, NaN);
|
|---|
| 101 | var expectedString = 'Map (2) {' + inspect({ a: 1 }) + ' => ' + inspect(['b']) + ', 3 => NaN}';
|
|---|
| 102 | t.equal(inspect(map), expectedString, 'new Map([[{ a: 1 }, ["b"]], [3, NaN]]) should show size and contents');
|
|---|
| 103 | t.equal(inspect(new Map()), 'Map (0) {}', 'empty Map should show as empty');
|
|---|
| 104 |
|
|---|
| 105 | var nestedMap = new Map();
|
|---|
| 106 | nestedMap.set(nestedMap, map);
|
|---|
| 107 | t.equal(inspect(nestedMap), 'Map (1) {[Circular] => ' + expectedString + '}', 'Map containing a Map should work');
|
|---|
| 108 |
|
|---|
| 109 | t.end();
|
|---|
| 110 | });
|
|---|
| 111 |
|
|---|
| 112 | test('WeakMap', { skip: typeof WeakMap !== 'function' }, function (t) {
|
|---|
| 113 | var map = new WeakMap();
|
|---|
| 114 | map.set({ a: 1 }, ['b']);
|
|---|
| 115 | var expectedString = 'WeakMap { ? }';
|
|---|
| 116 | t.equal(inspect(map), expectedString, 'new WeakMap([[{ a: 1 }, ["b"]]]) should not show size or contents');
|
|---|
| 117 | t.equal(inspect(new WeakMap()), 'WeakMap { ? }', 'empty WeakMap should not show as empty');
|
|---|
| 118 |
|
|---|
| 119 | t.end();
|
|---|
| 120 | });
|
|---|
| 121 |
|
|---|
| 122 | test('Set', { skip: typeof Set !== 'function' }, function (t) {
|
|---|
| 123 | var set = new Set();
|
|---|
| 124 | set.add({ a: 1 });
|
|---|
| 125 | set.add(['b']);
|
|---|
| 126 | var expectedString = 'Set (2) {' + inspect({ a: 1 }) + ', ' + inspect(['b']) + '}';
|
|---|
| 127 | t.equal(inspect(set), expectedString, 'new Set([{ a: 1 }, ["b"]]) should show size and contents');
|
|---|
| 128 | t.equal(inspect(new Set()), 'Set (0) {}', 'empty Set should show as empty');
|
|---|
| 129 |
|
|---|
| 130 | var nestedSet = new Set();
|
|---|
| 131 | nestedSet.add(set);
|
|---|
| 132 | nestedSet.add(nestedSet);
|
|---|
| 133 | t.equal(inspect(nestedSet), 'Set (2) {' + expectedString + ', [Circular]}', 'Set containing a Set should work');
|
|---|
| 134 |
|
|---|
| 135 | t.end();
|
|---|
| 136 | });
|
|---|
| 137 |
|
|---|
| 138 | test('WeakSet', { skip: typeof WeakSet !== 'function' }, function (t) {
|
|---|
| 139 | var map = new WeakSet();
|
|---|
| 140 | map.add({ a: 1 });
|
|---|
| 141 | var expectedString = 'WeakSet { ? }';
|
|---|
| 142 | t.equal(inspect(map), expectedString, 'new WeakSet([{ a: 1 }]) should not show size or contents');
|
|---|
| 143 | t.equal(inspect(new WeakSet()), 'WeakSet { ? }', 'empty WeakSet should not show as empty');
|
|---|
| 144 |
|
|---|
| 145 | t.end();
|
|---|
| 146 | });
|
|---|
| 147 |
|
|---|
| 148 | test('WeakRef', { skip: typeof WeakRef !== 'function' }, function (t) {
|
|---|
| 149 | var ref = new WeakRef({ a: 1 });
|
|---|
| 150 | var expectedString = 'WeakRef { ? }';
|
|---|
| 151 | t.equal(inspect(ref), expectedString, 'new WeakRef({ a: 1 }) should not show contents');
|
|---|
| 152 |
|
|---|
| 153 | t.end();
|
|---|
| 154 | });
|
|---|
| 155 |
|
|---|
| 156 | test('FinalizationRegistry', { skip: typeof FinalizationRegistry !== 'function' }, function (t) {
|
|---|
| 157 | var registry = new FinalizationRegistry(function () {});
|
|---|
| 158 | var expectedString = 'FinalizationRegistry [FinalizationRegistry] {}';
|
|---|
| 159 | t.equal(inspect(registry), expectedString, 'new FinalizationRegistry(function () {}) should work normallys');
|
|---|
| 160 |
|
|---|
| 161 | t.end();
|
|---|
| 162 | });
|
|---|
| 163 |
|
|---|
| 164 | test('Strings', function (t) {
|
|---|
| 165 | var str = 'abc';
|
|---|
| 166 |
|
|---|
| 167 | t.equal(inspect(str), "'" + str + "'", 'primitive string shows as such');
|
|---|
| 168 | t.equal(inspect(str, { quoteStyle: 'single' }), "'" + str + "'", 'primitive string shows as such, single quoted');
|
|---|
| 169 | t.equal(inspect(str, { quoteStyle: 'double' }), '"' + str + '"', 'primitive string shows as such, double quoted');
|
|---|
| 170 | t.equal(inspect(Object(str)), 'Object(' + inspect(str) + ')', 'String object shows as such');
|
|---|
| 171 | t.equal(inspect(Object(str), { quoteStyle: 'single' }), 'Object(' + inspect(str, { quoteStyle: 'single' }) + ')', 'String object shows as such, single quoted');
|
|---|
| 172 | t.equal(inspect(Object(str), { quoteStyle: 'double' }), 'Object(' + inspect(str, { quoteStyle: 'double' }) + ')', 'String object shows as such, double quoted');
|
|---|
| 173 |
|
|---|
| 174 | t.end();
|
|---|
| 175 | });
|
|---|
| 176 |
|
|---|
| 177 | test('Numbers', function (t) {
|
|---|
| 178 | var num = 42;
|
|---|
| 179 |
|
|---|
| 180 | t.equal(inspect(num), String(num), 'primitive number shows as such');
|
|---|
| 181 | t.equal(inspect(Object(num)), 'Object(' + inspect(num) + ')', 'Number object shows as such');
|
|---|
| 182 |
|
|---|
| 183 | t.end();
|
|---|
| 184 | });
|
|---|
| 185 |
|
|---|
| 186 | test('Booleans', function (t) {
|
|---|
| 187 | t.equal(inspect(true), String(true), 'primitive true shows as such');
|
|---|
| 188 | t.equal(inspect(Object(true)), 'Object(' + inspect(true) + ')', 'Boolean object true shows as such');
|
|---|
| 189 |
|
|---|
| 190 | t.equal(inspect(false), String(false), 'primitive false shows as such');
|
|---|
| 191 | t.equal(inspect(Object(false)), 'Object(' + inspect(false) + ')', 'Boolean false object shows as such');
|
|---|
| 192 |
|
|---|
| 193 | t.end();
|
|---|
| 194 | });
|
|---|
| 195 |
|
|---|
| 196 | test('Date', function (t) {
|
|---|
| 197 | var now = new Date();
|
|---|
| 198 | t.equal(inspect(now), String(now), 'Date shows properly');
|
|---|
| 199 | t.equal(inspect(new Date(NaN)), 'Invalid Date', 'Invalid Date shows properly');
|
|---|
| 200 |
|
|---|
| 201 | t.end();
|
|---|
| 202 | });
|
|---|
| 203 |
|
|---|
| 204 | test('RegExps', function (t) {
|
|---|
| 205 | t.equal(inspect(/a/g), '/a/g', 'regex shows properly');
|
|---|
| 206 | t.equal(inspect(new RegExp('abc', 'i')), '/abc/i', 'new RegExp shows properly');
|
|---|
| 207 |
|
|---|
| 208 | var match = 'abc abc'.match(/[ab]+/);
|
|---|
| 209 | delete match.groups; // for node < 10
|
|---|
| 210 | t.equal(inspect(match), '[ \'ab\', index: 0, input: \'abc abc\' ]', 'RegExp match object shows properly');
|
|---|
| 211 |
|
|---|
| 212 | t.end();
|
|---|
| 213 | });
|
|---|
| 214 |
|
|---|
| 215 | test('Proxies', { skip: typeof Proxy !== 'function' || !hasToStringTag }, function (t) {
|
|---|
| 216 | var target = { proxy: true };
|
|---|
| 217 | var fake = new Proxy(target, { has: function () { return false; } });
|
|---|
| 218 |
|
|---|
| 219 | // needed to work around a weird difference in node v6.0 - v6.4 where non-present properties are not logged
|
|---|
| 220 | var isNode60 = semver.satisfies(process.version, '6.0 - 6.4');
|
|---|
| 221 |
|
|---|
| 222 | forEach([
|
|---|
| 223 | 'Boolean',
|
|---|
| 224 | 'Number',
|
|---|
| 225 | 'String',
|
|---|
| 226 | 'Symbol',
|
|---|
| 227 | 'Date'
|
|---|
| 228 | ], function (tag) {
|
|---|
| 229 | target[Symbol.toStringTag] = tag;
|
|---|
| 230 |
|
|---|
| 231 | t.equal(
|
|---|
| 232 | inspect(fake),
|
|---|
| 233 | '{ ' + (isNode60 ? '' : 'proxy: true, ') + '[Symbol(Symbol.toStringTag)]: \'' + tag + '\' }',
|
|---|
| 234 | 'Proxy for + ' + tag + ' shows as the target, which has no slots'
|
|---|
| 235 | );
|
|---|
| 236 | });
|
|---|
| 237 |
|
|---|
| 238 | t.end();
|
|---|
| 239 | });
|
|---|
| 240 |
|
|---|
| 241 | test('fakers', { skip: !hasToStringTag }, function (t) {
|
|---|
| 242 | var target = { proxy: false };
|
|---|
| 243 |
|
|---|
| 244 | forEach([
|
|---|
| 245 | 'Boolean',
|
|---|
| 246 | 'Number',
|
|---|
| 247 | 'String',
|
|---|
| 248 | 'Symbol',
|
|---|
| 249 | 'Date'
|
|---|
| 250 | ], function (tag) {
|
|---|
| 251 | target[Symbol.toStringTag] = tag;
|
|---|
| 252 |
|
|---|
| 253 | t.equal(
|
|---|
| 254 | inspect(target),
|
|---|
| 255 | '{ proxy: false, [Symbol(Symbol.toStringTag)]: \'' + tag + '\' }',
|
|---|
| 256 | 'Object pretending to be ' + tag + ' does not trick us'
|
|---|
| 257 | );
|
|---|
| 258 | });
|
|---|
| 259 |
|
|---|
| 260 | t.end();
|
|---|
| 261 | });
|
|---|