Permalink
Browse files

`done` should be independent on specs printing. closes #6

  • Loading branch information...
masylum committed Apr 22, 2011
1 parent f9e5e35 commit 11f4c1a3650c9b734f3ec135c70cb1207cdd3b88
Showing with 67 additions and 47 deletions.
  1. +3 −0 Makefile
  2. +3 −3 Readme.md
  3. +30 −38 lib/testosterone.js
  4. +2 −2 package.json
  5. +25 −0 tests/async.js
  6. +4 −4 tests/stubs.js
View
@@ -17,4 +17,7 @@ test_stubs:
test_sync:
@$(NODE) tests/sync.js
+test_async:
+ @$(NODE) tests/async.js
+
.PHONY: test
View
@@ -21,14 +21,14 @@ the same testing library.
- `port` _(80)_ : Host to do the http calls.
- `quiet` _(false)_ : Ninja mode.
- `title` _(Testosterone)_ : Test title, it will be printed out.
-- `sync` _(false)_ : If set to true, you don't need to call `done` to specify when your tests are executed.
+- `sync` _(false)_ : If set to true, you don't need to call `done` to specify when your tests are done.
## API
_testosterone_ is simple and flexible.
- `get|post|head|put|delete...(url, req, response, cb)`: Does a http call with the given request. If a response is given, testosterone will assert that the real response matches.
-- `add(spec, function(done))`: Adds a test. The test is considered executed and `spec` will be printed when `done` function is called. You can use `done` to curry a function.
+- `add(spec, function(done))`: Adds a test. The test is considered executed when `done` function is called. You can use `done` to curry a function.
- `run(cb)`: Runs the tests in serial. `cb` will be called once all the tests are executed.
- `assert`: Using this assert object instead of the native one will allow you to count and print the assertions.
@@ -89,7 +89,7 @@ You have more examples on the `test` folder:
})
.run(function () {
- require('sys').print('done!');
+ require('sys').print('All tests passed!');
});
// Output
View
@@ -8,8 +8,7 @@ module.exports = function (config) {
var _sys = require('sys'),
- _specs = require('funk')(),
- _finished = require('funk')(),
+ _specs = [],
_config = config || {},
_client = require('http').createClient(
@@ -264,38 +263,8 @@ module.exports = function (config) {
* @returns
* Testosterone, so you can chain http calls.
*/
- TESTOSTERONE.add = function (spec, done) {
- var print = function () {
- _sys.print('\n\n' + _parseSpec(spec) + ' => '.yellow);
- };
-
- _specs.add(function () {
- if (_config.sync) {
- _finished.add(function () {})();
- if (!_config.quiet) {
- print();
- }
- done.call(done, function () { });
- } else {
- done.call(done, function (fn) {
- if (fn) {
- return _finished.add(function () {
- if (!_config.quiet) {
- print();
- }
- fn.apply(fn, arguments);
- });
- } else {
- // this is hackish
- _finished.add(function () {})();
- if (!_config.quiet) {
- print();
- }
- }
- });
- }
- })();
-
+ TESTOSTERONE.add = function (spec, body) {
+ _specs.push({spec: spec, body: body});
return TESTOSTERONE;
};
@@ -311,12 +280,35 @@ module.exports = function (config) {
* Testosterone, so you can chain http calls.
*/
TESTOSTERONE.serial = TESTOSTERONE.run = function (cb) {
- _specs.serial(function () {
- _finished.parallel(function () {
+ (function next() {
+ if (_specs.length > 0) {
+ var spec = _specs.shift();
+
+ if (!_config.quiet) {
+ _sys.print('\n\n' + _parseSpec(spec.spec) + ' => '.yellow);
+ }
+
+ if (_config.sync) {
+ spec.body.call(spec.body);
+ next();
+ } else {
+ spec.body.call(spec.body, function done(fn) {
+ // curry
+ if (fn) {
+ return function () {
+ fn.apply(fn, arguments);
+ next();
+ };
+ } else {
+ next();
+ }
+ });
+ }
+ } else {
cb.call(cb, arguments);
_test();
- });
- });
+ }
+ }());
return TESTOSTERONE;
};
View
@@ -1,11 +1,11 @@
{
"name": "testosterone",
"description": "Virile testing for http servers or any nodejs application",
- "version": "1.1.0",
+ "version": "1.2.0",
"repository": "git://github.com/masylum/testosterone.git",
"author": "Pau Ramon Revilla <masylum@gmail.com>",
"directories": { "lib": "lib" },
- "dependencies": {"funk": ">=1.0.1", "colors": ">=0.3.0"},
+ "dependencies": {"colors": ">=0.3.0"},
"main" : "index",
"engines": { "node": "*" }
}
View
@@ -0,0 +1,25 @@
+var testosterone = require('../lib/testosterone')({port: 3000, title: 'Async Testosterone'}),
+ add = testosterone.add,
+ order = 0,
+ assert = testosterone.assert;
+
+testosterone
+ .add('`first` test', function (next) {
+ assert.equal(order, 0);
+ setTimeout(function () {
+ next();
+ assert.equal(order, 0);
+ order++;
+ }, 100);
+ })
+
+ .add('and this is a `second` test', function (done) {
+ setTimeout(done(function () {
+ assert.equal(order, 1);
+ order++;
+ }), 200);
+ })
+
+ .run(function () {
+ require('sys').print('done!');
+ });
View
@@ -4,29 +4,29 @@ var testosterone = require('./..')({post: 3000, title: 'Testing with stubs', qui
assert = testosterone.assert;
testosterone
- .add('GIVEN foo.txt \nWHEN its empty \nTHEN it return null', function (spec) {
+ .add('GIVEN foo.txt \nWHEN its empty \nTHEN it return null', function (next) {
gently.expect(fs, 'readFile', function (path, encoding, cb) {
assert.equal(path, 'foo.txt');
cb(null, null);
});
- spec();
fs.readFile('foo.txt', 'utf-8', function (er, data) {
assert.equal(er, null);
assert.equal(data, null);
+ next();
});
})
- .add('GIVEN foo.txt \nWHEN it have content \nTHEN it return that content', function (spec) {
+ .add('GIVEN foo.txt \nWHEN it have content \nTHEN it return that content', function (next) {
gently.expect(fs, 'readFile', function (path, encoding, cb) {
assert.equal(path, 'foo.txt');
cb(null, 'foo');
});
- spec();
fs.readFile('foo.txt', 'utf-8', function (er, data) {
assert.equal(er, null);
assert.equal(data, 'foo');
+ next();
});
})

0 comments on commit 11f4c1a

Please sign in to comment.