|
| 1 | +/* |
| 2 | + * instanceof is a more complete check then .constructor === |
| 3 | + * |
| 4 | + * It works when using node's built-in util.inherits function |
| 5 | + * and it also honors a class's entire ancestry |
| 6 | + * |
| 7 | + * Here I am only testing the change to vows in suite.js at line 147 to change |
| 8 | + * the check from .constructor === to instanceof. These tests should demonstrate |
| 9 | + * that this change should work both cases. For completness I also check |
| 10 | + * the case when EventEmitter is an ancestor, not the parent Class. |
| 11 | + * |
| 12 | + */ |
| 13 | +var EventEmitter = process.EventEmitter, |
| 14 | + util = require('util'), |
| 15 | + vows = require('vows'), |
| 16 | + assert = require('assert'); |
| 17 | + |
| 18 | +vows.describe('EventEmitters as a return value from a topic').addBatch({ |
| 19 | + 'returning an EventEmitter' : { |
| 20 | + topic : function () { |
| 21 | + //Make an event emitter |
| 22 | + var tmp = new EventEmitter(); |
| 23 | + //set it to emit success in a bit |
| 24 | + setTimeout(function () { |
| 25 | + //pass a value to make sure this all works |
| 26 | + tmp.emit('success', 'I work'); |
| 27 | + }, 10); |
| 28 | + |
| 29 | + return tmp; |
| 30 | + }, |
| 31 | + 'will catch what I pass to success' : function (ret) { |
| 32 | + assert.strictEqual(ret, 'I work'); |
| 33 | + } |
| 34 | + }, |
| 35 | + 'returning a class that uses util.inherit to inherit from EventEmitter' : { |
| 36 | + topic : function () { |
| 37 | + //Make a class that will util.inherit from EventEmitter |
| 38 | + var Class = function () { |
| 39 | + EventEmitter.call(this); |
| 40 | + }, |
| 41 | + tmp; |
| 42 | + |
| 43 | + //inherit from EventEmitter |
| 44 | + util.inherits(Class, EventEmitter); |
| 45 | + //Get a new one |
| 46 | + tmp = new Class(); |
| 47 | + //set it to emit success in a bit |
| 48 | + setTimeout(function () { |
| 49 | + //pass a value to make sure this all works |
| 50 | + tmp.emit('success', 'I work'); |
| 51 | + }, 10); |
| 52 | + |
| 53 | + return tmp; |
| 54 | + }, |
| 55 | + 'will catch what I pass to success' : function (ret) { |
| 56 | + assert.strictEqual(ret, 'I work'); |
| 57 | + } |
| 58 | + }, |
| 59 | + 'returning a class that uses Class.prototype = new EventEmitter()' : { |
| 60 | + topic : function () { |
| 61 | + //Make a class that will inherit from EventEmitter |
| 62 | + var Class = function () {}, tmp; |
| 63 | + //inherit |
| 64 | + Class.prototype = new EventEmitter(); |
| 65 | + //Get a new one |
| 66 | + tmp = new Class(); |
| 67 | + //set it to emit success in a bit |
| 68 | + setTimeout(function () { |
| 69 | + //pass a value to make sure this all works |
| 70 | + tmp.emit('success', 'I work'); |
| 71 | + }, 10); |
| 72 | + |
| 73 | + return tmp; |
| 74 | + }, |
| 75 | + 'will catch what I pass to success' : function (ret) { |
| 76 | + assert.strictEqual(ret, 'I work'); |
| 77 | + } |
| 78 | + }, |
| 79 | + 'returning a class that uses util.inherit to inherit from a class that inherits from EventEmitter ' : { |
| 80 | + topic : function () { |
| 81 | + //Class1 inherits from EventEmitter |
| 82 | + var Class1 = function () { |
| 83 | + var self = this; |
| 84 | + EventEmitter.call(self); |
| 85 | + }, |
| 86 | + //Class2 inherits from Class1 |
| 87 | + Class2 = function () { |
| 88 | + Class1.call(this); |
| 89 | + }, tmp; |
| 90 | + //Inherit |
| 91 | + util.inherits(Class1, EventEmitter); |
| 92 | + util.inherits(Class2, Class1); |
| 93 | + //Get a new one |
| 94 | + tmp = new Class2(); |
| 95 | + //set it to emit success in a bit |
| 96 | + setTimeout(function () { |
| 97 | + //pass a value to make sure this all works |
| 98 | + tmp.emit('success', 'I work'); |
| 99 | + },10); |
| 100 | + |
| 101 | + return tmp; |
| 102 | + }, |
| 103 | + 'will catch what I pass to success' : function (ret) { |
| 104 | + assert.strictEqual(ret, 'I work'); |
| 105 | + } |
| 106 | + }, |
| 107 | + 'returning a class that uses Class2.prototype = new Class1() and Class1.prototype = new EventEmitter()' : { |
| 108 | + topic : function () { |
| 109 | + //Class1 will inherit from EventEmitter |
| 110 | + var Class1 = function () {}, |
| 111 | + //Class2 will inherit from Class1 |
| 112 | + Class2 = function () {}, tmp; |
| 113 | + //Inherit |
| 114 | + Class1.prototype = new EventEmitter(); |
| 115 | + Class2.prototype = new Class1(); |
| 116 | + //Get a new one |
| 117 | + tmp = new Class2(); |
| 118 | + //seit it to emit success in a bit |
| 119 | + setTimeout(function () { |
| 120 | + //pass a value to make sure this all works |
| 121 | + tmp.emit('success', 'I work'); |
| 122 | + },10); |
| 123 | + |
| 124 | + return tmp; |
| 125 | + }, |
| 126 | + 'will catch what I pass to success' : function (ret) { |
| 127 | + assert.strictEqual(ret, 'I work'); |
| 128 | + } |
| 129 | + } |
| 130 | +}).export(module); |
| 131 | + |
| 132 | + |
| 133 | + |
0 commit comments