Permalink
Browse files

[api] added all the stuff

  • Loading branch information...
1 parent e9547b4 commit c0339a3a49d536785145c63cb66bd63efd764080 @dscape committed Mar 5, 2012
Showing with 294 additions and 39 deletions.
  1. +3 −0 .gitignore
  2. +117 −0 README
  3. +3 −3 package.json
  4. +15 −0 reporters/default.js
  5. +46 −36 specify.js
  6. +14 −0 test.sh
  7. +15 −0 test/fixtures/all.txt
  8. +5 −0 test/fixtures/filters.txt
  9. +76 −0 test/specify.js
View
@@ -0,0 +1,3 @@
+.DS_Store
+node_modules/
+*.log
View
117 README
@@ -0,0 +1,117 @@
+<a name="specify"/>
+# specify
+
+`specify` is the simplest way i could think to do node.js testing. it works with sync code and async code all the same.
+
+``` js
+specify('create_by_secret#wrong_invitation_code', function (assert) {
+ user.create_by_secret({invitation_code: "1234321!!"}, function (err) {
+ assert.equal(err.eid, "ec:api:user:create_by_secret:wrong_code");
+ assert.equal(err.status_code, 400);
+ });
+});
+```
+
+the assert calls are function that wrap the assert module. when you call them you are actually calling a callback.
+
+the way i figure out how many asserts you will run is by [static-analysis]. putting it simply it means i count the numbers of time you wrote `assert.`. this doesn't work for for loop, so in that case you can do something like this:
+
+``` js
+specify('specify#more_assertions_than_asserts', function(assert) {
+ assert.expect(5);
+ for(var i in [1,2,3,4,5]) {
+ assert.equal(i,i);
+ }
+});
+```
+
+specify runs tests in one by one, not in parallel. this means that if you set `assert.expect` higher than the number of asserts you actually do the rest of the tests wont run, cause you will never finish the current test.
+
+<a name="installation"/>
+# installation
+
+<a name="node"/>
+## node.js
+
+1. install [npm]
+2. `npm install specify`
+3. `var specify = require('specify');`
+
+<a name="reporters"/>
+# reporters
+
+if you feel like the output sent to `stdout` is ugly you can do `npm install colors`. if thats still not good enough, write your own reporter and send in a pull request. now use it:
+
+``` js
+specify('specify#ask_for_a_specific_reporter', function(assert) {
+ specify.summary('my_awesome_reporter');
+ setTimeout(function (){
+ assert.ok(true);
+ },1);
+});
+```
+
+you can also do this with a function if you like:
+
+``` js
+specify('specify#custom_reporter_from_function', function(assert) {
+ specify.summary(function (name, report, errors) {
+ console.log(name);
+ });
+ setTimeout(function () {
+ assert.ok(false, 'i see dead people');
+ assert.ok(true);
+ },1);
+});
+```
+
+<a name="samples"/>
+# samples
+
+check out the tests in `test/specify.js`
+
+<a name="contribute"/>
+# contribute
+
+everyone is welcome to contribute. patches, bug-fixes, new features
+
+1. create an [issue][issues] so the community can comment on your idea
+2. fork `specify`
+3. create a new branch `git checkout -b feature_name`
+4. create tests for the changes you made
+5. make sure you pass both existing and newly inserted tests
+6. commit your changes
+7. push to your branch `git push origin feature_name`
+8. create an pull request
+
+<a name="meta"/>
+# meta
+
+* code: `git clone git://github.com/dscape/specify.git`
+* home: <http://github.com/dscape/specify>
+* bugs: <http://github.com/dscape/specify/issues>
+* build: [![build status](https://secure.travis-ci.org/dscape/p.png)](http://travis-ci.org/dscape/specify)
+
+`(oO)--',-` in [caos]
+
+<a name="license"/>
+# license
+
+copyright 2012 nuno job <nunojob.com> `(oO)--',--`
+
+licensed under the apache license, version 2.0 (the "license");
+you may not use this file except in compliance with the license.
+you may obtain a copy of the license at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+unless required by applicable law or agreed to in writing, software
+distributed under the license is distributed on an "as is" basis,
+without warranties or conditions of any kind, either express or implied.
+see the license for the specific language governing permissions and
+limitations under the license
+
+[npm]: http://npmjs.org
+[issues]: http://github.com/dscape/specify/issues
+[caos]: http://caos.di.uminho.pt/
+[static-analysis]: http://en.wikipedia.org/wiki/Static_program_analysis
View
@@ -1,13 +1,13 @@
{ "name" : "specify"
, "description" : "bite sized node.js testing"
, "author" : "nuno job <nunojobpinto@gmail.com> (http://nunojob.com/)"
-, "version" : "0.0.1"
+, "version" : "0.0.2"
, "main" : "./specify.js"
, "homepage" : "https://github.com/dscape/specify"
, "repository" :
{ "type": "git", "url": "http://github.com/dscape/specify.git" }
, "bugs" : "http://github.com/dscape/specify/issues"
, "keywords" : [ "test", "assert", "should", "tdd" ]
-, "engines" : { "node" : ">=0.4.0" }
-, "scripts" : { "test": "node test"}
+, "engines" : { "node" : ">=0.4.0" }
+, "scripts" : { "test" : "./test.sh"}
}
View
@@ -0,0 +1,15 @@
+var colors;
+
+try { colors = require('colors'); } catch (e) {}
+function c(msg,color) { return colors ? msg[color] : msg; }
+
+module.exports = function default_reporter(name, report, errors){
+ errors = errors || [];
+ var symbol = report.fail === 0 ? c('', 'green') : c('','red');
+ process.stdout.write(symbol + ' ');
+ process.stdout.write(report.ok + '/' + (report.ok+report.fail) + ' ');
+ console.log(c(name, 'cyan') + " ");
+ errors.forEach(function(err) {
+ console.log(c('└───── ', 'grey') + err);
+ });
+};
View
@@ -1,18 +1,21 @@
var assert = require('assert')
+ , path = require('path'), colors
+ , reporters = {}
, assertions =
- [ 'fail', 'ok', 'equal', 'notEqual', 'deepEqual', 'notDeepEqual'
- , 'strictEqual', 'notStrictEqual', 'throws', 'doesNotThrow', 'ifError' ]
- , colors
+ [ 'ok', 'equal', 'notEqual', 'deepEqual', 'notDeepEqual'
+ , 'strictEqual', 'notStrictEqual' ]
;
-module.exports = (function describe() {
- try { colors = require('colors'); } catch (e) {}
- function c(msg,color) { return colors ? msg[color] : msg; }
+require('fs').readdirSync('./reporters').forEach(function(reporter) {
+ reporters[reporter] = require(path.join(__dirname, 'reporters', reporter));
+});
+module.exports = (function specify() {
var cache = []
, counts = { _totals: {ok: 0, fail: 0} }
- , desc
+ , spec, summary, def_summary
;
- function ensureFor(test, expect, done) {
- var ensure = {}, count = expect;
+ def_summary = summary = reporters['default.js'];
+ function ensure_for(test, expect, done) {
+ var ensure = {}, count = expect, errored = [];
assertions.forEach(function(assertion) {
counts[test] = {ok: 0, fail: 0};
ensure[assertion] = function () {
@@ -21,25 +24,23 @@ module.exports = (function describe() {
counts._totals.ok++;
counts[test].ok++;
}
- catch (ex) {
+ catch (err) {
+ errored.push(err.message);
counts._totals.fail++;
counts[test].fail++;
}
count--;
if(count === 0) {
- done();
+ done(errored);
}
};
});
+ ensure.expect = function (nr) { count = nr; };
return ensure;
}
- function runTests(tests) {
+ function run_tests(tests) {
if(tests.length === 0) {
- var symbol = counts._totals.fail === 0 ? c('', 'green') : c('','red');
- process.stdout.write(symbol + ' ');
- process.stdout.write(c('summary', 'yellow') + " ");
- console.log(counts._totals.ok + ' worked, ' +
- counts._totals.fail + ' failed ');
+ summary('summary', counts._totals);
process.exit(counts._totals.fail === 0 ? 0 : -1);
}
else {
@@ -54,44 +55,53 @@ module.exports = (function describe() {
var match = fbody.match(new RegExp("assert\\.\\w", "gm"));
if(match) {
expect = match.length;
- return f(ensureFor(name, expect, function () {
- var symbol =
- counts[name].fail === 0 ? c('', 'green') : c('','red');
- process.stdout.write(symbol + ' ');
- process.stdout.write(c(name, 'cyan') + " ");
- console.log(counts[name].ok + ' worked, ' +
- counts[name].fail + ' failed ');
- runTests(tests);
+ return f(ensure_for(name, expect, function (errors) {
+ summary(name, counts[name], errors);
+ run_tests(tests);
}));
} else {
- console.log(c('','red') + c(name, 'cyan')
- + ' you need to add at least on `assert.*` call');
+ summary(name, {ok: 0, fail: 1},
+ [' you need to add at least on `assert.*` call']);
}
} else {
- console.log(c('','red') + c(name, 'cyan')
- + ' `assert` must be the first argument of your callback');
+ summary(name, {ok: 0, fail: 1},
+ [' `assert` must be the first argument of your callback']);
}
counts._totals.fail++;
- runTests(tests);
+ run_tests(tests);
}
}
- desc = function describeTest(name, f) {
+ spec = function specify_test(name, f) {
cache.push([].slice.call(arguments,0));
};
- desc.run = function runAllTests(filter) {
- if(filter.length !== 0) {
+ spec.summary = function (f) {
+ if (typeof f === 'function') {
+ summary = f;
+ return;
+ }
+ else if (typeof f === 'string') {
+ var reporter = reporters[f + '.js'];
+ if(typeof reporter === 'function') {
+ summary = reporter;
+ return;
+ }
+ }
+ summary = def_summary;
+ };
+ spec.run = function run_all_tests(filter) {
+ if(filter && filter.length !== 0) {
var filtered_cache = [];
filter.forEach(function (e) {
cache.forEach(function (c){
var name = c[0];
if(name===e) filtered_cache.push(c);
});
});
- runTests(filtered_cache);
+ run_tests(filtered_cache);
}
else {
- runTests(cache);
+ run_tests(cache);
}
};
- return desc;
+ return spec;
})();
View
14 test.sh
@@ -0,0 +1,14 @@
+node test/specify.js | sed 's/.\[[0-9][0-9]m//g' > test/all.log
+node test/specify.js specify#assertion_with_optional_message \
+ specify#all_assertions | sed 's/.\[[0-9][0-9]m//g' > test/filters.log
+diff test/all.log test/fixtures/all.txt
+if [ $? -eq 0 ]; then
+ diff test/filters.log test/fixtures/filters.txt
+ if [ $? -eq 0 ]; then
+ echo "ok";
+ else
+ return -1;
+ fi
+else
+ return -1;
+fi
View
@@ -0,0 +1,15 @@
+✗ 0/1 specify#no_arguments_in_cb
+└───── `assert` must be the first argument of your callback
+✗ 0/1 specify#no_assertions
+└───── you need to add at least on `assert.*` call
+✔ 1/1 specify#sync
+✔ 7/7 specify#all_assertions
+✗ 1/3 specify#assertion_with_optional_message
+└───── this is the error you are looking for
+└───── this will
+[ 'specify#custom_pretty_print', [ 'i see dead people' ] ]
+✔ 1/1 specify#ask_for_a_specific_reporter
+specify#custom_pretty_print_just_name
+✔ 1/1 specify#async
+✔ 5/5 specify#more_assertions_than_asserts
+✗ 18/24 summary
@@ -0,0 +1,5 @@
+✗ 1/3 specify#assertion_with_optional_message
+└───── this is the error you are looking for
+└───── this will
+✔ 7/7 specify#all_assertions
+✗ 8/10 summary
Oops, something went wrong.

0 comments on commit c0339a3

Please sign in to comment.