$search
00001 /* 00002 * Copyright (c) 2011, Willow Garage, Inc. 00003 * All rights reserved. 00004 * 00005 * Redistribution and use in source and binary forms, with or without 00006 * modification, are permitted provided that the following conditions are met: 00007 * 00008 * * Redistributions of source code must retain the above copyright 00009 * notice, this list of conditions and the following disclaimer. 00010 * * Redistributions in binary form must reproduce the above copyright 00011 * notice, this list of conditions and the following disclaimer in the 00012 * documentation and/or other materials provided with the distribution. 00013 * * Neither the name of the Willow Garage, Inc. nor the names of its 00014 * contributors may be used to endorse or promote products derived from 00015 * this software without specific prior written permission. 00016 * 00017 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 00018 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 00019 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 00020 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 00021 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 00022 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 00023 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 00024 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 00025 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 00026 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 00027 * POSSIBILITY OF SUCH DAMAGE. 00028 */ 00029 00030 #include <gtest/gtest.h> 00031 #include <rviz/config.h> 00032 00033 #include <ros/package.h> // For ros::package::getPath() 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, default_values ) 00059 { 00060 rviz::Config c; 00061 int a; 00062 EXPECT_FALSE( c.get( "a", &a, 2 )); 00063 EXPECT_EQ( a, 2 ); 00064 EXPECT_FALSE( c.get( "a", &a )); 00065 EXPECT_EQ( a, 0 ); 00066 float aa; 00067 EXPECT_FALSE( c.get( "a", &aa, 2.0 )); 00068 EXPECT_EQ( aa, 2.0 ); 00069 EXPECT_FALSE( c.get( "a", &aa )); 00070 EXPECT_EQ( aa, 0.0 ); 00071 std::string aaa; 00072 EXPECT_FALSE( c.get( "a", &aaa, "two" )); 00073 EXPECT_EQ( aaa, "two" ); 00074 EXPECT_FALSE( c.get( "a", &aaa )); 00075 EXPECT_EQ( aaa, "" ); 00076 } 00077 00078 TEST( Config, write_empty_string_value ) 00079 { 00080 rviz::Config c; 00081 c.set( "key", "" ); 00082 00083 std::stringstream ss; 00084 c.write( ss ); 00085 00086 EXPECT_EQ( "key=\n", ss.str() ); 00087 } 00088 00089 TEST( Config, read_empty_string_value ) 00090 { 00091 std::istringstream ss( "key=\n" ); 00092 00093 rviz::Config c; 00094 c.read( ss ); 00095 00096 std::string s; 00097 EXPECT_TRUE( c.get( "key", &s, "default" )); 00098 EXPECT_EQ( "", s ); 00099 } 00100 00101 TEST( Config, set_get_empty_value ) 00102 { 00103 rviz::Config c; 00104 c.set( "key", "" ); 00105 00106 std::string s; 00107 EXPECT_TRUE( c.get( "key", &s, "default" )); 00108 EXPECT_EQ( "", s ); 00109 } 00110 00111 TEST( Config, write ) 00112 { 00113 rviz::Config c; 00114 c.set( "/b/c_d", 1 ); 00115 c.set( "b/alpha", 2 ); 00116 c.set( "z", 6 ); 00117 c.set( "/a/z", 3 ); 00118 c.set( "d", 7 ); 00119 c.set( "/b/c/d", 4); 00120 c.set( "/a", 5 ); 00121 c.set( "/art/for/arts/sake", 8 ); 00122 std::stringstream ss; 00123 c.write( ss ); 00124 EXPECT_EQ( 00125 "a=5\n\ 00126 d=7\n\ 00127 z=6\n\ 00128 [a]\n\ 00129 z=3\n\ 00130 [art]\n\ 00131 [art/for]\n\ 00132 [art/for/arts]\n\ 00133 sake=8\n\ 00134 [b]\n\ 00135 alpha=2\n\ 00136 c_d=1\n\ 00137 [b/c]\n\ 00138 d=4\n\ 00139 ", ss.str() ); 00140 } 00141 00142 TEST( Config, read ) 00143 { 00144 std::string input = 00145 "a=1\n\ 00146 foo_bar=biff=1; bazz=17\n\ 00147 [chub]\n\ 00148 sand=wich\n\ 00149 [chub/town]\n\ 00150 belly=big\n\ 00151 tummy=large\n\ 00152 "; 00153 std::istringstream iss; 00154 iss.str( input ); 00155 rviz::Config c; 00156 c.read( iss ); 00157 00158 std::string s; 00159 EXPECT_TRUE( c.get( "/chub/town/belly", &s )); 00160 EXPECT_EQ( "big", s ); 00161 00162 EXPECT_TRUE( c.get( "/chub/sand", &s )); 00163 EXPECT_EQ( "wich", s ); 00164 00165 EXPECT_TRUE( c.get( "/foo_bar", &s )); 00166 EXPECT_EQ( "biff=1; bazz=17", s ); 00167 00168 int i; 00169 EXPECT_TRUE( c.get( "/a", &i )); 00170 EXPECT_EQ( 1, i ); 00171 } 00172 00173 TEST( Config, escape_space ) 00174 { 00175 rviz::Config c; 00176 c.set( "a b", "c d" ); 00177 std::string s; 00178 EXPECT_TRUE( c.get( "a b", &s )); 00179 EXPECT_EQ( "c d", s ); 00180 00181 std::stringstream ss; 00182 c.write( ss ); 00183 EXPECT_EQ( "a\\ b=c d\n", ss.str() ); 00184 } 00185 00186 TEST( Config, escape_colon ) 00187 { 00188 rviz::Config c; 00189 c.set( "a:b", "c:d" ); 00190 std::string s; 00191 EXPECT_TRUE( c.get( "a:b", &s )); 00192 EXPECT_EQ( "c:d", s ); 00193 00194 std::stringstream ss; 00195 c.write( ss ); 00196 EXPECT_EQ( "a\\:b=c:d\n", ss.str() ); 00197 } 00198 00199 TEST( Config, first_slash_is_optional ) 00200 { 00201 rviz::Config c; 00202 c.set( "a", 1 ); 00203 int i; 00204 EXPECT_TRUE( c.get( "/a", &i )); 00205 EXPECT_EQ( 1, i ); 00206 00207 c.set( "/b", 2 ); 00208 EXPECT_TRUE( c.get( "b", &i )); 00209 EXPECT_EQ( 2, i ); 00210 } 00211 00212 TEST( Config, end_to_end ) 00213 { 00214 rviz::Config c; 00215 c.set( "/a: b/foo/bar", 1.25f ); 00216 c.set( "/a: b/foo/biff", 17 ); 00217 c.set( "a", "a/b=c d" ); 00218 00219 std::stringstream ss; 00220 c.write( ss ); 00221 00222 c.clear(); 00223 00224 c.read( ss ); 00225 float f; 00226 EXPECT_TRUE( c.get( "/a: b/foo/bar", &f )); 00227 EXPECT_EQ( 1.25, f ); // I don't need approximately-equal because I chose a floating-point-friendly number. 00228 int i; 00229 EXPECT_TRUE( c.get( "/a: b/foo/biff", &i )); 00230 EXPECT_EQ( 17, i ); 00231 std::string s; 00232 EXPECT_TRUE( c.get( "a", &s )); 00233 EXPECT_EQ( "a/b=c d", s ); 00234 } 00235 00236 TEST( Config, file_io ) 00237 { 00238 std::string package_path = ros::package::getPath(ROS_PACKAGE_NAME); 00239 00240 rviz::Config c; 00241 EXPECT_TRUE( c.readFromFile( package_path + "/src/test/test_in.vcg" )); 00242 EXPECT_TRUE( c.writeToFile( package_path + "/build/test_out.vcg" )); 00243 00244 // This test would be better if it sorted the input and output files 00245 // and checked that the results were the same. I don't care enough 00246 // to learn how to do that with gtest right now. 00247 } 00248 00249 int main(int argc, char **argv){ 00250 testing::InitGoogleTest(&argc, argv); 00251 return RUN_ALL_TESTS(); 00252 } 00253