Voice recog working nearly

Esse commit está contido em:
Tim Fogarty
2013-10-04 19:53:58 +01:00
commit cd936cee21
2972 arquivos alterados com 1220738 adições e 14809 exclusões
+1
Ver Arquivo
@@ -12,6 +12,7 @@ app.use(app.router);
app.use(express.static(__dirname + '/public')); app.use(express.static(__dirname + '/public'));
app.get('/', routes.index); app.get('/', routes.index);
app.get('/takeoff', routes.takeoff);
app.listen(3000, function(){ app.listen(3000, function(){
console.log("Server started on port 3000"); console.log("Server started on port 3000");
gerado externo Link simbólico
+1
Ver Arquivo
@@ -0,0 +1 @@
../share/bin/sharejs
gerado externo Link simbólico
+1
Ver Arquivo
@@ -0,0 +1 @@
../share/bin/exampleserver
+1 -5
Ver Arquivo
@@ -37,9 +37,5 @@
"url": "https://github.com/felixge/node-buffy/issues" "url": "https://github.com/felixge/node-buffy/issues"
}, },
"_id": "buffy@0.0.4", "_id": "buffy@0.0.4",
"dist": { "_from": "buffy@0.0.4"
"shasum": "c5d55f54042c6ae4d98ec06e8f9d38096cb55e7d"
},
"_from": "buffy@0.0.4",
"_resolved": "https://registry.npmjs.org/buffy/-/buffy-0.0.4.tgz"
} }
+4
Ver Arquivo
@@ -0,0 +1,4 @@
module.exports = process.env.CONNECT_MONGO_COV
? require('./lib-cov/connect-mongo')
: require('./lib/connect-mongo');
+261
Ver Arquivo
@@ -0,0 +1,261 @@
/*!
* connect-mongo
* Copyright(c) 2011 Casey Banner <kcbanner@gmail.com>
* MIT Licensed
*/
/**
* Module dependencies
*/
var mongo = require('mongodb');
var url = require('url');
/**
* Default options
*/
var defaultOptions = {host: '127.0.0.1',
port: 27017,
stringify: true,
collection: 'sessions',
auto_reconnect: false,
clear_interval: -1};
module.exports = function(connect) {
var Store = connect.session.Store;
/**
* Initialize MongoStore with the given `options`.
* Calls `callback` when db connection is ready (mainly for testing purposes).
*
* @param {Object} options
* @param {Function} callback
* @api public
*/
function MongoStore(options, callback) {
options = options || {};
Store.call(this, options);
if(options.url) {
var db_url = url.parse(options.url);
if (db_url.port) {
options.port = parseInt(db_url.port);
}
if (db_url.pathname != undefined) {
var pathname = db_url.pathname.split('/');
if (pathname.length >= 2) {
options.db = pathname[1];
}
if (pathname.length >= 3) {
options.collection = pathname[2];
}
}
if (db_url.hostname != undefined) {
options.host = db_url.hostname;
}
if (db_url.auth != undefined) {
var auth = db_url.auth.split(':');
if (auth.length >= 1) {
options.username = auth[0];
}
if (auth.length >= 2) {
options.password = auth[1];
}
}
}
if(!options.db) {
throw new Error('Required MongoStore option `db` missing');
}
this.db = new mongo.Db(options.db,
new mongo.Server(options.host || defaultOptions.host,
options.port || defaultOptions.port,
{
auto_reconnect: options.auto_reconnect ||
defaultOptions.auto_reconnect
}));
this.db_collection_name = options.collection || defaultOptions.collection;
if (options.hasOwnProperty('stringify') ? options.stringify : defaultOptions.stringify) {
this._serialize_session = JSON.stringify;
this._unserialize_session = JSON.parse;
} else {
this._serialize_session = function(x) { return x; };
this._unserialize_session = function(x) { return x; };
}
var self = this;
this._get_collection = function(callback) {
if (self.collection) {
callback && callback(self.collection);
} else {
self.db.collection(self.db_collection_name, function(err, collection) {
if (err) {
throw new Error('Error getting collection: ' + self.db_collection_name);
} else {
self.collection = collection;
var clear_interval = options.clear_interval || defaultOptions.clear_interval;
if (clear_interval > 0) {
self.clear_interval = setInterval(function() {
self.collection.remove({expires: {$lte: new Date()}});
}, clear_interval * 1000, self);
}
callback && callback(self.collection);
}
});
}
};
this.db.open(function(err, db) {
if (err) {
throw new Error('Error connecting to database');
}
if (options.username && options.password) {
db.authenticate(options.username, options.password, function () {
self._get_collection(callback);
});
} else {
self._get_collection(callback);
}
});
};
/**
* Inherit from `Store`.
*/
MongoStore.prototype.__proto__ = Store.prototype;
/**
* Attempt to fetch session by the given `sid`.
*
* @param {String} sid
* @param {Function} callback
* @api public
*/
MongoStore.prototype.get = function(sid, callback) {
var self = this;
this._get_collection(function(collection) {
collection.findOne({_id: sid}, function(err, session) {
try {
if (err) {
callback && callback(err, null);
} else {
if (session) {
if (!session.expires || new Date < session.expires) {
callback(null, self._unserialize_session(session.session));
} else {
self.destroy(sid, callback);
}
} else {
callback && callback();
}
}
} catch (err) {
callback && callback(err);
}
});
});
};
/**
* Commit the given `sess` object associated with the given `sid`.
*
* @param {String} sid
* @param {Session} sess
* @param {Function} callback
* @api public
*/
MongoStore.prototype.set = function(sid, session, callback) {
try {
var s = {_id: sid, session: this._serialize_session(session)};
if (session && session.cookie && session.cookie._expires) {
s.expires = new Date(session.cookie._expires);
}
this._get_collection(function(collection) {
collection.update({_id: sid}, s, {upsert: true, safe: true}, function(err, data) {
if (err) {
callback && callback(err);
} else {
callback && callback(null);
}
});
});
} catch (err) {
callback && callback(err);
}
};
/**
* Destroy the session associated with the given `sid`.
*
* @param {String} sid
* @param {Function} callback
* @api public
*/
MongoStore.prototype.destroy = function(sid, callback) {
this._get_collection(function(collection) {
collection.remove({_id: sid}, function() {
callback && callback();
});
});
};
/**
* Fetch number of sessions.
*
* @param {Function} callback
* @api public
*/
MongoStore.prototype.length = function(callback) {
this._get_collection(function(collection) {
collection.count({}, function(err, count) {
if (err) {
callback && callback(err);
} else {
callback && callback(null, count);
}
});
});
};
/**
* Clear all sessions.
*
* @param {Function} callback
* @api public
*/
MongoStore.prototype.clear = function(callback) {
this._get_collection(function(collection) {
collection.drop(function() {
callback && callback();
});
});
};
return MongoStore;
};
+3
Ver Arquivo
@@ -0,0 +1,3 @@
.git*
docs/
data/
+397
Ver Arquivo
@@ -0,0 +1,397 @@
0.9.9 2012-02-13
----------------
* Added createFromTime method on ObjectID to allow for queries against _id more easily using the timestamp.
* Db.close(true) now makes connection unusable as it's been force closed by app.
* Fixed mapReduce and group functions to correctly send slaveOk on queries.
* Fixes for find method to correctly work with find(query, fields, callback) (Issue #506).
* A fix for connection error handling when using the SSL on MongoDB.
0.9.8-7 2012-02-06
------------------
* Simplified findOne to use the find command instead of the custom code (Issue #498).
* BSON JS parser not also checks for _bsonType variable in case BSON object is in weird scope (Issue #495).
0.9.8-6 2012-02-04
------------------
* Removed the check for replicaset change code as it will never work with node.js.
0.9.8-5 2012-02-02
------------------
* Added geoNear command to Collection.
* Added geoHaystackSearch command to Collection.
* Added indexes command to collection to retrieve the indexes on a Collection.
* Added stats command to collection to retrieve the statistics on a Collection.
* Added listDatabases command to admin object to allow retrieval of all available dbs.
* Changed createCreateIndexCommand to work better with options.
* Fixed dereference method on Db class to correctly dereference Db reference objects.
* Moved connect object onto Db class(Db.connect) as well as keeping backward compatibility.
* Removed writeBuffer method from gridstore, write handles switching automatically now.
* Changed readBuffer to read on Gridstore, Gridstore now only supports Binary Buffers no Strings anymore.
* Moved Long class to bson directory.
0.9.8-4 2012-01-28
------------------
* Added reIndex command to collection and db level.
* Added support for $returnKey, $maxScan, $min, $max, $showDiskLoc, $comment to cursor and find/findOne methods.
* Added dropDups and v option to createIndex and ensureIndex.
* Added isCapped method to Collection.
* Added indexExists method to Collection.
* Added findAndRemove method to Collection.
* Fixed bug for replicaset connection when no active servers in the set.
* Fixed bug for replicaset connections when errors occur during connection.
* Merged in patch for BSON Number handling from Lee Salzman, did some small fixes and added test coverage.
0.9.8-3 2012-01-21
------------------
* Workaround for issue with Object.defineProperty (Issue #484)
* ObjectID generation with date does not set rest of fields to zero (Issue #482)
0.9.8-2 2012-01-20
------------------
* Fixed a missing this in the ReplSetServers constructor.
0.9.8-1 2012-01-17
------------------
* FindAndModify bug fix for duplicate errors (Issue #481)
0.9.8 2012-01-17
----------------
* Replicasets now correctly adjusts to live changes in the replicaset configuration on the servers, reconnecting correctly.
- Set the interval for checking for changes setting the replicaSetCheckInterval property when creating the ReplSetServers instance or on db.serverConfig.replicaSetCheckInterval. (default 1000 miliseconds)
* Fixes formattedOrderClause in collection.js to accept a plain hash as a parameter (Issue #469) https://github.com/tedeh
* Removed duplicate code for formattedOrderClause and moved to utils module
* Pass in poolSize for ReplSetServers to set default poolSize for new replicaset members
* Bug fix for BSON JS deserializer. Isolating the eval functions in separate functions to avoid V8 deoptimizations
* Correct handling of illegal BSON messages during deserialization
* Fixed Infinite loop when reading GridFs file with no chunks (Issue #471)
* Correctly update existing user password when using addUser (Issue #470)
0.9.7.3-5 2012-01-04
--------------------
* Fix for RegExp serialization for 0.4.X where typeof /regexp/ == 'function' vs in 0.6.X typeof /regexp/ == 'object'
* Don't allow keepAlive and setNoDelay for 0.4.X as it throws errors
0.9.7.3-4 2012-01-04
--------------------
* Chased down potential memory leak on findAndModify, Issue #467 (node.js removeAllListeners leaves the key in the _events object, node.js bug on eventlistener?, leads to extremely slow memory leak on listener object)
* Sanity checks for GridFS performance with benchmark added
0.9.7.3-3 2012-01-04
--------------------
* Bug fixes for performance issues going form 0.9.6.X to 0.9.7.X on linux
* BSON bug fixes for performance
0.9.7.3-2 2012-01-02
--------------------
* Fixed up documentation to reflect the preferred way of instantiating bson types
* GC bug fix for JS bson parser to avoid stop-and-go GC collection
0.9.7.3-1 2012-01-02
--------------------
* Fix to make db.bson_serializer and db.bson_deserializer work as it did previously
0.9.7.3 2011-12-30
--------------------
* Moved BSON_BINARY_SUBTYPE_DEFAULT from BSON object to Binary object and removed the BSON_BINARY_ prefixes
* Removed Native BSON types, C++ parser uses JS types (faster due to cost of crossing the JS-C++ barrier for each call)
* Added build fix for 0.4.X branch of Node.js where GetOwnPropertyNames is not defined in v8
* Fix for wire protocol parser for corner situation where the message is larger than the maximum socket buffer in node.js (Issue #464, #461, #447)
* Connection pool status set to connected on poolReady, isConnected returns false on anything but connected status (Issue #455)
0.9.7.2-5 2011-12-22
--------------------
* Brand spanking new Streaming Cursor support Issue #458 (https://github.com/christkv/node-mongodb-native/pull/458) thanks to Mr Aaron Heckmann
0.9.7.2-4 2011-12-21
--------------------
* Refactoring of callback code to work around performance regression on linux
* Fixed group function to correctly use the command mode as default
0.9.7.2-3 2011-12-18
--------------------
* Fixed error handling for findAndModify while still working for mongodb 1.8.6 (Issue #450).
* Allow for force send query to primary, pass option (read:'primary') on find command.
* ``find({a:1}, {read:'primary'}).toArray(function(err, items) {});``
0.9.7.2-2 2011-12-16
--------------------
* Fixes infinite streamRecords QueryFailure fix when using Mongos (Issue #442)
0.9.7.2-1 2011-12-16
--------------------
* ~10% perf improvement for ObjectId#toHexString (Issue #448, https://github.com/aheckmann)
* Only using process.nextTick on errors emitted on callbacks not on all parsing, reduces number of ticks in the driver
* Changed parsing off bson messages to use process.nextTick to do bson parsing in batches if the message is over 10K as to yield more time to the event look increasing concurrency on big mongoreply messages with multiple documents
0.9.7.2 2011-12-15
------------------
* Added SSL support for future version of mongodb (VERY VERY EXPERIMENTAL)
* pass in the ssl:true option to the server or replicaset server config to enable
* a bug either in mongodb or node.js does not allow for more than 1 connection pr db instance (poolSize:1).
* Added getTimestamp() method to objectID that returns a date object
* Added finalize function to collection.group
* function group (keys, condition, initial, reduce, finalize, command, callback)
* Reaper no longer using setTimeout to handle reaping. Triggering is done in the general flow leading to predictable behavior.
* reaperInterval, set interval for reaper (default 10000 miliseconds)
* reaperTimeout, set timeout for calls (default 30000 miliseconds)
* reaper, enable/disable reaper (default false)
* Work around for issues with findAndModify during high concurrency load, insure that the behavior is the same across the 1.8.X branch and 2.X branch of MongoDb
* Reworked multiple db's sharing same connection pool to behave correctly on error, timeout and close
* EnsureIndex command can be executed without a callback (Issue #438)
* Eval function no accepts options including nolock (Issue #432)
* eval(code, parameters, options, callback) (where options = {nolock:true})
0.9.7.1-4 2011-11-27
--------------------
* Replaced install.sh with install.js to install correctly on all supported os's
0.9.7.1-3 2011-11-27
--------------------
* Fixes incorrect scope for ensureIndex error wrapping (Issue #419) https://github.com/ritch
0.9.7.1-2 2011-11-27
--------------------
* Set statistical selection strategy as default for secondary choice.
0.9.7.1-1 2011-11-27
--------------------
* Better handling of single server reconnect (fixes some bugs)
* Better test coverage of single server failure
* Correct handling of callbacks on replicaset servers when firewall dropping packets, correct reconnect
0.9.7.1 2011-11-24
------------------
* Better handling of dead server for single server instances
* FindOne and find treats selector == null as {}, Issue #403
* Possible to pass in a strategy for the replicaset to pick secondary reader node
* parameter strategy
* ping (default), pings the servers and picks the one with the lowest ping time
* statistical, measures each request and pick the one with the lowest mean and std deviation
* Set replicaset read preference replicaset.setReadPreference()
* Server.READ_PRIMARY (use primary server for reads)
* Server.READ_SECONDARY (from a secondary server (uses the strategy set))
* tags, {object of tags}
* Added replay of commands issued to a closed connection when the connection is re-established
* Fix isConnected and close on unopened connections. Issue #409, fix by (https://github.com/sethml)
* Moved reaper to db.open instead of constructor (Issue #406)
* Allows passing through of socket connection settings to Server or ReplSetServer under the option socketOptions
* timeout = set seconds before connection times out (default 0)
* noDelay = Disables the Nagle algorithm (default true)
* keepAlive = Set if keepAlive is used (default 0, which means no keepAlive, set higher than 0 for keepAlive)
* encoding = ['ascii', 'utf8', or 'base64'] (default null)
* Fixes for handling of errors during shutdown off a socket connection
* Correctly applies socket options including timeout
* Cleanup of test management code to close connections correctly
* Handle parser errors better, closing down the connection and emitting an error
* Correctly emit errors from server.js only wrapping errors that are strings
0.9.7 2011-11-10
----------------
* Added priority setting to replicaset manager
* Added correct handling of passive servers in replicaset
* Reworked socket code for simpler clearer handling
* Correct handling of connections in test helpers
* Added control of retries on failure
* control with parameters retryMiliSeconds and numberOfRetries when creating a db instance
* Added reaper that will timeout and cleanup queries that never return
* control with parameters reaperInterval and reaperTimeout when creating a db instance
* Refactored test helper classes for replicaset tests
* Allows raw (no bson parser mode for insert, update, remove, find and findOne)
* control raw mode passing in option raw:true on the commands
* will return buffers with the binary bson objects
* Fixed memory leak in cursor.toArray
* Fixed bug in command creation for mongodb server with wrong scope of call
* Added db(dbName) method to db.js to allow for reuse of connections against other databases
* Serialization of functions in an object is off by default, override with parameter
* serializeFunctions [true/false] on db level, collection level or individual insert/update/findAndModify
* Added Long.fromString to c++ class and fixed minor bug in the code (Test case for $gt operator on 64-bit integers, Issue #394)
* FindOne and find now share same code execution and will work in the same manner, Issue #399
* Fix for tailable cursors, Issue #384
* Fix for Cursor rewind broken, Issue #389
* Allow Gridstore.exist to query using regexp, Issue #387, fix by (https://github.com/kaij)
* Updated documentation on https://github.com/christkv/node-mongodb-native
* Fixed toJSON methods across all objects for BSON, Binary return Base64 Encoded data
0.9.6-22 2011-10-15
-------------------
* Fixed bug in js bson parser that could cause wrong object size on serialization, Issue #370
* Fixed bug in findAndModify that did not throw error on replicaset timeout, Issue #373
0.9.6-21 2011-10-05
-------------------
* Reworked reconnect code to work correctly
* Handling errors in different parts of the code to ensure that it does not lock the connection
* Consistent error handling for Object.createFromHexString for JS and C++
0.9.6-20 2011-10-04
-------------------
* Reworked bson.js parser to get rid off Array.shift() due to it allocating new memory for each call. Speedup varies between 5-15% depending on doc
* Reworked bson.cc to throw error when trying to serialize js bson types
* Added MinKey, MaxKey and Double support for JS and C++ parser
* Reworked socket handling code to emit errors on unparsable messages
* Added logger option for Db class, lets you pass in a function in the shape
{
log : function(message, object) {},
error : function(errorMessage, errorObject) {},
debug : function(debugMessage, object) {},
}
Usage is new Db(new Server(..), {logger: loggerInstance})
0.9.6-19 2011-09-29
-------------------
* Fixing compatibility issues between C++ bson parser and js parser
* Added Symbol support to C++ parser
* Fixed socket handling bug for seldom misaligned message from mongodb
* Correctly handles serialization of functions using the C++ bson parser
0.9.6-18 2011-09-22
-------------------
* Fixed bug in waitForConnection that would lead to 100% cpu usage, Issue #352
0.9.6-17 2011-09-21
-------------------
* Fixed broken exception test causing bamboo to hang
* Handling correctly command+lastError when both return results as in findAndModify, Issue #351
0.9.6-16 2011-09-14
-------------------
* Fixing a bunch of issues with compatibility with MongoDB 2.0.X branch. Some fairly big changes in behavior from 1.8.X to 2.0.X on the server.
* Error Connection MongoDB V2.0.0 with Auth=true, Issue #348
0.9.6-15 2011-09-09
-------------------
* Fixed issue where pools would not be correctly cleaned up after an error, Issue #345
* Fixed authentication issue with secondary servers in Replicaset, Issue #334
* Duplicate replica-set servers when omitting port, Issue #341
* Fixing findAndModify to correctly work with Replicasets ensuring proper error handling, Issue #336
* Merged in code from (https://github.com/aheckmann) that checks for global variable leaks
0.9.6-14 2011-09-05
-------------------
* Minor fixes for error handling in cursor streaming (https://github.com/sethml), Issue #332
* Minor doc fixes
* Some more cursor sort tests added, Issue #333
* Fixes to work with 0.5.X branch
* Fix Db not removing reconnect listener from serverConfig, (https://github.com/sbrekken), Issue #337
* Removed node_events.h includes (https://github.com/jannehietamaki), Issue #339
* Implement correct safe/strict mode for findAndModify.
0.9.6-13 2011-08-24
-------------------
* Db names correctly error checked for illegal characters
0.9.6-12 2011-08-24
-------------------
* Nasty bug in GridFS if you changed the default chunk size
* Fixed error handling bug in findOne
0.9.6-11 2011-08-23
-------------------
* Timeout option not correctly making it to the cursor, Issue #320, Fix from (https://github.com/year2013)
* Fixes for memory leaks when using buffers and C++ parser
* Fixes to make tests pass on 0.5.X
* Cleanup of bson.js to remove duplicated code paths
* Fix for errors occurring in ensureIndex, Issue #326
* Removing require.paths to make tests work with the 0.5.X branch
0.9.6-10 2011-08-11
-------------------
* Specific type Double for capped collections (https://github.com/mbostock), Issue #312
* Decorating Errors with all all object info from Mongo (https://github.com/laurie71), Issue #308
* Implementing fixes for mongodb 1.9.1 and higher to make tests pass
* Admin validateCollection now takes an options argument for you to pass in full option
* Implemented keepGoing parameter for mongodb 1.9.1 or higher, Issue #310
* Added test for read_secondary count issue, merged in fix from (https://github.com/year2013), Issue #317
0.9.6-9
-------
* Bug fix for bson parsing the key '':'' correctly without crashing
0.9.6-8
-------
* Changed to using node.js crypto library MD5 digest
* Connect method support documented mongodb: syntax by (https://github.com/sethml)
* Support Symbol type for BSON, serializes to it's own type Symbol, Issue #302, #288
* Code object without scope serializing to correct BSON type
* Lot's of fixes to avoid double callbacks (https://github.com/aheckmann) Issue #304
* Long deserializes as Number for values in the range -2^53 to 2^53, Issue #305 (https://github.com/sethml)
* Fixed C++ parser to reflect JS parser handling of long deserialization
* Bson small optimizations
0.9.6-7 2011-07-13
------------------
* JS Bson deserialization bug #287
0.9.6-6 2011-07-12
------------------
* FindAndModify not returning error message as other methods Issue #277
* Added test coverage for $push, $pushAll and $inc atomic operations
* Correct Error handling for non 12/24 bit ids on Pure JS ObjectID class Issue #276
* Fixed terrible deserialization bug in js bson code #285
* Fix by andrewjstone to avoid throwing errors when this.primary not defined
0.9.6-5 2011-07-06
------------------
* Rewritten BSON js parser now faster than the C parser on my core2duo laptop
* Added option full to indexInformation to get all index info Issue #265
* Passing in ObjectID for new Gridstore works correctly Issue #272
0.9.6-4 2011-07-01
------------------
* Added test and bug fix for insert/update/remove without callback supplied
0.9.6-3 2011-07-01
------------------
* Added simple grid class called Grid with put, get, delete methods
* Fixed writeBuffer/readBuffer methods on GridStore so they work correctly
* Automatic handling of buffers when using write method on GridStore
* GridStore now accepts a ObjectID instead of file name for write and read methods
* GridStore.list accepts id option to return of file ids instead of filenames
* GridStore close method returns document for the file allowing user to reference _id field
0.9.6-2 2011-06-30
------------------
* Fixes for reconnect logic for server object (replays auth correctly)
* More testcases for auth
* Fixes in error handling for replicaset
* Fixed bug with safe parameter that would fail to execute safe when passing w or wtimeout
* Fixed slaveOk bug for findOne method
* Implemented auth support for replicaset and test cases
* Fixed error when not passing in rs_name
0.9.6-1 2011-06-25
------------------
* Fixes for test to run properly using c++ bson parser
* Fixes for dbref in native parser (correctly handles ref without db component)
* Connection fixes for replicasets to avoid runtime conditions in cygwin (https://github.com/vincentcr)
* Fixes for timestamp in js bson parser (distinct timestamp type now)
0.9.6 2011-06-21
----------------
* Worked around npm version handling bug
* Race condition fix for cygwin (https://github.com/vincentcr)
0.9.5-1 2011-06-21
------------------
* Extracted Timestamp as separate class for bson js parser to avoid instanceof problems
* Fixed driver strict mode issue
0.9.5 2011-06-20
----------------
* Replicaset support (failover and reading from secondary servers)
* Removed ServerPair and ServerCluster
* Added connection pool functionality
* Fixed serious bug in C++ bson parser where bytes > 127 would generate 2 byte sequences
* Allows for forcing the server to assign ObjectID's using the option {forceServerObjectId: true}
0.6.8
-----
* Removed multiple message concept from bson
* Changed db.open(db) to be db.open(err, db)
0.1 2010-01-30
--------------
* Initial release support of driver using native node.js interface
* Supports gridfs specification
* Supports admin functionality
+67
Ver Arquivo
@@ -0,0 +1,67 @@
NODE = node
NPM = npm
NODEUNIT = deps/nodeunit/bin/nodeunit
DOX = node_modules/dox/bin/dox
name = all
total: build_native
build_native:
$(MAKE) -C ./external-libs/bson all
build_native_debug:
$(MAKE) -C ./external-libs/bson all_debug
build_native_clang:
$(MAKE) -C ./external-libs/bson clang
build_native_clang_debug:
$(MAKE) -C ./external-libs/bson clang_debug
clean_native:
$(MAKE) -C ./external-libs/bson clean
test: build_native
@echo "\n == Run All tests minus replicaset tests=="
$(NODE) dev/tools/test_all.js --noreplicaset --boot
test_junit: build_native
@echo "\n == Run All tests minus replicaset tests=="
$(NODE) dev/tools/test_all.js --junit --noreplicaset
test_nodeunit_pure:
@echo "\n == Execute Test Suite using Pure JS BSON Parser == "
@$(NODEUNIT) test/ test/gridstore test/bson
test_js:
@$(NODEUNIT) $(TESTS)
test_nodeunit_replicaset_pure:
@echo "\n == Execute Test Suite using Pure JS BSON Parser == "
@$(NODEUNIT) test/replicaset
test_nodeunit_native:
@echo "\n == Execute Test Suite using Native BSON Parser == "
@TEST_NATIVE=TRUE $(NODEUNIT) test/ test/gridstore test/bson
test_nodeunit_replicaset_native:
@echo "\n == Execute Test Suite using Native BSON Parser == "
@TEST_NATIVE=TRUE $(NODEUNIT) test/replicaset
test_all: build_native
@echo "\n == Run All tests =="
$(NODE) dev/tools/test_all.js --boot
test_all_junit: build_native
@echo "\n == Run All tests =="
$(NODE) dev/tools/test_all.js --junit --boot
clean:
rm ./external-libs/bson/bson.node
rm -r ./external-libs/bson/build
generate_docs:
$(NODE) dev/tools/build-docs.js
make --directory=./docs/sphinx-docs --file=Makefile html
.PHONY: total
+408
Ver Arquivo
@@ -0,0 +1,408 @@
Install
========
To install the most recent release from npm, run:
npm install mongodb
That may give you a warning telling you that bugs['web'] should be bugs['url'], it would be safe to ignore it (this has been fixed in the development version)
To install from the latest from the repository, run::
npm install path/to/node-mongodb-native
Community
========
Check out the google group [node-mongodb-native](http://groups.google.com/group/node-mongodb-native) for questions/answers from users of the driver.
Introduction
========
This is a node.js driver for MongoDB. It's a port (or close to a port) of the library for ruby at http://github.com/mongodb/mongo-ruby-driver/.
A simple example of inserting a document.
var client = new Db('test', new Server("127.0.0.1", 27017, {})),
test = function (err, collection) {
collection.insert({a:2}, function(err, docs) {
collection.count(function(err, count) {
test.assertEquals(1, count);
});
// Locate all the entries using find
collection.find().toArray(function(err, results) {
test.assertEquals(1, results.length);
test.assertTrue(results.a === 2);
// Let's close the db
client.close();
});
});
};
client.open(function(err, p_client) {
client.collection('test_insert', test);
});
Data types
========
To store and retrieve the non-JSON MongoDb primitives ([ObjectID](http://www.mongodb.org/display/DOCS/Object+IDs), Long, Binary, [Timestamp](http://www.mongodb.org/display/DOCS/Timestamp+data+type), [DBRef](http://www.mongodb.org/display/DOCS/Database+References#DatabaseReferences-DBRef), Code).
In particular, every document has a unique `_id` which can be almost any type, and by default a 12-byte ObjectID is created. ObjectIDs can be represented as 24-digit hexadecimal strings, but you must convert the string back into an ObjectID before you can use it in the database. For example:
// Get the objectID type
var ObjectID = require('mongodb').ObjectID;
var idString = '4e4e1638c85e808431000003';
collection.findOne({_id: new ObjectID(idString)}, console.log) // ok
collection.findOne({_id: idString}, console.log) // wrong! callback gets undefined
Here are the constructors the non-Javascript BSON primitive types:
// Fetch the library
var mongo = require('mongodb');
// Create new instances of BSON types
new mongo.Long(numberString)
new mongo.ObjectID(hexString)
new mongo.Timestamp() // the actual unique number is generated on insert.
new mongo.DBRef(collectionName, id, dbName)
new mongo.Binary(buffer) // takes a string or Buffer
new mongo.Code(code, [context])
new mongo.Symbol(string)
new mongo.MinKey()
new mongo.MaxKey()
new mongo.Double(number) // Force double storage
The C/C++ bson parser/serializer
--------
From V0.8.0 to V0.9.6.9, the Javascript bson parser was slower than an optional C/C++ bson parser. As of V0.9.6.9+, due to performance improvements in the Javascript parser, the C/C++ parser is deprecated and is not installed by default anymore.
If you are running a version of this library has the C/C++ parser compiled, to enable the driver to use the C/C++ bson parser pass it the option native_parser:true like below
// using Deprecated native_parser:
var client = new Db('integration_tests_20',
new Server("127.0.0.1", 27017),
{native_parser:true});
The C++ parser uses the js objects both for serialization and deserialization.
GitHub information
========
The source code is available at http://github.com/christkv/node-mongodb-native.
You can either clone the repository or download a tarball of the latest release.
Once you have the source you can test the driver by running
$ make test
in the main directory. You will need to have a mongo instance running on localhost for the integration tests to pass.
Examples
========
For examples look in the examples/ directory. You can execute the examples using node.
$ cd examples
$ node queries.js
GridStore
=========
The GridStore class allows for storage of binary files in mongoDB using the mongoDB defined files and chunks collection definition.
For more information have a look at [Gridstore](https://github.com/christkv/node-mongodb-native/blob/master/docs/gridfs.md)
Replicasets
===========
For more information about how to connect to a replicaset have a look at [Replicasets](https://github.com/christkv/node-mongodb-native/blob/master/docs/replicaset.md)
Primary Key Factories
--------
Defining your own primary key factory allows you to generate your own series of id's
(this could f.ex be to use something like ISBN numbers). The generated the id needs to be a 12 byte long "string".
Simple example below
// Custom factory (need to provide a 12 byte array);
CustomPKFactory = function() {}
CustomPKFactory.prototype = new Object();
CustomPKFactory.createPk = function() {
return new ObjectID("aaaaaaaaaaaa");
}
var p_client = new Db('integration_tests_20', new Server("127.0.0.1", 27017, {}), {'pk':CustomPKFactory});
p_client.open(function(err, p_client) {
p_client.dropDatabase(function(err, done) {
p_client.createCollection('test_custom_key', function(err, collection) {
collection.insert({'a':1}, function(err, docs) {
collection.find({'_id':new ObjectID("aaaaaaaaaaaa")}, function(err, cursor) {
cursor.toArray(function(err, items) {
test.assertEquals(1, items.length);
// Let's close the db
p_client.close();
});
});
});
});
});
});
Strict mode
--------
Each database has an optional strict mode. If it is set then asking for a collection
that does not exist will return an Error object in the callback. Similarly if you
attempt to create a collection that already exists. Strict is provided for convenience.
var error_client = new Db('integration_tests_', new Server("127.0.0.1", 27017, {auto_reconnect: false}), {strict:true});
test.assertEquals(true, error_client.strict);
error_client.open(function(err, error_client) {
error_client.collection('does-not-exist', function(err, collection) {
test.assertTrue(err instanceof Error);
test.assertEquals("Collection does-not-exist does not exist. Currently in strict mode.", err.message);
});
error_client.createCollection('test_strict_access_collection', function(err, collection) {
error_client.collection('test_strict_access_collection', function(err, collection) {
test.assertTrue(collection instanceof Collection);
// Let's close the db
error_client.close();
});
});
});
Documentation
========
If this document doesn't answer your questions, see the source of
[Collection](https://github.com/christkv/node-mongodb-native/blob/master/lib/mongodb/collection.js)
or [Cursor](https://github.com/christkv/node-mongodb-native/blob/master/lib/mongodb/cursor.js),
or the documentation at MongoDB for query and update formats.
Find
--------
The find method is actually a factory method to create
Cursor objects. A Cursor lazily uses the connection the first time
you call `nextObject`, `each`, or `toArray`.
The basic operation on a cursor is the `nextObject` method
that fetches the next matching document from the database. The convenience
methods `each` and `toArray` call `nextObject` until the cursor is exhausted.
Signatures:
var cursor = collection.find(query, [fields], options);
cursor.sort(fields).limit(n).skip(m).
cursor.nextObject(function(err, doc) {});
cursor.each(function(err, doc) {});
cursor.toArray(function(err, docs) {});
cursor.rewind() // reset the cursor to its initial state.
Useful chainable methods of cursor. These can optionally be options of `find` instead of method calls:
* `.limit(n).skip(m)` to control paging.
* `.sort(fields)` Order by the given fields. There are several equivalent syntaxes:
* `.sort({field1: -1, field2: 1})` descending by field1, then ascending by field2.
* `.sort([['field1', 'desc'], ['field2', 'asc']])` same as above
* `.sort([['field1', 'desc'], 'field2'])` same as above
* `.sort('field1')` ascending by field1
Other options of `find`:
* `fields` the fields to fetch (to avoid transferring the entire document)
* `tailable` if true, makes the cursor [tailable](http://www.mongodb.org/display/DOCS/Tailable+Cursors).
* `batchSize` The number of the subset of results to request the database
to return for every request. This should initially be greater than 1 otherwise
the database will automatically close the cursor. The batch size can be set to 1
with `batchSize(n, function(err){})` after performing the initial query to the database.
* `hint` See [Optimization: hint](http://www.mongodb.org/display/DOCS/Optimization#Optimization-Hint).
* `explain` turns this into an explain query. You can also call
`explain()` on any cursor to fetch the explanation.
* `snapshot` prevents documents that are updated while the query is active
from being returned multiple times. See more
[details about query snapshots](http://www.mongodb.org/display/DOCS/How+to+do+Snapshotted+Queries+in+the+Mongo+Database).
* `timeout` if false, asks MongoDb not to time out this cursor after an
inactivity period.
For information on how to create queries, see the
[MongoDB section on querying](http://www.mongodb.org/display/DOCS/Querying).
var mongodb = require('mongodb');
var server = new mongodb.Server("127.0.0.1", 27017, {});
new mongodb.Db('test', server, {}).open(function (error, client) {
if (error) throw error;
var collection = new mongodb.Collection(client, 'test_collection');
collection.find({}, {limit:10}).toArray(function(err, docs) {
console.dir(docs);
});
});
Insert
--------
Signature:
collection.insert(docs, options, [callback]);
where `docs` can be a single document or an array of documents.
Useful options:
* `safe:true` Should always set if you have a callback.
See also: [MongoDB docs for insert](http://www.mongodb.org/display/DOCS/Inserting).
var mongodb = require('mongodb');
var server = new mongodb.Server("127.0.0.1", 27017, {});
new mongodb.Db('test', server, {}).open(function (error, client) {
if (error) throw error;
var collection = new mongodb.Collection(client, 'test_collection');
collection.insert({hello: 'world'}, {safe:true},
function(err, objects) {
if (err) console.warn(err.message);
if (err && err.message.indexOf('E11000 ') !== -1) {
// this _id was already inserted in the database
}
});
});
Note that there's no reason to pass a callback to the insert or update commands
unless you use the `safe:true` option. If you don't specify `safe:true`, then
your callback will be called immediately.
Update; update and insert (upsert)
--------
The update operation will update the first document that matches your query
(or all documents that match if you use `multi:true`).
If `safe:true`, `upsert` is not set, and no documents match, your callback
will be given an error.
See the [MongoDB docs](http://www.mongodb.org/display/DOCS/Updating) for
the modifier (`$inc`, `$set`, `$push`, etc.) formats.
Signature:
collection.update(criteria, objNew, options, [callback]);
Useful options:
* `safe:true` Should always set if you have a callback.
* `multi:true` If set, all matching documents are updated, not just the first.
* `upsert:true` Atomically inserts the document if no documents matched.
Example for `update`:
var mongodb = require('mongodb');
var server = new mongodb.Server("127.0.0.1", 27017, {});
new mongodb.Db('test', server, {}).open(function (error, client) {
if (error) throw error;
var collection = new mongodb.Collection(client, 'test_collection');
collection.update({hi: 'here'}, {$set: {hi: 'there'}}, {safe:true},
function(err) {
if (err) console.warn(err.message);
else console.log('successfully updated');
});
});
Find and modify
--------
`findAndModify` is like `update`, but it also gives the updated document to
your callback. But there are a few key differences between findAndModify and
update:
1. The signatures differ.
2. You can only findAndModify a single item, not multiple items.
Signature:
collection.findAndModify(query, sort, update, options, callback)
The sort parameter is used to specify which object to operate on, if more than
one document matches. It takes the same format as the cursor sort (see
Connection.find above).
See the
[MongoDB docs for findAndModify](http://www.mongodb.org/display/DOCS/findAndModify+Command)
for more details.
Useful options:
* `remove:true` set to a true to remove the object before returning
* `new:true` set to true if you want to return the modified object rather than the original. Ignored for remove.
* `upsert:true` Atomically inserts the document if no documents matched.
Example for `findAndModify`:
var mongodb = require('mongodb');
var server = new mongodb.Server("127.0.0.1", 27017, {});
new mongodb.Db('test', server, {}).open(function (error, client) {
if (error) throw error;
var collection = new mongodb.Collection(client, 'test_collection');
collection.findAndModify({hello: 'world'}, [['_id','asc']], {$set: {hi: 'there'}}, {},
function(err, object) {
if (err) console.warn(err.message);
else console.dir(object); // undefined if no matching object exists.
});
});
Save
--------
The `save` method is a shorthand for upsert if the document contains an
`_id`, or an insert if there is no `_id`.
Sponsors
========
Just as Felix Geisendörfer I'm also working on the driver for my own startup and this driver is a big project that also benefits other companies who are using MongoDB.
If your company could benefit from a even better-engineered node.js mongodb driver I would appreciate any type of sponsorship you may be able to provide. All the sponsors will get a lifetime display in this readme, priority support and help on problems and votes on the roadmap decisions for the driver. If you are interested contact me on [christkv AT g m a i l.com](mailto:christkv@gmail.com) for details.
And I'm very thankful for code contributions. If you are interested in working on features please contact me so we can discuss API design and testing.
Release Notes
=============
See HISTORY
Credits
========
1. [10gen](http://github.com/mongodb/mongo-ruby-driver/)
2. [Google Closure Library](http://code.google.com/closure/library/)
3. [Jonas Raoni Soares Silva](http://jsfromhell.com/classes/binary-parser)
Contributors
=============
Aaron Heckmann, Christoph Pojer, Pau Ramon Revilla, Nathan White, Emmerman, Seth LaForge, Boris Filipov, Stefan Schärmeli, Tedde Lundgren, renctan, Sergey Ukustov, Ciaran Jessup, kuno, srimonti, Erik Abele, Pratik Daga, Slobodan Utvic, Kristina Chodorow, Yonathan Randolph, Brian Noguchi, Sam Epstein, James Harrison Fisher, Vladimir Dronnikov, Ben Hockey, Henrik Johansson, Simon Weare, Alex Gorbatchev, Shimon Doodkin, Kyle Mueller, Eran Hammer-Lahav, Marcin Ciszak, François de Metz, Vinay Pulim, nstielau, Adam Wiggins, entrinzikyl, Jeremy Selier, Ian Millington, Public Keating, andrewjstone, Christopher Stott, Corey Jewett, brettkiefer, Rob Holland, Senmiao Liu, heroic, gitfy
License
========
Copyright 2009 - 2010 Christian Amor Kvalheim.
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.
+25
Ver Arquivo
@@ -0,0 +1,25 @@
TODO for jan 4 - 2011
- Chase down potential memory leak in findAndModify
- Check compatibility for gridFS between python and js
- Ensure Gridfs speed is comparable to other solutions
- Map out python replicaset driver functionality
ACCOMPLISHED jan 4 - 2011
+ Chase down potential memory leak in findAndModify
+ Check compatibility for gridFS between python and js
+ Ensure Gridfs speed is comparable to other solutions
0.9.7.4-dev
- Amortize documents (only deserialize when accessed)
- Mongo tests to handle special Mongos situations
- If a secondary server disappears don't kill the driver connection
- Check for new servers coming online (a new secondary server etc)
- http://api.mongodb.org/python/current/api/pymongo/index.html#pymongo.ReadPreference
--------------------------------------------------------------------------------------------* Allow secondary read with no master
* Add lowest response time selection of read server for replicaset using a selectable strategy. First one being using the ping command response time
* Allow execution of multiple commands against the same server connection when having a connection pool
* Implement tag support for replicasets
* Change bson c++ parser to use js objects instead of native c++ objects
* Whole stack benchmark with profiling to locate where the driver spends time
* Change bson c++ parser to be stackless to look at performance difference
@@ -0,0 +1,4 @@
*.swp
*.swo
*.swu
node_modules/
@@ -0,0 +1,42 @@
0.2.1 / 10-18-2011
==================
* fixed; package.json dependency versioning
0.2.0 / 10-11-2011
==================
* added; node v0.5 / v0.6 support
0.1.3 / 09-22-2011
==================
* use old school node engine format in package.json
0.1.2 / 09-08-2011
==================
* changed; utilize detectNew in middleware
* updated; docs
0.1.1 / 09-07-2011
==================
* added; #detectNew method
0.1.0 / 09-06-2011
==================
* added; #ignore method
* added; multiple instance support
0.0.2 / 09-06-2011
==================
* added; allow whitelisting by variable name
0.0.1 / 09-03-2011
==================
* initial release
@@ -0,0 +1,6 @@
test:
@NODE_ENV=test ./node_modules/expresso/bin/expresso \
test/index.js
.PHONY: test
+118
Ver Arquivo
@@ -0,0 +1,118 @@
# Gleak
Global variable leak detection for Node.js
var detector = require('gleak')();
detector.detect().forEach(function (name) {
console.warn('found global leak: %s', name);
});
Global variable leaks in javascript can bite you when you least
expect it. Do something about it now and run this module after
your tests, after HTTP requests, and after you brush your teeth.
## Detectable
As demonstrated, gleak comes with the `detect` method which returns
an array of all found variable leaks.
Often times we want to run the detector many times, progressively
checking for any new leaks that occurred since we last checked. In
this scenario we can utilize the `detectNew` method.
var detector = require('gleak')();
x = 1;
detector.detectNew(); // ['x']
detector.detectNew(); // []
y = 3;
detector.detectNew(); // ['y']
## Configurable:
Gleak comes configured for Node.js and will ignore built-ins by default
but you can configure it however your like:
var gleak = require('gleak')();
gleak.ignore(app, db);
The `gleak.ignore` method allows us to add globals we want to ignore
while safely ignoring duplicates.
`gleak.whitelist` is an array that holds all globals we are ignoring.
You can push to it or blow it away completely with your own list too.
var gleak = require('gleak')();
gleak.whitelist = [dnode, cluster];
Changes to your whitelists do not impact any global settings. For example:
var gleak = require('gleak');
var g1 = gleak();
var g2 = gleak();
g1.ignore(myglobal);
g2.whitelist.indexOf(myglobal) === -1;
`g2` does not inherit changes to `g1`s whitelist.
## Printable
If you don't want anything fancy and want to quickly dump all
global leaks to your console, just call `print()`.
var gleak = require('gleak')();
gleak.print(); // prints "Gleak!: leakedVarName"
## Expressable
We might want to print leaked variables to our console after each
HTTP request. This is especially helpful during development.
To accomplish this we can utilize the bundled [express](http://expressjs.com) middleware:
var app = express.createServer();
app.use(gleak.middleware());
What if we want to output to a different stream than stderr?
app.use(gleak.middleware(stream));
How about customized logging formats?
app.use(gleak.middleware('\x1b[31mLeak!\x1b[0m %s'));
Combining formats and streams?
app.use(gleak.middleware(stream, '\x1b[31mLeak!\x1b[0m %s'));
## Installable
npm install gleak
### Node version
Compatible with Node >=v0.4 <0.5.0
## License
(The MIT License)
Copyright (c) 2011 [Aaron Heckmann](aaron.heckmann+github@gmail.com)
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
'Software'), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+190
Ver Arquivo
@@ -0,0 +1,190 @@
/**
* Gleak - detect global var leaks.
* @api public
*/
module.exports = exports = function gleak () {
return new Gleak;
}
/**
* Version.
* @api public
*/
exports.version = '0.2.1';
/**
* Express middleware.
* @api public
*/
exports.middleware = function gleakMiddleware (stream, format) {
var g = new Gleak;
if (!format) {
switch (typeof stream) {
case 'string':
format = stream;
stream = process.stderr;
break;
case 'undefined':
format = g.format;
stream = process.stderr;
break;
default:
format = g.format;
}
}
setTimeout(print, 1000);
function print () {
g.detectNew().forEach(function (leak) {
stream.write(format.replace(/%s/, leak) + '\n');
});
}
return function gleakMiddleware (req, res, next) {
if (res._gleak) return next();
res._gleak = true;
var send = res.send;
res.send = function () {
res.send = send;
res.send.apply(res, arguments);
print();
}
next();
}
}
/**
* Gleak constructor
* @api private
*/
function Gleak () {
this.whitelist = this.whitelist.slice();
}
/**
* Whitelisted globals.
* @api public
*/
// v0.4.x
Gleak.prototype.whitelist = [
setTimeout
, setInterval
, clearTimeout
, clearInterval
, console
, Buffer
, process
, global
, GLOBAL
, root
];
// check for new globals in >= v0.5x
var version = process.version.replace(/^v/, '').split('.');
if ('0' === version[0] && version[1] > 4) {
Gleak.prototype.whitelist.push(
ArrayBuffer
, Int8Array
, Uint8Array
, Int16Array
, Uint16Array
, Int32Array
, Uint32Array
, Float32Array
, Float64Array
, DataView
, 'errno' // node >= v0.5.x hack
)
}
/**
* Default format.
* @api public
*/
Gleak.prototype.format = '\x1b[31mGleak!:\x1b[0m %s';
/**
* Detects global variable leaks.
* @api public
*/
Gleak.prototype.detect = function detect () {
var whitelist = this.whitelist
, ret = []
Object.keys(global).forEach(function (key) {
var w = whitelist.length
, bad = true
, white
while (w--) {
white = whitelist[w];
if (global[key] === white || 'string' === typeof white && key === white) {
bad = false;
break;
}
}
if (bad) ret.push(key);
});
return ret;
};
/**
* Return only new leaks since the last time `detectNew`
* was run.
* @api public
*/
Gleak.prototype.detectNew = function detectNew () {
var found = this.found || (this.found = []);
var ret = [];
this.detect().forEach(function (leak) {
if (~found.indexOf(leak)) return;
found.push(leak);
ret.push(leak);
});
return ret;
}
/**
* Prints all gleaks to stderr.
* @api public
*/
Gleak.prototype.print = function print () {
var format = this.format;
this.detect().forEach(function (leak) {
console.error(format, leak);
});
}
/**
* Add items to the whitelist disallowing duplicates.
* @api public
*/
Gleak.prototype.ignore = function ignore () {
var i = arguments.length;
while (i--) {
if (~this.whitelist.indexOf(arguments[i])) continue;
this.whitelist.push(arguments[i]);
}
return this;
}
@@ -0,0 +1,22 @@
{
"author": "Aaron Heckmann <aaron.heckmann+github@gmail.com>",
"name": "gleak",
"description": "Node global variable leak detector",
"version": "0.2.1",
"repository": {
"type": "git"
, "url": "git://github.com/aheckmann/gleak.git"
},
"main": "./index.js",
"scripts": {
"test": "make test"
},
"engines": {
"node": ">=0.4.0"
},
"dependencies": {},
"devDependencies": {
"express": ">=2.0.0"
, "expresso": "0.7.5"
}
}
@@ -0,0 +1,215 @@
var assert = require('assert')
var express = require('express')
var gleak = require('../index')
exports['version exists'] = function () {
assert.equal('string', typeof gleak.version);
}
exports['middleware exists'] = function () {
assert.equal('function', typeof gleak.middleware);
}
exports['gleak is a function'] = function () {
assert.equal('function', typeof gleak);
}
exports['default format is correct'] = function () {
var g = gleak();
assert.equal('\x1b[31mGleak!:\x1b[0m %s', g.format);
}
exports['whitelist is an array'] = function () {
var g = gleak();
assert.ok(Array.isArray(g.whitelist));
}
exports['setTimeout is a default'] = function () {
var g = gleak();
assert.ok(~g.whitelist.indexOf(setTimeout));
};
exports['setInterval is a default'] = function () {
var g = gleak();
assert.ok(~g.whitelist.indexOf(setInterval));
};
exports['clearTimeout is a default'] = function () {
var g = gleak();
assert.ok(~g.whitelist.indexOf(clearTimeout));
};
exports['clearInterval is a default'] = function () {
var g = gleak();
assert.ok(~g.whitelist.indexOf(clearInterval));
};
exports['console is a default'] = function () {
var g = gleak();
assert.ok(~g.whitelist.indexOf(console));
};
exports['Buffer is a default'] = function () {
var g = gleak();
assert.ok(~g.whitelist.indexOf(Buffer));
};
exports['process is a default'] = function () {
var g = gleak();
assert.ok(~g.whitelist.indexOf(process));
};
exports['global is a default'] = function () {
var g = gleak();
assert.ok(~g.whitelist.indexOf(global));
};
exports['whitelist is mutable'] = function () {
var g = gleak();
var i = g.whitelist.push(assert);
assert.ok(~g.whitelist.indexOf(assert));
g.whitelist.splice(i-1, 1);
assert.ok(!~g.whitelist.indexOf(assert));
}
exports['#detect is a function'] = function () {
var g = gleak();
assert.ok('function' === typeof g.detect);
}
exports['detect()'] = function () {
var g = gleak();
var found = g.detect();
assert.ok(Array.isArray(found));
assert.ok(0 === found.length);
haha = "lol"
assert.ok(1 === g.detect().length);
assert.equal("haha", g.detect()[0]);
}
exports['unknown values can be whitelisted by passing strings'] = function () {
var g = gleak();
ignoreme = 1;
assert.ok(~g.detect().indexOf('ignoreme'));
g.whitelist.push('ignoreme');
assert.ok(!~g.detect().indexOf('ignoreme'));
delete global.ignoreme;
}
exports['#ignore'] = function () {
var g = gleak();
assert.equal('function', typeof g.ignore);
}
exports['ignore identical whitelisted values'] = function () {
var g = gleak();
var len = g.whitelist.length;
var an = 'another';
g.ignore('another', 'another', 'another', an);
assert.equal(len + 1, g.whitelist.length);
}
exports['#print'] = function () {
var g = gleak();
var write = console.error;
var times = 0;
haha = "heh";
console.error = function (format, item) {
assert.equal(g.format, format);
assert.equal("haha", item);
++times;
}
g.print();
console.error = write;
assert.equal(1, times);
}
exports['whitelists are seperate from other instances'] = function () {
var g1 = gleak()
, g2 = gleak();
g1.ignore('the', 'bad');
assert.ok(~g1.whitelist.indexOf('the'));
assert.ok(!~g2.whitelist.indexOf('the'));
}
exports['formats are seperate from other instances'] = function () {
var g1 = gleak()
, g2 = gleak();
g1.format = "different %s";
assert.ok(~g1.format !== g1.format);
}
exports['#detectNew'] = function () {
var g = gleak();
assert.equal('function', typeof g.detectNew);
var found = g.detectNew();
assert.equal(true, Array.isArray(found));
assert.equal(found.length, 1);
assert.equal(g.detectNew().length, 0);
zombocom = 'welcome';
found = g.detectNew();
assert.equal(found.length, 1);
assert.equal(found[0], 'zombocom');
assert.equal(g.detectNew().length, 0);
delete global.zombocom;
}
exports['test middleware'] = function (beforeExit) {
var called = false;
var req = {};
var res = { send: function (x) { assert.equal(x, 'yes'); called = true; }};
var m = gleak.middleware();
m(req, res, function(){});
assert.equal(res._gleak, true);
res.send('yes');
assert.equal(true, called);
// another leak
meToo = 47;
// mock stream
function makeStream (tests) {
return {
i: 0
, write: function (data) {
assert.equal(tests[this.i], data);
++this.i;
}
}
}
var app = express.createServer();
var sout = [
'\x1b[31mGleak!:\x1b[0m haha\n'
, '\x1b[31mGleak!:\x1b[0m meToo\n'
];
var stream1 = makeStream(sout);
app.get('/stream', gleak.middleware(stream1), function (req, res, next) {
res.send('passed a stream');
});
var both = [
'yes : haha\n'
, 'yes : meToo\n'
];
var stream2 = makeStream(both);
app.get('/formatstream', gleak.middleware(stream2, 'yes : %s'), function (req, res, next) {
res.send('passed format and stream');
});
assert.response(app,
{ url: '/stream' }
, { status: 200
, body: 'passed a stream' })
assert.response(app,
{ url: '/formatstream' }
, { status: 200
, body: 'passed format and stream' })
beforeExit(function () {
assert.equal(stream1.i, 2);
assert.equal(stream2.i, 2);
});
}
@@ -0,0 +1,3 @@
dist
stamp-build
test/fixtures/dir2
@@ -0,0 +1,68 @@
Nodeunit contributors (sorted alphabeticaly)
============================================
* **[Alex Gorbatchev](https://github.com/alexgorbatchev)**
* Deeper default object inspection
* Timeout to ensure flushing of console output (default reporter)
* **[Alex Wolfe](https://github.com/alexkwolfe)**
* HTML test reporter
* **[Caolan McMahon](https://github.com/caolan)**
* Author and maintainer
* Most features develpopment
* **[Carl Fürstenberg](https://github.com/azatoth)**
* Debian-friendly Makefile, supports both 'node' and 'nodejs' executables
* Sandbox utility
* Minimal test reporter
* **[Gerad Suyderhoud](https://github.com/gerad)**
* First comand-line tool
* **[Kadir Pekel](https://github.com/kadirpekel)**
* Improvements to default test reporter
* HTTP test utility
* **[Λlisue](https://github.com/lambdalisue)**
* Add machineout reporter
* **[Matthias Lübken](https://github.com/luebken)**
* Utility functions for tracking incomplete tests on exit
* **[Oleg Efimov](https://github.com/Sannis)**
* Adding 'make lint' and fixing nodelint errors
* Option parsing, --help text and config file support
* Reporters option for command-line tool
* **[Orlando Vazquez](https://github.com/orlandov)**
* Added jUnit XML reporter
* **[Ryan Dahl](https://github.com/ry)**
* Add package.json
* **[Sam Stephenson](https://github.com/sstephenson)**
* Coffee-script support
* **[Thomas Mayfield](https://github.com/thegreatape)**
* Async setUp and tearDown support for testCase
* **[Maciej Małecki](https://github.com/mmalecki)**
* Removal of `testCase`
**[Full contributors list](https://github.com/caolan/nodeunit/contributors).**
@@ -0,0 +1,19 @@
Copyright (c) 2010 Caolan McMahon
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
@@ -0,0 +1,176 @@
PACKAGE = nodeunit
NODEJS = $(if $(shell test -f /usr/bin/nodejs && echo "true"),nodejs,node)
PREFIX ?= /usr/local
BINDIR ?= $(PREFIX)/bin
DATADIR ?= $(PREFIX)/share
MANDIR ?= $(PREFIX)/share/man
LIBDIR ?= $(PREFIX)/lib
NODEJSLIBDIR ?= $(LIBDIR)/$(NODEJS)
BUILDDIR = dist
DOCS = $(shell find doc -name '*.md' \
|sed 's|.md|.1|g' \
|sed 's|doc/|man1/|g' \
)
$(shell if [ ! -d $(BUILDDIR) ]; then mkdir $(BUILDDIR); fi)
all: build doc
browser:
# super hacky build script for browser version!
mkdir -p $(BUILDDIR)/browser
rm -rf $(BUILDDIR)/browser/*
# build browser version of nodeunit.js
cat share/license.js >> $(BUILDDIR)/browser/nodeunit.js
echo "nodeunit = (function(){" >> $(BUILDDIR)/browser/nodeunit.js
cat deps/json2.js >> $(BUILDDIR)/browser/nodeunit.js
# make assert global
echo "var assert = this.assert = {};" >> $(BUILDDIR)/browser/nodeunit.js
echo "var types = {};" >> $(BUILDDIR)/browser/nodeunit.js
echo "var core = {};" >> $(BUILDDIR)/browser/nodeunit.js
echo "var nodeunit = {};" >> $(BUILDDIR)/browser/nodeunit.js
echo "var reporter = {};" >> $(BUILDDIR)/browser/nodeunit.js
cat deps/async.js >> $(BUILDDIR)/browser/nodeunit.js
echo "(function(exports){" >> $(BUILDDIR)/browser/nodeunit.js
cat lib/assert.js >> $(BUILDDIR)/browser/nodeunit.js
echo "})(assert);" >> $(BUILDDIR)/browser/nodeunit.js
echo "(function(exports){" >> $(BUILDDIR)/browser/nodeunit.js
cat lib/types.js >> $(BUILDDIR)/browser/nodeunit.js
echo "})(types);" >> $(BUILDDIR)/browser/nodeunit.js
echo "(function(exports){" >> $(BUILDDIR)/browser/nodeunit.js
cat lib/core.js >> $(BUILDDIR)/browser/nodeunit.js
echo "})(core);" >> $(BUILDDIR)/browser/nodeunit.js
echo "(function(exports){" >> $(BUILDDIR)/browser/nodeunit.js
cat lib/reporters/browser.js >> $(BUILDDIR)/browser/nodeunit.js
echo "})(reporter);" >> $(BUILDDIR)/browser/nodeunit.js
echo "nodeunit = core;" >> $(BUILDDIR)/browser/nodeunit.js
echo "nodeunit.assert = assert;" >> $(BUILDDIR)/browser/nodeunit.js
echo "nodeunit.reporter = reporter;" >> $(BUILDDIR)/browser/nodeunit.js
echo "nodeunit.run = reporter.run;" >> $(BUILDDIR)/browser/nodeunit.js
echo "return nodeunit; })();" >> $(BUILDDIR)/browser/nodeunit.js
cp $(BUILDDIR)/browser/nodeunit.js $(BUILDDIR)/browser/.nodeunit.js
sed "/\@REMOVE_LINE_FOR_BROWSER/d" <$(BUILDDIR)/browser/.nodeunit.js > $(BUILDDIR)/browser/nodeunit.js
rm $(BUILDDIR)/browser/.nodeunit.js
# copy nodeunit.css
cp share/nodeunit.css $(BUILDDIR)/browser/nodeunit.css
# create nodeunit.min.js
node_modules/uglify-js/bin/uglifyjs $(BUILDDIR)/browser/nodeunit.js > $(BUILDDIR)/browser/nodeunit.min.js
# create test scripts
mkdir -p $(BUILDDIR)/browser/test
cp test/test.html $(BUILDDIR)/browser/test/test.html
# test-base.js
echo "(function (exports) {" > $(BUILDDIR)/browser/test/test-base.js
cat test/test-base.js >> $(BUILDDIR)/browser/test/test-base.js
echo "})(this.test_base = {});" >> $(BUILDDIR)/browser/test/test-base.js
cp $(BUILDDIR)/browser/test/test-base.js $(BUILDDIR)/browser/.test-base.js
sed "/\@REMOVE_LINE_FOR_BROWSER/d" <$(BUILDDIR)/browser/.test-base.js > $(BUILDDIR)/browser/test/test-base.js
rm $(BUILDDIR)/browser/.test-base.js
# test-runmodule.js
echo "(function (exports) {" > $(BUILDDIR)/browser/test/test-runmodule.js
cat test/test-runmodule.js >> $(BUILDDIR)/browser/test/test-runmodule.js
echo "})(this.test_runmodule = {});" >> $(BUILDDIR)/browser/test/test-runmodule.js
cp $(BUILDDIR)/browser/test/test-runmodule.js $(BUILDDIR)/browser/.test-runmodule.js
sed "/\@REMOVE_LINE_FOR_BROWSER/d" <$(BUILDDIR)/browser/.test-runmodule.js > $(BUILDDIR)/browser/test/test-runmodule.js
rm $(BUILDDIR)/browser/.test-runmodule.js
# test-runtest.js
echo "(function (exports) {" > $(BUILDDIR)/browser/test/test-runtest.js
cat test/test-runtest.js >> $(BUILDDIR)/browser/test/test-runtest.js
echo "})(this.test_runtest = {});" >> $(BUILDDIR)/browser/test/test-runtest.js
cp $(BUILDDIR)/browser/test/test-runtest.js $(BUILDDIR)/browser/.test-runtest.js
sed "/\@REMOVE_LINE_FOR_BROWSER/d" <$(BUILDDIR)/browser/.test-runtest.js > $(BUILDDIR)/browser/test/test-runtest.js
rm $(BUILDDIR)/browser/.test-runtest.js
# test-testcase.js
echo "(function (exports) {" > $(BUILDDIR)/browser/test/test-testcase.js
cat test/test-testcase.js >> $(BUILDDIR)/browser/test/test-testcase.js
echo "})(this.test_testcase = {});" >> $(BUILDDIR)/browser/test/test-testcase.js
cp $(BUILDDIR)/browser/test/test-testcase.js $(BUILDDIR)/browser/.test-testcase.js
sed "/\@REMOVE_LINE_FOR_BROWSER/d" <$(BUILDDIR)/browser/.test-testcase.js > $(BUILDDIR)/browser/test/test-testcase.js
rm $(BUILDDIR)/browser/.test-testcase.js
# test-testcase-legacy.js
echo "(function (exports) {" > $(BUILDDIR)/browser/test/test-testcase-legacy.js
cat test/test-testcase-legacy.js >> $(BUILDDIR)/browser/test/test-testcase-legacy.js
echo "})(this.test_testcase_legacy = {});" >> $(BUILDDIR)/browser/test/test-testcase-legacy.js
cp $(BUILDDIR)/browser/test/test-testcase-legacy.js $(BUILDDIR)/browser/.test-testcase-legacy.js
sed "/\@REMOVE_LINE_FOR_BROWSER/d" <$(BUILDDIR)/browser/.test-testcase-legacy.js > $(BUILDDIR)/browser/test/test-testcase-legacy.js
rm $(BUILDDIR)/browser/.test-testcase-legacy.js
# copy nodeunit.js to dist/browser/test to make it easier for me to host and
# run on windows VMs with IE
cp $(BUILDDIR)/browser/nodeunit.js $(BUILDDIR)/browser/test/nodeunit.js
cp $(BUILDDIR)/browser/nodeunit.css $(BUILDDIR)/browser/test/nodeunit.css
commonjs:
# super hacky build script for browser commonjs version!
##### make commonjs browser module ######
mkdir -p $(BUILDDIR)/commonjs
mkdir -p $(BUILDDIR)/commonjs/deps
cp deps/json2.js $(BUILDDIR)/commonjs/deps
cp deps/async.js $(BUILDDIR)/commonjs/deps
echo "var async = require('async');" >> $(BUILDDIR)/commonjs/nodeunit.js
echo "var assert = {};" >> $(BUILDDIR)/commonjs/nodeunit.js
echo "var types = {};" >> $(BUILDDIR)/commonjs/nodeunit.js
echo "var core = {};" >> $(BUILDDIR)/commonjs/nodeunit.js
echo "var nodeunit = {};" >> $(BUILDDIR)/commonjs/nodeunit.js
echo "var reporter = {};" >> $(BUILDDIR)/commonjs/nodeunit.js
echo "(function(exports){" >> $(BUILDDIR)/commonjs/nodeunit.js
cat lib/assert.js >> $(BUILDDIR)/commonjs/nodeunit.js
echo "})(assert);" >> $(BUILDDIR)/commonjs/nodeunit.js
echo "(function(exports){" >> $(BUILDDIR)/commonjs/nodeunit.js
cat lib/types.js >> $(BUILDDIR)/commonjs/nodeunit.js
echo "})(types);" >> $(BUILDDIR)/commonjs/nodeunit.js
echo "(function(exports){" >> $(BUILDDIR)/commonjs/nodeunit.js
cat lib/core.js >> $(BUILDDIR)/commonjs/nodeunit.js
echo "})(core);" >> $(BUILDDIR)/commonjs/nodeunit.js
echo "module.exports = core;" >> $(BUILDDIR)/commonjs/nodeunit.js
echo "(function(exports, nodeunit){" >> $(BUILDDIR)/commonjs/nodeunit.js
cat lib/reporters/browser.js >> $(BUILDDIR)/commonjs/nodeunit.js
echo "})(reporter, module.exports);" >> $(BUILDDIR)/commonjs/nodeunit.js
echo "module.exports.assert = assert;" >> $(BUILDDIR)/commonjs/nodeunit.js
echo "module.exports.reporter = reporter;" >> $(BUILDDIR)/commonjs/nodeunit.js
echo "module.exports.run = reporter.run;" >> $(BUILDDIR)/commonjs/nodeunit.js
sed -i "/\@REMOVE_LINE_FOR_BROWSER/d" $(BUILDDIR)/commonjs/nodeunit.js
sed -i "/\@REMOVE_LINE_FOR_COMMONJS/d" $(BUILDDIR)/commonjs/nodeunit.js
##### end of commonjs browser module #####
build: stamp-build
stamp-build: $(wildcard deps/* lib/*.js)
touch $@;
mkdir -p $(BUILDDIR)/nodeunit
cp -R bin node_modules deps index.js lib package.json share $(BUILDDIR)/nodeunit
printf '#!/bin/sh\n$(NODEJS) $(NODEJSLIBDIR)/$(PACKAGE)/bin/nodeunit $$@' > $(BUILDDIR)/nodeunit.sh
test:
$(NODEJS) ./bin/nodeunit test
install: build
install -d $(NODEJSLIBDIR)
cp -a $(BUILDDIR)/nodeunit $(NODEJSLIBDIR)
install -m 0755 $(BUILDDIR)/nodeunit.sh $(BINDIR)/nodeunit
install -d $(MANDIR)/man1/
cp -a man1/nodeunit.1 $(MANDIR)/man1/
uninstall:
rm -rf $(NODEJSLIBDIR)/nodeunit $(NODEJSLIBDIR)/nodeunit.js $(BINDIR)/nodeunit
rm -rf $(MANDIR)/man1/nodeunit.1
clean:
rm -rf $(BUILDDIR) stamp-build
lint:
nodelint --config nodelint.cfg ./index.js ./bin/nodeunit ./bin/nodeunit.json ./lib/*.js ./lib/reporters/*.js ./test/*.js
doc: man1 $(DOCS)
@true
man1:
@if ! test -d man1 ; then mkdir -p man1 ; fi
# use `npm install ronn` for this to work.
man1/%.1: doc/%.md
ronn --roff $< > $@
.PHONY: browser test install uninstall build all
@@ -0,0 +1,443 @@
Nodeunit
========
Simple syntax, powerful tools. Nodeunit provides easy async unit testing for
node.js and the browser.
* Simple to use
* Just export the tests from a module
* Works with node.js and in the browser.
* Helps you avoid common pitfalls when testing asynchronous code
* Easy to add test cases with setUp and tearDown functions if you wish
* Flexible reporters for custom output, built-in support for HTML and jUnit XML
* Allows the use of mocks and stubs
__Contributors__
* [alexgorbatchev](https://github.com/alexgorbatchev)
* [alexkwolfe](https://github.com/alexkwolfe)
* [azatoth](https://github.com/azatoth)
* [kadirpekel](https://github.com/kadirpekel)
* [lambdalisue](https://github.com/lambdalisue)
* [luebken](https://github.com/luebken)
* [orlandov](https://github.com/orlandov)
* [Sannis](https://github.com/Sannis)
* [sstephenson](https://github.com/sstephenson)
* [thegreatape](https://github.com/thegreatape)
* [mmalecki](https://github.com/mmalecki)
* and thanks to [cjohansen](https://github.com/cjohansen) for input and advice
on implementing setUp and tearDown functions. See
[cjohansen's fork](https://github.com/cjohansen/nodeunit).
Also, check out gerad's [nodeunit-dsl](https://github.com/gerad/nodeunit-dsl)
project, which implements a 'pretty dsl on top of nodeunit'.
More contributor information can be found in the
[CONTRIBUTORS.md](https://github.com/caolan/nodeunit/blob/master/CONTRIBUTORS.md)
file.
Usage
-----
Here is an example unit test module:
exports.testSomething = function(test){
test.expect(1);
test.ok(true, "this assertion should pass");
test.done();
};
exports.testSomethingElse = function(test){
test.ok(false, "this assertion should fail");
test.done();
};
When run using the included test runner, this will output the following:
<img src="https://github.com/caolan/nodeunit/raw/master/img/example_fail.png" />
Installation
------------
There are two options for installing nodeunit:
1. Clone / download nodeunit from [github](https://github.com/caolan/nodeunit),
then:
make && sudo make install
2. Install via npm:
npm install nodeunit
API Documentation
-----------------
Nodeunit uses the functions available in the node.js
[assert module](http://nodejs.org/docs/v0.4.2/api/assert.html):
* __ok(value, [message])__ - Tests if value is a true value.
* __equal(actual, expected, [message])__ - Tests shallow, coercive equality
with the equal comparison operator ( == ).
* __notEqual(actual, expected, [message])__ - Tests shallow, coercive
non-equality with the not equal comparison operator ( != ).
* __deepEqual(actual, expected, [message])__ - Tests for deep equality.
* __notDeepEqual(actual, expected, [message])__ - Tests for any deep
inequality.
* __strictEqual(actual, expected, [message])__ - Tests strict equality, as
determined by the strict equality operator ( === )
* __notStrictEqual(actual, expected, [message])__ - Tests strict non-equality,
as determined by the strict not equal operator ( !== )
* __throws(block, [error], [message])__ - Expects block to throw an error.
* __doesNotThrow(block, [error], [message])__ - Expects block not to throw an
error.
* __ifError(value)__ - Tests if value is not a false value, throws if it is a
true value. Useful when testing the first argument, error in callbacks.
Nodeunit also provides the following functions within tests:
* __expect(amount)__ - Specify how many assertions are expected to run within a
test. Very useful for ensuring that all your callbacks and assertions are
run.
* __done()__ - Finish the current test function, and move on to the next. ALL
tests should call this!
Nodeunit aims to be simple and easy to learn. This is achieved through using
existing structures (such as node.js modules) to maximum effect, and reducing
the API where possible, to make it easier to digest.
Tests are simply exported from a module, but they are still run in the order
they are defined.
__Note:__ Users of old nodeunit versions may remember using ok, equals and same
in the style of qunit, instead of the assert functions above. These functions
still exist for backwards compatibility, and are simply aliases to their assert
module counterparts.
Asynchronous Testing
--------------------
When testing asynchronous code, there are a number of sharp edges to watch out
for. Thankfully, nodeunit is designed to help you avoid as many of these
pitfalls as possible. For the most part, testing asynchronous code in nodeunit
_just works_.
### Tests run in series
While running tests in parallel seems like a good idea for speeding up your
test suite, in practice I've found it means writing much more complicated
tests. Because of node's module cache, running tests in parallel means mocking
and stubbing is pretty much impossible. One of the nicest things about testing
in javascript is the ease of doing stubs:
var _readFile = fs.readFile;
fs.readFile = function(path, callback){
// its a stub!
};
// test function that uses fs.readFile
// we're done
fs.readFile = _readFile;
You cannot do this when running tests in parallel. In order to keep testing as
simple as possible, nodeunit avoids it. Thankfully, most unit-test suites run
fast anyway.
### Explicit ending of tests
When testing async code its important that tests end at the correct point, not
just after a given number of assertions. Otherwise your tests can run short,
ending before all assertions have completed. Its important to detect too
many assertions as well as too few. Combining explicit ending of tests with
an expected number of assertions helps to avoid false test passes, so be sure
to use the test.expect() method at the start of your test functions, and
test.done() when finished.
Groups, setUp and tearDown
--------------------------
Nodeunit allows the nesting of test functions:
exports.test1 = function (test) {
...
}
exports.group = {
test2: function (test) {
...
},
test3: function (test) {
...
}
}
This would be run as:
test1
group - test2
group - test3
Using these groups, Nodeunit allows you to define a `setUp` function, which is
run before each test, and a `tearDown` function, which is run after each test
calls `test.done()`:
module.exports = {
setUp: function (callback) {
this.foo = 'bar';
callback();
},
tearDown: function (callback) {
// clean up
callback();
},
test1: function (test) {
test.equals(this.foo, 'bar');
test.done();
}
};
In this way, its possible to have multiple groups of tests in a module, each
group with its own setUp and tearDown functions.
Running Tests
-------------
Nodeunit comes with a basic command-line test runner, which can be installed
using 'sudo make install'. Example usage:
nodeunit testmodule1.js testfolder [...]
The default test reporter uses color output, because I think that's more fun :) I
intend to add a no-color option in future. To give you a feeling of the fun you'll
be having writing tests, lets fix the example at the start of the README:
<img src="https://github.com/caolan/nodeunit/raw/master/img/example_pass.png" />
Ahhh, Doesn't that feel better?
When using the included test runner, it will exit using the failed number of
assertions as the exit code. Exiting with 0 when all tests pass.
### Command-line Options
* __--reporter FILE__ - you can set the test reporter to a custom module or
on of the modules in nodeunit/lib/reporters, when omitted, the default test runner
is used.
* __--list-reporters__ - list available build-in reporters.
* __--config FILE__ - load config options from a JSON file, allows
the customisation of color schemes for the default test reporter etc. See
bin/nodeunit.json for current available options.
* __--version__ or __-v__ - report nodeunit version
* __--help__ - show nodeunit help
Running tests in the browser
----------------------------
Nodeunit tests can also be run inside the browser. For example usage, see
the examples/browser folder. The basic syntax is as follows:
__test.html__
<html>
<head>
<title>Example Test Suite</title>
<link rel="stylesheet" href="nodeunit.css" type="text/css" />
<script src="nodeunit.js"></script>
<script src="suite1.js"></script>
<script src="suite2.js"></script>
</head>
<body>
<h1 id="nodeunit-header>Example Test Suite</h1>
<script>
nodeunit.run({
'Suite One': suite1,
'Suite Two': suite2
});
</script>
</body>
</html>
Here, suite1 and suite2 are just object literals containing test functions or
groups, as would be returned if you did require('test-suite') in node.js:
__suite1.js__
this.suite1 = {
'example test': function (test) {
test.ok(true, 'everything is ok');
test.done();
}
};
If you wish to use a commonjs format for your test suites (using exports), it is
up to you to define the commonjs tools for the browser. There are a number of
alternatives and its important it fits with your existing code, which is
why nodeunit does not currently provide this out of the box.
In the example above, the tests will run when the page is loaded.
The browser-version of nodeunit.js is created in dist/browser when you do, 'make
browser'. You'll need [UglifyJS](https://github.com/mishoo/UglifyJS) installed in
order for it to automatically create nodeunit.min.js.
Adding nodeunit to Your Projects
--------------------------------
If you don't want people to have to install the nodeunit command-line tool,
you'll want to create a script that runs the tests for your project with the
correct require paths set up. Here's an example test script, that assumes you
have nodeunit in a suitably located node_modules directory.
#!/usr/bin/env node
var reporter = require('nodeunit').reporters.default;
reporter.run(['test']);
If you're using git, you might find it useful to include nodeunit as a
submodule. Using submodules makes it easy for developers to download nodeunit
and run your test suite, without cluttering up your repository with
the source code. To add nodeunit as a git submodule do the following:
git submodule add git://github.com/caolan/nodeunit.git node_modules/nodeunit
This will add nodeunit to the node_modules folder of your project. Now, when
cloning the repository, nodeunit can be downloaded by doing the following:
git submodule init
git submodule update
Let's update the test script above with a helpful hint on how to get nodeunit,
if its missing:
#!/usr/bin/env node
try {
var reporter = require('nodeunit').reporters.default;
}
catch(e) {
console.log("Cannot find nodeunit module.");
console.log("You can download submodules for this project by doing:");
console.log("");
console.log(" git submodule init");
console.log(" git submodule update");
console.log("");
process.exit();
}
process.chdir(__dirname);
reporter.run(['test']);
Now if someone attempts to run your test suite without nodeunit installed they
will be prompted to download the submodules for your project.
Built-in Test Reporters
-----------------------
* __default__ - The standard reporter seen in the nodeunit screenshots
* __minimal__ - Pretty, minimal output, shows errors and progress only
* __html__ - Outputs a HTML report to stdout
* __junit__ - Creates jUnit compatible XML reports, which can be used with
continuous integration tools such as [Hudson](http://hudson-ci.org/).
* __machineout__ - Simple reporter for machine analysis. There is [nodeunit.vim](https://github.com/lambdalisue/nodeunit.vim)
which is useful for TDD on VIM
Writing a Test Reporter
---------------------
Nodeunit exports runTest(fn, options), runModule(mod, options) and
runFiles(paths, options). You'll most likely want to run test suites from
files, which can be done using the latter function. The _options_ argument can
contain callbacks which run during testing. Nodeunit provides the following
callbacks:
* __moduleStart(name)__ - called before a module is tested
* __moduleDone(name, assertions)__ - called once all test functions within the
module have completed (see assertions object reference below)
ALL tests within the module
* __testStart(name)__ - called before a test function is run
* __testDone(name, assertions)__ - called once a test function has completed
(by calling test.done())
* __log(assertion)__ - called whenever an assertion is made (see assertion
object reference below)
* __done(assertions)__ - called after all tests/modules are complete
The __assertion__ object:
* __passed()__ - did the assertion pass?
* __failed()__ - did the assertion fail?
* __error__ - the AssertionError if the assertion failed
* __method__ - the nodeunit assertion method used (ok, same, equals...)
* __message__ - the message the assertion method was called with (optional)
The __assertionList__ object:
* An array-like object with the following new attributes:
* __failures()__ - the number of assertions which failed
* __duration__ - the time taken for the test to complete in msecs
For a reference implementation of a test reporter, see lib/reporters/default.js in
the nodeunit project directory.
Sandbox utility
---------------
This is a function which evaluates JavaScript files in a sandbox and returns the
context. The sandbox function can be used for testing client-side code or private
un-exported functions within a module.
var sandbox = require('nodeunit').utils.sandbox;
var example = sandbox('example.js');
__sandbox(files, sandbox)__ - Evaluates JavaScript files in a sandbox, returning
the context. The first argument can either be a single filename or an array of
filenames. If multiple filenames are given their contents are concatenated before
evalution. The second argument is an optional context to use for the sandbox.
Running the nodeunit Tests
--------------------------
The tests for nodeunit are written using nodeunit itself as the test framework.
However, the module test-base.js first does some basic tests using the assert
module to ensure that test functions are actually run, and a basic level of
nodeunit functionality is available.
To run the nodeunit tests do:
make test
__Note:__ There was a bug in node v0.2.0 causing the tests to hang, upgrading
to v0.2.1 fixes this.
__machineout__ reporter
----------------------------------------------
The default reporter is really readable for human but for machinally analysis.
When you want to analyze the output of nodeunit, use __machineout__ reporter and you will get
<img src="https://github.com/caolan/nodeunit/raw/master/img/example_machineout.png" />
nodeunit with vim
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
There is [nodeunit.vim](https://github.com/lambdalisue/nodeunit.vim) so you can use nodeunit with VIM.
That compiler use __machineout__ reporter and it is useful to use with [vim-makegreen](https://github.com/reinh/vim-makegreen)
Contributing
------------
Contributions to the project are most welcome, so feel free to fork and improve.
When submitting a pull request, please run 'make lint' first to ensure
we're following a consistent coding style.
+125
Ver Arquivo
@@ -0,0 +1,125 @@
#!/usr/bin/env node
var
fs = require('fs'),
path = require('path');
// TODO: remove this when https://github.com/joyent/node/pull/1312
// lands in core.
//
// Until then, use console.log from npm (https://gist.github.com/1077544)
require('../deps/console.log');
//require.paths.push(process.cwd());
var args = (process.ARGV || process.argv).slice(2);
var files = [];
var testrunner,
config_file,
config_param_found = false,
output_param_found = false,
reporter_file = 'default',
reporter_param_found = false,
testspec_param_found = false;
var usage = "Usage: nodeunit [options] testmodule1.js testfolder [...] \n" +
"Options:\n\n" +
" --config FILE the path to a JSON file with options\n" +
" --reporter FILE optional path to a reporter file to customize the output\n" +
" --list-reporters list available build-in reporters\n" +
" -t name, specify a test to run\n" +
" -h, --help display this help and exit\n" +
" -v, --version output version information and exit";
// load default options
var content = fs.readFileSync(__dirname + '/nodeunit.json', 'utf8');
var options = JSON.parse(content);
// a very basic pseudo --options parser
args.forEach(function (arg) {
if (arg.slice(0, 9) === "--config=") {
config_file = arg.slice(9);
} else if (arg === '--config') {
config_param_found = true;
} else if (config_param_found) {
config_file = arg;
config_param_found = false;
} else if (arg.slice(0, 9) === "--output=") {
options.output = arg.slice(9);
} else if (arg === '--output') {
output_param_found = true;
} else if (output_param_found) {
options.output = arg;
output_param_found = false;
} else if (arg.slice(0, 11) === "--reporter=") {
reporter_file = arg.slice(11);
} else if (arg === '--reporter') {
reporter_param_found = true;
} else if (reporter_param_found) {
reporter_file = arg;
reporter_param_found = false;
} else if (arg === '-t') {
testspec_param_found = true;
} else if (testspec_param_found) {
options.testspec = arg;
testspec_param_found = false;
} else if (arg === '--list-reporters') {
var reporters = fs.readdirSync(__dirname + '/../lib/reporters');
reporters = reporters.filter(function (reporter_file) {
return (/\.js$/).test(reporter_file);
}).map(function (reporter_file) {
return reporter_file.replace(/\.js$/, '');
}).filter(function (reporter_file) {
return reporter_file !== 'index';
});
console.log('Build-in reporters: ');
reporters.forEach(function (reporter_file) {
var reporter = require('../lib/reporters/' + reporter_file);
console.log(' * ' + reporter_file + (reporter.info ? ': ' + reporter.info : ''));
});
process.exit(0);
} else if ((arg === '-v') || (arg === '--version')) {
var content = fs.readFileSync(__dirname + '/../package.json', 'utf8');
var pkg = JSON.parse(content);
console.log(pkg.version);
process.exit(0);
} else if ((arg === '-h') || (arg === '--help')) {
console.log(usage);
process.exit(0);
} else {
files.push(arg);
}
});
if (files.length === 0) {
console.log('Files required.');
console.log(usage);
process.exit(1);
}
if (config_file) {
content = fs.readFileSync(config_file, 'utf8');
var custom_options = JSON.parse(content);
for (var option in custom_options) {
if (typeof option === 'string') {
options[option] = custom_options[option];
}
}
}
var builtin_reporters = require(__dirname + '/../lib/reporters');
if (reporter_file in builtin_reporters) {
testrunner = builtin_reporters[reporter_file];
}
else {
testrunner = require(reporter_file);
}
testrunner.run(files, options, function(err) {
if (err) {
process.exit(1);
}
});
@@ -0,0 +1,10 @@
{
"error_prefix": "\u001B[31m",
"error_suffix": "\u001B[39m",
"ok_prefix": "\u001B[32m",
"ok_suffix": "\u001B[39m",
"bold_prefix": "\u001B[1m",
"bold_suffix": "\u001B[22m",
"assertion_prefix": "\u001B[35m",
"assertion_suffix": "\u001B[39m"
}
@@ -0,0 +1,623 @@
/*global setTimeout: false, console: false */
(function () {
var async = {};
// global on the server, window in the browser
var root = this,
previous_async = root.async;
if (typeof module !== 'undefined' && module.exports) {
module.exports = async;
}
else {
root.async = async;
}
async.noConflict = function () {
root.async = previous_async;
return async;
};
//// cross-browser compatiblity functions ////
var _forEach = function (arr, iterator) {
if (arr.forEach) {
return arr.forEach(iterator);
}
for (var i = 0; i < arr.length; i += 1) {
iterator(arr[i], i, arr);
}
};
var _map = function (arr, iterator) {
if (arr.map) {
return arr.map(iterator);
}
var results = [];
_forEach(arr, function (x, i, a) {
results.push(iterator(x, i, a));
});
return results;
};
var _reduce = function (arr, iterator, memo) {
if (arr.reduce) {
return arr.reduce(iterator, memo);
}
_forEach(arr, function (x, i, a) {
memo = iterator(memo, x, i, a);
});
return memo;
};
var _keys = function (obj) {
if (Object.keys) {
return Object.keys(obj);
}
var keys = [];
for (var k in obj) {
if (obj.hasOwnProperty(k)) {
keys.push(k);
}
}
return keys;
};
var _indexOf = function (arr, item) {
if (arr.indexOf) {
return arr.indexOf(item);
}
for (var i = 0; i < arr.length; i += 1) {
if (arr[i] === item) {
return i;
}
}
return -1;
};
//// exported async module functions ////
//// nextTick implementation with browser-compatible fallback ////
if (typeof process === 'undefined' || !(process.nextTick)) {
async.nextTick = function (fn) {
setTimeout(fn, 0);
};
}
else {
async.nextTick = process.nextTick;
}
async.forEach = function (arr, iterator, callback) {
if (!arr.length) {
return callback();
}
var completed = 0;
_forEach(arr, function (x) {
iterator(x, function (err) {
if (err) {
callback(err);
callback = function () {};
}
else {
completed += 1;
if (completed === arr.length) {
callback();
}
}
});
});
};
async.forEachSeries = function (arr, iterator, callback) {
if (!arr.length) {
return callback();
}
var completed = 0;
var iterate = function () {
iterator(arr[completed], function (err) {
if (err) {
callback(err);
callback = function () {};
}
else {
completed += 1;
if (completed === arr.length) {
callback();
}
else {
iterate();
}
}
});
};
iterate();
};
var doParallel = function (fn) {
return function () {
var args = Array.prototype.slice.call(arguments);
return fn.apply(null, [async.forEach].concat(args));
};
};
var doSeries = function (fn) {
return function () {
var args = Array.prototype.slice.call(arguments);
return fn.apply(null, [async.forEachSeries].concat(args));
};
};
var _asyncMap = function (eachfn, arr, iterator, callback) {
var results = [];
arr = _map(arr, function (x, i) {
return {index: i, value: x};
});
eachfn(arr, function (x, callback) {
iterator(x.value, function (err, v) {
results[x.index] = v;
callback(err);
});
}, function (err) {
callback(err, results);
});
};
async.map = doParallel(_asyncMap);
async.mapSeries = doSeries(_asyncMap);
// reduce only has a series version, as doing reduce in parallel won't
// work in many situations.
async.reduce = function (arr, memo, iterator, callback) {
async.forEachSeries(arr, function (x, callback) {
iterator(memo, x, function (err, v) {
memo = v;
callback(err);
});
}, function (err) {
callback(err, memo);
});
};
// inject alias
async.inject = async.reduce;
// foldl alias
async.foldl = async.reduce;
async.reduceRight = function (arr, memo, iterator, callback) {
var reversed = _map(arr, function (x) {
return x;
}).reverse();
async.reduce(reversed, memo, iterator, callback);
};
// foldr alias
async.foldr = async.reduceRight;
var _filter = function (eachfn, arr, iterator, callback) {
var results = [];
arr = _map(arr, function (x, i) {
return {index: i, value: x};
});
eachfn(arr, function (x, callback) {
iterator(x.value, function (v) {
if (v) {
results.push(x);
}
callback();
});
}, function (err) {
callback(_map(results.sort(function (a, b) {
return a.index - b.index;
}), function (x) {
return x.value;
}));
});
};
async.filter = doParallel(_filter);
async.filterSeries = doSeries(_filter);
// select alias
async.select = async.filter;
async.selectSeries = async.filterSeries;
var _reject = function (eachfn, arr, iterator, callback) {
var results = [];
arr = _map(arr, function (x, i) {
return {index: i, value: x};
});
eachfn(arr, function (x, callback) {
iterator(x.value, function (v) {
if (!v) {
results.push(x);
}
callback();
});
}, function (err) {
callback(_map(results.sort(function (a, b) {
return a.index - b.index;
}), function (x) {
return x.value;
}));
});
};
async.reject = doParallel(_reject);
async.rejectSeries = doSeries(_reject);
var _detect = function (eachfn, arr, iterator, main_callback) {
eachfn(arr, function (x, callback) {
iterator(x, function (result) {
if (result) {
main_callback(x);
}
else {
callback();
}
});
}, function (err) {
main_callback();
});
};
async.detect = doParallel(_detect);
async.detectSeries = doSeries(_detect);
async.some = function (arr, iterator, main_callback) {
async.forEach(arr, function (x, callback) {
iterator(x, function (v) {
if (v) {
main_callback(true);
main_callback = function () {};
}
callback();
});
}, function (err) {
main_callback(false);
});
};
// any alias
async.any = async.some;
async.every = function (arr, iterator, main_callback) {
async.forEach(arr, function (x, callback) {
iterator(x, function (v) {
if (!v) {
main_callback(false);
main_callback = function () {};
}
callback();
});
}, function (err) {
main_callback(true);
});
};
// all alias
async.all = async.every;
async.sortBy = function (arr, iterator, callback) {
async.map(arr, function (x, callback) {
iterator(x, function (err, criteria) {
if (err) {
callback(err);
}
else {
callback(null, {value: x, criteria: criteria});
}
});
}, function (err, results) {
if (err) {
return callback(err);
}
else {
var fn = function (left, right) {
var a = left.criteria, b = right.criteria;
return a < b ? -1 : a > b ? 1 : 0;
};
callback(null, _map(results.sort(fn), function (x) {
return x.value;
}));
}
});
};
async.auto = function (tasks, callback) {
callback = callback || function () {};
var keys = _keys(tasks);
if (!keys.length) {
return callback(null);
}
var completed = [];
var listeners = [];
var addListener = function (fn) {
listeners.unshift(fn);
};
var removeListener = function (fn) {
for (var i = 0; i < listeners.length; i += 1) {
if (listeners[i] === fn) {
listeners.splice(i, 1);
return;
}
}
};
var taskComplete = function () {
_forEach(listeners, function (fn) {
fn();
});
};
addListener(function () {
if (completed.length === keys.length) {
callback(null);
}
});
_forEach(keys, function (k) {
var task = (tasks[k] instanceof Function) ? [tasks[k]]: tasks[k];
var taskCallback = function (err) {
if (err) {
callback(err);
// stop subsequent errors hitting callback multiple times
callback = function () {};
}
else {
completed.push(k);
taskComplete();
}
};
var requires = task.slice(0, Math.abs(task.length - 1)) || [];
var ready = function () {
return _reduce(requires, function (a, x) {
return (a && _indexOf(completed, x) !== -1);
}, true);
};
if (ready()) {
task[task.length - 1](taskCallback);
}
else {
var listener = function () {
if (ready()) {
removeListener(listener);
task[task.length - 1](taskCallback);
}
};
addListener(listener);
}
});
};
async.waterfall = function (tasks, callback) {
if (!tasks.length) {
return callback();
}
callback = callback || function () {};
var wrapIterator = function (iterator) {
return function (err) {
if (err) {
callback(err);
callback = function () {};
}
else {
var args = Array.prototype.slice.call(arguments, 1);
var next = iterator.next();
if (next) {
args.push(wrapIterator(next));
}
else {
args.push(callback);
}
async.nextTick(function () {
iterator.apply(null, args);
});
}
};
};
wrapIterator(async.iterator(tasks))();
};
async.parallel = function (tasks, callback) {
callback = callback || function () {};
if (tasks.constructor === Array) {
async.map(tasks, function (fn, callback) {
if (fn) {
fn(function (err) {
var args = Array.prototype.slice.call(arguments, 1);
if (args.length <= 1) {
args = args[0];
}
callback.call(null, err, args || null);
});
}
}, callback);
}
else {
var results = {};
async.forEach(_keys(tasks), function (k, callback) {
tasks[k](function (err) {
var args = Array.prototype.slice.call(arguments, 1);
if (args.length <= 1) {
args = args[0];
}
results[k] = args;
callback(err);
});
}, function (err) {
callback(err, results);
});
}
};
async.series = function (tasks, callback) {
callback = callback || function () {};
if (tasks.constructor === Array) {
async.mapSeries(tasks, function (fn, callback) {
if (fn) {
fn(function (err) {
var args = Array.prototype.slice.call(arguments, 1);
if (args.length <= 1) {
args = args[0];
}
callback.call(null, err, args || null);
});
}
}, callback);
}
else {
var results = {};
async.forEachSeries(_keys(tasks), function (k, callback) {
tasks[k](function (err) {
var args = Array.prototype.slice.call(arguments, 1);
if (args.length <= 1) {
args = args[0];
}
results[k] = args;
callback(err);
});
}, function (err) {
callback(err, results);
});
}
};
async.iterator = function (tasks) {
var makeCallback = function (index) {
var fn = function () {
if (tasks.length) {
tasks[index].apply(null, arguments);
}
return fn.next();
};
fn.next = function () {
return (index < tasks.length - 1) ? makeCallback(index + 1): null;
};
return fn;
};
return makeCallback(0);
};
async.apply = function (fn) {
var args = Array.prototype.slice.call(arguments, 1);
return function () {
return fn.apply(
null, args.concat(Array.prototype.slice.call(arguments))
);
};
};
var _concat = function (eachfn, arr, fn, callback) {
var r = [];
eachfn(arr, function (x, cb) {
fn(x, function (err, y) {
r = r.concat(y || []);
cb(err);
});
}, function (err) {
callback(err, r);
});
};
async.concat = doParallel(_concat);
async.concatSeries = doSeries(_concat);
async.whilst = function (test, iterator, callback) {
if (test()) {
iterator(function (err) {
if (err) {
return callback(err);
}
async.whilst(test, iterator, callback);
});
}
else {
callback();
}
};
async.until = function (test, iterator, callback) {
if (!test()) {
iterator(function (err) {
if (err) {
return callback(err);
}
async.until(test, iterator, callback);
});
}
else {
callback();
}
};
async.queue = function (worker, concurrency) {
var workers = 0;
var tasks = [];
var q = {
concurrency: concurrency,
push: function (data, callback) {
tasks.push({data: data, callback: callback});
async.nextTick(q.process);
},
process: function () {
if (workers < q.concurrency && tasks.length) {
var task = tasks.splice(0, 1)[0];
workers += 1;
worker(task.data, function () {
workers -= 1;
if (task.callback) {
task.callback.apply(task, arguments);
}
q.process();
});
}
},
length: function () {
return tasks.length;
}
};
return q;
};
var _console_fn = function (name) {
return function (fn) {
var args = Array.prototype.slice.call(arguments, 1);
fn.apply(null, args.concat([function (err) {
var args = Array.prototype.slice.call(arguments, 1);
if (typeof console !== 'undefined') {
if (err) {
if (console.error) {
console.error(err);
}
}
else if (console[name]) {
_forEach(args, function (x) {
console[name](x);
});
}
}
}]));
};
};
async.log = _console_fn('log');
async.dir = _console_fn('dir');
/*async.info = _console_fn('info');
async.warn = _console_fn('warn');
async.error = _console_fn('error');*/
async.memoize = function (fn, hasher) {
var memo = {};
hasher = hasher || function (x) {
return x;
};
return function () {
var args = Array.prototype.slice.call(arguments);
var callback = args.pop();
var key = hasher.apply(null, args);
if (key in memo) {
callback.apply(null, memo[key]);
}
else {
fn.apply(null, args.concat([function () {
memo[key] = arguments;
callback.apply(null, arguments);
}]));
}
};
};
}());
@@ -0,0 +1,55 @@
/*
A console.log that won't leave you hanging when node exits
90% of this file was ripped from node.js
License: see: https://github.com/joyent/node/blob/master/lib/console.js
*/
// console object
var formatRegExp = /%[sdj]/g;
function format(f) {
var util = require('util');
if (typeof f !== 'string') {
var objects = [];
for (var i = 0; i < arguments.length; i++) {
objects.push(util.inspect(arguments[i]));
}
return objects.join(' ');
}
var i = 1;
var args = arguments;
var str = String(f).replace(formatRegExp, function(x) {
switch (x) {
case '%s': return String(args[i++]);
case '%d': return Number(args[i++]);
case '%j': return JSON.stringify(args[i++]);
default:
return x;
}
});
for (var len = args.length, x = args[i]; i < len; x = args[++i]) {
if (x === null || typeof x !== 'object') {
str += ' ' + x;
} else {
str += ' ' + util.inspect(x);
}
}
return str;
}
console.log = function() {
var res = process.stdout.write(format.apply(this, arguments) + '\n');
// this is the first time stdout got backed up
if (!res && !process.stdout.pendingWrite) {
process.stdout.pendingWrite = true;
// magic sauce: keep node alive until stdout has flushed
process.stdout.once('drain', function () {
process.stdout.draining = false;
});
}
};
@@ -0,0 +1,70 @@
0.4.3 / 2011-06-20
==================
* Fixed stacktraces line number when used multiline js expressions [Octave]
0.4.2 / 2011-05-11
==================
* Added client side support
0.4.1 / 2011-04-21
==================
* Fixed error context
0.4.0 / 2011-04-21
==================
* Added; ported jade's error reporting to ejs. [slaskis]
0.3.1 / 2011-02-23
==================
* Fixed optional `compile()` options
0.3.0 / 2011-02-14
==================
* Added 'json' filter [Yuriy Bogdanov]
* Use exported version of parse function to allow monkey-patching [Anatoliy Chakkaev]
0.2.1 / 2010-10-07
==================
* Added filter support
* Fixed _cache_ option. ~4x performance increase
0.2.0 / 2010-08-05
==================
* Added support for global tag config
* Added custom tag support. Closes #5
* Fixed whitespace bug. Closes #4
0.1.0 / 2010-08-04
==================
* Faster implementation [ashleydev]
0.0.4 / 2010-08-02
==================
* Fixed single quotes for content outside of template tags. [aniero]
* Changed; `exports.compile()` now expects only "locals"
0.0.3 / 2010-07-15
==================
* Fixed single quotes
0.0.2 / 2010-07-09
==================
* Fixed newline preservation
0.0.1 / 2010-07-09
==================
* Initial release
@@ -0,0 +1,20 @@
SRC = $(shell find lib -name "*.js" -type f)
UGLIFY_FLAGS = --no-mangle
test:
@./node_modules/.bin/expresso test/*.test.js
ejs.js: $(SRC)
@node support/compile.js $^
ejs.min.js: ejs.js
@uglifyjs $(UGLIFY_FLAGS) $< > $@ \
&& du ejs.min.js \
&& du ejs.js
clean:
rm -f ejs.js
rm -f ejs.min.js
.PHONY: test
@@ -0,0 +1,152 @@
# EJS
Embedded JavaScript templates.
## Installation
$ npm install ejs
## Features
* Complies with the [Express](http://expressjs.com) view system
* Static caching of intermediate JavaScript
* Unbuffered code for conditionals etc `<% code %>`
* Escapes html by default with `<%= code %>`
* Unescaped buffering with `<%- code %>`
* Supports tag customization
* Filter support for designer-friendly templates
* Client-side support
## Example
<% if (user) { %>
<h2><%= user.name %></h2>
<% } %>
## Usage
ejs.compile(str, options);
// => Function
ejs.render(str, options);
// => str
## Options
- `locals` Local variables object
- `cache` Compiled functions are cached, requires `filename`
- `filename` Used by `cache` to key caches
- `scope` Function execution context
- `debug` Output generated function body
- `open` Open tag, defaulting to "<%"
- `close` Closing tag, defaulting to "%>"
## Custom tags
Custom tags can also be applied globally:
var ejs = require('ejs');
ejs.open = '{{';
ejs.close = '}}';
Which would make the following a valid template:
<h1>{{= title }}</h1>
## Filters
EJS conditionally supports the concept of "filters". A "filter chain"
is a designer friendly api for manipulating data, without writing JavaScript.
Filters can be applied by supplying the _:_ modifier, so for example if we wish to take the array `[{ name: 'tj' }, { name: 'mape' }, { name: 'guillermo' }]` and output a list of names we can do this simply with filters:
Template:
<p><%=: users | map:'name' | join %></p>
Output:
<p>Tj, Mape, Guillermo</p>
Render call:
ejs.render(str, {
locals: {
users: [
{ name: 'tj' },
{ name: 'mape' },
{ name: 'guillermo' }
]
}
});
Or perhaps capitalize the first user's name for display:
<p><%=: users | first | capitalize %></p>
## Filter list
Currently these filters are available:
- first
- last
- capitalize
- downcase
- upcase
- sort
- sort_by:'prop'
- size
- length
- plus:n
- minus:n
- times:n
- divided_by:n
- join:'val'
- truncate:n
- truncate_words:n
- replace:pattern,substitution
- prepend:val
- append:val
- map:'prop'
- reverse
- get:'prop'
## Adding filters
To add a filter simply add a method to the `.filters` object:
```js
ejs.filters.last = function(obj) {
return obj[obj.length - 1];
};
```
## client-side support
include `./ejs.js` or `./ejs.min.js` and `require("ejs").compile(str)`.
## License
(The MIT License)
Copyright (c) 2009-2010 TJ Holowaychuk &lt;tj@vision-media.ca&gt;
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
'Software'), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,14 @@
var ejs = require('./lib/ejs'),
str = '<% if (foo) { %><p><%= foo %></p><% } %>',
times = 50000;
console.log('rendering ' + times + ' times');
var start = new Date;
while (times--) {
ejs.render(str, { cache: true, filename: 'test', locals: { foo: 'bar' }});
}
console.log('took ' + (new Date - start) + 'ms');
@@ -0,0 +1,531 @@
// CommonJS require()
function require(p){
var path = require.resolve(p)
, mod = require.modules[path];
if (!mod) throw new Error('failed to require "' + p + '"');
if (!mod.exports) {
mod.exports = {};
mod.call(mod.exports, mod, mod.exports, require.relative(path));
}
return mod.exports;
}
require.modules = {};
require.resolve = function (path){
var orig = path
, reg = path + '.js'
, index = path + '/index.js';
return require.modules[reg] && reg
|| require.modules[index] && index
|| orig;
};
require.register = function (path, fn){
require.modules[path] = fn;
};
require.relative = function (parent) {
return function(p){
if ('.' != p[0]) return require(p);
var path = parent.split('/')
, segs = p.split('/');
path.pop();
for (var i = 0; i < segs.length; i++) {
var seg = segs[i];
if ('..' == seg) path.pop();
else if ('.' != seg) path.push(seg);
}
return require(path.join('/'));
};
};
require.register("ejs.js", function(module, exports, require){
/*!
* EJS
* Copyright(c) 2010 TJ Holowaychuk <tj@vision-media.ca>
* MIT Licensed
*/
/**
* Module dependencies.
*/
var utils = require('./utils');
/**
* Library version.
*/
exports.version = '0.4.2';
/**
* Filters.
*
* @type Object
*/
var filters = exports.filters = require('./filters');
/**
* Intermediate js cache.
*
* @type Object
*/
var cache = {};
/**
* Clear intermediate js cache.
*
* @api public
*/
exports.clearCache = function(){
cache = {};
};
/**
* Translate filtered code into function calls.
*
* @param {String} js
* @return {String}
* @api private
*/
function filtered(js) {
return js.substr(1).split('|').reduce(function(js, filter){
var parts = filter.split(':')
, name = parts.shift()
, args = parts.shift() || '';
if (args) args = ', ' + args;
return 'filters.' + name + '(' + js + args + ')';
});
};
/**
* Re-throw the given `err` in context to the
* `str` of ejs, `filename`, and `lineno`.
*
* @param {Error} err
* @param {String} str
* @param {String} filename
* @param {String} lineno
* @api private
*/
function rethrow(err, str, filename, lineno){
var lines = str.split('\n')
, start = Math.max(lineno - 3, 0)
, end = Math.min(lines.length, lineno + 3);
// Error context
var context = lines.slice(start, end).map(function(line, i){
var curr = i + start + 1;
return (curr == lineno ? ' >> ' : ' ')
+ curr
+ '| '
+ line;
}).join('\n');
// Alter exception message
err.path = filename;
err.message = (filename || 'ejs') + ':'
+ lineno + '\n'
+ context + '\n\n'
+ err.message;
throw err;
}
/**
* Parse the given `str` of ejs, returning the function body.
*
* @param {String} str
* @return {String}
* @api public
*/
var parse = exports.parse = function(str, options){
var options = options || {}
, open = options.open || exports.open || '<%'
, close = options.close || exports.close || '%>';
var buf = [
"var buf = [];"
, "\nwith (locals) {"
, "\n buf.push('"
];
var lineno = 1;
for (var i = 0, len = str.length; i < len; ++i) {
if (str.slice(i, open.length + i) == open) {
i += open.length
var prefix, postfix, line = '__stack.lineno=' + lineno;
switch (str[i]) {
case '=':
prefix = "', escape((" + line + ', ';
postfix = ")), '";
++i;
break;
case '-':
prefix = "', (" + line + ', ';
postfix = "), '";
++i;
break;
default:
prefix = "');" + line + ';';
postfix = "; buf.push('";
}
var start = i;
var end = str.indexOf(close, i);
var js = str.substring(i, end);
var n = 0;
while ((n = js.indexOf("\n", n)) > -1) {
n++;
lineno++;
}
if (js[0] == ':') js = filtered(js);
buf.push(prefix, js, postfix);
i += end - start + close.length - 1;
} else if (str[i] == "\\") {
buf.push("\\\\");
} else if (str[i] == "'") {
buf.push("\\'");
} else if (str[i] == "\r") {
buf.push(" ");
} else if (str[i] == "\n") {
buf.push("\\n");
lineno++;
} else {
buf.push(str[i]);
}
}
buf.push("');\n}\nreturn buf.join('');");
return buf.join('');
};
/**
* Compile the given `str` of ejs into a `Function`.
*
* @param {String} str
* @param {Object} options
* @return {Function}
* @api public
*/
var compile = exports.compile = function(str, options){
options = options || {};
var input = JSON.stringify(str)
, filename = options.filename
? JSON.stringify(options.filename)
: 'undefined';
// Adds the fancy stack trace meta info
str = [
'var __stack = { lineno: 1, input: ' + input + ', filename: ' + filename + ' };',
rethrow.toString(),
'try {',
exports.parse(str, options),
'} catch (err) {',
' rethrow(err, __stack.input, __stack.filename, __stack.lineno);',
'}'
].join("\n");
if (options.debug) console.log(str);
var fn = new Function('locals, filters, escape', str);
return function(locals){
return fn.call(this, locals, filters, utils.escape);
}
};
/**
* Render the given `str` of ejs.
*
* Options:
*
* - `locals` Local variables object
* - `cache` Compiled functions are cached, requires `filename`
* - `filename` Used by `cache` to key caches
* - `scope` Function execution context
* - `debug` Output generated function body
* - `open` Open tag, defaulting to "<%"
* - `close` Closing tag, defaulting to "%>"
*
* @param {String} str
* @param {Object} options
* @return {String}
* @api public
*/
exports.render = function(str, options){
var fn
, options = options || {};
if (options.cache) {
if (options.filename) {
fn = cache[options.filename] || (cache[options.filename] = compile(str, options));
} else {
throw new Error('"cache" option requires "filename".');
}
} else {
fn = compile(str, options);
}
return fn.call(options.scope, options.locals || {});
};
/**
* Expose to require().
*/
if (require.extensions) {
require.extensions['.ejs'] = function(module, filename) {
source = require('fs').readFileSync(filename, 'utf-8');
module._compile(compile(source, {}), filename);
};
} else if (require.registerExtension) {
require.registerExtension('.ejs', function(src) {
return compile(src, {});
});
}
}); // module: ejs.js
require.register("filters.js", function(module, exports, require){
/*!
* EJS - Filters
* Copyright(c) 2010 TJ Holowaychuk <tj@vision-media.ca>
* MIT Licensed
*/
/**
* First element of the target `obj`.
*/
exports.first = function(obj) {
return obj[0];
};
/**
* Last element of the target `obj`.
*/
exports.last = function(obj) {
return obj[obj.length - 1];
};
/**
* Capitalize the first letter of the target `str`.
*/
exports.capitalize = function(str){
str = String(str);
return str[0].toUpperCase() + str.substr(1, str.length);
};
/**
* Downcase the target `str`.
*/
exports.downcase = function(str){
return String(str).toLowerCase();
};
/**
* Uppercase the target `str`.
*/
exports.upcase = function(str){
return String(str).toUpperCase();
};
/**
* Sort the target `obj`.
*/
exports.sort = function(obj){
return Object.create(obj).sort();
};
/**
* Sort the target `obj` by the given `prop` ascending.
*/
exports.sort_by = function(obj, prop){
return Object.create(obj).sort(function(a, b){
a = a[prop], b = b[prop];
if (a > b) return 1;
if (a < b) return -1;
return 0;
});
};
/**
* Size or length of the target `obj`.
*/
exports.size = exports.length = function(obj) {
return obj.length;
};
/**
* Add `a` and `b`.
*/
exports.plus = function(a, b){
return Number(a) + Number(b);
};
/**
* Subtract `b` from `a`.
*/
exports.minus = function(a, b){
return Number(a) - Number(b);
};
/**
* Multiply `a` by `b`.
*/
exports.times = function(a, b){
return Number(a) * Number(b);
};
/**
* Divide `a` by `b`.
*/
exports.divided_by = function(a, b){
return Number(a) / Number(b);
};
/**
* Join `obj` with the given `str`.
*/
exports.join = function(obj, str){
return obj.join(str || ', ');
};
/**
* Truncate `str` to `len`.
*/
exports.truncate = function(str, len){
str = String(str);
return str.substr(0, len);
};
/**
* Truncate `str` to `n` words.
*/
exports.truncate_words = function(str, n){
var str = String(str)
, words = str.split(/ +/);
return words.slice(0, n).join(' ');
};
/**
* Replace `pattern` with `substitution` in `str`.
*/
exports.replace = function(str, pattern, substitution){
return String(str).replace(pattern, substitution || '');
};
/**
* Prepend `val` to `obj`.
*/
exports.prepend = function(obj, val){
return Array.isArray(obj)
? [val].concat(obj)
: val + obj;
};
/**
* Append `val` to `obj`.
*/
exports.append = function(obj, val){
return Array.isArray(obj)
? obj.concat(val)
: obj + val;
};
/**
* Map the given `prop`.
*/
exports.map = function(arr, prop){
return arr.map(function(obj){
return obj[prop];
});
};
/**
* Reverse the given `obj`.
*/
exports.reverse = function(obj){
return Array.isArray(obj)
? obj.reverse()
: String(obj).split('').reverse().join('');
};
/**
* Get `prop` of the given `obj`.
*/
exports.get = function(obj, prop){
return obj[prop];
};
/**
* Packs the given `obj` into json string
*/
exports.json = function(obj){
return JSON.stringify(obj);
};
}); // module: filters.js
require.register("utils.js", function(module, exports, require){
/*!
* EJS
* Copyright(c) 2010 TJ Holowaychuk <tj@vision-media.ca>
* MIT Licensed
*/
/**
* Escape the given string of `html`.
*
* @param {String} html
* @return {String}
* @api private
*/
exports.escape = function(html){
return String(html)
.replace(/&(?!\w+;)/g, '&amp;')
.replace(/</g, '&lt;')
.replace(/>/g, '&gt;')
.replace(/"/g, '&quot;');
};
}); // module: utils.js
Diff do arquivo suprimido porque uma ou mais linhas são muito longas
@@ -0,0 +1,5 @@
<html>
<head>
<script src="../ejs.js"></script>
</head>
</html>
@@ -0,0 +1,7 @@
<% if (names.length) { %>
<ul>
<% names.forEach(function(name){ %>
<li><%= name %></li>
<% }) %>
</ul>
<% } %>
@@ -0,0 +1,16 @@
/**
* Module dependencies.
*/
var ejs = require('../')
, fs = require('fs')
, str = fs.readFileSync(__dirname + '/list.ejs', 'utf8');
var ret = ejs.render(str, {
locals: {
names: ['foo', 'bar', 'baz']
}
});
console.log(ret);
@@ -0,0 +1,2 @@
module.exports = require('./lib/ejs');
@@ -0,0 +1,251 @@
/*!
* EJS
* Copyright(c) 2010 TJ Holowaychuk <tj@vision-media.ca>
* MIT Licensed
*/
/**
* Module dependencies.
*/
var utils = require('./utils');
/**
* Library version.
*/
exports.version = '0.4.3';
/**
* Filters.
*
* @type Object
*/
var filters = exports.filters = require('./filters');
/**
* Intermediate js cache.
*
* @type Object
*/
var cache = {};
/**
* Clear intermediate js cache.
*
* @api public
*/
exports.clearCache = function(){
cache = {};
};
/**
* Translate filtered code into function calls.
*
* @param {String} js
* @return {String}
* @api private
*/
function filtered(js) {
return js.substr(1).split('|').reduce(function(js, filter){
var parts = filter.split(':')
, name = parts.shift()
, args = parts.shift() || '';
if (args) args = ', ' + args;
return 'filters.' + name + '(' + js + args + ')';
});
};
/**
* Re-throw the given `err` in context to the
* `str` of ejs, `filename`, and `lineno`.
*
* @param {Error} err
* @param {String} str
* @param {String} filename
* @param {String} lineno
* @api private
*/
function rethrow(err, str, filename, lineno){
var lines = str.split('\n')
, start = Math.max(lineno - 3, 0)
, end = Math.min(lines.length, lineno + 3);
// Error context
var context = lines.slice(start, end).map(function(line, i){
var curr = i + start + 1;
return (curr == lineno ? ' >> ' : ' ')
+ curr
+ '| '
+ line;
}).join('\n');
// Alter exception message
err.path = filename;
err.message = (filename || 'ejs') + ':'
+ lineno + '\n'
+ context + '\n\n'
+ err.message;
throw err;
}
/**
* Parse the given `str` of ejs, returning the function body.
*
* @param {String} str
* @return {String}
* @api public
*/
var parse = exports.parse = function(str, options){
var options = options || {}
, open = options.open || exports.open || '<%'
, close = options.close || exports.close || '%>';
var buf = [
"var buf = [];"
, "\nwith (locals) {"
, "\n buf.push('"
];
var lineno = 1;
for (var i = 0, len = str.length; i < len; ++i) {
if (str.slice(i, open.length + i) == open) {
i += open.length
var prefix, postfix, line = '__stack.lineno=' + lineno;
switch (str.substr(i, 1)) {
case '=':
prefix = "', escape((" + line + ', ';
postfix = ")), '";
++i;
break;
case '-':
prefix = "', (" + line + ', ';
postfix = "), '";
++i;
break;
default:
prefix = "');" + line + ';';
postfix = "; buf.push('";
}
var end = str.indexOf(close, i)
, js = str.substring(i, end)
, start = i
, n = 0;
while (~(n = js.indexOf("\n", n))) n++, lineno++;
if (js.substr(0, 1) == ':') js = filtered(js);
buf.push(prefix, js, postfix);
i += end - start + close.length - 1;
} else if (str.substr(i, 1) == "\\") {
buf.push("\\\\");
} else if (str.substr(i, 1) == "'") {
buf.push("\\'");
} else if (str.substr(i, 1) == "\r") {
buf.push(" ");
} else if (str.substr(i, 1) == "\n") {
buf.push("\\n");
lineno++;
} else {
buf.push(str.substr(i, 1));
}
}
buf.push("');\n}\nreturn buf.join('');");
return buf.join('');
};
/**
* Compile the given `str` of ejs into a `Function`.
*
* @param {String} str
* @param {Object} options
* @return {Function}
* @api public
*/
var compile = exports.compile = function(str, options){
options = options || {};
var input = JSON.stringify(str)
, filename = options.filename
? JSON.stringify(options.filename)
: 'undefined';
// Adds the fancy stack trace meta info
str = [
'var __stack = { lineno: 1, input: ' + input + ', filename: ' + filename + ' };',
rethrow.toString(),
'try {',
exports.parse(str, options),
'} catch (err) {',
' rethrow(err, __stack.input, __stack.filename, __stack.lineno);',
'}'
].join("\n");
if (options.debug) console.log(str);
var fn = new Function('locals, filters, escape', str);
return function(locals){
return fn.call(this, locals, filters, utils.escape);
}
};
/**
* Render the given `str` of ejs.
*
* Options:
*
* - `locals` Local variables object
* - `cache` Compiled functions are cached, requires `filename`
* - `filename` Used by `cache` to key caches
* - `scope` Function execution context
* - `debug` Output generated function body
* - `open` Open tag, defaulting to "<%"
* - `close` Closing tag, defaulting to "%>"
*
* @param {String} str
* @param {Object} options
* @return {String}
* @api public
*/
exports.render = function(str, options){
var fn
, options = options || {};
if (options.cache) {
if (options.filename) {
fn = cache[options.filename] || (cache[options.filename] = compile(str, options));
} else {
throw new Error('"cache" option requires "filename".');
}
} else {
fn = compile(str, options);
}
return fn.call(options.scope, options.locals || {});
};
/**
* Expose to require().
*/
if (require.extensions) {
require.extensions['.ejs'] = function(module, filename) {
source = require('fs').readFileSync(filename, 'utf-8');
module._compile(compile(source, {}), filename);
};
} else if (require.registerExtension) {
require.registerExtension('.ejs', function(src) {
return compile(src, {});
});
}
@@ -0,0 +1,198 @@
/*!
* EJS - Filters
* Copyright(c) 2010 TJ Holowaychuk <tj@vision-media.ca>
* MIT Licensed
*/
/**
* First element of the target `obj`.
*/
exports.first = function(obj) {
return obj[0];
};
/**
* Last element of the target `obj`.
*/
exports.last = function(obj) {
return obj[obj.length - 1];
};
/**
* Capitalize the first letter of the target `str`.
*/
exports.capitalize = function(str){
str = String(str);
return str[0].toUpperCase() + str.substr(1, str.length);
};
/**
* Downcase the target `str`.
*/
exports.downcase = function(str){
return String(str).toLowerCase();
};
/**
* Uppercase the target `str`.
*/
exports.upcase = function(str){
return String(str).toUpperCase();
};
/**
* Sort the target `obj`.
*/
exports.sort = function(obj){
return Object.create(obj).sort();
};
/**
* Sort the target `obj` by the given `prop` ascending.
*/
exports.sort_by = function(obj, prop){
return Object.create(obj).sort(function(a, b){
a = a[prop], b = b[prop];
if (a > b) return 1;
if (a < b) return -1;
return 0;
});
};
/**
* Size or length of the target `obj`.
*/
exports.size = exports.length = function(obj) {
return obj.length;
};
/**
* Add `a` and `b`.
*/
exports.plus = function(a, b){
return Number(a) + Number(b);
};
/**
* Subtract `b` from `a`.
*/
exports.minus = function(a, b){
return Number(a) - Number(b);
};
/**
* Multiply `a` by `b`.
*/
exports.times = function(a, b){
return Number(a) * Number(b);
};
/**
* Divide `a` by `b`.
*/
exports.divided_by = function(a, b){
return Number(a) / Number(b);
};
/**
* Join `obj` with the given `str`.
*/
exports.join = function(obj, str){
return obj.join(str || ', ');
};
/**
* Truncate `str` to `len`.
*/
exports.truncate = function(str, len){
str = String(str);
return str.substr(0, len);
};
/**
* Truncate `str` to `n` words.
*/
exports.truncate_words = function(str, n){
var str = String(str)
, words = str.split(/ +/);
return words.slice(0, n).join(' ');
};
/**
* Replace `pattern` with `substitution` in `str`.
*/
exports.replace = function(str, pattern, substitution){
return String(str).replace(pattern, substitution || '');
};
/**
* Prepend `val` to `obj`.
*/
exports.prepend = function(obj, val){
return Array.isArray(obj)
? [val].concat(obj)
: val + obj;
};
/**
* Append `val` to `obj`.
*/
exports.append = function(obj, val){
return Array.isArray(obj)
? obj.concat(val)
: obj + val;
};
/**
* Map the given `prop`.
*/
exports.map = function(arr, prop){
return arr.map(function(obj){
return obj[prop];
});
};
/**
* Reverse the given `obj`.
*/
exports.reverse = function(obj){
return Array.isArray(obj)
? obj.reverse()
: String(obj).split('').reverse().join('');
};
/**
* Get `prop` of the given `obj`.
*/
exports.get = function(obj, prop){
return obj[prop];
};
/**
* Packs the given `obj` into json string
*/
exports.json = function(obj){
return JSON.stringify(obj);
};
@@ -0,0 +1,23 @@
/*!
* EJS
* Copyright(c) 2010 TJ Holowaychuk <tj@vision-media.ca>
* MIT Licensed
*/
/**
* Escape the given string of `html`.
*
* @param {String} html
* @return {String}
* @api private
*/
exports.escape = function(html){
return String(html)
.replace(/&(?!\w+;)/g, '&amp;')
.replace(/</g, '&lt;')
.replace(/>/g, '&gt;')
.replace(/"/g, '&quot;');
};
@@ -0,0 +1,11 @@
{
"name": "ejs",
"description": "Embedded JavaScript templates",
"version": "0.4.3",
"author": "TJ Holowaychuk <tj@vision-media.ca>",
"keywords": ["template", "engine", "ejs"],
"devDependencies": {
"expresso": "0.9.2"
},
"main": "./lib/ejs.js"
}
@@ -0,0 +1,173 @@
/**
* Module dependencies.
*/
var fs = require('fs');
/**
* Arguments.
*/
var args = process.argv.slice(2)
, pending = args.length
, files = {};
console.log('');
// parse arguments
args.forEach(function(file){
var mod = file.replace('lib/', '');
fs.readFile(file, 'utf8', function(err, js){
if (err) throw err;
console.log(' \033[90mcompile : \033[0m\033[36m%s\033[0m', file);
files[file] = parse(js);
--pending || compile();
});
});
/**
* Parse the given `js`.
*/
function parse(js) {
return parseInheritance(parseConditionals(js));
}
/**
* Parse __proto__.
*/
function parseInheritance(js) {
return js
.replace(/^ *(\w+)\.prototype\.__proto__ * = *(\w+)\.prototype *;?/gm, function(_, child, parent){
return child + '.prototype = new ' + parent + ';\n'
+ child + '.prototype.constructor = '+ child + ';\n';
});
}
/**
* Parse the given `js`, currently supporting:
*
* 'if' ['node' | 'browser']
* 'end'
*
*/
function parseConditionals(js) {
var lines = js.split('\n')
, len = lines.length
, buffer = true
, browser = false
, buf = []
, line
, cond;
for (var i = 0; i < len; ++i) {
line = lines[i];
if (/^ *\/\/ *if *(node|browser)/gm.exec(line)) {
cond = RegExp.$1;
buffer = browser = 'browser' == cond;
} else if (/^ *\/\/ *end/.test(line)) {
buffer = true;
browser = false;
} else if (browser) {
buf.push(line.replace(/^( *)\/\//, '$1'));
} else if (buffer) {
buf.push(line);
}
}
return buf.join('\n');
}
/**
* Compile the files.
*/
function compile() {
var buf = '';
buf += '\n// CommonJS require()\n\n';
buf += browser.require + '\n\n';
buf += 'require.modules = {};\n\n';
buf += 'require.resolve = ' + browser.resolve + ';\n\n';
buf += 'require.register = ' + browser.register + ';\n\n';
buf += 'require.relative = ' + browser.relative + ';\n\n';
args.forEach(function(file){
var js = files[file];
file = file.replace('lib/', '');
buf += '\nrequire.register("' + file + '", function(module, exports, require){\n';
buf += js;
buf += '\n}); // module: ' + file + '\n';
});
fs.writeFile('ejs.js', buf, function(err){
if (err) throw err;
console.log(' \033[90m create : \033[0m\033[36m%s\033[0m', 'ejs.js');
console.log();
});
}
// refactored version of weepy's
// https://github.com/weepy/brequire/blob/master/browser/brequire.js
var browser = {
/**
* Require a module.
*/
require: function require(p){
var path = require.resolve(p)
, mod = require.modules[path];
if (!mod) throw new Error('failed to require "' + p + '"');
if (!mod.exports) {
mod.exports = {};
mod.call(mod.exports, mod, mod.exports, require.relative(path));
}
return mod.exports;
},
/**
* Resolve module path.
*/
resolve: function(path){
var orig = path
, reg = path + '.js'
, index = path + '/index.js';
return require.modules[reg] && reg
|| require.modules[index] && index
|| orig;
},
/**
* Return relative require().
*/
relative: function(parent) {
return function(p){
if ('.' != p.substr(0, 1)) return require(p);
var path = parent.split('/')
, segs = p.split('/');
path.pop();
for (var i = 0; i < segs.length; i++) {
var seg = segs[i];
if ('..' == seg) path.pop();
else if ('.' != seg) path.push(seg);
}
return require(path.join('/'));
};
},
/**
* Register a module.
*/
register: function(path, fn){
require.modules[path] = fn;
}
};
@@ -0,0 +1,269 @@
/**
* Module dependencies.
*/
var ejs = require('../')
, assert = require('assert');
module.exports = {
'test .version': function(){
assert.ok(/^\d+\.\d+\.\d+$/.test(ejs.version), 'Test .version format');
},
'test html': function(){
assert.equal('<p>yay</p>', ejs.render('<p>yay</p>'));
},
'test buffered code': function(){
var html = '<p>tj</p>',
str = '<p><%= name %></p>',
locals = { name: 'tj' };
assert.equal(html, ejs.render(str, { locals: locals }));
},
'test unbuffered code': function(){
var html = '<p>tj</p>',
str = '<% if (name) { %><p><%= name %></p><% } %>',
locals = { name: 'tj' };
assert.equal(html, ejs.render(str, { locals: locals }));
},
'test `scope` option': function(){
var html = '<p>tj</p>',
str = '<p><%= this %></p>';
assert.equal(html, ejs.render(str, { scope: 'tj' }));
},
'test escaping': function(){
assert.equal('&lt;script&gt;', ejs.render('<%= "<script>" %>'));
assert.equal('<script>', ejs.render('<%- "<script>" %>'));
},
'test newlines': function(){
var html = '\n<p>tj</p>\n<p>tj@sencha.com</p>',
str = '<% if (name) { %>\n<p><%= name %></p>\n<p><%= email %></p><% } %>',
locals = { name: 'tj', email: 'tj@sencha.com' };
assert.equal(html, ejs.render(str, { locals: locals }));
},
'test single quotes': function(){
var html = '<p>WAHOO</p>',
str = "<p><%= up('wahoo') %></p>",
locals = { up: function(str){ return str.toUpperCase(); }};
assert.equal(html, ejs.render(str, { locals: locals }));
},
'test single quotes in the html': function(){
var html = '<p>WAHOO that\'s cool</p>',
str = '<p><%= up(\'wahoo\') %> that\'s cool</p>',
locals = { up: function(str){ return str.toUpperCase(); }};
assert.equal(html, ejs.render(str, { locals: locals }));
},
'test multiple single quotes': function() {
var html = "<p>couldn't shouldn't can't</p>",
str = "<p>couldn't shouldn't can't</p>";
assert.equal(html, ejs.render(str));
},
'test single quotes inside tags': function() {
var html = '<p>string</p>',
str = "<p><%= 'string' %></p>";
assert.equal(html, ejs.render(str));
},
'test back-slashes in the document': function() {
var html = "<p>backslash: '\\'</p>",
str = "<p>backslash: '\\'</p>";
assert.equal(html, ejs.render(str));
},
'test double quotes': function(){
var html = '<p>WAHOO</p>',
str = '<p><%= up("wahoo") %></p>',
locals = { up: function(str){ return str.toUpperCase(); }};
assert.equal(html, ejs.render(str, { locals: locals }));
},
'test multiple double quotes': function() {
var html = '<p>just a "test" wahoo</p>',
str = '<p>just a "test" wahoo</p>';
assert.equal(html, ejs.render(str));
},
'test whitespace': function(){
var html = '<p>foo</p>',
str = '<p><%="foo"%></p>';
assert.equal(html, ejs.render(str));
var html = '<p>foo</p>',
str = '<p><%=bar%></p>';
assert.equal(html, ejs.render(str, { locals: { bar: 'foo' }}));
},
'test custom tags': function(){
var html = '<p>foo</p>',
str = '<p>{{= "foo" }}</p>';
assert.equal(html, ejs.render(str, {
open: '{{',
close: '}}'
}));
var html = '<p>foo</p>',
str = '<p><?= "foo" ?></p>';
assert.equal(html, ejs.render(str, {
open: '<?',
close: '?>'
}));
},
'test custom tags over 2 chars': function(){
var html = '<p>foo</p>',
str = '<p>{{{{= "foo" }>>}</p>';
assert.equal(html, ejs.render(str, {
open: '{{{{',
close: '}>>}'
}));
var html = '<p>foo</p>',
str = '<p><??= "foo" ??></p>';
assert.equal(html, ejs.render(str, {
open: '<??',
close: '??>'
}));
},
'test global custom tags': function(){
var html = '<p>foo</p>',
str = '<p>{{= "foo" }}</p>';
ejs.open = '{{';
ejs.close = '}}';
assert.equal(html, ejs.render(str));
delete ejs.open;
delete ejs.close;
},
'test iteration': function(){
var html = '<p>foo</p>',
str = '<% for (var key in items) { %>'
+ '<p><%= items[key] %></p>'
+ '<% } %>';
assert.equal(html, ejs.render(str, {
locals: {
items: ['foo']
}
}));
var html = '<p>foo</p>',
str = '<% items.forEach(function(item){ %>'
+ '<p><%= item %></p>'
+ '<% }) %>';
assert.equal(html, ejs.render(str, {
locals: {
items: ['foo']
}
}));
},
'test filter support': function(){
var html = 'Zab',
str = '<%=: items | reverse | first | reverse | capitalize %>';
assert.equal(html, ejs.render(str, {
locals: {
items: ['foo', 'bar', 'baz']
}
}));
},
'test filter argument support': function(){
var html = 'tj, guillermo',
str = '<%=: users | map:"name" | join:", " %>';
assert.equal(html, ejs.render(str, {
locals: {
users: [
{ name: 'tj' },
{ name: 'guillermo' }
]
}
}));
},
'test sort_by filter': function(){
var html = 'tj',
str = '<%=: users | sort_by:"name" | last | get:"name" %>';
assert.equal(html, ejs.render(str, {
locals: {
users: [
{ name: 'guillermo' },
{ name: 'tj' },
{ name: 'mape' }
]
}
}));
},
'test custom filters': function(){
var html = 'Welcome Tj Holowaychuk',
str = '<%=: users | first | greeting %>';
ejs.filters.greeting = function(user){
return 'Welcome ' + user.first + ' ' + user.last + '';
};
assert.equal(html, ejs.render(str, {
locals: {
users: [
{ first: 'Tj', last: 'Holowaychuk' }
]
}
}));
},
'test useful stack traces': function(){
var str = [
"A little somethin'",
"somethin'",
"<% if (name) { %>", // Failing line
" <p><%= name %></p>",
" <p><%= email %></p>",
"<% } %>"
].join("\n");
try {
ejs.render(str)
} catch( err ){
assert.includes(err.message,"name is not defined");
assert.eql(err.name,"ReferenceError");
var lineno = parseInt(err.toString().match(/ejs:(\d+)\n/)[1]);
assert.eql(lineno,3,"Error should been thrown on line 3, was thrown on line "+lineno);
}
},
'test useful stack traces multiline': function(){
var str = [
"A little somethin'",
"somethin'",
"<% var some = 'pretty';",
" var multiline = 'javascript';",
"%>",
"<% if (name) { %>", // Failing line
" <p><%= name %></p>",
" <p><%= email %></p>",
"<% } %>"
].join("\n");
try {
ejs.render(str)
} catch( err ){
assert.includes(err.message,"name is not defined");
assert.eql(err.name,"ReferenceError");
var lineno = parseInt(err.toString().match(/ejs:(\d+)\n/)[1]);
assert.eql(lineno,6,"Error should been thrown on line 3, was thrown on line "+lineno);
}
}
};
@@ -0,0 +1,481 @@
/*
http://www.JSON.org/json2.js
2010-11-17
Public Domain.
NO WARRANTY EXPRESSED OR IMPLIED. USE AT YOUR OWN RISK.
See http://www.JSON.org/js.html
This code should be minified before deployment.
See http://javascript.crockford.com/jsmin.html
USE YOUR OWN COPY. IT IS EXTREMELY UNWISE TO LOAD CODE FROM SERVERS YOU DO
NOT CONTROL.
This file creates a global JSON object containing two methods: stringify
and parse.
JSON.stringify(value, replacer, space)
value any JavaScript value, usually an object or array.
replacer an optional parameter that determines how object
values are stringified for objects. It can be a
function or an array of strings.
space an optional parameter that specifies the indentation
of nested structures. If it is omitted, the text will
be packed without extra whitespace. If it is a number,
it will specify the number of spaces to indent at each
level. If it is a string (such as '\t' or '&nbsp;'),
it contains the characters used to indent at each level.
This method produces a JSON text from a JavaScript value.
When an object value is found, if the object contains a toJSON
method, its toJSON method will be called and the result will be
stringified. A toJSON method does not serialize: it returns the
value represented by the name/value pair that should be serialized,
or undefined if nothing should be serialized. The toJSON method
will be passed the key associated with the value, and this will be
bound to the value
For example, this would serialize Dates as ISO strings.
Date.prototype.toJSON = function (key) {
function f(n) {
// Format integers to have at least two digits.
return n < 10 ? '0' + n : n;
}
return this.getUTCFullYear() + '-' +
f(this.getUTCMonth() + 1) + '-' +
f(this.getUTCDate()) + 'T' +
f(this.getUTCHours()) + ':' +
f(this.getUTCMinutes()) + ':' +
f(this.getUTCSeconds()) + 'Z';
};
You can provide an optional replacer method. It will be passed the
key and value of each member, with this bound to the containing
object. The value that is returned from your method will be
serialized. If your method returns undefined, then the member will
be excluded from the serialization.
If the replacer parameter is an array of strings, then it will be
used to select the members to be serialized. It filters the results
such that only members with keys listed in the replacer array are
stringified.
Values that do not have JSON representations, such as undefined or
functions, will not be serialized. Such values in objects will be
dropped; in arrays they will be replaced with null. You can use
a replacer function to replace those with JSON values.
JSON.stringify(undefined) returns undefined.
The optional space parameter produces a stringification of the
value that is filled with line breaks and indentation to make it
easier to read.
If the space parameter is a non-empty string, then that string will
be used for indentation. If the space parameter is a number, then
the indentation will be that many spaces.
Example:
text = JSON.stringify(['e', {pluribus: 'unum'}]);
// text is '["e",{"pluribus":"unum"}]'
text = JSON.stringify(['e', {pluribus: 'unum'}], null, '\t');
// text is '[\n\t"e",\n\t{\n\t\t"pluribus": "unum"\n\t}\n]'
text = JSON.stringify([new Date()], function (key, value) {
return this[key] instanceof Date ?
'Date(' + this[key] + ')' : value;
});
// text is '["Date(---current time---)"]'
JSON.parse(text, reviver)
This method parses a JSON text to produce an object or array.
It can throw a SyntaxError exception.
The optional reviver parameter is a function that can filter and
transform the results. It receives each of the keys and values,
and its return value is used instead of the original value.
If it returns what it received, then the structure is not modified.
If it returns undefined then the member is deleted.
Example:
// Parse the text. Values that look like ISO date strings will
// be converted to Date objects.
myData = JSON.parse(text, function (key, value) {
var a;
if (typeof value === 'string') {
a =
/^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2}(?:\.\d*)?)Z$/.exec(value);
if (a) {
return new Date(Date.UTC(+a[1], +a[2] - 1, +a[3], +a[4],
+a[5], +a[6]));
}
}
return value;
});
myData = JSON.parse('["Date(09/09/2001)"]', function (key, value) {
var d;
if (typeof value === 'string' &&
value.slice(0, 5) === 'Date(' &&
value.slice(-1) === ')') {
d = new Date(value.slice(5, -1));
if (d) {
return d;
}
}
return value;
});
This is a reference implementation. You are free to copy, modify, or
redistribute.
*/
/*jslint evil: true, strict: false, regexp: false */
/*members "", "\b", "\t", "\n", "\f", "\r", "\"", JSON, "\\", apply,
call, charCodeAt, getUTCDate, getUTCFullYear, getUTCHours,
getUTCMinutes, getUTCMonth, getUTCSeconds, hasOwnProperty, join,
lastIndex, length, parse, prototype, push, replace, slice, stringify,
test, toJSON, toString, valueOf
*/
// Create a JSON object only if one does not already exist. We create the
// methods in a closure to avoid creating global variables.
var JSON = {};
(function () {
"use strict";
function f(n) {
// Format integers to have at least two digits.
return n < 10 ? '0' + n : n;
}
if (typeof Date.prototype.toJSON !== 'function') {
Date.prototype.toJSON = function (key) {
return isFinite(this.valueOf()) ?
this.getUTCFullYear() + '-' +
f(this.getUTCMonth() + 1) + '-' +
f(this.getUTCDate()) + 'T' +
f(this.getUTCHours()) + ':' +
f(this.getUTCMinutes()) + ':' +
f(this.getUTCSeconds()) + 'Z' : null;
};
String.prototype.toJSON =
Number.prototype.toJSON =
Boolean.prototype.toJSON = function (key) {
return this.valueOf();
};
}
var cx = /[\u0000\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]/g,
escapable = /[\\\"\x00-\x1f\x7f-\x9f\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]/g,
gap,
indent,
meta = { // table of character substitutions
'\b': '\\b',
'\t': '\\t',
'\n': '\\n',
'\f': '\\f',
'\r': '\\r',
'"' : '\\"',
'\\': '\\\\'
},
rep;
function quote(string) {
// If the string contains no control characters, no quote characters, and no
// backslash characters, then we can safely slap some quotes around it.
// Otherwise we must also replace the offending characters with safe escape
// sequences.
escapable.lastIndex = 0;
return escapable.test(string) ?
'"' + string.replace(escapable, function (a) {
var c = meta[a];
return typeof c === 'string' ? c :
'\\u' + ('0000' + a.charCodeAt(0).toString(16)).slice(-4);
}) + '"' :
'"' + string + '"';
}
function str(key, holder) {
// Produce a string from holder[key].
var i, // The loop counter.
k, // The member key.
v, // The member value.
length,
mind = gap,
partial,
value = holder[key];
// If the value has a toJSON method, call it to obtain a replacement value.
if (value && typeof value === 'object' &&
typeof value.toJSON === 'function') {
value = value.toJSON(key);
}
// If we were called with a replacer function, then call the replacer to
// obtain a replacement value.
if (typeof rep === 'function') {
value = rep.call(holder, key, value);
}
// What happens next depends on the value's type.
switch (typeof value) {
case 'string':
return quote(value);
case 'number':
// JSON numbers must be finite. Encode non-finite numbers as null.
return isFinite(value) ? String(value) : 'null';
case 'boolean':
case 'null':
// If the value is a boolean or null, convert it to a string. Note:
// typeof null does not produce 'null'. The case is included here in
// the remote chance that this gets fixed someday.
return String(value);
// If the type is 'object', we might be dealing with an object or an array or
// null.
case 'object':
// Due to a specification blunder in ECMAScript, typeof null is 'object',
// so watch out for that case.
if (!value) {
return 'null';
}
// Make an array to hold the partial results of stringifying this object value.
gap += indent;
partial = [];
// Is the value an array?
if (Object.prototype.toString.apply(value) === '[object Array]') {
// The value is an array. Stringify every element. Use null as a placeholder
// for non-JSON values.
length = value.length;
for (i = 0; i < length; i += 1) {
partial[i] = str(i, value) || 'null';
}
// Join all of the elements together, separated with commas, and wrap them in
// brackets.
v = partial.length === 0 ? '[]' :
gap ? '[\n' + gap +
partial.join(',\n' + gap) + '\n' +
mind + ']' :
'[' + partial.join(',') + ']';
gap = mind;
return v;
}
// If the replacer is an array, use it to select the members to be stringified.
if (rep && typeof rep === 'object') {
length = rep.length;
for (i = 0; i < length; i += 1) {
k = rep[i];
if (typeof k === 'string') {
v = str(k, value);
if (v) {
partial.push(quote(k) + (gap ? ': ' : ':') + v);
}
}
}
} else {
// Otherwise, iterate through all of the keys in the object.
for (k in value) {
if (Object.hasOwnProperty.call(value, k)) {
v = str(k, value);
if (v) {
partial.push(quote(k) + (gap ? ': ' : ':') + v);
}
}
}
}
// Join all of the member texts together, separated with commas,
// and wrap them in braces.
v = partial.length === 0 ? '{}' :
gap ? '{\n' + gap + partial.join(',\n' + gap) + '\n' +
mind + '}' : '{' + partial.join(',') + '}';
gap = mind;
return v;
}
}
// If the JSON object does not yet have a stringify method, give it one.
if (typeof JSON.stringify !== 'function') {
JSON.stringify = function (value, replacer, space) {
// The stringify method takes a value and an optional replacer, and an optional
// space parameter, and returns a JSON text. The replacer can be a function
// that can replace values, or an array of strings that will select the keys.
// A default replacer method can be provided. Use of the space parameter can
// produce text that is more easily readable.
var i;
gap = '';
indent = '';
// If the space parameter is a number, make an indent string containing that
// many spaces.
if (typeof space === 'number') {
for (i = 0; i < space; i += 1) {
indent += ' ';
}
// If the space parameter is a string, it will be used as the indent string.
} else if (typeof space === 'string') {
indent = space;
}
// If there is a replacer, it must be a function or an array.
// Otherwise, throw an error.
rep = replacer;
if (replacer && typeof replacer !== 'function' &&
(typeof replacer !== 'object' ||
typeof replacer.length !== 'number')) {
throw new Error('JSON.stringify');
}
// Make a fake root object containing our value under the key of ''.
// Return the result of stringifying the value.
return str('', {'': value});
};
}
// If the JSON object does not yet have a parse method, give it one.
if (typeof JSON.parse !== 'function') {
JSON.parse = function (text, reviver) {
// The parse method takes a text and an optional reviver function, and returns
// a JavaScript value if the text is a valid JSON text.
var j;
function walk(holder, key) {
// The walk method is used to recursively walk the resulting structure so
// that modifications can be made.
var k, v, value = holder[key];
if (value && typeof value === 'object') {
for (k in value) {
if (Object.hasOwnProperty.call(value, k)) {
v = walk(value, k);
if (v !== undefined) {
value[k] = v;
} else {
delete value[k];
}
}
}
}
return reviver.call(holder, key, value);
}
// Parsing happens in four stages. In the first stage, we replace certain
// Unicode characters with escape sequences. JavaScript handles many characters
// incorrectly, either silently deleting them, or treating them as line endings.
text = String(text);
cx.lastIndex = 0;
if (cx.test(text)) {
text = text.replace(cx, function (a) {
return '\\u' +
('0000' + a.charCodeAt(0).toString(16)).slice(-4);
});
}
// In the second stage, we run the text against regular expressions that look
// for non-JSON patterns. We are especially concerned with '()' and 'new'
// because they can cause invocation, and '=' because it can cause mutation.
// But just to be safe, we want to reject all unexpected forms.
// We split the second stage into 4 regexp operations in order to work around
// crippling inefficiencies in IE's and Safari's regexp engines. First we
// replace the JSON backslash pairs with '@' (a non-JSON character). Second, we
// replace all simple value tokens with ']' characters. Third, we delete all
// open brackets that follow a colon or comma or that begin the text. Finally,
// we look to see that the remaining characters are only whitespace or ']' or
// ',' or ':' or '{' or '}'. If that is so, then the text is safe for eval.
if (/^[\],:{}\s]*$/
.test(text.replace(/\\(?:["\\\/bfnrt]|u[0-9a-fA-F]{4})/g, '@')
.replace(/"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g, ']')
.replace(/(?:^|:|,)(?:\s*\[)+/g, ''))) {
// In the third stage we use the eval function to compile the text into a
// JavaScript structure. The '{' operator is subject to a syntactic ambiguity
// in JavaScript: it can begin a block or an object literal. We wrap the text
// in parens to eliminate the ambiguity.
j = eval('(' + text + ')');
// In the optional fourth stage, we recursively walk the new structure, passing
// each name/value pair to a reviver function for possible transformation.
return typeof reviver === 'function' ?
walk({'': j}, '') : j;
}
// If the text is not JSON parseable, then a SyntaxError is thrown.
throw new SyntaxError('JSON.parse');
};
}
}());
@@ -0,0 +1,60 @@
nodeunit(1) -- simple node.js unit testing tool
===============================================
## SYNOPSIS
nodeunit [options] <file-or-directory> [<file-or-directory> ...]
## DESCRIPTION
Nodeunit is a simple unit testing tool based on the node.js assert module.
* Simple to use
* Just export the tests from a module
* Helps you avoid common pitfalls when testing asynchronous code
* Easy to add test cases with setUp and tearDown functions if you wish
* Allows the use of mocks and stubs
## OPTIONS
__--config FILE__:
Load config options from a JSON file, allows the customisation
of color schemes for the default test reporter etc.
See bin/nodeunit.json for current available options.
__--reporter FILE__:
You can set the test reporter to a custom module or on of the modules
in nodeunit/lib/reporters, when omitted, the default test runner is used.
__--list-reporters__:
List available build-in reporters.
__-h__, __--help__:
Display the help and exit.
__-v__, __--version__:
Output version information and exit.
__<file-or-directory>__:
You can run nodeunit on specific files or on all *\*.js* files inside
a directory.
## AUTHORS
Written by Caolan McMahon and other nodeunit contributors.
Contributors list: <http://github.com/caolan/nodeunit/contributors>.
## REPORTING BUGS
Report nodeunit bugs to <http://github.com/caolan/nodeunit/issues>.
## COPYRIGHT
Copyright © 2010 Caolan McMahon.
Nodeunit has been released under the MIT license:
<http://github.com/caolan/nodeunit/raw/master/LICENSE>.
## SEE ALSO
node(1)
Diferenças do arquivo suprimidas por serem muito extensas Carregar Diff
@@ -0,0 +1,12 @@
this.suite1 = {
'test one': function (test) {
test.ok(true, 'everythings ok');
setTimeout(function () {
test.done();
}, 10);
},
'apples and oranges': function (test) {
test.equal('apples', 'oranges', 'comparing apples and oranges');
test.done();
}
};
@@ -0,0 +1,13 @@
this.suite2 = {
'another test': function (test) {
setTimeout(function () {
// lots of assertions
test.ok(true, 'everythings ok');
test.ok(true, 'everythings ok');
test.ok(true, 'everythings ok');
test.ok(true, 'everythings ok');
test.ok(true, 'everythings ok');
test.done();
}, 10);
}
};
@@ -0,0 +1,7 @@
this.suite3 = {
'test for ie6,7,8': function (test) {
test.deepEqual(["test"], ["test"]);
test.notDeepEqual(["a"], ["b"]);
test.done();
}
};
@@ -0,0 +1,18 @@
<html>
<head>
<title>Example tests</title>
<script src="nodeunit.js"></script>
<script src="suite1.js"></script>
<script src="suite2.js"></script>
<script src="suite3.js"></script>
</head>
<body>
<script>
nodeunit.run({
'suite1': suite1,
'suite2': suite2,
'suite3': suite3
});
</script>
</body>
</html>
@@ -0,0 +1,94 @@
var testCase = require('nodeunit').testCase;
/*
This is an example test suite to demonstrate the nested test reporter.
Run with --reporter nested, e.g.,
nodeunit --reporter nested nested_reporter_test.unit.js
The test output should be something like:
nested_reporter_test.unit.js
Test 0.1 (pass)
TC 1
TC 1.1
Test 1.1.1 (pass)
TC 2
TC 2.1
TC 2.1.1
Test 2.1.1.1 (pass)
Test 2.1.1.2 (pass)
TC 2.2.1
Test 2.2.1.1 (pass)
TC 2.2.1.1
Test 2.2.1.1.1 (pass)
Test 2.2.1.2 (pass)
TC 3
TC 3.1
TC 3.1.1
Test 3.1.1.1 (should fail) (fail) ✖
AssertionError: false == true
// stack trace here.
FAILURES: 1/8 assertions failed (6ms)
*/
module.exports = testCase({
"Test 0.1": function(test) {
test.ok(true);
test.done();
},
"TC 1": testCase({
"TC 1.1": testCase({
"Test 1.1.1": function(test) {
test.ok(true);
test.done();
}
})
}),
"TC 2": testCase({
"TC 2.1": testCase({
"TC 2.1.1": testCase({
"Test 2.1.1.1": function(test) {
test.ok(true);
test.done();
},
"Test 2.1.1.2": function(test) {
test.ok(true);
test.done();
}
}),
"TC 2.2.1": testCase({
"Test 2.2.1.1": function(test) {
test.ok(true);
test.done();
},
"TC 2.2.1.1": testCase({
"Test 2.2.1.1.1": function(test) {
test.ok(true);
test.done();
},
}),
"Test 2.2.1.2": function(test) {
test.ok(true);
test.done();
}
})
})
}),
"TC 3": testCase({
"TC 3.1": testCase({
"TC 3.1.1": testCase({
"Test 3.1.1.1 (should fail)": function(test) {
test.ok(false);
test.done();
}
})
})
})
});
Arquivo binário não exibido.

Depois

Largura:  |  Altura:  |  Tamanho: 38 KiB

Arquivo binário não exibido.

Depois

Largura:  |  Altura:  |  Tamanho: 412 KiB

Arquivo binário não exibido.

Depois

Largura:  |  Altura:  |  Tamanho: 14 KiB

@@ -0,0 +1,3 @@
// This file is just added for convenience so this repository can be
// directly checked out into a project's deps folder
module.exports = require('./lib/nodeunit');
@@ -0,0 +1,327 @@
/**
* This file is based on the node.js assert module, but with some small
* changes for browser-compatibility
* THIS FILE SHOULD BE BROWSER-COMPATIBLE JS!
*/
/**
* Added for browser compatibility
*/
var _keys = function(obj){
if(Object.keys) return Object.keys(obj);
if (typeof obj != 'object' && typeof obj != 'function') {
throw new TypeError('-');
}
var keys = [];
for(var k in obj){
if(obj.hasOwnProperty(k)) keys.push(k);
}
return keys;
};
// http://wiki.commonjs.org/wiki/Unit_Testing/1.0
//
// THIS IS NOT TESTED NOR LIKELY TO WORK OUTSIDE V8!
//
// Originally from narwhal.js (http://narwhaljs.org)
// Copyright (c) 2009 Thomas Robinson <280north.com>
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the 'Software'), to
// deal in the Software without restriction, including without limitation the
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
// ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
var pSlice = Array.prototype.slice;
// 1. The assert module provides functions that throw
// AssertionError's when particular conditions are not met. The
// assert module must conform to the following interface.
var assert = exports;
// 2. The AssertionError is defined in assert.
// new assert.AssertionError({message: message, actual: actual, expected: expected})
assert.AssertionError = function AssertionError (options) {
this.name = "AssertionError";
this.message = options.message;
this.actual = options.actual;
this.expected = options.expected;
this.operator = options.operator;
var stackStartFunction = options.stackStartFunction || fail;
if (Error.captureStackTrace) {
Error.captureStackTrace(this, stackStartFunction);
}
};
// code from util.inherits in node
assert.AssertionError.super_ = Error;
// EDITED FOR BROWSER COMPATIBILITY: replaced Object.create call
// TODO: test what effect this may have
var ctor = function () { this.constructor = assert.AssertionError; };
ctor.prototype = Error.prototype;
assert.AssertionError.prototype = new ctor();
assert.AssertionError.prototype.toString = function() {
if (this.message) {
return [this.name+":", this.message].join(' ');
} else {
return [ this.name+":"
, JSON.stringify(this.expected )
, this.operator
, JSON.stringify(this.actual)
].join(" ");
}
};
// assert.AssertionError instanceof Error
assert.AssertionError.__proto__ = Error.prototype;
// At present only the three keys mentioned above are used and
// understood by the spec. Implementations or sub modules can pass
// other keys to the AssertionError's constructor - they will be
// ignored.
// 3. All of the following functions must throw an AssertionError
// when a corresponding condition is not met, with a message that
// may be undefined if not provided. All assertion methods provide
// both the actual and expected values to the assertion error for
// display purposes.
function fail(actual, expected, message, operator, stackStartFunction) {
throw new assert.AssertionError({
message: message,
actual: actual,
expected: expected,
operator: operator,
stackStartFunction: stackStartFunction
});
}
// EXTENSION! allows for well behaved errors defined elsewhere.
assert.fail = fail;
// 4. Pure assertion tests whether a value is truthy, as determined
// by !!guard.
// assert.ok(guard, message_opt);
// This statement is equivalent to assert.equal(true, guard,
// message_opt);. To test strictly for the value true, use
// assert.strictEqual(true, guard, message_opt);.
assert.ok = function ok(value, message) {
if (!!!value) fail(value, true, message, "==", assert.ok);
};
// 5. The equality assertion tests shallow, coercive equality with
// ==.
// assert.equal(actual, expected, message_opt);
assert.equal = function equal(actual, expected, message) {
if (actual != expected) fail(actual, expected, message, "==", assert.equal);
};
// 6. The non-equality assertion tests for whether two objects are not equal
// with != assert.notEqual(actual, expected, message_opt);
assert.notEqual = function notEqual(actual, expected, message) {
if (actual == expected) {
fail(actual, expected, message, "!=", assert.notEqual);
}
};
// 7. The equivalence assertion tests a deep equality relation.
// assert.deepEqual(actual, expected, message_opt);
assert.deepEqual = function deepEqual(actual, expected, message) {
if (!_deepEqual(actual, expected)) {
fail(actual, expected, message, "deepEqual", assert.deepEqual);
}
};
function _deepEqual(actual, expected) {
// 7.1. All identical values are equivalent, as determined by ===.
if (actual === expected) {
return true;
// 7.2. If the expected value is a Date object, the actual value is
// equivalent if it is also a Date object that refers to the same time.
} else if (actual instanceof Date && expected instanceof Date) {
return actual.getTime() === expected.getTime();
// 7.2.1 If the expcted value is a RegExp object, the actual value is
// equivalent if it is also a RegExp object that refers to the same source and options
} else if (actual instanceof RegExp && expected instanceof RegExp) {
return actual.source === expected.source &&
actual.global === expected.global &&
actual.ignoreCase === expected.ignoreCase &&
actual.multiline === expected.multiline;
// 7.3. Other pairs that do not both pass typeof value == "object",
// equivalence is determined by ==.
} else if (typeof actual != 'object' && typeof expected != 'object') {
return actual == expected;
// 7.4. For all other Object pairs, including Array objects, equivalence is
// determined by having the same number of owned properties (as verified
// with Object.prototype.hasOwnProperty.call), the same set of keys
// (although not necessarily the same order), equivalent values for every
// corresponding key, and an identical "prototype" property. Note: this
// accounts for both named and indexed properties on Arrays.
} else {
return objEquiv(actual, expected);
}
}
function isUndefinedOrNull (value) {
return value === null || value === undefined;
}
function isArguments (object) {
return Object.prototype.toString.call(object) == '[object Arguments]';
}
function objEquiv (a, b) {
if (isUndefinedOrNull(a) || isUndefinedOrNull(b))
return false;
// an identical "prototype" property.
if (a.prototype !== b.prototype) return false;
//~~~I've managed to break Object.keys through screwy arguments passing.
// Converting to array solves the problem.
if (isArguments(a)) {
if (!isArguments(b)) {
return false;
}
a = pSlice.call(a);
b = pSlice.call(b);
return _deepEqual(a, b);
}
try{
var ka = _keys(a),
kb = _keys(b),
key, i;
} catch (e) {//happens when one is a string literal and the other isn't
return false;
}
// having the same number of owned properties (keys incorporates hasOwnProperty)
if (ka.length != kb.length)
return false;
//the same set of keys (although not necessarily the same order),
ka.sort();
kb.sort();
//~~~cheap key test
for (i = ka.length - 1; i >= 0; i--) {
if (ka[i] != kb[i])
return false;
}
//equivalent values for every corresponding key, and
//~~~possibly expensive deep test
for (i = ka.length - 1; i >= 0; i--) {
key = ka[i];
if (!_deepEqual(a[key], b[key] ))
return false;
}
return true;
}
// 8. The non-equivalence assertion tests for any deep inequality.
// assert.notDeepEqual(actual, expected, message_opt);
assert.notDeepEqual = function notDeepEqual(actual, expected, message) {
if (_deepEqual(actual, expected)) {
fail(actual, expected, message, "notDeepEqual", assert.notDeepEqual);
}
};
// 9. The strict equality assertion tests strict equality, as determined by ===.
// assert.strictEqual(actual, expected, message_opt);
assert.strictEqual = function strictEqual(actual, expected, message) {
if (actual !== expected) {
fail(actual, expected, message, "===", assert.strictEqual);
}
};
// 10. The strict non-equality assertion tests for strict inequality, as determined by !==.
// assert.notStrictEqual(actual, expected, message_opt);
assert.notStrictEqual = function notStrictEqual(actual, expected, message) {
if (actual === expected) {
fail(actual, expected, message, "!==", assert.notStrictEqual);
}
};
function _throws (shouldThrow, block, err, message) {
var exception = null,
threw = false,
typematters = true;
message = message || "";
//handle optional arguments
if (arguments.length == 3) {
if (typeof(err) == "string") {
message = err;
typematters = false;
}
} else if (arguments.length == 2) {
typematters = false;
}
try {
block();
} catch (e) {
threw = true;
exception = e;
}
if (shouldThrow && !threw) {
fail( "Missing expected exception"
+ (err && err.name ? " ("+err.name+")." : '.')
+ (message ? " " + message : "")
);
}
if (!shouldThrow && threw && typematters && exception instanceof err) {
fail( "Got unwanted exception"
+ (err && err.name ? " ("+err.name+")." : '.')
+ (message ? " " + message : "")
);
}
if ((shouldThrow && threw && typematters && !(exception instanceof err)) ||
(!shouldThrow && threw)) {
throw exception;
}
};
// 11. Expected to throw an error:
// assert.throws(block, Error_opt, message_opt);
assert.throws = function(block, /*optional*/error, /*optional*/message) {
_throws.apply(this, [true].concat(pSlice.call(arguments)));
};
// EXTENSION! This is annoying to write outside this module.
assert.doesNotThrow = function(block, /*optional*/error, /*optional*/message) {
_throws.apply(this, [false].concat(pSlice.call(arguments)));
};
assert.ifError = function (err) { if (err) {throw err;}};
@@ -0,0 +1,316 @@
/*!
* Nodeunit
* Copyright (c) 2010 Caolan McMahon
* MIT Licensed
*
* THIS FILE SHOULD BE BROWSER-COMPATIBLE JS!
* You can use @REMOVE_LINE_FOR_BROWSER to remove code from the browser build.
* Only code on that line will be removed, it's mostly to avoid requiring code
* that is node specific
*/
/**
* Module dependencies
*/
var async = require('../deps/async'), //@REMOVE_LINE_FOR_BROWSER
nodeunit = require('./nodeunit'), //@REMOVE_LINE_FOR_BROWSER
types = require('./types'); //@REMOVE_LINE_FOR_BROWSER
/**
* Added for browser compatibility
*/
var _keys = function (obj) {
if (Object.keys) {
return Object.keys(obj);
}
var keys = [];
for (var k in obj) {
if (obj.hasOwnProperty(k)) {
keys.push(k);
}
}
return keys;
};
var _copy = function (obj) {
var nobj = {};
var keys = _keys(obj);
for (var i = 0; i < keys.length; i += 1) {
nobj[keys[i]] = obj[keys[i]];
}
return nobj;
};
/**
* Runs a test function (fn) from a loaded module. After the test function
* calls test.done(), the callback is executed with an assertionList as its
* second argument.
*
* @param {String} name
* @param {Function} fn
* @param {Object} opt
* @param {Function} callback
* @api public
*/
exports.runTest = function (name, fn, opt, callback) {
var options = types.options(opt);
options.testStart(name);
var start = new Date().getTime();
var test = types.test(name, start, options, callback);
try {
fn(test);
}
catch (e) {
test.done(e);
}
};
/**
* Takes an object containing test functions or other test suites as properties
* and runs each in series. After all tests have completed, the callback is
* called with a list of all assertions as the second argument.
*
* If a name is passed to this function it is prepended to all test and suite
* names that run within it.
*
* @param {String} name
* @param {Object} suite
* @param {Object} opt
* @param {Function} callback
* @api public
*/
exports.runSuite = function (name, suite, opt, callback) {
suite = wrapGroup(suite);
var keys = _keys(suite);
async.concatSeries(keys, function (k, cb) {
var prop = suite[k], _name;
_name = name ? [].concat(name, k) : [k];
_name.toString = function () {
// fallback for old one
return this.join(' - ');
};
if (typeof prop === 'function') {
var in_name = false;
for (var i = 0; i < _name.length; i += 1) {
if (_name[i] === opt.testspec) {
in_name = true;
}
}
if (!opt.testspec || in_name) {
if (opt.moduleStart) {
opt.moduleStart();
}
exports.runTest(_name, suite[k], opt, cb);
}
else {
return cb();
}
}
else {
exports.runSuite(_name, suite[k], opt, cb);
}
}, callback);
};
/**
* Run each exported test function or test suite from a loaded module.
*
* @param {String} name
* @param {Object} mod
* @param {Object} opt
* @param {Function} callback
* @api public
*/
exports.runModule = function (name, mod, opt, callback) {
var options = _copy(types.options(opt));
var _run = false;
var _moduleStart = options.moduleStart;
mod = wrapGroup(mod);
function run_once() {
if (!_run) {
_run = true;
_moduleStart(name);
}
}
options.moduleStart = run_once;
var start = new Date().getTime();
exports.runSuite(null, mod, options, function (err, a_list) {
var end = new Date().getTime();
var assertion_list = types.assertionList(a_list, end - start);
options.moduleDone(name, assertion_list);
if (nodeunit.complete) {
nodeunit.complete(name, assertion_list);
}
callback(null, a_list);
});
};
/**
* Treats an object literal as a list of modules keyed by name. Runs each
* module and finished with calling 'done'. You can think of this as a browser
* safe alternative to runFiles in the nodeunit module.
*
* @param {Object} modules
* @param {Object} opt
* @api public
*/
// TODO: add proper unit tests for this function
exports.runModules = function (modules, opt) {
var all_assertions = [];
var options = types.options(opt);
var start = new Date().getTime();
async.concatSeries(_keys(modules), function (k, cb) {
exports.runModule(k, modules[k], options, cb);
},
function (err, all_assertions) {
var end = new Date().getTime();
options.done(types.assertionList(all_assertions, end - start));
});
};
/**
* Wraps a test function with setUp and tearDown functions.
* Used by testCase.
*
* @param {Function} setUp
* @param {Function} tearDown
* @param {Function} fn
* @api private
*/
var wrapTest = function (setUp, tearDown, fn) {
return function (test) {
var context = {};
if (tearDown) {
var done = test.done;
test.done = function (err) {
try {
tearDown.call(context, function (err2) {
if (err && err2) {
test._assertion_list.push(
types.assertion({error: err})
);
return done(err2);
}
done(err || err2);
});
}
catch (e) {
done(e);
}
};
}
if (setUp) {
setUp.call(context, function (err) {
if (err) {
return test.done(err);
}
fn.call(context, test);
});
}
else {
fn.call(context, test);
}
};
};
/**
* Returns a serial callback from two functions.
*
* @param {Function} funcFirst
* @param {Function} funcSecond
* @api private
*/
var getSerialCallback = function (fns) {
if (!fns.length) {
return null;
}
return function (callback) {
var that = this;
var bound_fns = [];
for (var i = 0, len = fns.length; i < len; i++) {
(function (j) {
bound_fns.push(function () {
return fns[j].apply(that, arguments);
});
})(i);
}
return async.series(bound_fns, callback);
};
};
/**
* Wraps a group of tests with setUp and tearDown functions.
* Used by testCase.
*
* @param {Object} group
* @param {Array} setUps - parent setUp functions
* @param {Array} tearDowns - parent tearDown functions
* @api private
*/
var wrapGroup = function (group, setUps, tearDowns) {
var tests = {};
var setUps = setUps ? setUps.slice(): [];
var tearDowns = tearDowns ? tearDowns.slice(): [];
if (group.setUp) {
setUps.push(group.setUp);
delete group.setUp;
}
if (group.tearDown) {
tearDowns.unshift(group.tearDown);
delete group.tearDown;
}
var keys = _keys(group);
for (var i = 0; i < keys.length; i += 1) {
var k = keys[i];
if (typeof group[k] === 'function') {
tests[k] = wrapTest(
getSerialCallback(setUps),
getSerialCallback(tearDowns),
group[k]
);
}
else if (typeof group[k] === 'object') {
tests[k] = wrapGroup(group[k], setUps, tearDowns);
}
}
return tests;
};
/**
* Backwards compatibility for test suites using old testCase API
*/
exports.testCase = function (suite) {
return suite;
};
@@ -0,0 +1,103 @@
/*!
* Nodeunit
* Copyright (c) 2010 Caolan McMahon
* MIT Licensed
*/
/**
* Module dependencies
*/
var async = require('../deps/async'),
types = require('./types'),
utils = require('./utils'),
core = require('./core'),
reporters = require('./reporters'),
assert = require('./assert'),
path = require('path'),
events = require('events');
/**
* Export sub-modules.
*/
exports.types = types;
exports.utils = utils;
exports.reporters = reporters;
exports.assert = assert;
// backwards compatibility
exports.testrunner = {
run: function () {
console.log(
'WARNING: nodeunit.testrunner is going to be deprecated, please ' +
'use nodeunit.reporters.default instead!'
);
return reporters['default'].run.apply(this, arguments);
}
};
/**
* Export all core functions
*/
for (var k in core) {
exports[k] = core[k];
};
/**
* Load modules from paths array and run all exported tests in series. If a path
* is a directory, load all supported file types inside it as modules. This only
* reads 1 level deep in the directory and does not recurse through
* sub-directories.
*
* @param {Array} paths
* @param {Object} opt
* @api public
*/
exports.runFiles = function (paths, opt) {
var all_assertions = [];
var options = types.options(opt);
var start = new Date().getTime();
if (!paths.length) {
return options.done(types.assertionList(all_assertions));
}
utils.modulePaths(paths, function (err, files) {
if (err) throw err;
async.concatSeries(files, function (file, cb) {
var name = path.basename(file);
exports.runModule(name, require(file), options, cb);
},
function (err, all_assertions) {
var end = new Date().getTime();
exports.done()
options.done(types.assertionList(all_assertions, end - start));
});
});
};
/* Export all prototypes from events.EventEmitter */
var label;
for (label in events.EventEmitter.prototype) {
exports[label] = events.EventEmitter.prototype[label];
}
/* Emit event 'complete' on completion of a test suite. */
exports.complete = function(name, assertions)
{
exports.emit('complete', name, assertions);
};
/* Emit event 'complete' on completion of all tests. */
exports.done = function()
{
exports.emit('done');
};
module.exports = exports;
@@ -0,0 +1,121 @@
/*!
* Nodeunit
* Copyright (c) 2010 Caolan McMahon
* MIT Licensed
*
* THIS FILE SHOULD BE BROWSER-COMPATIBLE JS!
* You can use @REMOVE_LINE_FOR_BROWSER to remove code from the browser build.
* Only code on that line will be removed, its mostly to avoid requiring code
* that is node specific
*/
/**
* NOTE: this test runner is not listed in index.js because it cannot be
* used with the command-line tool, only inside the browser.
*/
/**
* Reporter info string
*/
exports.info = "Browser-based test reporter";
/**
* Run all tests within each module, reporting the results
*
* @param {Array} files
* @api public
*/
exports.run = function (modules, options) {
var start = new Date().getTime(), div;
options = options || {};
div = options.div || document.body;
function setText(el, txt) {
if ('innerText' in el) {
el.innerText = txt;
}
else if ('textContent' in el){
el.textContent = txt;
}
}
function getOrCreate(tag, id) {
var el = document.getElementById(id);
if (!el) {
el = document.createElement(tag);
el.id = id;
div.appendChild(el);
}
return el;
};
var header = getOrCreate('h1', 'nodeunit-header');
var banner = getOrCreate('h2', 'nodeunit-banner');
var userAgent = getOrCreate('h2', 'nodeunit-userAgent');
var tests = getOrCreate('ol', 'nodeunit-tests');
var result = getOrCreate('p', 'nodeunit-testresult');
setText(userAgent, navigator.userAgent);
nodeunit.runModules(modules, {
moduleStart: function (name) {
/*var mheading = document.createElement('h2');
mheading.innerText = name;
results.appendChild(mheading);
module = document.createElement('ol');
results.appendChild(module);*/
},
testDone: function (name, assertions) {
var test = document.createElement('li');
var strong = document.createElement('strong');
strong.innerHTML = name + ' <b style="color: black;">(' +
'<b class="fail">' + assertions.failures() + '</b>, ' +
'<b class="pass">' + assertions.passes() + '</b>, ' +
assertions.length +
')</b>';
test.className = assertions.failures() ? 'fail': 'pass';
test.appendChild(strong);
var aList = document.createElement('ol');
aList.style.display = 'none';
test.onclick = function () {
var d = aList.style.display;
aList.style.display = (d == 'none') ? 'block': 'none';
};
for (var i=0; i<assertions.length; i++) {
var li = document.createElement('li');
var a = assertions[i];
if (a.failed()) {
li.innerHTML = (a.message || a.method || 'no message') +
'<pre>' + (a.error.stack || a.error) + '</pre>';
li.className = 'fail';
}
else {
li.innerHTML = a.message || a.method || 'no message';
li.className = 'pass';
}
aList.appendChild(li);
}
test.appendChild(aList);
tests.appendChild(test);
},
done: function (assertions) {
var end = new Date().getTime();
var duration = end - start;
var failures = assertions.failures();
banner.className = failures ? 'fail': 'pass';
result.innerHTML = 'Tests completed in ' + duration +
' milliseconds.<br/><span class="passed">' +
assertions.passes() + '</span> assertions of ' +
'<span class="all">' + assertions.length + '<span> passed, ' +
assertions.failures() + ' failed.';
}
});
};
@@ -0,0 +1,130 @@
/*!
* Nodeunit
* Copyright (c) 2010 Caolan McMahon
* MIT Licensed
*/
/**
* Module dependencies
*/
var nodeunit = require('../nodeunit'),
utils = require('../utils'),
fs = require('fs'),
track = require('../track'),
path = require('path'),
AssertionError = require('../assert').AssertionError;
/**
* Reporter info string
*/
exports.info = "Default tests reporter";
/**
* Run all tests within each module, reporting the results to the command-line.
*
* @param {Array} files
* @api public
*/
exports.run = function (files, options, callback) {
if (!options) {
// load default options
var content = fs.readFileSync(
__dirname + '/../../bin/nodeunit.json', 'utf8'
);
options = JSON.parse(content);
}
var error = function (str) {
return options.error_prefix + str + options.error_suffix;
};
var ok = function (str) {
return options.ok_prefix + str + options.ok_suffix;
};
var bold = function (str) {
return options.bold_prefix + str + options.bold_suffix;
};
var assertion_message = function (str) {
return options.assertion_prefix + str + options.assertion_suffix;
};
var start = new Date().getTime();
var tracker = track.createTracker(function (tracker) {
if (tracker.unfinished()) {
console.log('');
console.log(error(bold(
'FAILURES: Undone tests (or their setups/teardowns): '
)));
var names = tracker.names();
for (var i = 0; i < names.length; i += 1) {
console.log('- ' + names[i]);
}
console.log('');
console.log('To fix this, make sure all tests call test.done()');
process.reallyExit(tracker.unfinished());
}
});
var opts = {
testspec: options.testspec,
moduleStart: function (name) {
console.log('\n' + bold(name));
},
testDone: function (name, assertions) {
tracker.remove(name);
if (!assertions.failures()) {
console.log('✔ ' + name);
}
else {
console.log(error('✖ ' + name) + '\n');
assertions.forEach(function (a) {
if (a.failed()) {
a = utils.betterErrors(a);
if (a.error instanceof AssertionError && a.message) {
console.log(
'Assertion Message: ' +
assertion_message(a.message)
);
}
console.log(a.error.stack + '\n');
}
});
}
},
done: function (assertions, end) {
var end = end || new Date().getTime();
var duration = end - start;
if (assertions.failures()) {
console.log(
'\n' + bold(error('FAILURES: ')) + assertions.failures() +
'/' + assertions.length + ' assertions failed (' +
assertions.duration + 'ms)'
);
}
else {
console.log(
'\n' + bold(ok('OK: ')) + assertions.length +
' assertions (' + assertions.duration + 'ms)'
);
}
if (callback) callback(assertions.failures() ? new Error('We have got test failures.') : undefined);
},
testStart: function(name) {
tracker.put(name);
}
};
if (files && files.length) {
var paths = files.map(function (p) {
return path.join(process.cwd(), p);
});
nodeunit.runFiles(paths, opts);
} else {
nodeunit.runModules(files,opts);
}
};
@@ -0,0 +1,104 @@
/*!
* Nodeunit
* Copyright (c) 2010 Caolan McMahon
* MIT Licensed
*/
/**
* Module dependencies
*/
var nodeunit = require('../nodeunit'),
utils = require('../utils'),
fs = require('fs'),
track = require('../track'),
path = require('path'),
AssertionError = require('../assert').AssertionError;
/**
* Reporter info string
*/
exports.info = "Reporter for eclipse plugin";
/**
* Run all tests within each module, reporting the results to the command-line.
*
* @param {Array} files
* @api public
*/
exports.run = function (files, options, callback) {
var start = new Date().getTime();
var paths = files.map(function (p) {
if (p.indexOf('/') === 0) {
return p;
}
return path.join(process.cwd(), p);
});
var tracker = track.createTracker(function (tracker) {
if (tracker.unfinished()) {
console.log('');
console.log('FAILURES: Undone tests (or their setups/teardowns): ');
var names = tracker.names();
for (var i = 0; i < names.length; i += 1) {
console.log('- ' + names[i]);
}
console.log('');
console.log('To fix this, make sure all tests call test.done()');
process.reallyExit(tracker.unfinished());
}
});
nodeunit.runFiles(paths, {
testspec: undefined,
moduleStart: function (name) {
console.log('\n' + name);
},
testDone: function (name, assertions) {
tracker.remove(name);
if (!assertions.failures()) {
console.log('✔ ' + name);
}
else {
console.log('✖ ' + name + '\n');
assertions.forEach(function (a) {
if (a.failed()) {
a = utils.betterErrors(a);
if (a.error instanceof AssertionError && a.message) {
console.log(
'Assertion Message: ' + a.message
);
}
console.log(a.error.stack + '\n');
}
});
}
},
done: function (assertions, end) {
var end = end || new Date().getTime();
var duration = end - start;
if (assertions.failures()) {
console.log(
'\n' + 'FAILURES: ' + assertions.failures() +
'/' + assertions.length + ' assertions failed (' +
assertions.duration + 'ms)'
);
}
else {
console.log(
'\n' + 'OK: ' + assertions.length +
' assertions (' + assertions.duration + 'ms)'
);
}
if (callback) callback(assertions.failures() ? new Error('We have got test failures.') : undefined);
},
testStart: function (name) {
tracker.put(name);
}
});
};
@@ -0,0 +1,109 @@
/*!
* Nodeunit
* Copyright (c) 2010 Caolan McMahon
* MIT Licensed
*/
/**
* Module dependencies
*/
var nodeunit = require('../nodeunit'),
utils = require('../utils'),
fs = require('fs'),
path = require('path'),
AssertionError = require('assert').AssertionError;
/**
* Reporter info string
*/
exports.info = "Report tests result as HTML";
/**
* Run all tests within each module, reporting the results to the command-line.
*
* @param {Array} files
* @api public
*/
exports.run = function (files, options, callback) {
var start = new Date().getTime();
var paths = files.map(function (p) {
return path.join(process.cwd(), p);
});
console.log('<html>');
console.log('<head>');
console.log('<title></title>');
console.log('<style type="text/css">');
console.log('body { font: 12px Helvetica Neue }');
console.log('h2 { margin:0 ; padding:0 }');
console.log('pre { font: 11px Andale Mono; margin-left: 1em; padding-left: 1em; margin-top:0; font-size:smaller;}');
console.log('.assertion_message { margin-left: 1em; }');
console.log(' ol {' +
' list-style: none;' +
' margin-left: 1em;' +
' padding-left: 1em;' +
' text-indent: -1em;' +
'}');
console.log(' ol li.pass:before { content: "\\2714 \\0020"; }');
console.log(' ol li.fail:before { content: "\\2716 \\0020"; }');
console.log('</style>');
console.log('</head>');
console.log('<body>');
nodeunit.runFiles(paths, {
testspec: options.testspec,
moduleStart: function (name) {
console.log('<h2>' + name + '</h2>');
console.log('<ol>');
},
testDone: function (name, assertions) {
if (!assertions.failures()) {
console.log('<li class="pass">' + name + '</li>');
}
else {
console.log('<li class="fail">' + name);
assertions.forEach(function (a) {
if (a.failed()) {
a = utils.betterErrors(a);
if (a.error instanceof AssertionError && a.message) {
console.log('<div class="assertion_message">' +
'Assertion Message: ' + a.message +
'</div>');
}
console.log('<pre>');
console.log(a.error.stack);
console.log('</pre>');
}
});
console.log('</li>');
}
},
moduleDone: function () {
console.log('</ol>');
},
done: function (assertions) {
var end = new Date().getTime();
var duration = end - start;
if (assertions.failures()) {
console.log(
'<h3>FAILURES: ' + assertions.failures() +
'/' + assertions.length + ' assertions failed (' +
assertions.duration + 'ms)</h3>'
);
}
else {
console.log(
'<h3>OK: ' + assertions.length +
' assertions (' + assertions.duration + 'ms)</h3>'
);
}
console.log('</body>');
console.log('</html>');
if (callback) callback(assertions.failures() ? new Error('We have got test failures.') : undefined);
}
});
};
@@ -0,0 +1,14 @@
module.exports = {
'junit': require('./junit'),
'default': require('./default'),
'skip_passed': require('./skip_passed'),
'minimal': require('./minimal'),
'html': require('./html'),
'eclipse': require('./eclipse'),
'machineout': require('./machineout'),
'tap': require('./tap'),
'nested': require('./nested'),
'verbose' : require('./verbose')
// browser test reporter is not listed because it cannot be used
// with the command line tool, only inside a browser.
};
@@ -0,0 +1,179 @@
/*!
* Nodeunit
* Copyright (c) 2010 Caolan McMahon
* MIT Licensed
*/
/**
* Module dependencies
*/
var nodeunit = require('../nodeunit'),
utils = require('../utils'),
fs = require('fs'),
path = require('path'),
async = require('../../deps/async'),
AssertionError = require('assert').AssertionError,
child_process = require('child_process'),
ejs = require('../../deps/ejs');
/**
* Reporter info string
*/
exports.info = "jUnit XML test reports";
/**
* Ensures a directory exists using mkdir -p.
*
* @param {String} path
* @param {Function} callback
* @api private
*/
var ensureDir = function (path, callback) {
var mkdir = child_process.spawn('mkdir', ['-p', path]);
mkdir.on('error', function (err) {
callback(err);
callback = function(){};
});
mkdir.on('exit', function (code) {
if (code === 0) callback();
else callback(new Error('mkdir exited with code: ' + code));
});
};
/**
* Returns absolute version of a path. Relative paths are interpreted
* relative to process.cwd() or the cwd parameter. Paths that are already
* absolute are returned unaltered.
*
* @param {String} p
* @param {String} cwd
* @return {String}
* @api public
*/
var abspath = function (p, /*optional*/cwd) {
if (p[0] === '/') return p;
cwd = cwd || process.cwd();
return path.normalize(path.join(cwd, p));
};
/**
* Run all tests within each module, reporting the results to the command-line,
* then writes out junit-compatible xml documents.
*
* @param {Array} files
* @api public
*/
exports.run = function (files, opts, callback) {
if (!opts.output) {
console.error(
'Error: No output directory defined.\n' +
'\tEither add an "output" property to your nodeunit.json config ' +
'file, or\n\tuse the --output command line option.'
);
return;
}
opts.output = abspath(opts.output);
var error = function (str) {
return opts.error_prefix + str + opts.error_suffix;
};
var ok = function (str) {
return opts.ok_prefix + str + opts.ok_suffix;
};
var bold = function (str) {
return opts.bold_prefix + str + opts.bold_suffix;
};
var start = new Date().getTime();
var paths = files.map(function (p) {
return path.join(process.cwd(), p);
});
var modules = {}
var curModule;
nodeunit.runFiles(paths, {
testspec: opts.testspec,
moduleStart: function (name) {
curModule = {
errorCount: 0,
failureCount: 0,
tests: 0,
testcases: [],
name: name
};
modules[name] = curModule;
},
testDone: function (name, assertions) {
var testcase = {name: name};
for (var i=0; i<assertions.length; i++) {
var a = assertions[i];
if (a.failed()) {
a = utils.betterErrors(a);
testcase.failure = {
message: a.message,
backtrace: a.error.stack
};
if (a.error instanceof AssertionError) {
curModule.failureCount++;
}
else {
curModule.errorCount++;
}
break;
}
}
curModule.tests++;
curModule.testcases.push(testcase);
},
done: function (assertions) {
var end = new Date().getTime();
var duration = end - start;
ensureDir(opts.output, function (err) {
var tmpl = __dirname + "/../../share/junit.xml.ejs";
fs.readFile(tmpl, function (err, data) {
if (err) throw err;
var tmpl = data.toString();
for(var k in modules) {
var module = modules[k];
var rendered = ejs.render(tmpl, {
locals: {suites: [module]}
});
var filename = path.join(
opts.output,
module.name + '.xml'
);
console.log('Writing ' + filename);
fs.writeFileSync(filename, rendered, 'utf8');
}
if (assertions.failures()) {
console.log(
'\n' + bold(error('FAILURES: ')) +
assertions.failures() + '/' +
assertions.length + ' assertions failed (' +
assertions.duration + 'ms)'
);
}
else {
console.log(
'\n' + bold(ok('OK: ')) + assertions.length +
' assertions (' + assertions.duration + 'ms)'
);
}
if (callback) callback(assertions.failures() ? new Error('We have got test failures.') : undefined);
});
});
}
});
}
@@ -0,0 +1,111 @@
/*!
* Nodeunit
*
* @author Alisue (lambdalisue@hashnote.net)
* @url http://hashnote.net/
*
* Copyright (c) 2011 Alisue
* MIT Licensed
*/
/**
* Module dependencies
*/
var nodeunit = require('../nodeunit'),
utils = require('../utils'),
fs = require('fs'),
track = require('../track'),
path = require('path'),
AssertionError = require('../assert').AssertionError;
/**
* Reporter info string
*/
exports.info = "Tests reporter for machinally analysis";
/**
* Run all tests within each module, reporting the results to the command-line.
*
* @param {Array} files
* @api public
*/
exports.run = function (files, options, callback) {
// options doesn't effect
var parseStack = function (stack, delimiter) {
var parseTrace = function (trace) {
var filename, row, column;
pattern1 = /\s{4}at\s\S+\s\(([^:]+):(\d+):(\d+)\)/;
pattern2 = /\s{4}at\s([^:]+):(\d+):(\d+)/;
if (trace.match(pattern1) !== null) {
filename = RegExp.$1;
row = RegExp.$2;
column = RegExp.$3;
} else if (trace.match(pattern2) !== null) {
filename = RegExp.$1;
row = RegExp.$2;
column = RegExp.$3;
} else {
throw new Error("Could not parse a line of stack trace: " + trace);
}
return {filename: filename, row: row, column: column};
};
if (delimiter === undefined) {
delimiter = ':';
}
traceback = stack.split('\n');
firstline = traceback.shift();
trace = parseTrace(traceback[0]);
return {filename: trace.filename, row: trace.row, column: trace.column, message: firstline};
};
var createErrorMessage = function(type, name, filename, row, column, message){
return [type, name, filename, row, column, message].join(":");
};
var paths = files.map(function (p) {
return path.join(process.cwd(), p);
});
var tracker = track.createTracker(function (tracker) {
if (tracker.unfinished()) {
var names = tracker.names();
for (var i = 0; i < names.length; i += 1) {
console.log(createErrorMessage(
'Error', names[i],
'', '', '',
'Undone tests - To fix this, make sure all tests call test.done()'
));
}
process.reallyExit(tracker.unfinished());
}
});
nodeunit.runFiles(paths, {
testspec: options.testspec,
moduleStart: function (name) {},
testDone: function (name, assertions) {
tracker.remove(name);
if (assertions.failures()) {
assertions.forEach(function (a) {
var stacks, message, filename, row, column;
if (a.failed()) {
stackinfo = parseStack(a.error.stack, ':');
console.log(createErrorMessage(
'Fail', name, stackinfo.filename,
stackinfo.row, stackinfo.column, stackinfo.message));
}
});
}
},
done: function (assertions, end) {
if (callback) callback(assertions.failures() ? new Error('We have got test failures.') : undefined);
},
testStart: function(name) {
tracker.put(name);
}
});
};
@@ -0,0 +1,120 @@
/*!
* Nodeunit
* Copyright (c) 2010 Caolan McMahon
* MIT Licensed
*/
/**
* Module dependencies
*/
var nodeunit = require('../nodeunit'),
utils = require('../utils'),
fs = require('fs'),
path = require('path'),
AssertionError = require('assert').AssertionError;
/**
* Reporter info string
*/
exports.info = "Pretty minimal output";
/**
* Run all tests within each module, reporting the results to the command-line.
*
* @param {Array} files
* @api public
*/
exports.run = function (files, options, callback) {
if (!options) {
// load default options
var content = fs.readFileSync(
__dirname + '/../../bin/nodeunit.json', 'utf8'
);
options = JSON.parse(content);
}
var red = function (str) {
return options.error_prefix + str + options.error_suffix;
};
var green = function (str) {
return options.ok_prefix + str + options.ok_suffix;
};
var magenta = function (str) {
return options.assertion_prefix + str + options.assertion_suffix;
};
var bold = function (str) {
return options.bold_prefix + str + options.bold_suffix;
};
var start = new Date().getTime();
var opts = {
testspec: options.testspec,
moduleStart: function (name) {
process.stdout.write(bold(name) + ': ');
},
moduleDone: function (name, assertions) {
console.log('');
if (assertions.failures()) {
assertions.forEach(function (a) {
if (a.failed()) {
a = utils.betterErrors(a);
if (a.error instanceof AssertionError && a.message) {
console.log(
'Assertion in test ' + bold(a.testname) + ': ' +
magenta(a.message)
);
}
console.log(a.error.stack + '\n');
}
});
}
},
testStart: function () {
},
testDone: function (name, assertions) {
if (!assertions.failures()) {
process.stdout.write('.');
}
else {
process.stdout.write(red('F'));
assertions.forEach(function (assertion) {
assertion.testname = name;
});
}
},
done: function (assertions) {
var end = new Date().getTime();
var duration = end - start;
if (assertions.failures()) {
console.log(
'\n' + bold(red('FAILURES: ')) + assertions.failures() +
'/' + assertions.length + ' assertions failed (' +
assertions.duration + 'ms)'
);
}
else {
console.log(
'\n' + bold(green('OK: ')) + assertions.length +
' assertions (' + assertions.duration + 'ms)'
);
}
if (callback) callback(assertions.failures() ? new Error('We have got test failures.') : undefined);
}
};
if (files && files.length) {
var paths = files.map(function (p) {
return path.join(process.cwd(), p);
});
nodeunit.runFiles(paths, opts);
} else {
nodeunit.runModules(files,opts);
}
};
@@ -0,0 +1,213 @@
/*!
* Nodeunit
* Copyright (c) 2010 Caolan McMahon
* MIT Licensed
*/
/**
* Module dependencies
*/
var nodeunit = require('../nodeunit'),
utils = require('../utils'),
fs = require('fs'),
track = require('../track'),
path = require('path'),
AssertionError = require('../assert').AssertionError;
/**
* Reporter info string
*/
exports.info = "Nested test reporter";
/**
* Run all tests within each module, reporting the results to the command-line.
*
* @param {Array} files
* @api public
*/
exports.run = function (files, options) {
if (!options) {
// load default options
var content = fs.readFileSync(
__dirname + '/../../bin/nodeunit.json',
'utf8'
);
options = JSON.parse(content);
}
var error = function (str) {
return options.error_prefix + str + options.error_suffix;
};
var ok = function (str) {
return options.ok_prefix + str + options.ok_suffix;
};
var bold = function (str) {
return options.bold_prefix + str + options.bold_suffix;
};
var assertion_message = function (str) {
return options.assertion_prefix + str + options.assertion_suffix;
};
var spaces_per_indent = options.spaces_per_indent || 4;
var start = new Date().getTime();
var paths = files.map(function (p) {
return path.join(process.cwd(), p);
});
var tracker = track.createTracker(function (tracker) {
var i, names;
if (tracker.unfinished()) {
console.log('');
console.log(error(bold(
'FAILURES: Undone tests (or their setups/teardowns): '
)));
names = tracker.names();
for (i = 0; i < names.length; i += 1) {
console.log('- ' + names[i]);
}
console.log('');
console.log('To fix this, make sure all tests call test.done()');
process.reallyExit(tracker.unfinished());
}
});
// Object to hold status of each 'part' of the testCase/name array,
// i.e., whether this part has been printed yet.
tracker.already_printed = {};
var pass_text = function (txt) {
// Print in bold green.
return bold(ok(txt + " (pass)"));
};
var fail_text = function (txt) {
return bold(error(txt + " (fail) ✖ "));
};
var status_text = function (txt, status) {
if (status === 'pass') {
return pass_text(txt);
} else {
return fail_text(txt);
}
};
/**
* Slices an array, returns a string by joining the sliced elements.
* @example
* > name_slice(['TC1', 'TC1.1', 'mytest'], 1);
* "TC1,TC1.1"
*/
var name_slice = function (name_arr, end_index) {
return name_arr.slice(0, end_index + 1).join(",");
};
var indent = (function () {
var txt = '';
var i;
for (i = 0; i < spaces_per_indent; i++) {
txt += ' ';
}
return txt;
}());
// Indent once for each indent_level
var add_indent = function (txt, indent_level) {
var k;
for (k = 0; k < indent_level; k++) {
txt += indent;
}
return txt;
};
// If it's not the last element of the name_arr, it's a testCase.
var is_testCase = function (name_arr, index) {
return index === name_arr.length - 1 ? false : true;
};
var testCase_line = function (txt) {
return txt + "\n";
};
/**
* Prints (console.log) the nested test status line(s).
*
* @param {Array} name_arr - Array of name elements.
* @param {String} status - either 'pass' or 'fail'.
* @example
* > print_status(['TC1', 'TC1.1', 'mytest'], 'pass');
* TC1
* TC1.1
* mytest (pass)
*/
var print_status = function (name_arr, status) {
var txt = '';
var _name_slice, part, i;
for (i = 0; i < name_arr.length; i++) {
_name_slice = name_slice(name_arr, i);
part = name_arr[i];
if (!tracker.already_printed[_name_slice]) {
txt = add_indent(txt, i);
if (is_testCase(name_arr, i)) {
txt += testCase_line(part);
} else {
txt += status_text(part, status);
}
tracker.already_printed[_name_slice] = true;
}
}
console.log(txt);
};
nodeunit.runFiles(paths, {
testspec: options.testspec,
moduleStart: function (name) {
console.log('\n' + bold(name));
},
testDone: function (name, assertions) {
tracker.remove(name);
if (!assertions.failures()) {
print_status(name, 'pass');
} else {
print_status(name, 'fail');
assertions.forEach(function (a) {
if (a.failed()) {
a = utils.betterErrors(a);
if (a.error instanceof AssertionError && a.message) {
console.log(
'Assertion Message: ' +
assertion_message(a.message)
);
}
console.log(a.error.stack + '\n');
}
});
}
},
done: function (assertions, end) {
end = end || new Date().getTime();
var duration = end - start;
if (assertions.failures()) {
console.log(
'\n' + bold(error('FAILURES: ')) + assertions.failures() +
'/' + assertions.length + ' assertions failed (' +
assertions.duration + 'ms)'
);
} else {
console.log(
'\n' + bold(ok('OK: ')) + assertions.length +
' assertions (' + assertions.duration + 'ms)'
);
}
},
testStart: function (name) {
tracker.put(name);
}
});
};
@@ -0,0 +1,107 @@
/*!
* Nodeunit
* Copyright (c) 2010 Caolan McMahon
* MIT Licensed
*/
/**
* Module dependencies
*/
var nodeunit = require('../nodeunit'),
utils = require('../utils'),
fs = require('fs'),
path = require('path'),
AssertionError = require('assert').AssertionError;
/**
* Reporter info string
*/
exports.info = "Skip passed tests output";
/**
* Run all tests within each module, reporting the results to the command-line.
*
* @param {Array} files
* @api public
*/
exports.run = function (files, options, callback) {
if (!options) {
// load default options
var content = fs.readFileSync(
__dirname + '/../../bin/nodeunit.json', 'utf8'
);
options = JSON.parse(content);
}
var error = function (str) {
return options.error_prefix + str + options.error_suffix;
};
var ok = function (str) {
return options.ok_prefix + str + options.ok_suffix;
};
var bold = function (str) {
return options.bold_prefix + str + options.bold_suffix;
};
var assertion_message = function (str) {
return options.assertion_prefix + str + options.assertion_suffix;
};
var start = new Date().getTime();
var paths = files.map(function (p) {
return path.join(process.cwd(), p);
});
nodeunit.runFiles(paths, {
testspec: options.testspec,
moduleStart: function (name) {
console.log('\n' + bold(name));
},
testDone: function (name, assertions) {
if (assertions.failures()) {
console.log(error('✖ ' + name) + '\n');
assertions.forEach(function (a) {
if (a.failed()) {
a = utils.betterErrors(a);
if (a.error instanceof AssertionError && a.message) {
console.log(
'Assertion Message: ' + assertion_message(a.message)
);
}
console.log(a.error.stack + '\n');
}
});
}
},
moduleDone: function (name, assertions) {
if (!assertions.failures()) {
console.log('✔ all tests passed');
}
else {
console.log(error('✖ some tests failed'));
}
},
done: function (assertions) {
var end = new Date().getTime();
var duration = end - start;
if (assertions.failures()) {
console.log(
'\n' + bold(error('FAILURES: ')) + assertions.failures() +
'/' + assertions.length + ' assertions failed (' +
assertions.duration + 'ms)'
);
}
else {
console.log(
'\n' + bold(ok('OK: ')) + assertions.length +
' assertions (' + assertions.duration + 'ms)'
);
}
if (callback) callback(assertions.failures() ? new Error('We have got test failures.') : undefined);
}
});
};
@@ -0,0 +1,65 @@
/**
* Module dependencies
*/
var nodeunit = require('../nodeunit'),
path = require('path'),
assert = require('tap-assert'),
TapProducer = require('tap-producer');
/**
* Reporter info string
*/
exports.info = "TAP output";
/**
* Run all tests within each module, reporting the results to the command-line.
*
* @param {Array} files
* @api public
*/
exports.run = function (files, options) {
if (!options) {
// load default options
var content = fs.readFileSync(
__dirname + '/../../bin/nodeunit.json', 'utf8'
);
options = JSON.parse(content);
}
var paths = files.map(function (p) {
return path.join(process.cwd(), p);
});
var output = new TapProducer();
output.pipe(process.stdout);
nodeunit.runFiles(paths, {
testStart: function (name) {
output.write(name.toString());
},
testDone: function (name, assertions) {
assertions.forEach(function (e) {
var extra = {};
if (e.error) {
extra.error = {
name: e.error.name,
message: e.error.message,
stack: e.error.stack.split(/\n/).filter(function (line) {
// exclude line of "types.js"
return ! RegExp(/types.js:83:39/).test(line);
}).join('\n')
};
extra.wanted = e.error.expected;
extra.found = e.error.actual;
}
output.write(assert(e.passed(), e.message, extra));
});
},
done: function (assertions) {
output.end();
}
});
};
@@ -0,0 +1,122 @@
/*!
* Nodeunit
* Copyright (c) 2010 Caolan McMahon
* MIT Licensed
*/
/**
* Module dependencies
*/
var nodeunit = require('../nodeunit'),
utils = require('../utils'),
fs = require('fs'),
track = require('../track'),
path = require('path');
AssertionError = require('../assert').AssertionError;
/**
* Reporter info string
*/
exports.info = "Verbose tests reporter"
/**
* Run all tests within each module, reporting the results to the command-line.
*
* @param {Array} files
* @api public
*/
exports.run = function (files, options) {
if (!options) {
// load default options
var content = fs.readFileSync(
__dirname + '/../../bin/nodeunit.json', 'utf8'
);
options = JSON.parse(content);
}
var error = function (str) {
return options.error_prefix + str + options.error_suffix;
};
var ok = function (str) {
return options.ok_prefix + str + options.ok_suffix;
};
var bold = function (str) {
return options.bold_prefix + str + options.bold_suffix;
};
var assertion_message = function (str) {
return options.assertion_prefix + str + options.assertion_suffix;
};
var start = new Date().getTime();
var paths = files.map(function (p) {
return path.join(process.cwd(), p);
});
var tracker = track.createTracker(function (tracker) {
if (tracker.unfinished()) {
console.log('');
console.log(error(bold(
'FAILURES: Undone tests (or their setups/teardowns): '
)));
var names = tracker.names();
for (var i = 0; i < names.length; i += 1) {
console.log('- ' + names[i]);
}
console.log('');
console.log('To fix this, make sure all tests call test.done()');
process.reallyExit(tracker.unfinished());
}
});
nodeunit.runFiles(paths, {
testspec: options.testspec,
moduleStart: function (name) {
console.log('\n' + bold(name));
},
testDone: function (name, assertions) {
tracker.remove(name);
if (!assertions.failures()) {
console.log('✔ ' + name);
}
else {
console.log(error('✖ ' + name));
}
// verbose so print everything
assertions.forEach(function (a) {
if (a.failed()) {
console.log(error(' ✖ ' + a.message));
a = utils.betterErrors(a);
console.log(' ' + a.error.stack);
}
else {
console.log(' ✔ ' + a.message);
}
});
},
done: function (assertions, end) {
var end = end || new Date().getTime();
var duration = end - start;
if (assertions.failures()) {
console.log(
'\n' + bold(error('FAILURES: ')) + assertions.failures() +
'/' + assertions.length + ' assertions failed (' +
assertions.duration + 'ms)'
);
}
else {
console.log(
'\n' + bold(ok('OK: ')) + assertions.length +
' assertions (' + assertions.duration + 'ms)'
);
}
},
testStart: function(name) {
tracker.put(name);
}
});
};
@@ -0,0 +1,48 @@
/*!
* Simple util module to track tests. Adds a process.exit hook to print
* the undone tests.
*/
exports.createTracker = function (on_exit) {
var names = {};
var tracker = {
names: function () {
var arr = [];
for (var k in names) {
if (names.hasOwnProperty(k)) {
arr.push(k);
}
}
return arr;
},
unfinished: function () {
return tracker.names().length;
},
put: function (testname) {
names[testname] = testname;
},
remove: function (testname) {
delete names[testname];
}
};
process.on('exit', function() {
on_exit = on_exit || exports.default_on_exit;
on_exit(tracker);
});
return tracker;
};
exports.default_on_exit = function (tracker) {
if (tracker.unfinished()) {
console.log('');
console.log('Undone tests (or their setups/teardowns): ');
var names = tracker.names();
for (var i = 0; i < names.length; i += 1) {
console.log(names[i]);
}
process.reallyExit(tracker.unfinished());
}
};
@@ -0,0 +1,189 @@
/*!
* Nodeunit
* Copyright (c) 2010 Caolan McMahon
* MIT Licensed
*
* THIS FILE SHOULD BE BROWSER-COMPATIBLE JS!
* You can use @REMOVE_LINE_FOR_BROWSER to remove code from the browser build.
* Only code on that line will be removed, it's mostly to avoid requiring code
* that is node specific
*/
/**
* Module dependencies
*/
var assert = require('./assert'), //@REMOVE_LINE_FOR_BROWSER
async = require('../deps/async'); //@REMOVE_LINE_FOR_BROWSER
/**
* Creates assertion objects representing the result of an assert call.
* Accepts an object or AssertionError as its argument.
*
* @param {object} obj
* @api public
*/
exports.assertion = function (obj) {
return {
method: obj.method || '',
message: obj.message || (obj.error && obj.error.message) || '',
error: obj.error,
passed: function () {
return !this.error;
},
failed: function () {
return Boolean(this.error);
}
};
};
/**
* Creates an assertion list object representing a group of assertions.
* Accepts an array of assertion objects.
*
* @param {Array} arr
* @param {Number} duration
* @api public
*/
exports.assertionList = function (arr, duration) {
var that = arr || [];
that.failures = function () {
var failures = 0;
for (var i = 0; i < this.length; i += 1) {
if (this[i].failed()) {
failures += 1;
}
}
return failures;
};
that.passes = function () {
return that.length - that.failures();
};
that.duration = duration || 0;
return that;
};
/**
* Create a wrapper function for assert module methods. Executes a callback
* after it's complete with an assertion object representing the result.
*
* @param {Function} callback
* @api private
*/
var assertWrapper = function (callback) {
return function (new_method, assert_method, arity) {
return function () {
var message = arguments[arity - 1];
var a = exports.assertion({method: new_method, message: message});
try {
assert[assert_method].apply(null, arguments);
}
catch (e) {
a.error = e;
}
callback(a);
};
};
};
/**
* Creates the 'test' object that gets passed to every test function.
* Accepts the name of the test function as its first argument, followed by
* the start time in ms, the options object and a callback function.
*
* @param {String} name
* @param {Number} start
* @param {Object} options
* @param {Function} callback
* @api public
*/
exports.test = function (name, start, options, callback) {
var expecting;
var a_list = [];
var wrapAssert = assertWrapper(function (a) {
a_list.push(a);
if (options.log) {
async.nextTick(function () {
options.log(a);
});
}
});
var test = {
done: function (err) {
if (expecting !== undefined && expecting !== a_list.length) {
var e = new Error(
'Expected ' + expecting + ' assertions, ' +
a_list.length + ' ran'
);
var a1 = exports.assertion({method: 'expect', error: e});
a_list.push(a1);
if (options.log) {
async.nextTick(function () {
options.log(a1);
});
}
}
if (err) {
var a2 = exports.assertion({error: err});
a_list.push(a2);
if (options.log) {
async.nextTick(function () {
options.log(a2);
});
}
}
var end = new Date().getTime();
async.nextTick(function () {
var assertion_list = exports.assertionList(a_list, end - start);
options.testDone(name, assertion_list);
callback(null, a_list);
});
},
ok: wrapAssert('ok', 'ok', 2),
same: wrapAssert('same', 'deepEqual', 3),
equals: wrapAssert('equals', 'equal', 3),
expect: function (num) {
expecting = num;
},
_assertion_list: a_list
};
// add all functions from the assert module
for (var k in assert) {
if (assert.hasOwnProperty(k)) {
test[k] = wrapAssert(k, k, assert[k].length);
}
}
return test;
};
/**
* Ensures an options object has all callbacks, adding empty callback functions
* if any are missing.
*
* @param {Object} opt
* @return {Object}
* @api public
*/
exports.options = function (opt) {
var optionalCallback = function (name) {
opt[name] = opt[name] || function () {};
};
optionalCallback('moduleStart');
optionalCallback('moduleDone');
optionalCallback('testStart');
optionalCallback('testDone');
//optionalCallback('log');
// 'done' callback is not optional.
return opt;
};
@@ -0,0 +1,209 @@
/*!
* Nodeunit
* Copyright (c) 2010 Caolan McMahon
* MIT Licensed
*/
/**
* Module dependencies
*/
var async = require('../deps/async'),
fs = require('fs'),
util = require('util'),
Script = process.binding('evals').Script || process.binding('evals').NodeScript,
http = require('http');
/**
* Detect if coffee-script is available and search for .coffee as an
* extension in modulePaths if it is.
*/
var extensionPattern;
try {
require('coffee-script');
extensionPattern = /\.(?:js|coffee)$/;
}
catch (e) {
extensionPattern = /\.js$/;
}
/**
* Finds all modules at each path in an array, If a path is a directory, it
* returns all supported file types inside it. This only reads 1 level deep in
* the directory and does not recurse through sub-directories.
*
* The extension (.js, .coffee etc) is stripped from the filenames so they can
* simply be require()'ed.
*
* @param {Array} paths
* @param {Function} callback
* @api public
*/
exports.modulePaths = function (paths, callback) {
async.concat(paths, function (p, cb) {
fs.stat(p, function (err, stats) {
if (err) {
return cb(err);
}
if (stats.isFile()) {
return cb(null, [p]);
}
if (stats.isDirectory()) {
fs.readdir(p, function (err, files) {
if (err) {
return cb(err);
}
// filter out any filenames with unsupported extensions
var modules = files.filter(function (filename) {
return extensionPattern.exec(filename);
});
// remove extension from module name and prepend the
// directory path
var fullpaths = modules.map(function (filename) {
var mod_name = filename.replace(extensionPattern, '');
return [p, mod_name].join('/');
});
// sort filenames here, because Array.map changes order
fullpaths.sort();
cb(null, fullpaths);
});
}
});
}, callback);
};
/**
* Evaluates JavaScript files in a sandbox, returning the context. The first
* argument can either be a single filename or an array of filenames. If
* multiple filenames are given their contents are concatenated before
* evalution. The second argument is an optional context to use for the sandbox.
*
* @param files
* @param {Object} sandbox
* @return {Object}
* @api public
*/
exports.sandbox = function (files, /*optional*/sandbox) {
var source, script, result;
if (!(files instanceof Array)) {
files = [files];
}
source = files.map(function (file) {
return fs.readFileSync(file, 'utf8');
}).join('');
if (!sandbox) {
sandbox = {};
}
script = new Script(source);
result = script.runInNewContext(sandbox);
return sandbox;
};
/**
* Provides a http request, response testing environment.
*
* Example:
*
* var httputil = require('nodeunit').utils.httputil
* exports.testSomething = function(test) {
* httputil(function (req, resp) {
* resp.writeHead(200, {});
* resp.end('test data');
* },
* function(server, client) {
* client.fetch('GET', '/', {}, function(resp) {
* test.equal('test data', resp.body);
* server.close();
* test.done();
* })
* });
* };
*
* @param {Function} cgi
* @param {Function} envReady
* @api public
*/
exports.httputil = function (cgi, envReady) {
var hostname = process.env.HOSTNAME || 'localhost';
var port = process.env.PORT || 3000;
var server = http.createServer(cgi);
server.listen(port, hostname);
var client = http.createClient(port, hostname);
client.fetch = function (method, path, headers, respReady) {
var request = this.request(method, path, headers);
request.end();
request.on('response', function (response) {
response.setEncoding('utf8');
response.on('data', function (chunk) {
if (response.body) {
response.body += chunk;
} else {
response.body = chunk;
}
});
response.on('end', function () {
if (response.headers['content-type'] === 'application/json') {
response.bodyAsObject = JSON.parse(response.body);
}
respReady(response);
});
});
};
process.nextTick(function () {
if (envReady && typeof envReady === 'function') {
envReady(server, client);
}
});
};
/**
* Improves formatting of AssertionError messages to make deepEqual etc more
* readable.
*
* @param {Object} assertion
* @return {Object}
* @api public
*/
exports.betterErrors = function (assertion) {
if (!assertion.error) return;
var e = assertion.error;
// deepEqual error message is a bit sucky, lets improve it!
// e.actual and e.expected could be null or undefined, so
// using getOwnPropertyDescriptor to see if they exist:
if (Object.getOwnPropertyDescriptor(e, 'actual') &&
Object.getOwnPropertyDescriptor(e, 'expected')) {
// alexgorbatchev 2010-10-22 :: Added a bit of depth to inspection
var actual = util.inspect(e.actual, false, 10).replace(/\n$/, '');
var expected = util.inspect(e.expected, false, 10).replace(/\n$/, '');
var multiline = (
actual.indexOf('\n') !== -1 ||
expected.indexOf('\n') !== -1
);
var spacing = (multiline ? '\n' : ' ');
e._message = e.message;
e.stack = (
e.name + ':' + spacing +
actual + spacing + e.operator + spacing +
expected + '\n' +
e.stack.split('\n').slice(1).join('\n')
);
}
return assertion;
};
@@ -0,0 +1,95 @@
.\" Generated with Ronnjs/v0.1
.\" http://github.com/kapouer/ronnjs/
.
.TH "NODEUNIT" "1" "October 2010" "" ""
.
.SH "NAME"
\fBnodeunit\fR \-\- simple node\.js unit testing tool
.
.SH "SYNOPSIS"
.
.nf
nodeunit [options] <file\-or\-directory> [<file\-or\-directory> \.\.\.]
.
.fi
.
.SH "DESCRIPTION"
Nodeunit is a simple unit testing tool based on the node\.js assert module\.
.
.IP "\(bu" 4
Simple to use
.
.IP "\(bu" 4
Just export the tests from a module
.
.IP "\(bu" 4
Helps you avoid common pitfalls when testing asynchronous code
.
.IP "\(bu" 4
Easy to add test cases with setUp and tearDown functions if you wish
.
.IP "\(bu" 4
Allows the use of mocks and stubs
.
.IP "" 0
.
.SH "OPTIONS"
\fB\-\-config FILE\fR:
.
.br
Load config options from a JSON file, allows the customisation
of color schemes for the default test reporter etc\.
See bin/nodeunit\.json for current available options\.
.
.P
\fB\-\-reporter FILE\fR:
.
.br
You can set the test reporter to a custom module or on of the modules
in nodeunit/lib/reporters, when omitted, the default test runner is used\.
.
.P
\fB\-\-list\-reporters\fR:
.
.br
List available build\-in reporters\.
.
.P
\fB\-h\fR, \fB\-\-help\fR:
.
.br
Display the help and exit\.
.
.P
\fB\-v\fR, \fB\-\-version\fR:
.
.br
Output version information and exit\.
.
.P
\fB<file\-or\-directory>\fR:
You can run nodeunit on specific files or on all \fI*\.js\fR files inside
.
.br
a directory\.
.
.SH "AUTHORS"
Written by Caolan McMahon and other nodeunit contributors\.
.
.br
Contributors list: \fIhttp://github\.com/caolan/nodeunit/contributors\fR\|\.
.
.SH "REPORTING BUGS"
Report nodeunit bugs to \fIhttp://github\.com/caolan/nodeunit/issues\fR\|\.
.
.SH "COPYRIGHT"
Copyright © 2010 Caolan McMahon\.
.
.br
Nodeunit has been released under the MIT license:
.
.br
\fIhttp://github\.com/caolan/nodeunit/raw/master/LICENSE\fR\|\.
.
.SH "SEE ALSO"
node(1)
@@ -0,0 +1,7 @@
//See: http://www.jslint.com/lint.html#options
var options = {
//white: false, // if false, strict whitespace rules should be enforced.
indent: 4,
onevar: false,
vars: true // allow multiple var statement per function.
};
@@ -0,0 +1,65 @@
{ "name": "nodeunit"
, "description": "Easy unit testing for node.js and the browser."
, "maintainers":
[ { "name": "Caolan McMahon"
, "web": "https://github.com/caolan"
}
]
, "contributors" :
[ { "name": "Romain Beauxis"
, "web": "https://github.com/toots"
}
, { "name": "Alex Gorbatchev"
, "web": "https://github.com/alexgorbatchev"
}
, { "name": "Alex Wolfe"
, "web": "https://github.com/alexkwolfe"
}
, { "name": "Carl Fürstenberg"
, "web": "https://github.com/azatoth"
}
, { "name": "Gerad Suyderhoud"
, "web": "https://github.com/gerad"
}
, { "name": "Kadir Pekel"
, "web": "https://github.com/coffeemate"
}
, { "name": "Oleg Efimov"
, "web": "https://github.com/Sannis"
}
, { "name": "Orlando Vazquez"
, "web": "https://github.com/orlandov"
}
, { "name": "Ryan Dahl"
, "web": "https://github.com/ry"
}
, { "name": "Sam Stephenson"
, "web": "https://github.com/sstephenson"
}
, { "name": "Thomas Mayfield"
, "web": "https://github.com/thegreatape"
}
, { "name": "Elijah Insua <tmpvar@gmail.com>",
"web": "http://tmpvar.com"
}
]
, "version": "0.6.4"
, "repository" :
{ "type" : "git"
, "url" : "http://github.com/caolan/nodeunit.git"
}
, "devDependencies":
{ "uglify-js": ">=1.1.0" }
, "bugs" : { "url" : "http://github.com/caolan/nodeunit/issues" }
, "licenses" :
[ { "type" : "MIT"
, "url" : "http://github.com/caolan/nodeunit/raw/master/LICENSE"
}
]
, "directories" : { "lib": "./lib", "doc" : "./doc", "man" : "./man1" }
, "bin" : { "nodeunit" : "./bin/nodeunit" }
, "dependencies" :
{ "tap-assert": ">=0.0.9"
, "tap-producer": ">=0.0.1"
}
}
@@ -0,0 +1,19 @@
<?xml version="1.0" encoding="UTF-8" ?>
<% for (var i=0; i < suites.length; i++) { %>
<% var suite=suites[i]; %>
<testsuite name="<%= suite.name %>"
errors="<%= suite.errorCount %>"
failures="<%= suite.failureCount %>"
tests="<%= suite.tests %>">
<% for (var j=0; j < suite.testcases.length; j++) { %>
<% var testcase=suites[i].testcases[j]; %>
<testcase name="<%= testcase.name %>">
<% if (testcase.failure) { %>
<failure message="<%= testcase.failure.message %>">
<% if (testcase.failure.backtrace) { %><%= testcase.failure.backtrace %><% } %>
</failure>
<% } %>
</testcase>
<% } %>
</testsuite>
<% } %>
@@ -0,0 +1,11 @@
/*!
* Nodeunit
* https://github.com/caolan/nodeunit
* Copyright (c) 2010 Caolan McMahon
* MIT Licensed
*
* json2.js
* http://www.JSON.org/json2.js
* Public Domain.
* NO WARRANTY EXPRESSED OR IMPLIED. USE AT YOUR OWN RISK.
*/
@@ -0,0 +1,70 @@
/*!
* Styles taken from qunit.css
*/
h1#nodeunit-header, h1.nodeunit-header {
padding: 15px;
font-size: large;
background-color: #06b;
color: white;
font-family: 'trebuchet ms', verdana, arial;
margin: 0;
}
h1#nodeunit-header a {
color: white;
}
h2#nodeunit-banner {
height: 2em;
border-bottom: 1px solid white;
background-color: #eee;
margin: 0;
font-family: 'trebuchet ms', verdana, arial;
}
h2#nodeunit-banner.pass {
background-color: green;
}
h2#nodeunit-banner.fail {
background-color: red;
}
h2#nodeunit-userAgent, h2.nodeunit-userAgent {
padding: 10px;
background-color: #eee;
color: black;
margin: 0;
font-size: small;
font-weight: normal;
font-family: 'trebuchet ms', verdana, arial;
font-size: 10pt;
}
div#nodeunit-testrunner-toolbar {
background: #eee;
border-top: 1px solid black;
padding: 10px;
font-family: 'trebuchet ms', verdana, arial;
margin: 0;
font-size: 10pt;
}
ol#nodeunit-tests {
font-family: 'trebuchet ms', verdana, arial;
font-size: 10pt;
}
ol#nodeunit-tests li strong {
cursor:pointer;
}
ol#nodeunit-tests .pass {
color: green;
}
ol#nodeunit-tests .fail {
color: red;
}
p#nodeunit-testresult {
margin-left: 1em;
font-size: 10pt;
font-family: 'trebuchet ms', verdana, arial;
}
@@ -0,0 +1,4 @@
j = 0
j += i for i in [0..5]
exports.name = "mock_coffee_#{j}"
@@ -0,0 +1 @@
exports.name = 'mock_module3';
@@ -0,0 +1 @@
exports.name = 'mock_module4';
@@ -0,0 +1 @@
exports.name = 'mock_module1';
@@ -0,0 +1 @@
exports.name = 'mock_module2';
@@ -0,0 +1,3 @@
function hello_world(arg) {
return "_" + arg + "_";
}
@@ -0,0 +1,3 @@
function get_a_variable() {
return typeof a_variable;
}
@@ -0,0 +1 @@
var t=t?t+1:1;
@@ -0,0 +1,219 @@
/*
* This module is not a plain nodeunit test suite, but instead uses the
* assert module to ensure a basic level of functionality is present,
* allowing the rest of the tests to be written using nodeunit itself.
*
* THIS FILE SHOULD BE BROWSER-COMPATIBLE JS!
* You can use @REMOVE_LINE_FOR_BROWSER to remove code from the browser build.
* Only code on that line will be removed, its mostly to avoid requiring code
* that is node specific
*/
var assert = require('assert'), // @REMOVE_LINE_FOR_BROWSER
async = require('../deps/async'), // @REMOVE_LINE_FOR_BROWSER
nodeunit = require('../lib/nodeunit'); // @REMOVE_LINE_FOR_BROWSER
// NOT A TEST - util function to make testing faster.
// retries the assertion until it passes or the timeout is reached,
// at which point it throws the assertion error
var waitFor = function (fn, timeout, callback, start) {
start = start || new Date().getTime();
callback = callback || function () {};
try {
fn();
callback();
}
catch (e) {
if (e instanceof assert.AssertionError) {
var now = new Date().getTime();
if (now - start >= timeout) {
throw e;
}
else {
async.nextTick(function () {
waitFor(fn, timeout, callback, start);
});
}
}
else {
throw e;
}
}
};
// TESTS:
// Are exported tests actually run? - store completed tests in this variable
// for checking later
var tests_called = {};
// most basic test that should run, the tests_called object is tested
// at the end of this module to ensure the tests were actually run by nodeunit
exports.testCalled = function (test) {
tests_called.testCalled = true;
test.done();
};
// generates test functions for nodeunit assertions
var makeTest = function (method, args_pass, args_fail) {
return function (test) {
var test1_called = false;
var test2_called = false;
// test pass
nodeunit.runTest(
'testname',
function (test) {
test[method].apply(test, args_pass);
test.done();
},
{testDone: function (name, assertions) {
assert.equal(assertions.length, 1);
assert.equal(assertions.failures(), 0);
}},
function () {
test1_called = true;
}
);
// test failure
nodeunit.runTest(
'testname',
function (test) {
test[method].apply(test, args_fail);
test.done();
},
{testDone: function (name, assertions) {
assert.equal(assertions.length, 1);
assert.equal(assertions.failures(), 1);
}},
function () {
test2_called = true;
}
);
// ensure tests were run
waitFor(function () {
assert.ok(test1_called);
assert.ok(test2_called);
tests_called[method] = true;
}, 500, test.done);
};
};
// ensure basic assertions are working:
exports.testOk = makeTest('ok', [true], [false]);
exports.testEquals = makeTest('equals', [1, 1], [1, 2]);
exports.testSame = makeTest('same',
[{test: 'test'}, {test: 'test'}],
[{test: 'test'}, {monkey: 'penguin'}]
);
// from the assert module:
exports.testEqual = makeTest('equal', [1, 1], [1, 2]);
exports.testNotEqual = makeTest('notEqual', [1, 2], [1, 1]);
exports.testDeepEqual = makeTest('deepEqual',
[{one: 1}, {one: 1}], [{one: 1}, {two: 2}]
);
exports.testNotDeepEqual = makeTest('notDeepEqual',
[{one: 1}, {two: 2}], [{one: 1}, {one: 1}]
);
exports.testStrictEqual = makeTest('strictEqual', [1, 1], [1, true]);
exports.testNotStrictEqual = makeTest('notStrictEqual', [true, 1], [1, 1]);
exports.testThrows = makeTest('throws',
[function () {
throw new Error('test');
}],
[function () {
return;
}]
);
exports.testDoesNotThrows = makeTest('doesNotThrow',
[function () {
return;
}],
[function () {
throw new Error('test');
}]
);
exports.testIfError = makeTest('ifError', [false], [new Error('test')]);
exports.testExpect = function (test) {
var test1_called = false,
test2_called = false,
test3_called = false;
// correct number of tests run
nodeunit.runTest(
'testname',
function (test) {
test.expect(2);
test.ok(true);
test.ok(true);
test.done();
},
{testDone: function (name, assertions) {
test.equals(assertions.length, 2);
test.equals(assertions.failures(), 0);
}},
function () {
test1_called = true;
}
);
// no tests run
nodeunit.runTest(
'testname',
function (test) {
test.expect(2);
test.done();
},
{testDone: function (name, assertions) {
test.equals(assertions.length, 1);
test.equals(assertions.failures(), 1);
}},
function () {
test2_called = true;
}
);
// incorrect number of tests run
nodeunit.runTest(
'testname',
function (test) {
test.expect(2);
test.ok(true);
test.ok(true);
test.ok(true);
test.done();
},
{testDone: function (name, assertions) {
test.equals(assertions.length, 4);
test.equals(assertions.failures(), 1);
}},
function () {
test3_called = true;
}
);
// ensure callbacks fired
waitFor(function () {
assert.ok(test1_called);
assert.ok(test2_called);
assert.ok(test3_called);
tests_called.expect = true;
}, 500, test.done);
};
// tests are async, so wait for them to be called
waitFor(function () {
assert.ok(tests_called.testCalled);
assert.ok(tests_called.ok);
assert.ok(tests_called.equals);
assert.ok(tests_called.same);
assert.ok(tests_called.expect);
}, 10000);
@@ -0,0 +1,114 @@
var nodeunit = require('../lib/nodeunit');
exports.testFailingLog = function (test) {
test.expect(3);
// this is meant to bubble to the top, and will be ignored for the purposes
// of testing:
var ignored_error = new Error('ignore this callback error');
var err_handler = function (err) {
if (err && err.message !== ignored_error.message) {
throw err;
}
};
process.addListener('uncaughtException', err_handler);
// A failing callback should not affect the test outcome
var testfn = function (test) {
test.ok(true, 'test.ok');
test.done();
};
nodeunit.runTest('testname', testfn, {
log: function (assertion) {
test.ok(true, 'log called');
throw ignored_error;
},
testDone: function (name, assertions) {
test.equals(assertions.failures(), 0, 'failures');
test.equals(assertions.length, 1, 'total');
process.removeListener('uncaughtException', err_handler);
}
}, test.done);
};
exports.testFailingTestDone = function (test) {
test.expect(2);
var ignored_error = new Error('ignore this callback error');
var err_handler = function (err) {
if (err && err.message !== ignored_error.message) {
throw err;
}
};
process.addListener('uncaughtException', err_handler);
// A failing callback should not affect the test outcome
var testfn = function (test) {
test.done();
};
nodeunit.runTest('testname', testfn, {
log: function (assertion) {
test.ok(false, 'log should not be called');
},
testDone: function (name, assertions) {
test.equals(assertions.failures(), 0, 'failures');
test.equals(assertions.length, 0, 'total');
process.nextTick(function () {
process.removeListener('uncaughtException', err_handler);
test.done();
});
throw ignored_error;
}
}, function () {});
};
exports.testAssertionObj = function (test) {
test.expect(4);
var testfn = function (test) {
test.ok(true, 'ok true');
test.done();
};
nodeunit.runTest('testname', testfn, {
log: function (assertion) {
test.ok(assertion.passed() === true, 'assertion.passed');
test.ok(assertion.failed() === false, 'assertion.failed');
},
testDone: function (name, assertions) {
test.equals(assertions.failures(), 0, 'failures');
test.equals(assertions.length, 1, 'total');
}
}, test.done);
};
exports.testLogOptional = function (test) {
test.expect(2);
var testfn = function (test) {
test.ok(true, 'ok true');
test.done();
};
nodeunit.runTest('testname', testfn, {
testDone: function (name, assertions) {
test.equals(assertions.failures(), 0, 'failures');
test.equals(assertions.length, 1, 'total');
}
}, test.done);
};
exports.testExpectWithFailure = function (test) {
test.expect(3);
var testfn = function (test) {
test.expect(1);
test.ok(false, 'test.ok');
test.done();
};
nodeunit.runTest('testname', testfn, {
log: function (assertion) {
test.equals(assertion.method, 'ok', 'assertion.method');
},
testDone: function (name, assertions) {
test.equals(assertions.failures(), 1, 'failures');
test.equals(assertions.length, 1, 'total');
}
}, test.done);
};
@@ -0,0 +1,55 @@
var nodeunit = require('../lib/nodeunit');
var httputil = require('../lib/utils').httputil;
exports.testHttpUtilBasics = function (test) {
test.expect(6);
httputil(function (req, resp) {
test.equal(req.method, 'PUT');
test.equal(req.url, '/newpair');
test.equal(req.headers.foo, 'bar');
resp.writeHead(500, {'content-type': 'text/plain'});
resp.end('failed');
}, function (server, client) {
client.fetch('PUT', '/newpair', {'foo': 'bar'}, function (resp) {
test.equal(resp.statusCode, 500);
test.equal(resp.headers['content-type'], 'text/plain');
test.equal(resp.body, 'failed');
server.close();
test.done();
});
});
};
exports.testHttpUtilJsonHandling = function (test) {
test.expect(9);
httputil(function (req, resp) {
test.equal(req.method, 'GET');
test.equal(req.url, '/');
test.equal(req.headers.foo, 'bar');
var testdata = {foo1: 'bar', foo2: 'baz'};
resp.writeHead(200, {'content-type': 'application/json'});
resp.end(JSON.stringify(testdata));
}, function (server, client) {
client.fetch('GET', '/', {'foo': 'bar'}, function (resp) {
test.equal(resp.statusCode, 200);
test.equal(resp.headers['content-type'], 'application/json');
test.ok(resp.bodyAsObject);
test.equal(typeof resp.bodyAsObject, 'object');
test.equal(resp.bodyAsObject.foo1, 'bar');
test.equal(resp.bodyAsObject.foo2, 'baz');
server.close();
test.done();
});
});
};
@@ -0,0 +1,214 @@
var assert = require('assert'),
fs = require('fs'),
path = require('path'),
nodeunit = require('../lib/nodeunit');
var setup = function (fn) {
return function (test) {
process.chdir(__dirname);
var env = {
mock_module1: require(__dirname + '/fixtures/mock_module1'),
mock_module2: require(__dirname + '/fixtures/mock_module2'),
mock_module3: require(__dirname + '/fixtures/dir/mock_module3'),
mock_module4: require(__dirname + '/fixtures/dir/mock_module4')
};
fn.call(env, test);
};
};
exports.testRunFiles = setup(function (test) {
test.expect(24);
var runModule_copy = nodeunit.runModule;
var runModule_calls = [];
var modules = [];
var opts = {
moduleStart: function () {
return 'moduleStart';
},
testDone: function () {
return 'testDone';
},
testStart: function () {
return 'testStart';
},
log: function () {
return 'log';
},
done: function (assertions) {
test.equals(assertions.failures(), 0, 'failures');
test.equals(assertions.length, 4, 'length');
test.ok(typeof assertions.duration === "number");
var called_with = function (name) {
return runModule_calls.some(function (m) {
return m.name === name;
});
};
test.ok(called_with('mock_module1'), 'mock_module1 ran');
test.ok(called_with('mock_module2'), 'mock_module2 ran');
test.ok(called_with('mock_module3'), 'mock_module3 ran');
test.ok(called_with('mock_module4'), 'mock_module4 ran');
test.equals(runModule_calls.length, 4);
nodeunit.runModule = runModule_copy;
test.done();
}
};
nodeunit.runModule = function (name, mod, options, callback) {
test.equals(options.testDone, opts.testDone);
test.equals(options.testStart, opts.testStart);
test.equals(options.log, opts.log);
test.ok(typeof name === "string");
runModule_calls.push(mod);
var m = [{failed: function () {
return false;
}}];
modules.push(m);
callback(null, m);
};
nodeunit.runFiles(
[__dirname + '/fixtures/mock_module1.js',
__dirname + '/fixtures/mock_module2.js',
__dirname + '/fixtures/dir'],
opts
);
});
exports.testRunFilesEmpty = function (test) {
test.expect(3);
nodeunit.runFiles([], {
moduleStart: function () {
test.ok(false, 'should not be called');
},
testDone: function () {
test.ok(false, 'should not be called');
},
testStart: function () {
test.ok(false, 'should not be called');
},
log: function () {
test.ok(false, 'should not be called');
},
done: function (assertions) {
test.equals(assertions.failures(), 0, 'failures');
test.equals(assertions.length, 0, 'length');
test.ok(typeof assertions.duration === "number");
test.done();
}
});
};
exports.testEmptyDir = function (test) {
var dir2 = __dirname + '/fixtures/dir2';
// git doesn't like empty directories, so we have to create one
path.exists(dir2, function (exists) {
if (!exists) {
fs.mkdirSync(dir2, 0777);
}
// runFiles on empty directory:
nodeunit.runFiles([dir2], {
moduleStart: function () {
test.ok(false, 'should not be called');
},
testDone: function () {
test.ok(false, 'should not be called');
},
testStart: function () {
test.ok(false, 'should not be called');
},
log: function () {
test.ok(false, 'should not be called');
},
done: function (assertions) {
test.equals(assertions.failures(), 0, 'failures');
test.equals(assertions.length, 0, 'length');
test.ok(typeof assertions.duration === "number");
test.done();
}
});
});
};
var CoffeeScript;
try {
CoffeeScript = require('coffee-script');
} catch (e) {
}
if (CoffeeScript) {
exports.testCoffeeScript = function (test) {
process.chdir(__dirname);
var env = {
mock_coffee_module: require(__dirname +
'/fixtures/coffee/mock_coffee_module')
};
test.expect(9);
var runModule_copy = nodeunit.runModule;
var runModule_calls = [];
var modules = [];
var opts = {
moduleStart: function () {
return 'moduleStart';
},
testDone: function () {
return 'testDone';
},
testStart: function () {
return 'testStart';
},
log: function () {
return 'log';
},
done: function (assertions) {
test.equals(assertions.failures(), 0, 'failures');
test.equals(assertions.length, 1, 'length');
test.ok(typeof assertions.duration === "number");
var called_with = function (name) {
return runModule_calls.some(function (m) {
return m.name === name;
});
};
test.ok(
called_with('mock_coffee_15'),
'mock_coffee_module ran'
);
test.equals(runModule_calls.length, 1);
nodeunit.runModule = runModule_copy;
test.done();
}
};
nodeunit.runModule = function (name, mod, options, callback) {
test.equals(options.testDone, opts.testDone);
test.equals(options.testStart, opts.testStart);
test.equals(options.log, opts.log);
test.ok(typeof name === "string");
runModule_calls.push(mod);
var m = [{failed: function () {
return false;
}}];
modules.push(m);
callback(null, m);
};
nodeunit.runFiles(
[__dirname + 'fixtures/coffee/mock_coffee_module.coffee'],
opts
);
};
}
@@ -0,0 +1,177 @@
/* THIS FILE SHOULD BE BROWSER-COMPATIBLE JS!
* You can use @REMOVE_LINE_FOR_BROWSER to remove code from the browser build.
* Only code on that line will be removed, its mostly to avoid requiring code
* that is node specific
*/
var nodeunit = require('../lib/nodeunit'); // @REMOVE_LINE_FOR_BROWSER
exports.testRunModule = function (test) {
test.expect(11);
var call_order = [];
var testmodule = {
test1: function (test) {
call_order.push('test1');
test.ok(true, 'ok true');
test.done();
},
test2: function (test) {
call_order.push('test2');
test.ok(false, 'ok false');
test.ok(false, 'ok false');
test.done();
},
test3: function (test) {
call_order.push('test3');
test.done();
}
};
nodeunit.runModule('testmodule', testmodule, {
log: function (assertion) {
call_order.push('log');
},
testStart: function (name) {
call_order.push('testStart');
test.ok(
name.toString() === 'test1' ||
name.toString() === 'test2' ||
name.toString() === 'test3',
'testStart called with test name '
);
},
testDone: function (name, assertions) {
call_order.push('testDone');
test.ok(
name.toString() === 'test1' ||
name.toString() === 'test2' ||
name.toString() === 'test3',
'testDone called with test name'
);
},
moduleDone: function (name, assertions) {
call_order.push('moduleDone');
test.equals(assertions.length, 3);
test.equals(assertions.failures(), 2);
test.equals(name, 'testmodule');
test.ok(typeof assertions.duration === "number");
test.same(call_order, [
'testStart', 'test1', 'log', 'testDone',
'testStart', 'test2', 'log', 'log', 'testDone',
'testStart', 'test3', 'testDone',
'moduleDone'
]);
}
}, test.done);
};
exports.testRunModuleTestSpec = function (test) {
test.expect(6);
var call_order = [];
var testmodule = {
test1: function (test) {
test.ok(true, 'ok true');
test.done();
},
test2: function (test) {
call_order.push('test2');
test.ok(false, 'ok false');
test.ok(false, 'ok false');
test.done();
},
test3: function (test) {
test.done();
}
};
nodeunit.runModule('testmodule', testmodule, {
testspec: "test2",
log: function (assertion) {
call_order.push('log');
},
testStart: function (name) {
call_order.push('testStart');
test.ok(
name.toString() === 'test2',
'testStart called with test name '
);
},
testDone: function (name, assertions) {
call_order.push('testDone');
test.ok(
name.toString() === 'test2',
'testDone called with test name'
);
},
moduleDone: function (name, assertions) {
call_order.push('moduleDone');
test.equals(assertions.length, 2);
test.equals(name, 'testmodule');
test.ok(typeof assertions.duration === "number");
test.same(call_order, [
'testStart', 'test2', 'log', 'log', 'testDone',
'moduleDone'
]);
}
}, test.done);
};
exports.testRunModuleEmpty = function (test) {
nodeunit.runModule('module with no exports', {}, {
log: function (assertion) {
test.ok(false, 'log should not be called');
},
testStart: function (name) {
test.ok(false, 'testStart should not be called');
},
testDone: function (name, assertions) {
test.ok(false, 'testDone should not be called');
},
moduleDone: function (name, assertions) {
test.equals(assertions.length, 0);
test.equals(assertions.failures(), 0);
test.equals(name, 'module with no exports');
test.ok(typeof assertions.duration === "number");
}
}, test.done);
};
exports.testNestedTests = function (test) {
var call_order = [];
var m = {
test1: function (test) {
test.done();
},
suite: {
t1: function (test) {
test.done();
},
t2: function (test) {
test.done();
},
another_suite: {
t3: function (test) {
test.done();
}
}
}
};
nodeunit.runModule('modulename', m, {
testStart: function (name) {
call_order.push(['testStart'].concat(name));
},
testDone: function (name, assertions) {
call_order.push(['testDone'].concat(name));
}
}, function () {
test.same(call_order, [
['testStart', 'test1'], ['testDone', 'test1'],
['testStart', 'suite', 't1'], ['testDone', 'suite', 't1'],
['testStart', 'suite', 't2'], ['testDone', 'suite', 't2'],
['testStart', 'suite', 'another_suite', 't3'],
['testDone', 'suite', 'another_suite', 't3']
]);
test.done();
});
};
@@ -0,0 +1,46 @@
/* THIS FILE SHOULD BE BROWSER-COMPATIBLE JS!
* You can use @REMOVE_LINE_FOR_BROWSER to remove code from the browser build.
* Only code on that line will be removed, its mostly to avoid requiring code
* that is node specific
*/
var nodeunit = require('../lib/nodeunit'); // @REMOVE_LINE_FOR_BROWSER
exports.testArgs = function (test) {
test.ok(test.expect instanceof Function, 'test.expect');
test.ok(test.done instanceof Function, 'test.done');
test.ok(test.ok instanceof Function, 'test.ok');
test.ok(test.same instanceof Function, 'test.same');
test.ok(test.equals instanceof Function, 'test.equals');
test.done();
};
exports.testDoneCallback = function (test) {
test.expect(4);
nodeunit.runTest('testname', exports.testArgs, {
testDone: function (name, assertions) {
test.equals(assertions.failures(), 0, 'failures');
test.equals(assertions.length, 5, 'length');
test.ok(typeof assertions.duration === "number");
test.equals(name, 'testname');
}
}, test.done);
};
exports.testThrowError = function (test) {
test.expect(3);
var err = new Error('test');
var testfn = function (test) {
throw err;
};
nodeunit.runTest('testname', testfn, {
log: function (assertion) {
test.same(assertion.error, err, 'assertion.error');
},
testDone: function (name, assertions) {
test.equals(assertions.failures(), 1);
test.equals(assertions.length, 1);
}
}, test.done);
};
@@ -0,0 +1,31 @@
var nodeunit = require('../lib/nodeunit');
var sandbox = require('../lib/utils').sandbox;
var testCase = nodeunit.testCase;
exports.testSimpleSandbox = function (test) {
var raw_jscode1 = sandbox(__dirname + '/fixtures/raw_jscode1.js');
test.equal(raw_jscode1.hello_world('foo'), '_foo_', 'evaluation ok');
test.done();
};
exports.testSandboxContext = function (test) {
var a_variable = 42; // should not be visible in the sandbox
var raw_jscode2 = sandbox(__dirname + '/fixtures/raw_jscode2.js');
a_variable = 42; // again for the win
test.equal(
raw_jscode2.get_a_variable(),
'undefined',
'the variable should not be defined'
);
test.done();
};
exports.testSandboxMultiple = function (test) {
var raw_jscode3 = sandbox([
__dirname + '/fixtures/raw_jscode3.js',
__dirname + '/fixtures/raw_jscode3.js',
__dirname + '/fixtures/raw_jscode3.js'
]);
test.equal(raw_jscode3.t, 3, 'two files loaded');
test.done();
};
@@ -0,0 +1,257 @@
/* THIS FILE SHOULD BE BROWSER-COMPATIBLE JS!
* You can use @REMOVE_LINE_FOR_BROWSER to remove code from the browser build.
* Only code on that line will be removed, its mostly to avoid requiring code
* that is node specific
*/
var nodeunit = require('../lib/nodeunit'); // @REMOVE_LINE_FOR_BROWSER
var testCase = nodeunit.testCase;
exports.testTestCase = function (test) {
test.expect(7);
var call_order = [];
var s = testCase({
setUp: function (callback) {
call_order.push('setUp');
test.equals(this.one, undefined);
this.one = 1;
callback();
},
tearDown: function (callback) {
call_order.push('tearDown');
test.ok(true, 'tearDown called');
callback();
},
test1: function (t) {
call_order.push('test1');
test.equals(this.one, 1);
this.one = 2;
t.done();
},
test2: function (t) {
call_order.push('test2');
test.equals(this.one, 1);
t.done();
}
});
nodeunit.runSuite(null, s, {}, function () {
test.same(call_order, [
'setUp', 'test1', 'tearDown',
'setUp', 'test2', 'tearDown'
]);
test.done();
});
};
exports.tearDownAfterError = function (test) {
test.expect(1);
var s = testCase({
tearDown: function (callback) {
test.ok(true, 'tearDown called');
callback();
},
test: function (t) {
throw new Error('some error');
}
});
nodeunit.runSuite(null, s, {}, function () {
test.done();
});
};
exports.catchSetUpError = function (test) {
test.expect(2);
var test_error = new Error('test error');
var s = testCase({
setUp: function (callback) {
throw test_error;
},
test: function (t) {
test.ok(false, 'test function should not be called');
t.done();
}
});
nodeunit.runSuite(null, s, {}, function (err, assertions) {
test.equal(assertions.length, 1);
test.equal(assertions[0].error, test_error);
test.done();
});
};
exports.setUpErrorCallback = function (test) {
test.expect(2);
var test_error = new Error('test error');
var s = testCase({
setUp: function (callback) {
callback(test_error);
},
test: function (t) {
test.ok(false, 'test function should not be called');
t.done();
}
});
nodeunit.runSuite(null, s, {}, function (err, assertions) {
test.equal(assertions.length, 1);
test.equal(assertions[0].error, test_error);
test.done();
});
};
exports.catchTearDownError = function (test) {
test.expect(2);
var test_error = new Error('test error');
var s = testCase({
tearDown: function (callback) {
throw test_error;
},
test: function (t) {
t.done();
}
});
nodeunit.runSuite(null, s, {}, function (err, assertions) {
test.equal(assertions.length, 1);
test.equal(assertions[0].error, test_error);
test.done();
});
};
exports.tearDownErrorCallback = function (test) {
test.expect(2);
var test_error = new Error('test error');
var s = testCase({
tearDown: function (callback) {
callback(test_error);
},
test: function (t) {
t.done();
}
});
nodeunit.runSuite(null, s, {}, function (err, assertions) {
test.equal(assertions.length, 1);
test.equal(assertions[0].error, test_error);
test.done();
});
};
exports.testErrorAndtearDownError = function (test) {
test.expect(3);
var error1 = new Error('test error one');
var error2 = new Error('test error two');
var s = testCase({
tearDown: function (callback) {
callback(error2);
},
test: function (t) {
t.done(error1);
}
});
nodeunit.runSuite(null, s, {}, function (err, assertions) {
test.equal(assertions.length, 2);
test.equal(assertions[0].error, error1);
test.equal(assertions[1].error, error2);
test.done();
});
};
exports.testCaseGroups = function (test) {
var call_order = [];
var s = testCase({
setUp: function (callback) {
call_order.push('setUp');
callback();
},
tearDown: function (callback) {
call_order.push('tearDown');
callback();
},
test1: function (test) {
call_order.push('test1');
test.done();
},
group1: {
test2: function (test) {
call_order.push('group1.test2');
test.done();
}
}
});
nodeunit.runSuite(null, s, {}, function (err, assertions) {
test.same(call_order, [
'setUp',
'test1',
'tearDown',
'setUp',
'group1.test2',
'tearDown'
]);
test.done();
});
};
exports.nestedTestCases = function (test) {
var call_order = [];
var s = testCase({
setUp: function (callback) {
call_order.push('setUp');
callback();
},
tearDown: function (callback) {
call_order.push('tearDown');
callback();
},
test1: function (test) {
call_order.push('test1');
test.done();
},
group1: testCase({
setUp: function (callback) {
call_order.push('group1.setUp');
callback();
},
tearDown: function (callback) {
call_order.push('group1.tearDown');
callback();
},
test2: function (test) {
call_order.push('group1.test2');
test.done();
}
})
});
nodeunit.runSuite(null, s, {}, function (err, assertions) {
test.same(call_order, [
'setUp',
'test1',
'tearDown',
'setUp',
'group1.setUp',
'group1.test2',
'group1.tearDown',
'tearDown'
]);
test.done();
});
};
exports.deepNestedTestCases = function (test) {
var val = 'foo';
var s = testCase({
setUp: function (callback) {
val = 'bar';
callback();
},
group1: testCase({
test: testCase({
test2: function (test) {
test.equal(val, 'bar');
test.done();
}
})
})
});
nodeunit.runSuite(null, s, {}, function (err, assertions) {
test.ok(!assertions[0].failed());
test.equal(assertions.length, 1);
test.done();
});
};
@@ -0,0 +1,256 @@
/* THIS FILE SHOULD BE BROWSER-COMPATIBLE JS!
* You can use @REMOVE_LINE_FOR_BROWSER to remove code from the browser build.
* Only code on that line will be removed, its mostly to avoid requiring code
* that is node specific
*/
var nodeunit = require('../lib/nodeunit'); // @REMOVE_LINE_FOR_BROWSER
exports.testTestCase = function (test) {
test.expect(7);
var call_order = [];
var s = {
setUp: function (callback) {
call_order.push('setUp');
test.equals(this.one, undefined, 'in setUp, this.one not set');
this.one = 1;
callback();
},
tearDown: function (callback) {
call_order.push('tearDown');
test.ok(true, 'tearDown called');
callback();
},
test1: function (t) {
call_order.push('test1');
test.equals(this.one, 1, 'in test1, this.one is 1');
this.one = 2;
t.done();
},
test2: function (t) {
call_order.push('test2');
test.equals(this.one, 1, 'in test2, this.one is still 1');
t.done();
}
};
nodeunit.runSuite(null, s, {}, function () {
test.same(call_order, [
'setUp', 'test1', 'tearDown',
'setUp', 'test2', 'tearDown'
]);
test.done();
});
};
exports.tearDownAfterError = function (test) {
test.expect(1);
var s = {
tearDown: function (callback) {
test.ok(true, 'tearDown called');
callback();
},
test: function (t) {
throw new Error('some error');
}
};
nodeunit.runSuite(null, s, {}, function () {
test.done();
});
};
exports.catchSetUpError = function (test) {
test.expect(2);
var test_error = new Error('test error');
var s = {
setUp: function (callback) {
throw test_error;
},
test: function (t) {
test.ok(false, 'test function should not be called');
t.done();
}
};
nodeunit.runSuite(null, s, {}, function (err, assertions) {
test.equal(assertions.length, 1);
test.equal(assertions[0].error, test_error);
test.done();
});
};
exports.setUpErrorCallback = function (test) {
test.expect(2);
var test_error = new Error('test error');
var s = {
setUp: function (callback) {
callback(test_error);
},
test: function (t) {
test.ok(false, 'test function should not be called');
t.done();
}
};
nodeunit.runSuite(null, s, {}, function (err, assertions) {
test.equal(assertions.length, 1);
test.equal(assertions[0].error, test_error);
test.done();
});
};
exports.catchTearDownError = function (test) {
test.expect(2);
var test_error = new Error('test error');
var s = {
tearDown: function (callback) {
throw test_error;
},
test: function (t) {
t.done();
}
};
nodeunit.runSuite(null, s, {}, function (err, assertions) {
test.equal(assertions.length, 1);
test.equal(assertions[0].error, test_error);
test.done();
});
};
exports.tearDownErrorCallback = function (test) {
test.expect(2);
var test_error = new Error('test error');
var s = {
tearDown: function (callback) {
callback(test_error);
},
test: function (t) {
t.done();
}
};
nodeunit.runSuite(null, s, {}, function (err, assertions) {
test.equal(assertions.length, 1);
test.equal(assertions[0].error, test_error);
test.done();
});
};
exports.testErrorAndtearDownError = function (test) {
test.expect(3);
var error1 = new Error('test error one');
var error2 = new Error('test error two');
var s = {
tearDown: function (callback) {
callback(error2);
},
test: function (t) {
t.done(error1);
}
};
nodeunit.runSuite(null, s, {}, function (err, assertions) {
test.equal(assertions.length, 2);
test.equal(assertions[0].error, error1);
test.equal(assertions[1].error, error2);
test.done();
});
};
exports.testCaseGroups = function (test) {
var call_order = [];
var s = {
setUp: function (callback) {
call_order.push('setUp');
callback();
},
tearDown: function (callback) {
call_order.push('tearDown');
callback();
},
test1: function (test) {
call_order.push('test1');
test.done();
},
group1: {
test2: function (test) {
call_order.push('group1.test2');
test.done();
}
}
};
nodeunit.runSuite(null, s, {}, function (err, assertions) {
test.same(call_order, [
'setUp',
'test1',
'tearDown',
'setUp',
'group1.test2',
'tearDown'
]);
test.done();
});
};
exports.nestedTestCases = function (test) {
var call_order = [];
var s = {
setUp: function (callback) {
call_order.push('setUp');
callback();
},
tearDown: function (callback) {
call_order.push('tearDown');
callback();
},
test1: function (test) {
call_order.push('test1');
test.done();
},
group1: {
setUp: function (callback) {
call_order.push('group1.setUp');
callback();
},
tearDown: function (callback) {
call_order.push('group1.tearDown');
callback();
},
test2: function (test) {
call_order.push('group1.test2');
test.done();
}
}
};
nodeunit.runSuite(null, s, {}, function (err, assertions) {
test.same(call_order, [
'setUp',
'test1',
'tearDown',
'setUp',
'group1.setUp',
'group1.test2',
'group1.tearDown',
'tearDown'
]);
test.done();
});
};
exports.deepNestedTestCases = function (test) {
var val = 'foo';
var s = {
setUp: function (callback) {
val = 'bar';
callback();
},
group1: {
test: {
test2: function (test) {
test.equal(val, 'bar');
test.done();
}
}
}
};
nodeunit.runSuite(null, s, {}, function (err, assertions) {
test.ok(!assertions[0].failed());
test.equal(assertions.length, 1);
test.done();
});
};
@@ -0,0 +1,28 @@
<html>
<head>
<title>Nodeunit Test Suite</title>
<!--
Note this file is only used for 'make browser', when it is copied to
dist/browser/test.html for running the browser test suite
-->
<link rel="stylesheet" href="nodeunit.css" type="text/css" media="screen" />
<script src="nodeunit.js"></script>
<script src="test-base.js"></script>
<script src="test-runmodule.js"></script>
<script src="test-runtest.js"></script>
<script src="test-testcase.js"></script>
<script src="test-testcase-legacy.js"></script>
</head>
<body>
<h1 id="nodeunit-header">Nodeunit Test Suite</h1>
<script>
nodeunit.run({
'test-base': test_base,
'test-runmodule': test_runmodule,
'test-runtest': test_runtest,
'test-testcase': test_testcase,
'test-testcase-legacy': test_testcase_legacy
});
</script>
</body>
</html>
@@ -0,0 +1,71 @@
# Step
A simple control-flow library for node.JS that makes parallel execution, serial execution, and error handling painless.
## How to install
Simply copy or link the lib/step.js file into your `$HOME/.node_libraries` folder.
## How to use
The step library exports a single function I call `Step`. It accepts any number of functions as arguments and runs them in serial order using the passed in `this` context as the callback to the next step.
Step(
function readSelf() {
fs.readFile(__filename, this);
},
function capitalize(err, text) {
if (err) throw err;
return text.toUpperCase();
},
function showIt(err, newText) {
if (err) throw err;
console.log(newText);
}
);
Notice that we pass in `this` as the callback to `fs.readFile`. When the file read completes, step will send the result as the arguments to the next function in the chain. Then in the `capitalize` function we're doing synchronous work so we can simple return the new value and Step will route it as if we called the callback.
The first parameter is reserved for errors since this is the node standard. Also any exceptions thrown are caught and passed as the first argument to the next function. As long as you don't nest callback functions inline your main functions this prevents there from ever being any uncaught exceptions. This is very important for long running node.JS servers since a single uncaught exception can bring the whole server down.
Also there is support for parallel actions:
Step(
// Loads two files in parallel
function loadStuff() {
fs.readFile(__filename, this.parallel());
fs.readFile("/etc/passwd", this.parallel());
},
// Show the result when done
function showStuff(err, code, users) {
if (err) throw err;
sys.puts(code);
sys.puts(users);
}
)
Here we pass `this.parallel()` instead of `this` as the callback. It internally keeps track of the number of callbacks issued and preserves their order then giving the result to the next step after all have finished. If there is an error in any of the parallel actions, it will be passed as the first argument to the next step.
Also you can use group with a dynamic number of common tasks.
Step(
function readDir() {
fs.readdir(__dirname, this);
},
function readFiles(err, results) {
if (err) throw err;
// Create a new group
var group = this.group();
results.forEach(function (filename) {
if (/\.js$/.test(filename)) {
fs.readFile(__dirname + "/" + filename, 'utf8', group());
}
});
},
function showAll(err , files) {
if (err) throw err;
sys.p(files);
}
);
*Note* that we both call `this.group()` and `group()`. The first reserves a slot in the parameters of the next step, then calling `group()` generates the individual callbacks and increments the internal counter.
+154
Ver Arquivo
@@ -0,0 +1,154 @@
/*
Copyright (c) 2011 Tim Caswell <tim@creationix.com>
MIT License
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
// Inspired by http://github.com/willconant/flow-js, but reimplemented and
// modified to fit my taste and the node.JS error handling system.
function Step() {
var steps = Array.prototype.slice.call(arguments),
pending, counter, results, lock;
// Define the main callback that's given as `this` to the steps.
function next() {
// Check if there are no steps left
if (steps.length === 0) {
// Throw uncaught errors
if (arguments[0]) {
throw arguments[0];
}
return;
}
// Get the next step to execute
var fn = steps.shift();
counter = pending = 0;
results = [];
// Run the step in a try..catch block so exceptions don't get out of hand.
try {
lock = true;
var result = fn.apply(next, arguments);
} catch (e) {
// Pass any exceptions on through the next callback
next(e);
}
// If a syncronous return is used, pass it to the callback
if (result !== undefined) {
next(undefined, result);
}
lock = false;
}
// Add a special callback generator `this.parallel()` that groups stuff.
next.parallel = function () {
var index = 1 + counter++;
pending++;
function check() {
if (pending === 0) {
// When they're all done, call the callback
next.apply(null, results);
}
}
process.nextTick(check); // Ensures that check is called at least once
return function () {
pending--;
// Compress the error from any result to the first argument
if (arguments[0]) {
results[0] = arguments[0];
}
// Send the other results as arguments
results[index] = arguments[1];
if (!lock) { check(); }
};
};
// Generates a callback generator for grouped results
next.group = function () {
var localCallback = next.parallel();
var counter = 0;
var pending = 0;
var result = [];
var error = undefined;
function check() {
if (pending === 0) {
// When group is done, call the callback
localCallback(error, result);
}
}
process.nextTick(check); // Ensures that check is called at least once
// Generates a callback for the group
return function () {
var index = counter++;
pending++;
return function () {
pending--;
// Compress the error from any result to the first argument
if (arguments[0]) {
error = arguments[0];
}
// Send the other results as arguments
result[index] = arguments[1];
if (!lock) { check(); }
};
};
};
// Start the engine an pass nothing to the first step.
next();
}
// Tack on leading and tailing steps for input and output and return
// the whole thing as a function. Basically turns step calls into function
// factories.
Step.fn = function StepFn() {
var steps = Array.prototype.slice.call(arguments);
return function () {
var args = Array.prototype.slice.call(arguments);
// Insert a first step that primes the data stream
var toRun = [function () {
this.apply(null, args);
}].concat(steps);
// If the last arg is a function add it as a last step
if (typeof args[args.length-1] === 'function') {
toRun.push(args.pop());
}
Step.apply(null, toRun);
}
}
// Hook into commonJS module systems
if (typeof module !== 'undefined' && "exports" in module) {
module.exports = Step;
}
@@ -0,0 +1,11 @@
{ "name": "step",
"version": "0.0.4",
"description": "A simple control-flow library for node.JS that makes parallel execution, serial execution, and error handling painless.",
"engine": [ "node >=0.2.0" ],
"author": "Tim Caswell <tim@creationix.com>",
"repository":
{ "type" : "git",
"url" : "http://github.com/creationix/step.git"
},
"main": "lib/step"
}

Alguns arquivos não foram exibidos porque demasiados arquivos foram alterados neste diff Mostrar Mais