index.js 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. 'use strict';
  2. /* globals window */
  3. var test = require('tape');
  4. var generatorFuncs = require('make-generator-function')();
  5. var hasToStringTag = require('has-tostringtag/shams')();
  6. var forEach = require('for-each');
  7. var isGeneratorFunction = require('../index');
  8. test('returns false for non-functions', function (t) {
  9. var nonFuncs = [
  10. true,
  11. false,
  12. null,
  13. undefined,
  14. {},
  15. [],
  16. /a/g,
  17. 'string',
  18. 42,
  19. new Date()
  20. ];
  21. t.plan(nonFuncs.length);
  22. forEach(nonFuncs, function (nonFunc) {
  23. t.notOk(isGeneratorFunction(nonFunc), nonFunc + ' is not a function');
  24. });
  25. t.end();
  26. });
  27. test('returns false for non-generator functions', function (t) {
  28. var func = function () {};
  29. t.notOk(isGeneratorFunction(func), 'anonymous function is not an generator function');
  30. var namedFunc = function foo() {};
  31. t.notOk(isGeneratorFunction(namedFunc), 'named function is not an generator function');
  32. if (typeof window === 'undefined') {
  33. t.skip('window.alert is not an generator function');
  34. } else {
  35. t.notOk(isGeneratorFunction(window.alert), 'window.alert is not an generator function');
  36. }
  37. t.end();
  38. });
  39. var fakeToString = function () { return 'function* () { return "TOTALLY REAL I SWEAR!"; }'; };
  40. test('returns false for non-generator function with faked toString', function (t) {
  41. var func = function () {};
  42. func.toString = fakeToString;
  43. t.notEqual(String(func), Function.prototype.toString.apply(func), 'faked toString is not real toString');
  44. t.notOk(isGeneratorFunction(func), 'anonymous function with faked toString is not a generator function');
  45. t.end();
  46. });
  47. test('returns false for non-generator function with faked @@toStringTag', { skip: !hasToStringTag || generatorFuncs.length === 0 }, function (t) {
  48. var generatorFunc = generatorFuncs[0];
  49. /** @type {{ toString(): unknown; valueOf(): unknown; [Symbol.toStringTag]?: unknown; }} */
  50. var fakeGenFunction = {
  51. toString: function () { return String(generatorFunc); },
  52. valueOf: function () { return generatorFunc; }
  53. };
  54. fakeGenFunction[Symbol.toStringTag] = 'GeneratorFunction';
  55. t.notOk(isGeneratorFunction(fakeGenFunction), 'fake GeneratorFunction with @@toStringTag "GeneratorFunction" is not a generator function');
  56. t.end();
  57. });
  58. test('returns true for generator functions', function (t) {
  59. if (generatorFuncs.length > 0) {
  60. forEach(generatorFuncs, function (generatorFunc) {
  61. t.ok(isGeneratorFunction(generatorFunc), generatorFunc + ' is generator function');
  62. });
  63. } else {
  64. t.skip('generator function is generator function - this environment does not support ES6 generator functions. Please run `node --harmony`, or use a supporting browser.');
  65. }
  66. t.end();
  67. });