source: trip-planner-front/node_modules/get-intrinsic/test/GetIntrinsic.js@ 8d391a1

Last change on this file since 8d391a1 was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

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