Skip to content

Commit a955d4e

Browse files
test: add inspect tests for null constructor scenarios
1 parent 98e72c1 commit a955d4e

File tree

1 file changed

+82
-0
lines changed

1 file changed

+82
-0
lines changed

test/parallel/test-util-inspect.js

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,88 @@ assert.strictEqual(util.inspect(async () => {}), '[AsyncFunction (anonymous)]');
8585
);
8686
}
8787

88+
// Null constructor scenarios
89+
{
90+
function fnNull() {}
91+
Object.setPrototypeOf(fnNull, null);
92+
assert.strictEqual(
93+
util.inspect(fnNull),
94+
'[Function (null prototype): fnNull]'
95+
);
96+
97+
function fnNullAndStringTag() {}
98+
Object.defineProperty(fnNullAndStringTag, Symbol.toStringTag, {
99+
value: 'CustomTag',
100+
configurable: true
101+
});
102+
Object.setPrototypeOf(fnNullAndStringTag, null);
103+
assert.strictEqual(
104+
util.inspect(fnNullAndStringTag),
105+
'[Function (null prototype): fnNullAndStringTag] [CustomTag]'
106+
);
107+
108+
function fnAnonymous() {}
109+
Object.defineProperty(fnAnonymous, Symbol.toStringTag, {
110+
value: 'AnonymousCustom',
111+
configurable: true
112+
});
113+
Object.setPrototypeOf(fnAnonymous, null);
114+
assert.strictEqual(
115+
util.inspect(fnAnonymous),
116+
'[Function (null prototype): fnAnonymous] [AnonymousCustom]'
117+
);
118+
119+
const fnArrow = () => {};
120+
Object.setPrototypeOf(fnArrow, null);
121+
assert.strictEqual(
122+
util.inspect(fnArrow),
123+
'[Function (null prototype): fnArrow]'
124+
);
125+
126+
async function fnAsync() {}
127+
Object.defineProperty(fnAsync, Symbol.toStringTag, {
128+
value: 'AsyncCustom',
129+
configurable: true
130+
});
131+
Object.setPrototypeOf(fnAsync, null);
132+
assert.strictEqual(
133+
util.inspect(fnAsync),
134+
'[AsyncFunction (null prototype): fnAsync] [AsyncCustom]'
135+
);
136+
137+
class TestClass {}
138+
Object.defineProperty(TestClass, Symbol.toStringTag, {
139+
value: 'ClassTag',
140+
configurable: true
141+
});
142+
Object.setPrototypeOf(TestClass, null);
143+
assert.strictEqual(
144+
util.inspect(TestClass),
145+
'[class TestClass [ClassTag] extends [null prototype]]'
146+
);
147+
148+
function fnMatchConstructor() {}
149+
Object.defineProperty(fnMatchConstructor, Symbol.toStringTag, {
150+
value: 'Function',
151+
configurable: true
152+
});
153+
assert.strictEqual(
154+
util.inspect(fnMatchConstructor),
155+
'[Function: fnMatchConstructor]'
156+
);
157+
158+
function fnNullMatchConstructor() {}
159+
Object.defineProperty(fnNullMatchConstructor, Symbol.toStringTag, {
160+
value: 'Function',
161+
configurable: true
162+
});
163+
Object.setPrototypeOf(fnNullMatchConstructor, null);
164+
assert.strictEqual(
165+
util.inspect(fnNullMatchConstructor),
166+
'[Function (null prototype): fnNullMatchConstructor] [Function]'
167+
);
168+
}
169+
88170
assert.strictEqual(util.inspect(undefined), 'undefined');
89171
assert.strictEqual(util.inspect(null), 'null');
90172
assert.strictEqual(util.inspect(/foo(bar\n)?/gi), '/foo(bar\\n)?/gi');

0 commit comments

Comments
 (0)