GetIntrinsic.js 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. 'use strict';
  2. var GetIntrinsic = require('get-intrinsic');
  3. var test = require('tape');
  4. var forEach = require('foreach');
  5. var debug = require('object-inspect');
  6. var generatorFns = require('make-generator-function')();
  7. var asyncFns = require('make-async-function').list();
  8. var asyncGenFns = require('make-async-generator-function')();
  9. var callBound = require('call-bind/callBound');
  10. var v = require('es-value-fixtures');
  11. var $gOPD = require('../helpers/getOwnPropertyDescriptor');
  12. var defineProperty = require('./helpers/defineProperty');
  13. var $isProto = callBound('%Object.prototype.isPrototypeOf%');
  14. test('export', function (t) {
  15. t.equal(typeof GetIntrinsic, 'function', 'it is a function');
  16. t.equal(GetIntrinsic.length, 2, 'function has length of 2');
  17. t.end();
  18. });
  19. test('throws', function (t) {
  20. t['throws'](
  21. function () { GetIntrinsic('not an intrinsic'); },
  22. SyntaxError,
  23. 'nonexistent intrinsic throws a syntax error'
  24. );
  25. t['throws'](
  26. function () { GetIntrinsic(''); },
  27. TypeError,
  28. 'empty string intrinsic throws a type error'
  29. );
  30. t['throws'](
  31. function () { GetIntrinsic('.'); },
  32. SyntaxError,
  33. '"just a dot" intrinsic throws a syntax error'
  34. );
  35. forEach(v.nonStrings, function (nonString) {
  36. t['throws'](
  37. function () { GetIntrinsic(nonString); },
  38. TypeError,
  39. debug(nonString) + ' is not a String'
  40. );
  41. });
  42. forEach(v.nonBooleans, function (nonBoolean) {
  43. t['throws'](
  44. function () { GetIntrinsic('%', nonBoolean); },
  45. TypeError,
  46. debug(nonBoolean) + ' is not a Boolean'
  47. );
  48. });
  49. forEach([
  50. 'toString',
  51. 'propertyIsEnumerable',
  52. 'hasOwnProperty'
  53. ], function (objectProtoMember) {
  54. t['throws'](
  55. function () { GetIntrinsic(objectProtoMember); },
  56. SyntaxError,
  57. debug(objectProtoMember) + ' is not an intrinsic'
  58. );
  59. });
  60. t.end();
  61. });
  62. test('base intrinsics', function (t) {
  63. t.equal(GetIntrinsic('%Object%'), Object, '%Object% yields Object');
  64. t.equal(GetIntrinsic('Object'), Object, 'Object yields Object');
  65. t.equal(GetIntrinsic('%Array%'), Array, '%Array% yields Array');
  66. t.equal(GetIntrinsic('Array'), Array, 'Array yields Array');
  67. t.end();
  68. });
  69. test('dotted paths', function (t) {
  70. t.equal(GetIntrinsic('%Object.prototype.toString%'), Object.prototype.toString, '%Object.prototype.toString% yields Object.prototype.toString');
  71. t.equal(GetIntrinsic('Object.prototype.toString'), Object.prototype.toString, 'Object.prototype.toString yields Object.prototype.toString');
  72. t.equal(GetIntrinsic('%Array.prototype.push%'), Array.prototype.push, '%Array.prototype.push% yields Array.prototype.push');
  73. t.equal(GetIntrinsic('Array.prototype.push'), Array.prototype.push, 'Array.prototype.push yields Array.prototype.push');
  74. test('underscore paths are aliases for dotted paths', { skip: !Object.isFrozen || Object.isFrozen(Object.prototype) }, function (st) {
  75. var original = GetIntrinsic('%ObjProto_toString%');
  76. forEach([
  77. '%Object.prototype.toString%',
  78. 'Object.prototype.toString',
  79. '%ObjectPrototype.toString%',
  80. 'ObjectPrototype.toString',
  81. '%ObjProto_toString%',
  82. 'ObjProto_toString'
  83. ], function (name) {
  84. defineProperty(Object.prototype, 'toString', {
  85. value: function toString() {
  86. return original.apply(this, arguments);
  87. }
  88. });
  89. st.equal(GetIntrinsic(name), original, name + ' yields original Object.prototype.toString');
  90. });
  91. defineProperty(Object.prototype, 'toString', { value: original });
  92. st.end();
  93. });
  94. test('dotted paths cache', { skip: !Object.isFrozen || Object.isFrozen(Object.prototype) }, function (st) {
  95. var original = GetIntrinsic('%Object.prototype.propertyIsEnumerable%');
  96. forEach([
  97. '%Object.prototype.propertyIsEnumerable%',
  98. 'Object.prototype.propertyIsEnumerable',
  99. '%ObjectPrototype.propertyIsEnumerable%',
  100. 'ObjectPrototype.propertyIsEnumerable'
  101. ], function (name) {
  102. // eslint-disable-next-line no-extend-native
  103. Object.prototype.propertyIsEnumerable = function propertyIsEnumerable() {
  104. return original.apply(this, arguments);
  105. };
  106. st.equal(GetIntrinsic(name), original, name + ' yields cached Object.prototype.propertyIsEnumerable');
  107. });
  108. // eslint-disable-next-line no-extend-native
  109. Object.prototype.propertyIsEnumerable = original;
  110. st.end();
  111. });
  112. test('dotted path reports correct error', function (st) {
  113. st['throws'](function () {
  114. GetIntrinsic('%NonExistentIntrinsic.prototype.property%');
  115. }, /%NonExistentIntrinsic%/, 'The base intrinsic of %NonExistentIntrinsic.prototype.property% is %NonExistentIntrinsic%');
  116. st['throws'](function () {
  117. GetIntrinsic('%NonExistentIntrinsicPrototype.property%');
  118. }, /%NonExistentIntrinsicPrototype%/, 'The base intrinsic of %NonExistentIntrinsicPrototype.property% is %NonExistentIntrinsicPrototype%');
  119. st.end();
  120. });
  121. t.end();
  122. });
  123. test('accessors', { skip: !$gOPD || typeof Map !== 'function' }, function (t) {
  124. var actual = $gOPD(Map.prototype, 'size');
  125. t.ok(actual, 'Map.prototype.size has a descriptor');
  126. t.equal(typeof actual.get, 'function', 'Map.prototype.size has a getter function');
  127. t.equal(GetIntrinsic('%Map.prototype.size%'), actual.get, '%Map.prototype.size% yields the getter for it');
  128. t.equal(GetIntrinsic('Map.prototype.size'), actual.get, 'Map.prototype.size yields the getter for it');
  129. t.end();
  130. });
  131. test('generator functions', { skip: !generatorFns.length }, function (t) {
  132. var $GeneratorFunction = GetIntrinsic('%GeneratorFunction%');
  133. var $GeneratorFunctionPrototype = GetIntrinsic('%Generator%');
  134. var $GeneratorPrototype = GetIntrinsic('%GeneratorPrototype%');
  135. forEach(generatorFns, function (genFn) {
  136. var fnName = genFn.name;
  137. fnName = fnName ? "'" + fnName + "'" : 'genFn';
  138. t.ok(genFn instanceof $GeneratorFunction, fnName + ' instanceof %GeneratorFunction%');
  139. t.ok($isProto($GeneratorFunctionPrototype, genFn), '%Generator% is prototype of ' + fnName);
  140. t.ok($isProto($GeneratorPrototype, genFn.prototype), '%GeneratorPrototype% is prototype of ' + fnName + '.prototype');
  141. });
  142. t.end();
  143. });
  144. test('async functions', { skip: !asyncFns.length }, function (t) {
  145. var $AsyncFunction = GetIntrinsic('%AsyncFunction%');
  146. var $AsyncFunctionPrototype = GetIntrinsic('%AsyncFunctionPrototype%');
  147. forEach(asyncFns, function (asyncFn) {
  148. var fnName = asyncFn.name;
  149. fnName = fnName ? "'" + fnName + "'" : 'asyncFn';
  150. t.ok(asyncFn instanceof $AsyncFunction, fnName + ' instanceof %AsyncFunction%');
  151. t.ok($isProto($AsyncFunctionPrototype, asyncFn), '%AsyncFunctionPrototype% is prototype of ' + fnName);
  152. });
  153. t.end();
  154. });
  155. test('async generator functions', { skip: !asyncGenFns.length }, function (t) {
  156. var $AsyncGeneratorFunction = GetIntrinsic('%AsyncGeneratorFunction%');
  157. var $AsyncGeneratorFunctionPrototype = GetIntrinsic('%AsyncGenerator%');
  158. var $AsyncGeneratorPrototype = GetIntrinsic('%AsyncGeneratorPrototype%');
  159. forEach(asyncGenFns, function (asyncGenFn) {
  160. var fnName = asyncGenFn.name;
  161. fnName = fnName ? "'" + fnName + "'" : 'asyncGenFn';
  162. t.ok(asyncGenFn instanceof $AsyncGeneratorFunction, fnName + ' instanceof %AsyncGeneratorFunction%');
  163. t.ok($isProto($AsyncGeneratorFunctionPrototype, asyncGenFn), '%AsyncGenerator% is prototype of ' + fnName);
  164. t.ok($isProto($AsyncGeneratorPrototype, asyncGenFn.prototype), '%AsyncGeneratorPrototype% is prototype of ' + fnName + '.prototype');
  165. });
  166. t.end();
  167. });