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