2009-04-23 20:28:22 +02:00
|
|
|
// javajstests.cpp
|
2009-02-03 06:14:14 +01:00
|
|
|
//
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Copyright (C) 2009 10gen Inc.
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License, version 3,
|
|
|
|
* as published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2009-04-23 20:28:22 +02:00
|
|
|
#include "../scripting/engine.h"
|
2009-02-03 06:14:14 +01:00
|
|
|
|
|
|
|
#include "dbtests.h"
|
|
|
|
|
2009-04-24 04:44:19 +02:00
|
|
|
namespace JSTests {
|
2009-02-03 06:14:14 +01:00
|
|
|
|
|
|
|
class Fundamental {
|
|
|
|
public:
|
|
|
|
void run() {
|
2009-02-03 17:50:30 +01:00
|
|
|
// By calling JavaJSImpl() inside run(), we ensure the unit test framework's
|
|
|
|
// signal handlers are pre-installed from JNI's perspective. This allows
|
|
|
|
// JNI to catch signals generated within the JVM and forward other signals
|
|
|
|
// as appropriate.
|
2009-04-23 20:28:22 +02:00
|
|
|
ScriptEngine::setup();
|
|
|
|
globalScriptEngine->runTest();
|
2009-02-03 06:14:14 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2009-04-24 04:44:19 +02:00
|
|
|
class BasicScope {
|
|
|
|
public:
|
|
|
|
void run(){
|
|
|
|
Scope * s = globalScriptEngine->createScope();
|
|
|
|
|
|
|
|
s->setNumber( "x" , 5 );
|
|
|
|
assert( 5 == s->getNumber( "x" ) );
|
|
|
|
|
|
|
|
s->setNumber( "x" , 1.67 );
|
|
|
|
assert( 1.67 == s->getNumber( "x" ) );
|
|
|
|
|
|
|
|
s->setString( "s" , "eliot was here" );
|
|
|
|
assert( "eliot was here" == s->getString( "s" ) );
|
|
|
|
|
|
|
|
s->setBoolean( "b" , true );
|
|
|
|
assert( s->getBoolean( "b" ) );
|
|
|
|
|
|
|
|
if ( 0 ){
|
|
|
|
s->setBoolean( "b" , false );
|
|
|
|
assert( ! s->getBoolean( "b" ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
delete s;
|
|
|
|
}
|
|
|
|
};
|
2009-04-24 05:52:47 +02:00
|
|
|
|
|
|
|
class FalseTests {
|
|
|
|
public:
|
|
|
|
void run(){
|
|
|
|
Scope * s = globalScriptEngine->createScope();
|
|
|
|
|
|
|
|
assert( ! s->getBoolean( "x" ) );
|
|
|
|
|
|
|
|
s->setString( "z" , "" );
|
|
|
|
assert( ! s->getBoolean( "z" ) );
|
|
|
|
|
|
|
|
|
|
|
|
delete s ;
|
|
|
|
}
|
|
|
|
};
|
2009-04-24 22:41:40 +02:00
|
|
|
|
|
|
|
class SimpleFunctions {
|
|
|
|
public:
|
|
|
|
void run(){
|
|
|
|
Scope * s = globalScriptEngine->createScope();
|
|
|
|
|
|
|
|
s->invoke( "x=5;" , BSONObj() );
|
|
|
|
assert( 5 == s->getNumber( "x" ) );
|
|
|
|
|
|
|
|
s->invoke( "return 17;" , BSONObj() );
|
|
|
|
assert( 17 == s->getNumber( "return" ) );
|
|
|
|
|
|
|
|
s->invoke( "function(){ return 17; }" , BSONObj() );
|
|
|
|
assert( 17 == s->getNumber( "return" ) );
|
|
|
|
|
|
|
|
s->setNumber( "x" , 1.76 );
|
|
|
|
s->invoke( "return x == 1.76; " , BSONObj() );
|
|
|
|
assert( s->getBoolean( "return" ) );
|
|
|
|
|
|
|
|
s->setNumber( "x" , 1.76 );
|
|
|
|
s->invoke( "return x == 1.79; " , BSONObj() );
|
|
|
|
assert( ! s->getBoolean( "return" ) );
|
|
|
|
|
|
|
|
delete s;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2009-04-24 23:19:24 +02:00
|
|
|
class ObjectMapping {
|
|
|
|
public:
|
|
|
|
void run(){
|
|
|
|
Scope * s = globalScriptEngine->createScope();
|
|
|
|
|
2009-04-29 17:12:35 +02:00
|
|
|
BSONObj o = BSON( "x" << 17 << "y" << "eliot" << "z" << "sara" );
|
2009-04-24 23:19:24 +02:00
|
|
|
s->setObject( "blah" , o );
|
2009-04-29 16:29:56 +02:00
|
|
|
|
2009-04-25 03:27:25 +02:00
|
|
|
s->invoke( "return blah.x;" , BSONObj() );
|
2009-04-29 16:16:39 +02:00
|
|
|
ASSERT_EQUALS( 17 , s->getNumber( "return" ) );
|
2009-04-29 16:29:56 +02:00
|
|
|
s->invoke( "return blah.y;" , BSONObj() );
|
|
|
|
ASSERT_EQUALS( "eliot" , s->getString( "return" ) );
|
2009-04-24 23:19:24 +02:00
|
|
|
|
2009-04-29 17:12:35 +02:00
|
|
|
s->setThis( & o );
|
|
|
|
s->invoke( "return this.z;" , BSONObj() );
|
|
|
|
ASSERT_EQUALS( "sara" , s->getString( "return" ) );
|
2009-04-29 21:45:11 +02:00
|
|
|
|
|
|
|
s->invoke( "return this.x == 17;" , BSONObj() );
|
|
|
|
ASSERT_EQUALS( true , s->getBoolean( "return" ) );
|
|
|
|
|
|
|
|
s->invoke( "return this.x == 18;" , BSONObj() );
|
|
|
|
ASSERT_EQUALS( false , s->getBoolean( "return" ) );
|
|
|
|
|
|
|
|
s->invoke( "function(){ return this.x == 17; }" , BSONObj() );
|
|
|
|
ASSERT_EQUALS( true , s->getBoolean( "return" ) );
|
2009-04-29 17:12:35 +02:00
|
|
|
|
2009-04-29 21:45:11 +02:00
|
|
|
s->invoke( "function(){ return this.x == 18; }" , BSONObj() );
|
|
|
|
ASSERT_EQUALS( false , s->getBoolean( "return" ) );
|
|
|
|
|
|
|
|
s->invoke( "function (){ return this.x == 17; }" , BSONObj() );
|
|
|
|
ASSERT_EQUALS( true , s->getBoolean( "return" ) );
|
2009-05-06 21:30:05 +02:00
|
|
|
|
|
|
|
s->invoke( "function z(){ return this.x == 18; }" , BSONObj() );
|
|
|
|
ASSERT_EQUALS( false , s->getBoolean( "return" ) );
|
2009-04-29 17:12:35 +02:00
|
|
|
|
2009-04-24 23:19:24 +02:00
|
|
|
delete s;
|
|
|
|
}
|
|
|
|
};
|
2009-05-06 17:53:39 +02:00
|
|
|
|
|
|
|
class ObjectDecoding {
|
|
|
|
public:
|
|
|
|
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 );
|
2009-05-06 18:48:19 +02:00
|
|
|
out = s->getObject( "blah" );
|
|
|
|
ASSERT_EQUALS( 17 , out["x"].number() );
|
|
|
|
|
2009-05-06 17:53:39 +02:00
|
|
|
delete s;
|
|
|
|
}
|
|
|
|
};
|
2009-04-24 04:44:19 +02:00
|
|
|
|
2009-05-08 17:02:12 +02:00
|
|
|
class JSOIDTests {
|
|
|
|
public:
|
|
|
|
void run(){
|
2009-05-08 23:04:07 +02:00
|
|
|
#ifdef MOZJS
|
2009-05-08 17:02:12 +02:00
|
|
|
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;
|
2009-05-08 23:22:01 +02:00
|
|
|
#endif
|
2009-05-08 17:02:12 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2009-02-03 06:14:14 +01:00
|
|
|
class All : public UnitTest::Suite {
|
|
|
|
public:
|
|
|
|
All() {
|
|
|
|
add< Fundamental >();
|
2009-04-24 04:44:19 +02:00
|
|
|
add< BasicScope >();
|
2009-04-24 05:52:47 +02:00
|
|
|
add< FalseTests >();
|
2009-04-24 22:41:40 +02:00
|
|
|
add< SimpleFunctions >();
|
2009-04-24 23:19:24 +02:00
|
|
|
add< ObjectMapping >();
|
2009-05-06 17:53:39 +02:00
|
|
|
add< ObjectDecoding >();
|
2009-05-08 17:02:12 +02:00
|
|
|
add< JSOIDTests >();
|
2009-02-03 06:14:14 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace JavaJSTests
|
|
|
|
|
2009-04-24 04:44:19 +02:00
|
|
|
UnitTest::TestPtr jsTests() {
|
|
|
|
return UnitTest::createSuite< JSTests::All >();
|
2009-02-03 06:14:14 +01:00
|
|
|
}
|