00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030 #include <gtest/gtest.h>
00031 #include <rviz/config.h>
00032
00033 #include <ros/package.h>
00034
00035 TEST( Config, comparison )
00036 {
00037 std::string lhs = "b/c/d";
00038 std::string rhs = "b/c_d";
00039 rviz::Config::DirectoryCompare dc;
00040 EXPECT_FALSE( dc( lhs, rhs ));
00041 }
00042
00043 TEST( Config, set_then_get )
00044 {
00045 rviz::Config c;
00046 c.set( "a", "1" );
00047 int a;
00048 EXPECT_TRUE( c.get( "a", &a, 2 ));
00049 EXPECT_EQ( a, 1 );
00050 float aa;
00051 EXPECT_TRUE( c.get( "a", &aa, 2.0 ));
00052 EXPECT_EQ( aa, 1.0 );
00053 std::string aaa;
00054 EXPECT_TRUE( c.get( "a", &aaa, "two" ));
00055 EXPECT_EQ( aaa, "1" );
00056 }
00057
00058 TEST( Config, parse_floats )
00059 {
00060 rviz::Config c;
00061 c.set( "f", "1.1" );
00062 float f;
00063 EXPECT_TRUE( c.get( "f", &f, 2.0f ));
00064 EXPECT_EQ( f, 1.1f );
00065
00066
00067 c.set( "f", "1,2" );
00068 EXPECT_TRUE( c.get( "f", &f, 2.0f ));
00069 EXPECT_EQ( f, 1.2f );
00070
00071
00072
00073 std::string str;
00074 c.set( "f", 1.2f );
00075 c.get( "f", &str, "chub" );
00076 EXPECT_EQ( str, "1.2" );
00077 }
00078
00079 TEST( Config, default_values )
00080 {
00081 rviz::Config c;
00082 int a;
00083 EXPECT_FALSE( c.get( "a", &a, 2 ));
00084 EXPECT_EQ( a, 2 );
00085 EXPECT_FALSE( c.get( "a", &a ));
00086 EXPECT_EQ( a, 0 );
00087 float aa;
00088 EXPECT_FALSE( c.get( "a", &aa, 2.0 ));
00089 EXPECT_EQ( aa, 2.0 );
00090 EXPECT_FALSE( c.get( "a", &aa ));
00091 EXPECT_EQ( aa, 0.0 );
00092 std::string aaa;
00093 EXPECT_FALSE( c.get( "a", &aaa, "two" ));
00094 EXPECT_EQ( aaa, "two" );
00095 EXPECT_FALSE( c.get( "a", &aaa ));
00096 EXPECT_EQ( aaa, "" );
00097 }
00098
00099 TEST( Config, write_empty_string_value )
00100 {
00101 rviz::Config c;
00102 c.set( "key", "" );
00103
00104 std::stringstream ss;
00105 c.write( ss );
00106
00107 EXPECT_EQ( "key=\n", ss.str() );
00108 }
00109
00110 TEST( Config, read_empty_string_value )
00111 {
00112 std::istringstream ss( "key=\n" );
00113
00114 rviz::Config c;
00115 c.read( ss );
00116
00117 std::string s;
00118 EXPECT_TRUE( c.get( "key", &s, "default" ));
00119 EXPECT_EQ( "", s );
00120 }
00121
00122 TEST( Config, set_get_empty_value )
00123 {
00124 rviz::Config c;
00125 c.set( "key", "" );
00126
00127 std::string s;
00128 EXPECT_TRUE( c.get( "key", &s, "default" ));
00129 EXPECT_EQ( "", s );
00130 }
00131
00132 TEST( Config, write )
00133 {
00134 rviz::Config c;
00135 c.set( "/b/c_d", 1 );
00136 c.set( "b/alpha", 2 );
00137 c.set( "z", 6 );
00138 c.set( "/a/z", 3 );
00139 c.set( "d", 7 );
00140 c.set( "/b/c/d", 4);
00141 c.set( "/a", 5 );
00142 c.set( "/art/for/arts/sake", 8 );
00143 std::stringstream ss;
00144 c.write( ss );
00145 EXPECT_EQ(
00146 "a=5\n\
00147 d=7\n\
00148 z=6\n\
00149 [a]\n\
00150 z=3\n\
00151 [art]\n\
00152 [art/for]\n\
00153 [art/for/arts]\n\
00154 sake=8\n\
00155 [b]\n\
00156 alpha=2\n\
00157 c_d=1\n\
00158 [b/c]\n\
00159 d=4\n\
00160 ", ss.str() );
00161 }
00162
00163 TEST( Config, read )
00164 {
00165 std::string input =
00166 "a=1\n\
00167 foo_bar=biff=1; bazz=17\n\
00168 [chub]\n\
00169 sand=wich\n\
00170 [chub/town]\n\
00171 belly=big\n\
00172 tummy=large\n\
00173 ";
00174 std::istringstream iss;
00175 iss.str( input );
00176 rviz::Config c;
00177 c.read( iss );
00178
00179 std::string s;
00180 EXPECT_TRUE( c.get( "/chub/town/belly", &s ));
00181 EXPECT_EQ( "big", s );
00182
00183 EXPECT_TRUE( c.get( "/chub/sand", &s ));
00184 EXPECT_EQ( "wich", s );
00185
00186 EXPECT_TRUE( c.get( "/foo_bar", &s ));
00187 EXPECT_EQ( "biff=1; bazz=17", s );
00188
00189 int i;
00190 EXPECT_TRUE( c.get( "/a", &i ));
00191 EXPECT_EQ( 1, i );
00192 }
00193
00194 TEST( Config, escape_space )
00195 {
00196 rviz::Config c;
00197 c.set( "a b", "c d" );
00198 std::string s;
00199 EXPECT_TRUE( c.get( "a b", &s ));
00200 EXPECT_EQ( "c d", s );
00201
00202 std::stringstream ss;
00203 c.write( ss );
00204 EXPECT_EQ( "a\\ b=c d\n", ss.str() );
00205 }
00206
00207 TEST( Config, escape_colon )
00208 {
00209 rviz::Config c;
00210 c.set( "a:b", "c:d" );
00211 std::string s;
00212 EXPECT_TRUE( c.get( "a:b", &s ));
00213 EXPECT_EQ( "c:d", s );
00214
00215 std::stringstream ss;
00216 c.write( ss );
00217 EXPECT_EQ( "a\\:b=c:d\n", ss.str() );
00218 }
00219
00220 TEST( Config, first_slash_is_optional )
00221 {
00222 rviz::Config c;
00223 c.set( "a", 1 );
00224 int i;
00225 EXPECT_TRUE( c.get( "/a", &i ));
00226 EXPECT_EQ( 1, i );
00227
00228 c.set( "/b", 2 );
00229 EXPECT_TRUE( c.get( "b", &i ));
00230 EXPECT_EQ( 2, i );
00231 }
00232
00233 TEST( Config, end_to_end )
00234 {
00235 rviz::Config c;
00236 c.set( "/a: b/foo/bar", 1.25f );
00237 c.set( "/a: b/foo/biff", 17 );
00238 c.set( "a", "a/b=c d" );
00239
00240 std::stringstream ss;
00241 c.write( ss );
00242
00243 c.clear();
00244
00245 c.read( ss );
00246 float f;
00247 EXPECT_TRUE( c.get( "/a: b/foo/bar", &f ));
00248 EXPECT_EQ( 1.25, f );
00249 int i;
00250 EXPECT_TRUE( c.get( "/a: b/foo/biff", &i ));
00251 EXPECT_EQ( 17, i );
00252 std::string s;
00253 EXPECT_TRUE( c.get( "a", &s ));
00254 EXPECT_EQ( "a/b=c d", s );
00255 }
00256
00257 TEST( Config, file_io )
00258 {
00259 std::string package_path = ros::package::getPath(ROS_PACKAGE_NAME);
00260
00261 rviz::Config c;
00262 EXPECT_TRUE( c.readFromFile( package_path + "/src/test/test_in.vcg" ));
00263 EXPECT_TRUE( c.writeToFile( package_path + "/build/test_out.vcg" ));
00264
00265
00266
00267
00268 }
00269
00270 int main(int argc, char **argv){
00271 testing::InitGoogleTest(&argc, argv);
00272 return RUN_ALL_TESTS();
00273 }
00274