Permalink
Browse files

some refactor, extend method will be used from sharedjs lib, @insin a…

…pi has been changed a bit, no module and file properties for file object, just a path, no matter if its module name or the full path
  • Loading branch information...
1 parent 21d25e7 commit 9f766c333528ec0b33d41de9eb7379680ab0a1eb @kof kof committed Mar 10, 2011
Showing with 71 additions and 115 deletions.
  1. +0 −3 .gitmodules
  2. +16 −19 bin/cli.js
  3. +0 −1 deps/argsparser
  4. +17 −53 lib/api.js
  5. +18 −23 lib/bootstrap.js
  6. +12 −11 lib/qunit.js
  7. +7 −4 package.json
  8. +1 −1 test/api.js
View
@@ -1,6 +1,3 @@
-[submodule "deps/argsparser"]
- path = deps/argsparser
- url = http://github.com/kof/node-argsparser.git
[submodule "deps/jscoverage"]
path = deps/jscoverage
url = http://github.com/visionmedia/node-jscoverage.git
View
35 bin/cli.js 100644 → 100755
@@ -1,35 +1,32 @@
#!/usr/bin/env node
var root = __dirname + "/..",
- args = require( root + "/deps/argsparser" ).parse(),
+ args = require("argsparser").parse(),
qunit = require( root ),
util = require( "util" ),
o = qunit.options,
code, tests;
/**
* Parses a code or dependency argument, returning an object defining the
- * specified file path or module name. Any input ending in ".js" will be
- * treated as a file path, otherwise it will be treated as a module name.
+ * specified file path or/and module name.
* The exports of the module will be exposed globally by default. To expose
* exports as a named variable, prefix the resource with the desired variable
* name followed by a colon.
* This allows you to more accurately recreate browser usage of QUnit, for
* tests which are portable between browser runtime environmemts and Node.js.
+ * @param {string} path to file or module name to require.
+ * @return {Object} resource
*/
-function parseTestResource ( input ) {
- var parts = input.split( ":" ),
- requirePath = parts.pop(),
- resource = {};
+function parsePath( path ) {
+ var parts = path.split( ":" ),
+ resource = {
+ path: path
+ };
- if ( /\.js$/.test( requirePath ) ) {
- resource.file = requirePath;
- } else {
- resource.module = requirePath;
- }
-
- if ( parts.length === 1 ) {
+ if ( parts.length === 2 ) {
resource.as = parts[0];
+ resource.path = parts[1];
}
return resource;
@@ -55,7 +52,7 @@ for ( var key in args ) {
switch( key ) {
case "-c":
case "--code":
- code = parseTestResource(args[key]);
+ code = parsePath(args[key]);
break;
case "-t":
case "--tests":
@@ -66,11 +63,11 @@ for ( var key in args ) {
break;
case "-d":
case "--deps":
- var deps = args[key];
- if ( !Array.isArray ( deps ) ) {
- deps = [deps];
+ o.deps = args[key];
+ if ( !Array.isArray ( o.deps ) ) {
+ o.deps = [o.deps];
}
- o.deps = deps.map(parseTestResource);
+ o.deps = o.deps.map(parsePath);
break;
case "-o":
case "--errors-only":
Submodule argsparser deleted from 0bdc6d
View
@@ -1,8 +1,8 @@
var util = require( "util" ),
- assert = require( "assert" );
+ assert = require( "assert" ),
+ $ = require('sharedjs');
var undefined,
- noop = function(){},
pause = false,
queue = [],
results = [],
@@ -21,11 +21,7 @@ exports.QUnit = QUnit;
function run() {
// all tests are done
if ( queue.length <= 0 ) {
- var doneResult = QUnit.done();
- if ( doneResult ) {
- results.push( doneResult );
- }
- util.print( JSON.stringify(results) );
+ QUnit.done(results);
return;
}
@@ -39,7 +35,7 @@ function run() {
// run the test
if ( !pause ) {
- test.fn.call( QUnit.current_testEnvironment = extend( true, test.env || {}, module.env ) );
+ test.fn.call( QUnit.current_testEnvironment = $.extend( true, {}, module.env , test.env) );
}
if ( test.async ) {
@@ -91,50 +87,16 @@ function testDone() {
}
/**
- * Print stringified object to stdout
- * @param {Object} obj
+ * Collect results
+ * @param {Object} res test result
*/
-function log( obj ) {
-
- obj.message = obj.message || "";
- obj.name = test.name || "";
- obj.module = module.name || "";
-
- results.push(obj);
+function log(res) {
+ res.message = res.message || "";
+ res.name = test.name || "";
+ res.module = module.name || "";
+ results.push(res);
}
-/**
- * Ecma 5 doesn't defines this method, so just hope they also like jQuery's way :)
- * You can pass so much objects as you like, all properties will be copied to the first object
- *
- * @param {Object|Boolean} deep
- * @param {Object}
- * @return {Object}
- */
-function extend( deep /*, obj, obj, ...*/ ) {
- // take first argument, if its not a boolean
- var args = arguments,
- i = typeof deep === 'boolean' ? 1 : 0,
- target = args[i];
-
- for ( ; i < args.length; ++i ) {
- typeof args[i] === "object" && Object.keys( args[i] ).forEach(function( key ) {
- // if deep extending and both of keys are objects
- if (key == "console") {
- return;
- }
- if ( deep === true && target[key] ) {
- args.callee(deep, target[key], args[i][key]);
- } else {
- target[key] = args[i][key];
- }
- });
- }
-
- return target;
-}
-
-exports.extend = extend;
/**
* Begin tests execution
@@ -150,7 +112,7 @@ QUnit.begin = function() {
/**
* Callback for tests complete
*/
-QUnit.done = noop;
+QUnit.done;
/**
* Define current module name and environment
@@ -589,9 +551,11 @@ QUnit.equiv = function () {
// copy api to QUnit namespace
-extend( QUnit, QUnit.api );
+$.extend( QUnit, QUnit.api );
+
+// make qunit api global
+$.extend( global, QUnit.api );
+// provide QUnit global namespace
global.QUnit = QUnit;
-// make qunit api global
-extend( global, QUnit.api );
View
@@ -1,21 +1,18 @@
var qunit = require( "./api" ),
path = require( "path" ),
util = require( "util" ),
+ $ = require('sharedjs'),
options = JSON.parse( process.argv[2] );
-function requireTestResource(res) {
- // test resource must define .file or .module
- var requirePath = res.file ?
- res.file.replace( /\.js$/, "" ) :
- res.module,
- mod = {};
+function load(res) {
+ var requirePath = res.path.replace( /\.js$/, "" );
+
// test resource can define .as to expose its exports as a named object
if (res.as) {
- mod[res.as] = require( requirePath );
+ global[res.as] = require(requirePath);
} else {
- mod = require ( requirePath );
+ $.extend(global, require(requirePath));
}
- qunit.extend( global, mod );
}
// add paths to require
@@ -24,24 +21,22 @@ if ( options.paths ) {
}
// require deps
-options.deps.forEach(function( dep ){
- requireTestResource( dep );
-});
+options.deps.forEach(load);
// require code
-requireTestResource( options.code );
+load(options.code);
// require tests
-options.tests.forEach(function( test ){
- require( test.replace( /\.js$/, "" ) );
-});
+options.tests.forEach(load);
-if ( options.coverage ) {
- QUnit.done = function() {
- return {
+QUnit.done = function(results) {
+ if ( options.coverage ) {
+ results.push({
cov: _$jscoverage
- };
- };
-}
+ });
+ }
+
+ util.print( JSON.stringify(results) );
+};
-QUnit.begin();
+QUnit.begin();
View
@@ -92,7 +92,7 @@ function run( opts, report, callback ) {
util.print( "stderr: " + buf );
});
- child.on( "exit", function( code ) {
+ child.on( "exit", function() {
JSON.parse( results.join( "" ) ).forEach(function( data ) {
report.assertions++;
@@ -125,13 +125,15 @@ function run( opts, report, callback ) {
/**
* Make an absolute path from relative
* @param {String|Object} file
- * @return {String|Object}
+ * @return {Object}
*/
-function absPath( file ) {
- if ( typeof file === "string" && file.charAt(0) !== "/" ) {
- file = path.join( process.cwd(), file );
- } else if ( file.file && file.file.charAt(0) !== "/" ) {
- file.file = path.join( process.cwd(), file.file );
+function absPath(file) {
+ if (typeof file === 'string') {
+ file = {path: file};
+ }
+
+ if(file.path.charAt(0) === '.') {
+ file.path = path.join( process.cwd(), file.path );
}
return file;
@@ -162,8 +164,7 @@ function toArray( files ) {
* @param {Function} callback optional
*/
exports.run = function( files, callback ) {
-
- if ( !(files instanceof Array) ) {
+ if ( !Array.isArray(files) ) {
files = [files];
}
@@ -203,8 +204,8 @@ exports.run = function( files, callback ) {
}
if ( opts.coverage ) {
- coverage.instrument( opts.code.file, function( newCodePath, cleanup ) {
- opts.code.file = newCodePath;
+ coverage.instrument( opts.code.path, function( newCodePath, cleanup ) {
+ opts.code.path = newCodePath;
run( opts, report, function(cov) {
cleanup();
finished(cov);
View
@@ -1,24 +1,27 @@
{
"name": "qunit",
"description": "A port of QUnit unit testing framework to nodejs",
- "version": "0.1.4",
+ "version": "0.1.5",
"author": "Oleg Slobodskoi <oleg008@gmail.com>",
"contributors": [
{ "name": "John Resig", "email": "jeresig@gmail.com" },
- { "name": "Jörn Zaefferer", "email": "joern.zaefferer@gmail.com" }
+ { "name": "Jörn Zaefferer", "email": "joern.zaefferer@gmail.com" },
+ { "name": "Jonathan Buchanan"}
],
"repository": {
"type": "git",
"url": "http://github.com/kof/node-qunit.git"
},
"keywords": [ "TDD", "QUnit", "unit testing", "tests" ],
- "directories": { "lib": "./lib" },
"bin": { "qunit": "./bin/cli.js" },
- "main": "./lib/qunit.js",
"engines": { "node": ">= 0.3.0" },
"scripts": {
"install": "make install"
},
+ "dependencies": {
+ "sharedjs": ">= 0.0.2",
+ "argsparser": ">= 0.0.4"
+ },
"licenses": [
{
"type": "MIT",
View
@@ -185,7 +185,7 @@ test("makeurl working",function() {
module("testEnvironment with makeurl settings", {
url:'http://google.com/',
-q:'another_search_test'
+ q:'another_search_test'
});
test("makeurl working with settings from testEnvironment", function() {
equal( makeurl(), 'http://google.com/?q=another_search_test', 'rather than passing arguments, we use test metadata to form the url');

0 comments on commit 9f766c3

Please sign in to comment.