Skip to content

Commit 22d61f2

Browse files
committed
closes #94 generators support
1 parent 0542636 commit 22d61f2

7 files changed

Lines changed: 154 additions & 45 deletions

File tree

lib/child.js

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ var QUnit = require('qunitjs'),
22
path = require('path'),
33
_ = require('underscore'),
44
trace = require('tracejs').trace,
5-
coverage = require('./coverage');
5+
coverage = require('./coverage'),
6+
generators = require('./generators'),
7+
co = require('co');
68

79
// cycle.js: This file contains two functions, JSON.decycle and JSON.retrocycle,
810
// which make it possible to encode cyclical structures and dags in JSON, and to
@@ -107,6 +109,34 @@ QUnit.done(_.debounce(function(data) {
107109
});
108110
}, 1000));
109111

112+
if (generators.support) {
113+
var test = QUnit.test;
114+
115+
/**
116+
* Support generators.
117+
*/
118+
global.test = QUnit.test = function(testName, expected, callback, async) {
119+
var fn;
120+
121+
if ( arguments.length === 2 ) {
122+
callback = expected;
123+
expected = null;
124+
}
125+
126+
if (generators.isGeneratorFn(callback)) {
127+
fn = function() {
128+
stop();
129+
co(callback)();
130+
start();
131+
};
132+
} else {
133+
fn = callback;
134+
}
135+
136+
return test.call(this, testName, expected, fn, async);
137+
};
138+
}
139+
110140
/**
111141
* Provide better stack traces
112142
*/

lib/generators.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
'use strict'
2+
3+
/**
4+
* Is true when generators are supported.
5+
*/
6+
exports.support = false;
7+
8+
try {
9+
eval("(function *(){})()");
10+
exports.support = true;
11+
} catch(err) {}
12+
13+
/**
14+
* Returns true if function is a generator fn.
15+
*
16+
* @param {Function} fn
17+
* @return {Boolean}
18+
*/
19+
exports.isGeneratorFn = function(fn) {
20+
return fn.constructor.name == 'GeneratorFunction';
21+
}

package.json

