[d565449] | 1 | 'use strict';
|
---|
| 2 |
|
---|
| 3 | /* globals window */
|
---|
| 4 |
|
---|
| 5 | var test = require('tape');
|
---|
[79a0317] | 6 |
|
---|
[d565449] | 7 | var generatorFuncs = require('make-generator-function')();
|
---|
| 8 | var hasToStringTag = require('has-tostringtag/shams')();
|
---|
[79a0317] | 9 | var forEach = require('for-each');
|
---|
[d565449] | 10 |
|
---|
[79a0317] | 11 | var isGeneratorFunction = require('../index');
|
---|
[d565449] | 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 |
|
---|
| 33 | test('returns false for non-generator functions', function (t) {
|
---|
| 34 | var func = function () {};
|
---|
| 35 | t.notOk(isGeneratorFunction(func), 'anonymous function is not an generator function');
|
---|
| 36 |
|
---|
| 37 | var namedFunc = function foo() {};
|
---|
| 38 | t.notOk(isGeneratorFunction(namedFunc), 'named function is not an generator function');
|
---|
| 39 |
|
---|
| 40 | if (typeof window === 'undefined') {
|
---|
| 41 | t.skip('window.alert is not an generator function');
|
---|
| 42 | } else {
|
---|
| 43 | t.notOk(isGeneratorFunction(window.alert), 'window.alert is not an generator function');
|
---|
| 44 | }
|
---|
| 45 | t.end();
|
---|
| 46 | });
|
---|
| 47 |
|
---|
| 48 | var fakeToString = function () { return 'function* () { return "TOTALLY REAL I SWEAR!"; }'; };
|
---|
| 49 |
|
---|
| 50 | test('returns false for non-generator function with faked toString', function (t) {
|
---|
| 51 | var func = function () {};
|
---|
| 52 | func.toString = fakeToString;
|
---|
| 53 |
|
---|
| 54 | t.notEqual(String(func), Function.prototype.toString.apply(func), 'faked toString is not real toString');
|
---|
| 55 | t.notOk(isGeneratorFunction(func), 'anonymous function with faked toString is not a generator function');
|
---|
| 56 | t.end();
|
---|
| 57 | });
|
---|
| 58 |
|
---|
| 59 | test('returns false for non-generator function with faked @@toStringTag', { skip: !hasToStringTag || generatorFuncs.length === 0 }, function (t) {
|
---|
| 60 | var generatorFunc = generatorFuncs[0];
|
---|
[79a0317] | 61 | /** @type {{ toString(): unknown; valueOf(): unknown; [Symbol.toStringTag]?: unknown; }} */
|
---|
[d565449] | 62 | var fakeGenFunction = {
|
---|
| 63 | toString: function () { return String(generatorFunc); },
|
---|
| 64 | valueOf: function () { return generatorFunc; }
|
---|
| 65 | };
|
---|
| 66 | fakeGenFunction[Symbol.toStringTag] = 'GeneratorFunction';
|
---|
| 67 | t.notOk(isGeneratorFunction(fakeGenFunction), 'fake GeneratorFunction with @@toStringTag "GeneratorFunction" is not a generator function');
|
---|
| 68 | t.end();
|
---|
| 69 | });
|
---|
| 70 |
|
---|
| 71 | test('returns true for generator functions', function (t) {
|
---|
| 72 | if (generatorFuncs.length > 0) {
|
---|
| 73 | forEach(generatorFuncs, function (generatorFunc) {
|
---|
| 74 | t.ok(isGeneratorFunction(generatorFunc), generatorFunc + ' is generator function');
|
---|
| 75 | });
|
---|
| 76 | } else {
|
---|
| 77 | t.skip('generator function is generator function - this environment does not support ES6 generator functions. Please run `node --harmony`, or use a supporting browser.');
|
---|
| 78 | }
|
---|
| 79 | t.end();
|
---|
| 80 | });
|
---|