void run(){ #ifdef MOZJS Scope * s = globalScriptEngine->createScope(); s->localConnect( "blah" ); s->invoke( "z = { _id : new ObjectId() , a : 123 };" , BSONObj() ); BSONObj out = s->getObject( "z" ); ASSERT_EQUALS( 123 , out["a"].number() ); ASSERT_EQUALS( jstOID , out["_id"].type() ); OID save = out["_id"].__oid(); s->setObject( "a" , out ); s->invoke( "y = { _id : a._id , a : 124 };" , BSONObj() ); out = s->getObject( "y" ); ASSERT_EQUALS( 124 , out["a"].number() ); ASSERT_EQUALS( jstOID , out["_id"].type() ); ASSERT_EQUALS( out["_id"].__oid().str() , save.str() ); s->invoke( "y = { _id : new ObjectId( a._id ) , a : 125 };" , BSONObj() ); out = s->getObject( "y" ); ASSERT_EQUALS( 125 , out["a"].number() ); ASSERT_EQUALS( jstOID , out["_id"].type() ); ASSERT_EQUALS( out["_id"].__oid().str() , save.str() ); delete s; #endif }
void run() { Scope * s = globalScriptEngine->newScope(); s->localConnect( "blah" ); BSONObjBuilder b; long long val = (long long)( 0xbabadeadbeefbaddULL ); b.append( "a", val ); BSONObj in = b.obj(); s->setObject( "a", in ); BSONObj out = s->getObject( "a" ); ASSERT_EQUALS( mongo::NumberLong, out.firstElement().type() ); ASSERT( s->exec( "printjson( a ); b = {b:a.a}", "foo", false, true, false ) ); out = s->getObject( "b" ); ASSERT_EQUALS( mongo::NumberLong, out.firstElement().type() ); 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 << "NumberLong(\"" << val << "\")"; ASSERT_EQUALS( ss.str(), out.firstElement().valuestr() ); ASSERT( s->exec( "d = {d:a.a.toNumber()}", "foo", false, true, false ) ); out = s->getObject( "d" ); ASSERT_EQUALS( NumberDouble, out.firstElement().type() ); ASSERT_EQUALS( double( val ), out.firstElement().number() ); ASSERT( s->exec( "e = {e:a.a.floatApprox}", "foo", false, true, false ) ); out = s->getObject( "e" ); ASSERT_EQUALS( NumberDouble, out.firstElement().type() ); ASSERT_EQUALS( double( val ), out.firstElement().number() ); ASSERT( s->exec( "f = {f:a.a.top}", "foo", false, true, false ) ); out = s->getObject( "f" ); ASSERT( NumberDouble == out.firstElement().type() || NumberInt == out.firstElement().type() ); s->setObject( "z", BSON( "z" << (long long)( 4 ) ) ); ASSERT( s->exec( "y = {y:z.z.top}", "foo", false, true, false ) ); out = s->getObject( "y" ); ASSERT_EQUALS( Undefined, out.firstElement().type() ); ASSERT( s->exec( "x = {x:z.z.floatApprox}", "foo", false, true, false ) ); out = s->getObject( "x" ); ASSERT( NumberDouble == out.firstElement().type() || NumberInt == out.firstElement().type() ); ASSERT_EQUALS( double( 4 ), out.firstElement().number() ); ASSERT( s->exec( "w = {w:z.z}", "foo", false, true, false ) ); out = s->getObject( "w" ); ASSERT_EQUALS( mongo::NumberLong, out.firstElement().type() ); ASSERT_EQUALS( 4, out.firstElement().numberLong() ); }
void run(){ Scope * s = globalScriptEngine->createScope(); { // date BSONObj o; { BSONObjBuilder b; b.appendDate( "d" , 123456789 ); o = b.obj(); } s->setObject( "x" , o ); s->invoke( "return x.d.getTime() != 12;" , BSONObj() ); ASSERT_EQUALS( true, s->getBoolean( "return" ) ); s->invoke( "z = x.d.getTime();" , BSONObj() ); ASSERT_EQUALS( 123456789 , s->getNumber( "z" ) ); s->invoke( "z = { z : x.d }" , BSONObj() ); BSONObj out = s->getObject( "z" ); ASSERT( out["z"].type() == Date ); } { // regex BSONObj o; { BSONObjBuilder b; b.appendRegex( "r" , "^a" , "i" ); o = b.obj(); } s->setObject( "x" , o ); s->invoke( "z = x.r.test( 'b' );" , BSONObj() ); ASSERT_EQUALS( false , s->getBoolean( "z" ) ); s->invoke( "z = x.r.test( 'a' );" , BSONObj() ); ASSERT_EQUALS( true , s->getBoolean( "z" ) ); s->invoke( "z = x.r.test( 'ba' );" , BSONObj() ); ASSERT_EQUALS( false , s->getBoolean( "z" ) ); s->invoke( "z = { a : x.r };" , BSONObj() ); BSONObj out = s->getObject("z"); ASSERT_EQUALS( (string)"^a" , out["a"].regex() ); ASSERT_EQUALS( (string)"i" , out["a"].regexFlags() ); } delete s; }
void run(){ Scope * s = globalScriptEngine->createScope(); s->localConnect( "asd" ); const char * foo = "asdasdasdasd"; BSONObj in; { BSONObjBuilder b; b.append( "a" , 7 ); b.appendBinData( "b" , strlen( foo ) , ByteArray , foo ); in = b.obj(); s->setObject( "x" , in ); } s->invokeSafe( "myb = x.b; print( myb ); printjson( myb );" , BSONObj() ); s->invokeSafe( "y = { c : myb };" , BSONObj() ); BSONObj out = s->getObject( "y" ); ASSERT_EQUALS( BinData , out["c"].type() ); //blah( "in " , in["b"] ); //blah( "out" , out["c"] ); ASSERT_EQUALS( 0 , in["b"].woCompare( out["c"] , false ) ); delete s; }
void run(){ Scope * s = globalScriptEngine->createScope(); { BSONObjBuilder b; b.append( "a" , 1 ); b.appendCode( "b" , "function(){ out.b = 11; }" ); b.appendCodeWScope( "c" , "function(){ out.c = 12; }" , BSONObj() ); b.appendCodeWScope( "d" , "function(){ out.d = 13 + bleh; }" , BSON( "bleh" << 5 ) ); s->setObject( "foo" , b.obj() ); } s->invokeSafe( "out = {}; out.a = foo.a; foo.b(); foo.c();" , BSONObj() ); BSONObj out = s->getObject( "out" ); ASSERT_EQUALS( 1 , out["a"].number() ); ASSERT_EQUALS( 11 , out["b"].number() ); ASSERT_EQUALS( 12 , out["c"].number() ); //s->invokeSafe( "foo.d() " , BSONObj() ); //out = s->getObject( "out" ); //ASSERT_EQUALS( 18 , out["d"].number() ); delete s; }
void run(){ Scope * s = globalScriptEngine->createScope(); BSONObjBuilder b; b.appendTimestamp( "a" , 123456789 ); b.appendMinKey( "b" ); b.appendMaxKey( "c" ); b.appendTimestamp( "d" , 1234000 , 9876 ); { BSONObj t = b.done(); ASSERT_EQUALS( 1234000U , t["d"].timestampTime() ); ASSERT_EQUALS( 9876U , t["d"].timestampInc() ); } s->setObject( "z" , b.obj() ); ASSERT( s->invoke( "y = { a : z.a , b : z.b , c : z.c , d: z.d }" , BSONObj() ) == 0 ); BSONObj out = s->getObject( "y" ); ASSERT_EQUALS( Timestamp , out["a"].type() ); ASSERT_EQUALS( MinKey , out["b"].type() ); ASSERT_EQUALS( MaxKey , out["c"].type() ); ASSERT_EQUALS( Timestamp , out["d"].type() ); ASSERT_EQUALS( 9876U , out["d"].timestampInc() ); ASSERT_EQUALS( 1234000U , out["d"].timestampTime() ); ASSERT_EQUALS( 123456789U , out["a"].date() ); delete s; }
void run(){ Scope * s = globalScriptEngine->newScope(); s->localConnect( "asd" ); const char * foo = "asdas\0asdasd"; const char * base64 = "YXNkYXMAYXNkYXNk"; BSONObj in; { BSONObjBuilder b; b.append( "a" , 7 ); b.appendBinData( "b" , 12 , BinDataGeneral , foo ); in = b.obj(); s->setObject( "x" , in ); } s->invokeSafe( "myb = x.b; print( myb ); printjson( myb );" , BSONObj() ); s->invokeSafe( "y = { c : myb };" , BSONObj() ); BSONObj out = s->getObject( "y" ); ASSERT_EQUALS( BinData , out["c"].type() ); // pp( "in " , in["b"] ); // pp( "out" , out["c"] ); ASSERT_EQUALS( 0 , in["b"].woCompare( out["c"] , false ) ); // check that BinData js class is utilized s->invokeSafe( "q = x.b.toString();", BSONObj() ); stringstream expected; expected << "BinData(" << BinDataGeneral << ",\"" << base64 << "\")"; ASSERT_EQUALS( expected.str(), s->getString( "q" ) ); stringstream scriptBuilder; scriptBuilder << "z = { c : new BinData( " << BinDataGeneral << ", \"" << base64 << "\" ) };"; string script = scriptBuilder.str(); s->invokeSafe( script.c_str(), BSONObj() ); out = s->getObject( "z" ); // pp( "out" , out["c"] ); ASSERT_EQUALS( 0 , in["b"].woCompare( out["c"] , false ) ); s->invokeSafe( "a = { f: new BinData( 128, \"\" ) };", BSONObj() ); out = s->getObject( "a" ); int len = -1; out[ "f" ].binData( len ); ASSERT_EQUALS( 0, len ); ASSERT_EQUALS( 128, out[ "f" ].binDataType() ); delete s; }
void run() { Scope *s = globalScriptEngine->newScope(); BSONObj o = BSON( "foo" << "bar" ); s->setObject( "a.b", o ); ASSERT( s->getObject( "a" ).isEmpty() ); BSONObj o2 = BSONObj(); s->setObject( "a", o2 ); s->setObject( "a.b", o ); ASSERT( s->getObject( "a" ).isEmpty() ); o2 = fromjson( "{b:{}}" ); s->setObject( "a", o2 ); s->setObject( "a.b", o ); ASSERT( !s->getObject( "a" ).isEmpty() ); }
void run(){ Scope * s = globalScriptEngine->createScope(); s->invoke( "z = { num : 1 };" , BSONObj() ); BSONObj out = s->getObject( "z" ); ASSERT_EQUALS( 1 , out["num"].number() ); ASSERT_EQUALS( 1 , out.nFields() ); s->invoke( "z = { x : 'eliot' };" , BSONObj() ); out = s->getObject( "z" ); ASSERT_EQUALS( (string)"eliot" , out["x"].valuestr() ); ASSERT_EQUALS( 1 , out.nFields() ); BSONObj o = BSON( "x" << 17 ); s->setObject( "blah" , o ); out = s->getObject( "blah" ); ASSERT_EQUALS( 17 , out["x"].number() ); delete s; }
void run(){ Scope * s = globalScriptEngine->createScope(); BSONObj o = BSON( "x" << 17 << "y" << "eliot" << "z" << "sara" ); s->setObject( "blah" , o , true ); s->invoke( "blah.a = 19;" , BSONObj() ); BSONObj out = s->getObject( "blah" ); ASSERT( out["a"].eoo() ); delete s; }
void run(){ Scope * s = globalScriptEngine->newScope(); BSONObj o = BSON( "x" << 17 << "y" << "eliot" << "z" << "sara" << "zz" << BSONObj() ); s->setObject( "blah" , o , true ); s->invoke( "blah.y = 'e'", BSONObj() ); BSONObj out = s->getObject( "blah" ); ASSERT( strlen( out["y"].valuestr() ) > 1 ); s->invoke( "blah.a = 19;" , BSONObj() ); out = s->getObject( "blah" ); ASSERT( out["a"].eoo() ); s->invoke( "blah.zz.a = 19;" , BSONObj() ); out = s->getObject( "blah" ); ASSERT( out["zz"].embeddedObject()["a"].eoo() ); s->setObject( "blah.zz", BSON( "a" << 19 ) ); out = s->getObject( "blah" ); ASSERT( out["zz"].embeddedObject()["a"].eoo() ); s->invoke( "delete blah['x']" , BSONObj() ); out = s->getObject( "blah" ); ASSERT( !out["x"].eoo() ); // read-only object itself can be overwritten s->invoke( "blah = {}", BSONObj() ); out = s->getObject( "blah" ); ASSERT( out.isEmpty() ); // test array - can't implement this in v8 // o = fromjson( "{a:[1,2,3]}" ); // s->setObject( "blah", o, true ); // out = s->getObject( "blah" ); // s->invoke( "blah.a[ 0 ] = 4;", BSONObj() ); // s->invoke( "delete blah['a'][ 2 ];", BSONObj() ); // out = s->getObject( "blah" ); // ASSERT_EQUALS( 1.0, out[ "a" ].embeddedObject()[ 0 ].number() ); // ASSERT_EQUALS( 3.0, out[ "a" ].embeddedObject()[ 2 ].number() ); delete s; }
void run(){ Scope * s = globalScriptEngine->createScope(); // -- A -- BSONObj o; { BSONObjBuilder b ; b.append( "a" , (int)5 ); b.append( "b" , 5.6 ); o = b.obj(); } ASSERT_EQUALS( NumberInt , o["a"].type() ); ASSERT_EQUALS( NumberDouble , o["b"].type() ); s->setObject( "z" , o ); s->invoke( "return z" , BSONObj() ); BSONObj out = s->getObject( "return" ); ASSERT_EQUALS( 5 , out["a"].number() ); ASSERT_EQUALS( 5.6 , out["b"].number() ); ASSERT_EQUALS( NumberDouble , out["b"].type() ); ASSERT_EQUALS( NumberInt , out["a"].type() ); // -- B -- { BSONObjBuilder b ; b.append( "a" , (int)5 ); b.append( "b" , 5.6 ); o = b.obj(); } s->setObject( "z" , o , false ); s->invoke( "return z" , BSONObj() ); out = s->getObject( "return" ); ASSERT_EQUALS( 5 , out["a"].number() ); ASSERT_EQUALS( 5.6 , out["b"].number() ); ASSERT_EQUALS( NumberDouble , out["b"].type() ); ASSERT_EQUALS( NumberInt , out["a"].type() ); // -- C -- { BSONObjBuilder b ; { BSONObjBuilder c; c.append( "0" , 5.5 ); c.append( "1" , 6 ); b.appendArray( "a" , c.obj() ); } o = b.obj(); } ASSERT_EQUALS( NumberDouble , o["a"].embeddedObjectUserCheck()["0"].type() ); ASSERT_EQUALS( NumberInt , o["a"].embeddedObjectUserCheck()["1"].type() ); s->setObject( "z" , o , false ); out = s->getObject( "z" ); ASSERT_EQUALS( NumberDouble , out["a"].embeddedObjectUserCheck()["0"].type() ); ASSERT_EQUALS( NumberInt , out["a"].embeddedObjectUserCheck()["1"].type() ); s->invokeSafe( "z.z = 5;" , BSONObj() ); out = s->getObject( "z" ); ASSERT_EQUALS( 5 , out["z"].number() ); ASSERT_EQUALS( NumberDouble , out["a"].embeddedObjectUserCheck()["0"].type() ); ASSERT_EQUALS( NumberDouble , out["a"].embeddedObjectUserCheck()["1"].type() ); // TODO: this is technically bad, but here to make sure that i understand the behavior delete s; }
void run(){ Scope * s = globalScriptEngine->newScope(); // -- A -- BSONObj o; { BSONObjBuilder b ; b.append( "a" , (int)5 ); b.append( "b" , 5.6 ); o = b.obj(); } ASSERT_EQUALS( NumberInt , o["a"].type() ); ASSERT_EQUALS( NumberDouble , o["b"].type() ); s->setObject( "z" , o ); s->invoke( "return z" , BSONObj() ); BSONObj out = s->getObject( "return" ); ASSERT_EQUALS( 5 , out["a"].number() ); ASSERT_EQUALS( 5.6 , out["b"].number() ); ASSERT_EQUALS( NumberDouble , out["b"].type() ); ASSERT_EQUALS( NumberInt , out["a"].type() ); // -- B -- { BSONObjBuilder b ; b.append( "a" , (int)5 ); b.append( "b" , 5.6 ); o = b.obj(); } s->setObject( "z" , o , false ); s->invoke( "return z" , BSONObj() ); out = s->getObject( "return" ); ASSERT_EQUALS( 5 , out["a"].number() ); ASSERT_EQUALS( 5.6 , out["b"].number() ); ASSERT_EQUALS( NumberDouble , out["b"].type() ); ASSERT_EQUALS( NumberInt , out["a"].type() ); // -- C -- { BSONObjBuilder b ; { BSONObjBuilder c; c.append( "0" , 5.5 ); c.append( "1" , 6 ); b.appendArray( "a" , c.obj() ); } o = b.obj(); } ASSERT_EQUALS( NumberDouble , o["a"].embeddedObjectUserCheck()["0"].type() ); ASSERT_EQUALS( NumberInt , o["a"].embeddedObjectUserCheck()["1"].type() ); s->setObject( "z" , o , false ); out = s->getObject( "z" ); ASSERT_EQUALS( NumberDouble , out["a"].embeddedObjectUserCheck()["0"].type() ); ASSERT_EQUALS( NumberInt , out["a"].embeddedObjectUserCheck()["1"].type() ); s->invokeSafe( "z.z = 5;" , BSONObj() ); out = s->getObject( "z" ); ASSERT_EQUALS( 5 , out["z"].number() ); ASSERT_EQUALS( NumberDouble , out["a"].embeddedObjectUserCheck()["0"].type() ); // Commenting so that v8 tests will work // ASSERT_EQUALS( NumberDouble , out["a"].embeddedObjectUserCheck()["1"].type() ); // TODO: this is technically bad, but here to make sure that i understand the behavior // Eliot says I don't have to worry about this case // // -- D -- // // o = fromjson( "{a:3.0,b:4.5}" ); // ASSERT_EQUALS( NumberDouble , o["a"].type() ); // ASSERT_EQUALS( NumberDouble , o["b"].type() ); // // s->setObject( "z" , o , false ); // s->invoke( "return z" , BSONObj() ); // out = s->getObject( "return" ); // ASSERT_EQUALS( 3 , out["a"].number() ); // ASSERT_EQUALS( 4.5 , out["b"].number() ); // // ASSERT_EQUALS( NumberDouble , out["b"].type() ); // ASSERT_EQUALS( NumberDouble , out["a"].type() ); // delete s; }