react / react-0.13.3 / examples / basic-commonjs / node_modules / browserify / node_modules / assert / test.js
80713 views// Copyright Joyent, Inc. and other Node contributors.1//2// Permission is hereby granted, free of charge, to any person obtaining a3// copy of this software and associated documentation files (the4// "Software"), to deal in the Software without restriction, including5// without limitation the rights to use, copy, modify, merge, publish,6// distribute, sublicense, and/or sell copies of the Software, and to permit7// persons to whom the Software is furnished to do so, subject to the8// following conditions:9//10// The above copyright notice and this permission notice shall be included11// in all copies or substantial portions of the Software.12//13// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS14// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF15// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN16// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,17// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR18// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE19// USE OR OTHER DEALINGS IN THE SOFTWARE.2021var assert = require('./');2223var keys = Object.keys;2425function makeBlock(f) {26var args = Array.prototype.slice.call(arguments, 1);27return function() {28return f.apply(this, args);29};30}3132test('assert.ok', function () {33assert.throws(makeBlock(assert, false), assert.AssertionError, 'ok(false)');3435assert.doesNotThrow(makeBlock(assert, true), assert.AssertionError, 'ok(true)');3637assert.doesNotThrow(makeBlock(assert, 'test', 'ok(\'test\')'));3839assert.throws(makeBlock(assert.ok, false),40assert.AssertionError, 'ok(false)');4142assert.doesNotThrow(makeBlock(assert.ok, true),43assert.AssertionError, 'ok(true)');4445assert.doesNotThrow(makeBlock(assert.ok, 'test'), 'ok(\'test\')');46});4748test('assert.equal', function () {49assert.throws(makeBlock(assert.equal, true, false), assert.AssertionError, 'equal');5051assert.doesNotThrow(makeBlock(assert.equal, null, null), 'equal');5253assert.doesNotThrow(makeBlock(assert.equal, undefined, undefined), 'equal');5455assert.doesNotThrow(makeBlock(assert.equal, null, undefined), 'equal');5657assert.doesNotThrow(makeBlock(assert.equal, true, true), 'equal');5859assert.doesNotThrow(makeBlock(assert.equal, 2, '2'), 'equal');6061assert.doesNotThrow(makeBlock(assert.notEqual, true, false), 'notEqual');6263assert.throws(makeBlock(assert.notEqual, true, true),64assert.AssertionError, 'notEqual');65});6667test('assert.strictEqual', function () {68assert.throws(makeBlock(assert.strictEqual, 2, '2'),69assert.AssertionError, 'strictEqual');7071assert.throws(makeBlock(assert.strictEqual, null, undefined),72assert.AssertionError, 'strictEqual');7374assert.doesNotThrow(makeBlock(assert.notStrictEqual, 2, '2'), 'notStrictEqual');75});7677test('assert.deepEqual - 7.2', function () {78assert.doesNotThrow(makeBlock(assert.deepEqual, new Date(2000, 3, 14),79new Date(2000, 3, 14)), 'deepEqual date');8081assert.throws(makeBlock(assert.deepEqual, new Date(), new Date(2000, 3, 14)),82assert.AssertionError,83'deepEqual date');84});8586test('assert.deepEqual - 7.3', function () {87assert.doesNotThrow(makeBlock(assert.deepEqual, /a/, /a/));88assert.doesNotThrow(makeBlock(assert.deepEqual, /a/g, /a/g));89assert.doesNotThrow(makeBlock(assert.deepEqual, /a/i, /a/i));90assert.doesNotThrow(makeBlock(assert.deepEqual, /a/m, /a/m));91assert.doesNotThrow(makeBlock(assert.deepEqual, /a/igm, /a/igm));92assert.throws(makeBlock(assert.deepEqual, /ab/, /a/));93assert.throws(makeBlock(assert.deepEqual, /a/g, /a/));94assert.throws(makeBlock(assert.deepEqual, /a/i, /a/));95assert.throws(makeBlock(assert.deepEqual, /a/m, /a/));96assert.throws(makeBlock(assert.deepEqual, /a/igm, /a/im));9798var re1 = /a/;99re1.lastIndex = 3;100assert.throws(makeBlock(assert.deepEqual, re1, /a/));101});102103test('assert.deepEqual - 7.4', function () {104assert.doesNotThrow(makeBlock(assert.deepEqual, 4, '4'), 'deepEqual == check');105assert.doesNotThrow(makeBlock(assert.deepEqual, true, 1), 'deepEqual == check');106assert.throws(makeBlock(assert.deepEqual, 4, '5'),107assert.AssertionError,108'deepEqual == check');109});110111test('assert.deepEqual - 7.5', function () {112// having the same number of owned properties && the same set of keys113assert.doesNotThrow(makeBlock(assert.deepEqual, {a: 4}, {a: 4}));114assert.doesNotThrow(makeBlock(assert.deepEqual, {a: 4, b: '2'}, {a: 4, b: '2'}));115assert.doesNotThrow(makeBlock(assert.deepEqual, [4], ['4']));116assert.throws(makeBlock(assert.deepEqual, {a: 4}, {a: 4, b: true}),117assert.AssertionError);118assert.doesNotThrow(makeBlock(assert.deepEqual, ['a'], {0: 'a'}));119//(although not necessarily the same order),120assert.doesNotThrow(makeBlock(assert.deepEqual, {a: 4, b: '1'}, {b: '1', a: 4}));121var a1 = [1, 2, 3];122var a2 = [1, 2, 3];123a1.a = 'test';124a1.b = true;125a2.b = true;126a2.a = 'test';127assert.throws(makeBlock(assert.deepEqual, keys(a1), keys(a2)),128assert.AssertionError);129assert.doesNotThrow(makeBlock(assert.deepEqual, a1, a2));130});131132test('assert.deepEqual - instances', function () {133// having an identical prototype property134var nbRoot = {135toString: function() { return this.first + ' ' + this.last; }136};137138function nameBuilder(first, last) {139this.first = first;140this.last = last;141return this;142}143nameBuilder.prototype = nbRoot;144145function nameBuilder2(first, last) {146this.first = first;147this.last = last;148return this;149}150nameBuilder2.prototype = nbRoot;151152var nb1 = new nameBuilder('Ryan', 'Dahl');153var nb2 = new nameBuilder2('Ryan', 'Dahl');154155assert.doesNotThrow(makeBlock(assert.deepEqual, nb1, nb2));156157nameBuilder2.prototype = Object;158nb2 = new nameBuilder2('Ryan', 'Dahl');159assert.throws(makeBlock(assert.deepEqual, nb1, nb2), assert.AssertionError);160161// String literal + object blew up my implementation...162assert.throws(makeBlock(assert.deepEqual, 'a', {}), assert.AssertionError);163});164165function thrower(errorConstructor) {166throw new errorConstructor('test');167}168169test('assert - Testing the throwing', function () {170var aethrow = makeBlock(thrower, assert.AssertionError);171aethrow = makeBlock(thrower, assert.AssertionError);172173// the basic calls work174assert.throws(makeBlock(thrower, assert.AssertionError),175assert.AssertionError, 'message');176assert.throws(makeBlock(thrower, assert.AssertionError), assert.AssertionError);177assert.throws(makeBlock(thrower, assert.AssertionError));178179// if not passing an error, catch all.180assert.throws(makeBlock(thrower, TypeError));181182// when passing a type, only catch errors of the appropriate type183var threw = false;184try {185assert.throws(makeBlock(thrower, TypeError), assert.AssertionError);186} catch (e) {187threw = true;188assert.ok(e instanceof TypeError, 'type');189}190assert.equal(true, threw,191'a.throws with an explicit error is eating extra errors',192assert.AssertionError);193threw = false;194195// doesNotThrow should pass through all errors196try {197assert.doesNotThrow(makeBlock(thrower, TypeError), assert.AssertionError);198} catch (e) {199threw = true;200assert.ok(e instanceof TypeError);201}202assert.equal(true, threw,203'a.doesNotThrow with an explicit error is eating extra errors');204205// key difference is that throwing our correct error makes an assertion error206try {207assert.doesNotThrow(makeBlock(thrower, TypeError), TypeError);208} catch (e) {209threw = true;210assert.ok(e instanceof assert.AssertionError);211}212assert.equal(true, threw,213'a.doesNotThrow is not catching type matching errors');214});215216test('assert.ifError', function () {217assert.throws(function() {assert.ifError(new Error('test error'))});218assert.doesNotThrow(function() {assert.ifError(null)});219assert.doesNotThrow(function() {assert.ifError()});220});221222test('assert - make sure that validating using constructor really works', function () {223var threw = false;224try {225assert.throws(226function() {227throw ({});228},229Array230);231} catch (e) {232threw = true;233}234assert.ok(threw, 'wrong constructor validation');235});236237test('assert - use a RegExp to validate error message', function () {238assert.throws(makeBlock(thrower, TypeError), /test/);239});240241test('assert - se a fn to validate error object', function () {242assert.throws(makeBlock(thrower, TypeError), function(err) {243if ((err instanceof TypeError) && /test/.test(err)) {244return true;245}246});247});248249test('assert - Make sure deepEqual doesn\'t loop forever on circular refs', function () {250var b = {};251b.b = b;252253var c = {};254c.b = c;255256var gotError = false;257try {258assert.deepEqual(b, c);259} catch (e) {260gotError = true;261}262263assert.ok(gotError);264});265266267test('assert - test assertion message', function () {268function testAssertionMessage(actual, expected) {269try {270assert.equal(actual, '');271} catch (e) {272assert.equal(e.toString(),273['AssertionError:', expected, '==', '""'].join(' '));274}275}276testAssertionMessage(undefined, '"undefined"');277testAssertionMessage(null, 'null');278testAssertionMessage(true, 'true');279testAssertionMessage(false, 'false');280testAssertionMessage(0, '0');281testAssertionMessage(100, '100');282testAssertionMessage(NaN, '"NaN"');283testAssertionMessage(Infinity, '"Infinity"');284testAssertionMessage(-Infinity, '"-Infinity"');285testAssertionMessage('', '""');286testAssertionMessage('foo', '"foo"');287testAssertionMessage([], '[]');288testAssertionMessage([1, 2, 3], '[1,2,3]');289testAssertionMessage(/a/, '"/a/"');290testAssertionMessage(function f() {}, '"function f() {}"');291testAssertionMessage({}, '{}');292testAssertionMessage({a: undefined, b: null}, '{"a":"undefined","b":null}');293testAssertionMessage({a: NaN, b: Infinity, c: -Infinity},294'{"a":"NaN","b":"Infinity","c":"-Infinity"}');295});296297test('assert - regressions from node.js testcase', function () {298var threw = false;299300try {301assert.throws(function () {302assert.ifError(null);303});304} catch (e) {305threw = true;306assert.equal(e.message, 'Missing expected exception..');307}308assert.ok(threw);309310try {311assert.equal(1, 2);312} catch (e) {313assert.equal(e.toString().split('\n')[0], 'AssertionError: 1 == 2');314}315316try {317assert.equal(1, 2, 'oh no');318} catch (e) {319assert.equal(e.toString().split('\n')[0], 'AssertionError: oh no');320}321});322323324