index.js 2.6 KB

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