123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279 |
- // implement assert interface using already written peaces of should.js
- // 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.
- // when used in node, this will actually load the util module we depend on
- // versus loading the builtin util module as happens otherwise
- // this is a bug in node module loading as far as I am concerned
- var Assertion = require('./../assertion');
- var _deepEqual = require('should-equal');
- 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 = module.exports = ok;
- // 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.
- /**
- * Node.js standard [`assert.fail`](http://nodejs.org/api/assert.html#assert_assert_fail_actual_expected_message_operator).
- * @static
- * @memberOf should
- * @category assertion assert
- * @param {*} actual Actual object
- * @param {*} expected Expected object
- * @param {string} message Message for assertion
- * @param {string} operator Operator text
- */
- function fail(actual, expected, message, operator, stackStartFunction) {
- var a = new Assertion(actual);
- a.params = {
- operator: operator,
- expected: expected,
- message: message,
- stackStartFunction: stackStartFunction || fail
- };
- a.fail();
- }
- // 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);.
- /**
- * Node.js standard [`assert.ok`](http://nodejs.org/api/assert.html#assert_assert_value_message_assert_ok_value_message).
- * @static
- * @memberOf should
- * @category assertion assert
- * @param {*} value
- * @param {string} [message]
- */
- function ok(value, message) {
- if(!value) fail(value, true, message, '==', assert.ok);
- }
- assert.ok = ok;
- // 5. The equality assertion tests shallow, coercive equality with
- // ==.
- // assert.equal(actual, expected, message_opt);
- /**
- * Node.js standard [`assert.equal`](http://nodejs.org/api/assert.html#assert_assert_equal_actual_expected_message).
- * @static
- * @memberOf should
- * @category assertion assert
- * @param {*} actual
- * @param {*} expected
- * @param {string} [message]
- */
- 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);
- /**
- * Node.js standard [`assert.notEqual`](http://nodejs.org/api/assert.html#assert_assert_notequal_actual_expected_message).
- * @static
- * @memberOf should
- * @category assertion assert
- * @param {*} actual
- * @param {*} expected
- * @param {string} [message]
- */
- 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);
- /**
- * Node.js standard [`assert.deepEqual`](http://nodejs.org/api/assert.html#assert_assert_deepequal_actual_expected_message).
- * @static
- * @memberOf should
- * @category assertion assert
- * @param {*} actual
- * @param {*} expected
- * @param {string} [message]
- */
- assert.deepEqual = function deepEqual(actual, expected, message) {
- if(!_deepEqual(actual, expected).result) {
- fail(actual, expected, message, 'deepEqual', assert.deepEqual);
- }
- };
- // 8. The non-equivalence assertion tests for any deep inequality.
- // assert.notDeepEqual(actual, expected, message_opt);
- /**
- * Node.js standard [`assert.notDeepEqual`](http://nodejs.org/api/assert.html#assert_assert_notdeepequal_actual_expected_message).
- * @static
- * @memberOf should
- * @category assertion assert
- * @param {*} actual
- * @param {*} expected
- * @param {string} [message]
- */
- assert.notDeepEqual = function notDeepEqual(actual, expected, message) {
- if(_deepEqual(actual, expected).result) {
- fail(actual, expected, message, 'notDeepEqual', assert.notDeepEqual);
- }
- };
- // 9. The strict equality assertion tests strict equality, as determined by ===.
- // assert.strictEqual(actual, expected, message_opt);
- /**
- * Node.js standard [`assert.strictEqual`](http://nodejs.org/api/assert.html#assert_assert_strictequal_actual_expected_message).
- * @static
- * @memberOf should
- * @category assertion assert
- * @param {*} actual
- * @param {*} expected
- * @param {string} [message]
- */
- 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);
- /**
- * Node.js standard [`assert.notStrictEqual`](http://nodejs.org/api/assert.html#assert_assert_notstrictequal_actual_expected_message).
- * @static
- * @memberOf should
- * @category assertion assert
- * @param {*} actual
- * @param {*} expected
- * @param {string} [message]
- */
- assert.notStrictEqual = function notStrictEqual(actual, expected, message) {
- if(actual === expected) {
- fail(actual, expected, message, '!==', assert.notStrictEqual);
- }
- };
- function expectedException(actual, expected) {
- if(!actual || !expected) {
- return false;
- }
- if(Object.prototype.toString.call(expected) == '[object RegExp]') {
- return expected.test(actual);
- } else if(actual instanceof expected) {
- return true;
- } else if(expected.call({}, actual) === true) {
- return true;
- }
- return false;
- }
- function _throws(shouldThrow, block, expected, message) {
- var actual;
- if(typeof expected == 'string') {
- message = expected;
- expected = null;
- }
- try {
- block();
- } catch(e) {
- actual = e;
- }
- message = (expected && expected.name ? ' (' + expected.name + ')' : '.') +
- (message ? ' ' + message : '.');
- if(shouldThrow && !actual) {
- fail(actual, expected, 'Missing expected exception' + message);
- }
- if(!shouldThrow && expectedException(actual, expected)) {
- fail(actual, expected, 'Got unwanted exception' + message);
- }
- if((shouldThrow && actual && expected && !expectedException(actual, expected)) || (!shouldThrow && actual)) {
- throw actual;
- }
- }
- // 11. Expected to throw an error:
- // assert.throws(block, Error_opt, message_opt);
- /**
- * Node.js standard [`assert.throws`](http://nodejs.org/api/assert.html#assert_assert_throws_block_error_message).
- * @static
- * @memberOf should
- * @category assertion assert
- * @param {Function} block
- * @param {Function} [error]
- * @param {String} [message]
- */
- 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.
- /**
- * Node.js standard [`assert.doesNotThrow`](http://nodejs.org/api/assert.html#assert_assert_doesnotthrow_block_message).
- * @static
- * @memberOf should
- * @category assertion assert
- * @param {Function} block
- * @param {String} [message]
- */
- assert.doesNotThrow = function(block, /*optional*/message) {
- _throws.apply(this, [false].concat(pSlice.call(arguments)));
- };
- /**
- * Node.js standard [`assert.ifError`](http://nodejs.org/api/assert.html#assert_assert_iferror_value).
- * @static
- * @memberOf should
- * @category assertion assert
- * @param {Error} err
- */
- assert.ifError = function(err) {
- if(err) {
- throw err;
- }
- };
|