blob: 90972923e69044d6548c5ab51b36e5e005d22638 [file] [log] [blame]
Leo Repp58b9f112021-11-22 11:57:47 +01001var inspect = require('../');
2var test = require('tape');
3
4test('values', function (t) {
5 t.plan(1);
6 var obj = [{}, [], { 'a-b': 5 }];
7 t.equal(inspect(obj), '[ {}, [], { \'a-b\': 5 } ]');
8});
9
10test('arrays with properties', function (t) {
11 t.plan(1);
12 var arr = [3];
13 arr.foo = 'bar';
14 var obj = [1, 2, arr];
15 obj.baz = 'quux';
16 obj.index = -1;
17 t.equal(inspect(obj), '[ 1, 2, [ 3, foo: \'bar\' ], baz: \'quux\', index: -1 ]');
18});
19
20test('has', function (t) {
21 t.plan(1);
22 var has = Object.prototype.hasOwnProperty;
23 delete Object.prototype.hasOwnProperty;
24 t.equal(inspect({ a: 1, b: 2 }), '{ a: 1, b: 2 }');
25 Object.prototype.hasOwnProperty = has; // eslint-disable-line no-extend-native
26});
27
28test('indexOf seen', function (t) {
29 t.plan(1);
30 var xs = [1, 2, 3, {}];
31 xs.push(xs);
32
33 var seen = [];
34 seen.indexOf = undefined;
35
36 t.equal(
37 inspect(xs, {}, 0, seen),
38 '[ 1, 2, 3, {}, [Circular] ]'
39 );
40});
41
42test('seen seen', function (t) {
43 t.plan(1);
44 var xs = [1, 2, 3];
45
46 var seen = [xs];
47 seen.indexOf = undefined;
48
49 t.equal(
50 inspect(xs, {}, 0, seen),
51 '[Circular]'
52 );
53});
54
55test('seen seen seen', function (t) {
56 t.plan(1);
57 var xs = [1, 2, 3];
58
59 var seen = [5, xs];
60 seen.indexOf = undefined;
61
62 t.equal(
63 inspect(xs, {}, 0, seen),
64 '[Circular]'
65 );
66});
67
68test('symbols', { skip: typeof Symbol !== 'function' }, function (t) {
69 var sym = Symbol('foo');
70 t.equal(inspect(sym), 'Symbol(foo)', 'Symbol("foo") should be "Symbol(foo)"');
71 t.equal(inspect(Object(sym)), 'Object(Symbol(foo))', 'Object(Symbol("foo")) should be "Object(Symbol(foo))"');
72 t.end();
73});
74
75test('Map', { skip: typeof Map !== 'function' }, function (t) {
76 var map = new Map();
77 map.set({ a: 1 }, ['b']);
78 map.set(3, NaN);
79 var expectedString = 'Map (2) {' + inspect({ a: 1 }) + ' => ' + inspect(['b']) + ', 3 => NaN}';
80 t.equal(inspect(map), expectedString, 'new Map([[{ a: 1 }, ["b"]], [3, NaN]]) should show size and contents');
81 t.equal(inspect(new Map()), 'Map (0) {}', 'empty Map should show as empty');
82
83 var nestedMap = new Map();
84 nestedMap.set(nestedMap, map);
85 t.equal(inspect(nestedMap), 'Map (1) {[Circular] => ' + expectedString + '}', 'Map containing a Map should work');
86
87 t.end();
88});
89
90test('WeakMap', { skip: typeof WeakMap !== 'function' }, function (t) {
91 var map = new WeakMap();
92 map.set({ a: 1 }, ['b']);
93 var expectedString = 'WeakMap { ? }';
94 t.equal(inspect(map), expectedString, 'new WeakMap([[{ a: 1 }, ["b"]]]) should not show size or contents');
95 t.equal(inspect(new WeakMap()), 'WeakMap { ? }', 'empty WeakMap should not show as empty');
96
97 t.end();
98});
99
100test('Set', { skip: typeof Set !== 'function' }, function (t) {
101 var set = new Set();
102 set.add({ a: 1 });
103 set.add(['b']);
104 var expectedString = 'Set (2) {' + inspect({ a: 1 }) + ', ' + inspect(['b']) + '}';
105 t.equal(inspect(set), expectedString, 'new Set([{ a: 1 }, ["b"]]) should show size and contents');
106 t.equal(inspect(new Set()), 'Set (0) {}', 'empty Set should show as empty');
107
108 var nestedSet = new Set();
109 nestedSet.add(set);
110 nestedSet.add(nestedSet);
111 t.equal(inspect(nestedSet), 'Set (2) {' + expectedString + ', [Circular]}', 'Set containing a Set should work');
112
113 t.end();
114});
115
116test('WeakSet', { skip: typeof WeakSet !== 'function' }, function (t) {
117 var map = new WeakSet();
118 map.add({ a: 1 });
119 var expectedString = 'WeakSet { ? }';
120 t.equal(inspect(map), expectedString, 'new WeakSet([{ a: 1 }]) should not show size or contents');
121 t.equal(inspect(new WeakSet()), 'WeakSet { ? }', 'empty WeakSet should not show as empty');
122
123 t.end();
124});
125
126test('Strings', function (t) {
127 var str = 'abc';
128
129 t.equal(inspect(str), "'" + str + "'", 'primitive string shows as such');
130 t.equal(inspect(str, { quoteStyle: 'single' }), "'" + str + "'", 'primitive string shows as such, single quoted');
131 t.equal(inspect(str, { quoteStyle: 'double' }), '"' + str + '"', 'primitive string shows as such, double quoted');
132 t.equal(inspect(Object(str)), 'Object(' + inspect(str) + ')', 'String object shows as such');
133 t.equal(inspect(Object(str), { quoteStyle: 'single' }), 'Object(' + inspect(str, { quoteStyle: 'single' }) + ')', 'String object shows as such, single quoted');
134 t.equal(inspect(Object(str), { quoteStyle: 'double' }), 'Object(' + inspect(str, { quoteStyle: 'double' }) + ')', 'String object shows as such, double quoted');
135
136 t.end();
137});
138
139test('Numbers', function (t) {
140 var num = 42;
141
142 t.equal(inspect(num), String(num), 'primitive number shows as such');
143 t.equal(inspect(Object(num)), 'Object(' + inspect(num) + ')', 'Number object shows as such');
144
145 t.end();
146});
147
148test('Booleans', function (t) {
149 t.equal(inspect(true), String(true), 'primitive true shows as such');
150 t.equal(inspect(Object(true)), 'Object(' + inspect(true) + ')', 'Boolean object true shows as such');
151
152 t.equal(inspect(false), String(false), 'primitive false shows as such');
153 t.equal(inspect(Object(false)), 'Object(' + inspect(false) + ')', 'Boolean false object shows as such');
154
155 t.end();
156});
157
158test('Date', function (t) {
159 var now = new Date();
160 t.equal(inspect(now), String(now), 'Date shows properly');
161 t.equal(inspect(new Date(NaN)), 'Invalid Date', 'Invalid Date shows properly');
162
163 t.end();
164});
165
166test('RegExps', function (t) {
167 t.equal(inspect(/a/g), '/a/g', 'regex shows properly');
168 t.equal(inspect(new RegExp('abc', 'i')), '/abc/i', 'new RegExp shows properly');
169
170 var match = 'abc abc'.match(/[ab]+/);
171 delete match.groups; // for node < 10
172 t.equal(inspect(match), '[ \'ab\', index: 0, input: \'abc abc\' ]', 'RegExp match object shows properly');
173
174 t.end();
175});