fn.js 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. var inspect = require('../');
  2. var test = require('tape');
  3. var arrow = require('make-arrow-function')();
  4. var functionsHaveConfigurableNames = require('functions-have-names').functionsHaveConfigurableNames();
  5. test('function', function (t) {
  6. t.plan(1);
  7. var obj = [1, 2, function f(n) { return n; }, 4];
  8. t.equal(inspect(obj), '[ 1, 2, [Function: f], 4 ]');
  9. });
  10. test('function name', function (t) {
  11. t.plan(1);
  12. var f = (function () {
  13. return function () {};
  14. }());
  15. f.toString = function toStr() { return 'function xxx () {}'; };
  16. var obj = [1, 2, f, 4];
  17. t.equal(inspect(obj), '[ 1, 2, [Function (anonymous)] { toString: [Function: toStr] }, 4 ]');
  18. });
  19. test('anon function', function (t) {
  20. var f = (function () {
  21. return function () {};
  22. }());
  23. var obj = [1, 2, f, 4];
  24. t.equal(inspect(obj), '[ 1, 2, [Function (anonymous)], 4 ]');
  25. t.end();
  26. });
  27. test('arrow function', { skip: !arrow }, function (t) {
  28. t.equal(inspect(arrow), '[Function (anonymous)]');
  29. t.end();
  30. });
  31. test('truly nameless function', { skip: !arrow || !functionsHaveConfigurableNames }, function (t) {
  32. function f() {}
  33. Object.defineProperty(f, 'name', { value: false });
  34. t.equal(f.name, false);
  35. t.equal(
  36. inspect(f),
  37. '[Function: f]',
  38. 'named function with falsy `.name` does not hide its original name'
  39. );
  40. function g() {}
  41. Object.defineProperty(g, 'name', { value: true });
  42. t.equal(g.name, true);
  43. t.equal(
  44. inspect(g),
  45. '[Function: true]',
  46. 'named function with truthy `.name` hides its original name'
  47. );
  48. var anon = function () {}; // eslint-disable-line func-style
  49. Object.defineProperty(anon, 'name', { value: null });
  50. t.equal(anon.name, null);
  51. t.equal(
  52. inspect(anon),
  53. '[Function (anonymous)]',
  54. 'anon function with falsy `.name` does not hide its anonymity'
  55. );
  56. var anon2 = function () {}; // eslint-disable-line func-style
  57. Object.defineProperty(anon2, 'name', { value: 1 });
  58. t.equal(anon2.name, 1);
  59. t.equal(
  60. inspect(anon2),
  61. '[Function: 1]',
  62. 'anon function with truthy `.name` hides its anonymity'
  63. );
  64. t.end();
  65. });