Debian Codegraph @

get-intrinsic/test/GetIntrinsic.js

raw ◀ ▶
Source reconstructed at patch state 0 of 1 — not the installed (fully patched) source.
1 Debian patch touch this file
1 'use strict';
2
3 var GetIntrinsic = require('../');
4
5 var test = require('tape');
6 //var forEach = require('for-each');
7 var debug = require('object-inspect');
8 //var generatorFns = require('make-generator-function')();
9 //var asyncFns = require('make-async-function').list();
10 //var asyncGenFns = require('make-async-generator-function')();
11 //var mockProperty = require('mock-property');
12
13 var callBound = require('call-bind/callBound');
14 //var v = require('es-value-fixtures');
15 var $gOPD = require('gopd');
16 var DefinePropertyOrThrow = require('es-abstract/2021/DefinePropertyOrThrow');
17
18 var $isProto = callBound('%Object.prototype.isPrototypeOf%');
19
20 test('export', function (t) {
21 t.equal(typeof GetIntrinsic, 'function', 'it is a function');
22 t.equal(GetIntrinsic.length, 2, 'function has length of 2');
23
24 t.end();
25 });
26
27 test('throws', function (t) {
28 t['throws'](
29 function () { GetIntrinsic('not an intrinsic'); },
30 SyntaxError,
31 'nonexistent intrinsic throws a syntax error'
32 );
33
34 t['throws'](
35 function () { GetIntrinsic(''); },
36 TypeError,
37 'empty string intrinsic throws a type error'
38 );
39
40 t['throws'](
41 function () { GetIntrinsic('.'); },
42 SyntaxError,
43 '"just a dot" intrinsic throws a syntax error'
44 );
45
46 t['throws'](
47 function () { GetIntrinsic('%String'); },
48 SyntaxError,
49 'Leading % without trailing % throws a syntax error'
50 );
51
52 t['throws'](
53 function () { GetIntrinsic('String%'); },
54 SyntaxError,
55 'Trailing % without leading % throws a syntax error'
56 );
57
58 t['throws'](
59 function () { GetIntrinsic("String['prototype]"); },
60 SyntaxError,
61 'Dynamic property access is disallowed for intrinsics (unterminated string)'
62 );
63
64 t['throws'](
65 function () { GetIntrinsic('%Proxy.prototype.undefined%'); },
66 TypeError,
67 "Throws when middle part doesn't exist (%Proxy.prototype.undefined%)"
68 );
69
70 t['throws'](
71 function () { GetIntrinsic('%Array.prototype%garbage%'); },
72 SyntaxError,
73 'Throws with extra percent signs'
74 );
75
76 t['throws'](
77 function () { GetIntrinsic('%Array.prototype%push%'); },
78 SyntaxError,
79 'Throws with extra percent signs, even on an existing intrinsic'
80 );
81 /*
82 v.nonStrings.foreach( function (nonString) {
83 t['throws'](
84 function () { GetIntrinsic(nonString); },
85 TypeError,
86 debug(nonString) + ' is not a String'
87 );
88 });
89
90 v.nonBooleans.foreach( function (nonBoolean) {
91 t['throws'](
92 function () { GetIntrinsic('%', nonBoolean); },
93 TypeError,
94 debug(nonBoolean) + ' is not a Boolean'
95 );
96 });
97
98 [
99 'toString',
100 'propertyIsEnumerable',
101 'hasOwnProperty'
102 ].foreach( function (objectProtoMember) {
103 t['throws'](
104 function () { GetIntrinsic(objectProtoMember); },
105 SyntaxError,
106 debug(objectProtoMember) + ' is not an intrinsic'
107 );
108 });
109 */
110 t.end();
111 });
112
113 test('base intrinsics', function (t) {
114 t.equal(GetIntrinsic('%Object%'), Object, '%Object% yields Object');
115 t.equal(GetIntrinsic('Object'), Object, 'Object yields Object');
116 t.equal(GetIntrinsic('%Array%'), Array, '%Array% yields Array');
117 t.equal(GetIntrinsic('Array'), Array, 'Array yields Array');
118
119 t.end();
120 });
121
122 test('dotted paths', function (t) {
123 t.equal(GetIntrinsic('%Object.prototype.toString%'), Object.prototype.toString, '%Object.prototype.toString% yields Object.prototype.toString');
124 t.equal(GetIntrinsic('Object.prototype.toString'), Object.prototype.toString, 'Object.prototype.toString yields Object.prototype.toString');
125 t.equal(GetIntrinsic('%Array.prototype.push%'), Array.prototype.push, '%Array.prototype.push% yields Array.prototype.push');
126 t.equal(GetIntrinsic('Array.prototype.push'), Array.prototype.push, 'Array.prototype.push yields Array.prototype.push');
127 /*
128 test('underscore paths are aliases for dotted paths', { skip: !Object.isFrozen || Object.isFrozen(Object.prototype) }, function (st) {
129 var original = GetIntrinsic('%ObjProto_toString%');
130
131 forEach([
132 '%Object.prototype.toString%',
133 'Object.prototype.toString',
134 '%ObjectPrototype.toString%',
135 'ObjectPrototype.toString',
136 '%ObjProto_toString%',
137 'ObjProto_toString'
138 ], function (name) {
139 DefinePropertyOrThrow(Object.prototype, 'toString', {
140 '[[Value]]': function toString() {
141 return original.apply(this, arguments);
142 }
143 });
144 st.equal(GetIntrinsic(name), original, name + ' yields original Object.prototype.toString');
145 });
146
147 DefinePropertyOrThrow(Object.prototype, 'toString', { '[[Value]]': original });
148 st.end();
149 });
150 */
151 /*
152 test('dotted paths cache', { skip: !Object.isFrozen || Object.isFrozen(Object.prototype) }, function (st) {
153 var original = GetIntrinsic('%Object.prototype.propertyIsEnumerable%');
154
155 forEach([
156 '%Object.prototype.propertyIsEnumerable%',
157 'Object.prototype.propertyIsEnumerable',
158 '%ObjectPrototype.propertyIsEnumerable%',
159 'ObjectPrototype.propertyIsEnumerable'
160 ], function (name) {
161 var restore = mockProperty(Object.prototype, 'propertyIsEnumerable', {
162 value: function propertyIsEnumerable() {
163 return original.apply(this, arguments);
164 }
165 });
166 st.equal(GetIntrinsic(name), original, name + ' yields cached Object.prototype.propertyIsEnumerable');
167
168 restore();
169 });
170
171 st.end();
172 });
173 */
174 test('dotted path reports correct error', function (st) {
175 st['throws'](function () {
176 GetIntrinsic('%NonExistentIntrinsic.prototype.property%');
177 }, /%NonExistentIntrinsic%/, 'The base intrinsic of %NonExistentIntrinsic.prototype.property% is %NonExistentIntrinsic%');
178
179 st['throws'](function () {
180 GetIntrinsic('%NonExistentIntrinsicPrototype.property%');
181 }, /%NonExistentIntrinsicPrototype%/, 'The base intrinsic of %NonExistentIntrinsicPrototype.property% is %NonExistentIntrinsicPrototype%');
182
183 st.end();
184 });
185
186 t.end();
187 });
188
189 test('accessors', { skip: !$gOPD || typeof Map !== 'function' }, function (t) {
190 var actual = $gOPD(Map.prototype, 'size');
191 t.ok(actual, 'Map.prototype.size has a descriptor');
192 t.equal(typeof actual.get, 'function', 'Map.prototype.size has a getter function');
193 t.equal(GetIntrinsic('%Map.prototype.size%'), actual.get, '%Map.prototype.size% yields the getter for it');
194 t.equal(GetIntrinsic('Map.prototype.size'), actual.get, 'Map.prototype.size yields the getter for it');
195
196 t.end();
197 });
198 /*
199 test('generator functions', { skip: !generatorFns.length }, function (t) {
200 var $GeneratorFunction = GetIntrinsic('%GeneratorFunction%');
201 var $GeneratorFunctionPrototype = GetIntrinsic('%Generator%');
202 var $GeneratorPrototype = GetIntrinsic('%GeneratorPrototype%');
203
204 forEach(generatorFns, function (genFn) {
205 var fnName = genFn.name;
206 fnName = fnName ? "'" + fnName + "'" : 'genFn';
207
208 t.ok(genFn instanceof $GeneratorFunction, fnName + ' instanceof %GeneratorFunction%');
209 t.ok($isProto($GeneratorFunctionPrototype, genFn), '%Generator% is prototype of ' + fnName);
210 t.ok($isProto($GeneratorPrototype, genFn.prototype), '%GeneratorPrototype% is prototype of ' + fnName + '.prototype');
211 });
212
213 t.end();
214 });
215 */
216 /*
217 test('async functions', { skip: !asyncFns.length }, function (t) {
218 var $AsyncFunction = GetIntrinsic('%AsyncFunction%');
219 var $AsyncFunctionPrototype = GetIntrinsic('%AsyncFunctionPrototype%');
220
221 forEach(asyncFns, function (asyncFn) {
222 var fnName = asyncFn.name;
223 fnName = fnName ? "'" + fnName + "'" : 'asyncFn';
224
225 t.ok(asyncFn instanceof $AsyncFunction, fnName + ' instanceof %AsyncFunction%');
226 t.ok($isProto($AsyncFunctionPrototype, asyncFn), '%AsyncFunctionPrototype% is prototype of ' + fnName);
227 });
228
229 t.end();
230 });
231 */
232 /*
233 test('async generator functions', { skip: asyncGenFns.length === 0 }, function (t) {
234 var $AsyncGeneratorFunction = GetIntrinsic('%AsyncGeneratorFunction%');
235 var $AsyncGeneratorFunctionPrototype = GetIntrinsic('%AsyncGenerator%');
236 var $AsyncGeneratorPrototype = GetIntrinsic('%AsyncGeneratorPrototype%');
237
238 forEach(asyncGenFns, function (asyncGenFn) {
239 var fnName = asyncGenFn.name;
240 fnName = fnName ? "'" + fnName + "'" : 'asyncGenFn';
241
242 t.ok(asyncGenFn instanceof $AsyncGeneratorFunction, fnName + ' instanceof %AsyncGeneratorFunction%');
243 t.ok($isProto($AsyncGeneratorFunctionPrototype, asyncGenFn), '%AsyncGenerator% is prototype of ' + fnName);
244 t.ok($isProto($AsyncGeneratorPrototype, asyncGenFn.prototype), '%AsyncGeneratorPrototype% is prototype of ' + fnName + '.prototype');
245 });
246
247 t.end();
248 });
249 */
250 test('%ThrowTypeError%', function (t) {
251 var $ThrowTypeError = GetIntrinsic('%ThrowTypeError%');
252
253 t.equal(typeof $ThrowTypeError, 'function', 'is a function');
254 t['throws'](
255 $ThrowTypeError,
256 TypeError,
257 '%ThrowTypeError% throws a TypeError'
258 );
259
260 t.end();
261 });
262 /*
263 test('allowMissing', { skip: asyncGenFns.length > 0 }, function (t) {
264 t['throws'](
265 function () { GetIntrinsic('%AsyncGeneratorPrototype%'); },
266 TypeError,
267 'throws when missing'
268 );
269
270 t.equal(
271 GetIntrinsic('%AsyncGeneratorPrototype%', true),
272 undefined,
273 'does not throw when allowMissing'
274 );
275
276 t.end();
277 });
278 */