Skip to content

Commit

Permalink
done should be independent on specs printing. closes #6
Browse files Browse the repository at this point in the history
  • Loading branch information
masylum committed Apr 22, 2011
1 parent f9e5e35 commit 11f4c1a
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 47 deletions.
3 changes: 3 additions & 0 deletions Makefile
Expand Up @@ -17,4 +17,7 @@ test_stubs:
test_sync:
@$(NODE) tests/sync.js

test_async:
@$(NODE) tests/async.js

.PHONY: test
6 changes: 3 additions & 3 deletions Readme.md
Expand Up @@ -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.

Expand Down Expand Up @@ -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
Expand Down
68 changes: 30 additions & 38 deletions lib/testosterone.js
Expand Up @@ -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(
Expand Down Expand Up @@ -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;
};

Expand All @@ -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;
};
Expand Down
4 changes: 2 additions & 2 deletions package.json
@@ -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": "*" }
}
25 changes: 25 additions & 0 deletions tests/async.js
@@ -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!');
});
8 changes: 4 additions & 4 deletions tests/stubs.js
Expand Up @@ -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();
});
})

Expand Down

0 comments on commit 11f4c1a

Please sign in to comment.