Lines changed: 55 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,59 @@
11
{
2-
"name": "qunit",
3-
"description": "QUnit testing framework for nodejs",
4-
"version": "0.6.4",
5-
"author": "Oleg Slobodskoi <oleg008@gmail.com>",
6-
"contributors": [
7-
{"name": "Jonathan Buchanan"},
8-
{"name": "Ashar Voultoiz"},
9-
{"name": "Drew Fyock"}
10-
],
11-
"repository": {
12-
"type": "git",
13-
"url": "http://github.com/kof/node-qunit.git"
2+
"name": "qunit",
3+
"description": "QUnit testing framework for nodejs",
4+
"version": "0.7.0",
5+
"author": "Oleg Slobodskoi <oleg008@gmail.com>",
6+
"contributors": [
7+
{
8+
"name": "Jonathan Buchanan"
149
},
15-
"keywords": ["TDD", "QUnit", "unit", "testing", "tests", "async"],
16-
"bin": {"qunit": "./bin/cli.js"},
17-
"engines": {"node": ">=0.6.0 < 0.11.0"},
18-
"scripts": {
19-
"test": "node ./test/testrunner.js"
10+
{
11+
"name": "Ashar Voultoiz"
2012
},
21-
"dependencies": {
22-
"underscore": "^1.6.0",
23-
"argsparser": "^0.0.6",
24-
"cli-table": "^0.3.0",
25-
"tracejs": "^0.1.8",
26-
"qunitjs": "1.10.0"
27-
},
28-
"devDependencies": {
29-
"chainer": "^0.0.5",
30-
"timekeeper": "^0.0.4"
31-
},
32-
"optionalDependencies": {
33-
"istanbul": "^0.2.4"
34-
},
35-
"licenses": [
36-
{
37-
"type": "MIT",
38-
"url": "http: //www.opensource.org/licenses/mit-license.php"
39-
}
40-
]
13+
{
14+
"name": "Drew Fyock"
15+
}
16+
],
17+
"repository": {
18+
"type": "git",
19+
"url": "http://github.com/kof/node-qunit.git"
20+
},
21+
"keywords": [
22+
"TDD",
23+
"QUnit",
24+
"unit",
25+
"testing",
26+
"tests",
27+
"async"
28+
],
29+
"bin": {
30+
"qunit": "./bin/cli.js"
31+
},
32+
"engines": {
33+
"node": ">=0.6.0 < 0.12.0"
34+
},
35+
"scripts": {
36+
"test": "node --harmony ./test/testrunner.js"
37+
},
38+
"dependencies": {
39+
"argsparser": "^0.0.6",
40+
"cli-table": "^0.3.0",
41+
"co": "^3.0.6",
42+
"qunitjs": "1.10.0",
43+
"tracejs": "^0.1.8",
44+
"underscore": "^1.6.0"
45+
},
46+
"devDependencies": {
47+
"chainer": "^0.0.5",
48+
"timekeeper": "^0.0.4"
49+
},
50+
"optionalDependencies": {
51+
"istanbul": "https://github.com/gotwarlost/istanbul/tarball/harmony"
52+
},
53+
"licenses": [
54+
{
55+
"type": "MIT",
56+
"url": "http: //www.opensource.org/licenses/mit-license.php"
57+
}
58+
]
4159
}

readme.md

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ http://github.com/jquery/qunit
1515
- the simplest API of the world, especially for asynchronous testing
1616
- you can write tests in TDD or BDD style depending on your task and test type
1717
- you can run the same tests in browser if there is no dependencies to node
18+
- generators support
1819

1920
### Installation
2021

@@ -176,19 +177,19 @@ QUnit API and code which have to be tested are already loaded and attached to th
176177
Some tests examples
177178

178179

179-
test("a basic test example", function (assert) {
180+
test("a basic test example", function () {
180181
ok(true, "this test is fine");
181182
var value = "hello";
182183
equal("hello", value, "We expect value to be hello");
183184
});
184185

185186
QUnit.module("Module A");
186187

187-
test("first test within module", 1, function (assert) {
188+
test("first test within module", 1, function () {
188189
ok(true, "a dummy");
189190
});
190191

191-
test("second test within module", 2, function (assert) {
192+
test("second test within module", 2, function () {
192193
ok(true, "dummy 1 of 2");
193194
ok(true, "dummy 2 of 2");
194195
});
@@ -202,7 +203,7 @@ Some tests examples
202203
}
203204
});
204205

205-
test("some other test", function (assert) {
206+
test("some other test", function () {
206207
expect(2);
207208
equal(true, false, "failing test");
208209
equal(true, true, "passing test");
@@ -215,11 +216,11 @@ Some tests examples
215216
}
216217
});
217218

218-
test("this test is using shared environment", 1, function (assert) {
219+
test("this test is using shared environment", 1, function () {
219220
deepEqual({ test: 123 }, this.options, "passing test");
220221
});
221222

222-
test("this is an async test example", function (assert) {
223+
test("this is an async test example", function () {
223224
expect(2);
224225
stop();
225226
setTimeout(function () {
@@ -229,6 +230,12 @@ Some tests examples
229230
}, 100);
230231
});
231232

233+
### Generators support
234+
235+
test("my async test with generators", function* () {
236+
var data = yield asyncFn();
237+
equal(data, {a: 1}, 'generators work');
238+
});
232239

233240
### Run tests
234241

test/fixtures/generators-code.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
exports.getMyGen = function() {
2+
return function* () {
3+
return yield {a: 1};
4+
};
5+
};

test/fixtures/generators-test.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
test('generators', function* () {
2+
var data = yield getMyGen();
3+
deepEqual(data, {a: 1}, 'woks');
4+
});

test/testrunner.js

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ var a = require('assert'),
33
_ = require('underscore');
44

55
var tr = require('../lib/testrunner'),
6-
log = require('../lib/log');
6+
log = require('../lib/log'),
7+
generators = require('../lib/generators');
78

89
var fixtures = __dirname + '/fixtures',
910
chain = chainer();
@@ -175,10 +176,33 @@ chain.add('coverage', function() {
175176
delete res.runtime;
176177
a.equal(err, null, 'no errors');
177178
a.deepEqual(stat, res, 'coverage code testing works');
179+
tr.options.coverage = false;
178180
chain.next();
179181
});
180182
});
181183

184+
if (generators.support) {
185+
chain.add('generators', function() {
186+
tr.run({
187+
code: fixtures + '/generators-code.js',
188+
tests: fixtures + '/generators-test.js'
189+
}, function(err, res) {
190+
var stat = {
191+
files: 1,
192+
tests: 1,
193+
assertions: 1,
194+
failed: 0,
195+
passed: 1
196+
};
197+
delete res.runtime;
198+
delete res.coverage;
199+
a.equal(err, null, 'no errors');
200+
a.deepEqual(stat, res, 'coverage code testing works');
201+
chain.next();
202+
});
203+
});
204+
}
205+
182206
chain.add(function() {
183207
console.log('\nAll tests ok.');
184208
});

0 commit comments

Comments
 (0)