[d565449] | 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 |
|
---|
| 9 | test('values', function (t) {
|
---|
| 10 | t.plan(1);
|
---|
| 11 | var obj = [{}, [], { 'a-b': 5 }];
|
---|
| 12 | t.equal(inspect(obj), '[ {}, [], { \'a-b\': 5 } ]');
|
---|
| 13 | });
|
---|
| 14 |
|
---|
| 15 | test('arrays with properties', function (t) {
|
---|
| 16 | t.plan(1);
|
---|
| 17 | var arr = [3];
|
---|
| 18 | arr.foo = 'bar';
|
---|
| 19 | var obj = [1, 2, arr];
|
---|
| 20 | obj.baz = 'quux';
|
---|
| 21 | obj.index = -1;
|
---|
| 22 | t.equal(inspect(obj), '[ 1, 2, [ 3, foo: \'bar\' ], baz: \'quux\', index: -1 ]');
|
---|
| 23 | });
|
---|
| 24 |
|
---|
| 25 | test('has', function (t) {
|
---|
| 26 | t.plan(1);
|
---|
| 27 | t.teardown(mockProperty(Object.prototype, 'hasOwnProperty', { 'delete': true }));
|
---|
| 28 |
|
---|
| 29 | t.equal(inspect({ a: 1, b: 2 }), '{ a: 1, b: 2 }');
|
---|
| 30 | });
|
---|
| 31 |
|
---|
| 32 | test('indexOf seen', function (t) {
|
---|
| 33 | t.plan(1);
|
---|
| 34 | var xs = [1, 2, 3, {}];
|
---|
| 35 | xs.push(xs);
|
---|
| 36 |
|
---|
| 37 | var seen = [];
|
---|
| 38 | seen.indexOf = undefined;
|
---|
| 39 |
|
---|
| 40 | t.equal(
|
---|
| 41 | inspect(xs, {}, 0, seen),
|
---|
| 42 | '[ 1, 2, 3, {}, [Circular] ]'
|
---|
| 43 | );
|
---|
| 44 | });
|
---|
| 45 |
|
---|
| 46 | test('seen seen', function (t) {
|
---|
| 47 | t.plan(1);
|
---|
| 48 | var xs = [1, 2, 3];
|
---|
| 49 |
|
---|
| 50 | var seen = [xs];
|
---|
| 51 | seen.indexOf = undefined;
|
---|
| 52 |
|
---|
| 53 | t.equal(
|
---|
| 54 | inspect(xs, {}, 0, seen),
|
---|
| 55 | '[Circular]'
|
---|
| 56 | );
|
---|
| 57 | });
|
---|
| 58 |
|
---|
| 59 | test('seen seen seen', function (t) {
|
---|
| 60 | t.plan(1);
|
---|
| 61 | var xs = [1, 2, 3];
|
---|
| 62 |
|
---|
| 63 | var seen = [5, xs];
|
---|
| 64 | seen.indexOf = undefined;
|
---|
| 65 |
|
---|
| 66 | t.equal(
|
---|
| 67 | inspect(xs, {}, 0, seen),
|
---|
| 68 | '[Circular]'
|
---|
| 69 | );
|
---|
| 70 | });
|
---|
| 71 |
|
---|
| 72 | test('symbols', { skip: !hasSymbols }, function (t) {
|
---|
| 73 | var sym = Symbol('foo');
|
---|
| 74 | t.equal(inspect(sym), 'Symbol(foo)', 'Symbol("foo") should be "Symbol(foo)"');
|
---|
| 75 | if (typeof sym === 'symbol') {
|
---|
| 76 | // Symbol shams are incapable of differentiating boxed from unboxed symbols
|
---|
| 77 | t.equal(inspect(Object(sym)), 'Object(Symbol(foo))', 'Object(Symbol("foo")) should be "Object(Symbol(foo))"');
|
---|
| 78 | }
|
---|
| 79 |
|
---|
| 80 | t.test('toStringTag', { skip: !hasToStringTag }, function (st) {
|
---|
| 81 | st.plan(1);
|
---|
| 82 |
|
---|
| 83 | var faker = {};
|
---|
| 84 | faker[Symbol.toStringTag] = 'Symbol';
|
---|
| 85 | st.equal(
|
---|
| 86 | inspect(faker),
|
---|
| 87 | '{ [Symbol(Symbol.toStringTag)]: \'Symbol\' }',
|
---|
| 88 | 'object lying about being a Symbol inspects as an object'
|
---|
| 89 | );
|
---|
| 90 | });
|
---|
| 91 |
|
---|
| 92 | t.end();
|
---|
| 93 | });
|
---|
| 94 |
|
---|
| 95 | test('Map', { skip: typeof Map !== 'function' }, function (t) {
|
---|
| 96 | var map = new Map();
|
---|
| 97 | map.set({ a: 1 }, ['b']);
|
---|
| 98 | map.set(3, NaN);
|
---|
| 99 | var expectedString = 'Map (2) {' + inspect({ a: 1 }) + ' => ' + inspect(['b']) + ', 3 => NaN}';
|
---|
| 100 | t.equal(inspect(map), expectedString, 'new Map([[{ a: 1 }, ["b"]], [3, NaN]]) should show size and contents');
|
---|
| 101 | t.equal(inspect(new Map()), 'Map (0) {}', 'empty Map should show as empty');
|
---|
| 102 |
|
---|
| 103 | var nestedMap = new Map();
|
---|
| 104 | nestedMap.set(nestedMap, map);
|
---|
| 105 | t.equal(inspect(nestedMap), 'Map (1) {[Circular] => ' + expectedString + '}', 'Map containing a Map should work');
|
---|
| 106 |
|
---|
| 107 | t.end();
|
---|
| 108 | });
|
---|
| 109 |
|
---|
| 110 | test('WeakMap', { skip: typeof WeakMap !== 'function' }, function (t) {
|
---|
| 111 | var map = new WeakMap();
|
---|
| 112 | map.set({ a: 1 }, ['b']);
|
---|
| 113 | var expectedString = 'WeakMap { ? }';
|
---|
| 114 | t.equal(inspect(map), expectedString, 'new WeakMap([[{ a: 1 }, ["b"]]]) should not show size or contents');
|
---|
| 115 | t.equal(inspect(new WeakMap()), 'WeakMap { ? }', 'empty WeakMap should not show as empty');
|
---|
| 116 |
|
---|
| 117 | t.end();
|
---|
| 118 | });
|
---|
| 119 |
|
---|
| 120 | test('Set', { skip: typeof Set !== 'function' }, function (t) {
|
---|
| 121 | var set = new Set();
|
---|
| 122 | set.add({ a: 1 });
|
---|
| 123 | set.add(['b']);
|
---|
| 124 | var expectedString = 'Set (2) {' + inspect({ a: 1 }) + ', ' + inspect(['b']) + '}';
|
---|
| 125 | t.equal(inspect(set), expectedString, 'new Set([{ a: 1 }, ["b"]]) should show size and contents');
|
---|
| 126 | t.equal(inspect(new Set()), 'Set (0) {}', 'empty Set should show as empty');
|
---|
| 127 |
|
---|
| 128 | var nestedSet = new Set();
|
---|
| 129 | nestedSet.add(set);
|
---|
| 130 | nestedSet.add(nestedSet);
|
---|
| 131 | t.equal(inspect(nestedSet), 'Set (2) {' + expectedString + ', [Circular]}', 'Set containing a Set should work');
|
---|
| 132 |
|
---|
| 133 | t.end();
|
---|
| 134 | });
|
---|
| 135 |
|
---|
| 136 | test('WeakSet', { skip: typeof WeakSet !== 'function' }, function (t) {
|
---|
| 137 | var map = new WeakSet();
|
---|
| 138 | map.add({ a: 1 });
|
---|
| 139 | var expectedString = 'WeakSet { ? }';
|
---|
| 140 | t.equal(inspect(map), expectedString, 'new WeakSet([{ a: 1 }]) should not show size or contents');
|
---|
| 141 | t.equal(inspect(new WeakSet()), 'WeakSet { ? }', 'empty WeakSet should not show as empty');
|
---|
| 142 |
|
---|
| 143 | t.end();
|
---|
| 144 | });
|
---|
| 145 |
|
---|
| 146 | test('WeakRef', { skip: typeof WeakRef !== 'function' }, function (t) {
|
---|
| 147 | var ref = new WeakRef({ a: 1 });
|
---|
| 148 | var expectedString = 'WeakRef { ? }';
|
---|
| 149 | t.equal(inspect(ref), expectedString, 'new WeakRef({ a: 1 }) should not show contents');
|
---|
| 150 |
|
---|
| 151 | t.end();
|
---|
| 152 | });
|
---|
| 153 |
|
---|
| 154 | test('FinalizationRegistry', { skip: typeof FinalizationRegistry !== 'function' }, function (t) {
|
---|
| 155 | var registry = new FinalizationRegistry(function () {});
|
---|
| 156 | var expectedString = 'FinalizationRegistry [FinalizationRegistry] {}';
|
---|
| 157 | t.equal(inspect(registry), expectedString, 'new FinalizationRegistry(function () {}) should work normallys');
|
---|
| 158 |
|
---|
| 159 | t.end();
|
---|
| 160 | });
|
---|
| 161 |
|
---|
| 162 | test('Strings', function (t) {
|
---|
| 163 | var str = 'abc';
|
---|
| 164 |
|
---|
| 165 | t.equal(inspect(str), "'" + str + "'", 'primitive string shows as such');
|
---|
| 166 | t.equal(inspect(str, { quoteStyle: 'single' }), "'" + str + "'", 'primitive string shows as such, single quoted');
|
---|
| 167 | t.equal(inspect(str, { quoteStyle: 'double' }), '"' + str + '"', 'primitive string shows as such, double quoted');
|
---|
| 168 | t.equal(inspect(Object(str)), 'Object(' + inspect(str) + ')', 'String object shows as such');
|
---|
| 169 | t.equal(inspect(Object(str), { quoteStyle: 'single' }), 'Object(' + inspect(str, { quoteStyle: 'single' }) + ')', 'String object shows as such, single quoted');
|
---|
| 170 | t.equal(inspect(Object(str), { quoteStyle: 'double' }), 'Object(' + inspect(str, { quoteStyle: 'double' }) + ')', 'String object shows as such, double quoted');
|
---|
| 171 |
|
---|
| 172 | t.end();
|
---|
| 173 | });
|
---|
| 174 |
|
---|
| 175 | test('Numbers', function (t) {
|
---|
| 176 | var num = 42;
|
---|
| 177 |
|
---|
| 178 | t.equal(inspect(num), String(num), 'primitive number shows as such');
|
---|
| 179 | t.equal(inspect(Object(num)), 'Object(' + inspect(num) + ')', 'Number object shows as such');
|
---|
| 180 |
|
---|
| 181 | t.end();
|
---|
| 182 | });
|
---|
| 183 |
|
---|
| 184 | test('Booleans', function (t) {
|
---|
| 185 | t.equal(inspect(true), String(true), 'primitive true shows as such');
|
---|
| 186 | t.equal(inspect(Object(true)), 'Object(' + inspect(true) + ')', 'Boolean object true shows as such');
|
---|
| 187 |
|
---|
| 188 | t.equal(inspect(false), String(false), 'primitive false shows as such');
|
---|
| 189 | t.equal(inspect(Object(false)), 'Object(' + inspect(false) + ')', 'Boolean false object shows as such');
|
---|
| 190 |
|
---|
| 191 | t.end();
|
---|
| 192 | });
|
---|
| 193 |
|
---|
| 194 | test('Date', function (t) {
|
---|
| 195 | var now = new Date();
|
---|
| 196 | t.equal(inspect(now), String(now), 'Date shows properly');
|
---|
| 197 | t.equal(inspect(new Date(NaN)), 'Invalid Date', 'Invalid Date shows properly');
|
---|
| 198 |
|
---|
| 199 | t.end();
|
---|
| 200 | });
|
---|
| 201 |
|
---|
| 202 | test('RegExps', function (t) {
|
---|
| 203 | t.equal(inspect(/a/g), '/a/g', 'regex shows properly');
|
---|
| 204 | t.equal(inspect(new RegExp('abc', 'i')), '/abc/i', 'new RegExp shows properly');
|
---|
| 205 |
|
---|
| 206 | var match = 'abc abc'.match(/[ab]+/);
|
---|
| 207 | delete match.groups; // for node < 10
|
---|
| 208 | t.equal(inspect(match), '[ \'ab\', index: 0, input: \'abc abc\' ]', 'RegExp match object shows properly');
|
---|
| 209 |
|
---|
| 210 | t.end();
|
---|
| 211 | });
|
---|