diff options
Diffstat (limited to 'dbtests/jstests.cpp')
-rw-r--r-- | dbtests/jstests.cpp | 96 |
1 files changed, 82 insertions, 14 deletions
diff --git a/dbtests/jstests.cpp b/dbtests/jstests.cpp index 454dcdc..81b9673 100644 --- a/dbtests/jstests.cpp +++ b/dbtests/jstests.cpp @@ -17,10 +17,10 @@ * along with this program. If not, see <http://www.gnu.org/licenses/>. */ -#include "stdafx.h" +#include "pch.h" #include "../db/instance.h" -#include "../stdafx.h" +#include "../pch.h" #include "../scripting/engine.h" #include "dbtests.h" @@ -531,12 +531,17 @@ namespace JSTests { ASSERT( s->exec( "printjson( a ); b = {b:a.a}", "foo", false, true, false ) ); out = s->getObject( "b" ); ASSERT_EQUALS( mongo::NumberLong, out.firstElement().type() ); - ASSERT_EQUALS( val, out.firstElement().numberLong() ); + if( val != out.firstElement().numberLong() ) { + cout << val << endl; + cout << out.firstElement().numberLong() << endl; + cout << out.toString() << endl; + ASSERT_EQUALS( val, out.firstElement().numberLong() ); + } ASSERT( s->exec( "c = {c:a.a.toString()}", "foo", false, true, false ) ); out = s->getObject( "c" ); stringstream ss; - ss << val; + ss << "NumberLong( \"" << val << "\" )"; ASSERT_EQUALS( ss.str(), out.firstElement().valuestr() ); ASSERT( s->exec( "d = {d:a.a.toNumber()}", "foo", false, true, false ) ); @@ -627,7 +632,7 @@ namespace JSTests { private: void check( const BSONObj &one, const BSONObj &two ) { if ( one.woCompare( two ) != 0 ) { - static string fail = string( "Assertion failure expected " ) + string( one ) + ", got " + string( two ); + static string fail = string( "Assertion failure expected " ) + one.toString() + ", got " + two.toString(); FAIL( fail.c_str() ); } } @@ -652,6 +657,37 @@ namespace JSTests { } static const char *ns() { return "unittest.jstests.longutf8string"; } }; + + class InvalidUTF8Check { + public: + void run(){ + if( !globalScriptEngine->utf8Ok() ) + return; + + auto_ptr<Scope> s; + s.reset( globalScriptEngine->newScope() ); + + BSONObj b; + { + char crap[5]; + + crap[0] = (char) 128; + crap[1] = 17; + crap[2] = (char) 128; + crap[3] = 17; + crap[4] = 0; + + BSONObjBuilder bb; + bb.append( "x" , crap ); + b = bb.obj(); + } + + //cout << "ELIOT: " << b.jsonString() << endl; + s->setThis( &b ); + // its ok if this is handled by js, just can't create a c++ exception + s->invoke( "x=this.x.length;" , BSONObj() ); + } + }; class CodeTests { public: @@ -701,7 +737,8 @@ namespace JSTests { { BSONObj fromA = client.findOne( _a , BSONObj() ); - cout << "Froma : " << fromA << endl; + assert( fromA.valid() ); + //cout << "Froma : " << fromA << endl; BSONObjBuilder b; b.append( "b" , 18 ); b.appendDBRef( "c" , "dbref.a" , fromA["_id"].__oid() ); @@ -771,7 +808,7 @@ namespace JSTests { { BSONObjBuilder b; b.append( "a" , 7 ); - b.appendBinData( "b" , 12 , ByteArray , foo ); + b.appendBinData( "b" , 12 , BinDataGeneral , foo ); in = b.obj(); s->setObject( "x" , in ); } @@ -788,11 +825,11 @@ namespace JSTests { // check that BinData js class is utilized s->invokeSafe( "q = x.b.toString();", BSONObj() ); stringstream expected; - expected << "BinData( type: " << ByteArray << ", base64: \"" << base64 << "\" )"; + expected << "BinData(" << BinDataGeneral << ",\"" << base64 << "\")"; ASSERT_EQUALS( expected.str(), s->getString( "q" ) ); stringstream scriptBuilder; - scriptBuilder << "z = { c : new BinData( " << ByteArray << ", \"" << base64 << "\" ) };"; + scriptBuilder << "z = { c : new BinData( " << BinDataGeneral << ", \"" << base64 << "\" ) };"; string script = scriptBuilder.str(); s->invokeSafe( script.c_str(), BSONObj() ); out = s->getObject( "z" ); @@ -842,7 +879,34 @@ namespace JSTests { s->invoke( f , empty ); ASSERT_EQUALS( 11 , s->getNumber( "return" ) ); } - cout << "speed1: " << ( n / t.millis() ) << " ops/ms" << endl; + //cout << "speed1: " << ( n / t.millis() ) << " ops/ms" << endl; + } + }; + + class ScopeOut { + public: + void run(){ + auto_ptr<Scope> s; + s.reset( globalScriptEngine->newScope() ); + + s->invokeSafe( "x = 5;" , BSONObj() ); + { + BSONObjBuilder b; + s->append( b , "z" , "x" ); + ASSERT_EQUALS( BSON( "z" << 5 ) , b.obj() ); + } + + s->invokeSafe( "x = function(){ return 17; }" , BSONObj() ); + BSONObj temp; + { + BSONObjBuilder b; + s->append( b , "z" , "x" ); + temp = b.obj(); + s->setThis( &temp ); + } + + s->invokeSafe( "foo = this.z();" , BSONObj() ); + ASSERT_EQUALS( 17 , s->getNumber( "foo" ) ); } }; @@ -857,7 +921,7 @@ namespace JSTests { add< ResetScope >(); add< FalseTests >(); add< SimpleFunctions >(); - + add< ObjectMapping >(); add< ObjectDecoding >(); add< JSOIDTests >(); @@ -867,10 +931,8 @@ namespace JSTests { add< SpecialDBTypes >(); add< TypeConservation >(); add< NumberLong >(); - + add< WeirdObjects >(); - add< Utf8Check >(); - add< LongUtf8String >(); add< CodeTests >(); add< DBRefTest >(); add< InformalDBRef >(); @@ -879,6 +941,12 @@ namespace JSTests { add< VarTests >(); add< Speed1 >(); + + add< InvalidUTF8Check >(); + add< Utf8Check >(); + add< LongUtf8String >(); + + add< ScopeOut >(); } } myall; |