blob: 5f41e137d8af684f171ca0f08b5a96838c45bf6f [file] [log] [blame]
Leo Repp58b9f112021-11-22 11:57:47 +01001'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('es-abstract/helpers/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 forEach(v.nonStrings, function (nonString) {
46 t['throws'](
47 function () { GetIntrinsic(nonString); },
48 TypeError,
49 debug(nonString) + ' is not a String'
50 );
51 });
52
53 forEach(v.nonBooleans, function (nonBoolean) {
54 t['throws'](
55 function () { GetIntrinsic('%', nonBoolean); },
56 TypeError,
57 debug(nonBoolean) + ' is not a Boolean'
58 );
59 });
60
61 forEach([
62 'toString',
63 'propertyIsEnumerable',
64 'hasOwnProperty'
65 ], function (objectProtoMember) {
66 t['throws'](
67 function () { GetIntrinsic(objectProtoMember); },
68 SyntaxError,
69 debug(objectProtoMember) + ' is not an intrinsic'
70 );
71 });
72
73 t.end();
74});
75
76test('base intrinsics', function (t) {
77 t.equal(GetIntrinsic('%Object%'), Object, '%Object% yields Object');
78 t.equal(GetIntrinsic('Object'), Object, 'Object yields Object');
79 t.equal(GetIntrinsic('%Array%'), Array, '%Array% yields Array');
80 t.equal(GetIntrinsic('Array'), Array, 'Array yields Array');
81
82 t.end();
83});
84
85test('dotted paths', function (t) {
86 t.equal(GetIntrinsic('%Object.prototype.toString%'), Object.prototype.toString, '%Object.prototype.toString% yields Object.prototype.toString');
87 t.equal(GetIntrinsic('Object.prototype.toString'), Object.prototype.toString, 'Object.prototype.toString yields Object.prototype.toString');
88 t.equal(GetIntrinsic('%Array.prototype.push%'), Array.prototype.push, '%Array.prototype.push% yields Array.prototype.push');
89 t.equal(GetIntrinsic('Array.prototype.push'), Array.prototype.push, 'Array.prototype.push yields Array.prototype.push');
90
91 test('underscore paths are aliases for dotted paths', { skip: !Object.isFrozen || Object.isFrozen(Object.prototype) }, function (st) {
92 var original = GetIntrinsic('%ObjProto_toString%');
93
94 forEach([
95 '%Object.prototype.toString%',
96 'Object.prototype.toString',
97 '%ObjectPrototype.toString%',
98 'ObjectPrototype.toString',
99 '%ObjProto_toString%',
100 'ObjProto_toString'
101 ], function (name) {
102 defineProperty(Object.prototype, 'toString', {
103 value: function toString() {
104 return original.apply(this, arguments);
105 }
106 });
107 st.equal(GetIntrinsic(name), original, name + ' yields original Object.prototype.toString');
108 });
109
110 defineProperty(Object.prototype, 'toString', { value: original });
111 st.end();
112 });
113
114 test('dotted paths cache', { skip: !Object.isFrozen || Object.isFrozen(Object.prototype) }, function (st) {
115 var original = GetIntrinsic('%Object.prototype.propertyIsEnumerable%');
116
117 forEach([
118 '%Object.prototype.propertyIsEnumerable%',
119 'Object.prototype.propertyIsEnumerable',
120 '%ObjectPrototype.propertyIsEnumerable%',
121 'ObjectPrototype.propertyIsEnumerable'
122 ], function (name) {
123 // eslint-disable-next-line no-extend-native
124 Object.prototype.propertyIsEnumerable = function propertyIsEnumerable() {
125 return original.apply(this, arguments);
126 };
127 st.equal(GetIntrinsic(name), original, name + ' yields cached Object.prototype.propertyIsEnumerable');
128 });
129
130 // eslint-disable-next-line no-extend-native
131 Object.prototype.propertyIsEnumerable = original;
132 st.end();
133 });
134
135 test('dotted path reports correct error', function (st) {
136 st['throws'](function () {
137 GetIntrinsic('%NonExistentIntrinsic.prototype.property%');
138 }, /%NonExistentIntrinsic%/, 'The base intrinsic of %NonExistentIntrinsic.prototype.property% is %NonExistentIntrinsic%');
139
140 st['throws'](function () {
141 GetIntrinsic('%NonExistentIntrinsicPrototype.property%');
142 }, /%NonExistentIntrinsicPrototype%/, 'The base intrinsic of %NonExistentIntrinsicPrototype.property% is %NonExistentIntrinsicPrototype%');
143
144 st.end();
145 });
146
147 t.end();
148});
149
150test('accessors', { skip: !$gOPD || typeof Map !== 'function' }, function (t) {
151 var actual = $gOPD(Map.prototype, 'size');
152 t.ok(actual, 'Map.prototype.size has a descriptor');
153 t.equal(typeof actual.get, 'function', 'Map.prototype.size has a getter function');
154 t.equal(GetIntrinsic('%Map.prototype.size%'), actual.get, '%Map.prototype.size% yields the getter for it');
155 t.equal(GetIntrinsic('Map.prototype.size'), actual.get, 'Map.prototype.size yields the getter for it');
156
157 t.end();
158});
159
160test('generator functions', { skip: !generatorFns.length }, function (t) {
161 var $GeneratorFunction = GetIntrinsic('%GeneratorFunction%');
162 var $GeneratorFunctionPrototype = GetIntrinsic('%Generator%');
163 var $GeneratorPrototype = GetIntrinsic('%GeneratorPrototype%');
164
165 forEach(generatorFns, function (genFn) {
166 var fnName = genFn.name;
167 fnName = fnName ? "'" + fnName + "'" : 'genFn';
168
169 t.ok(genFn instanceof $GeneratorFunction, fnName + ' instanceof %GeneratorFunction%');
170 t.ok($isProto($GeneratorFunctionPrototype, genFn), '%Generator% is prototype of ' + fnName);
171 t.ok($isProto($GeneratorPrototype, genFn.prototype), '%GeneratorPrototype% is prototype of ' + fnName + '.prototype');
172 });
173
174 t.end();
175});
176
177test('async functions', { skip: !asyncFns.length }, function (t) {
178 var $AsyncFunction = GetIntrinsic('%AsyncFunction%');
179 var $AsyncFunctionPrototype = GetIntrinsic('%AsyncFunctionPrototype%');
180
181 forEach(asyncFns, function (asyncFn) {
182 var fnName = asyncFn.name;
183 fnName = fnName ? "'" + fnName + "'" : 'asyncFn';
184
185 t.ok(asyncFn instanceof $AsyncFunction, fnName + ' instanceof %AsyncFunction%');
186 t.ok($isProto($AsyncFunctionPrototype, asyncFn), '%AsyncFunctionPrototype% is prototype of ' + fnName);
187 });
188
189 t.end();
190});
191
192test('async generator functions', { skip: !asyncGenFns.length }, function (t) {
193 var $AsyncGeneratorFunction = GetIntrinsic('%AsyncGeneratorFunction%');
194 var $AsyncGeneratorFunctionPrototype = GetIntrinsic('%AsyncGenerator%');
195 var $AsyncGeneratorPrototype = GetIntrinsic('%AsyncGeneratorPrototype%');
196
197 forEach(asyncGenFns, function (asyncGenFn) {
198 var fnName = asyncGenFn.name;
199 fnName = fnName ? "'" + fnName + "'" : 'asyncGenFn';
200
201 t.ok(asyncGenFn instanceof $AsyncGeneratorFunction, fnName + ' instanceof %AsyncGeneratorFunction%');
202 t.ok($isProto($AsyncGeneratorFunctionPrototype, asyncGenFn), '%AsyncGenerator% is prototype of ' + fnName);
203 t.ok($isProto($AsyncGeneratorPrototype, asyncGenFn.prototype), '%AsyncGeneratorPrototype% is prototype of ' + fnName + '.prototype');
204 });
205
206 t.end();
207});