-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathsuite.js
159 lines (140 loc) · 4.08 KB
/
suite.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
// vim:ts=4:sts=4:sw=4:
require("./dsl");
var Test = require("./test");
var Expectation = require("./expectation");
var Reporter = require("./platform-reporter");
module.exports = Suite;
function Suite(name) {
this.name = name;
this.parent = null;
this.root = this; // Unless overridden
this.children = [];
this.exclusive = false;
this.beforeEach = null;
this.afterEach = null;
this.testCount = 0;
this.skip = false;
}
Suite.prototype.type = "describe";
// To be overriden if desired
Suite.prototype.Promise = null;
Suite.prototype.describe = function (callback) {
if (!callback) {
this.skip = true;
return this;
}
setCurrentSuite(this);
try {
callback();
} finally {
setCurrentSuite(this.parent);
}
return this;
};
Suite.prototype.nestSuite = function (name) {
var child = new this.constructor();
child.root = this.root;
child.parent = this;
child.name = name;
child.children = [];
child.exclusive = false;
this.children.push(child);
// Specialize the Expectation
function SuiteExpectation(value, report) {
Expectation.call(this, value, report);
}
SuiteExpectation.prototype = Object.create(this.Expectation.prototype);
child.Expectation = SuiteExpectation;
return child;
};
Suite.prototype.nestTest = function (name, callback) {
var child = new this.Test(name, callback, this, this.root);
this.root.testCount++;
child.exclusive = false;
this.children.push(child);
return child;
};
Suite.prototype.setExclusive = function () {
var child = this;
while (child) {
child.exclusive = true;
child = child.parent;
}
};
Suite.prototype.run = function (report, Promise) {
var self = this;
Promise = Promise || this.Promise;
var suiteReport = report.start(this);
return Promise.resolve().then(function () {
if (!self.skip) {
var exclusiveChildren = self.children.filter(function (child) {
return child.exclusive;
});
var children = exclusiveChildren.length ? exclusiveChildren : self.children;
return children.reduce(function (ready, child) {
return ready.then(function () {
return child.run(suiteReport, Promise);
});
}, Promise.resolve())
} else {
suiteReport.skip(self);
}
})
.then(function () {
suiteReport.end(self);
}, function (error) {
suiteReport.end(self);
throw error;
});
};
Suite.prototype.runSync = function (report) {
var suiteReport = report.start(this);
if (!this.skip) {
var exclusiveChildren = this.children.filter(function (child) {
return child.exclusive;
});
var children = exclusiveChildren.length ? exclusiveChildren : this.children;
try {
for (var index = 0; index < children.length; index++) {
var child = children[index];
child.runSync(suiteReport);
}
} finally {
suiteReport.end(this);
}
} else {
suiteReport.skip(this);
suiteReport.end(this);
}
};
Suite.prototype.runAndReport = function (options) {
options = options || {};
var report = options.report || new (options.Reporter || this.Reporter)(options);
var self = this;
if (report.enter) {
report.enter();
}
return this.run(report, options.Promise)
.then(function () {
report.summarize(self, options)
if (report.exit) {
return report.exit();
}
});
};
Suite.prototype.runAndReportSync = function (options) {
options = options || {};
var report = options.report || new (options.Reporter || this.Reporter)(options);
var self = this;
if (report.enter) {
report.enter();
}
this.runSync(report, options)
report.summarize(self, options)
if (report.exit) {
report.exit();
}
};
Suite.prototype.Test = Test;
Suite.prototype.Expectation = Expectation;
Suite.prototype.Reporter = Reporter;