test.js 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. describe('fn.name', function () {
  2. 'use strict';
  3. var assume = require('assume')
  4. , name = require('./');
  5. it('is exported as a function', function () {
  6. assume(name).is.a('function');
  7. });
  8. it('can extract the name from a function declaration', function () {
  9. function foobar() {}
  10. assume(name(foobar)).equals('foobar');
  11. });
  12. it('can extract the name from a function expression', function () {
  13. var a = function bar() {};
  14. assume(name(a)).equals('bar');
  15. });
  16. it('can be overriden using displayName', function () {
  17. var a = function bar() {};
  18. a.displayName = 'bro';
  19. assume(name(a)).equals('bro');
  20. });
  21. it('works with constructed instances', function () {
  22. function Bar(){}
  23. var foo = new Bar();
  24. assume(name(foo)).equals('Bar');
  25. });
  26. it('works with anonymous', function () {
  27. assume(name(function () {})).equals('anonymous');
  28. });
  29. it('returns the className if we were not given a function', function () {
  30. assume(name('string')).equals('String');
  31. });
  32. //
  33. // Test if the env supports async functions, if so add a test to ensure
  34. // that we will work with async functions.
  35. //
  36. var asyncfn = true;
  37. try { new Function('return async function hello() {}')(); }
  38. catch (e) { asyncfn = false; }
  39. if (asyncfn) it('detects the name of async functions', function () {
  40. var fn = new Function('return async function hello() {}')();
  41. assume(name(fn)).equals('hello');
  42. });
  43. //
  44. // Test that this env supports generators, if so add a test to ensure that
  45. // we will work with generators.
  46. //
  47. var generators = true;
  48. try { new Function('return function* generator() {}')(); }
  49. catch (e) { generator = false; }
  50. if (generators) it('detecs the name of a generator', function () {
  51. var fn = new Function('return function* hello() {}')();
  52. assume(name(fn)).equals('hello');
  53. });
  54. });