index.js 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. 'use strict';
  2. var test = require('tape');
  3. var hasNames = require('../');
  4. test('named functions', function (t) {
  5. function f() {} // eslint-disable-line func-style
  6. var g = function h() {};
  7. t.equal(typeof hasNames, 'function', 'is a function');
  8. t.equal(hasNames(), f.name === 'f' && g.name === 'h', 'functions have names or not as expected');
  9. t.end();
  10. });
  11. var oDP = Object.defineProperty;
  12. if (oDP) {
  13. try {
  14. oDP({}, 'a', { value: 1 });
  15. } catch (e) {
  16. oDP = null;
  17. }
  18. }
  19. test('functionsHaveConfigurableNames', function (t) {
  20. t.equal(typeof hasNames.functionsHaveConfigurableNames, 'function', 'is a function');
  21. if (hasNames()) {
  22. var fn = function f() {};
  23. if (oDP) {
  24. try {
  25. oDP(fn, 'name', { configurable: true, value: 'foo' });
  26. } catch (e) {}
  27. if (fn.name === 'f') {
  28. t.equal(hasNames.functionsHaveConfigurableNames(), false, 'function names are not configurable');
  29. } else if (fn.name === 'foo') {
  30. t.equal(hasNames.functionsHaveConfigurableNames(), true, 'function names are not configurable');
  31. } else {
  32. t.fail('functions have names, but something surprising has happened. Please report this!');
  33. }
  34. } else {
  35. t.equal(hasNames.functionsHaveConfigurableNames(), false, 'function names are not configurable');
  36. }
  37. } else {
  38. t.equal(hasNames.functionsHaveConfigurableNames(), false, 'functions do not have names');
  39. }
  40. t.end();
  41. });
  42. test('boundFunctionsHaveNames', function (t) {
  43. t.equal(typeof hasNames.boundFunctionsHaveNames, 'function', 'is a function');
  44. var fn = function f() {};
  45. if (typeof fn.bind !== 'function') {
  46. t.equal(hasNames.boundFunctionsHaveNames(), false, 'bound functions do not have names, because .bind does not exist');
  47. } else if (hasNames()) {
  48. t.equal(hasNames.boundFunctionsHaveNames(), fn.bind().name !== '', 'bound functions have names');
  49. } else {
  50. t.equal(hasNames.boundFunctionsHaveNames(), false, 'bound functions do not have names, because none do');
  51. }
  52. t.end();
  53. });