Permalink
Browse files

Greatly increased the robustness of srunner

* Fixed a few bugs relating to no error messages
on incorrect input
* Fixed a testing problem with the package.json
* Added a very basic test
* Fixed some minor organization/aesthetic issues
in stest.js
  • Loading branch information...
1 parent 1123de3 commit 4609afe5f77977f44d46de1f7e9db730e70fd7f9 Siddharth Mahendraker committed Nov 21, 2011
Showing with 100 additions and 83 deletions.
  1. +20 −17 bin/srunner.js
  2. +43 −38 lib/stest.js
  3. +9 −3 package.json
  4. +0 −25 test/example.js
  5. +28 −0 test/test-stest.js
View
@@ -3,39 +3,42 @@
var path = require("path"),
fs = require("fs"),
cp = require("child_process"),
- argv = require("optimist").argv;
+ color = require("ansi-color").set,
+ argv = require("optimist")
+ .usage("Usage: $0 [-s] -r [regex]")
+ .demand(["r"])
+ .argv;
-// options
-var r = path.join(process.cwd(), argv.r);
+//options
var silent = argv.s ? argv.s : false;
+var r = path.join(process.cwd(), argv.r);
-var info = function(buffer){
+var info = function(buffer, colr){
var string = buffer.toString();
- if(!silent) console.log(string);
+ if(!silent)
+ process.stdout.write(color(string, colr));
}
var dir = path.dirname(r);
var filexp = new RegExp(path.basename(r));
if(!path.existsSync(dir)){
- info("This directory does not exist");
+ info("This directory does not exist\n", "red");
process.exit();
}
var files = fs.readdirSync(dir);
+var matchExp = function(file){ return filexp.test(file) ? true : false; };
+var filtered = files.filter(matchExp);
-if(!files.length){
- info("No tests found to run");
+if(!filtered.length){
+ info("No tests found to run\n", "red");
process.exit();
}
-files.forEach(function(file){
-
- if(filexp.test(file)){
-
- cp.exec("node "+dir+"/"+file, function(err, stdout, stderr){
- if(err) throw err;
- info(stdout);
- });
- }
+filtered.forEach(function(file){
+ cp.exec("node "+dir+"/"+file, function(err, stdout, stderr){
+ if(err) throw err;
+ if(!silent) info(stdout);
+ });
});
View
@@ -1,5 +1,5 @@
-var events = require("events");
-
+var events = require("events"),
+ color = require("ansi-color").set;
/*
* @class stest
*
@@ -13,6 +13,34 @@ function stest(){
this._queue = [];
this.ctx = {};
this.promise = new events.EventEmitter();
+
+ this._addTime = function(func){
+ var now = Date.now();
+ func();
+ return Date.now() - now;
+ }
+
+ this._report = function(name, errors, time){
+
+ var result = "",
+ c = "green";
+
+ if(!errors.length)
+ result = "\u2713";
+ else{
+ result = "\u2717";
+ c = "red";
+ }
+
+ // write results
+ process.stdout.write(color(result+" "+name+" - "+time+"ms\n", c));
+ // print errs
+ errors.forEach(function(err){
+ process.stdout.write(color(err.message+"\n", "red"));
+ if(err.type !== "stest")
+ process.stdout.write(color(err.stack+"\n", "red"));
+ });
+ }
}
/*
@@ -63,30 +91,25 @@ stest.prototype.run = function(){
// add listeners for each key
Object.keys(test).forEach(function(event, i, array){
self.promise.once(event, function(){
-
var args = Array.prototype.slice.call(arguments, 0);
- var now = Date.now();
-
- try{
- test[event].apply(self.ctx, args);
- }catch(e){
- errors.push(e);
- }
-
- totalTime += Date.now() - now;
+ totalTime += self._addTime(function(){
+ try{
+ test[event].apply(self.ctx, args);
+ }catch(e){
+ errors.push(e);
+ }
+ });
});
});
// user setup
- var setupT = Date.now();
- setup.apply(self.ctx, [self.promise]);
- totalTime += Date.now() - setupT;
+ totalTime += self._addTime(function(){ setup.apply(self.ctx,[self.promise]); });
// start the timeout
setTimeout(function(){
+
// makes sure everything is removed
Object.keys(test).forEach(function(event){
-
var ears = self.promise.listeners(event).length;
if(ears > 0){
var e = new Error("The '"+event+"' event never fired.");
@@ -97,28 +120,10 @@ stest.prototype.run = function(){
});
// user teardown
- var teardownT = Date.now();
- teardown.apply(self.ctx,[]);
- totalTime += Date.now() - teardownT;
-
- // give a little report on what happend
- console.log(name+"\n");
-
- if(!errors.length)
- console.log("No errors :-)");
- else{
- console.log("Errors:");
- errors.forEach(function(err){
-
- console.log(err.message);
- if(err.type !== "stest")
- console.log(err.stack);
- });
- }
-
- console.log("\nElapsed time: "+totalTime.toString()+"ms");
-
- }, (opts.timeout > 0 ? opts.timeout * 1000 : 5000));
+ totalTime += self._addTime(function(){ teardown.apply(self.ctx,[]); });
+ //give a report
+ self._report(name, errors, totalTime);
+ }, (opts.timeout > 0 ? opts.timeout : 250));
});
}
View
@@ -3,22 +3,28 @@
"name": "stest",
"description": "Sane event driven async testing.",
"version": "0.0.1",
+ "keywords": [
+ "async",
+ "test",
+ "event"
+ ],
"repository": {
"type": "git",
"url": "git://github.com/siddMahen/stest.git"
},
"main": "lib/stest.js",
"scripts": {
- "test": "srunner -s -r test/test-*.js"
+ "test": "srunner -r test/test-.*.js"
},
"bin": {
- "srunner": "./bin/srunner.js"
+ "srunner": "/bin/srunner.js"
},
"engines": {
"node": ">=0.4.12"
},
"dependencies": {
- "optimist": ">=0.2.8"
+ "optimist": ">=0.2.8",
+ "ansi-color": "0.2.x"
},
"devDependencies": {}
}
View
@@ -1,25 +0,0 @@
-var stest = require("stest"),
- assert = require("assert");
-
-var op = { timeout: 0 };
-
-stest.addCase("stest", op, {
- setup: function(promise){
- setTimeout(function(){
- promise.emit("hey", "there");
- }, 4000);
-
- var n = 45;
- this.n = n;
- promise.emit("cookies", n);
- },
- hey: function(text){
- assert.ok(text);
- },
- cookies: function(n){
- assert.equal(45, n);
- },
- teardown: function(){
- assert.equal(this.n, 45);
- }
-}).run();
View
@@ -0,0 +1,28 @@
+var assert = require("assert"),
+ stest = require("../lib/stest");
+
+var opts = { timeout: 0 };
+
+stest.addCase("stest - basic tests", opts, {
+ setup: function(promise){
+ this.one = "one";
+ promise.emit("ctx_1");
+ promise.emit("ctx_2", this);
+ promise.emit("data", "hello");
+ },
+ ctx_1: function(){
+ assert.ok(this.one);
+ assert.deepEqual("one", this.one);
+ },
+ ctx_2: function(instance){
+ assert.throws(function(){
+ var e = instance.nonExistant["crazy"];
+ });
+ assert.deepEqual(undefined, this.nonExistant);
+ },
+ data: function(data){
+ assert.ok(data);
+ assert.deepEqual("hello", data);
+ }
+})
+.run();

0 comments on commit 4609afe

Please sign in to comment.