diff options
-rw-r--r-- | jstests/core/apitest_dbcollection.js | 50 | ||||
-rw-r--r-- | jstests/core/bulk_api_ordered.js | 2 | ||||
-rw-r--r-- | jstests/core/bulk_api_unordered.js | 7 | ||||
-rw-r--r-- | jstests/core/distinct4.js | 17 | ||||
-rw-r--r-- | jstests/core/finda.js | 12 | ||||
-rw-r--r-- | jstests/core/or1.js | 8 | ||||
-rw-r--r-- | jstests/core/or2.js | 6 | ||||
-rw-r--r-- | jstests/core/or3.js | 6 | ||||
-rw-r--r-- | jstests/core/uniqueness.js | 12 | ||||
-rw-r--r-- | jstests/mmap_v1/index_check1.js | 14 | ||||
-rw-r--r-- | jstests/multiVersion/libs/verify_collection_data.js | 2 | ||||
-rw-r--r-- | jstests/noPassthrough/backup_restore.js | 4 | ||||
-rw-r--r-- | jstests/sharding/diffservers1.js | 2 | ||||
-rw-r--r-- | jstests/sharding/movePrimary1.js | 2 | ||||
-rw-r--r-- | jstests/sharding/sync_cluster_config/sync7.js | 2 | ||||
-rw-r--r-- | src/mongo/shell/assert.js | 11 |
16 files changed, 87 insertions, 70 deletions
diff --git a/jstests/core/apitest_dbcollection.js b/jstests/core/apitest_dbcollection.js index 4142ef8d7dc..d60778363b8 100644 --- a/jstests/core/apitest_dbcollection.js +++ b/jstests/core/apitest_dbcollection.js @@ -1,5 +1,5 @@ /** - * Tests for the db collection + * Tests for the db collection */ @@ -8,34 +8,34 @@ * test drop */ db.getCollection( "test_db" ).drop(); -assert(db.getCollection( "test_db" ).find().length() == 0,1); +assert.eq(0, db.getCollection( "test_db" ).find().length(), "1"); db.getCollection( "test_db" ).save({a:1}); -assert(db.getCollection( "test_db" ).find().length() == 1,2); +assert.eq(1, db.getCollection( "test_db" ).find().length(), "2"); db.getCollection( "test_db" ).drop(); -assert(db.getCollection( "test_db" ).find().length() == 0,3); +assert.eq(0, db.getCollection( "test_db" ).find().length(), "3"); /* * test count */ - -assert(db.getCollection( "test_db" ).count() == 0,4); + +assert.eq(0, db.getCollection( "test_db" ).count(), "4"); db.getCollection( "test_db" ).save({a:1}); -assert(db.getCollection( "test_db" ).count() == 1,5); +assert.eq(1, db.getCollection( "test_db" ).count(), "5"); for (i = 0; i < 100; i++) { db.getCollection( "test_db" ).save({a:1}); } -assert(db.getCollection( "test_db" ).count() == 101,6); +assert.eq(101, db.getCollection( "test_db" ).count(), "6"); db.getCollection( "test_db" ).drop(); -assert(db.getCollection( "test_db" ).count() == 0,7); - +assert.eq(0, db.getCollection( "test_db" ).count(), "7"); + /* * test validate */ db.getCollection( "test_db" ).drop(); -assert(db.getCollection( "test_db" ).count() == 0,8); +assert.eq(0, db.getCollection( "test_db" ).count(), "8"); for (i = 0; i < 100; i++) { db.getCollection( "test_db" ).save({a:1}); @@ -82,59 +82,59 @@ for (i = 0; i < 100; i++) { /* * test deleteIndex, deleteIndexes */ - + db.getCollection( "test_db" ).drop(); -assert(db.getCollection( "test_db" ).count() == 0,12); +assert.eq(0, db.getCollection( "test_db" ).count(), "12"); db.getCollection( "test_db" ).dropIndexes(); -assert(db.getCollection( "test_db" ).getIndexes().length == 0,13); +assert.eq(0, db.getCollection( "test_db" ).getIndexes().length, "13"); db.getCollection( "test_db" ).save({a:10}); -assert(db.getCollection( "test_db" ).getIndexes().length == 1,14); +assert.eq(1, db.getCollection( "test_db" ).getIndexes().length, "14"); db.getCollection( "test_db" ).ensureIndex({a:1}); db.getCollection( "test_db" ).save({a:10}); print( tojson( db.getCollection( "test_db" ).getIndexes() ) ); -assert.eq(db.getCollection( "test_db" ).getIndexes().length , 2,15); +assert.eq(2, db.getCollection( "test_db" ).getIndexes().length, "15"); db.getCollection( "test_db" ).dropIndex({a:1}); -assert(db.getCollection( "test_db" ).getIndexes().length == 1,16); +assert.eq(1, db.getCollection( "test_db" ).getIndexes().length, "16"); db.getCollection( "test_db" ).save({a:10}); db.getCollection( "test_db" ).ensureIndex({a:1}); db.getCollection( "test_db" ).save({a:10}); -assert(db.getCollection( "test_db" ).getIndexes().length == 2,17); +assert.eq(2, db.getCollection( "test_db" ).getIndexes().length, "17"); db.getCollection( "test_db" ).dropIndex("a_1"); -assert.eq( db.getCollection( "test_db" ).getIndexes().length , 1,18); +assert.eq(1, db.getCollection( "test_db" ).getIndexes().length, "18"); db.getCollection( "test_db" ).save({a:10, b:11}); db.getCollection( "test_db" ).ensureIndex({a:1}); db.getCollection( "test_db" ).ensureIndex({b:1}); db.getCollection( "test_db" ).save({a:10, b:12}); -assert(db.getCollection( "test_db" ).getIndexes().length == 3,19); +assert.eq(3, db.getCollection( "test_db" ).getIndexes().length, "19"); db.getCollection( "test_db" ).dropIndex({b:1}); -assert(db.getCollection( "test_db" ).getIndexes().length == 2,20); +assert.eq(2, db.getCollection( "test_db" ).getIndexes().length, "20"); db.getCollection( "test_db" ).dropIndex({a:1}); -assert(db.getCollection( "test_db" ).getIndexes().length == 1,21); +assert.eq(1, db.getCollection( "test_db" ).getIndexes().length, "21"); db.getCollection( "test_db" ).save({a:10, b:11}); db.getCollection( "test_db" ).ensureIndex({a:1}); db.getCollection( "test_db" ).ensureIndex({b:1}); db.getCollection( "test_db" ).save({a:10, b:12}); -assert(db.getCollection( "test_db" ).getIndexes().length == 3,22); +assert.eq(3, db.getCollection( "test_db" ).getIndexes().length, "22"); db.getCollection( "test_db" ).dropIndexes(); -assert(db.getCollection( "test_db" ).getIndexes().length == 1,23); +assert.eq(1, db.getCollection( "test_db" ).getIndexes().length, "23"); db.getCollection( "test_db" ).find(); db.getCollection( "test_db" ).drop(); -assert(db.getCollection( "test_db" ).getIndexes().length == 0,24); +assert.eq(0, db.getCollection( "test_db" ).getIndexes().length, "24"); /* * stats() diff --git a/jstests/core/bulk_api_ordered.js b/jstests/core/bulk_api_ordered.js index beef9d24411..6cc032f68fc 100644 --- a/jstests/core/bulk_api_ordered.js +++ b/jstests/core/bulk_api_ordered.js @@ -124,7 +124,7 @@ var executeTests = function() { // Basic properties check assert.eq(1, result.nInserted); assert.eq(true, result.hasWriteErrors()); - assert(1, result.getWriteErrorCount()); + assert.eq(1, result.getWriteErrorCount()); // Individual error checking var error = result.getWriteErrorAt(0); diff --git a/jstests/core/bulk_api_unordered.js b/jstests/core/bulk_api_unordered.js index 1c8607dec37..64893fe011a 100644 --- a/jstests/core/bulk_api_unordered.js +++ b/jstests/core/bulk_api_unordered.js @@ -50,7 +50,8 @@ var executeTests = function() { assert.eq(4, result.nModified); } assert.eq(2, result.nRemoved); - assert(1, result.getWriteErrorCount()); + assert.eq(false, result.hasWriteErrors()); + assert.eq(0, result.getWriteErrorCount()); var upserts = result.getUpsertedIds(); assert.eq(2, upserts.length); assert.eq(3, upserts[0].index); @@ -88,7 +89,7 @@ var executeTests = function() { // Basic properties check assert.eq(2, result.nInserted); assert.eq(true, result.hasWriteErrors()); - assert(1, result.getWriteErrorCount()); + assert.eq(1, result.getWriteErrorCount()); // Get the first error var error = result.getWriteErrorAt(0); @@ -123,7 +124,7 @@ var executeTests = function() { assert.eq(2, result.nInserted); assert.eq(1, result.nUpserted); assert.eq(true, result.hasWriteErrors()); - assert(3, result.getWriteErrorCount()); + assert.eq(3, result.getWriteErrorCount()); // Individual error checking var error = result.getWriteErrorAt(0); diff --git a/jstests/core/distinct4.js b/jstests/core/distinct4.js index 5e4a1fd85a2..1fa2763bf40 100644 --- a/jstests/core/distinct4.js +++ b/jstests/core/distinct4.js @@ -15,7 +15,7 @@ //first argument should be a string or error // from shell helper - assert.throws( t.distinct, [{a:1}] ); + assert.throws( function () { t.distinct({a:1}); } ); // from command interface assert.commandFailedWithCode(t.runCommand("distinct", {"key": {a: 1}}), @@ -25,7 +25,7 @@ //second argument should be a document or error // from shell helper - assert.throws( t.distinct, ['a', '1'] ); + assert.throws( function () { t.distinct('a', '1'); } ); // from command interface assert.commandFailedWithCode(t.runCommand("distinct", {"key": "a", "query": "a"}), @@ -34,8 +34,17 @@ // empty query clause should not cause error - assert( t.runCommand( "distinct", { "key" : "a" } ) ); - assert( t.distinct, ['a'] ); + // from shell helper + var a = assert.doesNotThrow( function () { return t.distinct('a'); } ); + // [ null, 1, 2, 3 ] + assert.eq(4, a.length, tojson(a)); + assert.contains(null, a); + assert.contains(1, a); + assert.contains(2, a); + assert.contains(3, a); + + // from command interface + assert.commandWorked( t.runCommand( "distinct", { "key" : "a" } ) ); })(); diff --git a/jstests/core/finda.js b/jstests/core/finda.js index 05d87c40972..4017ce91ad5 100644 --- a/jstests/core/finda.js +++ b/jstests/core/finda.js @@ -13,15 +13,15 @@ function clearQueryPlanCache() { function assertAllFound( matches ) { // printjson( matches ); found = new Array( numDocs ); - for( i = 0; i < numDocs; ++i ) { + for( var i = 0; i < numDocs; ++i ) { found[ i ] = false; } - for( i in matches ) { + for( var i in matches ) { m = matches[ i ]; found[ m._id ] = true; } - for( i = 0; i < numDocs; ++i ) { - assert( found[ i ], i ); + for( var i = 0; i < numDocs; ++i ) { + assert( found[ i ], i.toString() ); } } @@ -66,7 +66,7 @@ function checkCursorWithBatchSize( query, sort, batchSize, expectedLeftInBatch ) if ( expectedLeftInBatch > 110 ) { cursor = makeCursor( query, {}, sort, batchSize, true ); lastNonAIndexResult = -1; - for( i = 0; i < expectedLeftInBatch; ++i ) { + for( var i = 0; i < expectedLeftInBatch; ++i ) { next = cursor.next(); // Identify the query plan used by checking the fields of a returnKey query. if ( !friendlyEqual( [ 'a', '_id' ], Object.keySet( next ) ) ) { @@ -80,7 +80,7 @@ function checkCursorWithBatchSize( query, sort, batchSize, expectedLeftInBatch ) function queryWithPlanTypes( withDups ) { t.drop(); - for( i = 1; i < numDocs; ++i ) { + for( var i = 1; i < numDocs; ++i ) { t.save( { _id:i, a:i, b:0 } ); } if ( withDups ) { diff --git a/jstests/core/or1.js b/jstests/core/or1.js index 66bbd2e6eea..0153afe9115 100644 --- a/jstests/core/or1.js +++ b/jstests/core/or1.js @@ -1,14 +1,14 @@ t = db.jstests_or1; t.drop(); -checkArrs = function( a, b, m ) { - assert.eq( a.length, b.length, m ); +checkArrs = function( a, b ) { + assert.eq( a.length, b.length ); aStr = []; bStr = []; a.forEach( function( x ) { aStr.push( tojson( x ) ); } ); b.forEach( function( x ) { bStr.push( tojson( x ) ); } ); for ( i = 0; i < aStr.length; ++i ) { - assert( -1 != bStr.indexOf( aStr[ i ] ), m ); + assert.neq( -1, bStr.indexOf( aStr[ i ] ) ); } } @@ -54,4 +54,4 @@ doTest(); t.drop(); t.ensureIndex( {a:1,b:1} ); -doTest();
\ No newline at end of file +doTest(); diff --git a/jstests/core/or2.js b/jstests/core/or2.js index f8de6c42ef9..cbbf51c0eaf 100644 --- a/jstests/core/or2.js +++ b/jstests/core/or2.js @@ -4,14 +4,14 @@ t.drop(); // Include helpers for analyzing explain output. load("jstests/libs/analyze_plan.js"); -checkArrs = function( a, b, m ) { - assert.eq( a.length, b.length, m ); +checkArrs = function( a, b ) { + assert.eq( a.length, b.length ); aStr = []; bStr = []; a.forEach( function( x ) { aStr.push( tojson( x ) ); } ); b.forEach( function( x ) { bStr.push( tojson( x ) ); } ); for ( i = 0; i < aStr.length; ++i ) { - assert( -1 != bStr.indexOf( aStr[ i ] ), m ); + assert.neq( -1, bStr.indexOf( aStr[ i ] ) ); } } diff --git a/jstests/core/or3.js b/jstests/core/or3.js index 1dab4d55ecd..293a3f37164 100644 --- a/jstests/core/or3.js +++ b/jstests/core/or3.js @@ -4,14 +4,14 @@ t.drop(); // Include helpers for analyzing explain output. load("jstests/libs/analyze_plan.js"); -checkArrs = function( a, b, m ) { - assert.eq( a.length, b.length, m ); +checkArrs = function( a, b ) { + assert.eq( a.length, b.length ); aStr = []; bStr = []; a.forEach( function( x ) { aStr.push( tojson( x ) ); } ); b.forEach( function( x ) { bStr.push( tojson( x ) ); } ); for ( i = 0; i < aStr.length; ++i ) { - assert( -1 != bStr.indexOf( aStr[ i ] ), m ); + assert.neq( -1, bStr.indexOf( aStr[ i ] ) ); } } diff --git a/jstests/core/uniqueness.js b/jstests/core/uniqueness.js index 9cde613bbe4..124748a91f4 100644 --- a/jstests/core/uniqueness.js +++ b/jstests/core/uniqueness.js @@ -12,7 +12,7 @@ assert.writeOK( res ); // this should yield an error res = t.insert( { _id : 3 } ); assert.writeError( res ); -assert( t.count() == 1, "hmmm"); +assert.eq( 1, t.count() ); res = t.insert( { _id : 4, x : 99 } ); assert.writeOK( res ); @@ -20,13 +20,13 @@ assert.writeOK( res ); // this should yield an error res = t.update( { _id : 4 } , { _id : 3, x : 99 } ); assert.writeError( res ); -assert( t.findOne( {_id:4} ), 5 ); +assert( t.findOne( {_id:4} ) ); // Check for an error message when we index and there are dups db.jstests_uniqueness2.drop(); db.jstests_uniqueness2.insert({a:3}); db.jstests_uniqueness2.insert({a:3}); -assert( db.jstests_uniqueness2.count() == 2 , 6) ; +assert.eq( 2, db.jstests_uniqueness2.count() ); res = db.jstests_uniqueness2.ensureIndex({a:1}, true); assert.commandFailed( res ); assert( res.errmsg.match( /E11000/ ) ); @@ -35,7 +35,7 @@ assert( res.errmsg.match( /E11000/ ) ); db.jstests_uniqueness2.drop(); db.jstests_uniqueness2.insert({a:3}); db.jstests_uniqueness2.insert({a:3}); -assert( db.jstests_uniqueness2.count() == 2 , 6) ; +assert.eq( 2, db.jstests_uniqueness2.count() ); res = db.jstests_uniqueness2.ensureIndex({a:1}, {unique:true,background:true}); assert.commandFailed( res ); assert( res.errmsg.match( /E11000/ ) ); @@ -46,11 +46,11 @@ assert( res.errmsg.match( /E11000/ ) ); t.drop(); t.save( { _id : 'Z' } ); t.update( {}, { k : 2 } ); -assert( t.findOne()._id == 'Z', "uniqueness.js problem with adding back _id" ); +assert.eq( 'Z', t.findOne()._id, "uniqueness.js problem with adding back _id" ); /* - test when doesn't grow */ t.drop(); t.save( { _id : 'Z', k : 3 } ); t.update( {}, { k : 2 } ); -assert( t.findOne()._id == 'Z', "uniqueness.js problem with adding back _id (2)" ); +assert.eq( 'Z', t.findOne()._id, "uniqueness.js problem with adding back _id (2)" ); diff --git a/jstests/mmap_v1/index_check1.js b/jstests/mmap_v1/index_check1.js index 7113dff0877..7f46e477d98 100644 --- a/jstests/mmap_v1/index_check1.js +++ b/jstests/mmap_v1/index_check1.js @@ -1,31 +1,31 @@ db.somecollection.drop(); -assert(db.system.namespaces.find({name:/somecollection/}).length() == 0, 1); +assert.eq(0, db.system.namespaces.find({name:/somecollection/}).length(), "1"); db.somecollection.save({a:1}); -assert(db.system.namespaces.find({name:/somecollection/}).length() == 2, 2); +assert.eq(2, db.system.namespaces.find({name:/somecollection/}).length(), "2"); db.somecollection.ensureIndex({a:1}); var z = db.system.namespaces.find({name:/somecollection/}).length(); -assert( z >= 1 , 3 ); +assert.gte(z, 1 , "3"); if( z == 1 ) print("warning: z==1, should only happen with alternate storage engines"); db.somecollection.drop(); -assert(db.system.namespaces.find({name:/somecollection/}).length() == 0, 4); +assert.eq(0, db.system.namespaces.find({name:/somecollection/}).length(), "4"); db.somecollection.save({a:1}); -assert(db.system.namespaces.find({name:/somecollection/}).length() == 2, 5); +assert.eq(2, db.system.namespaces.find({name:/somecollection/}).length(), "5"); db.somecollection.ensureIndex({a:1}); var x = db.system.namespaces.find({name:/somecollection/}).length(); -assert( x == 2 || x == z, 6); +assert( x == 2 || x == z, "6"); -assert(db.somecollection.validate().valid, 7); +assert(db.somecollection.validate().valid, "7"); diff --git a/jstests/multiVersion/libs/verify_collection_data.js b/jstests/multiVersion/libs/verify_collection_data.js index 72c3e01dac7..f8d10700cec 100644 --- a/jstests/multiVersion/libs/verify_collection_data.js +++ b/jstests/multiVersion/libs/verify_collection_data.js @@ -40,7 +40,7 @@ createCollectionWithData = function (db, collectionName, dataGenerator) { JSON.stringify(nextIndex.options) + ");"); var ensureIndexResult = collection.ensureIndex(nextIndex.spec, nextIndex.options); // XXX: Is this the real way to check for errors? - assert(ensureIndexResult === undefined, JSON.stringify(ensureIndexResult)); + assert(ensureIndexResult === undefined, tojson(ensureIndexResult)); numIndexes++; } diff --git a/jstests/noPassthrough/backup_restore.js b/jstests/noPassthrough/backup_restore.js index 7f2cbbc262e..ad5eff90000 100644 --- a/jstests/noPassthrough/backup_restore.js +++ b/jstests/noPassthrough/backup_restore.js @@ -264,8 +264,8 @@ // Note the dbhash can only run when the DB is inactive to get a result // that can be compared, which is only in the fsyncLock/fsynUnlock case if (dbHash !== undefined) { - assert(dbHash, rst.nodes[numNodes].getDB(crudDb).runCommand({dbhash: 1}).md5, - testName + ' dbHash'); + assert.eq(dbHash, rst.nodes[numNodes].getDB(crudDb).runCommand({dbhash: 1}).md5, + testName + ' dbHash'); } // Add new hidden secondary to replica set diff --git a/jstests/sharding/diffservers1.js b/jstests/sharding/diffservers1.js index b485d508b43..1ddff72d5d0 100644 --- a/jstests/sharding/diffservers1.js +++ b/jstests/sharding/diffservers1.js @@ -10,7 +10,7 @@ test1 = s.getDB( "test1" ).foo; test1.save( { a : 1 } ); test1.save( { a : 2 } ); test1.save( { a : 3 } ); -assert( 3 , test1.count() ); +assert.eq( 3 , test1.count() ); assert( ! s.admin.runCommand( { addshard: "sdd$%" } ).ok , "bad hostname" ); diff --git a/jstests/sharding/movePrimary1.js b/jstests/sharding/movePrimary1.js index 50cc1ccacca..242f58e5bcb 100644 --- a/jstests/sharding/movePrimary1.js +++ b/jstests/sharding/movePrimary1.js @@ -8,7 +8,7 @@ initDB = function( name ){ c.save( { a : 1 } ); c.save( { a : 2 } ); c.save( { a : 3 } ); - assert( 3 , c.count() ); + assert.eq( 3 , c.count() ); return s.getServer( name ); } diff --git a/jstests/sharding/sync_cluster_config/sync7.js b/jstests/sharding/sync_cluster_config/sync7.js index 25e95fdafc3..e8b036fca7a 100644 --- a/jstests/sharding/sync_cluster_config/sync7.js +++ b/jstests/sharding/sync_cluster_config/sync7.js @@ -25,7 +25,7 @@ initDB = function(name) { c.save( { a : 1 } ); c.save( { a : 2 } ); c.save( { a : 3 } ); - assert( 3, c.count() ); + assert.eq( 3, c.count() ); return s.getServer( name ); } diff --git a/src/mongo/shell/assert.js b/src/mongo/shell/assert.js index 2d207a021bd..ba3bd5133de 100644 --- a/src/mongo/shell/assert.js +++ b/src/mongo/shell/assert.js @@ -19,6 +19,12 @@ doassert = function(msg, obj) { } assert = function(b, msg){ + if (arguments.length > 2) { + doassert("Too many parameters to assert()."); + } + if (arguments.length > 1 && typeof(msg) !== "string") { + doassert("Non-string 'msg' parameters are invalid for assert()."); + } if (assert._debug && msg) print("in assert for: " + msg); if (b) return; @@ -234,13 +240,14 @@ assert.doesNotThrow = function(func, params, msg) { if (params && typeof(params) == "string") { throw ("2nd argument to assert.throws has to be an array, not " + params); } + var res; try { - func.apply(null, params); + res = func.apply(null, params); } catch (e) { doassert("threw unexpected exception: " + e + " : " + msg); } - return; + return res; }; assert.throws.automsg = function(func, params) { |