Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

fixes #11

  • Loading branch information...
commit dcc22ab14e44aca63ed983d655007affc6af10d9 1 parent 8aee1ca
Oleg Podsechin authored
Showing with 138 additions and 509 deletions.
  1. +1 −1  README.md
  2. +137 −508 lib/assert.js
2  README.md
View
@@ -11,7 +11,7 @@ proposals on top of [Node.js](http://nodejs.org) using
The following modules are included:
-* assert - [Unit Testing/1.0](httprin://wiki.commonjs.org/wiki/Unit_Testing/1.0) & a suite for running tests
+* assert - [Unit Testing/1.0](http://wiki.commonjs.org/wiki/Unit_Testing/1.0) (available in Node) & a suite for running tests
* console - [Console](http://wiki.commonjs.org/wiki/Console) - partial implementation available in Node
* system - [System/1.0](http://wiki.commonjs.org/wiki/System/1.0)
* binary - [Binary/B](http://wiki.commonjs.org/wiki/Binary/B)
645 lib/assert.js
View
@@ -3,515 +3,144 @@
* Testing](http://wiki.commonjs.org/wiki/Unit_Testing/1.0).
*/
-function stringify(object) {
- return JSON.stringify(object).substring(0, 32);
-}
-
-function getStackTrace() {
- // TODO check what format this should be in
- return new Error().stack.split('\n').splice(1);
-}
-
-/**
- * Returns the type of the object passed as argument. This is heavily inspired
- * by http://philrathe.com/articles/equiv and tlrobinson's Narwhal test module
- * (http://github.com/tlrobinson/narwhal/)
- *
- * @returns The type of the object passed as argument
- * @type String
- */
-function getType(obj) {
- if (typeof(obj) === "string") {
- return "string";
- } else if (typeof(obj) === "boolean") {
- return "boolean";
- } else if (typeof (obj) === "number") {
- return (isNaN(obj) === true) ? "nan" : "number";
- } else if (typeof(obj) === "undefined") {
- return "undefined";
- } else if (obj === null) {
- return "null";
- } else if (obj instanceof Array) {
- return "array";
- } else if (obj instanceof Date) {
- return "date";
- } else if (obj instanceof RegExp) {
- return "regexp";
- } else if (obj instanceof Function) {
- return "function";
- }
- return "object";
-};
-
-/**
- * @param {Object}
- * args The arguments array.
- * @param {Number}
- * argsExpected The number of expected arguments
- * @returns The comment appended to the expected arguments, if any
- * @type String
- */
-function evalArguments(args, argsExpected) {
- if (!(args.length == argsExpected ||
- (args.length == argsExpected + 1 && getType(args[args.length - 1]) == "string"))) {
- throw new ArgumentsError("Insufficient arguments passed to assertion function");
- }
- return args[argsExpected];
-}
-;
-
-/**
- * Deep-compares both arguments
- *
- * @param {Object}
- * value1 The argument to be compared
- * @param {Object}
- * value2 The argument to be compared to
- * @returns True if arguments are equal, false otherwise
- * @type Boolean
- */
-function isDeepEqual(value1, value2) {
- if (value1 === value2) {
- return true;
- } else if (value1 instanceof Date && value2 instanceof Date) {
- return value1.getTime() === value2.getTime();
- } else if (typeof(value1) != "object" || typeof(value2) != "object") {
- return value1 == value2;
- } else {
- return objectsAreEqual(value1, value2);
- }
-}
-
-/**
- * Returns true if the objects passed as argument are equal
- *
- * @param {Object}
- * value1 The object to be compared
- * @param {Object}
- * value2 The object to be compared to
- * @returns True if the objects are equal, false otherwise
- * @type Boolean
- */
-function objectsAreEqual(obj1, obj2) {
- if (isNullOrUndefined(obj1) || isNullOrUndefined(obj2)) {
- return false;
- }
- // the 1.0 spec (and Unittest/B) speaks of comparing the prototype
- // property, which is only set for constructor functions (for instances
- // it's undefined). plus only owned properties are compared, leading
- // to two objects being equivalent even if their prototypes have
- // different properties. instead using Object.getPrototypeOf()
- // to compare the prototypes of two objects
- // see also http://groups.google.com/group/commonjs/msg/501a7e3cd9a920e5
- if (Object.getPrototypeOf(obj1) !== Object.getPrototypeOf(obj2)) {
- return false;
- }
- // compare object keys (objects *and* arrays)
- var keys1 = getOwnKeys(obj1);
- var keys2 = getOwnKeys(obj2);
- var propsAreEqual = keys1.length === keys2.length && keys1.every(function(name, idx) {
- return name === keys2[idx] && isDeepEqual(obj1[name], obj2[name]);
- });
- if (propsAreEqual === false) {
- return propsAreEqual;
- }
- // array comparison
- if (getType(obj1) === "array") {
- return obj1.length === obj2.length && obj1.every(function(value, idx) {
- return isDeepEqual(value, obj2[idx]);
- });
- }
- return true;
-}
-
-/**
- * Returns true if the argument is null or undefined
- *
- * @param {Object}
- * obj The object to test
- * @returns True if the argument is null or undefined
- * @type Boolean
- */
-function isNullOrUndefined(obj) {
- return obj === null || obj === undefined;
-}
-
-/**
- * Returns the names of owned properties of the object passed as argument. Note
- * that this only includes those properties for which hasOwnProperty returns
- * true
- *
- * @param {Object}
- * obj The object to return its propery names for
- * @returns The property names
- * @type Array
- */
-function getOwnKeys(obj) {
- var keys = [];
- for (key in obj) {
- if (Object.prototype.hasOwnProperty.call(obj, key))
- keys.push(key);
- }
- return keys.sort();
-}
-
-/**
- * Basic failure method
- *
- * @param {Object|String}
- * options An object containing optional "message", "actual" and
- * "expected" properties, or alternatively a message string
- * @throws AssertionError
- */
-function fail(options) {
- throw new AssertionError(options);
-}
-
-/**
- * Prepends the comment to the message, if given
- *
- * @returns The message
- * @type String
- */
-function prependComment(message, comment) {
- if (getType(comment) === "string" && comment.length > 0) {
- return comment + "\n" + message;
- }
- return message;
-}
-
+module.exports = require('assert');
/*******************************************************************************
- * **** E R R O R S *****
- ******************************************************************************/
-
-
-/**
- * Constructs a new AssertionError instance
- *
- * @class Instances of this class represent an assertion error
- * @param {Object}
- * options An object containing error details
- * @param.message {String} The error message
- * @param.actual {Object} The actual value
- * @param.expected {Object} The expected value
- * @constructor
- * @augments Error
- */
-var AssertionError = function(options) {
- // accept a single string argument
- if (getType(options) === "string") {
- options = {
- "message": options
- };
- }
- var stackTrace = getStackTrace();
-
- Object.defineProperty(this, "name", {
- get: function() {
- return "AssertionError";
- }
- });
-
- Object.defineProperty(this, "message", {
- get: function() {
- return options.message;
- }
- });
-
- Object.defineProperty(this, "actual", {
- get: function() {
- return options.actual;
- }
- });
-
- Object.defineProperty(this, "expected", {
- get: function() {
- return options.expected;
- }
- });
-
- Object.defineProperty(this, "stackTrace", {
- get: function() {
- return stackTrace;
- }
- });
-
- return this;
-};
-
-/** @ignore */
-AssertionError.prototype = new Error();
-
-/** @ignore */
-AssertionError.toString = function() {
- return "[AssertionError]";
-};
-
-/** @ignore */
-AssertionError.prototype.toString = function() {
- return "[AssertionError '" + this.message + "']";
-};
-
-/**
- * Creates a new ArgumentsError instance
- *
- * @class Instances of this class represent an error thrown if insufficient
- * arguments have been passed to an assertion function
- * @param {String}
- * message The exception message
- * @returns A newly created ArgumentsError instance
- * @constructor
- */
-var ArgumentsError = exports.ArgumentsError = function(message) {
-
- var stackTrace = getStackTrace();
-
- Object.defineProperty(this, "message", {
- get: function() {
- return message;
- }
- });
-
- Object.defineProperty(this, "stackTrace", {
- get: function() {
- return stackTrace;
- }
- });
-
- return this;
-};
-
-/** @ignore */
-ArgumentsError.prototype = new Error();
-
-/** @ignore */
-ArgumentsError.toString = function() {
- return "[ArgumentsError]";
-};
-
-/** @ignore */
-ArgumentsError.prototype.toString = function() {
- return "[ArgumentsError '" + this.message + "']";
-};
-
+* **** E R R O R S *****
+******************************************************************************/
+
+/**
+* Constructs a new AssertionError instance
+*
+* @class Instances of this class represent an assertion error
+* @param {Object}
+* options An object containing error details
+* @param.message {String} The error message
+* @param.actual {Object} The actual value
+* @param.expected {Object} The expected value
+* @constructor
+* @augments Error
+*
+* @name AssertionError
+*/
+
+/**
+* Creates a new ArgumentsError instance
+*
+* @class Instances of this class represent an error thrown if insufficient
+* arguments have been passed to an assertion function
+* @param {String}
+* message The exception message
+* @returns A newly created ArgumentsError instance
+* @constructor
+*
+* @name ArgumentsError
+*/
/*******************************************************************************
- * **** C O M M O N J S A S S E R T I O N M E T H O D S *****
- ******************************************************************************/
-
-
-/**
- * Checks if the value passed as argument is truthy.
- *
- * @param {Object}
- * value The value to check for truthiness
- * @throws ArgumentsError
- * @throws AssertionError
- */
-var ok = exports.ok = function(value) {
- var comment = evalArguments(arguments, arguments.callee.length);
- if (!!value === false) {
- fail({
- "message": prependComment("Expected " + JSON.stringify(value) + " to be truthy", comment),
- "actual": value,
- "expected": true
- });
- }
- return;
-};
-
-/**
- * Checks if the values passed as arguments are equal.
- *
- * @param {Object}
- * actual The actual value
- * @param {Object}
- * expected The expected value
- * @throws ArgumentsError
- * @throws AssertionError
- */
-var equal = exports.equal = function(actual, expected) {
- var comment = evalArguments(arguments, arguments.callee.length);
- if (actual != expected) {
- fail({
- "message": prependComment("Expected " + stringify(expected) + ", got " + stringify(actual), comment),
- "actual": actual,
- "expected": expected
- });
- }
- return;
-};
-
-/**
- * Checks if the values passed as arguments are not equal.
- *
- * @param {Object}
- * actual The actual value
- * @param {Object}
- * expected The expected value
- * @throws ArgumentsError
- * @throws AssertionError
- */
-var notEqual = exports.notEqual = function(actual, expected) {
- var comment = evalArguments(arguments, arguments.callee.length);
- if (actual == expected) {
- fail({
- "message": prependComment("Expected different value than " + stringify(expected) +
- ", got equivalent value " + stringify(actual), comment),
- "actual": actual,
- "expected": expected
- });
- }
- return;
-};
-
-/**
- * Checks if the values passed as arguments are deep equal
- *
- * @param {Object}
- * actual The actual value
- * @param {Object}
- * expected The expected value
- * @throws ArgumentsError
- * @throws AssertionError
- */
-var deepEqual = exports.deepEqual = function(actual, expected) {
- var comment = evalArguments(arguments, arguments.callee.length);
- if (isDeepEqual(actual, expected) === false) {
- fail({
- "message": prependComment("Expected " + stringify(expected) + ", got " + stringify(actual), comment),
- "actual": actual,
- "expected": expected
- });
- }
- return;
-};
-
-/**
- * Checks if the values passed as arguments are not deep equal
- *
- * @param {Object}
- * actual The actual value
- * @param {Object}
- * expected The expected value
- * @throws ArgumentsError
- * @throws AssertionError
- */
-var notDeepEqual = exports.notDeepEqual = function(actual, expected) {
- var comment = evalArguments(arguments, arguments.callee.length);
- if (isDeepEqual(actual, expected) === true) {
- fail({
- "message": prependComment("Expected different value than " + stringify(expected) +
- ", got deep equal value " + stringify(actual), comment),
- "actual": actual,
- "expected": expected
- });
- }
- return;
-};
-
-/**
- * Checks if the values passed as arguments are strictly equal
- *
- * @param {Object}
- * actual The actual value
- * @param {Object}
- * expected The expected value
- * @throws ArgumentsError
- * @throws AssertionError
- */
-var strictEqual = exports.strictEqual = function(actual, expected) {
- var comment = evalArguments(arguments, arguments.callee.length);
- if (actual !== expected) {
- fail({
- // TODO determine the right way to show actual and expected
- "message": prependComment("Expected " + stringify(actual) + ", got " + stringify(expected), comment),
- "actual": actual,
- "expected": expected
- });
- }
- return;
-};
-
-/**
- * Checks if the values passed as arguments are not strictly equal
- *
- * @param {Object}
- * actual The actual value
- * @param {Object}
- * expected The expected value
- * @throws ArgumentsError
- * @throws AssertionError
- */
-var notStrictEqual = exports.notStrictEqual = function(actual, expected) {
- var comment = evalArguments(arguments, arguments.callee.length);
- if (actual === expected) {
- fail({
- "message": prependComment("Expected different value than " + stringify(expected) +
- ", got strictly equal value " + stringify(actual), comment),
- "actual": actual,
- "expected": expected
- });
- }
- return;
-};
-
-/**
- * Checks if the function passed as argument throws a defined exception.
- *
- * @param {Object}
- * func The function to call
- * @param {Object}
- * expectedError Optional object expected to be thrown when executing
- * the function
- * @throws ArgumentsError
- * @throws AssertionError
- */
-var throws = exports.throws = function(func, expectedError) {
- if (!(func instanceof Function)) {
- throw new ArgumentsError("First argument to throws() must be a function");
- }
- try {
- func();
- } catch (e) {
- var isExpected = false;
- var thrown = e;
- e = thrown;
- if (expectedError == null) {
- // accept everything
- isExpected = true;
- } else if (expectedError != null && e != null) {
- // check if exception is the one expected
- switch (typeof(expectedError)) {
- case "string":
- isExpected = (e.name === expectedError || e === expectedError);
- break;
- case "function":
- // this is true for all JS constructors and Java classes!
- isExpected = (e instanceof expectedError ||
- (thrown = e.rhinoException) instanceof expectedError ||
- (thrown = e.javaException) instanceof expectedError);
- break;
- case "number":
- case "boolean":
- default:
- isExpected = (e === expectedError);
- break;
- }
- }
- if (!isExpected) {
- fail({
- "message": "Expected " + stringify(expectedError) +
- " to be thrown, but got " + stringify(e) + " instead",
- "actual": e,
- "expected": expectedError
- });
- }
- return;
- }
- if (expectedError != null) {
- fail("Expected exception " + stringify(expectedError) + " to be thrown");
- }
- fail("Expected exception to be thrown");
-};
+* **** C O M M O N J S A S S E R T I O N M E T H O D S *****
+******************************************************************************/
+
+/**
+* Checks if the value passed as argument is truthy.
+*
+* @param {Object}
+* value The value to check for truthiness
+* @throws ArgumentsError
+* @throws AssertionError
+*
+* @name ok
+*/
+
+
+/**
+* Checks if the values passed as arguments are equal.
+*
+* @param {Object}
+* actual The actual value
+* @param {Object}
+* expected The expected value
+* @throws ArgumentsError
+* @throws AssertionError
+*
+* @name equal
+*/
+
+/**
+* Checks if the values passed as arguments are not equal.
+*
+* @param {Object}
+* actual The actual value
+* @param {Object}
+* expected The expected value
+* @throws ArgumentsError
+* @throws AssertionError
+*
+* @name notEqual
+*/
+
+/**
+* Checks if the values passed as arguments are deep equal
+*
+* @param {Object}
+* actual The actual value
+* @param {Object}
+* expected The expected value
+* @throws ArgumentsError
+* @throws AssertionError
+*
+* @name deepEqual
+*/
+
+/**
+* Checks if the values passed as arguments are not deep equal
+*
+* @param {Object}
+* actual The actual value
+* @param {Object}
+* expected The expected value
+* @throws ArgumentsError
+* @throws AssertionError
+*
+* @name notDeepEqual
+*/
+
+/**
+* Checks if the values passed as arguments are strictly equal
+*
+* @param {Object}
+* actual The actual value
+* @param {Object}
+* expected The expected value
+* @throws ArgumentsError
+* @throws AssertionError
+*
+* @name strictEqual
+*/
+
+/**
+* Checks if the values passed as arguments are not strictly equal
+*
+* @param {Object}
+* actual The actual value
+* @param {Object}
+* expected The expected value
+* @throws ArgumentsError
+* @throws AssertionError
+*
+* @name notStrictEqual
+*/
+
+/**
+* Checks if the function passed as argument throws a defined exception.
+*
+* @param {Object}
+* func The function to call
+* @param {Object}
+* expectedError Optional object expected to be thrown when executing
+* the function
+* @throws ArgumentsError
+* @throws AssertionError
+*
+* @name throws
+*/
Please sign in to comment.
Something went wrong with that request. Please try again.