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