00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #include "unit.hpp"
00020
00021 #include "operations_fixture.hpp"
00022 #include <scripting/Scripting.hpp>
00023 #include <scripting/ScriptingService.hpp>
00024 #include <extras/SequentialActivity.hpp>
00025 #include <plugin/PluginLoader.hpp>
00026 #include <scripting/Parser.hpp>
00027
00028 using namespace std;
00029 using namespace boost;
00030 using namespace RTT;
00031 using namespace RTT::detail;
00032
00033 #include <boost/shared_ptr.hpp>
00034
00035
00036
00037 BOOST_FIXTURE_TEST_SUITE( ScriptingTestSuite, OperationsFixture )
00038
00039
00040 BOOST_AUTO_TEST_CASE(TestGetProvider)
00041 {
00042
00043
00044 PluginLoader::Instance()->loadService("scripting",tc);
00045
00046
00047 tc->stop();
00048 BOOST_CHECK( tc->setActivity( new SequentialActivity() ) );
00049 tc->start();
00050
00051 boost::shared_ptr<Scripting> sc = tc->getProvider<Scripting>("scripting");
00052 BOOST_REQUIRE( sc );
00053 BOOST_CHECK ( sc->ready() );
00054 bool r;
00055 r = sc->loadProgramText("program Foo { do test.assert(true); set ret = 10.0; }");
00056 BOOST_REQUIRE( r );
00057 BOOST_CHECK( sc->hasProgram("Foo") );
00058 BOOST_REQUIRE( sc->startProgram("Foo") );
00059 BOOST_CHECK( sc->isProgramRunning("Foo") );
00060
00061
00062 tc->getActivity()->trigger();
00063
00064
00065 BOOST_CHECK( sc->isProgramRunning("Foo") == false );
00066 BOOST_CHECK( sc->inProgramError("Foo") == false );
00067 BOOST_CHECK( ret == 10.0 );
00068
00069 }
00070
00071 BOOST_AUTO_TEST_CASE(TestScriptingParser)
00072 {
00073 PluginLoader::Instance()->loadService("scripting",tc);
00074
00075
00076 tc->stop();
00077 BOOST_CHECK( tc->setActivity( new SequentialActivity() ) );
00078 tc->start();
00079
00080 boost::shared_ptr<Scripting> sc = tc->getProvider<Scripting>("scripting");
00081 BOOST_REQUIRE( sc );
00082 BOOST_CHECK ( sc->ready() );
00083 bool r;
00084
00085
00086 string statements=";;test.increase()\n;;;\ntest.increase()";
00087 r = sc->eval(statements);
00088 BOOST_CHECK( r );
00089 BOOST_CHECK_EQUAL( i, 1);
00090
00091
00092 statements="var int i = 0; var int y,z=10; test.i = z";
00093 r = sc->eval(statements);
00094 BOOST_CHECK( r );
00095 BOOST_CHECK_EQUAL( i, 10);
00096
00097
00098 statements="var int x=1,y=2; if 3 == 8 then test.i = x else test.i = y";
00099 r = sc->eval(statements);
00100 BOOST_CHECK( r );
00101 BOOST_CHECK_EQUAL( i, 2);
00102
00103
00104 statements="var int x=1,y=2; while x != y { test.i = 3; x = y; }";
00105 r = sc->eval(statements);
00106 BOOST_CHECK( r );
00107 BOOST_CHECK_EQUAL( i, 3);
00108
00109
00110 statements="var int x=10,y=20; for( x = 0; x != y; x = x + 1) { test.i = x; }";
00111 r = sc->eval(statements);
00112 BOOST_CHECK( r );
00113 BOOST_CHECK_EQUAL( i, 19);
00114
00115
00116 statements = "export function adder(int a, int b) { test.i = a + b; }\n adder(5,6)\n";
00117 r = sc->eval(statements);
00118 BOOST_CHECK( r );
00119 BOOST_CHECK_EQUAL( i, 11);
00120 statements = "export void adder2(int a, int b) { test.i = a + b; }\n adder2(7,8)\n";
00121 r = sc->eval(statements);
00122 BOOST_CHECK( r );
00123 BOOST_CHECK_EQUAL( i, 15);
00124 statements = "export int adder3(int a, int b) { return a + b; }\n test.i = adder3(6,10)\n";
00125 r = sc->eval(statements);
00126 BOOST_CHECK( r );
00127 BOOST_CHECK_EQUAL( i, 16);
00128
00129
00130 statements = "program rt_script { test.i = 3-9; }\n rt_script.start();;;; while( rt_script.isRunning() ) { trigger(); yield; }\n";
00131 r = sc->eval(statements);
00132 BOOST_CHECK( r );
00133 BOOST_CHECK_EQUAL( sc->getProgramStatus("rt_script"), ProgramInterface::Status::stopped );
00134 BOOST_CHECK_EQUAL( i, -6);
00135
00136
00137 statements = "StateMachine RTState { initial state init { entry { test.i = 0;} transitions { select fini; } } final state fini { entry { test.i = test.i - 2; } } }; RootMachine RTState rt_state; rt_state.activate(); rt_state.start(); while( !rt_state.inFinalState() ) { trigger(); yield; }\n";
00138 r = sc->eval(statements);
00139 BOOST_CHECK( r );
00140 BOOST_CHECK_EQUAL( sc->getStateMachineState("rt_state"), "fini" );
00141 BOOST_CHECK_EQUAL( i, -2);
00142 }
00143
00144
00145 BOOST_AUTO_TEST_SUITE_END()