88164fd763444979d1c2e8f061e88bfbb8ed669849f82bc1fd89f2a0524d5fd84691cfb6d7936035c2d01344c5758824ebe44929f627a93edd7ac63e06be26 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. 'use strict';
  2. var functionsHaveNames = require('functions-have-names')();
  3. var arrows = require('make-arrow-function').list();
  4. var generators = require('make-generator-function')();
  5. var asyncs = require('make-async-function').list();
  6. var IsCallable = require('is-callable');
  7. var forEach = require('for-each');
  8. var foo = Object(function foo() {});
  9. var anon = Object(function () {});
  10. var evalled = Object(Function()); // eslint-disable-line no-new-func
  11. module.exports = function (getName, t) {
  12. t.test('functions', function (st) {
  13. if (functionsHaveNames) {
  14. st.equal(getName(foo), foo.name, 'foo has name "foo"');
  15. st.equal(getName(anon), anon.name, 'anonymous function has name of empty string');
  16. st.equal(getName(evalled), evalled.name, 'eval-d function has name "anonymous" (or empty string)');
  17. }
  18. st.equal(getName(foo), 'foo', 'foo has name "foo"');
  19. st.equal(getName(anon), '', 'anonymous function has name of empty string');
  20. var evalledName = getName(evalled);
  21. st.equal(evalledName === 'anonymous' || evalledName === '', true, 'eval-d function has name "anonymous" (or empty string');
  22. st.end();
  23. });
  24. t.test('arrow functions', { skip: arrows.length === 0 }, function (st) {
  25. st.equal(true, functionsHaveNames, 'functions have names in any env with arrow functions');
  26. forEach(arrows, function (arrowFn) {
  27. st.equal(getName(arrowFn), arrowFn.name, 'arrow function name matches for ' + arrowFn);
  28. });
  29. st.end();
  30. });
  31. t.test('generators', { skip: generators.length === 0 }, function (st) {
  32. st.equal(true, functionsHaveNames, 'functions have names in any env with generator functions');
  33. forEach(generators, function (genFn) {
  34. st.equal(getName(genFn), genFn.name, 'generator function name matches for ' + genFn);
  35. });
  36. st.end();
  37. });
  38. t.test('asyncs', { skip: asyncs.length === 0 }, function (st) {
  39. st.equal(true, functionsHaveNames, 'functions have names in any env with async functions');
  40. forEach(asyncs, function (asyncFn) {
  41. st.equal(getName(asyncFn), asyncFn.name, 'async function name matches for ' + asyncFn);
  42. });
  43. st.end();
  44. });
  45. t.test('Function.prototype.name', function (st) {
  46. st.equal(getName(function before() {}), 'before', 'function prior to accessing Function.prototype has the right name');
  47. var protoName = getName(Function.prototype);
  48. // on <= node v2.5, this is "Empty"; on Opera 12.1, "Function.prototype" - otherwise, the empty string
  49. st.equal(protoName === '' || protoName === 'Empty' || protoName === 'Function.prototype', true, 'Function.prototype has the right name');
  50. st.equal(getName(function after() {}), 'after', 'function after accessing Function.prototype has the right name');
  51. st.end();
  52. });
  53. t.test('DOM', function (st) {
  54. /* eslint-env browser */
  55. st.test('document.all', { skip: typeof document !== 'object' }, function (s2t) {
  56. s2t['throws'](
  57. function () { getName(document.all); },
  58. TypeError,
  59. 'a document.all has no name'
  60. );
  61. s2t.end();
  62. });
  63. forEach([
  64. 'HTMLElement',
  65. 'HTMLAnchorElement'
  66. ], function (name) {
  67. var constructor = global[name];
  68. st.test(name, { skip: !constructor }, function (s2t) {
  69. s2t.match(typeof constructor, /^(?:function|object)$/, name + ' is a function or an object');
  70. if (IsCallable(constructor)) {
  71. try {
  72. s2t.equal(getName(constructor), name, name + ' has the right name');
  73. } catch (e) {
  74. s2t.fail(e);
  75. }
  76. } else {
  77. s2t['throws'](
  78. function () { getName(constructor); },
  79. TypeError,
  80. name + ' is not callable'
  81. );
  82. }
  83. s2t.end();
  84. });
  85. });
  86. st.end();
  87. });
  88. };