Permalink
Browse files

initial commit

  • Loading branch information...
0 parents commit 6e416c0c38399516ec69143c09efc3e8efaabd1d @kof kof committed Sep 17, 2010
Showing with 298 additions and 0 deletions.
  1. +1 −0 .gitignore
  2. +31 −0 lib/cli.js
  3. +211 −0 lib/qunit.js
  4. +23 −0 lib/testrunner.js
  5. +13 −0 package.json
  6. +19 −0 readme.md
@@ -0,0 +1 @@
+.DS_Store
@@ -0,0 +1,31 @@
+var code = process.argv[2],
+ test = process.argv[3],
+ qunit = require( "./qunit" ),
+ path = require( "path" );
+
+require.paths.push( path.join( process.env.PWD , path.dirname(process.env._) ) );
+
+// make qunit api global
+extend( global, qunit.api );
+
+// require code
+extend( global, require( code.substr(0, code.length-3) ) );
+
+// require test
+require( test.substr(0, test.length-3) );
+
+qunit.begin();
+
+function extend( target /* ,obj, obj */ ) {
+ var args = arguments,
+ i = 0,
+ key;
+
+ for ( ; i < args.length; ++i ) {
+ for ( key in args[i] ) {
+ target[key] = args[i][key];
+ }
+ }
+
+ return target;
+}
@@ -0,0 +1,211 @@
+var sys = require( "sys" ),
+ assert = require( "assert" );
+
+var options = exports.options = {
+ errorsOnly: false,
+ stack: true
+ };
+
+var api = exports.api = {},
+ undefined,
+ pause = false,
+ queue = [],
+ test,
+ module;
+
+// for the case api properties will be overwritten by tested module
+api.QUnit = api;
+
+exports.begin = function() {
+ if ( queue.length <= 0 ) {
+ throw new Error( "There are no tests defined" );
+ }
+
+ api.start();
+};
+
+api.module = function( name, opts ) {
+ module = {
+ name: name,
+ opts: opts
+ };
+
+ return this;
+};
+
+api.test = function( name, expect, fn, async ) {
+
+ if ( typeof name !== "string" ) {
+ throw new Error( "Test name should be the first parameter" );
+ }
+
+ if ( typeof expect === "function" ) {
+ fn = expect;
+ expect = 0;
+ }
+
+ if ( typeof fn !== "function" ) {
+ throw new Error( "No test function passed" );
+ }
+
+ queue.push({
+ name: name,
+ module: module,
+ fn: fn,
+ expected: expect,
+ done: 0,
+ async: async
+ });
+
+ return this;
+};
+
+api.asyncTest = function( name, expect, fn ) {
+ api.test( name, expect, fn, true );
+ return this;
+};
+
+api.start = function() {
+ pause = false;
+ testDone();
+ run();
+ return this;
+};
+
+api.stop = function() {
+ pause = true;
+ return this;
+};
+
+api.expect = function( amount ) {
+ test.expected = amount;
+ return this;
+};
+
+api.ok = function( value, message ) {
+ test.done++;
+ var error = false;
+
+ try {
+ assert.ok( value );
+ } catch (err) {
+ error = err;
+ }
+
+ log( error, {
+ module: module.name,
+ name: test.name,
+ message: message
+ });
+
+ return this;
+};
+
+api.equals = function( actual, expected, message ) {
+ test.done++;
+
+ var error = false;
+
+ try {
+ assert.equal( actual, expected );
+ } catch( err ) {
+ error = err;
+ }
+
+ log( error, {
+ module: module.name,
+ name: test.name,
+ message: message
+ });
+
+ return this;
+};
+
+api.same = function( actual, expected, message ) {
+ test.done++;
+
+ var error = false;
+
+ try {
+ assert.deepEqual( actual, expected );
+ } catch( err ) {
+ error = err;
+ }
+
+ log( error, {
+ module: module.name,
+ name: test.name,
+ message: message
+ });
+
+ return this;
+};
+
+function run() {
+
+ test = queue.shift();
+ module = test.module;
+
+ if ( module.opts && module.opts.setup ) {
+ module.opts.setup();
+ }
+
+ // run the test
+ test.fn();
+
+ if ( test.async ) {
+ api.stop();
+ } else {
+ testDone();
+ }
+
+ if ( !pause && queue.length > 0 ) {
+ run();
+ }
+}
+
+function testDone() {
+
+ if ( !test || !module ) {
+ return;
+ }
+
+ if ( module.opts && module.opts.teardown ) {
+ module.opts.teardown();
+ }
+
+ // check if expected assertions count is correct
+ if ( test.expected !== test.done ) {
+ log( true, {
+ name: test.name,
+ module: module.name,
+ message: "Expected " + test.expected + " test.assertions, but " + test.done + " were run"
+ });
+ }
+}
+
+function format( f, str ) {
+ switch ( f ) {
+ case "red":
+ return "\033[31m" + str + "\033[39m";
+ case "green":
+ return "\033[32m" + str + "\033[39m";
+ case "bold":
+ return "\033[1m" + str + "\033[22m";
+ }
+}
+
+function log( error, obj ) {
+ var descr = "[" + (obj.module ? obj.module +": " : "") + obj.name +"] ";
+
+ if ( error ) {
+ sys.print( format( "red", "[Error] ") + descr + obj.message );
+ if ( error && error.stack && options.stack ) {
+ sys.print( "\n" + error.stack + "\n" );
+ }
+ } else if ( !options.errorsOnly ) {
+ sys.print( format( "green", "[OK]" ) + descr + obj.message );
+ }
+
+ sys.print( "\n" );
+}
@@ -0,0 +1,23 @@
+var fs = require( "fs" ),
+ sys = require( "sys" ),
+ spawn = require("child_process").spawn;
+
+exports.run = function( files ) {
+
+ if ( !(files instanceof Array) ) {
+ files = [files];
+ }
+
+ files.forEach( function( file ) {
+ var proc = spawn( "node", [ __dirname + "/cli.js", file.code, file.test ] );
+
+ proc.stderr.on( "data", function (data) {
+ sys.print( data );
+ });
+
+ proc.stdout.on( "data", function (data) {
+ sys.print( data );
+ });
+
+ });
+};
@@ -0,0 +1,13 @@
+{
+ "name": "node-qunit",
+ "description": "Port of QUnit unit testing framework to nodejs",
+ "version": "0.0.1",
+ "author": "Oleg Slobodskoi <oleg008@gmail.com>",
+ "contributors": [
+ { "name": "John Resig", "email": "jeresig@gmail.com" },
+ { "name": "Jörn Zaefferer"}
+ ],
+ "keywords": ["TDD", "QUnit", "unit testing", "tests"],
+ "directories": { "lib": "./lib" },
+ "engines": { "node": ">= 2.0" }
+}
@@ -0,0 +1,19 @@
+## This is a port of QUnit unit testing framework to nodejs
+
+http://github.com/jquery/qunit
+
+## Features
+ * the simplest API of the world :)
+ * simple asynchronous testing
+ * tests inside of one testfile run synchronous
+ * usage via cli or testrunner
+ * evry tests file and code run in its own spawned node instance
+
+
+
+## Usage
+
+## Run tests
+
+
+

0 comments on commit 6e416c0

Please sign in to comment.