utest.cpp
Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2008, 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 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 /* Author: Brian Gerkey */
00031 
00032 #include <stdexcept> // for std::runtime_error
00033 #include <string>
00034 #include <vector>
00035 #include <stdlib.h>
00036 #include <unistd.h>
00037 #include <stdio.h>
00038 #include <time.h>
00039 #include <gtest/gtest.h>
00040 #include <boost/algorithm/string.hpp>
00041 #include <boost/filesystem.hpp>
00042 #include "rospack/rospack.h"
00043 #include "utils.h"
00044 
00045 
00046 TEST(rospack, reentrant)
00047 {
00048   rospack::ROSPack rp;
00049   std::string output;
00050   int ret = rp.run(std::string("plugins --attrib=foo --top=precedence1 roslang"));
00051   ASSERT_EQ(ret, 0);
00052   ret = rp.run(std::string("find roslang"));
00053   ASSERT_EQ(ret, 0);
00054   ret = rp.run(std::string("list-names"));
00055   ASSERT_EQ(ret, 0);
00056   std::vector<std::string> output_list;
00057   output = rp.getOutput();
00058   boost::trim(output);
00059   boost::split(output_list, output, boost::is_any_of("\n"));
00060   ASSERT_EQ((int)output_list.size(), 4);
00061   ret = rp.run(std::string("list"));
00062   ASSERT_EQ(ret, 0);
00063   output = rp.getOutput(); 
00064   boost::trim(output);
00065   boost::split(output_list, output, boost::is_any_of("\n"));
00066   ASSERT_EQ((int)output_list.size(), 4);
00067   std::vector<std::string> path_name;
00068   boost::split(path_name, output_list[0], boost::is_any_of(" "));
00069   ASSERT_EQ((int)path_name.size(), 2);
00070 }
00071 
00072 TEST(rospack, multiple_rospack_objects)
00073 {
00074   rospack::ROSPack rp;
00075   std::string output;
00076   int ret = rp.run(std::string("plugins --attrib=foo --top=precedence1 roslang"));
00077   ASSERT_EQ(ret, 0);
00078   ret = rp.run(std::string("find roslang"));
00079   ASSERT_EQ(ret, 0);
00080   ret = rp.run(std::string("list-names"));
00081   ASSERT_EQ(ret, 0);
00082   std::vector<std::string> output_list;
00083   output = rp.getOutput(); 
00084   boost::trim(output);
00085   boost::split(output_list, output, boost::is_any_of("\n"));
00086   ASSERT_EQ((int)output_list.size(), 4);
00087   ret = rp.run(std::string("list"));
00088   ASSERT_EQ(ret, 0);
00089   output = rp.getOutput(); 
00090   boost::trim(output);
00091   boost::split(output_list, output, boost::is_any_of("\n"));
00092   ASSERT_EQ((int)output_list.size(), 4);
00093   std::vector<std::string> path_name;
00094   boost::split(path_name, output_list[0], boost::is_any_of(" "));
00095   ASSERT_EQ((int)path_name.size(), 2);
00096 
00097   rospack::ROSPack rp2;
00098   ret = rp2.run(std::string("plugins --attrib=foo --top=precedence1 roslang"));
00099   ASSERT_EQ(ret, 0);
00100   ret = rp2.run(std::string("find roslang"));
00101   ASSERT_EQ(ret, 0);
00102   ret = rp2.run(std::string("list-names"));
00103   ASSERT_EQ(ret, 0);
00104   output_list.clear();
00105   output = rp2.getOutput(); 
00106   boost::trim(output);
00107   boost::split(output_list, output, boost::is_any_of("\n"));
00108   ASSERT_EQ((int)output_list.size(), 4);
00109   ret = rp2.run(std::string("list"));
00110   ASSERT_EQ(ret, 0);
00111   output = rp2.getOutput(); 
00112   boost::trim(output);
00113   boost::split(output_list, output, boost::is_any_of("\n"));
00114   ASSERT_EQ((int)output_list.size(), 4);
00115   path_name.clear();
00116   boost::split(path_name, output_list[0], boost::is_any_of(" "));
00117   ASSERT_EQ((int)path_name.size(), 2);
00118 }
00119 
00120 TEST(rospack, deduplicate_tokens)
00121 {
00122   std::string input = "foo foo bar bat bar foobar batbar";
00123   std::string truth = "foo bar bat foobar batbar";
00124   std::string output;
00125   rospack::deduplicate_tokens(input, false, output);
00126   ASSERT_EQ(truth, output);
00127 }
00128 
00129 int main(int argc, char **argv)
00130 {
00131   // Quiet some warnings
00132   (void)rospack::ROSPACK_NAME;
00133   (void)rospack::ROSSTACK_NAME;
00134 
00135   char buf[1024];
00136   std::string rr = std::string(getcwd(buf, sizeof(buf))) + "/test2";
00137   setenv("ROS_ROOT", rr.c_str(), 1);
00138   unsetenv("ROS_PACKAGE_PATH");
00139   char path[PATH_MAX];
00140   if(getcwd(path,sizeof(path)))
00141   {
00142     boost::filesystem::path p(path);
00143     p = p.parent_path();
00144     std::string newpath = p.string();
00145     char* oldpath = getenv("PATH");
00146     if(oldpath)
00147       newpath += std::string(":") + oldpath;
00148     setenv("PATH", newpath.c_str(), 1);
00149   }
00150 
00151   testing::InitGoogleTest(&argc, argv);
00152   return RUN_ALL_TESTS();
00153 }


rospack
Author(s): Brian Gerkey, Morgan Quigley, Dirk Thomas
autogenerated on Fri Aug 28 2015 12:41:42